Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Niefer2004-11-12 14:55:33 -0500
committerAndrew Niefer2004-11-12 14:55:33 -0500
commitb273c73149852f2155e48d23fab702fb72e3f4c6 (patch)
treea0791f4ab812a05e81ae303cab0cd27c4a33dbd7
parent90fd1337034737e8e1b232e1b660a7d270eab16c (diff)
downloadorg.eclipse.cdt-b273c73149852f2155e48d23fab702fb72e3f4c6.tar.gz
org.eclipse.cdt-b273c73149852f2155e48d23fab702fb72e3f4c6.tar.xz
org.eclipse.cdt-b273c73149852f2155e48d23fab702fb72e3f4c6.zip
work on function bindings and for statement
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java103
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CFunction.java46
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CParameter.java2
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CStructure.java19
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java180
5 files changed, 249 insertions, 101 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 c83f2fbaea..0e66e1a1fe 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
@@ -26,6 +26,8 @@ import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
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.IASTIdExpression;
@@ -35,7 +37,6 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
-import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTTypedefNameSpecifier;
@@ -611,6 +612,34 @@ public class AST2Tests extends TestCase {
}
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 );
+
+ IASTFunctionDefinition fDef = (IASTFunctionDefinition) tu.getDeclarations().get(0);
+ IASTFunctionDeclarator fDtor = fDef.getDeclarator();
+ IASTName fName = fDtor.getName();
+
+ IASTParameterDeclaration a = (IASTParameterDeclaration) fDtor.getParameters().get( 0 );
+ IASTName name_a = a.getDeclarator().getName();
+
+ IASTParameterDeclaration b = (IASTParameterDeclaration) fDtor.getParameters().get( 1 );
+ IASTName name_b = b.getDeclarator().getName();
+
+ IFunction function = (IFunction) fName.resolveBinding();
+ IParameter param_a = (IParameter) name_a.resolveBinding();
+ IParameter param_b = (IParameter) name_b.resolveBinding();
+
+ assertEquals( "f", function.getName() ); //$NON-NLS-1$
+ assertEquals( "a", param_a.getName() ); //$NON-NLS-1$
+ assertEquals( "b", param_b.getName() ); //$NON-NLS-1$
+
+ List params = function.getParameters();
+ assertEquals( 2, params.size() );
+ assertSame( params.get(0), param_a );
+ assertSame( params.get(1), param_b );
+ }
+
+ public void testSimpleFunctionCall() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append( "void f(); \n" ); //$NON-NLS-1$
buffer.append( "void g() { \n" ); //$NON-NLS-1$
@@ -627,19 +656,77 @@ public class AST2Tests extends TestCase {
//void g() {
IASTFunctionDefinition gdef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
- IASTFunctionDeclarator gdtor = gdef.getDeclarator();
- IASTName name_g = gdtor.getName();
// f();
IASTCompoundStatement compound = (IASTCompoundStatement) gdef.getBody();
- IASTStatement statement = (IASTStatement) compound.getStatements().get(0);
- assertTrue( statement instanceof IASTExpressionStatement );
- IASTExpressionStatement expStatement = (IASTExpressionStatement) statement;
- IASTExpression exp = expStatement.getExpression();
+ IASTExpressionStatement expStatement = (IASTExpressionStatement) compound.getStatements().get(0);
+ IASTFunctionCallExpression fcall = (IASTFunctionCallExpression) expStatement.getExpression();
+ IASTIdExpression fcall_id = (IASTIdExpression) fcall.getFunctionNameExpression();
+ IASTName name_fcall = fcall_id.getName();
+ assertNull( fcall.getParameterExpression() );
//void f() {}
- IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
+ IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(2);
fdtor = fdef.getDeclarator();
IASTName name_fdef = fdtor.getName();
+
+ //bindings
+ IFunction function_1 = (IFunction) name_fcall.resolveBinding();
+ IFunction function_2 = (IFunction) name_f.resolveBinding();
+ IFunction function_3 = (IFunction) name_fdef.resolveBinding();
+
+ assertNotNull( function_1 );
+ assertSame( function_1, function_2 );
+ assertSame( function_2, function_3 );
+ }
+
+ public void testForLoop() throws Exception {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append( "void f() { \n"); //$NON-NLS-1$
+ buffer.append( " for( int i = 0; i < 5; i++ ) { \n"); //$NON-NLS-1$
+ buffer.append( " i; \n"); //$NON-NLS-1$
+ buffer.append( " } \n"); //$NON-NLS-1$
+ buffer.append( "} \n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
+
+ //void f() {
+ IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(0);
+ IASTCompoundStatement compound = (IASTCompoundStatement) fdef.getBody();
+
+ // for(
+ IASTForStatement for_stmt = (IASTForStatement) compound.getStatements().get(0);
+ // int i = 0;
+ assertNull( for_stmt.getInitExpression() );
+ IASTSimpleDeclaration initDecl = (IASTSimpleDeclaration) for_stmt.getInitDeclaration();
+ IASTDeclarator dtor = (IASTDeclarator) initDecl.getDeclarators().get(0);
+ IASTName name_i = dtor.getName();
+ // i < 5;
+ IASTBinaryExpression exp = (IASTBinaryExpression) for_stmt.getCondition();
+ IASTIdExpression id_i = (IASTIdExpression) exp.getOperand1();
+ IASTName name_i2 = id_i.getName();
+ IASTLiteralExpression lit_5 = (IASTLiteralExpression) exp.getOperand2();
+ assertEquals( IASTLiteralExpression.lk_integer_constant, lit_5.getKind() );
+ // i++ ) {
+ IASTUnaryExpression un = (IASTUnaryExpression) for_stmt.getIterationExpression();
+ IASTIdExpression id_i2 = (IASTIdExpression) un.getOperand();
+ IASTName name_i3 = id_i2.getName();
+ assertEquals( IASTUnaryExpression.op_postFixIncr, un.getOperator() );
+
+ // i;
+ compound = (IASTCompoundStatement) for_stmt.getBody();
+ IASTExpressionStatement exprSt = (IASTExpressionStatement) compound.getStatements().get(0);
+ IASTIdExpression id_i3 = (IASTIdExpression) exprSt.getExpression();
+ IASTName name_i4 = id_i3.getName();
+
+ //bindings
+ IVariable var_1 = (IVariable) name_i4.resolveBinding();
+ IVariable var_2 = (IVariable) name_i.resolveBinding();
+ IVariable var_3 = (IVariable) name_i2.resolveBinding();
+ IVariable var_4 = (IVariable) name_i3.resolveBinding();
+
+ assertSame( var_1, var_2 );
+ assertSame( var_2, var_3 );
+ assertSame( var_3, var_4 );
}
}
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 32a1ce2ecb..d84602ebc5 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
@@ -11,9 +11,13 @@
**********************************************************************/
package org.eclipse.cdt.internal.core.parser2.c;
+import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+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.IFunction;
import org.eclipse.cdt.core.dom.ast.IScope;
@@ -22,33 +26,59 @@ import org.eclipse.cdt.core.dom.ast.IScope;
* @author aniefer
*/
public class CFunction implements IFunction {
- final private IASTDeclaration declaration;
+ final private IASTFunctionDeclarator declarator;
final private IScope functionScope;
- public CFunction( IASTDeclaration declaration ){
- this.declaration = declaration;
+ public CFunction( IASTFunctionDeclarator declarator ){
+ declarator = checkForDefinition( declarator );
+ 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 )
+ return declarator;
+
+ IASTFunctionDeclarator decl = CVisitor.findDefinition( declarator );
+ if( decl != null && decl != declarator ){
+ declarator = decl;
+ ((CASTName)declarator.getName()).setBinding( this );
+ }
+ return declarator;
+ }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters()
*/
public List getParameters() {
- return null;
+ List params = declarator.getParameters();
+ int size = params.size();
+ List result = new ArrayList( size );
+ if( size > 0 ){
+ for( int i = 0; i < size; i++ ){
+ IASTParameterDeclaration p = (IASTParameterDeclaration) params.get(i);
+ result.add( p.getDeclarator().getName().resolveBinding() );
+ }
+ }
+ return result;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBinding#getName()
*/
public String getName() {
- return null;
+ return declarator.getName().toString();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
*/
public IScope getScope() {
- return CVisitor.getContainingScope( declaration );
+ return CVisitor.getContainingScope( (IASTDeclaration) declarator.getParent() );
}
/* (non-Javadoc)
@@ -57,8 +87,4 @@ public class CFunction implements IFunction {
public IScope getFunctionScope() {
return functionScope;
}
-
- public IASTDeclaration getDeclaration(){
- return declaration;
- }
}
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 43cae014b8..aacbdf036a 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
@@ -38,7 +38,7 @@ public class CParameter implements IParameter {
* @see org.eclipse.cdt.core.dom.ast.IBinding#getName()
*/
public String getName() {
- return null;
+ return parameterDeclaration.getDeclarator().getName().toString();
}
/* (non-Javadoc)
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CStructure.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CStructure.java
index 63cc009642..54c5ea0df1 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CStructure.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CStructure.java
@@ -16,7 +16,6 @@ import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
-import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
@@ -24,29 +23,31 @@ import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
/**
* Created on Nov 8, 2004
* @author aniefer
*/
public class CStructure implements ICompositeType {
- private IASTDeclSpecifier declSpec;
+ final private IASTDeclSpecifier declSpec;
public CStructure( IASTDeclSpecifier declSpec ){
+ declSpec = checkForDefinition( declSpec );
this.declSpec = declSpec;
- checkForDefinition();
}
- private void checkForDefinition(){
+ private IASTDeclSpecifier checkForDefinition( IASTDeclSpecifier declSpec ){
if( declSpec instanceof ICASTCompositeTypeSpecifier )
- return;
+ return declSpec;
- IASTDeclSpecifier spec = CVisitor.findDefinition( (IASTElaboratedTypeSpecifier) declSpec );
+ IASTDeclSpecifier spec = CVisitor.findDefinition( (ICASTElaboratedTypeSpecifier) declSpec );
if( spec != null && spec instanceof ICASTCompositeTypeSpecifier ){
- this.declSpec = spec;
+ declSpec = spec;
ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) spec;
((CASTName)compTypeSpec.getName()).setBinding( this );
}
+ return declSpec;
}
/* (non-Javadoc)
@@ -55,8 +56,8 @@ public class CStructure implements ICompositeType {
public String getName() {
if( declSpec instanceof ICASTCompositeTypeSpecifier )
return ((ICASTCompositeTypeSpecifier)declSpec).getName().toString();
- else if( declSpec instanceof IASTElaboratedTypeSpecifier )
- return ((IASTElaboratedTypeSpecifier)declSpec).getName().toString();
+ else if( declSpec instanceof ICASTElaboratedTypeSpecifier )
+ return ((ICASTElaboratedTypeSpecifier)declSpec).getName().toString();
return ""; //$NON-NLS-1$
}
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 97c6dd52e5..7e41666789 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,21 +13,23 @@ package org.eclipse.cdt.internal.core.parser2.c;
import java.util.List;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
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.IASTElaboratedTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
+import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
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.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
-import org.eclipse.cdt.core.dom.ast.IASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IFunction;
@@ -35,6 +37,8 @@ import org.eclipse.cdt.core.dom.ast.IScope;
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.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
/**
@@ -52,7 +56,7 @@ public class CVisitor {
if( parent instanceof CASTIdExpression ){
binding = resolveBinding( parent );
- } else if( parent instanceof IASTTypedefNameSpecifier ){
+ } else if( parent instanceof ICASTTypedefNameSpecifier ){
binding = resolveBinding( parent );
} else if( parent instanceof IASTFieldReference ){
binding = findBinding( (IASTFieldReference) parent );
@@ -60,13 +64,13 @@ public class CVisitor {
binding = createBinding( (CASTDeclarator) parent, name );
} else if( parent instanceof CASTCompositeTypeSpecifier ){
binding = createBinding( (CASTCompositeTypeSpecifier) parent );
- } else if( parent instanceof IASTElaboratedTypeSpecifier ){
- binding = createBinding( (IASTElaboratedTypeSpecifier) parent );
+ } else if( parent instanceof ICASTElaboratedTypeSpecifier ){
+ binding = createBinding( (ICASTElaboratedTypeSpecifier) parent );
}
name.setBinding( binding );
}
- private static IBinding createBinding( IASTElaboratedTypeSpecifier elabTypeSpec ){
+ private static IBinding createBinding( ICASTElaboratedTypeSpecifier elabTypeSpec ){
IASTNode parent = elabTypeSpec.getParent();
if( parent instanceof IASTSimpleDeclaration ){
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) parent;
@@ -84,7 +88,7 @@ public class CVisitor {
private static IBinding findBinding( IASTFieldReference fieldReference ){
IASTExpression fieldOwner = fieldReference.getFieldOwner();
ICompositeType compositeType = null;
- if( fieldOwner instanceof CASTIdExpression ){
+ if( fieldOwner instanceof IASTIdExpression ){
IBinding binding = resolveBinding( fieldOwner );
if( binding instanceof IVariable ){
binding = ((IVariable)binding).getType();
@@ -106,41 +110,34 @@ public class CVisitor {
* @param parent
* @return
*/
- private static IBinding createBinding(CASTDeclarator declarator, CASTName name) {
+ private static IBinding createBinding(IASTDeclarator declarator, CASTName name) {
+ IBinding binding = null;
IASTNode parent = declarator.getParent();
-
- if( parent instanceof CASTSimpleDeclaration ){
- return createBinding( (CASTSimpleDeclaration) parent, name );
- } else if( parent instanceof CASTFunctionDefinition ){
- return createBinding( (CASTFunctionDefinition) parent );
- } else if( parent instanceof CASTParameterDeclaration ){
- return createBinding( (CASTParameterDeclaration ) parent );
+ if( declarator instanceof IASTFunctionDeclarator ){
+ binding = resolveBinding( parent, CURRENT_SCOPE );
+ if( binding == null )
+ binding = new CFunction( (IASTFunctionDeclarator) declarator );
+ } else if( parent instanceof IASTSimpleDeclaration ){
+ binding = createBinding( (IASTSimpleDeclaration) parent, name );
+ } else if( parent instanceof IASTParameterDeclaration ){
+ binding = createBinding( (IASTParameterDeclaration ) parent );
}
- return null;
+ return binding;
}
- private static IBinding createBinding( CASTCompositeTypeSpecifier compositeTypeSpec ){
+ private static IBinding createBinding( ICASTCompositeTypeSpecifier compositeTypeSpec ){
IBinding binding = resolveBinding( compositeTypeSpec, CURRENT_SCOPE );
if( binding == null )
binding = new CStructure( compositeTypeSpec );
return binding;
}
- /**
- * @param definition
- * @return
- */
- private static IBinding createBinding(CASTFunctionDefinition functionDefinition) {
- //TODO need to check for forward declaration first
- CFunction function = new CFunction( functionDefinition );
- return function;
- }
/**
* @param parent
* @return
*/
- private static IBinding createBinding(CASTSimpleDeclaration simpleDeclaration, CASTName name) {
+ private static IBinding createBinding(IASTSimpleDeclaration simpleDeclaration, IASTName name) {
IBinding binding = null;
if( simpleDeclaration.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef ){
binding = new CTypeDef( name );
@@ -153,7 +150,7 @@ public class CVisitor {
return binding;
}
- private static IBinding createBinding( CASTParameterDeclaration parameterDeclaration ){
+ private static IBinding createBinding( IASTParameterDeclaration parameterDeclaration ){
CParameter parameter = new CParameter( parameterDeclaration );
return parameter;
}
@@ -166,16 +163,18 @@ public class CVisitor {
IASTFunctionDefinition functionDef = (IASTFunctionDefinition) node;
IASTFunctionDeclarator functionDeclartor = functionDef.getDeclarator();
IASTName name = functionDeclartor.getName();
- return name.resolveBinding();
- } else if( node instanceof CASTIdExpression ){
IASTNode blockItem = getContainingBlockItem( node );
- return findBinding( blockItem, (CASTName) ((CASTIdExpression)node).getName(), scopeDepth );
- } else if( node instanceof IASTTypedefNameSpecifier ){
+ //return name.resolveBinding();
+ return findBinding( blockItem, (CASTName) name, scopeDepth );
+ } else if( node instanceof IASTIdExpression ){
+ IASTNode blockItem = getContainingBlockItem( node );
+ return findBinding( blockItem, (CASTName) ((IASTIdExpression)node).getName(), scopeDepth );
+ } else if( node instanceof ICASTTypedefNameSpecifier ){
IASTNode blockItem = getContainingBlockItem( node );
- return findBinding( blockItem, (CASTName) ((IASTTypedefNameSpecifier)node).getName(), scopeDepth );
- } else if( node instanceof IASTElaboratedTypeSpecifier ){
+ return findBinding( blockItem, (CASTName) ((ICASTTypedefNameSpecifier)node).getName(), scopeDepth );
+ } else if( node instanceof ICASTElaboratedTypeSpecifier ){
IASTNode blockItem = getContainingBlockItem( node );
- return findBinding( blockItem, (CASTName) ((IASTElaboratedTypeSpecifier)node).getName(), scopeDepth );
+ return findBinding( blockItem, (CASTName) ((ICASTElaboratedTypeSpecifier)node).getName(), scopeDepth );
} else if( node instanceof ICASTCompositeTypeSpecifier ){
IASTNode blockItem = getContainingBlockItem( node );
return findBinding( blockItem, (CASTName) ((ICASTCompositeTypeSpecifier)node).getName(), scopeDepth );
@@ -200,10 +199,11 @@ public class CVisitor {
public static IScope getContainingScope( IASTStatement statement ){
IASTNode parent = statement.getParent();
- if( parent instanceof CASTCompoundStatement ){
+ if( parent instanceof IASTCompoundStatement ){
return getContainingScope( (IASTStatement)parent );
- } else if( parent instanceof CASTFunctionDefinition ){
- IFunction function = (IFunction) resolveBinding( parent );
+ } else if( parent instanceof IASTFunctionDefinition ){
+ IASTFunctionDeclarator fnDeclarator = ((IASTFunctionDefinition) parent ).getDeclarator();
+ IFunction function = (IFunction) fnDeclarator.getName().resolveBinding();
return function.getFunctionScope();
}
@@ -220,10 +220,10 @@ public class CVisitor {
*/
public static IScope getContainingScope(IASTParameterDeclaration parameterDeclaration) {
IASTNode parent = parameterDeclaration.getParent();
- if( parent instanceof CASTFunctionDeclarator ){
- CASTFunctionDeclarator functionDeclarator = (CASTFunctionDeclarator) parent;
- parent = functionDeclarator.getParent();
- IFunction function = (IFunction) resolveBinding( parent );
+ if( parent instanceof IASTFunctionDeclarator ){
+ IASTFunctionDeclarator functionDeclarator = (IASTFunctionDeclarator) parent;
+ IASTName fnName = functionDeclarator.getName();
+ IFunction function = (IFunction) fnName.resolveBinding();
return function.getFunctionScope();
}
@@ -234,11 +234,17 @@ public class CVisitor {
IASTNode parent = node.getParent();
if( parent instanceof IASTDeclaration ){
IASTNode p = parent.getParent();
- if( p instanceof CASTDeclarationStatement )
+ if( p instanceof IASTDeclarationStatement )
return p;
return parent;
- } else if ( parent instanceof CASTCompoundStatement || parent instanceof CASTTranslationUnit )
+ }
+ //if parent is something that can contain a declaration
+ else if ( parent instanceof IASTCompoundStatement ||
+ parent instanceof IASTTranslationUnit ||
+ parent instanceof IASTForStatement )
+ {
return node;
+ }
return getContainingBlockItem( parent );
}
@@ -249,11 +255,11 @@ public class CVisitor {
IASTNode parent = blockItem.getParent();
List list = null;
- if( parent instanceof CASTCompoundStatement ){
- CASTCompoundStatement compound = (CASTCompoundStatement) parent;
+ if( parent instanceof IASTCompoundStatement ){
+ IASTCompoundStatement compound = (IASTCompoundStatement) parent;
list = compound.getStatements();
- } else if ( parent instanceof CASTTranslationUnit ){
- CASTTranslationUnit translation = (CASTTranslationUnit) parent;
+ } else if ( parent instanceof IASTTranslationUnit ){
+ IASTTranslationUnit translation = (IASTTranslationUnit) parent;
list = translation.getDeclarations();
}
@@ -262,11 +268,11 @@ public class CVisitor {
IASTNode node = (IASTNode) list.get(i);
if( node == blockItem )
break;
- if( node instanceof CASTDeclarationStatement ){
- CASTDeclarationStatement declStatement = (CASTDeclarationStatement) node;
- binding = findBinding( declStatement.getDeclaration(), name );
+ if( node instanceof IASTDeclarationStatement ){
+ IASTDeclarationStatement declStatement = (IASTDeclarationStatement) node;
+ binding = checkForBinding( declStatement.getDeclaration(), name );
} else if( node instanceof IASTDeclaration ){
- binding = findBinding( (IASTDeclaration) node, name );
+ binding = checkForBinding( (IASTDeclaration) node, name );
}
if( binding != null )
return binding;
@@ -274,7 +280,11 @@ public class CVisitor {
} else {
//check the parent
if( parent instanceof IASTDeclaration ){
- binding = findBinding( (IASTDeclaration) parent, name );
+ binding = checkForBinding( (IASTDeclaration) parent, name );
+ if( binding != null )
+ return binding;
+ } else if( parent instanceof IASTStatement ){
+ binding = checkForBinding( (IASTStatement) parent, name );
if( binding != null )
return binding;
}
@@ -288,14 +298,14 @@ public class CVisitor {
return null;
}
- private static IBinding findBinding( IASTDeclaration declaration, CASTName name ){
- if( declaration instanceof CASTSimpleDeclaration ){
- CASTSimpleDeclaration simpleDeclaration = (CASTSimpleDeclaration) declaration;
+ private static IBinding checkForBinding( IASTDeclaration declaration, CASTName name ){
+ if( declaration instanceof IASTSimpleDeclaration ){
+ IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
List declarators = simpleDeclaration.getDeclarators();
int size = declarators.size();
if( size > 0 ){
for( int i = 0; i < declarators.size(); i++ ){
- CASTDeclarator declarator = (CASTDeclarator) declarators.get(i);
+ IASTDeclarator declarator = (IASTDeclarator) declarators.get(i);
CASTName declaratorName = (CASTName) declarator.getName();
if( CharArrayUtils.equals( declaratorName.toCharArray(), name.toCharArray() ) ){
return declaratorName.resolveBinding();
@@ -304,16 +314,16 @@ public class CVisitor {
} else {
//forward declaration
IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier();
- if( declSpec instanceof IASTElaboratedTypeSpecifier ){
- CASTName elabName = (CASTName) ((IASTElaboratedTypeSpecifier)declSpec).getName();
+ if( declSpec instanceof ICASTElaboratedTypeSpecifier ){
+ CASTName elabName = (CASTName) ((ICASTElaboratedTypeSpecifier)declSpec).getName();
if( CharArrayUtils.equals( elabName.toCharArray(), name.toCharArray() ) ){
return elabName.resolveBinding();
}
}
}
- } else if( declaration instanceof CASTFunctionDefinition ){
- CASTFunctionDefinition functionDef = (CASTFunctionDefinition) declaration;
- CASTFunctionDeclarator declarator = (CASTFunctionDeclarator) functionDef.getDeclarator();
+ } else if( declaration instanceof IASTFunctionDefinition ){
+ IASTFunctionDefinition functionDef = (IASTFunctionDefinition) declaration;
+ IASTFunctionDeclarator declarator = functionDef.getDeclarator();
//check the function itself
CASTName declName = (CASTName) declarator.getName();
@@ -323,7 +333,7 @@ public class CVisitor {
//check the parameters
List parameters = declarator.getParameters();
for( int i = 0; i < parameters.size(); i++ ){
- CASTParameterDeclaration parameterDeclaration = (CASTParameterDeclaration) parameters.get(i);
+ IASTParameterDeclaration parameterDeclaration = (IASTParameterDeclaration) parameters.get(i);
declName = (CASTName) parameterDeclaration.getDeclarator().getName();
if( CharArrayUtils.equals( declName.toCharArray(), name.toCharArray() ) ){
return declName.resolveBinding();
@@ -333,16 +343,34 @@ public class CVisitor {
return null;
}
- protected static IASTDeclSpecifier findDefinition( IASTElaboratedTypeSpecifier declSpec ){
+ private static IBinding checkForBinding( IASTStatement statement, CASTName name ){
+ if( statement instanceof IASTForStatement ){
+ IASTForStatement forStatement = (IASTForStatement) statement;
+ if( forStatement.getInitDeclaration() != null ){
+ return checkForBinding( forStatement.getInitDeclaration(), name );
+ }
+ }
+
+ return null;
+ }
+
+ protected static IASTFunctionDeclarator findDefinition( IASTFunctionDeclarator declarator ){
+ return (IASTFunctionDeclarator) findDefinition( declarator, declarator.getName().toString() );
+ }
+ protected static IASTDeclSpecifier findDefinition( ICASTElaboratedTypeSpecifier declSpec ){
String elabName = declSpec.getName().toString();
- IASTNode blockItem = getContainingBlockItem( declSpec );
+ return (IASTDeclSpecifier) findDefinition(declSpec, elabName);
+ }
+
+ private static IASTNode findDefinition(IASTNode decl, String declName) {
+ IASTNode blockItem = getContainingBlockItem( decl );
IASTNode parent = blockItem.getParent();
List list = null;
- if( parent instanceof CASTCompoundStatement ){
- CASTCompoundStatement compound = (CASTCompoundStatement) parent;
+ if( parent instanceof IASTCompoundStatement ){
+ IASTCompoundStatement compound = (IASTCompoundStatement) parent;
list = compound.getStatements();
- } else if ( parent instanceof CASTTranslationUnit ){
- CASTTranslationUnit translation = (CASTTranslationUnit) parent;
+ } else if ( parent instanceof IASTTranslationUnit ){
+ IASTTranslationUnit translation = (IASTTranslationUnit) parent;
list = translation.getDeclarations();
}
if( list != null ){
@@ -350,16 +378,22 @@ public class CVisitor {
IASTNode node = (IASTNode) list.get(i);
if( node == blockItem )
continue;
- if( node instanceof CASTDeclarationStatement ){
- node = ((CASTDeclarationStatement) node).getDeclaration();
+ if( node instanceof IASTDeclarationStatement ){
+ node = ((IASTDeclarationStatement) node).getDeclaration();
}
- if( node instanceof IASTSimpleDeclaration ){
+ if( node instanceof IASTFunctionDefinition && decl instanceof IASTFunctionDeclarator ){
+ IASTFunctionDeclarator dtor = ((IASTFunctionDefinition) node).getDeclarator();
+ IASTName name = dtor.getName();
+ if( name.toString().equals( declName )){
+ return dtor;
+ }
+ } else if( node instanceof IASTSimpleDeclaration && decl instanceof ICASTElaboratedTypeSpecifier){
IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) node;
if( simpleDecl.getDeclSpecifier() instanceof ICASTCompositeTypeSpecifier ){
ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) simpleDecl.getDeclSpecifier();
IASTName name = compTypeSpec.getName();
- if( name.toString().equals( elabName ) ){
+ if( name.toString().equals( declName ) ){
return compTypeSpec;
}
}

Back to the top