diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java index da027e47bd7..8e56f2df2ae 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java @@ -14,12 +14,19 @@ package org.eclipse.cdt.core.parser.tests.ast2; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IBasicType; +import org.eclipse.cdt.core.dom.ast.IFunctionType; +import org.eclipse.cdt.core.dom.ast.IPointerType; import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; import org.eclipse.cdt.core.parser.ParserLanguage; /** @@ -51,44 +58,91 @@ public class AST2TemplateTests extends AST2BaseTest { assertNotNull( A ); } -// public void testTemplateInstance() throws Exception { -// StringBuffer buffer = new StringBuffer(); -// buffer.append("template < class T > class A { \n"); //$NON-NLS-1$ -// buffer.append(" T t; \n"); //$NON-NLS-1$ -// buffer.append("}; \n"); //$NON-NLS-1$ -// buffer.append("void f(){ \n"); //$NON-NLS-1$ -// buffer.append(" A a; \n"); //$NON-NLS-1$ -// buffer.append(" a.t; \n"); //$NON-NLS-1$ -// buffer.append("} \n"); //$NON-NLS-1$ -// -// IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); -// CPPNameCollector col = new CPPNameCollector(); -// tu.accept(col); -// -// assertEquals( col.size(), 10 ); -// -// ICPPClassType A1 = (ICPPClassType) col.getName(1).resolveBinding(); -// ICPPField t1 = (ICPPField) col.getName(3).resolveBinding(); -// -// ICPPVariable a = (ICPPVariable) col.getName(7).resolveBinding(); -// assertFalse( a.isTemplateInstance() ); -// -// ICPPClassType A2 = (ICPPClassType) col.getName(5).resolveBinding(); -// ICPPClassType A = (ICPPClassType) a.getType(); -// assertSame( A2, A ); -// ICPPClassScope AScope = (ICPPClassScope) A.getCompositeScope(); -// -// ICPPField t = (ICPPField) col.getName(9).resolveBinding(); -// IType type = t.getType(); -// assertTrue( type instanceof IBasicType ); -// assertEquals( ((IBasicType)type).getType(), IBasicType.t_int ); -// -// assertSame( t.getScope(), AScope ); -// -// assertTrue( A.isTemplateInstance() ); -// assertSame( A.getTemplatedBinding(), A1 ); -// assertTrue( t.isTemplateInstance() ); -// assertSame( t.getTemplatedBinding(), t1 ); -// } - + public void testBasicTemplateInstance_1() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("template < class T > class A { \n"); //$NON-NLS-1$ + buffer.append(" T t1; \n"); //$NON-NLS-1$ + buffer.append(" T * t2; \n"); //$NON-NLS-1$ + buffer.append("}; \n"); //$NON-NLS-1$ + buffer.append("void f(){ \n"); //$NON-NLS-1$ + buffer.append(" A a; \n"); //$NON-NLS-1$ + buffer.append(" a.t1; a.t2; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + CPPNameCollector col = new CPPNameCollector(); + tu.accept(col); + + assertEquals( col.size(), 14 ); + + ICPPClassType A = (ICPPClassType) col.getName(1).resolveBinding(); + ICPPTemplateParameter T = (ICPPTemplateParameter) col.getName(0).resolveBinding(); + ICPPField t1 = (ICPPField) col.getName(3).resolveBinding(); + ICPPField t2 = (ICPPField) col.getName(5).resolveBinding(); + + assertSame( t1.getType(), T ); + assertSame( ((IPointerType)t2.getType()).getType(), T ); + + ICPPVariable a = (ICPPVariable) col.getName(9).resolveBinding(); + + ICPPClassType A_int = (ICPPClassType) col.getName(7).resolveBinding(); + assertSame( A_int, a.getType() ); + + assertTrue( A_int instanceof ICPPInstance ); + assertSame( ((ICPPInstance)A_int).getOriginalBinding(), A ); + + ICPPClassScope A_int_Scope = (ICPPClassScope) A_int.getCompositeScope(); + assertNotSame( A_int_Scope, A.getCompositeScope() ); + + ICPPField t = (ICPPField) col.getName(11).resolveBinding(); + assertTrue( t instanceof ICPPInstance ); + assertSame( ((ICPPInstance)t).getOriginalBinding(), t1 ); + assertSame( t.getScope(), A_int_Scope ); + IType type = t.getType(); + assertTrue( type instanceof IBasicType ); + assertEquals( ((IBasicType)type).getType(), IBasicType.t_int ); + + t = (ICPPField) col.getName(13).resolveBinding(); + assertTrue( t instanceof ICPPInstance ); + assertSame( ((ICPPInstance)t).getOriginalBinding(), t2 ); + assertSame( t.getScope(), A_int_Scope ); + type = t.getType(); + assertTrue( type instanceof IPointerType ); + assertTrue( ((IPointerType)type).getType() instanceof IBasicType ); + assertEquals( ((IBasicType)((IPointerType)type).getType()).getType(), IBasicType.t_int ); + } + + public void testBasicTemplateInstance_2() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("template < class T > class A { \n"); //$NON-NLS-1$ + buffer.append(" T f( T *); \n"); //$NON-NLS-1$ + buffer.append("}; \n"); //$NON-NLS-1$ + buffer.append("void g(){ \n"); //$NON-NLS-1$ + buffer.append(" A a; \n"); //$NON-NLS-1$ + buffer.append(" a.f( (int*)0 ); \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + CPPNameCollector col = new CPPNameCollector(); + tu.accept(col); + + ICPPClassType A = (ICPPClassType) col.getName(1).resolveBinding(); + ICPPTemplateParameter T = (ICPPTemplateParameter) col.getName(0).resolveBinding(); + ICPPMethod f = (ICPPMethod) col.getName(3).resolveBinding(); + IFunctionType ft = f.getType(); + + assertSame( ft.getReturnType(), T ); + assertSame( ((IPointerType)ft.getParameterTypes()[0]).getType(), T ); + + ICPPClassType A_int = (ICPPClassType) col.getName(7).resolveBinding(); + assertTrue( A_int instanceof ICPPInstance ); + assertSame( ((ICPPInstance)A_int).getOriginalBinding(), A ); + + ICPPMethod f_int = (ICPPMethod) col.getName(11).resolveBinding(); + assertTrue( f_int instanceof ICPPInstance ); + assertSame( ((ICPPInstance)f_int).getOriginalBinding(), f ); + ft = f_int.getType(); + assertTrue( ft.getReturnType() instanceof IBasicType ); + assertTrue( ((IPointerType)ft.getParameterTypes()[0]).getType() instanceof IBasicType ); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPInstance.java new file mode 100644 index 00000000000..071ac04e517 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPInstance.java @@ -0,0 +1,39 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 28, 2005 + */ +package org.eclipse.cdt.core.dom.ast.cpp; + +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public interface ICPPInstance extends IBinding { + + /** + * Get the original binding of which this is an instance of + * @return + */ + public IBinding getOriginalBinding(); + + /** + * return a map which maps from template parameter to the corresponding + * template argument + * @return + */ + public ObjectMap getArgumentMap(); + + public ICPPTemplateDefinition getTemplate(); + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPTemplateDefinition.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPTemplateDefinition.java index 1853c9ab4a0..ea16d74b281 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPTemplateDefinition.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPTemplateDefinition.java @@ -10,7 +10,6 @@ **********************************************************************/ package org.eclipse.cdt.core.dom.ast.cpp; -import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; /** @@ -31,7 +30,7 @@ public interface ICPPTemplateDefinition extends ICPPBinding{ * @param arguments * @return */ - public IBinding instantiate( IASTNode [] arguments ); + public IBinding instantiate( ICPPASTTemplateId id ); /** * returns the templated declaration for this template, diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ObjectMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ObjectMap.java index 3ddb54620d1..12272293b71 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ObjectMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ObjectMap.java @@ -125,4 +125,10 @@ public class ObjectMap extends ObjectTable { } } } + + public Object [] valueArray(){ + Object [] vals = new Object[ size() ]; + System.arraycopy( valueTable, 0, vals, 0, vals.length ); + return vals; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java index 69e7874b03d..bca22ef7b65 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java @@ -125,10 +125,10 @@ public class CPPASTTemplateId extends CPPASTNode implements ICPPASTTemplateId { * @see org.eclipse.cdt.core.dom.ast.IASTName#toCharArray() */ public char[] toCharArray() { - return EMPTY_CHAR_ARRAY; + return templateName.toCharArray(); } public String toString() { - return EMPTY_STRING; + return templateName.toString(); } public boolean accept( ASTVisitor action ){ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstance.java new file mode 100644 index 00000000000..accacff9f34 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstance.java @@ -0,0 +1,195 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 28, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IField; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; +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.ICPPDelegate; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPClassInstance extends CPPInstance implements ICPPClassType, ICPPInternalBinding { + private IASTName id; + private CPPClassInstanceScope instanceScope; + + /** + * @param decl + * @param arguments + */ + public CPPClassInstance( IASTName id, ICPPScope scope, IBinding decl, ObjectMap argMap ) { + super( scope, decl, argMap ); + this.id = id; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getBases() + */ + public ICPPBase[] getBases() { + return ICPPBase.EMPTY_BASE_ARRAY; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getFields() + */ + public IField[] getFields() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.ICompositeType#findField(java.lang.String) + */ + public IField findField(String name) throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getDeclaredFields() + */ + public ICPPField[] getDeclaredFields() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getMethods() + */ + public ICPPMethod[] getMethods() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getAllDeclaredMethods() + */ + public ICPPMethod[] getAllDeclaredMethods() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getDeclaredMethods() + */ + public ICPPMethod[] getDeclaredMethods() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getConstructors() + */ + public ICPPConstructor[] getConstructors() throws DOMException { + return new ICPPConstructor[0]; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getFriends() + */ + public IBinding[] getFriends() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getKey() + */ + public int getKey() throws DOMException { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getCompositeScope() + */ + public IScope getCompositeScope() { + if( instanceScope == null ){ + instanceScope = new CPPClassInstanceScope( this ); + } + return instanceScope; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName() + */ + public String[] getQualifiedName() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray() + */ + public char[][] getQualifiedNameCharArray() throws DOMException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified() + */ + public boolean isGloballyQualified() throws DOMException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + public Object clone(){ + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations() + */ + public IASTNode[] getDeclarations() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition() + */ + public IASTNode getDefinition() { + return id; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#createDelegate(org.eclipse.cdt.core.dom.ast.IASTName) + */ + public ICPPDelegate createDelegate(IASTName name) { + return new CPPClassType.CPPClassTypeDelegate( name, this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDefinition(org.eclipse.cdt.core.dom.ast.IASTNode) + */ + public void addDefinition(IASTNode node) { + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstanceScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstanceScope.java new file mode 100644 index 00000000000..fe5f7c58cf7 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassInstanceScope.java @@ -0,0 +1,195 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 28, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.parser.util.ArrayUtil; +import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; +import org.eclipse.cdt.core.parser.util.ObjectMap; +import org.eclipse.cdt.core.parser.util.ObjectSet; + +/** + * @author aniefer + */ +public class CPPClassInstanceScope implements ICPPClassScope { + private CharArrayObjectMap bindings; + private ObjectMap instanceMap = ObjectMap.EMPTY_MAP; + + private ICPPInstance instance; + private boolean isFullyCached = false; + /** + * @param instance + */ + public CPPClassInstanceScope(CPPClassInstance instance ) { + this.instance = instance; + } + + private ICPPClassType getOriginalClass(){ + return (ICPPClassType) instance.getOriginalBinding(); + } + public boolean isFullyCached(){ + if( !isFullyCached ){ + CPPSemantics.LookupData data = new CPPSemantics.LookupData( CPPSemantics.EMPTY_NAME_ARRAY ); + try { + CPPSemantics.lookupInScope( data, this, null ); + } catch (DOMException e) { + } + } + return true; + } + + public IBinding getBinding( IASTName name, boolean forceResolve ) { + //ICPPClassScope scope = (ICPPClassScope) getOriginalClass().getCompositeScope(); + char [] c = name.toCharArray(); + if( bindings == null ) + return null; + + Object cache = bindings.get( c ); + if( cache != null ){ + int i = ( cache instanceof ObjectSet ) ? 0 : -1; + ObjectSet set = ( cache instanceof ObjectSet ) ? (ObjectSet) cache : null; + Object obj = ( set != null ) ? set.keyAt( i ) : cache; + IBinding [] bs = null; + IBinding binding = null; + while( obj != null ){ + if( obj instanceof IASTName ){ + IASTName n = (IASTName) obj; + if( n instanceof ICPPASTQualifiedName ){ + IASTName [] ns = ((ICPPASTQualifiedName)n).getNames(); + n = ns[ ns.length - 1 ]; + } + if( instanceMap.containsKey( n ) ){ + binding = (IBinding) instanceMap.get( n ); + } else { + binding = forceResolve ? n.resolveBinding() : n.getBinding(); + if( binding != null ){ + binding = CPPTemplates.createInstance( n, this, binding, instance.getArgumentMap() ); + if( instanceMap == ObjectMap.EMPTY_MAP ) + instanceMap = new ObjectMap(2); + instanceMap.put( n, binding ); + } + } + } else if( obj instanceof IBinding ){ + if( instanceMap.containsKey( obj ) ){ + binding = (IBinding) instanceMap.get( obj ); + } else { + binding = CPPTemplates.createInstance( null, this, (IBinding) obj, instance.getArgumentMap() ); + if( instanceMap == ObjectMap.EMPTY_MAP ) + instanceMap = new ObjectMap(2); + instanceMap.put( obj, binding ); + } + } + if( binding != null ){ + if( i == -1 ) + return binding; + bs = (IBinding[]) ArrayUtil.append( IBinding.class, bs, binding ); + binding = null; + } + if( i != -1 && ++i < set.size() ){ + obj = set.keyAt( i ); + } else { + obj = null; + } + } + bs = (IBinding[]) ArrayUtil.trim( IBinding.class, bs ); + if( bs.length == 1 ) + return bs[0]; + return CPPSemantics.resolveAmbiguities( name, bs ); + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope#getClassType() + */ + public ICPPClassType getClassType() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope#getImplicitMethods() + */ + public ICPPMethod[] getImplicitMethods() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#getScopeName() + */ + public IASTName getScopeName() { + return (IASTName) ((ICPPInternalBinding)instance).getDefinition(); + } + + public void addName(IASTName name) { + if( bindings == null ) + bindings = new CharArrayObjectMap(1); + char [] c = name.toCharArray(); + Object o = bindings.get( c ); + if( o != null ){ + if( o instanceof ObjectSet ){ + ((ObjectSet)o).put( name ); + //bindings.put( c, ArrayUtil.append( Object.class, (Object[]) o, name ) ); + } else { + ObjectSet temp = new ObjectSet( 2 ); + temp.put( o ); + temp.put( name ); + bindings.put( c, temp ); + } + } else { + bindings.put( c, name ); + } + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#setFullyCached(boolean) + */ + public void setFullyCached(boolean b) { + isFullyCached = b; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IScope#getParent() + */ + public IScope getParent() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) + */ + public IBinding[] find(String name) { + if( name != null ) {} + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode() + */ + public IASTNode getPhysicalNode() throws DOMException { + ICPPClassScope scope = (ICPPClassScope) getOriginalClass().getCompositeScope(); + return scope.getPhysicalNode(); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFieldInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFieldInstance.java new file mode 100644 index 00000000000..1469f44aa63 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFieldInstance.java @@ -0,0 +1,93 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 29, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPFieldInstance extends CPPInstance implements ICPPField { + private IType type = null; + /** + * @param orig + * @param args + */ + public CPPFieldInstance(ICPPScope scope, IBinding orig, ObjectMap argMap ) { + super(scope, orig, argMap); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPMember#getVisibility() + */ + public int getVisibility() throws DOMException { + return ((ICPPField)getOriginalBinding()).getVisibility(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IVariable#getType() + */ + public IType getType() throws DOMException { + if( type == null ){ + type = CPPTemplates.instantiateType( ((ICPPField)getOriginalBinding()).getType(), getArgumentMap() ); + } + return type; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IVariable#isStatic() + */ + public boolean isStatic() throws DOMException { + return ((ICPPField)getOriginalBinding()).isStatic(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName() + */ + public String[] getQualifiedName() { + return CPPVisitor.getQualifiedName( this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray() + */ + public char[][] getQualifiedNameCharArray() { + return CPPVisitor.getQualifiedNameCharArray( this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified() + */ + public boolean isGloballyQualified() throws DOMException { + IScope scope = getScope(); + while( scope != null ){ + if( scope instanceof ICPPBlockScope || + scope.getScopeName() == null || + scope.getScopeName().toCharArray().length == 0 ) + { + return false; + } + scope = scope.getParent(); + } + return true; + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionInstance.java new file mode 100644 index 00000000000..e2c26c036a6 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionInstance.java @@ -0,0 +1,131 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 29, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IFunctionType; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPFunctionInstance extends CPPInstance implements ICPPFunction, ICPPInternalBinding { + private IFunctionType type = null; + private IParameter [] parameters = null; + /** + * @param scope + * @param orig + * @param argMap + */ + public CPPFunctionInstance(ICPPScope scope, IBinding orig, ObjectMap argMap) { + super(scope, orig, argMap); + // TODO Auto-generated constructor stub + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations() + */ + public IASTNode[] getDeclarations() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition() + */ + public IASTNode getDefinition() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#createDelegate(org.eclipse.cdt.core.dom.ast.IASTName) + */ + public ICPPDelegate createDelegate(IASTName name) { + return new CPPFunction.CPPFunctionDelegate( name, this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters() + */ + public IParameter[] getParameters() throws DOMException { + if( parameters == null ){ + IParameter [] params = ((ICPPFunction)getOriginalBinding()).getParameters(); + parameters = new IParameter[ params.length ]; + for (int i = 0; i < params.length; i++) { + parameters[i] = new CPPParameterInstance( null, params[i], getArgumentMap() ); + } + } + + return parameters; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getFunctionScope() + */ + public IScope getFunctionScope() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getType() + */ + public IFunctionType getType() throws DOMException { + if( type == null ){ + type = (IFunctionType) CPPTemplates.instantiateType( ((ICPPFunction)getOriginalBinding()).getType(), getArgumentMap() ); + } + return type; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic() + */ + public boolean isStatic() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName() + */ + public String[] getQualifiedName() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray() + */ + public char[][] getQualifiedNameCharArray() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified() + */ + public boolean isGloballyQualified() { + // TODO Auto-generated method stub + return false; + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPInstance.java new file mode 100644 index 00000000000..44cb68a29b3 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPInstance.java @@ -0,0 +1,79 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 28, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPInstance implements ICPPInstance { + private IBinding binding; + private ObjectMap argMap; + private ICPPScope scope; + + public CPPInstance( ICPPScope scope, IBinding orig, ObjectMap argMap ){ + this.binding = orig; + this.argMap = argMap; + this.scope = scope; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance#getOriginalBinding() + */ + public IBinding getOriginalBinding() { + return binding; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance#getTemplate() + */ + public ICPPTemplateDefinition getTemplate() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() + */ + public String getName() { + return binding.getName(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray() + */ + public char[] getNameCharArray() { + return binding.getNameCharArray(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() + */ + public IScope getScope() { + return scope; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPInstance#getArgumentMap() + */ + public ObjectMap getArgumentMap() { + return argMap; + } + + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPMethodInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPMethodInstance.java new file mode 100644 index 00000000000..c1c99d6bd47 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPMethodInstance.java @@ -0,0 +1,45 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 29, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPMethodInstance extends CPPFunctionInstance implements + ICPPMethod { + + /** + * @param scope + * @param orig + * @param argMap + */ + public CPPMethodInstance(ICPPScope scope, IBinding orig, ObjectMap argMap) { + super(scope, orig, argMap); + // TODO Auto-generated constructor stub + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPMember#getVisibility() + */ + public int getVisibility() { + // TODO Auto-generated method stub + return 0; + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameterInstance.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameterInstance.java new file mode 100644 index 00000000000..1b535fa9f38 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameterInstance.java @@ -0,0 +1,106 @@ +/********************************************************************** + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +/* + * Created on Mar 29, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; +import org.eclipse.cdt.core.parser.util.ObjectMap; + +/** + * @author aniefer + */ +public class CPPParameterInstance extends CPPInstance implements IParameter, + ICPPVariable, ICPPInternalBinding { + + private IType type = null; + + /** + * @param scope + * @param orig + * @param argMap + */ + public CPPParameterInstance(ICPPScope scope, IBinding orig, ObjectMap argMap) { + super(scope, orig, argMap); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations() + */ + public IASTNode[] getDeclarations() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition() + */ + public IASTNode getDefinition() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#createDelegate(org.eclipse.cdt.core.dom.ast.IASTName) + */ + public ICPPDelegate createDelegate(IASTName name) { + if( name == null ) {} + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IVariable#getType() + */ + public IType getType() throws DOMException { + if( type == null ){ + type = CPPTemplates.instantiateType( ((IParameter)getOriginalBinding()).getType(), getArgumentMap() ); + } + return type; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IVariable#isStatic() + */ + public boolean isStatic() { + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName() + */ + public String[] getQualifiedName() { + return CPPVisitor.getQualifiedName( this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray() + */ + public char[][] getQualifiedNameCharArray() { + return CPPVisitor.getQualifiedNameCharArray( this ); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified() + */ + public boolean isGloballyQualified() { + return false; + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java index 3e25304fe07..8ffc6de5e4c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java @@ -19,6 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId; import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration; import org.eclipse.cdt.core.parser.util.ArrayUtil; @@ -86,6 +87,9 @@ abstract public class CPPScope implements ICPPScope{ public void addName(IASTName name) { if( bindings == null ) bindings = new CharArrayObjectMap(1); + if( name instanceof ICPPASTTemplateId ) + name = ((ICPPASTTemplateId)name).getTemplateName(); + char [] c = name.toCharArray(); Object o = bindings.get( c ); if( o != null ){ @@ -129,7 +133,7 @@ abstract public class CPPScope implements ICPPScope{ return CPPSemantics.resolveAmbiguities( name, bs ); } else if( obj instanceof IASTName ){ IBinding binding = null; - if( forceResolve && obj != name ) + if( forceResolve && obj != name && obj != name.getParent()) binding = ((IASTName)obj).resolveBinding(); else { IASTName n = (IASTName) obj; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java index 1fcd62e066d..9e3f8cbd03c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java @@ -1274,6 +1274,7 @@ public class CPPSemantics { if( node instanceof ICPPASTQualifiedName ) node = node.getParent(); if( node instanceof ICPPASTFunctionDeclarator ){ + if( binding instanceof CPPFunction ) ((CPPFunction)binding).addDefinition( (ICPPASTFunctionDeclarator) node ); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateDefinition.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateDefinition.java index 2ba0f345ddc..543965110c1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateDefinition.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateDefinition.java @@ -13,6 +13,7 @@ */ package org.eclipse.cdt.internal.core.dom.parser.cpp; +import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; @@ -22,9 +23,19 @@ 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.IScope; +import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; +import org.eclipse.cdt.core.parser.util.ArrayUtil; +import org.eclipse.cdt.core.parser.util.ObjectMap; /** * @author aniefer @@ -75,15 +86,34 @@ public class CPPTemplateDefinition implements ICPPTemplateDefinition { /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplate#instantiate(org.eclipse.cdt.core.dom.ast.IASTNode[]) */ - public IBinding instantiate(IASTNode[] arguments) { - if( arguments != null ) {} - return null; + public IBinding instantiate(ICPPASTTemplateId templateId ) {//IASTNode[] arguments) { + IBinding decl = getTemplatedDeclaration(); + ICPPTemplateParameter [] params = getParameters(); + IASTNode [] arguments = templateId.getTemplateArguments(); + + ObjectMap map = new ObjectMap(params.length); + if( arguments.length == params.length ){ + for( int i = 0; i < arguments.length; i++ ){ + IType t = CPPVisitor.createType( arguments[i] ); + map.put( params[i], t ); + } + } + + return CPPTemplates.createInstance( templateId, (ICPPScope) getScope(), decl, map ); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplate#getTemplatedDeclaration() */ public IBinding getTemplatedDeclaration() { - // TODO Auto-generated method stub + if( primaryDecl instanceof IASTSimpleDeclaration ){ + IASTSimpleDeclaration simple = (IASTSimpleDeclaration) primaryDecl; + if( simple.getDeclarators().length == 0 && simple.getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ){ + IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) simple.getDeclSpecifier(); + return compSpec.getName().resolveBinding(); + } + } else if( primaryDecl instanceof IASTFunctionDefinition ){ + + } return null; } /* (non-Javadoc) @@ -114,8 +144,24 @@ public class CPPTemplateDefinition implements ICPPTemplateDefinition { * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition#getParameters() */ public ICPPTemplateParameter[] getParameters() { - // TODO Auto-generated method stub - return null; + ICPPASTTemplateDeclaration template = (ICPPASTTemplateDeclaration) primaryDecl.getParent(); + ICPPASTTemplateParameter [] params = template.getTemplateParameters(); + ICPPTemplateParameter p = null; + ICPPTemplateParameter [] result = null; + for (int i = 0; i < params.length; i++) { + if( params[i] instanceof ICPPASTSimpleTypeTemplateParameter ){ + p = (ICPPTemplateParameter) ((ICPPASTSimpleTypeTemplateParameter)params[i]).getName().resolveBinding(); + } else if( params[i] instanceof ICPPASTParameterDeclaration ) { + p = (ICPPTemplateParameter) ((ICPPASTParameterDeclaration)params[i]).getDeclarator().getName().resolveBinding(); + } else if( params[i] instanceof ICPPASTTemplatedTypeTemplateParameter ){ + p = (ICPPTemplateParameter) ((ICPPASTTemplatedTypeTemplateParameter)params[i]).getName().resolveBinding(); + } + + if( p != null ){ + result = (ICPPTemplateParameter[]) ArrayUtil.append( ICPPTemplateParameter.class, result, p ); + } + } + return (ICPPTemplateParameter[]) ArrayUtil.trim( ICPPTemplateParameter.class, result ); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateParameter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateParameter.java index a40f04f6e81..954ae8e97c9 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateParameter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateParameter.java @@ -14,15 +14,17 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp; 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.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; /** * @author aniefer */ -public class CPPTemplateParameter implements ICPPTemplateParameter, IType { +public class CPPTemplateParameter implements ICPPTemplateParameter, IType, ICPPInternalBinding { private IASTName [] declarations; public CPPTemplateParameter( IASTName name ){ @@ -100,4 +102,26 @@ public class CPPTemplateParameter implements ICPPTemplateParameter, IType { // TODO Auto-generated method stub return false; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations() + */ + public IASTNode[] getDeclarations() { + return declarations; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition() + */ + public IASTNode getDefinition() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#createDelegate(org.eclipse.cdt.core.dom.ast.IASTName) + */ + public ICPPDelegate createDelegate(IASTName name) { + // TODO Auto-generated method stub + return null; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplates.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplates.java index 49c12781097..bb3b4327a73 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplates.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplates.java @@ -17,15 +17,25 @@ import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IFunctionType; +import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +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.ICPPInstance; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateScope; +import org.eclipse.cdt.core.parser.util.ObjectMap; +import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer; /** * @author aniefer @@ -80,9 +90,9 @@ public class CPPTemplates { public static IBinding createBinding(ICPPASTTemplateId id) { IASTName templateName = id.getTemplateName(); IBinding template = templateName.resolveBinding(); -// if( template != null && template instanceof ICPPTemplateDefinition ){ -// return ((ICPPTemplateDefinition)template).instantiate( id.getTemplateArguments() ); -// } + if( template != null && template instanceof ICPPTemplateDefinition ){ + return ((ICPPTemplateDefinition)template).instantiate( id ); + } return template; } @@ -94,4 +104,67 @@ public class CPPTemplates { if( scope != null ) {} return null; } + + /** + * @param decl + * @param arguments + * @return + */ + public static IBinding createInstance( IASTName id, ICPPScope scope, IBinding decl, ObjectMap argMap) { + ICPPInstance instance = null; + if( decl instanceof ICPPClassType ){ + instance = new CPPClassInstance( id, scope, decl, argMap ); + } else if( decl instanceof ICPPField ){ + instance = new CPPFieldInstance( scope, decl, argMap ); + } else if( decl instanceof ICPPMethod ) { + instance = new CPPMethodInstance( scope, decl, argMap ); + } else if( decl instanceof ICPPFunction ) { + instance = new CPPFunctionInstance( scope, decl, argMap ); + } + return instance; + } + + + /** + * @param type + * @param arguments + */ + public static IType instantiateType(IType type, ObjectMap argMap) { + if( argMap == null ) + return type; + + IType newType = type; + IType temp = null; + if( type instanceof IFunctionType ){ + IType ret = null; + IType [] params = null; + try { + ret = instantiateType( ((IFunctionType) type).getReturnType(), argMap ); + IType [] ps = ((IFunctionType) type).getParameterTypes(); + params = new IType[ ps.length ]; + for (int i = 0; i < params.length; i++) { + temp = instantiateType( ps[i], argMap ); + params[i] = temp; + } + } catch (DOMException e) { + } + newType = new CPPFunctionType( ret, params ); + } else if( type instanceof ITypeContainer ){ + try { + temp = ((ITypeContainer) type).getType(); + } catch (DOMException e) { + return type; + } + newType = instantiateType( temp, argMap ); + if( newType != temp ){ + temp = (IType) type.clone(); + ((ITypeContainer)temp).setType( newType ); + newType = temp; + } + } else if( type instanceof ICPPTemplateParameter && argMap.containsKey( type ) ){ + newType = (IType) argMap.get( type ); + } + + return newType; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index 5743041070a..ce81a5d9a3c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -1204,6 +1204,7 @@ public class CPPVisitor { return new CPPFunctionType( returnType, pTypes ); } + private static IType createType( IType returnType, ICPPASTFunctionDeclarator fnDtor ){ IASTParameterDeclaration [] params = fnDtor.getParameters(); IType [] pTypes = new IType [ params.length ]; @@ -1291,6 +1292,13 @@ public class CPPVisitor { return type; } + public static IType createType( IASTNode node ){ + if( node instanceof IASTExpression ) + return getExpressionType( (IASTExpression) node ); + if( node instanceof IASTTypeId ) + return createType( ((IASTTypeId) node).getAbstractDeclarator() ); + return null; + } /** * @param declarator * @return