Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Niefer2004-12-14 20:17:23 +0000
committerAndrew Niefer2004-12-14 20:17:23 +0000
commitb6fa19a6f74a2b3b314c56e0e2b37558905eb056 (patch)
tree219de7c869c3e13c9df5acfb94d2689d2edff281
parentb0f2b85991ad30989e4005bf2337099cafdd7955 (diff)
downloadorg.eclipse.cdt-b6fa19a6f74a2b3b314c56e0e2b37558905eb056.tar.gz
org.eclipse.cdt-b6fa19a6f74a2b3b314c56e0e2b37558905eb056.tar.xz
org.eclipse.cdt-b6fa19a6f74a2b3b314c56e0e2b37558905eb056.zip
for statements, field references
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java53
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTForStatement.java8
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBlockScope.java31
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java6
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java25
5 files changed, 87 insertions, 36 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
index 3ab8454e9c2..87251c3a45d 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
@@ -541,5 +541,58 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances( collector, f, 2 );
assertInstances( collector, a, 3 );
}
+ 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$
+ buffer.append( " f(); \n" ); //$NON-NLS-1$
+ buffer.append( "} \n" ); //$NON-NLS-1$
+ buffer.append( "void f(){ } \n" ); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
+ CPPNameCollector collector = new CPPNameCollector();
+ CPPVisitor.visitTranslationUnit( tu, collector );
+
+ IFunction f = (IFunction) collector.getName(0).resolveBinding();
+ IFunction g = (IFunction) collector.getName( 1 ).resolveBinding();
+
+ assertInstances( collector, f, 3 );
+ assertInstances( collector, g, 1 );
+ }
+
+ 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.CPP );
+ CPPNameCollector collector = new CPPNameCollector();
+ CPPVisitor.visitTranslationUnit( tu, collector );
+
+ IVariable i = (IVariable) collector.getName(1).resolveBinding();
+
+ assertInstances( collector, i, 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.CPP );
+ CPPNameCollector collector = new CPPNameCollector();
+ CPPVisitor.visitTranslationUnit( tu, collector );
+
+ ICPPClassType A = (ICPPClassType) collector.getName(0).resolveBinding();
+ IField x = (IField) collector.getName(1).resolveBinding();
+
+ assertInstances( collector, A, 2 );
+ assertInstances( collector, x, 2 );
+ }
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTForStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTForStatement.java
index 5d4b5369223..883fdd7cdde 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTForStatement.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTForStatement.java
@@ -20,7 +20,8 @@ import org.eclipse.cdt.core.dom.ast.IScope;
* @author jcamelon
*/
public class CPPASTForStatement extends CPPASTNode implements IASTForStatement {
-
+ private IScope scope = null;
+
private IASTExpression initialExpression;
private IASTDeclaration initDeclaration;
private IASTExpression condition;
@@ -102,8 +103,9 @@ public class CPPASTForStatement extends CPPASTNode implements IASTForStatement {
* @see org.eclipse.cdt.core.dom.ast.IASTForStatement#getScope()
*/
public IScope getScope() {
- // TODO Auto-generated method stub
- return null;
+ if( scope == null )
+ scope = new CPPBlockScope( this );
+ return scope;
}
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBlockScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBlockScope.java
index ff15d1385b0..2707fd4f78a 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBlockScope.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBlockScope.java
@@ -13,45 +13,14 @@
*/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
-import java.util.List;
-
import org.eclipse.cdt.core.dom.ast.IASTNode;
-import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
-import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
/**
* @author aniefer
*/
public class CPPBlockScope extends CPPNamespaceScope implements ICPPBlockScope {
- private CharArrayObjectMap bindings = CharArrayObjectMap.EMPTY_MAP;
-
public CPPBlockScope( IASTNode physicalNode ){
super( physicalNode );
}
- /* (non-Javadoc)
- * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#addBinding(org.eclipse.cdt.core.dom.ast.IBinding)
- */
- public void addBinding(IBinding binding) {
- if( bindings == CharArrayObjectMap.EMPTY_MAP )
- bindings = new CharArrayObjectMap(1);
- bindings.put( binding.getNameCharArray(), binding );
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#getBinding(int, char[])
- */
- public IBinding getBinding(int namespaceType, char[] name) {
- // TODO Auto-generated method stub
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
- */
- public List find(String name) {
- // TODO Auto-generated method stub
- return null;
- }
-
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java
index e31862ac621..6ca7e169ed3 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java
@@ -223,6 +223,9 @@ public class CPPSemantics {
}
}
static protected IBinding resolveBinding( IASTName name ){
+ if( name.toCharArray().length == 0 )
+ return null;
+
//1: get some context info off of the name to figure out what kind of lookup we want
LookupData data = createLookupData( name );
@@ -260,7 +263,8 @@ public class CPPSemantics {
}
}
} else if( parent instanceof IASTDeclarator ){
- data.forDefinition = true;
+ if( parent.getParent() instanceof IASTSimpleDeclaration )
+ data.forDefinition = true;
} else if ( parent instanceof ICPPASTBaseSpecifier ||
parent instanceof ICPPASTElaboratedTypeSpecifier)
{
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
index 4c783b0611f..98c2bfe87c9 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
@@ -147,6 +147,8 @@ public class CPPVisitor {
if( binding != null )
return binding;
}
+ } else if( parent instanceof IASTTypeId ){
+ return CPPSemantics.resolveBinding( elabType.getName() );
}
ICPPScope scope = (ICPPScope) getContainingScope( elabType );
@@ -191,6 +193,10 @@ public class CPPVisitor {
private static IBinding createBinding( IASTDeclarator declarator ){
IASTNode parent = declarator.getParent();
+
+ if( parent instanceof IASTTypeId )
+ return CPPSemantics.resolveBinding( declarator.getName() );
+
ICPPScope scope = (ICPPScope) getContainingScope( parent );
IBinding binding = ( scope != null ) ? scope.getBinding( declarator.getName() ) : null;
@@ -249,6 +255,8 @@ public class CPPVisitor {
return getContainingScope( (IASTDeclSpecifier) node );
else if( node instanceof IASTParameterDeclaration )
return getContainingScope( (IASTParameterDeclaration) node );
+ else if( node instanceof IASTExpression )
+ return getContainingScope( (IASTExpression) node );
else if( node instanceof IASTEnumerator ){
//put the enumerators in the same scope as the enumeration
return getContainingScope( (IASTEnumerationSpecifier) node.getParent() );
@@ -257,6 +265,15 @@ public class CPPVisitor {
return getContainingScope( node.getParent() );
}
+ public static IScope getContainingScope( IASTExpression expression ){
+ IASTNode parent = expression.getParent();
+ if( parent instanceof IASTForStatement ){
+ return ((IASTForStatement)parent).getScope();
+ } else if( parent instanceof IASTCompoundStatement ){
+ return ((IASTCompoundStatement)parent).getScope();
+ }
+ return getContainingScope( parent );
+ }
public static IScope getContainingScope( IASTName name ){
IASTNode parent = name.getParent();
if( parent instanceof ICPPASTQualifiedName ){
@@ -329,7 +346,11 @@ public class CPPVisitor {
public static IScope getContainingScope( IASTDeclSpecifier compTypeSpec ){
IASTNode parent = compTypeSpec.getParent();
- return getContainingScope( (IASTSimpleDeclaration) parent );
+ if( parent instanceof IASTSimpleDeclaration )
+ return getContainingScope( (IASTSimpleDeclaration) parent );
+ else if( parent instanceof IASTTypeId )
+ return getContainingScope( parent.getParent() );
+ return null;
}
/**
@@ -837,6 +858,8 @@ public class CPPVisitor {
declSpec = ((IASTParameterDeclaration) parent).getDeclSpecifier();
else if( parent instanceof IASTSimpleDeclaration )
declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier();
+ else if( parent instanceof IASTTypeId )
+ declSpec = ((IASTTypeId)parent).getDeclSpecifier();
IType type = createType( declSpec );

Back to the top