1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

pdom templates: add unit tests and make two small fixes

This commit is contained in:
Andrew Ferguson 2007-05-03 13:04:25 +00:00
parent 1feefea543
commit 2fac395f97
9 changed files with 410 additions and 142 deletions

View file

@ -392,4 +392,9 @@ public class AST2BaseTest extends BaseTestCase {
return TestSourceReader.getContentsForTest(
CTestPlugin.getDefault().getBundle(), "parser", getClass(), getName(), sections);
}
protected static void assertInstance(Object o, Class c) {
assertNotNull(o);
assertTrue("Expected "+c.getName()+" but got "+o.getClass().getName(), c.isInstance(o));
}
}

View file

@ -2015,4 +2015,45 @@ public class AST2TemplateTests extends AST2BaseTest {
assertTrue( col.getName(2).resolveBinding() instanceof ICPPSpecialization );
}
// // Brian W.'s example from bugzilla#167098
// template<class K>
// class D { //CPPClassTemplate
// public:
// template<class T, class X>
// D(T t, X x) {} // CPPConstructorTemplate
//
// template<class T, class X>
// void foo(T t, X x) {} // CPPMethodTemplate
// };
//
// void bar() {
// D<int> *var = new D<int>(5, 6);
// // First D<int>: CPPClassInstance
// // Second D<int>: CPPConstructorInstance
// // Now, getting the instance's specialized binding should
// // result in a CPPConstructorTemplateSpecialization
// var->foo<int,int>(7, 8);
// // foo -> CPPMethodTemplateSpecialization
// // foo<int,int> -> CPPMethodInstance
// }
public void testCPPConstructorTemplateSpecialization() throws Exception {
StringBuffer buffer = getContents(1)[0];
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP, true, true );
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
ICPPASTTemplateId tid= (ICPPASTTemplateId) col.getName(20);
IASTName cn= col.getName(21);
assertInstance(cn.resolveBinding(), ICPPClassTemplate.class); // *D*<int>(5, 6)
assertInstance(cn.resolveBinding(), ICPPClassType.class); // *D*<int>(5, 6)
assertInstance(tid.resolveBinding(), ICPPTemplateInstance.class); // *D<int>*(5, 6)
assertInstance(tid.resolveBinding(), ICPPConstructor.class); // *D<int>*(5, 6)
IBinding tidSpc= ((ICPPTemplateInstance)tid.resolveBinding()).getSpecializedBinding();
assertInstance(tidSpc, ICPPConstructor.class);
assertInstance(tidSpc, ICPPSpecialization.class);
assertInstance(tidSpc, ICPPFunctionTemplate.class);
}
}

View file

