1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 14:12:10 +02:00

Removes the need to flush the cache of c-scopes, bug 259373.

This commit is contained in:
Markus Schorn 2009-01-21 14:57:44 +00:00
parent a1e03d937a
commit a091e59733
40 changed files with 1178 additions and 1174 deletions

View file

@ -101,10 +101,6 @@ public class AST2KnRTests extends AST2BaseTest {
assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").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$ assertNotNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
CVisitor.clearBindings(tu);
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$
} }
public void testSimpleKRCTest2() throws Exception { public void testSimpleKRCTest2() throws Exception {
@ -136,10 +132,6 @@ public class AST2KnRTests extends AST2BaseTest {
assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").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$ assertNotNull( ((CScope)((IASTCompoundStatement)f2.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
CVisitor.clearBindings(tu);
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$
} }
public void testSimpleKRCTest3() throws Exception { public void testSimpleKRCTest3() throws Exception {
@ -209,11 +201,6 @@ public class AST2KnRTests extends AST2BaseTest {
assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").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("x").toCharArray()) ); //$NON-NLS-1$
assertNotNull( ((CScope)((IASTCompoundStatement)isroot_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("y").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( ((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$
} }
public void testKRCWithTypes() throws Exception { public void testKRCWithTypes() throws Exception {
@ -268,10 +255,6 @@ public class AST2KnRTests extends AST2BaseTest {
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("c").toCharArray()) ); //$NON-NLS-1$
assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("isroot").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("x").toCharArray()) ); //$NON-NLS-1$
CVisitor.clearBindings(tu);
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 { public void testKRCProblem1() throws Exception {
@ -519,11 +502,6 @@ public class AST2KnRTests extends AST2BaseTest {
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("A").toCharArray()) ); //$NON-NLS-1$
assertNotNull( ((CScope)tu.getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("f").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$ assertNotNull( ((CScope)((IASTCompoundStatement)f_def.getBody()).getScope()).getBinding(CScope.NAMESPACE_TYPE_OTHER, new String("x").toCharArray()) ); //$NON-NLS-1$
CVisitor.clearBindings(tu);
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$
} }
public void testKRC_monop_cards2() throws Exception { public void testKRC_monop_cards2() throws Exception {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -926,15 +926,13 @@ public class AST2Tests extends AST2BaseTest {
// b; // b;
// } // }
public void testFunctionParameters() throws Exception { public void testFunctionParameters() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C); final String code = getAboveComment();
IASTTranslationUnit tu = parse(code, ParserLanguage.C);
assertTrue(tu.isFrozen()); assertTrue(tu.isFrozen());
for (int i = 0; i < NUM_TESTS; i++) {
// void f( // void f(
IASTSimpleDeclaration f_decl = (IASTSimpleDeclaration) tu IASTSimpleDeclaration f_decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
.getDeclarations()[0]; IASTStandardFunctionDeclarator dtor = (IASTStandardFunctionDeclarator) f_decl.getDeclarators()[0];
IASTStandardFunctionDeclarator dtor = (IASTStandardFunctionDeclarator) f_decl
.getDeclarators()[0];
IASTName f_name1 = dtor.getName(); IASTName f_name1 = dtor.getName();
// int a); // int a);
IASTParameterDeclaration param1 = dtor.getParameters()[0]; IASTParameterDeclaration param1 = dtor.getParameters()[0];
@ -942,8 +940,7 @@ public class AST2Tests extends AST2BaseTest {
IASTName name_param1 = paramDtor.getName(); IASTName name_param1 = paramDtor.getName();
// void f( // void f(
IASTFunctionDefinition f_defn = (IASTFunctionDefinition) tu IASTFunctionDefinition f_defn = (IASTFunctionDefinition) tu.getDeclarations()[1];
.getDeclarations()[1];
assertTrue(f_defn.getDeclarator() instanceof IASTStandardFunctionDeclarator); assertTrue(f_defn.getDeclarator() instanceof IASTStandardFunctionDeclarator);
dtor = (IASTStandardFunctionDeclarator) f_defn.getDeclarator(); dtor = (IASTStandardFunctionDeclarator) f_defn.getDeclarator();
IASTName f_name2 = dtor.getName(); IASTName f_name2 = dtor.getName();
@ -953,12 +950,9 @@ public class AST2Tests extends AST2BaseTest {
IASTName name_param2 = paramDtor.getName(); IASTName name_param2 = paramDtor.getName();
// b; // b;
IASTCompoundStatement compound = (IASTCompoundStatement) f_defn IASTCompoundStatement compound = (IASTCompoundStatement) f_defn.getBody();
.getBody(); IASTExpressionStatement expStatement = (IASTExpressionStatement) compound.getStatements()[0];
IASTExpressionStatement expStatement = (IASTExpressionStatement) compound IASTIdExpression idexp = (IASTIdExpression) expStatement.getExpression();
.getStatements()[0];
IASTIdExpression idexp = (IASTIdExpression) expStatement
.getExpression();
IASTName name_param3 = idexp.getName(); IASTName name_param3 = idexp.getName();
// bindings // bindings
@ -974,7 +968,33 @@ public class AST2Tests extends AST2BaseTest {
assertSame(param_2, param_3); assertSame(param_2, param_3);
assertSame(f_1, f_2); assertSame(f_1, f_2);
CVisitor.clearBindings(tu); tu = parse(code, ParserLanguage.C);
assertTrue(tu.isFrozen());
// void f(
f_decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
dtor = (IASTStandardFunctionDeclarator) f_decl.getDeclarators()[0];
f_name1 = dtor.getName();
// int a);
param1 = dtor.getParameters()[0];
paramDtor = param1.getDeclarator();
name_param1 = paramDtor.getName();
// void f(
f_defn = (IASTFunctionDefinition) tu.getDeclarations()[1];
dtor = (IASTStandardFunctionDeclarator) f_defn.getDeclarator();
f_name2 = dtor.getName();
// int b);
param2 = dtor.getParameters()[0];
paramDtor = param2.getDeclarator();
name_param2 = paramDtor.getName();
// b;
compound = (IASTCompoundStatement) f_defn.getBody();
expStatement = (IASTExpressionStatement) compound.getStatements()[0];
idexp = (IASTIdExpression) expStatement.getExpression();
name_param3 = idexp.getName();
// bindings
param_1 = (IParameter) name_param1.resolveBinding(); param_1 = (IParameter) name_param1.resolveBinding();
param_2 = (IParameter) name_param3.resolveBinding(); param_2 = (IParameter) name_param3.resolveBinding();
param_3 = (IParameter) name_param2.resolveBinding(); param_3 = (IParameter) name_param2.resolveBinding();
@ -1009,7 +1029,6 @@ public class AST2Tests extends AST2BaseTest {
tu = validateCopy(tu); tu = validateCopy(tu);
} }
}
// void f(int a, int b) { } // void f(int a, int b) { }
public void testSimpleFunction() throws Exception { public void testSimpleFunction() throws Exception {
@ -1219,20 +1238,14 @@ public class AST2Tests extends AST2BaseTest {
IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C); IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C);
assertTrue(tu.isFrozen()); assertTrue(tu.isFrozen());
for (int i = 0; i < NUM_TESTS; i++) { for (int i = 0; i < NUM_TESTS; i++) {
IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) tu IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
.getDeclarations()[0]; IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) simpleDecl.getDeclSpecifier();
IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) simpleDecl IASTSimpleDeclaration decl_x = (IASTSimpleDeclaration) compType.getMembers()[0];
.getDeclSpecifier();
IASTSimpleDeclaration decl_x = (IASTSimpleDeclaration) compType
.getMembers()[0];
IASTName name_x1 = decl_x.getDeclarators()[0].getName(); IASTName name_x1 = decl_x.getDeclarators()[0].getName();
IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations()[1];
.getDeclarations()[1];
IASTCompoundStatement body = (IASTCompoundStatement) fdef.getBody(); IASTCompoundStatement body = (IASTCompoundStatement) fdef.getBody();
IASTExpressionStatement expStatement = (IASTExpressionStatement) body IASTExpressionStatement expStatement = (IASTExpressionStatement) body.getStatements()[0];
.getStatements()[0]; IASTFieldReference fieldRef = (IASTFieldReference) expStatement.getExpression();
IASTFieldReference fieldRef = (IASTFieldReference) expStatement
.getExpression();
IASTName name_x2 = fieldRef.getFieldName(); IASTName name_x2 = fieldRef.getFieldName();
IField x1 = (IField) name_x1.resolveBinding(); IField x1 = (IField) name_x1.resolveBinding();
@ -1242,8 +1255,7 @@ public class AST2Tests extends AST2BaseTest {
assertSame(x1, x2); assertSame(x1, x2);
// test tu.getDeclarationsInAST(IBinding) // test tu.getDeclarationsInAST(IBinding)
IASTName[] decls = tu.getDeclarationsInAST(compType.getName() IASTName[] decls = tu.getDeclarationsInAST(compType.getName().resolveBinding());
.resolveBinding());
assertEquals(decls.length, 1); assertEquals(decls.length, 1);
assertEquals(decls[0], compType.getName()); assertEquals(decls[0], compType.getName());
@ -1251,17 +1263,14 @@ public class AST2Tests extends AST2BaseTest {
assertEquals(decls.length, 1); assertEquals(decls.length, 1);
assertEquals(decls[0], name_x1); assertEquals(decls[0], name_x1);
decls = tu.getDeclarationsInAST(fdef.getDeclarator().getName() decls = tu.getDeclarationsInAST(fdef.getDeclarator().getName().resolveBinding());
.resolveBinding());
assertEquals(decls.length, 1); assertEquals(decls.length, 1);
assertEquals(decls[0], fdef.getDeclarator().getName()); assertEquals(decls[0], fdef.getDeclarator().getName());
IASTCastExpression castExpression = (IASTCastExpression) ((IASTUnaryExpression) ((IASTFieldReference) expStatement IASTCastExpression castExpression = (IASTCastExpression) ((IASTUnaryExpression) ((IASTFieldReference) expStatement.getExpression()).getFieldOwner())
.getExpression()).getFieldOwner()).getOperand(); .getOperand();
IASTElaboratedTypeSpecifier elaboratedTypeSpecifier = ((IASTElaboratedTypeSpecifier) castExpression IASTElaboratedTypeSpecifier elaboratedTypeSpecifier = ((IASTElaboratedTypeSpecifier) castExpression.getTypeId().getDeclSpecifier());
.getTypeId().getDeclSpecifier()); decls = tu.getDeclarationsInAST(elaboratedTypeSpecifier.getName().resolveBinding());
decls = tu.getDeclarationsInAST(elaboratedTypeSpecifier.getName()
.resolveBinding());
assertEquals(decls.length, 1); assertEquals(decls.length, 1);
assertEquals(decls[0], compType.getName()); assertEquals(decls[0], compType.getName());
@ -2954,22 +2963,23 @@ public class AST2Tests extends AST2BaseTest {
// struct s { double i; } f(void); // struct s { double i; } f(void);
// struct s f(void) {} // struct s f(void) {}
public void testBug84266() throws Exception { public void testBug84266() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C); final String code = getAboveComment();
IASTTranslationUnit tu = parse(code, ParserLanguage.C);
CNameCollector col = new CNameCollector(); CNameCollector col = new CNameCollector();
tu.accept(col); tu.accept(col);
assertEquals(col.size(), 7); assertEquals(col.size(), 7);
ICompositeType s_ref = (ICompositeType) col.getName(4).resolveBinding(); ICompositeType s_ref = (ICompositeType) col.getName(4).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(0) ICompositeType s_decl = (ICompositeType) col.getName(0).resolveBinding();
.resolveBinding();
assertSame(s_ref, s_decl); assertSame(s_ref, s_decl);
CVisitor.clearBindings(tu);
tu = parse(code, ParserLanguage.C);
col = new CNameCollector();
tu.accept(col);
assertEquals(col.size(), 7);
s_decl = (ICompositeType) col.getName(0).resolveBinding(); s_decl = (ICompositeType) col.getName(0).resolveBinding();
s_ref = (ICompositeType) col.getName(4).resolveBinding(); s_ref = (ICompositeType) col.getName(4).resolveBinding();
assertSame(s_ref, s_decl); assertSame(s_ref, s_decl);
} }
@ -3001,22 +3011,24 @@ public class AST2Tests extends AST2BaseTest {
// struct s1 { struct s2 *s2p; /* ... */ }; // D1 // struct s1 { struct s2 *s2p; /* ... */ }; // D1
// struct s2 { struct s1 *s1p; /* ... */ }; // D2 // struct s2 { struct s1 *s1p; /* ... */ }; // D2
public void testBug84186() throws Exception { public void testBug84186() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C); final String code = getAboveComment();
IASTTranslationUnit tu = parse(code, ParserLanguage.C);
CNameCollector col = new CNameCollector(); CNameCollector col = new CNameCollector();
tu.accept(col); tu.accept(col);
assertEquals(col.size(), 6); assertEquals(col.size(), 6);
ICompositeType s_ref = (ICompositeType) col.getName(1).resolveBinding(); ICompositeType s_ref = (ICompositeType) col.getName(1).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(3) ICompositeType s_decl = (ICompositeType) col.getName(3).resolveBinding();
.resolveBinding();
assertSame(s_ref, s_decl); assertSame(s_ref, s_decl);
CVisitor.clearBindings(tu);
tu = parse(code, ParserLanguage.C);
col = new CNameCollector();
tu.accept(col);
assertEquals(col.size(), 6);
s_decl = (ICompositeType) col.getName(3).resolveBinding(); s_decl = (ICompositeType) col.getName(3).resolveBinding();
s_ref = (ICompositeType) col.getName(1).resolveBinding(); s_ref = (ICompositeType) col.getName(1).resolveBinding();
assertSame(s_ref, s_decl); assertSame(s_ref, s_decl);
} }
@ -3161,10 +3173,11 @@ public class AST2Tests extends AST2BaseTest {
IParameter p = (IParameter) col.getName(1).resolveBinding(); IParameter p = (IParameter) col.getName(1).resolveBinding();
IVariable v1 = (IVariable) col.getName(2).resolveBinding(); IVariable v1 = (IVariable) col.getName(2).resolveBinding();
IScope scope = f.getFunctionScope(); IASTFunctionDefinition fdef= getDeclaration(tu, 0);
IScope scope = ((IASTCompoundStatement) fdef.getBody()).getScope();
IBinding[] bs = scope.find("par"); //$NON-NLS-1$ IBinding[] bs = scope.find("par"); //$NON-NLS-1$
assertEquals(bs.length, 1); assertEquals(1, bs.length);
assertSame(bs[0], p); assertSame(bs[0], p);
bs = scope.find("v1"); //$NON-NLS-1$ bs = scope.find("v1"); //$NON-NLS-1$
@ -3191,7 +3204,8 @@ public class AST2Tests extends AST2BaseTest {
IVariable S3 = (IVariable) col.getName(3).resolveBinding(); IVariable S3 = (IVariable) col.getName(3).resolveBinding();
ILabel S4 = (ILabel) col.getName(4).resolveBinding(); ILabel S4 = (ILabel) col.getName(4).resolveBinding();
IScope scope = f.getFunctionScope(); IASTFunctionDefinition fdef= getDeclaration(tu, 2);
IScope scope = ((IASTCompoundStatement) fdef.getBody()).getScope();
IBinding[] bs = scope.find("S"); //$NON-NLS-1$ IBinding[] bs = scope.find("S"); //$NON-NLS-1$
@ -5162,7 +5176,7 @@ public class AST2Tests extends AST2BaseTest {
// int a,b; // int a,b;
// { b; a; int a; } // { b; a; int a; }
// } // }
public void _testLocalVariableResolution_Bug235831() throws Exception { public void testLocalVariableResolution_Bug235831() throws Exception {
final String comment= getAboveComment(); final String comment= getAboveComment();
final boolean[] isCpps= {false, true}; final boolean[] isCpps= {false, true};
for (boolean isCpp : isCpps) { for (boolean isCpp : isCpps) {
@ -5901,4 +5915,17 @@ public class AST2Tests extends AST2BaseTest {
assertInstance(expr, IASTBinaryExpression.class); assertInstance(expr, IASTBinaryExpression.class);
} }
} }
// typedef int TInt;
// struct X {
// const TInt; // declaration without name
// const a; // declares a;
// };
public void testAmbiguousDeclaration_Bug259373() throws Exception {
final String code= getAboveComment();
IASTTranslationUnit tu= parseAndCheckBindings(code, ParserLanguage.C, true);
BindingAssertionHelper bh= new BindingAssertionHelper(code, false);
ITypedef td= bh.assertNonProblem("TInt; //", 4);
IField f= bh.assertNonProblem("a;", 1);
}
} }

View file

@ -1,19 +1,20 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * Doug Schaefer (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast; package org.eclipse.cdt.core.dom.ast;
/** /**
* This is a declarator for a function. * This is a declarator for a function.
* *
* @author Doug Schaefer * @noextend This interface is not intended to be extended by clients.
* @noimplement This interface is not intended to be implemented by clients. * @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface IASTFunctionDeclarator extends IASTDeclarator { public interface IASTFunctionDeclarator extends IASTDeclarator {

View file

@ -1,19 +1,20 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * Doug Schaefer (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast; package org.eclipse.cdt.core.dom.ast;
/** /**
* This is a declarator for a non K&R C function. * This is a declarator for a non K&R C function.
* *
* @author Doug Schaefer * @noextend This interface is not intended to be extended by clients.
* @noimplement This interface is not intended to be implemented by clients. * @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface IASTStandardFunctionDeclarator extends IASTFunctionDeclarator { public interface IASTStandardFunctionDeclarator extends IASTFunctionDeclarator {
@ -26,6 +27,13 @@ public interface IASTStandardFunctionDeclarator extends IASTFunctionDeclarator {
public final static ASTNodeProperty FUNCTION_PARAMETER = new ASTNodeProperty( public final static ASTNodeProperty FUNCTION_PARAMETER = new ASTNodeProperty(
"IASTStandardFunctionDeclarator.FUNCTION_PARAMETER - IASTParameterDeclaration for IASTStandardFunctionDeclarator"); //$NON-NLS-1$ "IASTStandardFunctionDeclarator.FUNCTION_PARAMETER - IASTParameterDeclaration for IASTStandardFunctionDeclarator"); //$NON-NLS-1$
/**
* Get the scope for this declarator. Returns <code>null</code>, if this declarator does not
* declare a function-prototype or function-definition.
* @since 5.1
*/
public IScope getFunctionScope();
/** /**
* Gets the parameter declarations for the function * Gets the parameter declarations for the function
* *

View file

@ -19,7 +19,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor; import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
@ -56,11 +55,6 @@ public abstract class ASTAmbiguousBinaryVsCastExpression extends ASTAmbiguousNod
return CVisitor.getExpressionType(getExpressions()[0]); return CVisitor.getExpressionType(getExpressions()[0]);
} }
@Override
protected final IScope getAffectedScope() {
return null;
}
@Override @Override
public final IASTNode[] getNodes() { public final IASTNode[] getNodes() {
return getExpressions(); return getExpressions();

View file

@ -21,7 +21,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
@ -46,11 +45,6 @@ public abstract class ASTAmbiguousCastVsFunctionCallExpression extends ASTAmbigu
fFunctionCallExpression= functionCall; fFunctionCallExpression= functionCall;
} }
@Override
protected final IScope getAffectedScope() {
return null;
}
@Override @Override
public final IASTNode[] getNodes() { public final IASTNode[] getNodes() {
return getExpressions(); return getExpressions();

View file

@ -19,7 +19,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
@ -57,11 +56,6 @@ public abstract class ASTAmbiguousNode extends ASTNode {
*/ */
public abstract IASTNode[] getNodes(); public abstract IASTNode[] getNodes();
/**
* Returns the scope that may get polluted by alternatives of this ambiguity.
*/
protected abstract IScope getAffectedScope();
@Override @Override
public final boolean accept(ASTVisitor visitor) { public final boolean accept(ASTVisitor visitor) {
if (visitor.shouldVisitAmbiguousNodes && visitor.visit(this) == ASTVisitor.PROCESS_ABORT) if (visitor.shouldVisitAmbiguousNodes && visitor.visit(this) == ASTVisitor.PROCESS_ABORT)
@ -76,7 +70,6 @@ public abstract class ASTAmbiguousNode extends ASTNode {
public IASTNode resolveAmbiguity(ASTVisitor resolver) { public IASTNode resolveAmbiguity(ASTVisitor resolver) {
beforeResolution(); beforeResolution();
final IScope scope= getAffectedScope();
final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent(); final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent();
IASTNode nodeToReplace= this; IASTNode nodeToReplace= this;
@ -85,10 +78,6 @@ public abstract class ASTAmbiguousNode extends ASTNode {
int minIssues = Integer.MAX_VALUE; int minIssues = Integer.MAX_VALUE;
for (IASTNode alternative : alternatives) { for (IASTNode alternative : alternatives) {
// flush scope, even if this is the first alternative. The ambiguous node may have contributed an
// invalid binding to the scope during the resolution of other ambiguous nodes.
ASTInternal.flushCache(scope);
// setup the ast to use the alternative // setup the ast to use the alternative
owner.replace(nodeToReplace, alternative); owner.replace(nodeToReplace, alternative);
nodeToReplace= alternative; nodeToReplace= alternative;
@ -145,7 +134,6 @@ public abstract class ASTAmbiguousNode extends ASTNode {
// switch back to the best alternative, if necessary. // switch back to the best alternative, if necessary.
if (nodeToReplace != bestAlternative) { if (nodeToReplace != bestAlternative) {
ASTInternal.flushCache(scope);
owner.replace(nodeToReplace, bestAlternative); owner.replace(nodeToReplace, bestAlternative);
} }
return bestAlternative; return bestAlternative;

View file

@ -17,7 +17,6 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.dom.parser.c.CScope;
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalFunction; import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
@ -43,37 +42,12 @@ public class ASTInternal {
return null; return null;
} }
public static void flushCache(IScope scope) {
if (scope instanceof CScope) {
((CScope) scope).flushCache();
}
}
public static boolean isFullyCached(IScope scope) throws DOMException {
if (scope instanceof CScope) {
return ((CScope) scope).isFullyCached();
}
return true;
}
public static void setFullyCached(IScope scope, boolean val) throws DOMException {
if (scope instanceof CScope) {
((CScope) scope).setFullyCached(val);
}
}
public static void addBinding(IScope scope, IBinding binding) throws DOMException { public static void addBinding(IScope scope, IBinding binding) throws DOMException {
if (scope instanceof IASTInternalScope) { if (scope instanceof IASTInternalScope) {
((IASTInternalScope) scope).addBinding(binding); ((IASTInternalScope) scope).addBinding(binding);
} }
} }
public static void removeBinding(IScope scope, IBinding binding) throws DOMException {
if (scope instanceof CScope) {
((CScope) scope).removeBinding(binding);
}
}
public static void addName(IScope scope, IASTName name) { public static void addName(IScope scope, IASTName name) {
if (scope instanceof IASTInternalScope) { if (scope instanceof IASTInternalScope) {
try { try {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2008 Wind River Systems, Inc. and others. * Copyright (c) 2008, 2009 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -28,7 +28,7 @@ public class DeclarationOptions {
public static final DeclarationOptions public static final DeclarationOptions
GLOBAL= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | ALLOW_CONSTRUCTOR_INITIALIZER), GLOBAL= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | ALLOW_CONSTRUCTOR_INITIALIZER),
FUNCTION_STYLE_ASM= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | NO_INITIALIZER), FUNCTION_STYLE_ASM= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | NO_INITIALIZER),
C_MEMBER= new DeclarationOptions(ALLOW_BITFIELD), C_MEMBER= new DeclarationOptions(ALLOW_BITFIELD | ALLOW_ABSTRACT),
CPP_MEMBER= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | ALLOW_BITFIELD | ALLOW_CONSTRUCTOR_INITIALIZER), CPP_MEMBER= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | ALLOW_BITFIELD | ALLOW_CONSTRUCTOR_INITIALIZER),
LOCAL= new DeclarationOptions(ALLOW_CONSTRUCTOR_INITIALIZER), LOCAL= new DeclarationOptions(ALLOW_CONSTRUCTOR_INITIALIZER),
PARAMETER= new DeclarationOptions(ALLOW_ABSTRACT), PARAMETER= new DeclarationOptions(ALLOW_ABSTRACT),

View file

@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2009 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
/**
* Needed to handle the ambiguity for simple declarations in plain C
* @since 5.1
*/
public interface IASTAmbiguousSimpleDeclaration extends IASTSimpleDeclaration {
}

View file

@ -41,4 +41,11 @@ public interface IASTInternalScope extends IScope {
* @throws DOMException * @throws DOMException
*/ */
public void addName(IASTName name) throws DOMException; public void addName(IASTName name) throws DOMException;
/**
* Can be called during ambiguity resolution to populate a scope without considering
* the ambiguous branches. The rest of the names has to be cached one by one after
* the ambiguities have been resolved.
*/
public void populateCache();
} }

View file

@ -239,4 +239,7 @@ public class ProblemBinding extends PlatformObject implements IProblemBinding, I
this.arg= null; this.arg= null;
} }
} }
public void populateCache() {
}
} }

View file

@ -1,23 +0,0 @@
/*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
public abstract class CASTAmbiguity extends ASTAmbiguousNode {
@Override
protected IScope getAffectedScope() {
return CVisitor.getContainingScope(this);
}
}

View file

@ -11,6 +11,14 @@
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode; import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
/** /**
@ -25,7 +33,34 @@ public final class CASTAmbiguityResolver extends ASTVisitor {
@Override @Override
public int visit(ASTAmbiguousNode astAmbiguousNode) { public int visit(ASTAmbiguousNode astAmbiguousNode) {
astAmbiguousNode.resolveAmbiguity(this); IASTNode node= astAmbiguousNode.resolveAmbiguity(this);
if (node instanceof IASTDeclarator || node instanceof IASTParameterDeclaration
|| node instanceof IASTDeclaration) {
while(node != null) {
if (node instanceof IASTDeclaration || node instanceof IASTParameterDeclaration) {
repopulateScope(node);
break;
}
if (node instanceof IASTExpression) {
break;
}
node= node.getParent();
}
} else if (node instanceof IASTDeclarationStatement) {
repopulateScope(((IASTDeclarationStatement) node).getDeclaration());
}
return PROCESS_SKIP; return PROCESS_SKIP;
} }
private void repopulateScope(IASTNode node) {
IScope scope= CVisitor.getContainingScope(node);
if (scope instanceof CScope) {
CScope cscope = (CScope) scope;
if (scope instanceof ICCompositeTypeScope) {
cscope.markAsUncached();
} else {
cscope.collectNames(node);
}
}
}
} }

View file

@ -15,8 +15,11 @@ import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
/** /**
@ -25,7 +28,7 @@ import org.eclipse.core.runtime.Assert;
* Example: void f(int (D)); // is D a type? * Example: void f(int (D)); // is D a type?
* @since 5.0.1 * @since 5.0.1
*/ */
public class CASTAmbiguousDeclarator extends CASTAmbiguity implements IASTAmbiguousDeclarator { public class CASTAmbiguousDeclarator extends ASTAmbiguousNode implements IASTAmbiguousDeclarator {
private IASTDeclarator[] dtors = new IASTDeclarator[2]; private IASTDeclarator[] dtors = new IASTDeclarator[2];
private int dtorPos=-1; private int dtorPos=-1;
@ -39,6 +42,15 @@ public class CASTAmbiguousDeclarator extends CASTAmbiguity implements IASTAmbigu
} }
} }
@Override
protected void beforeResolution() {
// populate containing scope, so that it will not be affected by the alternative branches.
IScope scope= CVisitor.getContainingScope(this);
if (scope instanceof IASTInternalScope) {
((IASTInternalScope) scope).populateCache();
}
}
public void addDeclarator(IASTDeclarator d) { public void addDeclarator(IASTDeclarator d) {
assertNotFrozen(); assertNotFrozen();
if (d != null) { if (d != null) {

View file

@ -13,12 +13,12 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousExpression; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousExpression;
public class CASTAmbiguousExpression extends CASTAmbiguity implements IASTAmbiguousExpression { public class CASTAmbiguousExpression extends ASTAmbiguousNode implements IASTAmbiguousExpression {
private IASTExpression [] expressions = new IASTExpression[2]; private IASTExpression [] expressions = new IASTExpression[2];
private int expressionsPos=-1; private int expressionsPos=-1;
@ -29,12 +29,6 @@ public class CASTAmbiguousExpression extends CASTAmbiguity implements IASTAmbigu
addExpression(e); addExpression(e);
} }
@Override
protected IScope getAffectedScope() {
// an expression cannot pollute a parent scope
return null;
}
public void addExpression(IASTExpression e) { public void addExpression(IASTExpression e) {
assertNotFrozen(); assertNotFrozen();
if (e != null) { if (e != null) {

View file

@ -14,8 +14,11 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousParameterDeclaration; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
/** /**
@ -24,7 +27,7 @@ import org.eclipse.core.runtime.Assert;
* void function(const D*); // is D a type? * void function(const D*); // is D a type?
* @since 5.0.1 * @since 5.0.1
*/ */
public class CASTAmbiguousParameterDeclaration extends CASTAmbiguity implements IASTAmbiguousParameterDeclaration { public class CASTAmbiguousParameterDeclaration extends ASTAmbiguousNode implements IASTAmbiguousParameterDeclaration {
private IASTParameterDeclaration[] paramDecls = new IASTParameterDeclaration[2]; private IASTParameterDeclaration[] paramDecls = new IASTParameterDeclaration[2];
private int declPos=-1; private int declPos=-1;
@ -44,6 +47,15 @@ public class CASTAmbiguousParameterDeclaration extends CASTAmbiguity implements
} }
} }
@Override
protected void beforeResolution() {
// populate containing scope, so that it will not be affected by the alternative branches.
IScope scope= CVisitor.getContainingScope(this);
if (scope instanceof IASTInternalScope) {
((IASTInternalScope) scope).populateCache();
}
}
public IASTParameterDeclaration[] getParameterDeclarations() { public IASTParameterDeclaration[] getParameterDeclarations() {
paramDecls = (IASTParameterDeclaration[]) ArrayUtil.removeNullsAfter(IASTParameterDeclaration.class, paramDecls, declPos ); paramDecls = (IASTParameterDeclaration[]) ArrayUtil.removeNullsAfter(IASTParameterDeclaration.class, paramDecls, declPos );
return paramDecls; return paramDecls;

View file

@ -0,0 +1,120 @@
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
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;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousSimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
/**
* Handles ambiguities for parameter declarations.
* <br>
* void function(const D*); // is D a type?
* @since 5.0.1
*/
public class CASTAmbiguousSimpleDeclaration extends ASTAmbiguousNode implements IASTAmbiguousSimpleDeclaration {
private IASTSimpleDeclaration fSimpleDecl;
private IASTDeclSpecifier fAltDeclSpec;
private IASTDeclarator fAltDtor;
public CASTAmbiguousSimpleDeclaration(IASTSimpleDeclaration decl, IASTDeclSpecifier declSpec, IASTDeclarator dtor) {
fSimpleDecl= decl;
fAltDeclSpec= declSpec;
fAltDtor= dtor;
}
@Override
protected void beforeResolution() {
// populate containing scope, so that it will not be affected by the alternative branches.
IScope scope= CVisitor.getContainingScope(this);
if (scope instanceof IASTInternalScope) {
((IASTInternalScope) scope).populateCache();
}
}
@Override
public IASTNode[] getNodes() {
return new IASTNode[] {fSimpleDecl, fAltDeclSpec, fAltDtor};
}
public IASTSimpleDeclaration copy() {
throw new UnsupportedOperationException();
}
public void addDeclarator(IASTDeclarator declarator) {
fSimpleDecl.addDeclarator(declarator);
}
public IASTDeclSpecifier getDeclSpecifier() {
return fSimpleDecl.getDeclSpecifier();
}
public IASTDeclarator[] getDeclarators() {
return fSimpleDecl.getDeclarators();
}
public void setDeclSpecifier(IASTDeclSpecifier declSpec) {
fSimpleDecl.setDeclSpecifier(declSpec);
}
@Override
public final IASTNode resolveAmbiguity(ASTVisitor visitor) {
final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent();
IASTNode nodeToReplace= this;
// handle nested ambiguities first
owner.replace(nodeToReplace, fSimpleDecl);
IASTDeclSpecifier declSpec= fSimpleDecl.getDeclSpecifier();
declSpec.accept(visitor);
// find nested names
final NameCollector nameCollector= new NameCollector();
declSpec.accept(nameCollector);
final IASTName[] names= nameCollector.getNames();
// resolve names
boolean hasIssue= false;
for (IASTName name : names) {
try {
IBinding b = name.resolveBinding();
if (b instanceof IProblemBinding) {
hasIssue= true;
break;
}
} catch (Exception t) {
hasIssue= true;
break;
}
}
if (hasIssue) {
// use the alternate version
final IASTAmbiguityParent parent = (IASTAmbiguityParent) fSimpleDecl;
parent.replace(declSpec, fAltDeclSpec);
parent.replace(fSimpleDecl.getDeclarators()[0], fAltDtor);
}
// resolve further nested ambiguities
fSimpleDecl.accept(visitor);
return fSimpleDecl;
}
}

View file

@ -13,21 +13,30 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
public class CASTAmbiguousStatement extends CASTAmbiguity implements IASTAmbiguousStatement { public class CASTAmbiguousStatement extends ASTAmbiguousNode implements IASTAmbiguousStatement {
private IASTStatement [] stmts = new IASTStatement[2]; private IASTStatement [] stmts = new IASTStatement[2];
private int stmtsPos=-1; private int stmtsPos=-1;
public CASTAmbiguousStatement(IASTStatement... statements) { public CASTAmbiguousStatement(IASTStatement... statements) {
for(IASTStatement s : statements) for(IASTStatement s : statements)
addStatement(s); addStatement(s);
} }
@Override
protected void beforeResolution() {
// populate containing scope, so that it will not be affected by the alternative branches.
IScope scope= CVisitor.getContainingScope(this);
if (scope instanceof IASTInternalScope) {
((IASTInternalScope) scope).populateCache();
}
}
public void addStatement(IASTStatement s) { public void addStatement(IASTStatement s) {
assertNotFrozen(); assertNotFrozen();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others. * Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -15,15 +15,17 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IScope; 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.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/** /**
* Implementation for C composite specifiers. * Implementation for C composite specifiers.
*/ */
public class CASTCompositeTypeSpecifier extends CASTBaseDeclSpecifier implements public class CASTCompositeTypeSpecifier extends CASTBaseDeclSpecifier implements
ICASTCompositeTypeSpecifier { ICASTCompositeTypeSpecifier, IASTAmbiguityParent {
private int key; private int key;
private IASTName name; private IASTName name;
@ -134,5 +136,17 @@ public class CASTCompositeTypeSpecifier extends CASTBaseDeclSpecifier implements
return r_unclear; return r_unclear;
} }
public void replace(IASTNode child, IASTNode other) {
if (declarations == null) return;
for(int i=0; i < declarations.length; ++i) {
if (declarations[i] == null) break;
if (declarations[i] == child) {
other.setParent(child.getParent());
other.setPropertyInParent(child.getPropertyInParent());
declarations[i] = (IASTDeclaration) other;
break;
}
}
}
} }

View file

@ -1,25 +1,28 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others. * Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Rational Software - Initial API and implementation * John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research) * Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement; import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/** /**
* @author jcamelon * A declaration statement.
*/ */
public class CASTDeclarationStatement extends ASTNode implements IASTDeclarationStatement { public class CASTDeclarationStatement extends ASTNode implements IASTDeclarationStatement, IASTAmbiguityParent {
private IASTDeclaration declaration; private IASTDeclaration declaration;
@ -71,4 +74,12 @@ public class CASTDeclarationStatement extends ASTNode implements IASTDeclaration
} }
return true; return true;
} }
public void replace(IASTNode child, IASTNode other) {
if (child == declaration) {
other.setPropertyInParent(child.getPropertyInParent());
other.setParent(child.getParent());
declaration = (IASTDeclaration) other;
}
}
} }

View file

@ -1,31 +1,37 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others. * Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Rational Software - Initial API and implementation * John Camelon (IBM Rational Software) - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
/** /**
* @author jcamelon * Models function declarators for plain c.
*/ */
public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStandardFunctionDeclarator { public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStandardFunctionDeclarator {
private IASTParameterDeclaration [] parameters = null; private IASTParameterDeclaration [] parameters = null;
private int parametersPos=-1; private int parametersPos=-1;
private boolean varArgs; private boolean varArgs;
private IScope scope;
public CASTFunctionDeclarator() { public CASTFunctionDeclarator() {
} }
@ -94,4 +100,26 @@ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStanda
} }
super.replace(child, other); super.replace(child, other);
} }
public IScope getFunctionScope() {
if (scope != null)
return scope;
// introduce a scope for function declarations and definitions, only.
IASTNode node= getParent();
while(!(node instanceof IASTDeclaration)) {
if (node==null)
return null;
node= node.getParent();
}
if (node instanceof IASTParameterDeclaration)
return null;
if (node instanceof IASTFunctionDefinition) {
scope= ((IASTFunctionDefinition) node).getScope();
} else if (ASTQueries.findTypeRelevantDeclarator(this) == this) {
scope = new CScope(this, EScopeKind.eLocal);
}
return scope;
}
} }

View file

@ -1,12 +1,12 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others. * Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Rational Software - Initial API and implementation * John Camelon (IBM Rational Software) - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Yuan Zhang / Beth Tibbitts (IBM Research) * Yuan Zhang / Beth Tibbitts (IBM Research)
*******************************************************************************/ *******************************************************************************/
@ -15,14 +15,16 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/** /**
* @author jcamelon * Models a simple declaration.
*/ */
public class CASTSimpleDeclaration extends ASTNode implements IASTSimpleDeclaration { public class CASTSimpleDeclaration extends ASTNode implements IASTSimpleDeclaration, IASTAmbiguityParent {
public CASTSimpleDeclaration() { public CASTSimpleDeclaration() {
@ -101,4 +103,22 @@ public class CASTSimpleDeclaration extends ASTNode implements IASTSimpleDeclarat
} }
return true; return true;
} }
public void replace(IASTNode child, IASTNode other) {
if (declSpecifier == child) {
other.setParent(child.getParent());
other.setPropertyInParent(child.getPropertyInParent());
declSpecifier= (IASTDeclSpecifier) other;
} else {
IASTDeclarator[] declarators = getDeclarators();
for(int i = 0; i < declarators.length; i++) {
if(declarators[i] == child) {
declarators[i] = (IASTDeclarator)other;
other.setParent(child.getParent());
other.setPropertyInParent(child.getPropertyInParent());
break;
}
}
}
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2008 IBM Corporation and others. * Copyright (c) 2002, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -16,7 +16,9 @@ import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTName; 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.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding; import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
@ -24,11 +26,12 @@ import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.Linkage; import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/** /**
* C-specific implementation of a translation unit. * C-specific implementation of a translation unit.
*/ */
public class CASTTranslationUnit extends ASTTranslationUnit { public class CASTTranslationUnit extends ASTTranslationUnit implements IASTAmbiguityParent {
private CScope compilationUnit = null; private CScope compilationUnit = null;
@ -93,4 +96,17 @@ public class CASTTranslationUnit extends ASTTranslationUnit {
protected ASTVisitor createAmbiguityNodeVisitor() { protected ASTVisitor createAmbiguityNodeVisitor() {
return new CASTAmbiguityResolver(); return new CASTAmbiguityResolver();
} }
public void replace(IASTNode child, IASTNode other) {
if (fDeclarations == null) return;
for(int i=0; i < fDeclarations.length; ++i) {
if (fDeclarations[i] == null) break;
if (fDeclarations[i] == child) {
other.setParent(child.getParent());
other.setPropertyInParent(child.getPropertyInParent());
fDeclarations[i] = (IASTDeclaration) other;
return;
}
}
}
} }

View file

@ -1,32 +1,35 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * Andrew Niefer (IBM Corporation) - initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
/*
* Created on Nov 25, 2004
*/
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName; 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; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; 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.ICCompositeTypeScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
/** /**
* @author aniefer * Implementation of scope for structs and unions.
*/ */
public class CCompositeTypeScope extends CScope implements ICCompositeTypeScope { public class CCompositeTypeScope extends CScope implements ICCompositeTypeScope {
public CCompositeTypeScope( ICASTCompositeTypeSpecifier compTypeSpec ){ public CCompositeTypeScope( ICASTCompositeTypeSpecifier compTypeSpec ){
@ -71,4 +74,39 @@ public class CCompositeTypeScope extends CScope implements ICCompositeTypeScope
return new CStructure.CStructureProblem( compSpec.getName(), IProblemBinding.SEMANTIC_BAD_SCOPE, compSpec.getName().toCharArray() ); return new CStructure.CStructureProblem( compSpec.getName(), IProblemBinding.SEMANTIC_BAD_SCOPE, compSpec.getName().toCharArray() );
} }
@Override
protected void doPopulateCache() {
ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) getPhysicalNode();
ICASTCompositeTypeSpecifier[] specStack = null;
int stackIdx = -1;
IASTDeclaration[] members = compSpec.getMembers();
while (members != null) {
int size = members.length;
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 = ASTQueries.findInnermostDeclarator(declarator).getName();
ASTInternal.addName(this, dtorName);
}
// anonymous structures and unions
if (declarators.length == 0 && ((IASTSimpleDeclaration) node).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier) {
IASTCompositeTypeSpecifier declSpec = (IASTCompositeTypeSpecifier) ((IASTSimpleDeclaration) node).getDeclSpecifier();
IASTName n = declSpec.getName();
if (n.toCharArray().length == 0) {
specStack = (ICASTCompositeTypeSpecifier[]) ArrayUtil.append(ICASTCompositeTypeSpecifier.class, specStack, declSpec);
}
}
}
}
if (specStack != null && ++stackIdx < specStack.length && specStack[stackIdx] != null) {
members = specStack[stackIdx].getMembers();
} else {
members = null;
}
}
}
} }

View file

@ -1,12 +1,12 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * Andrew Niefer (IBM Corporation) - initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Bryan Wilkinson (QNX) * Bryan Wilkinson (QNX)
* Andrew Ferguson (Symbian) * Andrew Ferguson (Symbian)
@ -20,13 +20,22 @@ import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement; import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier; import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression; import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
@ -35,22 +44,30 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.c.ICScope;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression; import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.ObjectMap; import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousSimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope; import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
/** /**
* @author aniefer * Base implementation for c-scopes
*/ */
public class CScope implements ICScope, IASTInternalScope { public class CScope implements ICScope, IASTInternalScope {
/** /**
@ -92,10 +109,9 @@ public class CScope implements ICScope, IASTInternalScope {
}; };
private IASTNode physicalNode = null; private IASTNode physicalNode = null;
private boolean isFullyCached = false; private boolean isCached = false;
private CharArrayObjectMap[] mapsToNameOrBinding = { CharArrayObjectMap.EMPTY_MAP, CharArrayObjectMap.EMPTY_MAP }; private CharArrayObjectMap[] mapsToNameOrBinding = { CharArrayObjectMap.EMPTY_MAP, CharArrayObjectMap.EMPTY_MAP };
private ObjectMap reuseBindings= null;
private final EScopeKind kind; private final EScopeKind kind;
public CScope(IASTNode physical, EScopeKind eKind) { public CScope(IASTNode physical, EScopeKind eKind) {
@ -149,7 +165,7 @@ public class CScope implements ICScope, IASTInternalScope {
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find(String name) throws DOMException { public IBinding[] find(String name) throws DOMException {
return CVisitor.findBindings( this, name, false ); return CVisitor.findBindings(this, name);
} }
public IBinding getBinding( int namespaceType, char [] name ){ public IBinding getBinding( int namespaceType, char [] name ){
@ -160,26 +176,12 @@ public class CScope implements ICScope, IASTInternalScope {
if (o instanceof IASTName) if (o instanceof IASTName)
return ((IASTName) o).resolveBinding(); return ((IASTName) o).resolveBinding();
if (o instanceof IASTName[]) {
return ((IASTName[]) o)[0].resolveBinding();
}
return null; 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) {
int type = ( binding instanceof ICompositeType || binding instanceof IEnumeration ) ?
NAMESPACE_TYPE_TAG : NAMESPACE_TYPE_OTHER;
final CharArrayObjectMap bindingsMap = mapsToNameOrBinding[type];
if( bindingsMap != CharArrayObjectMap.EMPTY_MAP ) {
Object o= bindingsMap.remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
if (o != null && reuseBindings != null) {
reuseBindings.remove(o);
}
}
isFullyCached = false;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode() * @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode()
*/ */
@ -187,40 +189,35 @@ public class CScope implements ICScope, IASTInternalScope {
return physicalNode; 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) { public void addName(IASTName name) {
final char[] nchars = name.toCharArray();
if (nchars.length == 0)
return;
int type = getNamespaceType(name); int type = getNamespaceType(name);
CharArrayObjectMap map = mapsToNameOrBinding[type]; CharArrayObjectMap map = mapsToNameOrBinding[type];
if (map == CharArrayObjectMap.EMPTY_MAP) if (map == CharArrayObjectMap.EMPTY_MAP)
map = mapsToNameOrBinding[type] = new CharArrayObjectMap(1); map = mapsToNameOrBinding[type] = new CharArrayObjectMap(1);
final char [] n = name.toCharArray(); Object o= map.get(nchars);
final Object current= map.get( n ); if (o instanceof IASTName) {
if (current instanceof IASTName) { if (o != name) {
final CASTName currentName = (CASTName)current; map.put(nchars, new IASTName[] {(IASTName) o, name});
if (currentName.getOffset() <= ((CASTName) name).getOffset() ){ }
} else if (o instanceof IASTName[]) {
final IASTName[] names = (IASTName[]) o;
for (IASTName n : names) {
if (n == null)
break;
if (n == name)
return; return;
} }
if (name.getBinding() == null) { final IASTName[] newNames= (IASTName[]) ArrayUtil.append(IASTName.class, names, name);
// bug 232300: we need to make sure that the binding is picked up even if the name is removed if (newNames != names)
// from the cache. Simply assigning it to the name is not enough, because a declaration or map.put(nchars, newNames);
// definition needs to be added to the binding. } else {
IBinding reuseBinding= currentName.getBinding(); map.put(nchars, name);
if (reuseBinding == null && reuseBindings != null) {
reuseBinding= (IBinding) reuseBindings.get(currentName);
} }
if (reuseBinding != null) {
if (reuseBindings == null) {
reuseBindings= new ObjectMap(1);
}
reuseBindings.put(name, reuseBinding);
reuseBindings.remove(currentName);
}
}
}
map.put(n, name);
} }
private int getNamespaceType( IASTName name ){ private int getNamespaceType( IASTName name ){
@ -243,12 +240,13 @@ public class CScope implements ICScope, IASTInternalScope {
return getBindings(name, resolve, prefix, IIndexFileSet.EMPTY); return getBindings(name, resolve, prefix, IIndexFileSet.EMPTY);
} }
public IBinding getBinding( IASTName name, boolean resolve, IIndexFileSet fileSet ) { public final IBinding getBinding(IASTName name, boolean resolve, IIndexFileSet fileSet) {
char[] c = name.toCharArray(); char[] c = name.toCharArray();
if( c.length == 0 ){ if( c.length == 0 ){
return null; return null;
} }
populateCache();
final int type = getNamespaceType(name); final int type = getNamespaceType(name);
Object o = mapsToNameOrBinding[type].get(name.toCharArray()); Object o = mapsToNameOrBinding[type].get(name.toCharArray());
@ -256,25 +254,21 @@ public class CScope implements ICScope, IASTInternalScope {
return (IBinding) o; return (IBinding) o;
if (o instanceof IASTName) { if (o instanceof IASTName) {
final IASTName n= (IASTName) o; IBinding b= extractBinding((IASTName) o, resolve, name);
if (!isTypeDefinition(name) || CVisitor.declaredBefore(n, name)) {
IBinding b= n.getBinding();
if (b != null) if (b != null)
return b; return b;
} else if (o instanceof IASTName[]) {
if (reuseBindings != null) { for (IASTName n: ((IASTName[]) o)) {
b= (IBinding) reuseBindings.get(n); if (n == null)
break;
IBinding b= extractBinding(n, resolve, name);
if (b != null) if (b != null)
return b; return b;
} }
if (resolve && n != name) {
return n.resolveBinding();
}
}
} }
IBinding result= null; IBinding result= null;
if(physicalNode instanceof IASTTranslationUnit) { if (resolve && physicalNode instanceof IASTTranslationUnit) {
final IASTTranslationUnit tu = (IASTTranslationUnit)physicalNode; final IASTTranslationUnit tu = (IASTTranslationUnit)physicalNode;
IIndex index= tu.getIndex(); IIndex index= tu.getIndex();
if(index!=null) { if(index!=null) {
@ -292,33 +286,47 @@ public class CScope implements ICScope, IASTInternalScope {
return result; return result;
} }
private boolean isTypeDefinition(IASTName name) { private IBinding extractBinding(final IASTName candidate, boolean resolve, IASTName forName) {
if (name.getPropertyInParent()==IASTNamedTypeSpecifier.NAME) { if (!resolve || acceptDeclaredAfter(forName) || CVisitor.declaredBefore(candidate, forName)) {
return true; if (resolve && candidate != forName) {
return candidate.resolveBinding();
}
return candidate.getBinding();
}
return null;
}
private boolean acceptDeclaredAfter(IASTName name) {
if (getKind() != EScopeKind.eGlobal)
return false;
final ASTNodeProperty propertyInParent = name.getPropertyInParent();
if (propertyInParent==IASTNamedTypeSpecifier.NAME ||
propertyInParent == IASTElaboratedTypeSpecifier.TYPE_NAME) {
return false;
} }
IASTNode parent= name.getParent(); IASTNode parent= name.getParent();
while (parent != null) { while (parent != null) {
if (parent instanceof IASTUnaryExpression) { if (parent instanceof IASTUnaryExpression) {
if (((IASTUnaryExpression) parent).getOperator() == IGNUASTUnaryExpression.op_typeof) if (((IASTUnaryExpression) parent).getOperator() == IGNUASTUnaryExpression.op_typeof)
return true; return false;
} }
else if (parent instanceof IASTTypeIdExpression) { else if (parent instanceof IASTTypeIdExpression) {
if (((IASTTypeIdExpression) parent).getOperator() == IASTTypeIdExpression.op_typeof) if (((IASTTypeIdExpression) parent).getOperator() == IASTTypeIdExpression.op_typeof)
return true; return false;
} }
parent= parent.getParent(); parent= parent.getParent();
} }
return false; return true;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(org.eclipse.cdt.core.dom.ast.IASTName, boolean) * @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(org.eclipse.cdt.core.dom.ast.IASTName, boolean)
*/ */
public IBinding[] getBindings( IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet ) { public final IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet) {
char [] c = name.toCharArray(); char [] c = name.toCharArray();
Object[] obj = null; Object[] obj = null;
populateCache();
for (CharArrayObjectMap map : mapsToNameOrBinding) { for (CharArrayObjectMap map : mapsToNameOrBinding) {
if (prefixLookup) { if (prefixLookup) {
Object[] keys = map.keyArray(); Object[] keys = map.keyArray();
@ -338,9 +346,8 @@ public class CScope implements ICScope, IASTInternalScope {
IIndex index = tu.getIndex(); IIndex index = tu.getIndex();
if (index != null) { if (index != null) {
try { try {
IBinding[] bindings = prefixLookup ? IBinding[] bindings = prefixLookup ? index.findBindingsForPrefix(name.toCharArray(), true, INDEX_FILTERS[NAMESPACE_TYPE_BOTH], null)
index.findBindingsForPrefix(name.toCharArray(), true, INDEX_FILTERS[NAMESPACE_TYPE_BOTH], null) : : index.findBindings(name.toCharArray(), INDEX_FILTERS[NAMESPACE_TYPE_BOTH], null);
index.findBindings(name.toCharArray(), INDEX_FILTERS[NAMESPACE_TYPE_BOTH], null);
if (fileSet != null) { if (fileSet != null) {
bindings = fileSet.filterFileLocalBindings(bindings); bindings = fileSet.filterFileLocalBindings(bindings);
} }
@ -357,21 +364,24 @@ public class CScope implements ICScope, IASTInternalScope {
for (Object element : obj) { for (Object element : obj) {
if (element instanceof IBinding) { if (element instanceof IBinding) {
result = (IBinding[]) ArrayUtil.append(IBinding.class, result, element); result = (IBinding[]) ArrayUtil.append(IBinding.class, result, element);
} else if (element instanceof IASTName) { } else {
final IASTName n= (IASTName) element; IASTName n= null;
if (element instanceof IASTName) {
n= (IASTName) element;
} else if (element instanceof IASTName[]) {
n = ((IASTName[]) element)[0];
}
if (n != null) {
IBinding b = n.getBinding(); IBinding b = n.getBinding();
if (b == null) { if (b == null) {
if (reuseBindings != null) { if (resolve && n != name) {
b= (IBinding) reuseBindings.get(n);
}
if (resolve && b == null && n != name) {
b = n.resolveBinding(); b = n.resolveBinding();
} }
} }
if (b != null) { if (b != null) {
result = (IBinding[]) ArrayUtil.append(IBinding.class, result, b); result = (IBinding[]) ArrayUtil.append(IBinding.class, result, b);
} }
}
} }
} }
@ -392,18 +402,163 @@ public class CScope implements ICScope, IASTInternalScope {
return bindings[0]; return bindings[0];
} }
/* (non-Javadoc) public void populateCache() {
* @see org.eclipse.cdt.core.dom.ast.c.ICScope#setFullyCached(boolean) if (isCached)
*/ return;
public void setFullyCached( boolean b ){
isFullyCached = b; doPopulateCache();
isCached= true;
} }
/* (non-Javadoc) protected void doPopulateCache() {
* @see org.eclipse.cdt.core.dom.ast.c.ICScope#isFullyCached() final IASTNode scopeNode = physicalNode;
*/ IASTNode[] nodes = null;
public boolean isFullyCached(){ if (scopeNode instanceof IASTCompoundStatement) {
return isFullyCached; IASTCompoundStatement compound = (IASTCompoundStatement) scopeNode;
if (scopeNode.getParent() instanceof IASTFunctionDefinition) {
IASTFunctionDeclarator dtor = ((IASTFunctionDefinition) scopeNode.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 (scopeNode instanceof IASTTranslationUnit) {
IASTTranslationUnit translation = (IASTTranslationUnit) scopeNode;
nodes = translation.getDeclarations();
} else if (scopeNode instanceof IASTStandardFunctionDeclarator) {
IASTStandardFunctionDeclarator dtor = (IASTStandardFunctionDeclarator) scopeNode;
nodes = dtor.getParameters();
} else if (scopeNode instanceof ICASTKnRFunctionDeclarator) {
ICASTKnRFunctionDeclarator dtor = (ICASTKnRFunctionDeclarator) scopeNode;
nodes = dtor.getParameterDeclarations();
} else if (scopeNode instanceof IASTForStatement) {
final IASTForStatement forStmt = (IASTForStatement) scopeNode;
nodes= new IASTNode[] {forStmt.getInitializerStatement()};
}
if (nodes != null) {
int idx = -1;
IASTNode node = nodes.length > 0 ? nodes[++idx] : null;
while (node != null) {
collectNames(node);
if (idx > -1 && ++idx < nodes.length) {
node = nodes[idx];
} else {
node = null;
if (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 = null;
if (scopeNode instanceof IASTCompoundStatement) {
compound = (IASTCompoundStatement) scopeNode;
} else if (scopeNode instanceof IASTFunctionDeclarator) {
IASTNode n = scopeNode.getParent();
while (n instanceof IASTDeclarator)
n = n.getParent();
if (n instanceof IASTFunctionDefinition) {
compound = (IASTCompoundStatement) ((IASTFunctionDefinition) n).getBody();
}
}
if (compound != null) {
nodes = compound.getStatements();
if (nodes.length > 0) {
idx = 0;
node = nodes[0];
}
}
}
}
}
}
}
public void collectNames(IASTNode node) {
if (node instanceof IASTDeclaration) {
collectNames((IASTDeclaration) node);
} else if (node instanceof IASTParameterDeclaration) {
collectNames((IASTParameterDeclaration) node);
} else if (node instanceof IASTDeclarationStatement) {
collectNames(((IASTDeclarationStatement)node).getDeclaration());
}
}
private void collectNames(IASTParameterDeclaration paramDecl) {
if (paramDecl == null || paramDecl instanceof IASTAmbiguousParameterDeclaration)
return;
collectNames(paramDecl.getDeclarator());
collectNames(paramDecl.getDeclSpecifier());
}
private void collectNames(IASTDeclarator dtor) {
IASTDeclarator innermost= null;
while (dtor != null) {
if (dtor instanceof IASTAmbiguousDeclarator) {
innermost= null;
break;
}
innermost= dtor;
dtor= dtor.getNestedDeclarator();
}
if (innermost != null)
ASTInternal.addName(this, innermost.getName());
}
private void collectNames(IASTDeclaration declaration) {
if (declaration instanceof IASTAmbiguousSimpleDeclaration)
return;
if (declaration instanceof IASTSimpleDeclaration) {
IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
IASTDeclarator[] declarators = simpleDeclaration.getDeclarators();
for (IASTDeclarator dtor : declarators) {
collectNames(dtor);
}
collectNames(simpleDeclaration.getDeclSpecifier());
} else if (declaration instanceof IASTFunctionDefinition) {
IASTFunctionDefinition functionDef = (IASTFunctionDefinition) declaration;
collectNames(functionDef.getDeclarator());
collectNames(functionDef.getDeclSpecifier());
}
}
private void collectNames(IASTDeclSpecifier declSpec) {
IASTName tempName = null;
if (declSpec instanceof ICASTElaboratedTypeSpecifier) {
tempName = ((ICASTElaboratedTypeSpecifier)declSpec).getName();
ASTInternal.addName(this, tempName);
} else if (declSpec instanceof ICASTCompositeTypeSpecifier) {
tempName = ((ICASTCompositeTypeSpecifier)declSpec).getName();
ASTInternal.addName(this, tempName);
//also have to check for any nested structs
IASTDeclaration[] nested = ((ICASTCompositeTypeSpecifier)declSpec).getMembers();
for (IASTDeclaration element : nested) {
if (element instanceof IASTSimpleDeclaration) {
IASTDeclSpecifier d = ((IASTSimpleDeclaration)element).getDeclSpecifier();
if (d instanceof ICASTCompositeTypeSpecifier || d instanceof IASTEnumerationSpecifier) {
collectNames(d);
}
}
}
} else if (declSpec instanceof ICASTEnumerationSpecifier) {
ICASTEnumerationSpecifier enumeration = (ICASTEnumerationSpecifier) declSpec;
tempName = enumeration.getName();
ASTInternal.addName(this, tempName);
//check enumerators
IASTEnumerator[] list = ((ICASTEnumerationSpecifier) declSpec).getEnumerators();
for (IASTEnumerator enumerator : list) {
if (enumerator == null) break;
tempName = enumerator.getName();
ASTInternal.addName(this, tempName);
}
}
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -416,40 +571,6 @@ public class CScope implements ICScope, IASTInternalScope {
return null; return null;
} }
public void flushCache() {
CharArrayObjectMap map= mapsToNameOrBinding[0];
CharArrayObjectMap builtins= null;
for (int i = 0; i < map.size(); i++) {
Object obj= map.getAt(i);
if (obj instanceof IASTName) {
((IASTName) obj).setBinding(null);
} else if (obj instanceof IBinding) {
if (builtins == null) {
builtins= new CharArrayObjectMap(2);
}
builtins.put(((IBinding) obj).getNameCharArray(), obj);
}
}
mapsToNameOrBinding[0]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
map= mapsToNameOrBinding[1];
builtins= null;
for (int i = 0; i < map.size(); i++) {
Object obj= map.getAt(i);
if (obj instanceof IASTName) {
((IASTName) obj).setBinding(null);
} else if (obj instanceof IBinding) {
if (builtins == null) {
builtins= new CharArrayObjectMap(2);
}
builtins.put(((IBinding) obj).getNameCharArray(), obj);
}
}
mapsToNameOrBinding[1]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
reuseBindings= null;
isFullyCached = false;
}
public void addBinding(IBinding binding) { public void addBinding(IBinding binding) {
int type = NAMESPACE_TYPE_OTHER; int type = NAMESPACE_TYPE_OTHER;
if (binding instanceof ICompositeType || binding instanceof IEnumeration) { if (binding instanceof ICompositeType || binding instanceof IEnumeration) {
@ -462,4 +583,12 @@ public class CScope implements ICScope, IASTInternalScope {
map.put(binding.getNameCharArray(), binding); map.put(binding.getNameCharArray(), binding);
} }
/**
* In case there was an ambiguity the cache has to be populated for a second time.
* However, we do not clear any names in order not to loose bindings.
*/
public void markAsUncached() {
isCached= false;
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others. * Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -21,6 +21,7 @@ import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField; import org.eclipse.cdt.core.dom.ast.IField;
@ -31,10 +32,9 @@ import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope; import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage; import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.cdt.internal.core.index.IIndexType; import org.eclipse.cdt.internal.core.index.IIndexType;
import org.eclipse.core.runtime.PlatformObject; import org.eclipse.core.runtime.PlatformObject;
@ -67,6 +67,8 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
private IASTName [] declarations = null; private IASTName [] declarations = null;
private IASTName definition; private IASTName definition;
private boolean checked;
private ICompositeType typeInIndex;
public CStructure( IASTName name ){ public CStructure( IASTName name ){
if( name.getPropertyInParent() == IASTCompositeTypeSpecifier.TYPE_NAME ) if( name.getPropertyInParent() == IASTCompositeTypeSpecifier.TYPE_NAME )
@ -80,14 +82,28 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
public IASTNode getPhysicalNode(){ public IASTNode getPhysicalNode(){
return ( definition != null ) ? (IASTNode)definition : (IASTNode)declarations[0]; return ( definition != null ) ? (IASTNode)definition : (IASTNode)declarations[0];
} }
private ICASTCompositeTypeSpecifier checkForDefinition( IASTElaboratedTypeSpecifier declSpec ){
private void checkForDefinition() {
if (!checked && definition == null) {
IASTNode declSpec = declarations[0].getParent();
if (declSpec instanceof ICASTElaboratedTypeSpecifier) {
IASTDeclSpecifier spec = CVisitor.findDefinition((ICASTElaboratedTypeSpecifier) declSpec); IASTDeclSpecifier spec = CVisitor.findDefinition((ICASTElaboratedTypeSpecifier) declSpec);
if( spec != null && spec instanceof ICASTCompositeTypeSpecifier ){ if (spec instanceof ICASTCompositeTypeSpecifier) {
ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) spec; ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) spec;
compTypeSpec.getName().setBinding( this ); definition= compTypeSpec.getName();
return compTypeSpec; definition.setBinding(this);
} }
return null; }
if (definition == null && typeInIndex == null) {
final IASTTranslationUnit translationUnit = declSpec.getTranslationUnit();
IIndex index= translationUnit.getIndex();
if (index != null) {
typeInIndex= (ICompositeType) index.adaptBinding(this);
}
}
}
checked = true;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -122,20 +138,17 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
* @see org.eclipse.cdt.core.dom.ast.ICompositeType#getFields() * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getFields()
*/ */
public IField[] getFields() throws DOMException { public IField[] getFields() throws DOMException {
checkForDefinition();
if (definition == null) { if (definition == null) {
ICASTCompositeTypeSpecifier temp = checkForDefinition( (IASTElaboratedTypeSpecifier) declarations[0].getParent() ); return new IField[] {
if( temp == null ) new CField.CFieldProblem(declarations[0], IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray())
return new IField [] { new CField.CFieldProblem( declarations[0], IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray() ) }; };
definition = temp.getName();
} }
ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent(); ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent();
IASTDeclaration[] members = compSpec.getMembers(); IASTDeclaration[] members = compSpec.getMembers();
int size = members.length; int size = members.length;
IField[] fields = new IField[ size ]; IField[] fields = new IField[ size ];
if (size > 0) { if (size > 0) {
ICCompositeTypeScope scope = (ICCompositeTypeScope) getCompositeScope();
if( ASTInternal.isFullyCached(scope) )
scope = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
IASTNode node = members[i]; IASTNode node = members[i];
if (node instanceof IASTSimpleDeclaration) { if (node instanceof IASTSimpleDeclaration) {
@ -144,77 +157,26 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
IASTDeclarator declarator = declarators[j]; IASTDeclarator declarator = declarators[j];
IASTName name = declarator.getName(); IASTName name = declarator.getName();
IBinding binding = name.resolveBinding(); IBinding binding = name.resolveBinding();
if( scope != null )
ASTInternal.addName(scope, name );
if (binding != null) if (binding != null)
fields = (IField[]) ArrayUtil.append(IField.class, fields, binding); fields = (IField[]) ArrayUtil.append(IField.class, fields, binding);
} }
} }
} }
if( scope != null )
ASTInternal.setFullyCached(scope, true);
} }
return (IField[]) ArrayUtil.trim( IField.class, fields ); return (IField[]) ArrayUtil.trim( IField.class, fields );
} }
public IField findField(String name) throws DOMException { public IField findField(String name) throws DOMException {
if (definition == null) { IScope scope = getCompositeScope();
ICASTCompositeTypeSpecifier temp = checkForDefinition((IASTElaboratedTypeSpecifier) declarations[0].getParent()); if (scope == null) {
if (temp == null)
return new CField.CFieldProblem(declarations[0], IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray()); return new CField.CFieldProblem(declarations[0], IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray());
definition = temp.getName();
} }
final char[] nchars= name.toCharArray(); final CASTName astName = new CASTName(name.toCharArray());
ICCompositeTypeScope scope = (ICCompositeTypeScope) getCompositeScope(); astName.setPropertyInParent(CVisitor.STRING_LOOKUP_PROPERTY);
if (scope != null && ASTInternal.isFullyCached(scope)) { IBinding binding = scope.getBinding(astName, true);
IBinding binding = scope.getBinding(nchars);
if (binding instanceof IField) if (binding instanceof IField)
return (IField) binding; return (IField) binding;
} else {
ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent();
ICASTCompositeTypeSpecifier[] specStack = null;
int stackIdx = -1;
IASTDeclaration[] members = compSpec.getMembers();
IField found = null;
while (members != null) {
int size = members.length;
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 = CVisitor.findInnermostDeclarator(declarator).getName();
if (scope != null)
ASTInternal.addName(scope, dtorName);
if (CharArrayUtils.equals(nchars, dtorName.toCharArray())) {
IBinding binding = dtorName.resolveBinding();
if (binding instanceof IField)
found = (IField) binding;
}
}
// anonymous structures and unions
if (declarators.length == 0 && ((IASTSimpleDeclaration) node).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier) {
IASTCompositeTypeSpecifier declSpec = (IASTCompositeTypeSpecifier) ((IASTSimpleDeclaration) node).getDeclSpecifier();
IASTName n = declSpec.getName();
if (n.toCharArray().length == 0) {
specStack = (ICASTCompositeTypeSpecifier[]) ArrayUtil.append(ICASTCompositeTypeSpecifier.class, specStack, declSpec);
}
}
}
}
if (specStack != null && ++stackIdx < specStack.length && specStack[stackIdx] != null) {
members = specStack[stackIdx].getMembers();
} else {
members = null;
}
}
if (scope != null)
ASTInternal.setFullyCached(scope, true);
if (found != null)
return found;
}
return null; return null;
} }
@ -231,7 +193,21 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
* @see org.eclipse.cdt.core.dom.ast.ICompositeType#getCompositeScope() * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getCompositeScope()
*/ */
public IScope getCompositeScope() { public IScope getCompositeScope() {
return (definition != null ) ? ((IASTCompositeTypeSpecifier)definition.getParent()).getScope() : null; checkForDefinition();
if (definition != null) {
return ((IASTCompositeTypeSpecifier)definition.getParent()).getScope();
}
// fwd-declarations must be backed up from the index
if (typeInIndex != null) {
try {
IScope scope = typeInIndex.getCompositeScope();
if (scope instanceof ICCompositeTypeScope)
return scope;
} catch (DOMException e) {
// index bindings don't throw DOMExeptions.
}
}
return null;
} }
@Override @Override
@ -253,6 +229,28 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
compositeTypeSpec.getName().setBinding( this ); compositeTypeSpec.getName().setBinding( this );
} }
public void addDeclaration(IASTName decl) {
if (decl.getPropertyInParent() != IASTElaboratedTypeSpecifier.TYPE_NAME)
return;
decl.setBinding(this);
if (declarations == null) {
declarations = new IASTName[] { decl };
return;
}
for (int i = 0; i < declarations.length; i++) {
if (declarations[i] == null) {
declarations[i] = decl;
return;
}
}
IASTName tmp[] = new IASTName[declarations.length * 2];
System.arraycopy(declarations, 0, tmp, 0, declarations.length);
tmp[declarations.length] = decl;
declarations = tmp;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IType#isSameType(org.eclipse.cdt.core.dom.ast.IType) * @see org.eclipse.cdt.core.dom.ast.IType#isSameType(org.eclipse.cdt.core.dom.ast.IType)
*/ */

View file

@ -16,7 +16,6 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
@ -93,45 +92,22 @@ import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding; import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer; import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.core.runtime.CoreException;
/** /**
* Collection of methods to find information in an AST. * Collection of methods to find information in an AST.
*/ */
public class CVisitor extends ASTQueries { public class CVisitor extends ASTQueries {
public static class ClearBindingAction extends CASTVisitor {
{
shouldVisitNames = true;
}
@Override
public int visit(IASTName name) {
if (name.getBinding() != null) {
try {
IScope scope = name.resolveBinding().getScope();
if (scope != null)
ASTInternal.removeBinding(scope, name.resolveBinding());
} catch (DOMException e) {
}
name.setBinding(null);
}
return PROCESS_CONTINUE;
}
}
public static class CollectProblemsAction extends CASTVisitor { public static class CollectProblemsAction extends CASTVisitor {
{ {
shouldVisitDeclarations = true; shouldVisitDeclarations = true;
@ -468,12 +444,6 @@ public class CVisitor extends ASTQueries {
private static final String PTRDIFF_T = "ptrdiff_t"; //$NON-NLS-1$ private static final String PTRDIFF_T = "ptrdiff_t"; //$NON-NLS-1$
public static final String EMPTY_STRING = ""; //$NON-NLS-1$ public static final String EMPTY_STRING = ""; //$NON-NLS-1$
public static final char[] EMPTY_CHAR_ARRAY = "".toCharArray(); //$NON-NLS-1$ public static final char[] EMPTY_CHAR_ARRAY = "".toCharArray(); //$NON-NLS-1$
//lookup bits
private static final int COMPLETE = 0;
private static final int CURRENT_SCOPE = 1;
private static final int TAGS = 1 << 1;
private static final int INCLUDE_BLOCK_ITEM = 1 << 2;
private static final int PREFIX_LOOKUP = 1 << 3;
//definition lookup start loc //definition lookup start loc
protected static final int AT_BEGINNING = 1; protected static final int AT_BEGINNING = 1;
@ -484,7 +454,7 @@ public class CVisitor extends ASTQueries {
IASTNode parent = name.getParent(); IASTNode parent = name.getParent();
if (parent instanceof CASTIdExpression) { if (parent instanceof CASTIdExpression) {
binding = resolveBinding(parent, COMPLETE | INCLUDE_BLOCK_ITEM); binding = resolveBinding(parent);
} else if (parent instanceof ICASTTypedefNameSpecifier) { } else if (parent instanceof ICASTTypedefNameSpecifier) {
binding = resolveBinding(parent); binding = resolveBinding(parent);
} else if (parent instanceof IASTFieldReference) { } else if (parent instanceof IASTFieldReference) {
@ -572,16 +542,23 @@ public class CVisitor extends ASTQueries {
} }
private static IBinding createBinding(ICASTElaboratedTypeSpecifier elabTypeSpec) { private static IBinding createBinding(ICASTElaboratedTypeSpecifier elabTypeSpec) {
IASTNode parent = elabTypeSpec.getParent(); IASTNode parent = elabTypeSpec.getParent();
if (parent instanceof IASTDeclaration) {
int bits = TAGS;
if (parent instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)parent).getDeclarators().length == 0) {
bits |= CURRENT_SCOPE;
}
IASTName name = elabTypeSpec.getName(); IASTName name = elabTypeSpec.getName();
IBinding binding = resolveBinding(elabTypeSpec, bits); if (parent instanceof IASTDeclaration) {
IBinding binding= null;
if (parent instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)parent).getDeclarators().length == 0) {
IScope scope= getContainingScope(elabTypeSpec);
try {
binding= scope.getBinding(name, false);
} catch (DOMException e) {
}
} else {
binding= resolveBinding(elabTypeSpec);
}
if (binding != null) { if (binding != null) {
if (binding instanceof CEnumeration) { if (binding instanceof CEnumeration) {
((CEnumeration)binding).addDeclaration(name); ((CEnumeration)binding).addDeclaration(name);
} else if (binding instanceof CStructure) {
((CStructure) binding).addDeclaration(name);
} }
} else { } else {
if (elabTypeSpec.getKind() == IASTElaboratedTypeSpecifier.k_enum) { if (elabTypeSpec.getKind() == IASTElaboratedTypeSpecifier.k_enum) {
@ -598,12 +575,7 @@ public class CVisitor extends ASTQueries {
return binding; return binding;
} else if (parent instanceof IASTTypeId || parent instanceof IASTParameterDeclaration) { } else if (parent instanceof IASTTypeId || parent instanceof IASTParameterDeclaration) {
IASTNode blockItem = getContainingBlockItem(parent); return resolveBinding(elabTypeSpec);
try {
return (IBinding) findBinding(blockItem, elabTypeSpec.getName(), COMPLETE | TAGS);
} catch (DOMException e) {
return null;
}
} }
return null; return null;
} }
@ -848,9 +820,12 @@ public class CVisitor extends ASTQueries {
private static IBinding createBinding(IASTDeclarator declarator, IASTName name) { private static IBinding createBinding(IASTDeclarator declarator, IASTName name) {
IBinding binding = null; IBinding binding = null;
if (declarator instanceof ICASTKnRFunctionDeclarator) { if (declarator instanceof ICASTKnRFunctionDeclarator) {
IASTNode parent = declarator.getParent();
if (CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray())) { if (CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray())) {
binding = resolveBinding(parent, CURRENT_SCOPE); IScope scope= CVisitor.getContainingScope(declarator);
try {
binding = scope.getBinding(name, false);
} catch (DOMException e) {
}
if (binding != null && binding instanceof IIndexBinding == false) { if (binding != null && binding instanceof IIndexBinding == false) {
if (binding instanceof ICInternalFunction) if (binding instanceof ICInternalFunction)
((ICInternalFunction)binding).addDeclarator((ICASTKnRFunctionDeclarator) declarator); ((ICInternalFunction)binding).addDeclarator((ICASTKnRFunctionDeclarator) declarator);
@ -864,13 +839,6 @@ public class CVisitor extends ASTQueries {
if (f instanceof CFunction) { if (f instanceof CFunction) {
binding = ((CFunction) f).resolveParameter(name); binding = ((CFunction) f).resolveParameter(name);
} }
if (declarator.getParent() instanceof IASTFunctionDefinition) {
IScope scope = ((IASTCompoundStatement)((IASTFunctionDefinition)declarator.getParent()).getBody()).getScope();
if (scope != null && binding != null) {
ASTInternal.addName(scope, name);
}
}
} }
} else { } else {
binding = createBinding(declarator); binding = createBinding(declarator);
@ -910,24 +878,17 @@ public class CVisitor extends ASTQueries {
if (parent instanceof IASTParameterDeclaration || parent.getPropertyInParent() == ICASTKnRFunctionDeclarator.FUNCTION_PARAMETER) { if (parent instanceof IASTParameterDeclaration || parent.getPropertyInParent() == ICASTKnRFunctionDeclarator.FUNCTION_PARAMETER) {
IASTDeclarator fdtor = (IASTDeclarator) parent.getParent(); IASTDeclarator fdtor = (IASTDeclarator) parent.getParent();
IASTDeclarator nested= fdtor.getNestedDeclarator(); if (ASTQueries.findTypeRelevantDeclarator(fdtor) instanceof IASTFunctionDeclarator) {
while (nested != null && nested.getPointerOperators().length == 0) { IASTName n= ASTQueries.findInnermostDeclarator(fdtor).getName();
fdtor= nested; IBinding temp = n.resolveBinding();
nested= nested.getNestedDeclarator();
}
IBinding temp = fdtor.getName().resolveBinding();
if (temp != null && temp instanceof CFunction) { if (temp != null && temp instanceof CFunction) {
binding = ((CFunction) temp).resolveParameter(name); binding = ((CFunction) temp).resolveParameter(name);
} else if (temp instanceof IFunction) { } else if (temp instanceof IFunction) {
//problems with the function, still create binding for the parameter //problems with the function, still create binding for the parameter
binding = new CParameter(name); binding = new CParameter(name);
} }
try {
if (scope != null && ASTInternal.getPhysicalNodeOfScope(scope) instanceof IASTTranslationUnit) {
return binding; return binding;
} }
} catch (DOMException e) {
}
} else if (funcDeclarator != null) { } else if (funcDeclarator != null) {
if (binding != null && !(binding instanceof IIndexBinding)) { if (binding != null && !(binding instanceof IIndexBinding)) {
if (binding instanceof IFunction) { if (binding instanceof IFunction) {
@ -971,10 +932,6 @@ public class CVisitor extends ASTQueries {
} }
} }
} }
if (scope != null && binding != null) {
ASTInternal.addName(scope, name);
}
return binding; return binding;
} }
@ -998,37 +955,24 @@ public class CVisitor extends ASTQueries {
} }
} catch (DOMException e2) { } catch (DOMException e2) {
} }
return new CStructure(name);
binding = new CStructure(name);
try {
scope= binding.getScope();
ASTInternal.addName(scope, name);
} catch (DOMException e) {
}
return binding;
} }
protected static IBinding resolveBinding(IASTNode node) { protected static IBinding resolveBinding(IASTNode node) {
return resolveBinding(node, COMPLETE);
}
protected static IBinding resolveBinding(IASTNode node, int bits) {
if (node instanceof IASTFunctionDefinition) { if (node instanceof IASTFunctionDefinition) {
IASTFunctionDefinition functionDef = (IASTFunctionDefinition) node; IASTFunctionDefinition functionDef = (IASTFunctionDefinition) node;
IASTFunctionDeclarator functionDeclartor = functionDef.getDeclarator(); IASTFunctionDeclarator functionDeclartor = functionDef.getDeclarator();
IASTName name = findInnermostDeclarator(functionDeclartor).getName(); IASTName name = findInnermostDeclarator(functionDeclartor).getName();
IASTNode blockItem = getContainingBlockItem(node); IScope scope = getContainingScope(node);
try { try {
return (IBinding) findBinding(blockItem, name, bits); return lookup(scope, name);
} catch (DOMException e) { } catch (DOMException e) {
return null; return null;
} }
} else if (node instanceof IASTIdExpression) { } else if (node instanceof IASTIdExpression) {
IASTNode blockItem = getContainingBlockItem(node); IScope scope = getContainingScope(node);
try { try {
IBinding binding = (IBinding) findBinding(blockItem, ((IASTIdExpression)node).getName(), bits); IBinding binding = lookup(scope, ((IASTIdExpression)node).getName());
if (binding instanceof IType && !(binding instanceof IProblemBinding) ) { if (binding instanceof IType && !(binding instanceof IProblemBinding) ) {
return new ProblemBinding(node, IProblemBinding.SEMANTIC_INVALID_TYPE, binding.getNameCharArray()); return new ProblemBinding(node, IProblemBinding.SEMANTIC_INVALID_TYPE, binding.getNameCharArray());
} }
@ -1037,10 +981,10 @@ public class CVisitor extends ASTQueries {
return null; return null;
} }
} else if (node instanceof ICASTTypedefNameSpecifier) { } else if (node instanceof ICASTTypedefNameSpecifier) {
IASTNode blockItem = getContainingBlockItem(node); IScope scope = getContainingScope(node);
try { try {
IASTName name= ((ICASTTypedefNameSpecifier)node).getName(); IASTName name= ((ICASTTypedefNameSpecifier)node).getName();
IBinding binding = (IBinding) findBinding(blockItem, name, bits); IBinding binding = lookup(scope, name);
if (binding == null) if (binding == null)
return new ProblemBinding(node, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, name.toCharArray()); return new ProblemBinding(node, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, name.toCharArray());
if (binding instanceof IType) if (binding instanceof IType)
@ -1050,16 +994,16 @@ public class CVisitor extends ASTQueries {
return null; return null;
} }
} else if (node instanceof ICASTElaboratedTypeSpecifier) { } else if (node instanceof ICASTElaboratedTypeSpecifier) {
IASTNode blockItem = getContainingBlockItem(node); IScope scope = getContainingScope(node);
try { try {
return (IBinding) findBinding(blockItem, ((ICASTElaboratedTypeSpecifier)node).getName(), bits); return lookup(scope, ((ICASTElaboratedTypeSpecifier)node).getName());
} catch (DOMException e) { } catch (DOMException e) {
return null; return null;
} }
} else if (node instanceof ICASTCompositeTypeSpecifier) { } else if (node instanceof ICASTCompositeTypeSpecifier) {
IASTNode blockItem = getContainingBlockItem(node); IScope scope = getContainingScope(node);
try { try {
return (IBinding) findBinding(blockItem, ((ICASTCompositeTypeSpecifier)node).getName(), bits); return lookup(scope, ((ICASTCompositeTypeSpecifier)node).getName());
} catch (DOMException e) { } catch (DOMException e) {
return null; return null;
} }
@ -1158,20 +1102,44 @@ public class CVisitor extends ASTQueries {
return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope(); return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope();
} }
} }
} else if (node instanceof IASTStatement) } else if (node instanceof IASTStatement) {
return getContainingScope((IASTStatement) node); return getContainingScope((IASTStatement) node);
else if (node instanceof IASTParameterDeclaration) { } else if (node instanceof IASTExpression) {
IASTNode parent = node.getParent();
if (parent instanceof IASTForStatement) {
return ((IASTForStatement)parent).getScope();
}
} else if (node instanceof IASTParameterDeclaration) {
IASTNode parent = node.getParent(); IASTNode parent = node.getParent();
if (parent instanceof IASTStandardFunctionDeclarator) { if (parent instanceof IASTStandardFunctionDeclarator) {
parent = ((IASTDeclarator)parent).getParent(); IASTStandardFunctionDeclarator dtor = (IASTStandardFunctionDeclarator) parent;
if (parent instanceof IASTFunctionDefinition) if (ASTQueries.findTypeRelevantDeclarator(dtor) == dtor) {
return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope(); parent= ASTQueries.findOutermostDeclarator(dtor);
return null; // parameter name in function declarations ASTNodeProperty prop = parent.getPropertyInParent();
if (prop == IASTSimpleDeclaration.DECLARATOR)
return dtor.getFunctionScope();
else if (prop == IASTFunctionDefinition.DECLARATOR)
return ((IASTCompoundStatement) ((IASTFunctionDefinition) parent.getParent()).getBody()).getScope();
} }
} }
else if (node instanceof IASTEnumerator) { } else if (node instanceof IASTEnumerator) {
//put the enumerators in the same scope as the enumeration //put the enumerators in the same scope as the enumeration
node = node.getParent(); node = node.getParent();
} else if (node instanceof IASTName) {
ASTNodeProperty prop = node.getPropertyInParent();
if (prop == IASTLabelStatement.NAME) {
IScope scope= getContainingScope(node.getParent());
//labels have function scope
while (scope != null && !(scope instanceof ICFunctionScope)) {
try {
scope = scope.getParent();
} catch (DOMException e) {
scope = e.getProblem();
break;
}
}
return scope;
}
} }
node = node.getParent(); node = node.getParent();
@ -1186,22 +1154,18 @@ public class CVisitor extends ASTQueries {
IASTCompoundStatement compound = (IASTCompoundStatement) parent; IASTCompoundStatement compound = (IASTCompoundStatement) parent;
scope = compound.getScope(); scope = compound.getScope();
} else if (parent instanceof IASTStatement) { } else if (parent instanceof IASTStatement) {
if (parent instanceof IASTForStatement) {
scope= ((IASTForStatement) parent).getScope();
} else {
scope = getContainingScope((IASTStatement)parent); scope = getContainingScope((IASTStatement)parent);
}
} else if (parent instanceof IASTFunctionDefinition) { } else if (parent instanceof IASTFunctionDefinition) {
IASTFunctionDeclarator fnDeclarator = ((IASTFunctionDefinition) parent).getDeclarator(); return ((IASTFunctionDefinition) parent).getScope();
IBinding function = ASTQueries.findInnermostDeclarator(fnDeclarator).getName().resolveBinding(); } else {
try { return getContainingScope(parent);
if (function instanceof IFunction) {
scope = ((IFunction)function).getFunctionScope();
} else if (function instanceof ProblemBinding) {
return (IScope) function;
}
} catch (DOMException e) {
return e.getProblem();
}
} }
if (statement instanceof IASTGotoStatement || statement instanceof IASTLabelStatement) { if (statement instanceof IASTGotoStatement) {
//labels have function scope //labels have function scope
while (scope != null && !(scope instanceof ICFunctionScope)) { while (scope != null && !(scope instanceof ICFunctionScope)) {
try { try {
@ -1237,202 +1201,82 @@ public class CVisitor extends ASTQueries {
} }
/** /**
* if (bits & PREFIX_LOOKUP) then returns IBinding[] * Lookup for a name starting from the given scope.
* otherwise returns IBinding
*/ */
protected static Object findBinding(IASTNode blockItem, IASTName name, int bits) throws DOMException{ protected static IBinding lookup(IScope scope, IASTName name) throws DOMException{
if (scope == null)
return null;
IIndexFileSet fileSet= IIndexFileSet.EMPTY; IIndexFileSet fileSet= IIndexFileSet.EMPTY;
if (blockItem != null) { IASTTranslationUnit tu= name.getTranslationUnit();
final IASTTranslationUnit tu= blockItem.getTranslationUnit(); if (tu == null && scope instanceof IASTInternalScope) {
tu= ((IASTInternalScope) scope).getPhysicalNode().getTranslationUnit();
}
if (tu != null) { if (tu != null) {
final IIndexFileSet fs= (IIndexFileSet) tu.getAdapter(IIndexFileSet.class); final IIndexFileSet fs= (IIndexFileSet) tu.getAdapter(IIndexFileSet.class);
if (fs != null) { if (fs != null) {
fileSet= fs; fileSet= fs;
} }
} }
}
boolean prefix = (bits & PREFIX_LOOKUP) != 0; while (scope != null) {
@SuppressWarnings("unchecked")
Object binding = prefix ? new ObjectSet(2) : null;
IIndexBinding foundIndexBinding= null;
CharArrayObjectMap prefixMap = prefix ? new CharArrayObjectMap(2) : null;
while (blockItem != null) {
IASTNode parent = blockItem.getParent();
IASTNode[] nodes = null;
IScope scope = null;
if (parent instanceof IASTCompoundStatement) {
IASTCompoundStatement compound = (IASTCompoundStatement) parent;
scope = 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;
if (!prefix) {
nodes = translation.getDeclarations();
scope = translation.getScope();
} else {
// The index will be search later, still we need to look at the declarations found in
// the AST, bug 180883
nodes = translation.getDeclarations();
}
} else if (parent instanceof IASTStandardFunctionDeclarator) {
IASTStandardFunctionDeclarator dtor = (IASTStandardFunctionDeclarator) parent;
nodes = dtor.getParameters();
scope = getContainingScope(blockItem);
} else if (parent instanceof ICASTKnRFunctionDeclarator) {
ICASTKnRFunctionDeclarator dtor = (ICASTKnRFunctionDeclarator) parent;
nodes = dtor.getParameterDeclarations();
scope = getContainingScope(blockItem);
}
boolean typesOnly = (bits & TAGS) != 0;
boolean includeBlockItem = (bits & INCLUDE_BLOCK_ITEM) != 0;
if (prefix)
scope = null;
if (scope != null && ASTInternal.isFullyCached(scope)) {
try { try {
binding = scope.getBinding(name, true, fileSet); if (!(scope instanceof ICCompositeTypeScope)) {
} catch (DOMException e) { IBinding binding = scope.getBinding(name, true, fileSet);
binding = null;
}
if (binding != null) if (binding != null)
return binding; return binding;
} else {
if (!prefix && scope != null && scope.getParent() == null) {
binding= scope.getBinding(name, false, fileSet);
if (binding != null) {
if (binding instanceof IIndexBinding) {
foundIndexBinding= (IIndexBinding) binding;
} }
else {
return binding;
}
}
}
Object result = null;
boolean reachedBlockItem = false;
if (nodes != null) {
int idx = -1;
IASTNode node = nodes.length > 0 ? nodes[++idx] : null;
while (node != null) {
Object candidate = null;
try {
candidate = checkForBinding(scope, node, name, typesOnly, prefixMap);
} catch (DOMException e) {
continue;
}
if (result == null && !reachedBlockItem &&
(includeBlockItem || (node != blockItem)))
{
result = candidate;
}
if (node == blockItem) {
reachedBlockItem = true;
}
if (idx > -1 && ++idx < nodes.length) {
node = nodes[idx];
} else {
node = null;
if (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 = null;
if (parent instanceof IASTCompoundStatement) {
compound = (IASTCompoundStatement) parent;
} else if (parent instanceof IASTFunctionDeclarator) {
IASTNode n = parent.getParent();
while (n instanceof IASTDeclarator)
n = n.getParent();
if (n instanceof IASTFunctionDefinition) {
compound = (IASTCompoundStatement) ((IASTFunctionDefinition)n).getBody();
}
}
if (compound != null) {
nodes = compound.getStatements();
if (nodes.length > 0) {
idx = 0;
node = nodes[0];
}
}
}
}
}
} else {
try {
result = checkForBinding(scope, parent, name, typesOnly, prefixMap);
} catch (DOMException e) { } catch (DOMException e) {
} }
scope= scope.getParent();
} }
if (scope != null) {
return externalBinding(tu, name);
}
/**
* if (bits & PREFIX_LOOKUP) then returns IBinding[]
* otherwise returns IBinding
*/
protected static IBinding[] lookupPrefix(IScope scope, IASTName name) throws DOMException{
if (scope == null)
return null;
IIndexFileSet fileSet= IIndexFileSet.EMPTY;
IASTTranslationUnit tu= name.getTranslationUnit();
if (tu == null && scope instanceof IASTInternalScope) {
tu= ((IASTInternalScope) scope).getPhysicalNode().getTranslationUnit();
}
if (tu != null) {
final IIndexFileSet fs= (IIndexFileSet) tu.getAdapter(IIndexFileSet.class);
if (fs != null) {
fileSet= fs;
}
}
CharArrayObjectMap prefixMap = new CharArrayObjectMap(2);
while (scope != null) {
try { try {
ASTInternal.setFullyCached(scope, true); if (!(scope instanceof ICCompositeTypeScope)) {
IBinding[] bindings= scope.getBindings(name, true, true, fileSet);
for (IBinding b : bindings) {
final char[] n= b.getNameCharArray();
if (!prefixMap.containsKey(n)) {
prefixMap.put(n, b);
}
}
}
} catch (DOMException e) { } catch (DOMException e) {
} }
scope= scope.getParent();
} }
if (result != null) {
if (CVisitor.declaredBefore((IASTName)result, name)) {
return ((IASTName)result).resolveBinding();
}
}
}
if ((bits & CURRENT_SCOPE) == 0)
blockItem = parent;
else
blockItem = null;
if (blockItem instanceof IASTTranslationUnit)
break;
}
if (foundIndexBinding != null) {
return foundIndexBinding;
}
if (prefixMap != null) {
IBinding[] result = null; IBinding[] result = null;
Object[] vals = prefixMap.valueArray(); Object[] vals = prefixMap.valueArray();
for (Object val : vals) { for (Object val : vals) {
result = (IBinding[]) ArrayUtil.append(IBinding.class, result, ((IASTName) val).resolveBinding()); result = (IBinding[]) ArrayUtil.append(IBinding.class, result, val);
} }
return (IBinding[]) ArrayUtil.trim(IBinding.class, result);
IASTTranslationUnit tu = (IASTTranslationUnit)blockItem;
IIndex index = tu.getIndex();
if (index != null) {
try {
IndexFilter filter = IndexFilter.C_DECLARED_OR_IMPLICIT;
IBinding[] bindings= prefix
? index.findBindingsForPrefix(name.toCharArray(), true, filter, null)
: index.findBindings(name.toCharArray(), filter, null);
bindings= fileSet.filterFileLocalBindings(bindings);
result = (IBinding[]) ArrayUtil.addAll(IBinding.class, result, bindings);
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
return ArrayUtil.trim(IBinding.class, result);
}
if (blockItem != null) {
if (binding == null)
return externalBinding((IASTTranslationUnit) blockItem, name);
return binding;
}
return null;
} }
private static IBinding externalBinding(IASTTranslationUnit tu, IASTName name) { private static IBinding externalBinding(IASTTranslationUnit tu, IASTName name) {
@ -1442,7 +1286,7 @@ public class CVisitor extends ASTQueries {
if (parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME) { if (parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME) {
//external function //external function
external = new CExternalFunction(tu, name); external = new CExternalFunction(tu, name);
((CScope)tu.getScope()).addName(name); ASTInternal.addName(tu.getScope(), name);
} }
else { else {
//external variable //external variable
@ -1454,182 +1298,6 @@ public class CVisitor extends ASTQueries {
return external; return external;
} }
private static IASTName checkForBinding(IScope scope, IASTDeclSpecifier declSpec, IASTName name, boolean typesOnly, CharArrayObjectMap prefixMap) throws DOMException{
IASTName tempName = null;
IASTName resultName = null;
char[] n = name.toCharArray();
if (declSpec instanceof ICASTElaboratedTypeSpecifier) {
tempName = ((ICASTElaboratedTypeSpecifier)declSpec).getName();
// Don't include the query name in the results
if (tempName == name) {
return null;
}
if (scope != null)
ASTInternal.addName(scope, tempName);
if (typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
} else if (declSpec instanceof ICASTCompositeTypeSpecifier) {
tempName = ((ICASTCompositeTypeSpecifier)declSpec).getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
//also have to check for any nested structs
IASTDeclaration[] nested = ((ICASTCompositeTypeSpecifier)declSpec).getMembers();
for (IASTDeclaration element : nested) {
if (element instanceof IASTSimpleDeclaration) {
IASTDeclSpecifier d = ((IASTSimpleDeclaration)element).getDeclSpecifier();
if (d instanceof ICASTCompositeTypeSpecifier || d instanceof IASTEnumerationSpecifier) {
Object obj = checkForBinding(scope, d, name, typesOnly, prefixMap);
if (prefixMap == null && resultName == null) {
resultName = (IASTName) obj;
}
}
}
}
} else if (declSpec instanceof ICASTEnumerationSpecifier) {
ICASTEnumerationSpecifier enumeration = (ICASTEnumerationSpecifier) declSpec;
tempName = enumeration.getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
//check enumerators
IASTEnumerator[] list = ((ICASTEnumerationSpecifier) declSpec).getEnumerators();
for (IASTEnumerator enumerator : list) {
if (enumerator == null) break;
tempName = enumerator.getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (!typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
}
}
return resultName;
}
private static Object collectResult(IASTName candidate, char[] name, CharArrayObjectMap prefixMap) {
char[] c = candidate.toCharArray();
if (prefixMap == null && CharArrayUtils.equals(c, name)) {
return candidate;
} else if (prefixMap != null && CharArrayUtils.equals(c, 0, name.length, name, true) && !prefixMap.containsKey(c)) {
prefixMap.put(c, candidate);
}
return prefixMap;
}
private static IASTName checkForBinding(IScope scope, IASTParameterDeclaration paramDecl, IASTName name, boolean typesOnly, CharArrayObjectMap prefixMap) throws DOMException{
if (paramDecl == null) return null;
IASTDeclarator dtor = paramDecl.getDeclarator();
while (dtor.getNestedDeclarator() != null) {
dtor = dtor.getNestedDeclarator();
}
IASTName tempName = dtor.getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (!typesOnly) {
char[] c = tempName.toCharArray();
char[] n = name.toCharArray();
if (prefixMap == null && CharArrayUtils.equals(c, n))
return tempName;
else if (prefixMap != null && CharArrayUtils.equals(c, 0, n.length, n, true) && !prefixMap.containsKey(c))
prefixMap.put(c, tempName);
} else {
return checkForBinding(scope, paramDecl.getDeclSpecifier(), name, typesOnly, prefixMap);
}
return null;
}
/**
* if not a prefix lookup, returns IASTName
* if doing prefix lookup, results are in prefixMap, returns null
*/
private static IASTName checkForBinding(IScope scope, IASTNode node, IASTName name, boolean typesOnly, CharArrayObjectMap prefixMap) throws DOMException{
if (node instanceof IASTDeclaration) {
return checkForBinding(scope, (IASTDeclaration) node, name, typesOnly, prefixMap);
} else if (node instanceof IASTParameterDeclaration) {
return checkForBinding(scope, (IASTParameterDeclaration) node, name, typesOnly, prefixMap);
} else if (node instanceof IASTDeclarationStatement) {
return checkForBinding(scope, ((IASTDeclarationStatement)node).getDeclaration(), name, typesOnly, prefixMap);
} else if (node instanceof IASTForStatement) {
IASTForStatement forStatement = (IASTForStatement) node;
if (forStatement.getInitializerStatement() instanceof IASTDeclarationStatement) {
return checkForBinding(scope, ((IASTDeclarationStatement)forStatement.getInitializerStatement()).getDeclaration(), name, typesOnly, prefixMap);
}
}
return null;
}
private static IASTName checkForBinding(IScope scope, IASTDeclaration declaration, IASTName name, boolean typesOnly, CharArrayObjectMap prefixMap) throws DOMException{
char[] n = name.toCharArray();
IASTName tempName = null;
IASTName resultName = null;
if (declaration instanceof IASTSimpleDeclaration) {
IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
IASTDeclarator[] declarators = simpleDeclaration.getDeclarators();
for (IASTDeclarator declarator : declarators) {
declarator= ASTQueries.findInnermostDeclarator(declarator);
tempName = declarator.getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (!typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
}
tempName = checkForBinding(scope, simpleDeclaration.getDeclSpecifier(), name, typesOnly, prefixMap);
if (prefixMap == null && tempName != null) {
resultName = tempName;
}
} else if (!typesOnly && declaration instanceof IASTFunctionDefinition) {
IASTFunctionDefinition functionDef = (IASTFunctionDefinition) declaration;
IASTDeclarator dtor = ASTQueries.findInnermostDeclarator(functionDef.getDeclarator());
tempName = dtor.getName();
if (scope != null)
ASTInternal.addName(scope, tempName);
if (!typesOnly) {
if (prefixMap != null)
prefixMap = (CharArrayObjectMap) collectResult(tempName, n, prefixMap);
else if (collectResult(tempName, n, prefixMap) != null)
resultName = tempName;
}
tempName = checkForBinding(scope, functionDef.getDeclSpecifier(), name, typesOnly, prefixMap);
if (prefixMap == null && tempName != null) {
resultName = tempName;
}
}
return resultName;
}
protected static IASTDeclarator findDefinition(IASTDeclarator declarator, int beginAtLoc) { protected static IASTDeclarator findDefinition(IASTDeclarator declarator, int beginAtLoc) {
return (IASTDeclarator) findDefinition(declarator, declarator.getName().toCharArray(), beginAtLoc); return (IASTDeclarator) findDefinition(declarator, declarator.getName().toCharArray(), beginAtLoc);
} }
@ -1702,10 +1370,6 @@ public class CVisitor extends ASTQueries {
return null; return null;
} }
public static void clearBindings(IASTTranslationUnit tu) {
tu.accept(new ClearBindingAction());
}
/** /**
* Create an IType for an IASTDeclarator. * Create an IType for an IASTDeclarator.
* *
@ -1978,20 +1642,6 @@ public class CVisitor extends ASTQueries {
return action.getReferences(); return action.getReferences();
} }
public static IBinding findTypeBinding(IASTNode startingPoint, IASTName name) throws DOMException {
if (startingPoint instanceof IASTTranslationUnit) {
IASTDeclaration[] declarations = ((IASTTranslationUnit)startingPoint).getDeclarations();
if (declarations.length > 0)
return (IBinding) findBinding(declarations[declarations.length - 1], name, COMPLETE | INCLUDE_BLOCK_ITEM );
}
if (startingPoint instanceof IASTCompoundStatement) {
IASTStatement[] statements = ((IASTCompoundStatement)startingPoint).getStatements();
if (statements.length > 0)
return (IBinding) findBinding(statements[statements.length - 1], name, COMPLETE | INCLUDE_BLOCK_ITEM);
}
return null;
}
public static IBinding[] findBindingsForContentAssist(IASTName name, boolean isPrefix) { public static IBinding[] findBindingsForContentAssist(IASTName name, boolean isPrefix) {
ASTNodeProperty prop = name.getPropertyInParent(); ASTNodeProperty prop = name.getPropertyInParent();
@ -2000,36 +1650,25 @@ public class CVisitor extends ASTQueries {
if (prop == IASTFieldReference.FIELD_NAME) { if (prop == IASTFieldReference.FIELD_NAME) {
result = (IBinding[]) findBinding((IASTFieldReference) name.getParent(), isPrefix); result = (IBinding[]) findBinding((IASTFieldReference) name.getParent(), isPrefix);
} else { } else {
int bits = isPrefix ? PREFIX_LOOKUP : COMPLETE; IScope scope= getContainingScope(name);
if (prop == IASTElaboratedTypeSpecifier.TYPE_NAME) {
bits |= TAGS;
} else if (prop == IASTIdExpression.ID_NAME) {
bits |= INCLUDE_BLOCK_ITEM;
}
IASTNode blockItem = getContainingBlockItem(name);
try { try {
result = isPrefix ? (IBinding[]) findBinding(blockItem, name, bits) : if (isPrefix) {
new IBinding[] { (IBinding) findBinding(blockItem, name, bits) }; result = lookupPrefix(scope, name);
} else {
result = new IBinding[] { lookup(scope, name) };
}
} catch (DOMException e) { } catch (DOMException e) {
} }
} }
return (IBinding[]) ArrayUtil.trim(IBinding.class, result); return (IBinding[]) ArrayUtil.trim(IBinding.class, result);
} }
public static IBinding[] findBindings(IScope scope, String name, boolean prefixLookup) throws DOMException { public static IBinding[] findBindings(IScope scope, String name) throws DOMException {
IASTNode node = ASTInternal.getPhysicalNodeOfScope(scope);
if (node instanceof IASTFunctionDefinition)
node = ((IASTFunctionDefinition)node).getBody();
CASTName astName = new CASTName(name.toCharArray()); CASTName astName = new CASTName(name.toCharArray());
astName.setParent(node);
//normal names //normal names
astName.setPropertyInParent(STRING_LOOKUP_PROPERTY); astName.setPropertyInParent(STRING_LOOKUP_PROPERTY);
int flags = prefixLookup ? COMPLETE | PREFIX_LOOKUP : COMPLETE; Object o1 = lookup(scope, astName);
Object o1 = findBinding(astName, astName, flags);
IBinding[] b1 = null; IBinding[] b1 = null;
if (o1 instanceof IBinding) { if (o1 instanceof IBinding) {
@ -2040,8 +1679,7 @@ public class CVisitor extends ASTQueries {
//structure names //structure names
astName.setPropertyInParent(STRING_LOOKUP_TAGS_PROPERTY); astName.setPropertyInParent(STRING_LOOKUP_TAGS_PROPERTY);
flags = prefixLookup ? COMPLETE | TAGS | PREFIX_LOOKUP : COMPLETE | TAGS; Object o2 = lookup(scope, astName);
Object o2 = findBinding(astName, astName, flags);
IBinding[] b2 = null; IBinding[] b2 = null;
if (o2 instanceof IBinding) { if (o2 instanceof IBinding) {
@ -2057,19 +1695,12 @@ public class CVisitor extends ASTQueries {
if (scope instanceof ICFunctionScope) { if (scope instanceof ICFunctionScope) {
ILabel[] labels = ((CFunctionScope)scope).getLabels(); ILabel[] labels = ((CFunctionScope)scope).getLabels();
for (ILabel label : labels) { for (ILabel label : labels) {
if (prefixLookup) {
if (CharArrayUtils.equals(label.getNameCharArray(),
0, n.length, n, true)) {
b3.add(label);
}
} else {
if (CharArrayUtils.equals(label.getNameCharArray(), n)) { if (CharArrayUtils.equals(label.getNameCharArray(), n)) {
b3.add(label); b3.add(label);
break; break;
} }
} }
} break;
if (!prefixLookup) break;
} }
scope = scope.getParent(); scope = scope.getParent();
} while (scope != null); } while (scope != null);

View file

@ -304,6 +304,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
IASTDeclSpecifier declSpec= null; IASTDeclSpecifier declSpec= null;
IASTDeclarator dtor= null; IASTDeclarator dtor= null;
IASTDeclSpecifier altDeclSpec= null;
IASTDeclarator altDeclarator= null;
IToken markBeforDtor= null; IToken markBeforDtor= null;
try { try {
declSpec = declSpecifierSeq(declOption); declSpec = declSpecifierSeq(declOption);
@ -341,6 +343,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
if (e.altSpec != null) { if (e.altSpec != null) {
declSpec= e.altSpec; declSpec= e.altSpec;
dtor= e.altDeclarator; dtor= e.altDeclarator;
altDeclSpec= e.declSpec;
altDeclarator= e.declarator;
} else { } else {
declSpec = e.declSpec; declSpec = e.declSpec;
dtor= e.declarator; dtor= e.declarator;
@ -411,6 +415,9 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
simpleDeclaration.addDeclarator(declarator); simpleDeclaration.addDeclarator(declarator);
((ASTNode) simpleDeclaration).setOffsetAndLength(firstOffset, endOffset-firstOffset); ((ASTNode) simpleDeclaration).setOffsetAndLength(firstOffset, endOffset-firstOffset);
if ( altDeclSpec != null && altDeclarator != null) {
simpleDeclaration= new CASTAmbiguousSimpleDeclaration(simpleDeclaration, altDeclSpec, altDeclarator);
}
if (insertSemi) { if (insertSemi) {
IASTProblem problem= createProblem(IProblem.SYNTAX_ERROR, endOffset, 0); IASTProblem problem= createProblem(IProblem.SYNTAX_ERROR, endOffset, 0);
@ -425,7 +432,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
throwBacktrack(firstOffset, LA(1).getEndOffset()); throwBacktrack(firstOffset, LA(1).getEndOffset());
final IASTDeclarator outerDtor= declarators[0]; final IASTDeclarator outerDtor= declarators[0];
final IASTDeclarator fdtor= CVisitor.findTypeRelevantDeclarator(outerDtor); final IASTDeclarator fdtor= ASTQueries.findTypeRelevantDeclarator(outerDtor);
if (fdtor instanceof IASTFunctionDeclarator == false) if (fdtor instanceof IASTFunctionDeclarator == false)
throwBacktrack(firstOffset, LA(1).getEndOffset() - firstOffset); throwBacktrack(firstOffset, LA(1).getEndOffset() - firstOffset);
@ -1147,7 +1154,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
case IToken.tLBRACE: case IToken.tLBRACE:
if (option == DeclarationOptions.GLOBAL || option == DeclarationOptions.C_MEMBER if (option == DeclarationOptions.GLOBAL || option == DeclarationOptions.C_MEMBER
|| option == DeclarationOptions.FUNCTION_STYLE_ASM) { || option == DeclarationOptions.FUNCTION_STYLE_ASM) {
if (CVisitor.findTypeRelevantDeclarator(dtor) instanceof IASTFunctionDeclarator) { if (ASTQueries.findTypeRelevantDeclarator(dtor) instanceof IASTFunctionDeclarator) {
return true; return true;
} }
} }
@ -1392,8 +1399,6 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
// try abstract declarator // try abstract declarator
if (!option.fAllowAbstract) { if (!option.fAllowAbstract) {
// bit-fields may be abstract
if (!option.fAllowBitField || LT(1) != IToken.tCOLON)
throwBacktrack(LA(1)); throwBacktrack(LA(1));
} }
return declarator(pointerOps, nodeFactory.newName(), null, startingOffset, endOffset, option); return declarator(pointerOps, nodeFactory.newName(), null, startingOffset, endOffset, option);
@ -1465,7 +1470,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
} }
private boolean isAbstract(IASTName declaratorName, IASTDeclarator nestedDeclarator) { private boolean isAbstract(IASTName declaratorName, IASTDeclarator nestedDeclarator) {
nestedDeclarator= CVisitor.findInnermostDeclarator(nestedDeclarator); nestedDeclarator= ASTQueries.findInnermostDeclarator(nestedDeclarator);
if (nestedDeclarator != null) { if (nestedDeclarator != null) {
declaratorName= nestedDeclarator.getName(); declaratorName= nestedDeclarator.getName();
} }

View file

@ -1,22 +0,0 @@
/*******************************************************************************
* Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
public abstract class CPPASTAmbiguity extends ASTAmbiguousNode {
@Override
protected IScope getAffectedScope() {
return null;
}
}

View file

@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
@ -26,7 +27,7 @@ import org.eclipse.core.runtime.Assert;
* <br> * <br>
* Example: void f(int (D)); // is D a type? * Example: void f(int (D)); // is D a type?
*/ */
public class CPPASTAmbiguousDeclarator extends CPPASTAmbiguity implements IASTAmbiguousDeclarator { public class CPPASTAmbiguousDeclarator extends ASTAmbiguousNode implements IASTAmbiguousDeclarator {
private IASTDeclarator[] dtors = new IASTDeclarator[2]; private IASTDeclarator[] dtors = new IASTDeclarator[2];
private int dtorPos=-1; private int dtorPos=-1;

View file

@ -13,24 +13,18 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousExpression; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
public class CPPASTAmbiguousExpression extends CPPASTAmbiguity implements public class CPPASTAmbiguousExpression extends ASTAmbiguousNode implements
IASTAmbiguousExpression { IASTAmbiguousExpression {
private IASTExpression [] exp = new IASTExpression[2]; private IASTExpression [] exp = new IASTExpression[2];
private int expPos=-1; private int expPos=-1;
@Override
public IScope getAffectedScope() {
// an expression does not introduce names to a parent scope.
return null;
}
public CPPASTAmbiguousExpression(IASTExpression... expressions) { public CPPASTAmbiguousExpression(IASTExpression... expressions) {
for(IASTExpression e : expressions) for(IASTExpression e : expressions)
addExpression(e); addExpression(e);

View file

@ -15,10 +15,11 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
public class CPPASTAmbiguousStatement extends CPPASTAmbiguity implements public class CPPASTAmbiguousStatement extends ASTAmbiguousNode implements
IASTAmbiguousStatement { IASTAmbiguousStatement {
private IASTStatement [] stmts = new IASTStatement[2]; private IASTStatement [] stmts = new IASTStatement[2];

View file

@ -18,15 +18,15 @@ import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAmbiguousTemplateArgument; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAmbiguousTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.parser.ParserMessages; import org.eclipse.cdt.internal.core.parser.ParserMessages;
/** /**
* Ambiguity node for deciding between type-id and id-expression in a template argument. * Ambiguity node for deciding between type-id and id-expression in a template argument.
*/ */
public class CPPASTAmbiguousTemplateArgument extends CPPASTAmbiguity implements ICPPASTAmbiguousTemplateArgument { public class CPPASTAmbiguousTemplateArgument extends ASTAmbiguousNode implements ICPPASTAmbiguousTemplateArgument {
private List<IASTNode> fNodes; private List<IASTNode> fNodes;
@ -54,12 +54,6 @@ public class CPPASTAmbiguousTemplateArgument extends CPPASTAmbiguity implements
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
protected IScope getAffectedScope() {
// a template argument does not introduce names to a parent scope.
return null;
}
@Override @Override
public IASTNode[] getNodes() { public IASTNode[] getNodes() {
return fNodes.toArray(new IASTNode[fNodes.size()]); return fNodes.toArray(new IASTNode[fNodes.size()]);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -203,7 +203,7 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
} }
public void checkForDefinition() { public void checkForDefinition() {
if (!checked) { if (!checked && definition == null) {
FindDefinitionAction action = new FindDefinitionAction(); FindDefinitionAction action = new FindDefinitionAction();
IASTNode node = CPPVisitor.getContainingBlockItem(getPhysicalNode()).getParent(); IASTNode node = CPPVisitor.getContainingBlockItem(getPhysicalNode()).getParent();
@ -225,9 +225,8 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
} }
} }
} }
checked = true;
} }
return; checked = true;
} }
public ICPPASTCompositeTypeSpecifier getCompositeTypeSpecifier() { public ICPPASTCompositeTypeSpecifier getCompositeTypeSpecifier() {
@ -282,9 +281,7 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
} }
public ICPPClassScope getCompositeScope() { public ICPPClassScope getCompositeScope() {
if (definition == null) {
checkForDefinition(); checkForDefinition();
}
if (definition != null) { if (definition != null) {
return getCompositeTypeSpecifier().getScope(); return getCompositeTypeSpecifier().getScope();
} }

View file

@ -29,11 +29,4 @@ public interface ICPPASTInternalScope extends IASTInternalScope, ICPPScope {
*/ */
public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup,
IIndexFileSet acceptLocalBindings, boolean checkPointOfDecl) throws DOMException; IIndexFileSet acceptLocalBindings, boolean checkPointOfDecl) throws DOMException;
/**
* Can be called during ambiguity resolution to populate a scope without considering
* the ambiguous branches. The rest of the names has to be cached one by one after
* the ambiguities have been resolved.
*/
public void populateCache();
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2008 QNX Software Systems and others. * Copyright (c) 2006, 2009 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -10,7 +10,6 @@
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Andrew Ferguson (Symbian) * Andrew Ferguson (Symbian)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.c; package org.eclipse.cdt.internal.core.pdom.dom.c;
import java.util.ArrayList; import java.util.ArrayList;
@ -43,7 +42,6 @@ import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage; import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode; import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
@ -113,7 +111,7 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
@Override @Override
public Object clone() { public Object clone() {
throw new PDOMNotImplementedError(); throw new UnsupportedOperationException();
} }
public int getKey() throws DOMException { public int getKey() throws DOMException {
@ -226,8 +224,8 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
return result; return result;
} }
public IScope getCompositeScope() throws DOMException { public IScope getCompositeScope() {
throw new PDOMNotImplementedError(); return this;
} }
public boolean isSameType(IType type) { public boolean isSameType(IType type) {
@ -274,21 +272,28 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
} }
public IBinding getBinding(char[] name) throws DOMException { public IBinding getBinding(char[] name) throws DOMException {
fail(); return null; return findField(new String(name));
} }
@Override @Override
public IBinding getBinding(IASTName name, boolean resolve, IIndexFileSet fileSet) throws DOMException { public IBinding getBinding(IASTName name, boolean resolve, IIndexFileSet fileSet) throws DOMException {
fail(); return null; return getBinding(name.toCharArray());
} }
@Override @Override
public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet) throws DOMException { public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet) throws DOMException {
fail(); return null; return getBindings(name.toCharArray());
} }
public IBinding[] find(String name) throws DOMException { public IBinding[] find(String name) throws DOMException {
fail(); return null; return getBindings(name.toCharArray());
}
private IBinding[] getBindings(char[] name) throws DOMException {
IBinding b= getBinding(name);
if (b == null)
return IBinding.EMPTY_BINDING_ARRAY;
return new IBinding[]{b};
} }
public IIndexBinding getScopeBinding() { public IIndexBinding getScopeBinding() {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others. * Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -39,9 +39,6 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
"xOtherClass", "xOtherClass",
"AStruct", "AStruct",
"XStruct", "XStruct",
"__FUNCTION__ : const char *",
"__PRETTY_FUNCTION__ : const char *",
"__func__ : const char *",
"aNamespace", "aNamespace",
"xNamespace", "xNamespace",
"anEnumeration", "anEnumeration",

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 QNX Software Systems and others. * Copyright (c) 2007, 2009 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -66,6 +66,12 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.core.dom.parser.GCCBuiltinSymbolProvider.CBuiltinParameter;
import org.eclipse.cdt.internal.core.dom.parser.GCCBuiltinSymbolProvider.CPPBuiltinParameter;
import org.eclipse.cdt.internal.core.dom.parser.c.CBuiltinVariable;
import org.eclipse.cdt.internal.core.dom.parser.c.CImplicitFunction;
import org.eclipse.cdt.internal.core.dom.parser.c.CImplicitTypedef;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBuiltinVariable;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitFunction; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitMethod; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitTypedef; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitTypedef;
@ -263,7 +269,14 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
List<ICompletionProposal> proposals) { List<ICompletionProposal> proposals) {
if ((binding instanceof CPPImplicitFunction if ((binding instanceof CPPImplicitFunction
|| binding instanceof CPPImplicitFunctionTemplate || binding instanceof CPPImplicitTypedef) || binding instanceof CPPImplicitFunctionTemplate
|| binding instanceof CPPImplicitTypedef
|| binding instanceof CPPBuiltinVariable
|| binding instanceof CPPBuiltinParameter
|| binding instanceof CImplicitFunction
|| binding instanceof CImplicitTypedef
|| binding instanceof CBuiltinVariable
|| binding instanceof CBuiltinParameter)
&& !(binding instanceof CPPImplicitMethod)) { && !(binding instanceof CPPImplicitMethod)) {
return; return;
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others. * Copyright (c) 2006, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -90,20 +90,7 @@ public class C99Scope implements IC99Scope, IASTInternalScope {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public void flushCache() { public void populateCache() {
}
public boolean isFullyCached() {
return true;
}
public void removeBinding(IBinding binding) {
}
public void setFullyCached(boolean b) {
} }
public IBinding getBinding(IASTName name, boolean resolve, public IBinding getBinding(IASTName name, boolean resolve,