1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Patch for Hoda Amer

Core: 
        In completeParseASTFactory.getExpressionResultType(): Added the support 
        for expression type PRIMARY_THIS. 
        In createMethod(): changed the scope of a method definition to point to 
        the parent class. 

Tests: 
    Added CompleteParseASTExpressionTest.testPrimaryThis()
This commit is contained in:
John Camelon 2003-09-16 14:45:39 +00:00
parent 61976f1b51
commit 6f580b7c1a
5 changed files with 186 additions and 108 deletions

View file

@ -1,3 +1,6 @@
2003-09-16 Hoda Amer
Added CompleteParseASTExpressionTest.testPrimaryThis()
2003-09-15 John Camelon
Moved ASTFailedTests::testBug39556() to QuickParseASTTests.
Cleaned up some warnings in parser tests.

View file

@ -19,6 +19,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.IASTMethod;
import org.eclipse.cdt.core.parser.ast.IASTReference;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.parser.ast.IASTVariableReference;
@ -107,8 +108,23 @@ public class CompleteParseASTExpressionTest extends CompleteParseBaseTest{
assertEquals( fr1.getReferencedElement(), f1 );
}
// Kind PRIMARY_THIS
// Kind PRIMARY_THIS : type of inner most enclosing structure scope
public void testPrimaryThis() throws Exception
{
Iterator i = parse ("class A{ int m(); }; A a; \n int f(void); \n int f(A * a); \n int A::m(){ int x = f(this); }").getDeclarations();
IASTClassSpecifier cl = (IASTClassSpecifier)((IASTAbstractTypeSpecifierDeclaration)i.next()).getTypeSpecifier();
Iterator members = getDeclarations(cl);
IASTMethod method = (IASTMethod)members.next();
IASTVariable a = (IASTVariable) i.next();
IASTFunction f1 = (IASTFunction) i.next();
IASTFunction f2 = (IASTFunction) i.next();
IASTMethod m = (IASTMethod) i.next();
Iterator references = callback.getReferences().iterator();
assertEquals( ((IASTClassReference) references.next()).getReferencedElement(), cl );
assertEquals( ((IASTClassReference) references.next()).getReferencedElement(), cl );
assertEquals( ((IASTClassReference) references.next()).getReferencedElement(), cl );
assertEquals( ((IASTFunctionReference) references.next()).getReferencedElement(), f2 );
}
// Kind PRIMARY_BRACKETED_EXPRESSION : LHS
public void testPrimaryBracketedExpression() throws Exception
{

View file

@ -731,6 +731,7 @@ public class CompleteParseASTTest extends CompleteParseBaseTest
Iterator subIterator = getDeclarations( classSpec );
IASTMethod fooMethodDeclaration = (IASTMethod)subIterator.next();
assertFalse( subIterator.hasNext());
Iterator references = callback.getReferences().iterator();
assertEquals( callback.getReferences().size(), 3 );
for( int j = 0; j < 3; ++j)
assertEquals( ((IASTReference)callback.getReferences().get( j )).getReferencedElement(), classSpec );

View file

@ -1,3 +1,9 @@
2003-09-16 Hoda Amer
In completeParseASTFactory.getExpressionResultType(): Added the support
for expression type PRIMARY_THIS.
In createMethod(): changed the scope of a method definition to point to
the parent class.
2003-09-15 John Camelon
Fixed Bug 39556 : 'restrict' qualifier is not supported (ANSI C99)
Fixed Bug 43126 : ISourceElementRequestor.acceptParameterReference accesses internal class

View file

@ -133,6 +133,30 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
return true;
}
private ISymbol lookupElement (IContainerSymbol startingScope, String name, TypeInfo.eType type, List parameters) throws ParserSymbolTableException{
ISymbol result = null;
try {
if((type == TypeInfo.t_function) || (type == TypeInfo.t_constructor)){
// looking for a function
if(validParameterList(parameters))
if(type == TypeInfo.t_constructor){
IDerivableContainerSymbol startingDerivableScope = (IDerivableContainerSymbol) startingScope;
result = startingDerivableScope.lookupConstructor( new LinkedList(parameters));
}
else
result = startingScope.qualifiedFunctionLookup(name, new LinkedList(parameters));
else
result = null;
}else{
// looking for something else
result = startingScope.qualifiedLookup(name, type);
}
} catch (ParserSymbolTableException e) {
throw e;
}
return result;
}
protected ISymbol lookupQualifiedName( IContainerSymbol startingScope, String name, TypeInfo.eType type, List parameters, int offset, List references, boolean throwOnError ) throws ASTSemanticException
{
ISymbol result = null;
@ -141,16 +165,7 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
if( name == null ) throw new ASTSemanticException();
try
{
if(type == TypeInfo.t_function){
// looking for a function
if(validParameterList(parameters))
result = startingScope.qualifiedFunctionLookup(name, new LinkedList(parameters));
else
result = null;
}else{
// looking for something else
result = startingScope.qualifiedLookup(name, type);
}
result = lookupElement(startingScope, name, type, parameters);
if( result != null )
addReference(references, createReference( result, name, offset ));
else
@ -176,82 +191,84 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
return lookupQualifiedName(startingScope, name, TypeInfo.t_any, null, references, throwOnError);
}
protected ISymbol lookupQualifiedName( IContainerSymbol startingScope, ITokenDuple name, TypeInfo.eType type, List parameters, List references, boolean throwOnError ) throws ASTSemanticException
{
ISymbol result = null;
IToken firstSymbol = null;
try
{
if( name == null ) throw new ASTSemanticException();
switch( name.length() )
{
case 0:
if( throwOnError )
throw new ASTSemanticException();
case 1:
firstSymbol = name.getFirstToken();
try
{
if(type == TypeInfo.t_function)
if (validParameterList(parameters))
result = startingScope.unqualifiedFunctionLookup( firstSymbol.getImage(), new LinkedList(parameters));
else
result = null;
else
result = startingScope.lookup( firstSymbol.getImage());
if( result != null )
addReference( references, createReference( result, firstSymbol.getImage(), firstSymbol.getOffset() ));
else
throw new ASTSemanticException();
}
catch (ParserSymbolTableException e)
{
throw new ASTSemanticException();
}
break;
default:
Iterator iter = name.iterator();
firstSymbol = name.getFirstToken();
result = startingScope;
if( firstSymbol.getType() == IToken.tCOLONCOLON )
result = pst.getCompilationUnit();
while( iter.hasNext() )
{
IToken t = (IToken)iter.next();
if( t.getType() == IToken.tCOLONCOLON ) continue;
if( t.isPointer() ) break;
try
{
if( t == name.getLastToken() )
if(type == TypeInfo.t_function)
if (validParameterList(parameters))
result = ((IContainerSymbol)result).qualifiedFunctionLookup( t.getImage(), new LinkedList(parameters) );
else
result = null;
else
result = ((IContainerSymbol)result).qualifiedLookup( t.getImage() );
else
result = ((IContainerSymbol)result).lookupNestedNameSpecifier( t.getImage() );
addReference( references, createReference( result, t.getImage(), t.getOffset() ));
}
catch( ParserSymbolTableException pste )
{
throw new ASTSemanticException();
}
}
}
}
catch( ASTSemanticException se )
protected ISymbol lookupQualifiedName( IContainerSymbol startingScope, ITokenDuple name, TypeInfo.eType type, List parameters, List references, boolean throwOnError ) throws ASTSemanticException
{
if( throwOnError )
throw se;
return null;
ISymbol result = null;
IToken firstSymbol = null;
try
{
if( name == null ) throw new ASTSemanticException();
switch( name.length() )
{
case 0:
if( throwOnError )
throw new ASTSemanticException();
case 1:
firstSymbol = name.getFirstToken();
try
{
result = lookupElement(startingScope, firstSymbol.getImage(), type, parameters);
/* if(type == TypeInfo.t_function)
if (validParameterList(parameters))
result = startingScope.unqualifiedFunctionLookup( firstSymbol.getImage(), new LinkedList(parameters));
else
result = null;
else
result = startingScope.lookup( firstSymbol.getImage());
*/ if( result != null )
addReference( references, createReference( result, firstSymbol.getImage(), firstSymbol.getOffset() ));
else
throw new ASTSemanticException();
}
catch (ParserSymbolTableException e)
{
throw new ASTSemanticException();
}
break;
default:
Iterator iter = name.iterator();
firstSymbol = name.getFirstToken();
result = startingScope;
if( firstSymbol.getType() == IToken.tCOLONCOLON )
result = pst.getCompilationUnit();
while( iter.hasNext() )
{
IToken t = (IToken)iter.next();
if( t.getType() == IToken.tCOLONCOLON ) continue;
if( t.isPointer() ) break;
try
{
if( t == name.getLastToken() )
result = lookupElement((IContainerSymbol)result, t.getImage(), type, parameters);
/* if((type == TypeInfo.t_function) || (type == TypeInfo.t_constructor))
if (validParameterList(parameters))
result = ((IContainerSymbol)result).qualifiedFunctionLookup( t.getImage(), new LinkedList(parameters) );
else
result = null;
else
result = ((IContainerSymbol)result).qualifiedLookup( t.getImage() );
*/ else
result = ((IContainerSymbol)result).lookupNestedNameSpecifier( t.getImage() );
addReference( references, createReference( result, t.getImage(), t.getOffset() ));
}
catch( ParserSymbolTableException pste )
{
throw new ASTSemanticException();
}
}
}
}
catch( ASTSemanticException se )
{
if( throwOnError )
throw se;
return null;
}
return result;
}
return result;
}
/* (non-Javadoc)
@ -623,7 +640,7 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
return new ASTEnumerationReference( offset, string, (IASTEnumerationSpecifier)symbol.getASTExtension().getPrimaryDeclaration() );
else if( symbol.getType() == TypeInfo.t_enumerator )
return new ASTEnumeratorReference( offset, string, (IASTEnumerator)symbol.getASTExtension().getPrimaryDeclaration() );
else if( symbol.getType() == TypeInfo.t_function )
else if(( symbol.getType() == TypeInfo.t_function ) || (symbol.getType() == TypeInfo.t_constructor))
{
if( symbol.getContainingSymbol().getTypeInfo().isType( TypeInfo.t_class, TypeInfo.t_union ) )
return new ASTMethodReference( offset, string, (IASTMethod)symbol.getASTExtension().getPrimaryDeclaration() );
@ -781,6 +798,17 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
}
}
// go up the scope until you hit a class
if (kind == IASTExpression.Kind.PRIMARY_THIS){
ASTScope parentScope = (ASTScope)scope;
while (!(parentScope instanceof IASTClassSpecifier) )
{
parentScope = (ASTScope)((ASTScope)parentScope).getOwnerScope();
}
if(parentScope instanceof IASTClassSpecifier)
symbol = parentScope.getSymbol();
}
if (kind == IASTExpression.Kind.POSTFIX_FUNCTIONCALL){
ITokenDuple functionId = ((ASTExpression)lhs).getTypeId();
List parameters = ((ASTExpression)rhs).getResultType();
@ -1009,6 +1037,17 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
return result;
}
}
// this
if (expression.getExpressionKind() == IASTExpression.Kind.PRIMARY_THIS){
if(symbol != null)
{
info.setType(TypeInfo.t_type);
info.setTypeSymbol(symbol);
info.addPtrOperator(new TypeInfo.PtrOp(TypeInfo.PtrOp.t_reference));
result.add(info);
return result;
}
}
// new
/* if((expression.getExpressionKind() == IASTExpression.Kind.NEW_NEWTYPEID)
|| (expression.getExpressionKind() == IASTExpression.Kind.NEW_TYPEID)
@ -1325,26 +1364,11 @@ 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, functionParameters, 0, functionReferences, false);
if(methodDeclaration != null){
ASTMethodReference reference = (ASTMethodReference) functionReferences.iterator().next();
visibility = ((IASTMethod)reference.getReferencedElement()).getVisiblity();
}
return createMethod(scope, functionName, nameEndOffset, parameters, returnType,
IASTClassSpecifier methodParentScope = (IASTClassSpecifier)parentScope.getASTExtension().getPrimaryDeclaration();
return createMethod(methodParentScope, functionName,nameEndOffset, parameters, returnType,
exception, isInline, isFriend, isStatic, startOffset, offset,
ownerTemplate, isConst, isVolatile, isVirtual, isExplicit, isPureVirtual,
visibility, constructorChain,parentName, references, isFunctionDefinition);
visibility, constructorChain, references, isFunctionDefinition);
}
}
@ -1364,7 +1388,7 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
Iterator p = parameters.iterator();
while (p.hasNext()){
ASTParameterDeclaration param = (ASTParameterDeclaration)p.next();
functionParameters.add(getParameterTypeInfo(param));
functionParameters.add(param.getSymbol().getTypeInfo());
}
IParameterizedSymbol functionDeclaration = null;
@ -1600,7 +1624,7 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
return createMethod(scope, name, nameEndOffset, parameters, returnType,
exception, isInline, isFriend, isStatic, startOffset, nameOffset,
ownerTemplate, isConst, isVolatile, isVirtual, isExplicit, isPureVirtual,
visibility, constructorChain,scopeToSymbol(scope).getName(), null, isFunctionDefinition );
visibility, constructorChain, null, isFunctionDefinition );
}
public IASTMethod createMethod(
@ -1623,7 +1647,6 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
boolean isPureVirtual,
ASTAccessVisibility visibility,
List constructorChain,
String parentName,
List references, boolean isFunctionDefinition ) throws ASTSemanticException
{
boolean isConstructor = false;
@ -1639,6 +1662,8 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
if( returnType.getTypeSpecifier() != null )
setParameter( symbol, returnType, false, references );
setParameters( symbol, references, parameters.iterator() );
String parentName = ((IASTClassSpecifier)scope).getName();
// check constructor / destructor if no return type
if ( returnType.getTypeSpecifier() == null ){
@ -1651,6 +1676,33 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
isDestructor = true;
}
}
symbol.setIsForwardDeclaration(!isFunctionDefinition);
if( isFunctionDefinition )
{
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(param.getSymbol().getTypeInfo());
}
IParameterizedSymbol functionDeclaration = null;
List functionReferences = new ArrayList();
functionDeclaration =
(IParameterizedSymbol) lookupQualifiedName(ownerScope, name, isConstructor ? TypeInfo.t_constructor : TypeInfo.t_function, functionParameters, 0, functionReferences, false);
if( functionDeclaration != null )
{
functionDeclaration.setTypeSymbol( symbol );
// set the definition visibility = declaration visibility
ASTMethodReference reference = (ASTMethodReference) functionReferences.iterator().next();
visibility = ((IASTMethod)reference.getReferencedElement()).getVisiblity();
}
}
try
{