1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug fix for handling of default template parameters. See AST2TemplateTest#testDefaultTemplateParameter()

This commit is contained in:
Sergey Prigogin 2008-01-28 04:04:06 +00:00
parent 8146a961b8
commit 31f3a10fe2
3 changed files with 62 additions and 33 deletions

View file

@ -20,6 +20,7 @@ import java.util.Iterator;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement; import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
@ -2101,32 +2102,23 @@ public class AST2TemplateTests extends AST2BaseTest {
assertTrue(name.resolveBinding() instanceof IParameter); assertTrue(name.resolveBinding() instanceof IParameter);
} }
// template<typename _Tp> // template<typename _TpAllocator>
// class Allocator { // class Allocator {
// public: // public:
// typedef _Tp& alloc_reference; // typedef _TpAllocator& alloc_reference;
// template<typename _Tp1> // template<typename _TpRebind>
// struct rebind { // struct rebind {
// typedef Allocator<_Tp1> other; // typedef Allocator<_TpRebind> other;
// }; // };
// }; // };
// //
// template<typename _Tp, typename _Alloc>
// class VecBase {
// public:
// typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
// };
//
// template<typename _Tp, typename _Alloc = Allocator<_Tp> > // template<typename _Tp, typename _Alloc = Allocator<_Tp> >
// class Vec : protected VecBase<_Tp, _Alloc> { // class Vec {
// public: // public:
// typedef typename VecBase<_Tp, _Alloc>::_Tp_alloc_type::alloc_reference reference; // typedef typename _Alloc::template rebind<_Tp>::other::alloc_reference reference;
// }; // };
// //
// class A {}; // void f(Vec<int>::reference r) {}
//
// void f(Vec<A>::reference r) {
// }
public void _testRebindPattern_214447() throws Exception { public void _testRebindPattern_214447() throws Exception {
StringBuffer buffer = getContents(1)[0]; StringBuffer buffer = getContents(1)[0];
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP, true, true); IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP, true, true);
@ -2134,13 +2126,42 @@ public class AST2TemplateTests extends AST2BaseTest {
CPPNameCollector col = new CPPNameCollector(); CPPNameCollector col = new CPPNameCollector();
tu.accept(col); tu.accept(col);
for (IASTName name : col.nameList) { for (IASTName name : col.nameList) {
IBinding b0 = name.resolveBinding();
System.out.println(name.toString() + ": " + b0.getClass().getName());
if ("r".equals(String.valueOf(name))) { if ("r".equals(String.valueOf(name))) {
IBinding b0 = name.resolveBinding();
IType type = ((ICPPVariable) b0).getType(); IType type = ((ICPPVariable) b0).getType();
type = CPPSemantics.getUltimateType(type, false); type = CPPSemantics.getUltimateType(type, false);
assertInstance(type, ICPPClassType.class); assertInstance(type, IBasicType.class);
assertEquals("A", ((ICPPClassType) type).getName()); assertEquals("int", ASTTypeUtil.getType(type));
}
}
}
// template<typename _Tp>
// class A {
// public:
// typedef _Tp a;
// };
//
// template<typename _Tp1, typename _Tp2 = A<_Tp1> >
// class B {
// public:
// typedef _Tp2 b;
// };
//
// B<int>::b::a x;
public void testDefaultTemplateParameter() throws Exception {
StringBuffer buffer = getContents(1)[0];
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP, true, true);
CPPNameCollector col = new CPPNameCollector();
tu.accept(col);
for (IASTName name : col.nameList) {
if ("x".equals(String.valueOf(name))) {
IBinding b0 = name.resolveBinding();
IType type = ((ICPPVariable) b0).getType();
type = CPPSemantics.getUltimateType(type, false);
assertInstance(type, IBasicType.class);
assertEquals("int", ASTTypeUtil.getType(type));
} }
} }
} }

View file