@ -13,7 +13,9 @@ package org.eclipse.cdt.internal.index.tests;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
@ -38,9 +40,12 @@ import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
import org.eclipse.cdt.internal.pdom.tests.PDOMPrettyPrinter;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
@ -60,6 +65,7 @@ import org.osgi.framework.Bundle;
* the PDOM purely from AST information (i.e. without a real binding from the DOM)
*/
public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
private static final boolean DEBUG= false;
protected ITestStrategy strategy;
public void setStrategy(ITestStrategy strategy) {
@ -92,32 +98,24 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
return language.getSelectedNames(ast, strategy.getTestData()[1].indexOf(section), len);
}
/**
* Matching against a length of text can return several names, we use the RawSignature to
* disambiguate between them
* @param section
* @param rawSig
* @return
*/
protected IBinding getBindingFromASTNameWithRawSignature(String section, String rawSig) {
IASTName[] names= findNames(section, rawSig.length());
List matchSignature= new ArrayList();
for(int i=0; i<names.length; i++) {
if(rawSig.equals(names[i].getRawSignature())) {
matchSignature.add(names[i]);
}
}
assertTrue("<>1 names found for \""+section+"\" with signature "+rawSig, matchSignature.size()==1);
IASTName aname= (IASTName) matchSignature.get(0);
IBinding binding = aname.resolveBinding();
assertNotNull("No binding for "+aname.getRawSignature(), binding);
assertFalse("Binding is a ProblemBinding for name "+aname.getRawSignature(), IProblemBinding.class.isAssignableFrom(names[0].resolveBinding().getClass()));
return aname.resolveBinding();
}
protected IBinding getBindingFromASTName(String section, int len) {
return getBindingFromASTName(section, len, false);
}
protected IBinding getBindingFromASTName(String section, int len, boolean matchLength) {
IASTName[] names= findNames(section, len);
if(matchLength) {
List lnames= new ArrayList(Arrays.asList(names));
for(ListIterator li= lnames.listIterator(); li.hasNext(); ) {
IASTName name= (IASTName) li.next();
if(name.getRawSignature().length()!=len) {
li.remove();
}
}
names= (IASTName[]) lnames.toArray(new IASTName[lnames.size()]);
}
assertEquals("<>1 name found for \""+section+"\"", 1, names.length);
IBinding binding = names[0].resolveBinding();
assertNotNull("No binding for "+names[0].getRawSignature(), binding);
@ -276,7 +274,10 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
IFile cppfile= TestSourceReader.createFile(cproject.getProject(), new Path("references.c" + (cpp ? "pp" : "")), testData[1].toString());
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor()));
// ((PDOM)CCorePlugin.getIndexManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter());
if(DEBUG) {
((PDOM)CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter());
}
index= CCorePlugin.getIndexManager().getIndex(cproject);
@ -348,8 +349,10 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
CCorePlugin.getIndexManager().reindex(cproject);
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor()));
// System.out.println("Online: "+getName());
// ((PDOM)CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter());
if(DEBUG) {
System.out.println("Online: "+getName());
((PDOM)CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter());
}
index= CCorePlugin.getIndexManager().getIndex(cproject);
index.acquireReadLock();
@ -366,9 +369,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
IndexerPreferences.set(referenced.getProject(), IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_FAST_INDEXER);
CCorePlugin.getIndexManager().reindex(referenced);
//System.out.println("Referenced: "+getName());
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor()));
// ((PDOM)CCoreInternals.getPDOMManager().getPDOM(referenced)).accept(new PDOMPrettyPrinter());
if(DEBUG) {
System.out.println("Referenced: "+getName());
((PDOM)CCoreInternals.getPDOMManager().getPDOM(referenced)).accept(new PDOMPrettyPrinter());
}
return referenced;
}

View file

