Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Niefer2004-11-15 16:31:24 -0500
committerAndrew Niefer2004-11-15 16:31:24 -0500
commitcde20ef7ef445b8373689e0c20d2372f7cfcd31f (patch)
tree966085b45b98cf18a37a68e87e0c1bbd9a8f671a
parenteb7c47bc3fc26e16b947d5e6433c77f634b2a7fa (diff)
downloadorg.eclipse.cdt-cde20ef7ef445b8373689e0c20d2372f7cfcd31f.tar.gz
org.eclipse.cdt-cde20ef7ef445b8373689e0c20d2372f7cfcd31f.tar.xz
org.eclipse.cdt-cde20ef7ef445b8373689e0c20d2372f7cfcd31f.zip
function parameters and clearing bindings
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java60
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java4
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java21
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java167
4 files changed, 244 insertions, 8 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
index 0e66e1a1fe..41afc86d09 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
@@ -66,6 +66,7 @@ import org.eclipse.cdt.core.parser.tests.parser2.QuickParser2Tests.ProblemCollec
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser2.ISourceCodeParser;
import org.eclipse.cdt.internal.core.parser2.c.ANSICParserExtensionConfiguration;
+import org.eclipse.cdt.internal.core.parser2.c.CVisitor;
import org.eclipse.cdt.internal.core.parser2.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.parser2.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser2.cpp.ANSICPPParserExtensionConfiguration;
@@ -611,6 +612,65 @@ public class AST2Tests extends TestCase {
assertSame( structA_3, structA_4 );
}
+ public void testFunctionParameters() throws Exception {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append( "void f( int a ); \n"); //$NON-NLS-1$
+ buffer.append( "void f( int b ){ \n"); //$NON-NLS-1$
+ buffer.append( " b; \n"); //$NON-NLS-1$
+ buffer.append( "} \n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
+
+ //void f(
+ IASTSimpleDeclaration f_decl = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
+ IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) f_decl.getDeclarators().get(0);
+ IASTName f_name1 = dtor.getName();
+ // int a );
+ IASTParameterDeclaration param = (IASTParameterDeclaration) dtor.getParameters().get(0);
+ IASTDeclarator paramDtor = param.getDeclarator();
+ IASTName name_param1 = paramDtor.getName();
+
+ //void f(
+ IASTFunctionDefinition f_defn = (IASTFunctionDefinition) tu.getDeclarations().get(1);
+ dtor = f_defn.getDeclarator();
+ IASTName f_name2 = dtor.getName();
+ // int b );
+ param = (IASTParameterDeclaration) dtor.getParameters().get(0);
+ paramDtor = param.getDeclarator();
+ IASTName name_param2 = paramDtor.getName();
+
+ // b;
+ IASTCompoundStatement compound = (IASTCompoundStatement) f_defn.getBody();
+ IASTExpressionStatement expStatement = (IASTExpressionStatement) compound.getStatements().get(0);
+ IASTIdExpression idexp = (IASTIdExpression) expStatement.getExpression();
+ IASTName name_param3 = idexp.getName();
+
+ //bindings
+ IParameter param_1 = (IParameter) name_param3.resolveBinding();
+ IParameter param_2 = (IParameter) name_param2.resolveBinding();
+ IParameter param_3 = (IParameter) name_param1.resolveBinding();
+ IFunction f_1 = (IFunction) f_name1.resolveBinding();
+ IFunction f_2 = (IFunction) f_name2.resolveBinding();
+
+ assertNotNull( param_1 );
+ assertNotNull( f_1 );
+ assertSame( param_1, param_2 );
+ assertSame( param_2, param_3 );
+ assertSame( f_1, f_2 );
+
+ CVisitor.clearBindings( tu );
+ param_1 = (IParameter) name_param1.resolveBinding();
+ param_2 = (IParameter) name_param3.resolveBinding();
+ param_3 = (IParameter) name_param2.resolveBinding();
+ f_1 = (IFunction) f_name2.resolveBinding();
+ f_2 = (IFunction) f_name1.resolveBinding();
+ assertNotNull( param_1 );
+ assertNotNull( f_1 );
+ assertSame( param_1, param_2 );
+ assertSame( param_2, param_3 );
+ assertSame( f_1, f_2 );
+ }
+
public void testSimpleFunction() throws Exception {
StringBuffer buffer = new StringBuffer( "void f( int a, int b ) { } \n" ); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java
index d84602ebc5..d65059cde5 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java
@@ -34,10 +34,6 @@ public class CFunction implements IFunction {
this.declarator = declarator;
this.functionScope = new CFunctionScope( this );
}
-// public CStructure( IASTDeclSpecifier declSpec ){
-// declSpec = checkForDefinition( declSpec );
-// this.declSpec = declSpec;
-// }
private IASTFunctionDeclarator checkForDefinition( IASTFunctionDeclarator declarator ){
if( declarator.getParent() instanceof IASTFunctionDefinition )
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java
index aacbdf036a..f920292b32 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java
@@ -11,6 +11,8 @@
**********************************************************************/
package org.eclipse.cdt.internal.core.parser2.c;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IScope;
@@ -24,8 +26,27 @@ public class CParameter implements IParameter {
final private IASTParameterDeclaration parameterDeclaration;
public CParameter( IASTParameterDeclaration parameterDeclaration ){
+ parameterDeclaration = checkForDefinition( parameterDeclaration );
this.parameterDeclaration = parameterDeclaration;
}
+
+ private IASTParameterDeclaration checkForDefinition( IASTParameterDeclaration paramDecl ){
+ IASTFunctionDeclarator fnDtor = (IASTFunctionDeclarator) paramDecl.getParent();
+ if( fnDtor.getParent() instanceof IASTFunctionDefinition )
+ return paramDecl;
+
+ IASTFunctionDeclarator fDef = CVisitor.findDefinition( fnDtor );
+ if( fDef != null && fDef instanceof IASTFunctionDefinition ){
+ int index = fnDtor.getParameters().indexOf( paramDecl );
+ if( index >= 0 && index < fDef.getParameters().size() ) {
+ IASTParameterDeclaration pDef = (IASTParameterDeclaration) fDef.getParameters().get( index );
+ ((CASTName)pDef.getDeclarator().getName()).setBinding( this );
+ paramDecl = pDef;
+ }
+ }
+ return paramDecl;
+ }
+
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IVariable#getType()
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java
index 7e41666789..e5a8cfbb8d 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java
@@ -13,23 +13,41 @@ package org.eclipse.cdt.internal.core.parser2.c;
import java.util.List;
+import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
+import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
+import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
+import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
+import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
+import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
+import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.core.dom.ast.IASTTypeId;
+import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
+import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
+import org.eclipse.cdt.core.dom.ast.IASTUnaryTypeIdExpression;
+import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IFunction;
@@ -38,7 +56,9 @@ import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
+import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
/**
@@ -151,8 +171,10 @@ public class CVisitor {
}
private static IBinding createBinding( IASTParameterDeclaration parameterDeclaration ){
- CParameter parameter = new CParameter( parameterDeclaration );
- return parameter;
+ IBinding binding = resolveBinding( parameterDeclaration, CURRENT_SCOPE );
+ if( binding == null )
+ binding = new CParameter( parameterDeclaration );
+ return binding;
}
protected static IBinding resolveBinding( IASTNode node ){
@@ -164,7 +186,6 @@ public class CVisitor {
IASTFunctionDeclarator functionDeclartor = functionDef.getDeclarator();
IASTName name = functionDeclartor.getName();
IASTNode blockItem = getContainingBlockItem( node );
- //return name.resolveBinding();
return findBinding( blockItem, (CASTName) name, scopeDepth );
} else if( node instanceof IASTIdExpression ){
IASTNode blockItem = getContainingBlockItem( node );
@@ -178,10 +199,24 @@ public class CVisitor {
} else if( node instanceof ICASTCompositeTypeSpecifier ){
IASTNode blockItem = getContainingBlockItem( node );
return findBinding( blockItem, (CASTName) ((ICASTCompositeTypeSpecifier)node).getName(), scopeDepth );
+ } else if( node instanceof IASTParameterDeclaration ){
+ IASTParameterDeclaration param = (IASTParameterDeclaration) node;
+ IASTFunctionDeclarator fDtor = (IASTFunctionDeclarator) param.getParent();
+ if( fDtor.getParent() instanceof IASTFunctionDefinition ){
+ return null;
+ }
+ IASTFunctionDeclarator fdef = findDefinition( fDtor );
+ if( fdef != null ){
+ int index = fDtor.getParameters().indexOf( param );
+ if( index >= 0 && index < fdef.getParameters().size() ){
+ IASTParameterDeclaration pdef = (IASTParameterDeclaration) fdef.getParameters().get( index );
+ return pdef.getDeclarator().getName().resolveBinding();
+ }
+ }
}
return null;
}
-
+
/**
* @param declaration
* @return
@@ -402,4 +437,128 @@ public class CVisitor {
}
return null;
}
+
+ public static void clearBindings( IASTTranslationUnit tu ){
+ List decls = tu.getDeclarations();
+ for( int i = 0; i < decls.size(); i++ ){
+ clearBindings( (IASTDeclaration) decls.get(i) );
+ }
+ }
+ private static void clearBindings( IASTDeclaration declaration ){
+ if( declaration instanceof IASTSimpleDeclaration ){
+ IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) declaration;
+ clearBindings( simpleDecl.getDeclSpecifier() );
+ List list = simpleDecl.getDeclarators();
+ for( int i = 0; i < list.size(); i++ ){
+ clearBindings( (IASTDeclarator) list.get(i) );
+ }
+ } else if( declaration instanceof IASTFunctionDefinition ){
+ IASTFunctionDefinition fnDef = (IASTFunctionDefinition) declaration;
+ clearBindings( fnDef.getDeclSpecifier() );
+ clearBindings( fnDef.getDeclarator() );
+ clearBindings( fnDef.getBody() );
+ }
+ }
+ private static void clearBindings( IASTDeclarator declarator ){
+ ((CASTName)declarator.getName()).setBinding( null );
+
+ if( declarator.getNestedDeclarator() != null )
+ clearBindings( declarator.getNestedDeclarator() );
+
+ //TODO: if( declarator.getInitializer() != null )
+
+ if( declarator instanceof IASTFunctionDeclarator ){
+ List list = ((IASTFunctionDeclarator)declarator).getParameters();
+ for( int i = 0; i < list.size(); i++ ){
+ IASTParameterDeclaration param = (IASTParameterDeclaration) list.get(i);
+ clearBindings( param.getDeclarator() );
+ }
+ }
+ }
+ private static void clearBindings( IASTDeclSpecifier declSpec ){
+ if( declSpec instanceof ICASTCompositeTypeSpecifier ){
+ ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) declSpec;
+ ((CASTName) compTypeSpec.getName()).setBinding( null );
+
+ List list = compTypeSpec.getMembers();
+ for( int i = 0; i < list.size(); i++ ){
+ clearBindings( (IASTDeclaration) list.get(i) );
+ }
+ }
+ }
+ private static void clearBindings( IASTStatement statement ){
+ if( statement instanceof IASTCompoundStatement ){
+ List list = ((IASTCompoundStatement) statement).getStatements();
+ for( int i = 0; i < list.size(); i++ ){
+ clearBindings( (IASTStatement) list.get(i) );
+ }
+ } else if( statement instanceof IASTDeclarationStatement ){
+ clearBindings( ((IASTDeclarationStatement)statement).getDeclaration() );
+ } else if( statement instanceof IASTExpressionStatement ){
+ clearBindings( ((IASTExpressionStatement)statement).getExpression() );
+ } else if( statement instanceof IASTCaseStatement ){
+ clearBindings( ((IASTCaseStatement)statement).getExpression() );
+ } else if( statement instanceof IASTDoStatement ){
+ clearBindings( ((IASTDoStatement)statement).getBody() );
+ } else if( statement instanceof IASTGotoStatement ){
+ ((CASTName) ((IASTGotoStatement)statement).getName()).setBinding( null );
+ } else if( statement instanceof IASTIfStatement ){
+ clearBindings( ((IASTIfStatement) statement ).getCondition() );
+ clearBindings( ((IASTIfStatement) statement ).getThenClause() );
+ clearBindings( ((IASTIfStatement) statement ).getElseClause() );
+ } else if( statement instanceof IASTLabelStatement ){
+ ((CASTName) ((IASTLabelStatement)statement).getName()).setBinding( null );
+ } else if( statement instanceof IASTReturnStatement ){
+ clearBindings( ((IASTReturnStatement) statement ).getReturnValue() );
+ } else if( statement instanceof IASTSwitchStatement ){
+ clearBindings( ((IASTSwitchStatement) statement ).getController() );
+ clearBindings( ((IASTSwitchStatement) statement ).getBody() );
+ } else if( statement instanceof IASTWhileStatement ){
+ clearBindings( ((IASTWhileStatement) statement ).getCondition() );
+ clearBindings( ((IASTWhileStatement) statement ).getBody() );
+ }
+ }
+ private static void clearBindings( IASTTypeId typeId ){
+ clearBindings( typeId.getAbstractDeclarator() );
+ clearBindings( typeId.getDeclSpecifier() );
+ }
+ private static void clearBindings( IASTExpression expression ){
+ if( expression instanceof IASTArraySubscriptExpression ){
+ clearBindings( ((IASTArraySubscriptExpression)expression).getArrayExpression() );
+ clearBindings( ((IASTArraySubscriptExpression)expression).getSubscriptExpression() );
+ } else if( expression instanceof IASTBinaryExpression ){
+ clearBindings( ((IASTBinaryExpression)expression).getOperand1() );
+ clearBindings( ((IASTBinaryExpression)expression).getOperand2() );
+ } else if( expression instanceof IASTConditionalExpression){
+ clearBindings( ((IASTConditionalExpression)expression).getLogicalConditionExpression() );
+ clearBindings( ((IASTConditionalExpression)expression).getNegativeResultExpression() );
+ clearBindings( ((IASTConditionalExpression)expression).getPositiveResultExpression() );
+ } else if( expression instanceof IASTExpressionList ){
+ List list = ((IASTExpressionList)expression).getExpressions();
+ for( int i = 0; i < list.size(); i++){
+ clearBindings( (IASTExpression) list.get(i) );
+ }
+ } else if( expression instanceof IASTFieldReference ){
+ clearBindings( ((IASTFieldReference)expression).getFieldOwner() );
+ ((CASTName) ((IASTFieldReference)expression).getFieldName()).setBinding( null );
+ } else if( expression instanceof IASTFunctionCallExpression ){
+ clearBindings( ((IASTFunctionCallExpression)expression).getFunctionNameExpression() );
+ clearBindings( ((IASTFunctionCallExpression)expression).getParameterExpression() );
+ } else if( expression instanceof IASTIdExpression ){
+ ((CASTName) ((IASTIdExpression)expression).getName()).setBinding( null );
+ } else if( expression instanceof IASTTypeIdExpression ){
+ clearBindings( ((IASTTypeIdExpression)expression).getTypeId() );
+ } else if( expression instanceof IASTUnaryExpression ){
+ clearBindings( ((IASTUnaryExpression)expression).getOperand() );
+ } else if( expression instanceof IASTUnaryTypeIdExpression ){
+ clearBindings( ((IASTUnaryTypeIdExpression)expression).getOperand() );
+ clearBindings( ((IASTUnaryTypeIdExpression)expression).getTypeId() );
+ } else if( expression instanceof ICASTTypeIdInitializerExpression ){
+ clearBindings( ((ICASTTypeIdInitializerExpression)expression).getTypeId() );
+ //TODO: ((ICASTTypeIdInitializerExpression)expression).getInitializer();
+ } else if( expression instanceof IGNUASTCompoundStatementExpression ){
+ clearBindings( ((IGNUASTCompoundStatementExpression)expression).getCompoundStatement() );
+ }
+ }
+
}

Back to the top