From e8ba635111ad80e43fd42b2d40a03e1fe0e2cc4e Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Wed, 23 Mar 2005 15:26:11 +0000 Subject: [PATCH] caching of IASTNames in C --- .../core/parser/tests/ast2/AST2KnRTests.java | 70 +- .../cdt/core/parser/tests/ast2/AST2Tests.java | 71 +- .../eclipse/cdt/core/dom/ast/c/ICScope.java | 56 +- .../parser/c/CASTElaboratedTypeSpecifier.java | 2 +- .../dom/parser/c/CCompositeTypeScope.java | 55 +- .../core/dom/parser/c/CEnumeration.java | 2 + .../core/dom/parser/c/CEnumerator.java | 17 +- .../core/dom/parser/c/CFunctionScope.java | 62 +- .../internal/core/dom/parser/c/CLabel.java | 14 +- .../internal/core/dom/parser/c/CScope.java | 91 ++- .../core/dom/parser/c/CStructure.java | 113 +-- .../internal/core/dom/parser/c/CVisitor.java | 747 ++++++------------ .../eclipse/cdt/ui/tests/DOMAST/DOMAST.java | 27 +- .../cdt/ui/tests/DOMAST/TreeObject.java | 141 +++- 14 files changed, 662 insertions(+), 806 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2KnRTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2KnRTests.java index f0d73a7add4..c6b3f14216e 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2KnRTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2KnRTests.java @@ -41,10 +41,10 @@ import org.eclipse.cdt.core.dom.ast.IPointerType; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICBasicType; -import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.internal.core.dom.parser.c.CParameter; +import org.eclipse.cdt.internal.core.dom.parser.c.CScope; import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor; import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding; @@ -83,11 +83,11 @@ public class AST2KnRTests extends AST2BaseTest { assertEquals( decls[0], ((IASTStandardFunctionDeclarator)f1.getDeclarators()[0]).getParameters()[0].getDeclarator().getName() ); assertEquals( decls[1], ((IASTSimpleDeclaration)((ICASTKnRFunctionDeclarator)f2.getDeclarator()).getParameterDeclarations()[0]).getDeclarators()[0].getName() ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ } @@ -118,11 +118,11 @@ public class AST2KnRTests extends AST2BaseTest { assertEquals( decls.length, 1 ); assertEquals( decls[0], ((IASTSimpleDeclaration)((ICASTKnRFunctionDeclarator)f2.getDeclarator()).getParameterDeclarations()[0]).getDeclarators()[0].getName() ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ } @@ -190,13 +190,13 @@ public class AST2KnRTests extends AST2BaseTest { assertEquals( decls.length, 1 ); assertEquals( decls[0], x1.getName() ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("y").toCharArray()) ); //$NON-NLS-1$ } @@ -213,7 +213,7 @@ public class AST2KnRTests extends AST2BaseTest { IASTSimpleDeclaration isroot_decl = (IASTSimpleDeclaration)tu.getDeclarations()[1]; IASTFunctionDefinition isroot_def = (IASTFunctionDefinition)tu.getDeclarations()[2]; - IASTName x0 = ((IASTStandardFunctionDeclarator)isroot_decl.getDeclarators()[0]).getParameters()[0].getDeclarator().getName(); + //IASTName x0 = ((IASTStandardFunctionDeclarator)isroot_decl.getDeclarators()[0]).getParameters()[0].getDeclarator().getName(); IASTName x1 = ((ICASTKnRFunctionDeclarator)isroot_def.getDeclarator()).getParameterNames()[0]; IASTName x2 = ((IASTSimpleDeclaration)((ICASTKnRFunctionDeclarator)isroot_def.getDeclarator()).getParameterDeclarations()[0]).getDeclarators()[0].getName(); IASTName x3 = ((IASTIdExpression)((IASTBinaryExpression)((IASTReturnStatement)((IASTCompoundStatement)isroot_def.getBody()).getStatements()[0]).getReturnValue()).getOperand1()).getName(); @@ -249,13 +249,13 @@ public class AST2KnRTests extends AST2BaseTest { assertEquals( decls.length, 1 ); assertEquals( decls[0], x2 ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("c").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("c").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("c").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("c").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ } public void testKRCProblem1() throws Exception { @@ -313,11 +313,11 @@ public class AST2KnRTests extends AST2BaseTest { IASTName[] decls = tu.getDeclarations(((IASTIdExpression)((IASTBinaryExpression)((IASTReturnStatement)((IASTCompoundStatement)f.getBody()).getStatements()[0]).getReturnValue()).getOperand1()).getName().resolveBinding()); assertEquals( decls.length, 0 ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("i").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ } public void testKRCProblem3() throws Exception { @@ -453,7 +453,7 @@ public class AST2KnRTests extends AST2BaseTest { IASTName A_struct_name1 = ((IASTCompositeTypeSpecifier)A_struct.getDeclSpecifier()).getName(); assertEquals( A_struct_name1.toString(), "A_struct" ); //$NON-NLS-1$ ICompositeType A_struct_type1 = (ICompositeType)A_struct_name1.resolveBinding(); - assertEquals( ((ICInternalBinding)A_struct_type1).getPhysicalNode(), A_struct.getDeclSpecifier() ); + assertEquals( ((ICInternalBinding)A_struct_type1).getPhysicalNode(), ((IASTCompositeTypeSpecifier)A_struct.getDeclSpecifier()).getName() ); IField[] fields = A_struct_type1.getFields(); IField a1 = fields[0]; IField c1 = fields[1]; @@ -483,7 +483,7 @@ public class AST2KnRTests extends AST2BaseTest { IFunction f_fun1 = (IFunction)f_name1.resolveBinding(); assertEquals( f_name1.toString(), "f" ); //$NON-NLS-1$ assertEquals( f_decltor1.getParameters().length, 1 ); - IASTName x0 = f_decltor1.getParameters()[0].getDeclarator().getName(); + //IASTName x0 = f_decltor1.getParameters()[0].getDeclarator().getName(); IASTName A_name2 = ((ICASTTypedefNameSpecifier)f_decltor1.getParameters()[0].getDeclSpecifier()).getName(); assertEquals( A_name2.toString(), "A" ); //$NON-NLS-1$ ITypedef A_var2 = (ITypedef)A_name2.resolveBinding(); @@ -540,14 +540,14 @@ public class AST2KnRTests extends AST2BaseTest { assertEquals( decls.length, 1 ); assertEquals( decls[0], x2 ); - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_TAG, new String("A_struct").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("A").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNotNull( ((ICScope)((IASTCompoundStatement)f_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_TAG, new String("A_struct").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("A").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNotNull( ((CScope)((IASTCompoundStatement)f_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ CVisitor.clearBindings(tu); - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_TAG, new String("A_struct").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)tu.getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ - assertNull( ((ICScope)((IASTCompoundStatement)f_def.getBody()).getScope()).getBinding(ICScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_TAG, new String("A_struct").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").toCharArray()) ); //$NON-NLS-1$ + assertNull( ((CScope)((IASTCompoundStatement)f_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$ } 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 32a0e1e6a6c..a3d05f56457 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 @@ -71,7 +71,6 @@ 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.ICExternalBinding; 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.ICPPASTCastExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; @@ -200,24 +199,24 @@ public class AST2Tests extends AST2BaseTest { assertEquals(decls.length, 1); assertEquals(decls[0], name_y); - // test clearBindings - 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$ +// // test clearBindings +// 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 Exception { @@ -769,23 +768,23 @@ public class AST2Tests extends AST2BaseTest { assertEquals(decls.length, 1); assertEquals(decls[0], declaration2.getDeclarators()[0].getName()); - 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$ +// 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 { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java index cee367a5c02..4194be648d8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICScope.java @@ -15,6 +15,7 @@ package org.eclipse.cdt.core.dom.ast.c; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IScope; @@ -22,25 +23,6 @@ import org.eclipse.cdt.core.dom.ast.IScope; * @author aniefer */ public interface ICScope extends IScope { - /** - * ISO C:99 6.2.3 there are seperate namespaces for various categories of - * identifiers: - label names ( labels have ICFunctionScope ) - tags of - * structures or unions : NAMESPACE_TYPE_TAG - members of structures or - * unions ( members have ICCompositeTypeScope ) - all other identifiers : - * NAMESPACE_TYPE_OTHER - */ - public static final int NAMESPACE_TYPE_TAG = 0; - - public static final int NAMESPACE_TYPE_OTHER = 1; - - /** - * add a binding to this scope - * - * @param binding - * @throws DOMException - */ - void addBinding(IBinding binding) throws DOMException; - /** * remove the given binding from this scope * @@ -50,15 +32,39 @@ public interface ICScope extends IScope { void removeBinding(IBinding binding) throws DOMException; /** - * Get the binding that has previously been added to this scope that matches - * the given name and is in the appropriate namespace + * Add an IASTName to be cached in this scope * - * @param namespaceType : - * either NAMESPACE_TYPE_TAG or NAMESPACE_TYPE_OTHER * @param name - * @return * @throws DOMException */ - public IBinding getBinding(int namespaceType, char[] name) + public void addName(IASTName name) throws DOMException; + + /** + * Get the binding that the given name would resolve to in this scope. Could + * return null if there is no matching binding in this scope, or if resolve == + * false and the appropriate binding has not yet been resolved. + * + * @param name + * @param resolve : + * whether or not to resolve the matching binding if it has not + * been so already. + * @return : the binding in this scope that matches the name, or null + * @throws DOMException + */ + public IBinding getBinding(IASTName name, boolean resolve) throws DOMException; + + /** + * Set whether or not all the names in this scope have been cached + * + * @param b + */ + public void setFullyCached(boolean b) throws DOMException; + + /** + * whether or not this scope's cache contains all the names + * + * @return + */ + public boolean isFullyCached() throws DOMException; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTElaboratedTypeSpecifier.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTElaboratedTypeSpecifier.java index 9494926f39c..71dd1ad6ac4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTElaboratedTypeSpecifier.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTElaboratedTypeSpecifier.java @@ -87,7 +87,7 @@ public class CASTElaboratedTypeSpecifier extends CASTBaseDeclSpecifier implement IBinding binding = name.resolveBinding(); if( binding instanceof ICInternalBinding ){ IASTNode node = ((ICInternalBinding)binding).getPhysicalNode(); - if( node == this ) + if( node == name ) return r_declaration; } return r_reference; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java index 615d57e54a8..9d8a27bfc1b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java @@ -16,56 +16,24 @@ package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; -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.ICCompositeTypeScope; -import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.parser.util.ArrayUtil; -import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; -import org.eclipse.cdt.internal.core.dom.parser.c.CScope.CollectNamesAction; /** * @author aniefer */ -public class CCompositeTypeScope implements ICCompositeTypeScope { - private ICASTCompositeTypeSpecifier compositeTypeSpec = null; - - private CharArrayObjectMap bindings = CharArrayObjectMap.EMPTY_MAP; - +public class CCompositeTypeScope extends CScope implements ICCompositeTypeScope { public CCompositeTypeScope( ICASTCompositeTypeSpecifier compTypeSpec ){ - compositeTypeSpec = compTypeSpec; - } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.c.ICScope#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 ); + super( compTypeSpec ); } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(int, char[]) - */ - public IBinding getBinding( int namespaceType, char[] name ) { - if( namespaceType == ICScope.NAMESPACE_TYPE_OTHER ) - return getBinding( name ); - return null; - } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope#getBinding(char[]) */ public IBinding getBinding( char[] name ) { - return (IBinding) bindings.get( name ); - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IScope#getParent() - */ - public IScope getParent() { - return CVisitor.getContainingScope( compositeTypeSpec ); + return super.getBinding( NAMESPACE_TYPE_OTHER, name ); } /* (non-Javadoc) @@ -73,7 +41,7 @@ public class CCompositeTypeScope implements ICCompositeTypeScope { */ public IBinding[] find( String name ) { CollectNamesAction action = new CollectNamesAction( name.toCharArray() ); - compositeTypeSpec.accept( action ); + getPhysicalNode().accept( action ); IASTName [] names = action.getNames(); IBinding [] result = null; @@ -89,19 +57,4 @@ public class CCompositeTypeScope implements ICCompositeTypeScope { return (IBinding[]) ArrayUtil.trim( IBinding.class, result ); } - - /* (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); - } - } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode() - */ - public IASTNode getPhysicalNode() { - return compositeTypeSpec; - } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumeration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumeration.java index 048aad2295f..bb788a6f2b6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumeration.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumeration.java @@ -41,12 +41,14 @@ public class CEnumeration implements IEnumeration { declarations = new IASTName[] { enumeration }; else definition = enumeration; + ((CASTName)enumeration).setBinding( this ); } public void addDeclaration( IASTName decl ){ if( decl.getPropertyInParent() != IASTElaboratedTypeSpecifier.TYPE_NAME ) return; + ((CASTName) decl).setBinding( this ); if( declarations == null ){ declarations = new IASTName[] { decl }; return; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumerator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumerator.java index 10be3b84452..ab40e8a8a8b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumerator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CEnumerator.java @@ -16,6 +16,7 @@ package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumerator; @@ -38,36 +39,38 @@ public class CEnumerator implements IEnumerator { } } - private final IASTEnumerator enumerator; + private final IASTName enumeratorName; public CEnumerator( IASTEnumerator enumtor ){ - this.enumerator= enumtor; + this.enumeratorName = enumtor.getName(); + ((CASTName)enumeratorName).setBinding( this ); } public IASTNode getPhysicalNode(){ - return enumerator; + return enumeratorName; } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() */ public String getName() { - return enumerator.getName().toString(); + return enumeratorName.toString(); } public char[] getNameCharArray(){ - return ((CASTName) enumerator.getName()).toCharArray(); + return enumeratorName.toCharArray(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() */ public IScope getScope() { - return CVisitor.getContainingScope( enumerator ); + return CVisitor.getContainingScope( enumeratorName.getParent() ); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IEnumerator#getType() */ public IType getType() { - IASTEnumerationSpecifier enumSpec = (IASTEnumerationSpecifier) enumerator.getParent(); + IASTEnumerator etor = (IASTEnumerator) enumeratorName.getParent(); + IASTEnumerationSpecifier enumSpec = (IASTEnumerationSpecifier) etor.getParent(); IEnumeration enumeration = (IEnumeration) enumSpec.getName().resolveBinding(); return enumeration; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java index ca89b4d4606..c28fc37c260 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunctionScope.java @@ -21,72 +21,38 @@ import org.eclipse.cdt.core.dom.ast.ILabel; import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope; -import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.parser.util.ArrayUtil; -import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; /** * Created on Nov 8, 2004 * @author aniefer */ -public class CFunctionScope implements ICFunctionScope { - private final IASTFunctionDefinition function; - private CharArrayObjectMap bindings = CharArrayObjectMap.EMPTY_MAP; - +public class CFunctionScope extends CScope implements ICFunctionScope { public CFunctionScope( IASTFunctionDefinition function ){ - this.function = function; + super( function ); } - public void addBinding( IBinding binding ) { - //only labels have function scope - if( !(binding instanceof ILabel) ) - return; - if( bindings == CharArrayObjectMap.EMPTY_MAP ) - bindings = new CharArrayObjectMap(1); - bindings.put( binding.getNameCharArray(), binding ); - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(int, char[]) - */ - public IBinding getBinding( int namespaceType, char[] name ) { - if( namespaceType == ICScope.NAMESPACE_TYPE_OTHER ) - return getBinding( name ); - return null; - } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.c.ICFunctionScope#getBinding(char[]) */ public IBinding getBinding( char[] name ) { - return (IBinding) bindings.get( name ); + return super.getBinding( NAMESPACE_TYPE_OTHER, name ); } public IScope getBodyScope(){ - IASTStatement statement = function.getBody(); + IASTNode node = getPhysicalNode(); + IASTStatement statement = ((IASTFunctionDefinition)node).getBody(); if( statement instanceof IASTCompoundStatement ){ return ((IASTCompoundStatement)statement).getScope(); } return null; } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IScope#getParent() - */ - public IScope getParent() { - return function.getScope(); - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) - */ - public IBinding[] find(String name) { - return null; - } public ILabel[] getLabels(){ FindLabelsAction action = new FindLabelsAction(); - function.accept( action ); + getPhysicalNode().accept( action ); ILabel [] result = null; if( action.labels != null ){ @@ -114,20 +80,4 @@ public class CFunctionScope implements ICFunctionScope { return PROCESS_CONTINUE; } } - - /* (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); - } - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode() - */ - public IASTNode getPhysicalNode() { - return function; - } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CLabel.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CLabel.java index 3dab3c008e8..1ca780eb13f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CLabel.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CLabel.java @@ -16,6 +16,7 @@ package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ast.DOMException; 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.ILabel; import org.eclipse.cdt.core.dom.ast.IScope; @@ -36,10 +37,11 @@ public class CLabel implements ILabel { } } - private final IASTLabelStatement labelStatement; + private final IASTName labelStatement; - public CLabel( IASTLabelStatement statement ){ + public CLabel( IASTName statement ){ labelStatement = statement; + ((CASTName)statement).setBinding( this ); } public IASTNode getPhysicalNode(){ return labelStatement; @@ -48,24 +50,24 @@ public class CLabel implements ILabel { * @see org.eclipse.cdt.core.dom.ast.ILabel#getLabelStatement() */ public IASTLabelStatement getLabelStatement() { - return labelStatement; + return (IASTLabelStatement) labelStatement.getParent(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() */ public String getName() { - return labelStatement.getName().toString(); + return labelStatement.toString(); } public char[] getNameCharArray(){ - return ((CASTName) labelStatement.getName()).toCharArray(); + return labelStatement.toCharArray(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() */ public IScope getScope() { - return CVisitor.getContainingScope( labelStatement ); + return CVisitor.getContainingScope( labelStatement.getParent() ); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java index 4a3b6fa26f5..21c12d5a84a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java @@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTStatement; @@ -37,7 +38,18 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils; * @author aniefer */ public class CScope implements ICScope { + /** + * ISO C:99 6.2.3 there are seperate namespaces for various categories of + * identifiers: - label names ( labels have ICFunctionScope ) - tags of + * structures or unions : NAMESPACE_TYPE_TAG - members of structures or + * unions ( members have ICCompositeTypeScope ) - all other identifiers : + * NAMESPACE_TYPE_OTHER + */ + public static final int NAMESPACE_TYPE_TAG = 0; + public static final int NAMESPACE_TYPE_OTHER = 1; + private IASTNode physicalNode = null; + private boolean isFullyCached = false; private CharArrayObjectMap [] bindings = { CharArrayObjectMap.EMPTY_MAP, CharArrayObjectMap.EMPTY_MAP }; @@ -103,17 +115,18 @@ public class CScope implements ICScope { return (IBinding[]) ArrayUtil.trim( IBinding.class, result ); } - public void addBinding( IBinding binding ) { - int type = ( binding instanceof ICompositeType || binding instanceof IEnumeration ) ? - NAMESPACE_TYPE_TAG : NAMESPACE_TYPE_OTHER; - - if( bindings[type] == CharArrayObjectMap.EMPTY_MAP ) - bindings[type] = new CharArrayObjectMap(1); - bindings[type].put( binding.getNameCharArray(), binding ); - } +// public void addBinding( IBinding binding ) { +// int type = ( binding instanceof ICompositeType || binding instanceof IEnumeration ) ? +// NAMESPACE_TYPE_TAG : NAMESPACE_TYPE_OTHER; +// +// if( bindings[type] == CharArrayObjectMap.EMPTY_MAP ) +// bindings[type] = new CharArrayObjectMap(1); +// bindings[type].put( binding.getNameCharArray(), binding ); +// } public IBinding getBinding( int namespaceType, char [] name ){ - return (IBinding) bindings[namespaceType].get( name ); + IASTName n = (IASTName) bindings[namespaceType].get( name ); + return ( n != null ) ? n.resolveBinding() : null; } /* (non-Javadoc) @@ -126,6 +139,7 @@ public class CScope implements ICScope { if( bindings[type] != CharArrayObjectMap.EMPTY_MAP ) { bindings[type].remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length); } + isFullyCached = false; } /* (non-Javadoc) @@ -134,4 +148,63 @@ public class CScope implements ICScope { public IASTNode getPhysicalNode() { return physicalNode; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.c.ICScope#addName(org.eclipse.cdt.core.dom.ast.IASTName) + */ + public void addName( IASTName name ) { + int type = getNamespaceType( name ); + if( bindings[type] == CharArrayObjectMap.EMPTY_MAP ) + bindings[type] = new CharArrayObjectMap(1); + + char [] n = name.toCharArray(); + IASTName current = (IASTName) bindings[type].get( n ); + if( current == null || ((CASTName)current).getOffset() > ((CASTName) name).getOffset() ){ + bindings[type].put( n, name ); + } + } + + private int getNamespaceType( IASTName name ){ + ASTNodeProperty prop = name.getPropertyInParent(); + if( prop == IASTCompositeTypeSpecifier.TYPE_NAME || + prop == IASTElaboratedTypeSpecifier.TYPE_NAME || + prop == IASTEnumerationSpecifier.ENUMERATION_NAME ) + { + return NAMESPACE_TYPE_TAG; + } + + return NAMESPACE_TYPE_OTHER; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(org.eclipse.cdt.core.dom.ast.IASTName, boolean) + */ + public IBinding getBinding( IASTName name, boolean resolve ) { + int type = getNamespaceType( name ); + Object o = bindings[type].get( name.toCharArray() ); + + if( o == null ) + return null; + + if( o instanceof IBinding ) + return (IBinding) o; + + if( (resolve || ((CASTName)o).hasBinding()) && ( o != name ) ) + return ((IASTName)o).resolveBinding(); + + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.c.ICScope#setFullyCached(boolean) + */ + public void setFullyCached( boolean b ){ + isFullyCached = b; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.c.ICScope#isFullyCached() + */ + public boolean isFullyCached(){ + return isFullyCached; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java index a01b5298b65..e29c69fb860 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java @@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IBinding; @@ -34,15 +35,16 @@ import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope; * @author aniefer */ public class CStructure implements ICompositeType, ICInternalBinding { - private IASTElaboratedTypeSpecifier[] declarations = null; - private ICASTCompositeTypeSpecifier definition; + private IASTName [] declarations = null; + private IASTName definition; - public CStructure( IASTDeclSpecifier declSpec ){ - if( declSpec instanceof IASTCompositeTypeSpecifier ) - definition = (ICASTCompositeTypeSpecifier) declSpec; + public CStructure( IASTName name ){ + if( name.getPropertyInParent() == IASTCompositeTypeSpecifier.TYPE_NAME ) + definition = name; else { - declarations = new IASTElaboratedTypeSpecifier[] { (IASTElaboratedTypeSpecifier) declSpec }; + declarations = new IASTName[] { name }; } + ((CASTName) name).setBinding( this ); } public IASTNode getPhysicalNode(){ @@ -63,22 +65,22 @@ public class CStructure implements ICompositeType, ICInternalBinding { */ public String getName() { if( definition != null ) - return definition.getName().toString(); + return definition.toString(); - return declarations[0].getName().toString(); + return declarations[0].toString(); } public char[] getNameCharArray() { if( definition != null ) - return ((CASTName)definition.getName()).toCharArray(); + return definition.toCharArray(); - return ((CASTName)declarations[0].getName()).toCharArray(); + return declarations[0].toCharArray(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() */ public IScope getScope() throws DOMException { - IASTDeclSpecifier declSpec = (IASTDeclSpecifier) ( ( definition != null ) ? (IASTNode)definition : declarations[0] ); + IASTDeclSpecifier declSpec = (IASTDeclSpecifier) ( ( definition != null ) ? (IASTNode)definition.getParent() : declarations[0].getParent() ); IScope scope = CVisitor.getContainingScope( declSpec ); while( scope instanceof ICCompositeTypeScope ){ scope = scope.getParent(); @@ -89,32 +91,38 @@ public class CStructure implements ICompositeType, ICInternalBinding { /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getFields() */ - public IField[] getFields() { + public IField[] getFields() throws DOMException { if( definition == null ){ - ICASTCompositeTypeSpecifier temp = checkForDefinition( declarations[0] ); + ICASTCompositeTypeSpecifier temp = checkForDefinition( (IASTElaboratedTypeSpecifier) declarations[0].getParent() ); if( temp == null ) return new IField [] { new CField.CFieldProblem( IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray() ) }; - definition = temp; + definition = temp.getName(); } - - IASTDeclaration[] members = definition.getMembers(); + ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent(); + IASTDeclaration[] members = compSpec.getMembers(); int size = members.length; IField[] fields = new IField[ size ]; if( size > 0 ){ - + ICCompositeTypeScope scope = (ICCompositeTypeScope) getCompositeScope(); + if( scope.isFullyCached() ) + scope = null; for( int i = 0; i < size; i++ ){ IASTNode node = members[i]; if( node instanceof IASTSimpleDeclaration ){ IASTDeclarator[] declarators = ((IASTSimpleDeclaration)node).getDeclarators(); for( int j = 0; j < declarators.length; j++ ){ IASTDeclarator declarator = declarators[j]; - IBinding binding = declarator.getName().resolveBinding(); + IASTName name = declarator.getName(); + IBinding binding = name.resolveBinding(); + if( scope != null ) + scope.addName( name ); if( binding != null ) fields[i] = (IField) binding; } } } - + if( scope != null ) + scope.setFullyCached( true ); } return fields; } @@ -122,32 +130,48 @@ public class CStructure implements ICompositeType, ICInternalBinding { /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.ICompositeType#findField(org.eclipse.cdt.core.dom.ast.IASTName) */ - public IField findField(String name) { + public IField findField(String name) throws DOMException { if( definition == null ){ - ICASTCompositeTypeSpecifier temp = checkForDefinition( declarations[0] ); + ICASTCompositeTypeSpecifier temp = checkForDefinition( (IASTElaboratedTypeSpecifier) declarations[0].getParent() ); if( temp == null ) return new CField.CFieldProblem( IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray() ); - definition = temp; + definition = temp.getName(); + } + + ICCompositeTypeScope scope = (ICCompositeTypeScope) getCompositeScope(); + if( scope != null && scope.isFullyCached() ){ + IBinding binding = scope.getBinding( name.toCharArray() ); + if( binding instanceof IField ) + return (IField) binding; + } else { + ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent(); + IASTDeclaration[] members = compSpec.getMembers(); + int size = members.length; + if( size > 0 ){ + IField found = null; + for( int i = 0; i < size; i++ ){ + IASTNode node = members[i]; + if( node instanceof IASTSimpleDeclaration ){ + IASTDeclarator[] declarators = ((IASTSimpleDeclaration)node).getDeclarators(); + for( int j = 0; j < declarators.length; j++ ){ + IASTDeclarator declarator = declarators[j]; + IASTName dtorName = declarator.getName(); + if( scope != null ) + scope.addName( dtorName ); + if( name.equals( dtorName.toString() ) ){ + IBinding binding = dtorName.resolveBinding(); + if( binding instanceof IField ) + found = (IField) binding; + } + } + } + } + if( scope != null ) + scope.setFullyCached( true ); + if( found != null ) + return found; + } } - - IASTDeclaration[] members = definition.getMembers(); - int size = members.length; - if( size > 0 ){ - for( int i = 0; i < size; i++ ){ - IASTNode node = members[i]; - if( node instanceof IASTSimpleDeclaration ){ - IASTDeclarator[] declarators = ((IASTSimpleDeclaration)node).getDeclarators(); - for( int j = 0; j < declarators.length; j++ ){ - IASTDeclarator declarator = declarators[j]; - if( name.equals( declarator.getName().toString() ) ){ - IBinding binding = declarator.getName().resolveBinding(); - if( binding instanceof IField ) - return (IField) binding; - } - } - } - } - } return null; } @@ -155,14 +179,15 @@ public class CStructure implements ICompositeType, ICInternalBinding { * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getKey() */ public int getKey() { - return ( definition != null ) ? definition.getKey() : declarations[0].getKind(); + return ( definition != null ) ? ((IASTCompositeTypeSpecifier)definition.getParent()).getKey() + : ((IASTElaboratedTypeSpecifier)declarations[0].getParent()).getKind(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getCompositeScope() */ public IScope getCompositeScope() { - return (definition != null ) ? definition.getScope() : null; + return (definition != null ) ? ((IASTCompositeTypeSpecifier)definition.getParent()).getScope() : null; } public Object clone(){ @@ -179,7 +204,7 @@ public class CStructure implements ICompositeType, ICInternalBinding { * @param compositeTypeSpec */ public void addDefinition(ICASTCompositeTypeSpecifier compositeTypeSpec) { - definition = compositeTypeSpec; + definition = compositeTypeSpec.getName(); ((CASTName)compositeTypeSpec.getName()).setBinding( this ); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 2a9075aabe7..743f3d3974d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -462,7 +462,7 @@ public class CVisitor { ICScope scope = (ICScope) getContainingScope( enumeration ); IBinding binding; try { - binding = scope.getBinding( ICScope.NAMESPACE_TYPE_TAG, name.toCharArray() ); + binding = scope.getBinding( name, false ); } catch ( DOMException e ) { binding = null; } @@ -474,7 +474,7 @@ public class CVisitor { } else { binding = new CEnumeration( name ); try { - scope.addBinding( binding ); + scope.addName( name ); } catch ( DOMException e1 ) { } } @@ -483,7 +483,7 @@ public class CVisitor { private static IBinding createBinding( IASTEnumerator enumerator ){ IEnumerator binding = new CEnumerator( enumerator ); try { - ((ICScope)binding.getScope()).addBinding( binding ); + ((ICScope)binding.getScope()).addName( enumerator.getName() ); } catch ( DOMException e ) { } return binding; @@ -505,9 +505,10 @@ public class CVisitor { return new CLabel.CLabelProblem( IProblemBinding.SEMANTIC_LABEL_STATEMENT_NOT_FOUND, gotoName ); } } else if( statement instanceof IASTLabelStatement ){ - IBinding binding = new CLabel( (IASTLabelStatement) statement ); + IASTName name = ((IASTLabelStatement)statement).getName(); + IBinding binding = new CLabel( name ); try { - ((ICFunctionScope) binding.getScope()).addBinding( binding ); + ((ICFunctionScope) binding.getScope()).addName( name ); } catch ( DOMException e ) { } return binding; @@ -521,21 +522,21 @@ public class CVisitor { if( parent instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)parent).getDeclarators().length == 0 ){ bits |= CURRENT_SCOPE; } - + IASTName name = elabTypeSpec.getName(); IBinding binding = resolveBinding( elabTypeSpec, bits ); if( binding != null ){ if( binding instanceof CEnumeration ){ - ((CEnumeration)binding).addDeclaration( elabTypeSpec.getName() ); + ((CEnumeration)binding).addDeclaration( name ); } } else { if( elabTypeSpec.getKind() == IASTElaboratedTypeSpecifier.k_enum ){ - binding = new CEnumeration( elabTypeSpec.getName() ); + binding = new CEnumeration( name ); } else { - binding = new CStructure( elabTypeSpec ); + binding = new CStructure( name ); } try { - ((ICScope) binding.getScope()).addBinding( binding ); + ((ICScope) binding.getScope()).addName( name ); } catch ( DOMException e ) { } } @@ -543,7 +544,11 @@ public class CVisitor { return binding; } else if( parent instanceof IASTTypeId || parent instanceof IASTParameterDeclaration ){ IASTNode blockItem = getContainingBlockItem( parent ); - return findBinding( blockItem, elabTypeSpec.getName(), COMPLETE | TAGS ); + try { + return findBinding( blockItem, elabTypeSpec.getName(), COMPLETE | TAGS ); + } catch ( DOMException e ) { + return null; + } } return null; } @@ -647,7 +652,7 @@ public class CVisitor { ICScope scope = (ICScope) ((IASTCompoundStatement)((IASTFunctionDefinition)declarator.getParent()).getBody()).getScope(); if ( scope != null && binding != null ) try { - scope.addBinding(binding); + scope.addName(name); } catch ( DOMException e ) { } } @@ -665,7 +670,7 @@ public class CVisitor { ICScope scope = (ICScope) ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope(); if ( scope != null && binding != null ) try { - scope.addBinding(binding); + scope.addName(name); } catch ( DOMException e ) { } } @@ -699,7 +704,7 @@ public class CVisitor { IBinding binding = null; try { - binding = ( scope != null ) ? scope.getBinding( ICScope.NAMESPACE_TYPE_OTHER, declarator.getName().toCharArray() ) : null; + binding = ( scope != null ) ? scope.getBinding( declarator.getName(), false ) : null; } catch ( DOMException e1 ) { binding = null; } @@ -755,7 +760,7 @@ public class CVisitor { if( scope != null && binding != null ) try { - scope.addBinding( binding ); + scope.addName( declarator.getName() ); } catch ( DOMException e ) { } return binding; @@ -765,12 +770,13 @@ public class CVisitor { private static IBinding createBinding( ICASTCompositeTypeSpecifier compositeTypeSpec ){ ICScope scope = null; IBinding binding = null; + IASTName name = compositeTypeSpec.getName(); try { scope = (ICScope) getContainingScope( compositeTypeSpec ); while( scope instanceof ICCompositeTypeScope ) scope = (ICScope) scope.getParent(); - binding = scope.getBinding( ICScope.NAMESPACE_TYPE_TAG, compositeTypeSpec.getName().toCharArray() ); + binding = scope.getBinding( name, false ); if( binding != null ){ ((CStructure)binding).addDefinition( compositeTypeSpec ); return binding; @@ -778,11 +784,11 @@ public class CVisitor { } catch (DOMException e2) { } - binding = new CStructure( compositeTypeSpec ); + binding = new CStructure( name ); try { scope = (ICScope) binding.getScope(); - scope.addBinding( binding ); + scope.addName( name ); } catch ( DOMException e ) { } @@ -799,18 +805,18 @@ public class CVisitor { if( simpleDeclaration.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef ){ binding = new CTypeDef( name ); try { - ((ICScope) binding.getScope()).addBinding( binding ); + ((ICScope) binding.getScope()).addName( name ); } catch ( DOMException e ) { } } else if( simpleDeclaration.getParent() instanceof ICASTCompositeTypeSpecifier ){ binding = new CField( name ); try { - ((ICScope) binding.getScope()).addBinding( binding ); + ((ICScope) binding.getScope()).addName( name ); } catch ( DOMException e ) { } } else { CScope scope = (CScope) CVisitor.getContainingScope( simpleDeclaration ); - binding = scope.getBinding( ICScope.NAMESPACE_TYPE_OTHER, name.toCharArray() ); + binding = scope.getBinding( name, false ); if( binding == null ){ // if the simpleDeclaration is part of a KRC function declarator, then the binding is to a KRC parameter if ( simpleDeclaration.getParent() instanceof IASTFunctionDeclarator ) { @@ -823,7 +829,7 @@ public class CVisitor { binding = new CVariable( name ); } - scope.addBinding( binding ); + scope.addName( name ); } } @@ -839,19 +845,39 @@ public class CVisitor { IASTFunctionDeclarator functionDeclartor = functionDef.getDeclarator(); IASTName name = functionDeclartor.getName(); IASTNode blockItem = getContainingBlockItem( node ); - return findBinding( blockItem, name, bits ); + try { + return findBinding( blockItem, name, bits ); + } catch ( DOMException e ) { + return null; + } } else if( node instanceof IASTIdExpression ){ IASTNode blockItem = getContainingBlockItem( node ); - return findBinding( blockItem, ((IASTIdExpression)node).getName(), bits ); + try { + return findBinding( blockItem, ((IASTIdExpression)node).getName(), bits ); + } catch ( DOMException e ) { + return null; + } } else if( node instanceof ICASTTypedefNameSpecifier ){ IASTNode blockItem = getContainingBlockItem( node ); - return findBinding( blockItem, ((ICASTTypedefNameSpecifier)node).getName(), bits ); + try { + return findBinding( blockItem, ((ICASTTypedefNameSpecifier)node).getName(), bits ); + } catch ( DOMException e ) { + return null; + } } else if( node instanceof ICASTElaboratedTypeSpecifier ){ IASTNode blockItem = getContainingBlockItem( node ); - return findBinding( blockItem, ((ICASTElaboratedTypeSpecifier)node).getName(), bits ); + try { + return findBinding( blockItem, ((ICASTElaboratedTypeSpecifier)node).getName(), bits ); + } catch ( DOMException e ) { + return null; + } } else if( node instanceof ICASTCompositeTypeSpecifier ){ IASTNode blockItem = getContainingBlockItem( node ); - return findBinding( blockItem, ((ICASTCompositeTypeSpecifier)node).getName(), bits ); + try { + return findBinding( blockItem, ((ICASTCompositeTypeSpecifier)node).getName(), bits ); + } catch ( DOMException e ) { + return null; + } } else if( node instanceof IASTTypeId ){ IASTTypeId typeId = (IASTTypeId) node; IASTDeclSpecifier declSpec = typeId.getDeclSpecifier(); @@ -887,7 +913,11 @@ public class CVisitor { struct = ((IASTCompositeTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding(); if ( struct instanceof CStructure ) { - return ((CStructure)struct).findField(((ICASTFieldDesignator)node).getName().toString()); + try { + return ((CStructure)struct).findField(((ICASTFieldDesignator)node).getName().toString()); + } catch ( DOMException e ) { + return e.getProblem(); + } } else if ( struct instanceof ITypeContainer ) { IType type; try { @@ -901,7 +931,11 @@ public class CVisitor { if ( type instanceof CStructure ) - return ((CStructure)type).findField(((ICASTFieldDesignator)node).getName().toString()); + try { + return ((CStructure)type).findField(((ICASTFieldDesignator)node).getName().toString()); + } catch ( DOMException e1 ) { + return e1.getProblem(); + } } } } @@ -1002,7 +1036,7 @@ public class CVisitor { return getContainingBlockItem( parent ); } - protected static IBinding findBinding( IASTNode blockItem, IASTName name, int bits ){ + protected static IBinding findBinding( IASTNode blockItem, IASTName name, int bits ) throws DOMException{ IBinding binding = null; while( blockItem != null ){ @@ -1011,8 +1045,18 @@ public class CVisitor { ICScope scope = null; if( parent instanceof IASTCompoundStatement ){ IASTCompoundStatement compound = (IASTCompoundStatement) parent; - nodes = compound.getStatements(); scope = (ICScope) compound.getScope(); + + if( parent.getParent() instanceof IASTFunctionDefinition ){ + IASTFunctionDeclarator dtor = ((IASTFunctionDefinition)parent.getParent()).getDeclarator(); + if( dtor instanceof IASTStandardFunctionDeclarator ) + nodes = ((IASTStandardFunctionDeclarator)dtor).getParameters(); + else if( dtor instanceof ICASTKnRFunctionDeclarator ) + nodes = ((ICASTKnRFunctionDeclarator)dtor).getParameterDeclarations(); + } + if( nodes == null || nodes.length == 0 ){ + nodes = compound.getStatements(); + } } else if ( parent instanceof IASTTranslationUnit ){ IASTTranslationUnit translation = (IASTTranslationUnit) parent; nodes = translation.getDeclarations(); @@ -1029,47 +1073,64 @@ public class CVisitor { boolean typesOnly = (bits & TAGS) != 0; boolean includeBlockItem = (bits & INCLUDE_BLOCK_ITEM) != 0; - if( scope != null ){ - int namespaceType = typesOnly ? ICScope.NAMESPACE_TYPE_TAG : ICScope.NAMESPACE_TYPE_OTHER; + if( scope != null && scope.isFullyCached() ){ try { - binding = scope.getBinding( namespaceType, name.toCharArray() ); + binding = scope.getBinding( name, true ); } catch ( DOMException e ) { binding = null; } if( binding != null ) return binding; - } + } else { - if( nodes != null ){ - for( int i = 0; i < nodes.length; i++ ){ - IASTNode node = nodes[i]; - if( node == null || ( !includeBlockItem && node == blockItem ) ) - break; - if( node instanceof IASTDeclarationStatement ){ - IASTDeclarationStatement declStatement = (IASTDeclarationStatement) node; - binding = checkForBinding( declStatement.getDeclaration(), name, typesOnly ); - } else if( node instanceof IASTDeclaration ){ - binding = checkForBinding( (IASTDeclaration) node, name, typesOnly ); - } else if( node instanceof IASTParameterDeclaration ){ - binding = checkForBinding( (IASTParameterDeclaration) node, name, typesOnly ); - } - if( binding != null ){ - return binding; + IASTName result = null; + if( nodes != null ){ + int idx = -1; + IASTNode node = ( nodes != null ? (nodes.length > 0 ? nodes[++idx] : null ) : parent ); + while( node != null ) { + IASTName candidate = null; + try { + candidate = checkForBinding( scope, node, name, typesOnly ); + } catch ( DOMException e ) { + continue; + } + + if( result == null && ( includeBlockItem || (node != blockItem) ) ) + result = candidate; + + if( idx > -1 && ++idx < nodes.length ){ + node = nodes[idx]; + } else { + node = null; + if( parent instanceof IASTCompoundStatement && + ( nodes[0].getPropertyInParent() == ICASTKnRFunctionDeclarator.FUNCTION_PARAMETER || + nodes[0].getPropertyInParent() == IASTStandardFunctionDeclarator.FUNCTION_PARAMETER ) ) + { + //function body, we were looking at parameters, now check the body itself + IASTCompoundStatement compound = (IASTCompoundStatement) parent; + nodes = compound.getStatements(); + if( nodes.length > 0 ){ + idx = 0; + node = nodes[0]; + } + } + } } - if( includeBlockItem && node == blockItem ) - break; + } else { + try { + result = checkForBinding( scope, parent, name, typesOnly ); + } catch ( DOMException e ) { + } } - } else { - //check the parent - if( parent instanceof IASTDeclaration ){ - binding = checkForBinding( (IASTDeclaration) parent, name, typesOnly ); - } else if( parent instanceof IASTStatement ){ - binding = checkForBinding( (IASTStatement) parent, name, typesOnly ); - } - if( binding != null ){ - return binding; + if( scope != null ) { + try { + scope.setFullyCached( true ); + } catch ( DOMException e ) { + } } + if( result != null ) + return result.resolveBinding(); } if( (bits & CURRENT_SCOPE) == 0 ) blockItem = parent; @@ -1097,148 +1158,135 @@ public class CVisitor { //external variable external = new CExternalVariable( tu, name ); } - ((CScope)tu.getScope()).addBinding( external ); + ((CScope)tu.getScope()).addName( name ); } return external; } - private static IBinding checkForBinding( IASTDeclSpecifier declSpec, IASTName name, boolean typesOnly ){ + private static IASTName checkForBinding( ICScope scope, IASTDeclSpecifier declSpec, IASTName name, boolean typesOnly ) throws DOMException{ IASTName tempName = null; - if( typesOnly ){ - if( declSpec instanceof ICASTElaboratedTypeSpecifier ){ - tempName = ((ICASTElaboratedTypeSpecifier)declSpec).getName(); - if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ - return tempName.resolveBinding(); - } - } else if( declSpec instanceof ICASTCompositeTypeSpecifier ){ - tempName = ((ICASTCompositeTypeSpecifier)declSpec).getName(); - if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ - return tempName.resolveBinding(); - } - //also have to check for any nested structs - IASTDeclaration [] nested = ((ICASTCompositeTypeSpecifier)declSpec).getMembers(); - for( int i = 0; i < nested.length; i++ ){ - if( nested[i] instanceof IASTSimpleDeclaration ){ - IASTDeclSpecifier d = ((IASTSimpleDeclaration)nested[i]).getDeclSpecifier(); - if( d instanceof ICASTCompositeTypeSpecifier ) { - IBinding temp = checkForBinding( d, name, typesOnly ); - if( temp != null ) - return temp; - } + + if( declSpec instanceof ICASTElaboratedTypeSpecifier ){ + tempName = ((ICASTElaboratedTypeSpecifier)declSpec).getName(); + if( scope != null ) + scope.addName( tempName ); + if( typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; + } + } else if( declSpec instanceof ICASTCompositeTypeSpecifier ){ + tempName = ((ICASTCompositeTypeSpecifier)declSpec).getName(); + if( scope != null ) + scope.addName( tempName ); + if( typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; + } + //also have to check for any nested structs + IASTDeclaration [] nested = ((ICASTCompositeTypeSpecifier)declSpec).getMembers(); + for( int i = 0; i < nested.length; i++ ){ + if( nested[i] instanceof IASTSimpleDeclaration ){ + IASTDeclSpecifier d = ((IASTSimpleDeclaration)nested[i]).getDeclSpecifier(); + if( d instanceof ICASTCompositeTypeSpecifier ) { + IASTName temp = checkForBinding( scope, d, name, typesOnly ); + if( temp != null ) + return temp; } } - } else if( declSpec instanceof ICASTEnumerationSpecifier ){ - ICASTEnumerationSpecifier enumeration = (ICASTEnumerationSpecifier) declSpec; - tempName = enumeration.getName(); - if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ - return tempName.resolveBinding(); + } + } else if( declSpec instanceof ICASTEnumerationSpecifier ){ + ICASTEnumerationSpecifier enumeration = (ICASTEnumerationSpecifier) declSpec; + tempName = enumeration.getName(); + if( scope != null ) + scope.addName( tempName ); + if( typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; + } + //check enumerators + IASTEnumerator [] list = ((ICASTEnumerationSpecifier) declSpec).getEnumerators(); + for( int i = 0; i < list.length; i++ ) { + IASTEnumerator enumerator = list[i]; + if( enumerator == null ) break; + tempName = enumerator.getName(); + if( scope != null ) + scope.addName( tempName ); + if( !typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; } - } - } else { - if( declSpec instanceof ICASTEnumerationSpecifier ) { - //check enumerators - IASTEnumerator [] list = ((ICASTEnumerationSpecifier) declSpec).getEnumerators(); - for( int i = 0; i < list.length; i++ ) { - IASTEnumerator enumerator = list[i]; - if( enumerator == null ) break; - tempName = enumerator.getName(); - if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ - return tempName.resolveBinding(); - } - } - } + } } return null; } - private static IBinding checkForBinding( IASTParameterDeclaration paramDecl, IASTName name, boolean typesOnly ){ + + private static IASTName checkForBinding( ICScope scope, IASTParameterDeclaration paramDecl, IASTName name, boolean typesOnly ) throws DOMException{ if( paramDecl == null ) return null; - if( typesOnly ){ - return checkForBinding( paramDecl.getDeclSpecifier(), name, typesOnly ); - } - - IASTDeclarator dtor = paramDecl.getDeclarator(); + IASTDeclarator dtor = paramDecl.getDeclarator(); while( dtor.getNestedDeclarator() != null ){ dtor = dtor.getNestedDeclarator(); } - IASTName declName = dtor.getName(); - if( CharArrayUtils.equals( declName.toCharArray(), name.toCharArray() ) ){ - return declName.resolveBinding(); + IASTName tempName = dtor.getName(); + if( scope != null ) + scope.addName( tempName ); + if( !typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; } + + if( typesOnly ){ + return checkForBinding( scope, paramDecl.getDeclSpecifier(), name, typesOnly ); + } return null; } - private static IBinding checkForBinding( IASTDeclaration declaration, IASTName name, boolean typesOnly ){ + private static IASTName checkForBinding( ICScope scope, IASTNode node, IASTName name, boolean typesOnly ) throws DOMException{ + if( node instanceof IASTDeclaration ){ + return checkForBinding( scope, (IASTDeclaration) node, name, typesOnly ); + } else if( node instanceof IASTParameterDeclaration ){ + return checkForBinding( scope, (IASTParameterDeclaration) node, name, typesOnly ); + } else if( node instanceof IASTDeclarationStatement ){ + return checkForBinding( scope, ((IASTDeclarationStatement)node).getDeclaration(), name, typesOnly ); + } else if( node instanceof IASTForStatement ){ + IASTForStatement forStatement = (IASTForStatement) node; + if( forStatement.getInitDeclaration() != null ){ + return checkForBinding( scope, forStatement.getInitDeclaration(), name, typesOnly ); + } + } + return null; + } + private static IASTName checkForBinding( ICScope scope, IASTDeclaration declaration, IASTName name, boolean typesOnly ) throws DOMException{ IASTName tempName = null; if( declaration instanceof IASTSimpleDeclaration ){ IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration; - - if( !typesOnly ){ - IASTDeclarator [] declarators = simpleDeclaration.getDeclarators(); - for( int i = 0; i < declarators.length; i++ ){ - IASTDeclarator declarator = declarators[i]; - while( declarator.getNestedDeclarator() != null ){ - declarator = declarator.getNestedDeclarator(); - } - tempName = declarator.getName(); - if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ - return tempName.resolveBinding(); - } + IASTDeclarator [] declarators = simpleDeclaration.getDeclarators(); + for( int i = 0; i < declarators.length; i++ ){ + IASTDeclarator declarator = declarators[i]; + while( declarator.getNestedDeclarator() != null ){ + declarator = declarator.getNestedDeclarator(); + } + tempName = declarator.getName(); + if( scope != null ) + scope.addName( tempName ); + + if( !typesOnly && CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ + return tempName; } } - return checkForBinding( simpleDeclaration.getDeclSpecifier(), name, typesOnly ); + tempName = checkForBinding( scope, simpleDeclaration.getDeclSpecifier(), name, typesOnly ); + if( tempName != null ){ + return tempName; + } } else if( !typesOnly && declaration instanceof IASTFunctionDefinition ){ IASTFunctionDefinition functionDef = (IASTFunctionDefinition) declaration; - - if (functionDef.getDeclarator() instanceof IASTStandardFunctionDeclarator) { - CASTFunctionDeclarator declarator = (CASTFunctionDeclarator) functionDef.getDeclarator(); - - //check the function itself - IASTName declName = declarator.getName(); - if( CharArrayUtils.equals( declName.toCharArray(), name.toCharArray() ) ){ - return declName.resolveBinding(); - } - //check the parameters - IASTParameterDeclaration [] parameters = declarator.getParameters(); - for( int i = 0; i < parameters.length; i++ ){ - IBinding binding = checkForBinding( parameters[i], name, typesOnly ); - if( binding != null ){ - return binding; - } - } - } else if (functionDef.getDeclarator() instanceof ICASTKnRFunctionDeclarator) { - CASTKnRFunctionDeclarator declarator = (CASTKnRFunctionDeclarator) functionDef.getDeclarator(); - - //check the function itself - IASTName declName = declarator.getName(); - if( CharArrayUtils.equals( declName.toCharArray(), name.toCharArray() ) ){ - return declName.resolveBinding(); - } - //check the parameters - IASTDeclaration [] parameters = declarator.getParameterDeclarations(); - for( int i = 0; i < parameters.length; i++ ){ - IASTDeclaration parameterDeclaration = parameters[i]; - if( parameterDeclaration == null || !(parameters[i] instanceof IASTSimpleDeclaration)) break; - IASTDeclarator[] parmDecltors = ((IASTSimpleDeclaration)parameters[i]).getDeclarators(); - for (int j=0; j 0) continue; // only do getters + if (methods[j].getParameterTypes().length > 0 || (!shouldInvokeMethod(methods[j].getName()))) continue; // only do getters, that aren't in the bad list (like clone()) TextPropertyDescriptor text = null; if (obj instanceof IBinding) @@ -362,9 +375,9 @@ public class TreeObject implements IAdaptable { if (text != null) { if (obj instanceof IBinding) - text.setCategory(IBINDING_TAG + ((IASTName)node).resolveBinding().getClass().getName().substring(((IASTName)node).resolveBinding().getClass().getName().lastIndexOf(FILENAME_SEPARATOR) + 1) + COLON_SEPARATOR + ((IASTName)node).resolveBinding().toString()); + text.setCategory(IBINDING_TAG + ((IASTName)node).resolveBinding().getClass().getName().substring(((IASTName)node).resolveBinding().getClass().getName().lastIndexOf(PERIOD) + 1) + COLON_SEPARATOR + getValueString(((IASTName)node).resolveBinding())); else - text.setCategory(objClass.getName().substring(objClass.getName().lastIndexOf(FILENAME_SEPARATOR) + 1) + COLON_SEPARATOR + node.toString()); + text.setCategory(objClass.getName().substring(objClass.getName().lastIndexOf(PERIOD) + 1) + COLON_SEPARATOR + getValueString(node)); desc = (IPropertyDescriptor[])ArrayUtil.append(IPropertyDescriptor.class, desc, text); } } @@ -395,15 +408,127 @@ public class TreeObject implements IAdaptable { result = method.invoke(node, null); } - if (result != null) - value = result.toString(); + if (result == null) { + value = NULL_STRING; + } else if (result.getClass().isArray()) { // if it's an array + if (result.getClass().getComponentType().equals(char.class)) // array of char + value = String.valueOf((char[])result); + else if (result.getClass().isPrimitive()) { + value = trimObjectToString(result.toString()); + } else + value = getValueString((Object[])result); + } else { + value = getValueString(result); + } } catch (Exception e) { e.printStackTrace(); // display all exceptions to developers + + if (e instanceof InvocationTargetException) + return trimObjectToString(((InvocationTargetException)e).getTargetException().toString()) + EXCEPTION_ON + ((InvocationTargetException)e).getTargetException().getStackTrace()[0].toString(); + return e.toString(); } return value; } + + private String trimObjectToString(String str) { + return str.substring(str.lastIndexOf(PERIOD) + 1); + } + + private String getValueString(Object obj) { + StringBuffer buffer = new StringBuffer(); + + if (obj.getClass().isPrimitive()) { + buffer.append(trimObjectToString(obj.toString())); + } else if (obj instanceof ASTNodeProperty) { + buffer.append(((ASTNodeProperty)obj).getName()); + } else if (obj instanceof IASTName) { + buffer.append( trimObjectToString(((IASTName)obj).toString()) ); + buffer.append(COLON_SEPARATOR); + buffer.append( getType(((IASTName)obj).resolveBinding()) ); + } else if (obj instanceof IType) { + buffer.append(getType(obj)); + } else if (obj instanceof IBinding) { + buffer.append(((IBinding)obj).getName()); + buffer.append(COLON_SEPARATOR); + buffer.append(getType(obj)); + } else if (obj instanceof IASTExpression) { + buffer.append(ASTSignatureUtil.getExpressionString((IASTExpression)obj)); + } else if (obj instanceof IASTNode) { + String utilString = ASTSignatureUtil.getNodeSignature((IASTNode)obj); + if (utilString != null && !utilString.equals(BLANK_STRING)) { + buffer.append(trimObjectToString(obj.toString())); + buffer.append(COLON_SEPARATOR); + buffer.append(utilString); + } + else + buffer.append(trimObjectToString(obj.toString())); + } else + buffer.append(trimObjectToString(obj.toString())); + + return buffer.toString(); + } + + private String getType(Object obj) { + if (obj == null) return NULL_STRING; + + if (obj instanceof IType) + return ASTTypeUtil.getType((IType)obj); + + Method[] methods = obj.getClass().getMethods(); + boolean hasGetType = false; + + int i=0; + for(; i