From 94805a944d75be5084ae6f268cd576e0a34063f0 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Mon, 19 Apr 2004 19:03:13 +0000 Subject: [PATCH] testing template explicit specialization & small fix --- core/org.eclipse.cdt.core.tests/ChangeLog | 4 + .../tests/CompleteParseASTTemplateTest.java | 32 +++++ .../tests/ParserSymbolTableTemplateTests.java | 120 +++++++++--------- .../parser/ChangeLog-parser | 3 + .../complete/ASTTemplateSpecialization.java | 14 ++ .../core/parser/pst/TemplateFactory.java | 25 +--- 6 files changed, 120 insertions(+), 78 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/ChangeLog b/core/org.eclipse.cdt.core.tests/ChangeLog index ba27bcc31bd..52c2eb6ec98 100644 --- a/core/org.eclipse.cdt.core.tests/ChangeLog +++ b/core/org.eclipse.cdt.core.tests/ChangeLog @@ -1,3 +1,7 @@ +2004-04-19 Andrew Niefer + added parser/CompleteParseASTTemplateTest.test_14_7_3__5_ExplicitSpecialization() + uncommented & modified parser/ParserSymbolTableTemplateTests.test_14_7_3__5_ExplicitSpecialization() + 2004-04-15 Andrew Niefer added parser/org/eclipse/cdt/core/parser/tests/CompletionParseTest.testBug58492() diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTemplateTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTemplateTest.java index 73c3c9cc59f..684c644f856 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTemplateTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTemplateTest.java @@ -755,4 +755,36 @@ public class CompleteParseASTTemplateTest extends CompleteParseBaseTest { IASTTemplateDeclaration template = (IASTTemplateDeclaration) i.next(); IASTTemplateDeclaration temp2 = (IASTTemplateDeclaration) i.next(); } + + public void test_14_7_3__5_ExplicitSpecialization() throws Exception + { + Writer writer = new StringWriter(); + writer.write("template< class T > struct A { "); + writer.write(" void f( T ) {} "); + writer.write("}; "); + writer.write("template <> struct A< int >{ "); + writer.write(" void f( int ); "); + writer.write("}; "); + writer.write("void A< int >::f( int ){ } "); + + writer.write("void main(){ "); + writer.write(" A a; "); + writer.write(" a.f( 1 ); "); + writer.write("} "); + + Iterator i = parse( writer.toString() ).getDeclarations(); + + IASTTemplateDeclaration template = (IASTTemplateDeclaration) i.next(); + IASTTemplateParameter T = (IASTTemplateParameter) template.getTemplateParameters().next(); + IASTTemplateSpecialization spec = (IASTTemplateSpecialization) i.next(); + IASTMethod f = (IASTMethod) i.next(); + IASTFunction main = (IASTFunction) i.next(); + + IASTClassSpecifier ASpec = (IASTClassSpecifier) spec.getOwnedDeclaration(); + + i = getDeclarations( main ); + IASTVariable a = (IASTVariable) i.next(); + + assertAllReferences( 5, createTaskList( new Task( T ), new Task( ASpec, 2 ), new Task( a ), new Task( f ) ) ); + } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserSymbolTableTemplateTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserSymbolTableTemplateTests.java index 26102c6dea7..9a0ae049a53 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserSymbolTableTemplateTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserSymbolTableTemplateTests.java @@ -1820,66 +1820,63 @@ public class ParserSymbolTableTemplateTests extends TestCase { * @throws Exception */ public void test_14_7_3__5_ExplicitSpecialization() throws Exception{ - //TODO -// newTable(); -// -// ITemplateSymbol template = table.newTemplateSymbol( "A" ); -// ISymbol T = table.newSymbol( "T", TypeInfo.t_templateParameter ); -// template.addTemplateParameter( T ); -// -// ITemplateFactory factory = table.newTemplateFactory(); -// factory.setContainingSymbol( table.getCompilationUnit() ); -// factory.pushTemplate( template ); -// -// IDerivableContainerSymbol A = table.newDerivableContainerSymbol( "A", TypeInfo.t_struct ); -// factory.addSymbol( A ); -// -// IParameterizedSymbol f = table.newParameterizedSymbol( "f", TypeInfo.t_function ); -// f.addParameter( T, 0, null, false ); -// -// A.addSymbol( f ); -// -// ITemplateSymbol temp = table.newTemplateSymbol( "" ); -// factory = table.newTemplateFactory(); -// factory.setContainingSymbol( table.getCompilationUnit() ); -// factory.pushTemplate( temp ); -// -// LinkedList params = new LinkedList(), args = new LinkedList(); -// -// args.add( new TypeInfo( TypeInfo.t_int, 0, null ) ); -// -// ITemplateFactory factory = table.getCompilationUnit().lookupTemplateForMemberDefinition( "A", params, args ); -// -// IDerivableContainerSymbol ASpec = table.newDerivableContainerSymbol( "A", TypeInfo.t_struct ); -// ASpec.setIsTemplateMember( true ); -// IParameterizedSymbol f2 = table.newParameterizedSymbol( "f", TypeInfo.t_function ); -// f2.addParameter( TypeInfo.t_int, 0, null, false ); -// f2.setIsForwardDeclaration( true ); -// ASpec.addSymbol( f2 ); -// -// factory.addSymbol( ASpec ); -// -// IParameterizedSymbol f3 = table.newParameterizedSymbol( "f", TypeInfo.t_function ); -// f3.addParameter( TypeInfo.t_int, 0, null, false ); -// -// IDerivableContainerSymbol look = (IDerivableContainerSymbol) table.getCompilationUnit().lookupTemplateId( "A", args ); -// assertTrue( look.isTemplateInstance() ); -// assertEquals( look.getInstantiatedSymbol(), ASpec ); -// -// -// ISymbol flook = look.lookupMethodForDefinition( "f", args ); -// assertTrue( flook.isTemplateInstance() ); -// assertEquals( flook.getInstantiatedSymbol(), f2 ); -// flook.setTypeSymbol( f3 ); -// -// look.addSymbol( f3 ); -// -// -// look = (IDerivableContainerSymbol) table.getCompilationUnit().lookupTemplateId( "A", args ); -// flook = look.qualifiedFunctionLookup( "f", args ); -// -// assertEquals( flook, f3 ); + newTable(); + ITemplateSymbol template = table.newTemplateSymbol( "A" ); + ISymbol T = table.newSymbol( "T", TypeInfo.t_templateParameter ); + template.addTemplateParameter( T ); + + ITemplateFactory factory = table.newTemplateFactory(); + factory.setContainingSymbol( table.getCompilationUnit() ); + factory.pushTemplate( template ); + + IDerivableContainerSymbol A = table.newDerivableContainerSymbol( "A", TypeInfo.t_struct ); + factory.addSymbol( A ); + + IParameterizedSymbol f = table.newParameterizedSymbol( "f", TypeInfo.t_function ); + f.addParameter( T, 0, null, false ); + + A.addSymbol( f ); + + ITemplateSymbol temp = table.newTemplateSymbol( "" ); + factory = table.newTemplateFactory(); + factory.setContainingSymbol( table.getCompilationUnit() ); + factory.pushTemplate( temp ); + + LinkedList params = new LinkedList(), args = new LinkedList(); + + factory = table.newTemplateFactory(); + factory.setContainingSymbol( table.getCompilationUnit() ); + ITemplateSymbol templateSpec = table.newTemplateSymbol( ParserSymbolTable.EMPTY_NAME ); + factory.pushTemplate( templateSpec ); + + IDerivableContainerSymbol ASpec = table.newDerivableContainerSymbol( "A", TypeInfo.t_struct ); + args.add( new TypeInfo( TypeInfo.t_int, 0, null ) ); + factory.addTemplateId( ASpec, args ); + + IParameterizedSymbol f2 = table.newParameterizedSymbol( "f", TypeInfo.t_function ); + f2.addParameter( TypeInfo.t_int, 0, null, false ); + f2.setIsForwardDeclaration( true ); + ASpec.addSymbol( f2 ); + + IParameterizedSymbol f3 = table.newParameterizedSymbol( "f", TypeInfo.t_function ); + f3.addParameter( TypeInfo.t_int, 0, null, false ); + + IDerivableContainerSymbol look = (IDerivableContainerSymbol) table.getCompilationUnit().lookupTemplateId( "A", args ); + assertTrue( look.isTemplateInstance() ); + assertEquals( look.getInstantiatedSymbol(), ASpec ); + + ISymbol flook = look.lookupMethodForDefinition( "f", args ); + assertTrue( flook.isTemplateInstance() ); + assertEquals( flook.getInstantiatedSymbol(), f2 ); + flook.setTypeSymbol( f3 ); + + look.addSymbol( f3 ); + + look = (IDerivableContainerSymbol) table.getCompilationUnit().lookupTemplateId( "A", args ); + flook = look.qualifiedFunctionLookup( "f", args ); + + assertEquals( flook, f3 ); } /** @@ -1991,8 +1988,11 @@ public class ParserSymbolTableTemplateTests extends TestCase { // // List params = new LinkedList(); // -// ITemplateFactory factory = table.getCompilationUnit().lookupTemplateForMemberDefinition( "f", params, null ); +// ITemplateFactory factory = table.newTemplateFactory(); +// ITemplateSymbol template = table.newTemplateSymbol( ParserSymbolTable.EMPTY_NAME ); // +// factory.pushTemplate( template ); +// // IParameterizedSymbol f3 = table.newParameterizedSymbol( "f", TypeInfo.t_function ); // f3.addParameter( TypeInfo.t_int, 0, new PtrOp( PtrOp.t_pointer ), false ); // diff --git a/core/org.eclipse.cdt.core/parser/ChangeLog-parser b/core/org.eclipse.cdt.core/parser/ChangeLog-parser index 49a27b73023..b493b794b1c 100644 --- a/core/org.eclipse.cdt.core/parser/ChangeLog-parser +++ b/core/org.eclipse.cdt.core/parser/ChangeLog-parser @@ -1,3 +1,6 @@ +2004-04-19 Andrew Niefer + fix problems with explicit specialization + 2004-04-16 Andrew Niefer refactor ASTNode.lookup, partially addressing bug 50807 diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTTemplateSpecialization.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTTemplateSpecialization.java index 5c282c8cb33..ada3c3fac7c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTTemplateSpecialization.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTTemplateSpecialization.java @@ -10,8 +10,10 @@ ***********************************************************************/ package org.eclipse.cdt.internal.core.parser.ast.complete; +import org.eclipse.cdt.core.parser.ast.IASTDeclaration; import org.eclipse.cdt.core.parser.ast.IASTScope; import org.eclipse.cdt.core.parser.ast.IASTTemplateSpecialization; +import org.eclipse.cdt.internal.core.parser.pst.ISymbol; import org.eclipse.cdt.internal.core.parser.pst.ITemplateSymbol; /** @@ -20,6 +22,7 @@ import org.eclipse.cdt.internal.core.parser.pst.ITemplateSymbol; */ public class ASTTemplateSpecialization extends ASTTemplateDeclaration implements IASTTemplateSpecialization { + private ISymbol owned = null; /** * */ @@ -28,4 +31,15 @@ public class ASTTemplateSpecialization extends ASTTemplateDeclaration implements super(template, scope, null); } + public IASTDeclaration getOwnedDeclaration() + { + if( owned != null && owned.getASTExtension() != null ) + return owned.getASTExtension().getPrimaryDeclaration(); + + return null; + } + + public void setOwnedDeclaration(ISymbol symbol) { + owned = symbol; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/pst/TemplateFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/pst/TemplateFactory.java index 6cda9fe6a4e..15cfab215f8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/pst/TemplateFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/pst/TemplateFactory.java @@ -18,9 +18,9 @@ import java.util.ListIterator; import java.util.Map; import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility; -import org.eclipse.cdt.core.parser.ast.IASTScope; import org.eclipse.cdt.internal.core.parser.ast.complete.ASTTemplateDeclaration; import org.eclipse.cdt.internal.core.parser.ast.complete.ASTTemplateInstantiation; +import org.eclipse.cdt.internal.core.parser.ast.complete.ASTTemplateSpecialization; import org.eclipse.cdt.internal.core.parser.pst.TypeInfo.PtrOp; import org.eclipse.cdt.internal.core.parser.pst.TypeInfo.eType; @@ -72,6 +72,7 @@ public class TemplateFactory extends ExtensibleSymbol implements ITemplateFactor } else if( params.size() == 0 ){ //explicit specialization addExplicitSpecialization( origTemplate, symbol, args ); + } else { //partial speciailization ISpecializedSymbol spec = template.getSymbolTable().newSpecializedSymbol( symbol.getName() ); @@ -286,24 +287,12 @@ public class TemplateFactory extends ExtensibleSymbol implements ITemplateFactor } } private void addExplicitSpecialization( ITemplateSymbol template, ISymbol symbol, List arguments ) throws ParserSymbolTableException { - Iterator templatesIter = templates.iterator(); - Iterator argsIter = arguments.iterator(); + template.addExplicitSpecialization( symbol, arguments ); -// while( templatesIter.hasNext() ){ -// ITemplateSymbol template = (ITemplateSymbol)templatesIter.next(); - - template.addExplicitSpecialization( symbol, arguments ); - //} - -// if( getTemplateFunctions() != null && argsIter.hasNext() ){ -// List args = (List) argsIter.next(); -// ITemplateSymbol template = TemplateEngine.resolveTemplateFunctions( getTemplateFunctions(), args, symbol ); -// if( template != null ){ -// template.addExplicitSpecialization( symbol, args ); -// } else { -// throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplate ); -// } -// } + if( getASTExtension() != null ){ + ASTTemplateSpecialization spec = (ASTTemplateSpecialization) getASTExtension().getPrimaryDeclaration(); + spec.setOwnedDeclaration( symbol ); + } } private IContainerSymbol getLastSymbol() {