Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] support removing IBindings from IScopes


This patch adds support for removing IBindings from IScopes (#1-1 on Andrew's New Parser todo list).

Devin Steffler
IBM's Eclipse CDT
Ottawa (Palladium), Ontario, Canada


Index: parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java,v
retrieving revision 1.26
diff -u -r1.26 AST2Tests.java
--- parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java	21 Dec 2004 20:45:55 -0000	1.26
+++ parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java	5 Jan 2005 16:02:59 -0000
@@ -68,6 +68,7 @@
 import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
 import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
 import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope;
+import org.eclipse.cdt.core.dom.ast.c.ICScope;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
 import org.eclipse.cdt.core.parser.ParserLanguage;
 import org.eclipse.cdt.internal.core.dom.parser.c.CFunction;
@@ -153,7 +154,7 @@
 		IFunction func_f = (IFunction) name_f.resolveBinding();
 		assertEquals(globalScope, func_f.getScope());
 		IParameter var_y = (IParameter) name_y.resolveBinding();
-		assertEquals(func_f.getFunctionScope(), var_y.getScope());
+		assertEquals(((IASTCompoundStatement)funcdef_f.getBody()).getScope(), var_y.getScope());
 
 		IVariable var_z = (IVariable) name_z.resolveBinding();
 		assertEquals(((ICFunctionScope)func_f.getFunctionScope()).getBodyScope(), var_z.getScope());
@@ -162,6 +163,15 @@
 		assertEquals(var_x, name_ref_x.resolveBinding());
 		assertEquals(var_y, name_ref_y.resolveBinding());
 
+		assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)body_f.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("z").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)body_f.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$
+		CVisitor.clearBindings(tu);
+		assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)body_f.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("z").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)body_f.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$
 	}
 
     public void testSimpleStruct() throws ParserException {
@@ -500,7 +510,19 @@
         assertNotNull( x2 );
         assertSame( x1, x3 );
         assertNotSame( x2, x3 );
+
+        IASTDeclarator decl_i = declaration.getDeclarators()[0];
+        decl_i.getName().resolveBinding(); // add i's binding to the scope
         
+		assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_TAG, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)compound.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNotNull( ((ICScope)compound.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$
+		CVisitor.clearBindings(tu);
+		assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_TAG, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)compound.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
+		assertNull( ((ICScope)compound.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$
     }
     public void testFunctionParameters() throws Exception {
     	StringBuffer buffer  = new StringBuffer();
Index: parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java,v
retrieving revision 1.1
diff -u -r1.1 ICScope.java
--- parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java	25 Nov 2004 22:07:17 -0000	1.1
+++ parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java	5 Jan 2005 16:00:53 -0000
@@ -25,5 +25,6 @@
     public static final int NAMESPACE_TYPE_OTHER = 1;
     
     void addBinding( IBinding binding );
+    void removeBinding( IBinding binding );
     public IBinding getBinding( int namespaceType, char [] name );
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java,v
retrieving revision 1.1
diff -u -r1.1 CCompositeTypeScope.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java	10 Dec 2004 03:53:09 -0000	1.1
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java	5 Jan 2005 16:00:53 -0000
@@ -72,4 +72,13 @@
         // TODO Auto-generated method stub
         return null;
     }
+    
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.dom.ast.c.ICScope#removeBinding(org.eclipse.cdt.core.dom.ast.IBinding)
+	 */
+	public void removeBinding(IBinding binding) {
+		if( bindings != CharArrayObjectMap.EMPTY_MAP ) {
+			bindings.remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
+		}
+	}
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java,v
retrieving revision 1.1
diff -u -r1.1 CFunctionScope.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java	10 Dec 2004 03:53:09 -0000	1.1
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java	5 Jan 2005 16:00:53 -0000
@@ -114,5 +114,12 @@
         }
 	}
 
