1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-30 03:33:37 +02:00

Bug 296427: Api to distinguish explicit specializatio

This commit is contained in:
Markus Schorn 2010-03-08 10:24:16 +00:00
parent 84a7310e5a
commit 492d8e61e0
14 changed files with 135 additions and 22 deletions

View file

@ -4835,4 +4835,32 @@ public class AST2TemplateTests extends AST2BaseTest {
assertSame(ct, inst.getSpecializedBinding()); assertSame(ct, inst.getSpecializedBinding());
} }
// template<typename T> class X {};
// template<typename T> class Y {};
// template<> class Y<int> {};
// template<typename T> void f(T t) {}
// template<typename T> void g(T t) {}
// template<> void g(int t) {}
// void test() {
// X<int> x;
// Y<int> y;
// f(1);
// g(1);
// }
public void testExplicitSpecializations_296427() throws Exception {
final String code= getAboveComment();
BindingAssertionHelper bh= new BindingAssertionHelper(code, true);
ICPPTemplateInstance inst;
inst= bh.assertNonProblem("X<int>", 0);
assertFalse(inst.isExplicitSpecialization());
inst = bh.assertNonProblem("Y<int> y;", 6);
assertTrue(inst.isExplicitSpecialization());
inst = bh.assertNonProblem("f(1)", 1);
assertFalse(inst.isExplicitSpecialization());
inst = bh.assertNonProblem("g(1)", 1);
assertTrue(inst.isExplicitSpecialization());
}
} }

View file

@ -117,6 +117,9 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
* @see IndexBindingResolutionTestBase#getBindingFromASTName(Class, String, int) * @see IndexBindingResolutionTestBase#getBindingFromASTName(Class, String, int)
*/ */
protected <T extends IBinding> T getBindingFromASTName(String section, int len) { protected <T extends IBinding> T getBindingFromASTName(String section, int len) {
if (len <= 0)
len+= section.length();
IASTName name= findName(section, len); IASTName name= findName(section, len);
assertNotNull("name not found for \""+section+"\"", name); assertNotNull("name not found for \""+section+"\"", name);
assertEquals(section.substring(0, len), name.getRawSignature()); assertEquals(section.substring(0, len), name.getRawSignature());

View file

@ -1721,4 +1721,29 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
assertEquals("T", ct.getTemplateParameters()[0].getName()); assertEquals("T", ct.getTemplateParameters()[0].getName());
} }
// template<typename T> class X {};
// template<typename T> class Y {};
// template<> class Y<int> {};
// template<typename T> void f(T t) {}
// template<typename T> void g(T t) {}
// template<> void g(int t) {}
// void test() {
// X<int> x;
// Y<int> y;
// f(1);
// g(1);
// }
public void testExplicitSpecializations_296427() throws Exception {
ICPPTemplateInstance inst;
inst= getBindingFromASTName("X<int>", 0);
assertFalse(inst.isExplicitSpecialization());
inst = getBindingFromASTName("Y<int>", 0);
assertTrue(inst.isExplicitSpecialization());
inst = getBindingFromASTName("f(1)", 1);
assertFalse(inst.isExplicitSpecialization());
inst = getBindingFromASTName("g(1)", 1);
assertTrue(inst.isExplicitSpecialization());
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2009 IBM Corporation and others. * Copyright (c) 2005, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * IBM - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast.cpp; package org.eclipse.cdt.core.dom.ast.cpp;
@ -42,7 +43,14 @@ public interface ICPPTemplateInstance extends ICPPSpecialization {
public ICPPTemplateArgument[] getTemplateArguments(); public ICPPTemplateArgument[] getTemplateArguments();
/** /**
* @deprecated use {@link #getTemplateArguments()}, instead. * Explicit specializations are modeled as instances of a template.
* Returns <code>true</code> if this binding is an explicit specialization.
* @since 5.2
*/
public boolean isExplicitSpecialization();
/**
* @deprecated Replaced by {@link #getTemplateArguments()}.
*/ */
@Deprecated @Deprecated
public IType[] getArguments(); public IType[] getArguments();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2009 IBM Corporation and others. * Copyright (c) 2005, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -15,6 +15,7 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
@ -45,6 +46,14 @@ public class CPPClassInstance extends CPPClassSpecialization implements ICPPTemp
return arguments; return arguments;
} }
public boolean isExplicitSpecialization() {
try {
return !(getCompositeScope() instanceof ICPPClassSpecializationScope);
} catch (DOMException e) {
return false;
}
}
@Deprecated @Deprecated
public IType[] getArguments() { public IType[] getArguments() {
return CPPTemplates.getArguments(getTemplateArguments()); return CPPTemplates.getArguments(getTemplateArguments());

View file

@ -62,6 +62,10 @@ public class CPPDeferredClassInstance extends CPPUnknownClass implements ICPPDef
return (ICPPClassTemplate) getSpecializedBinding(); return (ICPPClassTemplate) getSpecializedBinding();
} }
public boolean isExplicitSpecialization() {
return false;
}
@Override @Override
public CPPDeferredClassInstance clone() { public CPPDeferredClassInstance clone() {
CPPDeferredClassInstance cloned= (CPPDeferredClassInstance) super.clone(); CPPDeferredClassInstance cloned= (CPPDeferredClassInstance) super.clone();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2009 IBM Corporation and others. * Copyright (c) 2005, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
@ -47,6 +48,19 @@ public class CPPFunctionInstance extends CPPFunctionSpecialization implements IC
return fArguments; return fArguments;
} }
public boolean isExplicitSpecialization() {
if (getDefinition() != null)
return true;
IASTNode[] decls = getDeclarations();
if (decls != null) {
for (IASTNode decl : decls) {
if (decl != null)
return true;
}
}
return false;
}
/* (non-Javadoc) /* (non-Javadoc)
* For debug purposes only * For debug purposes only
*/ */