@ -32,15 +32,15 @@ import org.eclipse.core.runtime.PlatformObject;
public class CPPTemplateParameter extends PlatformObject implements ICPPTemplateParameter, ICPPInternalBinding { public class CPPTemplateParameter extends PlatformObject implements ICPPTemplateParameter, ICPPInternalBinding {
private IASTName [] declarations; private IASTName [] declarations;
public CPPTemplateParameter( IASTName name ){ public CPPTemplateParameter(IASTName name) {
declarations = new IASTName[] { name }; declarations = new IASTName[] { name };
} }
public Object clone(){ public Object clone() {
IType t = null; IType t = null;
try { try {
t = (IType) super.clone(); t = (IType) super.clone();
} catch ( CloneNotSupportedException e ) { } catch (CloneNotSupportedException e) {
//not going to happen //not going to happen
} }
return t; return t;
@ -67,7 +67,7 @@ public class CPPTemplateParameter extends PlatformObject implements ICPPTemplate
* @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
*/ */
public IScope getScope() { public IScope getScope() {
return CPPVisitor.getContainingScope( getPrimaryDeclaration () ); return CPPVisitor.getContainingScope(getPrimaryDeclaration ());
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -103,7 +103,7 @@ public class CPPTemplateParameter extends PlatformObject implements ICPPTemplate
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition() * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition()
*/ */
public IASTNode getDefinition() { public IASTNode getDefinition() {
if( declarations != null && declarations.length > 0 ) if (declarations != null && declarations.length > 0)
return declarations[0]; return declarations[0];
return null; return null;
} }
@ -120,26 +120,26 @@ public class CPPTemplateParameter extends PlatformObject implements ICPPTemplate
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDefinition(org.eclipse.cdt.core.dom.ast.IASTNode) * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDefinition(org.eclipse.cdt.core.dom.ast.IASTNode)
*/ */
public void addDefinition(IASTNode node) { public void addDefinition(IASTNode node) {
addDeclaration( node ); addDeclaration(node);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDeclaration(org.eclipse.cdt.core.dom.ast.IASTNode) * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDeclaration(org.eclipse.cdt.core.dom.ast.IASTNode)
*/ */
public void addDeclaration(IASTNode node) { public void addDeclaration(IASTNode node) {
if( !(node instanceof IASTName) ) if (!(node instanceof IASTName))
return; return;
IASTName name = (IASTName) node; IASTName name = (IASTName) node;
if( declarations == null ) if (declarations == null) {
declarations = new IASTName[] { name }; declarations = new IASTName[] { name };
else { } else {
if( declarations.length > 0 && declarations[0] == node ) if (declarations.length > 0 && declarations[0] == node)
return; return;
//keep the lowest offset declaration in [0] //keep the lowest offset declaration in [0]
if( declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset() ){ if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) {
declarations = (IASTName[]) ArrayUtil.prepend( IASTName.class, declarations, name ); declarations = (IASTName[]) ArrayUtil.prepend(IASTName.class, declarations, name);
} else { } else {
declarations = (IASTName[]) ArrayUtil.append( IASTName.class, declarations, name ); declarations = (IASTName[]) ArrayUtil.append(IASTName.class, declarations, name);
} }
} }
} }
@ -150,4 +150,8 @@ public class CPPTemplateParameter extends PlatformObject implements ICPPTemplate
public ILinkage getLinkage() { public ILinkage getLinkage() {
return Linkage.CPP_LINKAGE; return Linkage.CPP_LINKAGE;
} }
public String toString() {
return getName();
}
} }

View file

@ -1618,6 +1618,10 @@ public class CPPTemplates {
if( map.containsKey( defaultType ) ){ if( map.containsKey( defaultType ) ){
arg = (IType) map.get( defaultType ); arg = (IType) map.get( defaultType );
} }
} else if (defaultType instanceof ICPPInternalDeferredClassInstance) {
// Default template parameter may be depend on a previously defined
// parameter: template<typename T1, typename T2 = A<T1> > class B {};
arg = ((ICPPInternalDeferredClassInstance) defaultType).instantiate(map);
} else { } else {
arg = defaultType; arg = defaultType;
} }