Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-patch] Solution PR#42560 + Partial solution to PR#42453


Patch updates + added some tests to completeParseASTTest

Hoda Amer
Staff Software Engineer
Rational Software - IBM Software Group







Hoda Amer/Ottawa/IBM@IBMCA
Sent by: cdt-patch-admin@xxxxxxxxxxx

09/04/2003 04:39 PM
Please respond to cdt-patch

       
        To:        cdt-patch@xxxxxxxxxxx
        cc:        
        Subject:        [cdt-patch] Solution PR#42560 + Partial solution to PR#42453




Core:
        - Added references to variables in solution of bug#42453:_expression_ result types not computed
        - Solution to bug#42560: Class Cast Exception during Method definition
Hoda Amer
Staff Software Engineer
Rational Software - IBM Software Group




#### core.sep.04_02.txt has been removed from this note on September 05, 2003 by Hoda Amer
Index: parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java,v
retrieving revision 1.22
diff -u -r1.22 CompleteParseASTTest.java
--- parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java	5 Sep 2003 14:19:58 -0000	1.22
+++ parser/org/eclipse/cdt/core/parser/tests/CompleteParseASTTest.java	5 Sep 2003 14:28:57 -0000
@@ -27,6 +27,7 @@
 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 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
@@ -618,5 +624,33 @@
 		IASTField intV  = (IASTField)sub.next();
 		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 );
+		 
+	}	
 	
 }
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/ChangeLog,v
retrieving revision 1.141
diff -u -r1.141 ChangeLog
--- ChangeLog	4 Sep 2003 15:37:20 -0000	1.141
+++ ChangeLog	5 Sep 2003 14:28:26 -0000
@@ -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
Index: parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java,v
retrieving revision 1.28
diff -u -r1.28 CompleteParseASTFactory.java
--- parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java	5 Sep 2003 14:20:14 -0000	1.28
+++ parser/org/eclipse/cdt/internal/core/parser/ast/complete/CompleteParseASTFactory.java	5 Sep 2003 14:28:28 -0000
@@ -61,6 +61,7 @@
 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 @@
         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 @@
 			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 @@
 			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 @@
 			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 @@
         }
     }
 
-    /**
-     * @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 @@
         	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 );

Back to the top