From 63ed749357ed988bf6ae697762eddfe2f37ae0bb Mon Sep 17 00:00:00 2001 From: John Camelon Date: Fri, 5 Sep 2003 15:23:17 +0000 Subject: [PATCH] Patch for Hoda Amer - Added references to variables in solution of bug#42453:Expression result types not computed - Solution to bug#42560: Class Cast Exception during Method definition --- .../parser/tests/CompleteParseASTTest.java | 64 ++++-- core/org.eclipse.cdt.core/ChangeLog | 6 +- .../ast/complete/CompleteParseASTFactory.java | 209 +++++++++++------- 3 files changed, 178 insertions(+), 101 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java index 85408f7ee2f..567260f0b4b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java @@ -27,6 +27,7 @@ import org.eclipse.cdt.core.parser.ast.IASTEnumerator; import org.eclipse.cdt.core.parser.ast.IASTField; import org.eclipse.cdt.core.parser.ast.IASTFieldReference; import org.eclipse.cdt.core.parser.ast.IASTFunction; +import org.eclipse.cdt.core.parser.ast.IASTFunctionReference; import org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification; import org.eclipse.cdt.core.parser.ast.IASTMethod; import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition; @@ -525,21 +526,26 @@ public class CompleteParseASTTest extends CompleteParseBaseTest public void testQualifiedNameReferences() throws Exception { - Iterator i = parse( "class A{ class B{ class C { public: int cMethod(); }; }; }; \n int A::B::C::cMethod() {}; \n" ).getDeclarations(); - IASTClassSpecifier classA = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)i.next()).getTypeSpecifier(); - Iterator j = getDeclarations(classA); - IASTClassSpecifier classB = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)j.next()).getTypeSpecifier(); - Iterator k = getDeclarations(classB); - IASTClassSpecifier classC = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)k.next()).getTypeSpecifier(); - - // Note : this used to be considered a function, not a method - IASTMethod method = (IASTMethod)i.next(); - - assertEquals( callback.getReferences().size(), 3 ); - Iterator references = callback.getReferences().iterator(); - assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classA ); - assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classB ); - assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classC ); + try { // This is to prove that there are no exceptions + // Used to cause AST Semantic exception + Iterator i = parse( "class A{ class B{ class C { public: int cMethod(); }; }; }; \n int A::B::C::cMethod() {}; \n" ).getDeclarations(); + IASTClassSpecifier classA = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)i.next()).getTypeSpecifier(); + Iterator j = getDeclarations(classA); + IASTClassSpecifier classB = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)j.next()).getTypeSpecifier(); + Iterator k = getDeclarations(classB); + IASTClassSpecifier classC = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)k.next()).getTypeSpecifier(); + + // Note : this used to be considered a function, not a method + IASTMethod method = (IASTMethod)i.next(); + + assertEquals( callback.getReferences().size(), 3 ); + Iterator references = callback.getReferences().iterator(); + assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classA ); + assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classB ); + assertEquals( ((IASTClassReference)references.next()).getReferencedElement(), classC ); + }catch (Exception e){ + fail(); + } } public void testIsConstructor() throws Exception @@ -619,4 +625,32 @@ public class CompleteParseASTTest extends CompleteParseBaseTest IASTField charA = (IASTField)sub.next(); } + public void testExpressionResultValueWithSimpleTypes() throws Exception + { + Iterator i = parse ("int f(int, int); \n int f(int); \n int x = f(1, 2+3);").getDeclarations(); + IASTFunction f1 = (IASTFunction) i.next(); + IASTFunction f2 = (IASTFunction) i.next(); + IASTVariable x = (IASTVariable) i.next(); + Iterator references = callback.getReferences().iterator(); + IASTFunctionReference fr1 = (IASTFunctionReference) references.next(); + assertEquals( fr1.getReferencedElement(), f1 ); + + } + + public void testExpressionResultValueWithReferenceTypes() throws Exception + { + Iterator i = parse ("class A{}a; \n int f(A a); \n int x = f(a);").getDeclarations(); + IASTVariable a = (IASTVariable) i.next(); + IASTFunction f1 = (IASTFunction) i.next(); + IASTVariable x = (IASTVariable) i.next(); + Iterator references = callback.getReferences().iterator(); + IASTClassReference clr1 = (IASTClassReference) references.next(); + IASTFunctionReference fr1 = (IASTFunctionReference) references.next(); + IASTVariableReference ar1 = (IASTVariableReference) references.next(); + IASTFunctionReference fr2 = (IASTFunctionReference) references.next(); + assertEquals( ar1.getReferencedElement(), a ); + assertEquals( fr2.getReferencedElement(), f1 ); + + } + } diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index cbaf4dbeb97..cf33a14f67d 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,7 @@ +2003-09-04 Hoda Amer + - Added references to variables in solution of bug#42453:Expression result types not computed + - Solution to bug#42560: Class Cast Exception during Method definition + 2003-09-04 Alain Magloire The IProgressMonitor.setCancelled() is incorrect, it tries to access @@ -16,7 +20,7 @@ whole ITOkenDuple for the typeId instead of just the string. - Changed the ASTExpression in both quick and complete packages and deleted the "id" parameter. - - Added partial solution to bug #42453: Exception result types not computed. + - Added partial solution to bug #42453:Expression result types not computed. Now they are computed for simple types only. 2003-09-03 David Inglis diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java index 386e1ff98d3..041d7f1b3f9 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java @@ -61,6 +61,7 @@ import org.eclipse.cdt.core.parser.ast.IASTExpression.IASTNewExpressionDescripto import org.eclipse.cdt.core.parser.ast.IASTExpression.Kind; import org.eclipse.cdt.core.parser.ast.IASTSimpleTypeSpecifier.Type; import org.eclipse.cdt.core.parser.ast.IASTTemplateParameter.ParamKind; +import org.eclipse.cdt.internal.core.parser.ast.ASTParameterDeclaration; import org.eclipse.cdt.internal.core.parser.ast.BaseASTFactory; import org.eclipse.cdt.internal.core.parser.pst.ForewardDeclaredSymbolExtension; import org.eclipse.cdt.internal.core.parser.pst.IContainerSymbol; @@ -718,24 +719,26 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto IContainerSymbol startingScope = scopeToSymbol( scope ); //look up typeId & add to references - if( typeId != null ) - lookupQualifiedName( startingScope, typeId, references, false ); + ISymbol symbol = null; + if( typeId != null ){ + symbol = lookupQualifiedName( startingScope, typeId, references, false ); + } if (kind == IASTExpression.Kind.POSTFIX_FUNCTIONCALL){ ITokenDuple functionId = ((ASTExpression)lhs).getTypeId(); List parameters = ((ASTExpression)rhs).getResultType(); - lookupQualifiedName(startingScope, functionId, TypeInfo.t_function, parameters, references, false); + symbol = lookupQualifiedName(startingScope, functionId, TypeInfo.t_function, parameters, references, false); } ASTExpression expression = new ASTExpression( kind, lhs, rhs, thirdExpression, typeId, literal, newDescriptor, references); - expression.setResultType (getExpressionResultType(expression)); + expression.setResultType (getExpressionResultType(expression, symbol)); return expression; } - protected List getExpressionResultType(IASTExpression expression){ + protected List getExpressionResultType(IASTExpression expression, ISymbol symbol){ List result = new ArrayList(); if (expression.getExpressionKind() == IASTExpression.Kind.PRIMARY_EMPTY) { @@ -768,6 +771,14 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto result.add(info); return result; } + if (expression.getExpressionKind() == IASTExpression.Kind.ID_EXPRESSION){ + TypeInfo info = new TypeInfo(); + info.setType(TypeInfo.t_type); + if(symbol != null) + info.setTypeSymbol(symbol); + result.add(info); + return result; + } if ((expression.getExpressionKind() == IASTExpression.Kind.ADDITIVE_PLUS) || (expression.getExpressionKind() == IASTExpression.Kind.ADDITIVE_MINUS) ){ ASTExpression right = (ASTExpression)expression.getLHSExpression(); @@ -798,9 +809,11 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto return result; } if(expression.getExpressionKind() == IASTExpression.Kind.POSTFIX_FUNCTIONCALL){ - TypeInfo type = new TypeInfo(); - type.setType(TypeInfo.t_function); - result.add(type); + TypeInfo info = new TypeInfo(); + info.setType(TypeInfo.t_function); + if(symbol != null) + info.setTypeSymbol(symbol); + result.add(info); return result; } return result; @@ -1012,8 +1025,16 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto if((parentScope != null) && (parentScope.getType() == TypeInfo.t_class)){ // find out the visibility of the method's declaration List functionReferences = new ArrayList(); + List functionParameters = new LinkedList(); + // the lookup requires a list of type infos + // instead of a list of IASTParameterDeclaration + Iterator p = parameters.iterator(); + while (p.hasNext()){ + ASTParameterDeclaration param = (ASTParameterDeclaration)p.next(); + functionParameters.add(getParameterTypeInfo(param)); + } IParameterizedSymbol methodDeclaration = - (IParameterizedSymbol) lookupQualifiedName(parentScope, functionName, TypeInfo.t_function, parameters, 0, functionReferences, false); + (IParameterizedSymbol) lookupQualifiedName(parentScope, functionName, TypeInfo.t_function, functionParameters, 0, functionReferences, false); if(methodDeclaration != null){ ASTMethodReference reference = (ASTMethodReference) functionReferences.iterator().next(); visibility = ((IASTMethod)reference.getReferencedElement()).getVisiblity(); @@ -1074,92 +1095,108 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto } } - /** - * @param symbol - * @param returnType - */ - protected void setParameter(IParameterizedSymbol symbol, IASTAbstractDeclaration absDecl, boolean isParameter, List references) throws ASTSemanticException - { - if (absDecl.getTypeSpecifier() == null) - return; - - TypeInfo.eType type = null; - ISymbol xrefSymbol = null; - List newReferences = null; - if( absDecl.getTypeSpecifier() instanceof IASTSimpleTypeSpecifier ) - { - IASTSimpleTypeSpecifier.Type kind = ((IASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getType(); - if( kind == IASTSimpleTypeSpecifier.Type.BOOL ) - type = TypeInfo.t_bool; - else if( kind == IASTSimpleTypeSpecifier.Type.CHAR ) - type = TypeInfo.t_char; - else if( kind == IASTSimpleTypeSpecifier.Type.DOUBLE ) - type = TypeInfo.t_double; - else if( kind == IASTSimpleTypeSpecifier.Type.FLOAT ) - type = TypeInfo.t_float; - else if( kind == IASTSimpleTypeSpecifier.Type.INT ) - type = TypeInfo.t_int; - else if( kind == IASTSimpleTypeSpecifier.Type.VOID ) - type = TypeInfo.t_void; - else if( kind == IASTSimpleTypeSpecifier.Type.WCHAR_T) - type = TypeInfo.t_wchar_t; - else if( kind == IASTSimpleTypeSpecifier.Type.CLASS_OR_TYPENAME ) - { - type = TypeInfo.t_type; - xrefSymbol = ((ASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getSymbol(); - newReferences = ((ASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getReferences(); - } - else - throw new ASTSemanticException(); - } - else if( absDecl.getTypeSpecifier() instanceof IASTClassSpecifier ) - { - ASTClassKind kind = ((IASTClassSpecifier)absDecl.getTypeSpecifier()).getClassKind(); - if( kind == ASTClassKind.CLASS ) - type = TypeInfo.t_class; - else if( kind == ASTClassKind.STRUCT ) - type = TypeInfo.t_struct; + protected TypeInfo getParameterTypeInfo( IASTAbstractDeclaration absDecl)throws ASTSemanticException{ + TypeInfo type = new TypeInfo(); + if( absDecl.getTypeSpecifier() instanceof IASTSimpleTypeSpecifier ) + { + IASTSimpleTypeSpecifier.Type kind = ((IASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getType(); + if( kind == IASTSimpleTypeSpecifier.Type.BOOL ) + type.setType(TypeInfo.t_bool); + else if( kind == IASTSimpleTypeSpecifier.Type.CHAR ) + type.setType(TypeInfo.t_char); + else if( kind == IASTSimpleTypeSpecifier.Type.DOUBLE ) + type.setType(TypeInfo.t_double); + else if( kind == IASTSimpleTypeSpecifier.Type.FLOAT ) + type.setType(TypeInfo.t_float); + else if( kind == IASTSimpleTypeSpecifier.Type.INT ) + type.setType(TypeInfo.t_int); + else if( kind == IASTSimpleTypeSpecifier.Type.VOID ) + type.setType(TypeInfo.t_void); + else if( kind == IASTSimpleTypeSpecifier.Type.WCHAR_T) + type.setType(TypeInfo.t_wchar_t); + else if( kind == IASTSimpleTypeSpecifier.Type.CLASS_OR_TYPENAME ) + type.setType(TypeInfo.t_type); + else + throw new ASTSemanticException(); + } + else if( absDecl.getTypeSpecifier() instanceof IASTClassSpecifier ) + { + ASTClassKind kind = ((IASTClassSpecifier)absDecl.getTypeSpecifier()).getClassKind(); + if( kind == ASTClassKind.CLASS ) + type.setType(TypeInfo.t_class); + else if( kind == ASTClassKind.STRUCT ) + type.setType(TypeInfo.t_struct); else if( kind == ASTClassKind.UNION ) - type = TypeInfo.t_union; + type.setType(TypeInfo.t_union); else throw new ASTSemanticException(); - } - else if( absDecl.getTypeSpecifier() instanceof IASTEnumerationSpecifier ) - { - type = TypeInfo.t_enumeration; - } - else if( absDecl.getTypeSpecifier() instanceof IASTElaboratedTypeSpecifier ) - { + } + else if( absDecl.getTypeSpecifier() instanceof IASTEnumerationSpecifier ) + { + type.setType(TypeInfo.t_enumeration); + } + else if( absDecl.getTypeSpecifier() instanceof IASTElaboratedTypeSpecifier ) + { ASTClassKind kind = ((IASTElaboratedTypeSpecifier)absDecl.getTypeSpecifier()).getClassKind(); if( kind == ASTClassKind.CLASS ) - type = TypeInfo.t_class; + type.setType(TypeInfo.t_class); else if( kind == ASTClassKind.STRUCT ) - type = TypeInfo.t_struct; + type.setType(TypeInfo.t_struct); else if( kind == ASTClassKind.UNION ) - type = TypeInfo.t_union; + type.setType(TypeInfo.t_union); else if( kind == ASTClassKind.ENUM ) - type = TypeInfo.t_enumeration; + type.setType(TypeInfo.t_enumeration); else throw new ASTSemanticException(); - } - else - throw new ASTSemanticException(); - - ISymbol paramSymbol = pst.newSymbol( "", type ); - if( xrefSymbol != null ) - paramSymbol.setTypeSymbol( xrefSymbol ); - - setPointerOperators( paramSymbol, absDecl.getPointerOperators(), absDecl.getArrayModifiers() ); - - if( isParameter) - symbol.addParameter( paramSymbol ); - else + } + else + throw new ASTSemanticException(); + return type; + } + /** + * @param symbol + * @param returnType + */ + protected void setParameter(IParameterizedSymbol symbol, IASTAbstractDeclaration absDecl, boolean isParameter, List references) throws ASTSemanticException + { + if (absDecl.getTypeSpecifier() == null) + return; + + // now determined by another function + TypeInfo.eType type = getParameterTypeInfo(absDecl).getType(); + + ISymbol xrefSymbol = null; + List newReferences = null; + if( absDecl.getTypeSpecifier() instanceof IASTSimpleTypeSpecifier ) + { + IASTSimpleTypeSpecifier.Type kind = ((IASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getType(); + if( kind == IASTSimpleTypeSpecifier.Type.CLASS_OR_TYPENAME ) + { + xrefSymbol = ((ASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getSymbol(); + newReferences = ((ASTSimpleTypeSpecifier)absDecl.getTypeSpecifier()).getReferences(); + } + } + + String paramName = ""; + if(absDecl instanceof IASTParameterDeclaration){ + paramName = ((IASTParameterDeclaration)absDecl).getName(); + } + + ISymbol paramSymbol = pst.newSymbol( paramName, type ); + if( xrefSymbol != null ) + paramSymbol.setTypeSymbol( xrefSymbol.getTypeSymbol() ); + + setPointerOperators( paramSymbol, absDecl.getPointerOperators(), absDecl.getArrayModifiers() ); + + if( isParameter) + symbol.addParameter( paramSymbol ); + else symbol.setReturnType( paramSymbol ); if( newReferences != null ) references.addAll( newReferences ); - } + } /** * @param paramSymbol @@ -1391,13 +1428,15 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto symbolToBeCloned = ((ASTSimpleTypeSpecifier)abstractDeclaration.getTypeSpecifier()).getSymbol(); references.addAll( ((ASTSimpleTypeSpecifier)abstractDeclaration.getTypeSpecifier()).getReferences() ); } - else if( abstractDeclaration.getTypeSpecifier() instanceof ASTClassSpecifier ) + else if( abstractDeclaration.getTypeSpecifier() instanceof ASTClassSpecifier ) { - symbolToBeCloned = ((ASTClassSpecifier)abstractDeclaration.getTypeSpecifier()).getSymbol(); - } - else if( abstractDeclaration.getTypeSpecifier() instanceof ASTElaboratedTypeSpecifier ) + symbolToBeCloned = pst.newSymbol(name, TypeInfo.t_type); + symbolToBeCloned.setTypeSymbol(((ASTClassSpecifier)abstractDeclaration.getTypeSpecifier()).getSymbol()); + } + else if( abstractDeclaration.getTypeSpecifier() instanceof ASTElaboratedTypeSpecifier ) { - symbolToBeCloned = ((ASTElaboratedTypeSpecifier)abstractDeclaration.getTypeSpecifier()).getSymbol(); + symbolToBeCloned = pst.newSymbol(name, TypeInfo.t_type); + symbolToBeCloned.setTypeSymbol(((ASTElaboratedTypeSpecifier)abstractDeclaration.getTypeSpecifier()).getSymbol()); } newSymbol = (ISymbol) symbolToBeCloned.clone(); newSymbol.setName( name );