Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Niefer2004-11-16 13:55:28 -0500
committerAndrew Niefer2004-11-16 13:55:28 -0500
commit5d1334ab11ea55c3ea56f7b23d048233e79a20b3 (patch)
treea2e2038a764df929fd9f082c0bac43430ad07ee8
parent6166dbe1fef681ebf297b51b810b1bf723b7fcdb (diff)
downloadorg.eclipse.cdt-5d1334ab11ea55c3ea56f7b23d048233e79a20b3.tar.gz
org.eclipse.cdt-5d1334ab11ea55c3ea56f7b23d048233e79a20b3.tar.xz
org.eclipse.cdt-5d1334ab11ea55c3ea56f7b23d048233e79a20b3.zip
bindings from Unary Id Expression
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java26
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser2/c/CVisitor.java66
2 files changed, 71 insertions, 21 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 41afc86d09..ef6e38c401 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
@@ -789,4 +789,30 @@ public class AST2Tests extends TestCase {
assertSame( var_2, var_3 );
assertSame( var_3, var_4 );
}
+
+ public void testExpressionFieldReference() throws Exception{
+ StringBuffer buffer = new StringBuffer();
+ buffer.append( "struct A { int x; }; \n"); //$NON-NLS-1$
+ buffer.append( "void f(){ \n"); //$NON-NLS-1$
+ buffer.append( " ((struct A *) 1)->x; \n"); //$NON-NLS-1$
+ buffer.append( "} \n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
+
+ IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
+ IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) simpleDecl.getDeclSpecifier();
+ IASTSimpleDeclaration decl_x = (IASTSimpleDeclaration) compType.getMembers().get(0);
+ IASTName name_x1 = ((IASTDeclarator) decl_x.getDeclarators().get(0)).getName();
+ IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
+ IASTCompoundStatement body = (IASTCompoundStatement) fdef.getBody();
+ IASTExpressionStatement expStatement = (IASTExpressionStatement) body.getStatements().get(0);
+ IASTFieldReference fieldRef = (IASTFieldReference) expStatement.getExpression();
+ IASTName name_x2 = fieldRef.getFieldName();
+
+ IField x1 = (IField) name_x1.resolveBinding();
+ IField x2 = (IField) name_x2.resolveBinding();
+
+ assertNotNull( x1 );
+ assertSame( x1, x2 );
+ }
}
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 e5a8cfbb8d..e9a7ca2ebc 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
@@ -102,6 +102,9 @@ public class CVisitor {
return binding;
}
return resolveBinding( elabTypeSpec );
+ } else if( parent instanceof IASTTypeId ){
+ IASTNode blockItem = getContainingBlockItem( parent );
+ return findBinding( blockItem, (CASTName) elabTypeSpec.getName(), COMPLETE );
}
return null;
}
@@ -117,6 +120,12 @@ public class CVisitor {
}
if( binding instanceof ICompositeType )
compositeType = (ICompositeType) binding;
+ } else if( fieldOwner instanceof IASTUnaryTypeIdExpression ){
+ IASTTypeId id = ((IASTUnaryTypeIdExpression)fieldOwner).getTypeId();
+ IBinding binding = resolveBinding( id );
+ if( binding != null && binding instanceof ICompositeType ){
+ compositeType = (ICompositeType) binding;
+ }
}
IBinding binding = null;
@@ -147,11 +156,9 @@ public class CVisitor {
}
private static IBinding createBinding( ICASTCompositeTypeSpecifier compositeTypeSpec ){
- IBinding binding = resolveBinding( compositeTypeSpec, CURRENT_SCOPE );
- if( binding == null )
- binding = new CStructure( compositeTypeSpec );
- return binding;
+ return new CStructure( compositeTypeSpec );
}
+
/**
* @param parent
@@ -213,6 +220,18 @@ public class CVisitor {
return pdef.getDeclarator().getName().resolveBinding();
}
}
+ } else if( node instanceof IASTTypeId ){
+ IASTTypeId typeId = (IASTTypeId) node;
+ IASTDeclSpecifier declSpec = typeId.getDeclSpecifier();
+ IASTName name = null;
+ if( declSpec instanceof ICASTElaboratedTypeSpecifier ){
+ name = ((ICASTElaboratedTypeSpecifier)declSpec).getName();
+ } else if( declSpec instanceof ICASTCompositeTypeSpecifier ){
+ name = ((ICASTCompositeTypeSpecifier)declSpec).getName();
+ }
+ if( name != null ){
+ return name.resolveBinding();
+ }
}
return null;
}
@@ -338,22 +357,26 @@ public class CVisitor {
IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
List declarators = simpleDeclaration.getDeclarators();
int size = declarators.size();
- if( size > 0 ){
- for( int i = 0; i < declarators.size(); i++ ){
- IASTDeclarator declarator = (IASTDeclarator) declarators.get(i);
- CASTName declaratorName = (CASTName) declarator.getName();
- if( CharArrayUtils.equals( declaratorName.toCharArray(), name.toCharArray() ) ){
- return declaratorName.resolveBinding();
- }
+
+ for( int i = 0; i < size; i++ ){
+ IASTDeclarator declarator = (IASTDeclarator) declarators.get(i);
+ CASTName declaratorName = (CASTName) declarator.getName();
+ if( CharArrayUtils.equals( declaratorName.toCharArray(), name.toCharArray() ) ){
+ return declaratorName.resolveBinding();
}
- } else {
- //forward declaration
- IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier();
- if( declSpec instanceof ICASTElaboratedTypeSpecifier ){
- CASTName elabName = (CASTName) ((ICASTElaboratedTypeSpecifier)declSpec).getName();
- if( CharArrayUtils.equals( elabName.toCharArray(), name.toCharArray() ) ){
- return elabName.resolveBinding();
- }
+ }
+
+ //decl spec
+ IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier();
+ if( declSpec instanceof ICASTElaboratedTypeSpecifier ){
+ CASTName elabName = (CASTName) ((ICASTElaboratedTypeSpecifier)declSpec).getName();
+ if( CharArrayUtils.equals( elabName.toCharArray(), name.toCharArray() ) ){
+ return elabName.resolveBinding();
+ }
+ } else if( declSpec instanceof ICASTCompositeTypeSpecifier ){
+ CASTName compName = (CASTName) ((ICASTCompositeTypeSpecifier)declSpec).getName();
+ if( CharArrayUtils.equals( compName.toCharArray(), name.toCharArray() ) ){
+ return compName.resolveBinding();
}
}
} else if( declaration instanceof IASTFunctionDefinition ){
@@ -379,13 +402,14 @@ public class CVisitor {
}
private static IBinding checkForBinding( IASTStatement statement, CASTName name ){
- if( statement instanceof IASTForStatement ){
+ if( statement instanceof IASTDeclarationStatement ){
+ return checkForBinding( ((IASTDeclarationStatement)statement).getDeclaration(), name );
+ } else if( statement instanceof IASTForStatement ){
IASTForStatement forStatement = (IASTForStatement) statement;
if( forStatement.getInitDeclaration() != null ){
return checkForBinding( forStatement.getInitDeclaration(), name );
}
}
-
return null;
}

Back to the top