1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Applied patch for Andrew Niefer.

CPPSemantics#findTypeBinding() will help us resolve ambiguities.
This commit is contained in:
John Camelon 2005-02-14 20:20:39 +00:00
parent b9742041fa
commit 8564247303
2 changed files with 1548 additions and 1402 deletions

View file

@ -13,6 +13,7 @@
*/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@ -26,6 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
@ -45,6 +47,7 @@ import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
@ -56,6 +59,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
/**
@ -68,7 +72,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { } a;"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compTypeSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
IASTName name_A = compTypeSpec.getName();
@ -89,7 +94,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTElaboratedTypeSpecifier elabSpec = (IASTElaboratedTypeSpecifier) decl
.getDeclSpecifier();
@ -112,7 +118,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A {}; A a;"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -140,7 +147,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { int f; };"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -162,7 +170,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { int f(); };"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -186,7 +195,8 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" void A::f() { } \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -227,7 +237,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -283,7 +294,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
IASTName name_A1 = comp.getName();
@ -303,8 +315,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTFunctionDefinition def = (IASTFunctionDefinition) tu
.getDeclarations()[2];
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) def.getDeclarator()
.getName();
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) def
.getDeclarator().getName();
IASTName name_B2 = name_f2.getNames()[0];
IASTName name_f3 = name_f2.getNames()[1];
@ -367,7 +379,8 @@ public class AST2CPPTests extends AST2BaseTest {
IVariable a = (IVariable) collector.getName(1).resolveBinding();
ICPPNamespace B = (ICPPNamespace) collector.getName(2).resolveBinding();
ICPPNamespace C = (ICPPNamespace) collector.getName(4).resolveBinding();
ICPPNamespace BC = (ICPPNamespace) collector.getName(6).resolveBinding();
ICPPNamespace BC = (ICPPNamespace) collector.getName(6)
.resolveBinding();
IFunction f = (IFunction) collector.getName(9).resolveBinding();
assertInstances(collector, A, 3);
assertInstances(collector, a, 3);
@ -416,9 +429,11 @@ public class AST2CPPTests extends AST2BaseTest {
CPPVisitor.visitTranslationUnit(tu, collector);
assertEquals(collector.size(), 9);
ICompositeType A = (ICompositeType) collector.getName(0).resolveBinding();
ICompositeType A = (ICompositeType) collector.getName(0)
.resolveBinding();
ICPPMethod f = (ICPPMethod) collector.getName(1).resolveBinding();
ICompositeType B = (ICompositeType) collector.getName(2).resolveBinding();
ICompositeType B = (ICompositeType) collector.getName(2)
.resolveBinding();
IVariable b = (IVariable) collector.getName(7).resolveBinding();
IVariable B2 = (IVariable) collector.getName(8).resolveBinding();
@ -488,8 +503,10 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2)
.resolveBinding();
IVariable a = (IVariable) collector.getName(4).resolveBinding();
assertNotNull(a);
@ -511,8 +528,10 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2)
.resolveBinding();
IVariable a = (IVariable) collector.getName(3).resolveBinding();
assertNotNull(a);
@ -535,7 +554,8 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
IVariable a = (IVariable) collector.getName(2).resolveBinding();
ICPPField i = (ICPPField) collector.getName(4).resolveBinding();
@ -674,7 +694,8 @@ public class AST2CPPTests extends AST2BaseTest {
IPointerType pt = (IPointerType) pf.getType();
assertTrue(pt.getType() instanceof IFunctionType);
tu = parse("struct A; int (*pfi)( int, struct A * );", ParserLanguage.CPP); //$NON-NLS-1$
tu = parse(
"struct A; int (*pfi)( int, struct A * );", ParserLanguage.CPP); //$NON-NLS-1$
collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A = (ICPPClassType) collector.getName(0).resolveBinding();
@ -698,7 +719,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTElaboratedTypeSpecifier elabSpec = (IASTElaboratedTypeSpecifier) decl
.getDeclSpecifier();
ICompositeType A = (ICompositeType) elabSpec.getName().resolveBinding();
@ -708,12 +730,13 @@ public class AST2CPPTests extends AST2BaseTest {
.resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[2];
IVariable g = (IVariable) decl.getDeclarators()[0].getNestedDeclarator()
.getName().resolveBinding();
IVariable g = (IVariable) decl.getDeclarators()[0]
.getNestedDeclarator().getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[3];
IVariable h = (IVariable) decl.getDeclarators()[0].getNestedDeclarator()
.getNestedDeclarator().getName().resolveBinding();
IVariable h = (IVariable) decl.getDeclarators()[0]
.getNestedDeclarator().getNestedDeclarator().getName()
.resolveBinding();
IFunctionType t_f = f.getType();
IType t_f_return = t_f.getReturnType();
@ -739,7 +762,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertSame(((IPointerType) t_g_func_p1).getType(), A);
//h is a pointer to a function that returns a pointer to a function
//the returned pointer to function returns void and takes 1 parameter int
//the returned pointer to function returns void and takes 1 parameter
// int
// the *h function takes 1 parameter struct A**
IType t_h = h.getType();
assertTrue(t_h instanceof IPointerType);
@ -949,7 +973,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
ICPPClassType A = (ICPPClassType) compSpec.getName().resolveBinding();
@ -975,7 +1000,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
ICPPClassType A = (ICPPClassType) compSpec.getName().resolveBinding();
@ -1014,8 +1040,11 @@ public class AST2CPPTests extends AST2BaseTest {
}
// public void testBug84250() throws Exception {
// assertTrue(((IASTDeclarationStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) parse(
// "void f() { int (*p) [2]; }", ParserLanguage.CPP).getDeclarations()[0]).getBody()).getStatements()[0]).getDeclaration() instanceof IASTSimpleDeclaration); //$NON-NLS-1$
// assertTrue(((IASTDeclarationStatement) ((IASTCompoundStatement)
// ((IASTFunctionDefinition) parse(
// "void f() { int (*p) [2]; }",
// ParserLanguage.CPP).getDeclarations()[0]).getBody()).getStatements()[0]).getDeclaration()
// instanceof IASTSimpleDeclaration); //$NON-NLS-1$
// }
public void testBug84250() throws Exception {
@ -1070,7 +1099,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals(col.size(), 7);
ICompositeType s_ref = (ICompositeType) col.getName(4).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(0).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(0)
.resolveBinding();
assertSame(s_ref, s_decl);
}
@ -1141,7 +1171,8 @@ public class AST2CPPTests extends AST2BaseTest {
ICPPField n = (ICPPField) col.getName(1).resolveBinding();
IBinding Aref = col.getName(5).resolveBinding();
IBinding nref = col.getName(6).resolveBinding();
IProblemBinding prob = (IProblemBinding) col.getName(7).resolveBinding();
IProblemBinding prob = (IProblemBinding) col.getName(7)
.resolveBinding();
assertSame(A, Aref);
assertSame(n, nref);
@ -1152,7 +1183,8 @@ public class AST2CPPTests extends AST2BaseTest {
String code = "int x = ::ABC::DEF::ghi;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTSimpleDeclaration x = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTInitializerExpression e = (IASTInitializerExpression) x.getDeclarators()[0].getInitializer();
IASTInitializerExpression e = (IASTInitializerExpression) x
.getDeclarators()[0].getInitializer();
IASTIdExpression id = (IASTIdExpression) e.getExpression();
ICPPASTQualifiedName name = (ICPPASTQualifiedName) id.getName();
assertTrue(name.isFullyQualified());
@ -1164,10 +1196,14 @@ public class AST2CPPTests extends AST2BaseTest {
public void testBug84679() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("namespace Y { void f(float); } \n"); //$NON-NLS-1$
buffer.append("namespace A { using namespace Y; f(int); } \n"); //$NON-NLS-1$
buffer.append("namespace B { void f(char); } \n"); //$NON-NLS-1$
buffer.append("namespace AB { using namespace A; using namespace B; } \n"); //$NON-NLS-1$
buffer
.append("namespace Y { void f(float); } \n"); //$NON-NLS-1$
buffer
.append("namespace A { using namespace Y; f(int); } \n"); //$NON-NLS-1$
buffer
.append("namespace B { void f(char); } \n"); //$NON-NLS-1$
buffer
.append("namespace AB { using namespace A; using namespace B; } \n"); //$NON-NLS-1$
buffer.append("void h(){ \n"); //$NON-NLS-1$
buffer.append(" AB::f(1); \n"); //$NON-NLS-1$
buffer.append(" AB::f(’c’); \n"); //$NON-NLS-1$
@ -1234,6 +1270,7 @@ public class AST2CPPTests extends AST2BaseTest {
IVariable a2 = (IVariable) col.getName(10).resolveBinding();
assertSame(a1, a2);
}
public void testBug84705() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("struct C { \n"); //$NON-NLS-1$
@ -1280,11 +1317,15 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
ICPPClassType A = (ICPPClassType) ((IASTCompositeTypeSpecifier)decl.getDeclSpecifier()).getName().resolveBinding();
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
ICPPClassType A = (ICPPClassType) ((IASTCompositeTypeSpecifier) decl
.getDeclSpecifier()).getName().resolveBinding();
IASTFunctionDefinition def = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTExpressionStatement expStatement = (IASTExpressionStatement) ((IASTCompoundStatement)def.getBody()).getStatements()[0];
IASTFunctionDefinition def = (IASTFunctionDefinition) tu
.getDeclarations()[1];
IASTExpressionStatement expStatement = (IASTExpressionStatement) ((IASTCompoundStatement) def
.getBody()).getStatements()[0];
assertTrue(expStatement.getExpression() instanceof IASTLiteralExpression);
IType type = CPPVisitor.getExpressionType(expStatement.getExpression());
@ -1292,8 +1333,10 @@ public class AST2CPPTests extends AST2BaseTest {
assertSame(((IPointerType) type).getType(), A);
def = (IASTFunctionDefinition) tu.getDeclarations()[2];
expStatement = (IASTExpressionStatement) ((IASTCompoundStatement)def.getBody()).getStatements()[0];
IASTUnaryExpression ue = (IASTUnaryExpression) expStatement.getExpression();
expStatement = (IASTExpressionStatement) ((IASTCompoundStatement) def
.getBody()).getStatements()[0];
IASTUnaryExpression ue = (IASTUnaryExpression) expStatement
.getExpression();
type = CPPVisitor.getExpressionType(ue);
//when 84749 is fixed, remove this assert and uncomment below.
@ -1308,7 +1351,8 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
ICPPConstructor T = (ICPPConstructor) col.getName(1).resolveBinding();
assertTrue( CharArrayUtils.equals( T.getNameCharArray(), "T".toCharArray() ) ) ; //$NON-NLS-1$
assertTrue(CharArrayUtils.equals(T.getNameCharArray(),
"T".toCharArray())); //$NON-NLS-1$
assertEquals(T.getName(), "T"); //$NON-NLS-1$
}
@ -1398,7 +1442,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances(col, i2, 4);
assertInstances(col, j, 2);
IProblemBinding problem = (IProblemBinding) col.getName(12).resolveBinding();
IProblemBinding problem = (IProblemBinding) col.getName(12)
.resolveBinding();
assertEquals(IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, problem.getID());
}
@ -1442,7 +1487,8 @@ public class AST2CPPTests extends AST2BaseTest {
}
public void testPointerToMemberType() throws Exception {
IASTTranslationUnit tu = parse( "struct S; int S::* pm;", ParserLanguage.CPP );
IASTTranslationUnit tu = parse("struct S; int S::* pm;", //$NON-NLS-1$
ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
@ -1499,8 +1545,10 @@ public class AST2CPPTests extends AST2BaseTest {
IType t = pm.getType();
assertTrue(t instanceof ICPPPointerToMemberType);
IFunctionType ft = (IFunctionType) ((ICPPPointerToMemberType)t).getType();
ICPPClassType ST = (ICPPClassType) ((ICPPPointerToMemberType)t).getMemberOfClass();
IFunctionType ft = (IFunctionType) ((ICPPPointerToMemberType) t)
.getType();
ICPPClassType ST = ((ICPPPointerToMemberType) t)
.getMemberOfClass();
assertTrue(ft.getReturnType() instanceof IPointerType);
assertSame(ST, ((IPointerType) ft.getReturnType()).getType());
@ -1511,5 +1559,54 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances(col, i, 2);
assertInstances(col, f, 3);
}
public void _testBug84469() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("struct S { int i; }; \n"); //$NON-NLS-1$
buffer.append("void f() { ; \n"); //$NON-NLS-1$
buffer.append(" int S::* pm = &S::i; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
}
public void testFindTypeBinding_1() throws Exception {
IASTTranslationUnit tu = parse(
"int x = 5; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$
IASTStandardFunctionDeclarator fdtor = (IASTStandardFunctionDeclarator) ((IASTSimpleDeclaration) tu
.getDeclarations()[1]).getDeclarators()[0];
IASTName name = fdtor.getParameters()[0].getDeclarator().getName();
IBinding binding = CPPSemantics.findTypeBinding(tu, name);
assertNull(binding);
tu = parse("struct x; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$
fdtor = (IASTStandardFunctionDeclarator) ((IASTSimpleDeclaration) tu
.getDeclarations()[1]).getDeclarators()[0];
name = ((ICPPASTNamedTypeSpecifier) fdtor.getParameters()[0]
.getDeclSpecifier()).getName();
binding = CPPSemantics.findTypeBinding(tu, name);
assertNotNull(binding);
assertTrue(binding instanceof ICPPClassType);
}
public void testFindTypeBinding_2() throws Exception {
IASTTranslationUnit tu = parse(
"struct B; void f() { B * bp; }", ParserLanguage.CPP); //$NON-NLS-1$
IASTCompoundStatement compound = (IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[1]).getBody();
IASTBinaryExpression exp = (IASTBinaryExpression) ((IASTExpressionStatement) compound
.getStatements()[0]).getExpression();
IBinding binding = CPPSemantics.findTypeBinding(compound,
((IASTIdExpression) exp.getOperand1()).getName());
assertNotNull(binding);
assertTrue(binding instanceof ICPPClassType);
}
}

View file

@ -124,8 +124,7 @@ public class CPPSemantics {
this.name = n;
}
public boolean includeBlockItem( IASTNode item ){
if( astName == null ) return false;
if( astName.getParent() instanceof IASTIdExpression ||
if( ( astName != null && astName.getParent() instanceof IASTIdExpression ) ||
item instanceof IASTNamespaceDefinition ||
(item instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)item).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ) )
{
@ -1759,4 +1758,54 @@ public class CPPSemantics {
}
return -1;
}
/**
* Find the binding for the type for the given name, if the given name is not a type, or can not
* be resolved, null is returned.
* @param mostRelevantScope
* @param name
* @return
*/
public static IBinding findTypeBinding( IASTNode mostRelevantScope, IASTName name ){
IScope scope = null;
if( mostRelevantScope instanceof IASTCompoundStatement )
scope = ((IASTCompoundStatement) mostRelevantScope).getScope();
else if ( mostRelevantScope instanceof IASTTranslationUnit )
scope = ((IASTTranslationUnit) mostRelevantScope).getScope();
else if ( mostRelevantScope instanceof ICPPASTNamespaceDefinition )
scope = ((ICPPASTNamespaceDefinition) mostRelevantScope).getScope();
else if( mostRelevantScope instanceof ICPPASTCompositeTypeSpecifier )
scope = ((ICPPASTCompositeTypeSpecifier) mostRelevantScope).getScope();
if( scope == null )
return null;
LookupData data = new LookupData( name ){
public boolean typesOnly(){ return true; }
public boolean forUsingDeclaration(){ return false; }
public boolean forDefinition(){ return false; }
public boolean considerConstructors(){ return false; }
public boolean functionCall(){ return false; }
public boolean qualified(){
IASTNode p1 = astName.getParent();
if( p1 instanceof ICPPASTQualifiedName ){
return ((ICPPASTQualifiedName)p1).getNames()[0] != astName;
}
return false;
}
};
try {
lookup( data, scope );
} catch (DOMException e) {
return null;
}
IBinding binding = null;
try {
binding = resolveAmbiguities( data, name );
} catch ( DOMException e2 ) {
}
return binding;
}
}