View file

@ -129,7 +129,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownClass;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownClassInstance; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownFunction; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPASTInternalTemplateDeclaration; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPASTInternalTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPClassSpecializationScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInstanceCache; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInstanceCache;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
@ -1279,17 +1278,12 @@ public class CPPTemplates {
if (j++ == missingTemplateDecls) { if (j++ == missingTemplateDecls) {
IBinding b= n.resolveBinding(); IBinding b= n.resolveBinding();
if (b instanceof ICPPTemplateInstance && b instanceof ICPPClassType) { if (b instanceof ICPPTemplateInstance && b instanceof ICPPClassType) {
try { if (((ICPPTemplateInstance) b).isExplicitSpecialization()) {
IScope s= ((ICPPClassType) b).getCompositeScope(); // For a template-id of an explicit specialization.
if (!(s instanceof ICPPClassSpecializationScope)) { // we don't have a template declaration. (see 14.7.3.5)
// template-id of an explicit specialization.
// here we don't have a template declaration. (see 14.7.3.5)
missingTemplateDecls++; missingTemplateDecls++;
lastIsTemplate= true; lastIsTemplate= true;
} }
} catch (DOMException e) {
// assume that it is not an explicit instance
}
} }
break; break;
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 Symbian Software Systems and others. * Copyright (c) 2007, 2010 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -38,6 +38,10 @@ public class CompositeCPPClassInstance extends CompositeCPPClassSpecialization i
return TemplateInstanceUtil.getTemplateParameterMap(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getTemplateParameterMap(cf, (ICPPTemplateInstance) rbinding);
} }
public boolean isExplicitSpecialization() {
return ((ICPPTemplateInstance) rbinding).isExplicitSpecialization();
}
@Deprecated @Deprecated
public IType[] getArguments() { public IType[] getArguments() {
return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 Symbian Software Systems and others. * Copyright (c) 2007, 2010 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -76,6 +76,10 @@ public class CompositeCPPDeferredClassInstance extends CompositeCPPClassType imp
return TemplateInstanceUtil.getTemplateArguments(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getTemplateArguments(cf, (ICPPTemplateInstance) rbinding);
} }
public boolean isExplicitSpecialization() {
return false;
}
@Deprecated @Deprecated
public IType[] getArguments() { public IType[] getArguments() {
return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 Symbian Software Systems and others. * Copyright (c) 2007, 2010 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -43,6 +43,10 @@ public class CompositeCPPFunctionInstance extends CompositeCPPFunction implement
return TemplateInstanceUtil.getTemplateParameterMap(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getTemplateParameterMap(cf, (ICPPTemplateInstance) rbinding);
} }
public boolean isExplicitSpecialization() {
return ((ICPPTemplateInstance) rbinding).isExplicitSpecialization();
}
@Deprecated @Deprecated
public IType[] getArguments() { public IType[] getArguments() {
return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding); return TemplateInstanceUtil.getArguments(cf, (ICPPTemplateInstance) rbinding);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2009 QNX Software Systems and others. * Copyright (c) 2007, 2010 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; 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.ICPPTemplateInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassInstance; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPClassSpecializationScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants; import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
@ -76,6 +77,10 @@ class PDOMCPPClassInstance extends PDOMCPPClassSpecialization implements ICPPTem
} }
} }
public boolean isExplicitSpecialization() {
return !(getCompositeScope() instanceof ICPPClassSpecializationScope);
}
@Override @Override
public boolean isSameType(IType type) { public boolean isSameType(IType type) {
if (type instanceof ITypedef) { if (type instanceof ITypedef) {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2009 QNX Software Systems and others. * Copyright (c) 2007, 2010 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -81,6 +81,10 @@ class PDOMCPPDeferredClassInstance extends PDOMCPPSpecialization implements ICPP
return IIndexCPPBindingConstants.CPP_DEFERRED_CLASS_INSTANCE; return IIndexCPPBindingConstants.CPP_DEFERRED_CLASS_INSTANCE;
} }
public boolean isExplicitSpecialization() {
return false;
}
public IScope getCompositeScope() throws DOMException { public IScope getCompositeScope() throws DOMException {
return asScope(); return asScope();
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2009 QNX Software Systems and others. * Copyright (c) 2007, 2010 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -70,11 +70,18 @@ class PDOMCPPFunctionInstance extends PDOMCPPFunctionSpecialization implements I
return IIndexCPPBindingConstants.CPP_FUNCTION_INSTANCE; return IIndexCPPBindingConstants.CPP_FUNCTION_INSTANCE;
} }
public ICPPTemplateDefinition getTemplateDefinition() { public ICPPTemplateDefinition getTemplateDefinition() {
return (ICPPTemplateDefinition) getSpecializedBinding(); return (ICPPTemplateDefinition) getSpecializedBinding();
} }
public boolean isExplicitSpecialization() {
try {
return hasDefinition();
} catch (CoreException e) {
return false;
}
}
public ICPPTemplateArgument[] getTemplateArguments() { public ICPPTemplateArgument[] getTemplateArguments() {
try { try {
final long rec= getPDOM().getDB().getRecPtr(record+ARGUMENTS); final long rec= getPDOM().getDB().getRecPtr(record+ARGUMENTS);