-
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.dom.ast.c.ICScope#removeBinding(org.eclipse.cdt.core.dom.ast.IBinding)
+	 */
+	public void removeBinding(IBinding binding) {
+		if( bindings != CharArrayObjectMap.EMPTY_MAP ) {
+			bindings.remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
+		}
+	}
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java,v
retrieving revision 1.1
diff -u -r1.1 CScope.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java	10 Dec 2004 03:53:09 -0000	1.1
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java	5 Jan 2005 16:00:53 -0000
@@ -63,4 +63,16 @@
     public IBinding getBinding( int namespaceType, char [] name ){
         return (IBinding) bindings[namespaceType].get( name );
     }
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.dom.ast.c.ICScope#removeBinding(org.eclipse.cdt.core.dom.ast.IBinding)
+	 */
+	public void removeBinding(IBinding binding) {
+        int type = ( binding instanceof ICompositeType || binding instanceof IEnumeration ) ? 
+				NAMESPACE_TYPE_TAG : NAMESPACE_TYPE_OTHER;
+
+		if( bindings[type] != CharArrayObjectMap.EMPTY_MAP ) {
+			bindings[type].remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
+		}
+	}
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java,v
retrieving revision 1.6
diff -u -r1.6 CVisitor.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	21 Dec 2004 16:27:04 -0000	1.6
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	5 Jan 2005 16:00:54 -0000
@@ -124,6 +124,20 @@
 			return true;
 		}
 	}
+
+	public static class ClearBindingFromScopeAction extends CBaseVisitorAction {
+		{
+			processNames = true;
+		}
+		public boolean processName(IASTName name) {
+			if ( name.resolveBinding() != null ) {
+				 ICScope scope = (ICScope)name.resolveBinding().getScope();
+				 if ( scope != null )
+				 	scope.removeBinding(name.resolveBinding()); 
+			}
+			return true;
+		}
+	}
 	
 	//lookup bits
 	private static final int COMPLETE = 0;		
@@ -260,6 +274,18 @@
 			binding = createBinding( (IASTSimpleDeclaration) parent, name );
 		} else if( parent instanceof IASTParameterDeclaration ){
 			binding = createBinding( (IASTParameterDeclaration ) parent );
+			
+			// C99 6.2.1-4: within the list of parameter declarations in a function definition, the 
+			// identifier has block scope, which terminates at the end of the associated block.
+			parent = parent.getParent();
+			if ( parent instanceof IASTFunctionDeclarator ) {
+				parent = parent.getParent();
+				if ( parent instanceof IASTFunctionDefinition ) {
+					ICScope scope = (ICScope) ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope();
+					if ( scope != null && binding != null )
+						scope.addBinding(binding);
+				}
+			}
 		} else if ( parent instanceof IASTFunctionDeclarator ) {
 			binding = createBinding(declarator);
 		}
@@ -477,10 +503,10 @@
 	public static IScope getContainingScope(IASTParameterDeclaration parameterDeclaration) {
 		IASTNode parent = parameterDeclaration.getParent();
 		if( parent instanceof IASTFunctionDeclarator ){
-			IASTFunctionDeclarator functionDeclarator = (IASTFunctionDeclarator) parent;
-			IASTName fnName = functionDeclarator.getName();
-			IFunction function = (IFunction) fnName.resolveBinding();
-			return function.getFunctionScope();
+			parent = ((IASTDeclarator)parent).getParent();
+			if ( parent instanceof IASTFunctionDefinition ) {
+				return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope();
+			}
 		}
 		
 		return null;
@@ -718,7 +744,8 @@
 	}
 	
 	public static void clearBindings( IASTTranslationUnit tu ){
-		visitTranslationUnit( tu, new ClearBindingAction() ); 
+		visitTranslationUnit( tu, new ClearBindingFromScopeAction() );
+		visitTranslationUnit( tu, new ClearBindingAction() );
 	}
 	
 	public static void visitTranslationUnit( IASTTranslationUnit tu, CBaseVisitorAction action ){

Back to the top