@ -3,7 +3,7 @@
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andrew Ferguson (Symbian) - Initial implementation
@ -12,6 +12,7 @@ package org.eclipse.cdt.internal.index.tests;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IType;
@ -19,12 +20,18 @@ import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
import org.eclipse.cdt.core.parser.util.ObjectMap;
@ -48,6 +55,210 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
setStrategy(new ReferencedProject(true));
}
// // Brian W.'s example from bugzilla#167098
// template<class K>
// class D { //CPPClassTemplate
// public:
// template<class T, class X>
// D(T t, X x) {} // CPPConstructorTemplate
//
// template<class T, class X>
// void foo(T t, X x) {} // CPPMethodTemplate
// };
// void bar() {
// D<int> *var = new D<int>(5, 6);
// // First D<int>: CPPClassInstance
// // Second D<int>: CPPConstructorInstance
// // Now, getting the instance's specialized binding should
// // result in a CPPConstructorTemplateSpecialization
// var->foo<int,int>(7, 8);
// // foo -> CPPMethodTemplateSpecialization
// // foo<int,int> -> CPPMethodInstance
// }
public void testCPPConstructorTemplateSpecialization() throws Exception {
IBinding b0= getBindingFromASTName("D<int>(", 1, true);
IBinding b1= getBindingFromASTName("D<int>(", 6, true);
assertInstance(b0, ICPPClassTemplate.class); // *D*<int>(5, 6)
assertInstance(b0, ICPPClassType.class); // *D*<int>(5, 6)
assertInstance(b1, ICPPTemplateInstance.class); // *D<int>*(5, 6)
assertInstance(b1, ICPPConstructor.class); // *D<int>*(5, 6)
IBinding tidSpc= ((ICPPTemplateInstance)b1).getSpecializedBinding();
assertInstance(tidSpc, ICPPConstructor.class);
assertInstance(tidSpc, ICPPSpecialization.class);
assertInstance(tidSpc, ICPPFunctionTemplate.class);
}
// class B {};
//
// template<typename T>
// class A {
// public:
// T (*f)(int x);
// };
//
// template<typename T> T foo(int x) {return *new T();}
// template<typename T> T foo(int x, int y) {return *new T();}
// void qux() {
// A<B> a;
// a.f= foo<B>;
// }
public void _testOverloadedFunctionTemplate() {
IBinding b0= getBindingFromASTName("foo<B>;", 6, true);
assertInstance(b0, ICPPFunction.class);
assertInstance(b0, ICPPSpecialization.class);
}
// template<typename T, template<typename U> class S>
// class Foo {
// public:
// S<T> s;
// };
//
// template<typename Z> class X {
// public:
// void foo(Z z) {}
// };
//
// class A {};
// void qux() {
// Foo<A,X> f;
// f.s.foo(*new A());
// }
public void _testTemplateTemplateParameter() throws Exception {
IBinding b0= getBindingFromASTName("Foo<A,X>", 3);
IBinding b1= getBindingFromASTName("Foo<A,X>", 8, true);
IBinding b2= getBindingFromASTName("f.s.foo", 1);
IBinding b3= getBindingFromASTName("s.foo", 1);
IBinding b4= getBindingFromASTName("foo(*", 3);
assertInstance(b0, ICPPClassTemplate.class);
assertInstance(b0, ICPPClassType.class);
ICPPTemplateParameter[] ps= ((ICPPClassTemplate)b0).getTemplateParameters();
assertEquals(2, ps.length);
assertInstance(ps[0], ICPPTemplateTypeParameter.class);
assertInstance(ps[1], ICPPTemplateTemplateParameter.class);
assertInstance(b1, ICPPTemplateInstance.class);
assertInstance(b1, ICPPClassType.class);
IType[] type= ((ICPPTemplateInstance)b1).getArguments();
assertInstance(type[0], ICPPClassType.class);
assertInstance(type[1], ICPPClassTemplate.class);
assertInstance(type[1], ICPPClassType.class);
ObjectMap om= ((ICPPTemplateInstance)b1).getArgumentMap();
assertEquals(2, om.size());
assertInstance(om.keyAt(0), ICPPTemplateTypeParameter.class);
assertInstance(om.getAt(0), ICPPClassType.class);
assertInstance(om.keyAt(1), ICPPTemplateTemplateParameter.class);
assertInstance(om.getAt(1), ICPPClassType.class);
assertInstance(om.getAt(1), ICPPClassTemplate.class);
IBinding b1_spcd= ((ICPPTemplateInstance)b1).getSpecializedBinding();
assertInstance(b1_spcd, ICPPClassTemplate.class);
assertInstance(b1_spcd, ICPPClassType.class);
assertTrue(((IType)b1_spcd).isSameType((IType)b0));
}
// template<typename T1, typename T2>
// class Foo {
// public:
// T1* foo (T2 t) {
// return 0;
// }
// };
//
// class A {};
// class B {};
//
// class X : public Foo<A,B> {};
// class Y : public Foo<B,A> {};
//
// class AA {};
// class BB {};
//
// class Z : public Foo<AA,BB> {};
//
// X x;
// Y y;
// Z z;
public void testInstanceInheritance() throws Exception {
IBinding[] bs= {
getBindingFromASTName("X x;", 1),
getBindingFromASTName("Y y;", 1),
getBindingFromASTName("Z z;", 1)
};
for(int i=0; i<bs.length; i++) {
IBinding b= bs[i];
assertInstance(b, ICPPClassType.class);
ICPPClassType C= (ICPPClassType) b;
assertEquals(1, C.getBases().length);
ICPPClassType xb= (ICPPClassType) C.getBases()[0].getBaseClass();
assertInstance(xb, ICPPTemplateInstance.class);
ObjectMap args= ((ICPPTemplateInstance) xb).getArgumentMap();
assertInstance(args.keyAt(0), ICPPTemplateTypeParameter.class);
assertInstance(args.keyAt(1), ICPPTemplateTypeParameter.class);
assertInstance(args.getAt(0), ICPPClassType.class);
assertInstance(args.getAt(1), ICPPClassType.class);
}
}
// class B {};
//
// template<typename T>
// class A {
// T t;
// int x;
// T foo(T t) { return t; }
// void bar(T t, int& x) {}
// };
//
// template<>
// class A<B> {
// B t;
// int x;
// B foo(B t) { B x= *new B(); return x; }
// void bar(B t, int& x) { x++; }
// };
// A<B> ab;
public void testClassSpecializationMethods() throws Exception {
IBinding b0= getBindingFromASTName("A<B> ab", 4, true);
assertInstance(b0, ICPPClassType.class);
assertInstance(b0, ICPPSpecialization.class);
assertFalse(b0 instanceof ICPPTemplateInstance);
ICPPClassType ct= (ICPPClassType) b0;
ICPPMethod[] dms= ct.getDeclaredMethods();
assertEquals(2, dms.length);
ICPPMethod foo= dms[0].getName().equals("foo") ? dms[0] : dms[1];
ICPPMethod bar= dms[0].getName().equals("bar") ? dms[0] : dms[1];
assertEquals(foo.getName(), "foo");
assertEquals(bar.getName(), "bar");
assertInstance(foo.getType().getReturnType(), ICPPClassType.class);
assertEquals(((ICPPClassType)foo.getType().getReturnType()).getName(), "B");
assertEquals(foo.getType().getParameterTypes().length, 1);
assertInstance(foo.getType().getParameterTypes()[0], ICPPClassType.class);
assertEquals(((ICPPClassType)foo.getType().getParameterTypes()[0]).getName(), "B");
assertInstance(bar.getType().getReturnType(), ICPPBasicType.class);
assertEquals(((ICPPBasicType)bar.getType().getReturnType()).getType(), IBasicType.t_void);
ICPPField[] fs= ct.getDeclaredFields();
assertEquals(2, fs.length);
}
// template<typename T> class A {
// public:
// typedef T TD;
@ -287,9 +498,9 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
// class Foo<A> {};
//
// Foo<B> b2;
public void _testClassSpecializationInHeader() {
public void _testClassSpecializations() {
IBinding b1a = getBindingFromASTName("Foo<B> b1;", 3);
IBinding b1b = getBindingFromASTNameWithRawSignature("Foo<B> b1;", "Foo<B>");
IBinding b1b = getBindingFromASTName("Foo<B> b1;", 5, true);
assertInstance(b1a, ICPPClassType.class);
assertInstance(b1a, ICPPClassTemplate.class);
@ -304,7 +515,7 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
assertEquals("B", b1pct.getName());
IBinding b2a = getBindingFromASTName("Foo<B> b2;", 3);
IBinding b2b = getBindingFromASTNameWithRawSignature("Foo<B> b2;", "Foo<B>");
IBinding b2b = getBindingFromASTName("Foo<B> b2;", 5, true);
assertInstance(b2a, ICPPClassType.class);
assertInstance(b2a, ICPPClassTemplate.class);

View file

@ -171,23 +171,9 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization implements
return new ICPPBase[0];
}
private static class ConstructorCollector implements IPDOMVisitor {
private List fConstructors = new ArrayList();
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPConstructor)
fConstructors.add(node);
return false;
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPConstructor[] getConstructors() {
return (ICPPConstructor[])fConstructors.toArray(new ICPPConstructor[fConstructors.size()]);
}
}
public ICPPConstructor[] getConstructors() throws DOMException {
try {
ConstructorCollector visitor= new ConstructorCollector();
PDOMClassUtil.ConstructorCollector visitor= new PDOMClassUtil.ConstructorCollector();
accept(visitor);
return visitor.getConstructors();
} catch (CoreException e) {
@ -196,9 +182,28 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization implements
}
}
//ICPPClassType unimplemented
public ICPPField[] getDeclaredFields() throws DOMException { fail(); return null; }
public ICPPMethod[] getDeclaredMethods() throws DOMException { fail(); return null; }
public ICPPMethod[] getDeclaredMethods() throws DOMException {
try {
PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(false);
accept(methods);
return methods.getMethods();
} catch (CoreException e) {
return new ICPPMethod[0];
}
}
public ICPPField[] getDeclaredFields() throws DOMException {
try {
PDOMClassUtil.FieldCollector visitor = new PDOMClassUtil.FieldCollector();
accept(visitor);
return visitor.getFields();
} catch (CoreException e) {
CCorePlugin.log(e);
return new ICPPField[0];
}
}
//ICPPClassType unimplemented
public IField[] getFields() throws DOMException { fail(); return null; }
public IBinding[] getFriends() throws DOMException { fail(); return null; }
public ICPPMethod[] getMethods() throws DOMException { fail(); return null; }
@ -344,36 +349,9 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization implements
return null;
}
private static class MethodCollector implements IPDOMVisitor {
private final List methods;
private final boolean acceptImplicit;
private final boolean acceptAll;
public MethodCollector(boolean acceptImplicit) {
this(acceptImplicit, true);
}
public MethodCollector(boolean acceptImplicit, boolean acceptExplicit) {
this.methods = new ArrayList();
this.acceptImplicit= acceptImplicit;
this.acceptAll= acceptImplicit && acceptExplicit;
}
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPMethod) {
if (acceptAll || ((ICPPMethod) node).isImplicit() == acceptImplicit) {
methods.add(node);
}
}
return false; // don't visit the method
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPMethod[] getMethods() {
return (ICPPMethod[])methods.toArray(new ICPPMethod[methods.size()]);
}
}
public ICPPMethod[] getImplicitMethods() {
try {
MethodCollector methods = new MethodCollector(true, false);
PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(true, false);
accept(methods);
return methods.getMethods();
} catch (CoreException e) {

View file

@ -158,7 +158,7 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
public ICPPMethod[] getDeclaredMethods() throws DOMException {
try {
MethodCollector methods = new MethodCollector(false);
PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(false);
accept(methods);
return methods.getMethods();
} catch (CoreException e) {
@ -168,7 +168,7 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
public ICPPMethod[] getMethods() throws DOMException {
try {
MethodCollector methods = new MethodCollector(true);
PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(true);
acceptInHierarchy(new HashSet(), methods);
return methods.getMethods();
} catch (CoreException e) {
@ -179,7 +179,7 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
public ICPPMethod[] getImplicitMethods() {
try {
MethodCollector methods = new MethodCollector(true, false);
PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(true, false);
accept(methods);
return methods.getMethods();
} catch (CoreException e) {
@ -207,7 +207,7 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
}
public ICPPMethod[] getAllDeclaredMethods() throws DOMException {
MethodCollector myMethods = new MethodCollector(false, true);
PDOMClassUtil.MethodCollector myMethods = new PDOMClassUtil.MethodCollector(false, true);
Set visited = new HashSet();
try {
acceptInHierarchy(visited, myMethods);
@ -217,52 +217,10 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
return new ICPPMethod[0];
}
}
private static class MethodCollector implements IPDOMVisitor {
private final List methods;
private final boolean acceptImplicit;
private final boolean acceptAll;
public MethodCollector(boolean acceptImplicit) {
this(acceptImplicit, true);
}
public MethodCollector(boolean acceptImplicit, boolean acceptExplicit) {
this.methods = new ArrayList();
this.acceptImplicit= acceptImplicit;
this.acceptAll= acceptImplicit && acceptExplicit;
}
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPMethod) {
if (acceptAll || ((ICPPMethod) node).isImplicit() == acceptImplicit) {
methods.add(node);
}
}
return false; // don't visit the method
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPMethod[] getMethods() {
return (ICPPMethod[])methods.toArray(new ICPPMethod[methods.size()]);
}
}
private static class FieldCollector implements IPDOMVisitor {
private List fields = new ArrayList();
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPField)
fields.add(node);
return false;
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPField[] getFields() {
return (ICPPField[])fields.toArray(new ICPPField[fields.size()]);
}
}
public IField[] getFields() throws DOMException {
try {
FieldCollector visitor = new FieldCollector();
PDOMClassUtil.FieldCollector visitor = new PDOMClassUtil.FieldCollector();
acceptInHierarchy(new HashSet(), visitor);
return visitor.getFields();
} catch (CoreException e) {
@ -273,7 +231,7 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
public ICPPField[] getDeclaredFields() throws DOMException {
try {
FieldCollector visitor = new FieldCollector();
PDOMClassUtil.FieldCollector visitor = new PDOMClassUtil.FieldCollector();
accept(visitor);
return visitor.getFields();
} catch (CoreException e) {
@ -336,22 +294,8 @@ ICPPClassScope, IPDOMMemberOwner, IIndexType, IIndexScope {
addMember(member);
}
private static class ConstructorCollector implements IPDOMVisitor {
private List fConstructors = new ArrayList();
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPConstructor)
fConstructors.add(node);
return false;
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPConstructor[] getConstructors() {
return (ICPPConstructor[])fConstructors.toArray(new ICPPConstructor[fConstructors.size()]);
}
}
public ICPPConstructor[] getConstructors() throws DOMException {
ConstructorCollector visitor= new ConstructorCollector();
PDOMClassUtil.ConstructorCollector visitor= new PDOMClassUtil.ConstructorCollector();
try {
accept(visitor);
} catch (CoreException e) {

View file

@ -44,7 +44,7 @@ public class PDOMCPPConstructorInstance extends PDOMCPPMethodInstance implements
}
public int getNodeType() {
return PDOMCPPLinkage.CPP_METHOD_INSTANCE;
return PDOMCPPLinkage.CPP_CONSTRUCTOR_INSTANCE;
}
public boolean isExplicit() throws DOMException {

View file

@ -211,6 +211,7 @@ class PDOMCPPFunction extends PDOMCPPBinding implements ICPPFunction, IPDOMOverl
StringBuffer result = new StringBuffer();
try {
result.append(getName()+" "+ASTTypeUtil.getParameterTypeString(getType())); //$NON-NLS-1$
result.append(" "+getNodeType()); //$NON-NLS-1$
} catch(DOMException de) {
result.append(de);
}

View file

@ -0,0 +1,82 @@
/*******************************************************************************
* Copyright (c) 2007 QNX Software Systems 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:
* QNX - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.dom.IPDOMNode;
import org.eclipse.cdt.core.dom.IPDOMVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.core.runtime.CoreException;
/**
* For implementation sharing between PDOMCPPClassType and PDOMCPPClassSpecialization
*/
class PDOMClassUtil {
static class FieldCollector implements IPDOMVisitor {
private List fields = new ArrayList();
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPField)
fields.add(node);
return false;
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPField[] getFields() {
return (ICPPField[])fields.toArray(new ICPPField[fields.size()]);
}
}
static class ConstructorCollector implements IPDOMVisitor {
private List fConstructors = new ArrayList();
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPConstructor)
fConstructors.add(node);
return false;
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPConstructor[] getConstructors() {
return (ICPPConstructor[])fConstructors.toArray(new ICPPConstructor[fConstructors.size()]);
}
}
static class MethodCollector implements IPDOMVisitor {
private final List methods;
private final boolean acceptImplicit;
private final boolean acceptAll;
public MethodCollector(boolean acceptImplicit) {
this(acceptImplicit, true);
}
public MethodCollector(boolean acceptImplicit, boolean acceptExplicit) {
this.methods = new ArrayList();
this.acceptImplicit= acceptImplicit;
this.acceptAll= acceptImplicit && acceptExplicit;
}
public boolean visit(IPDOMNode node) throws CoreException {
if (node instanceof ICPPMethod) {
if (acceptAll || ((ICPPMethod) node).isImplicit() == acceptImplicit) {
methods.add(node);
}
}
return false; // don't visit the method
}
public void leave(IPDOMNode node) throws CoreException {
}
public ICPPMethod[] getMethods() {
return (ICPPMethod[])methods.toArray(new ICPPMethod[methods.size()]);
}
}
}