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

Bug 245049. Also fixed test14_8_2_4s7.

This commit is contained in:
Sergey Prigogin 2008-08-25 00:39:02 +00:00
parent bc2382f2ca
commit e9629b194e
4 changed files with 105 additions and 58 deletions

View file

@ -163,16 +163,6 @@ public class AST2CPPSpecFailingTest extends AST2SpecBaseTest {
parse(getAboveComment(), ParserLanguage.CPP, true, 0);
}
// template<class T> void f(const T*) {}
// int *p;
// void s()
// {
// f(p); // f(const int *)
// }
public void _test14_8_2_4s7() throws Exception {
parse(getAboveComment(), ParserLanguage.CPP, true, 0);
}
// template <class T> struct B { };
// template <class T> struct D : public B<T> {};
// struct D2 : public B<int> {};

View file

@ -5482,6 +5482,16 @@ public class AST2CPPSpecTest extends AST2SpecBaseTest {
parse(getAboveComment(), ParserLanguage.CPP, false, 0);
}
// template<class T> void f(const T*) {}
// int *p;
// void s()
// {
// f(p); // f(const int *)
// }
public void test14_8_2_4s7() throws Exception {
parse(getAboveComment(), ParserLanguage.CPP, true, 0);
}
// template<class T, T i> void f(double a[10][i]);
// int v[10][20];
// int foo() {

View file

@ -2160,8 +2160,8 @@ public class AST2TemplateTests extends AST2BaseTest {
// struct A {};
//
// template <class T>
// void func(const T& p) {
// template <class T1>
// void func(const T1& p) {
// }
//
// void test() {
@ -2170,12 +2170,35 @@ public class AST2TemplateTests extends AST2BaseTest {
// func(a1);
// func(a2);
// }
public void _testFunctionTemplate_245049() throws Exception {
public void testFunctionTemplate_245049_1() throws Exception {
BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), true);
ICPPFunction b0= bh.assertNonProblem("func(a1)", 4, ICPPFunction.class);
assertInstance(b0, ICPPTemplateInstance.class);
ICPPFunction b1= bh.assertNonProblem("func(a2)", 4, ICPPFunction.class);
assertEquals(b0, b1);
assertSame(b0, b1);
}
// struct A {};
//
// template <class T1>
// void func(const T1& p) {
// }
// template <class T2>
// void func(T2& p) {
// }
//
// void test() {
// A a1;
// const A a2;
// func(a1);
// func(a2);
// }
public void testFunctionTemplate_245049_2() throws Exception {
BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), true);
ICPPFunction b0= bh.assertNonProblem("func(a1)", 4, ICPPFunction.class);
assertInstance(b0, ICPPTemplateInstance.class);
ICPPFunction b1= bh.assertNonProblem("func(a2)", 4, ICPPFunction.class);
assertNotSame(b0, b1);
}
// namespace ns {

View file

@ -1550,7 +1550,8 @@ public class CPPTemplates {
if (p instanceof IBasicType) {
if (a instanceof IBasicType) {
IBasicType pbt= (IBasicType) p, abt= (IBasicType) a;
IBasicType pbt= (IBasicType) p;
IBasicType abt= (IBasicType) a;
// non-type argument comparison
if (pbt.getValue() != null && abt.getValue() != null) {
@ -1570,10 +1571,10 @@ public class CPPTemplates {
} else if (p instanceof ICPPPointerToMemberType) {
if (!(a instanceof ICPPPointerToMemberType))
return false;
if (!deduceTemplateArgument(map, ((ICPPPointerToMemberType) p).getMemberOfClass(), ((ICPPPointerToMemberType) a).getMemberOfClass()))
if (!deduceTemplateArgument(map, ((ICPPPointerToMemberType) p).getMemberOfClass(),
((ICPPPointerToMemberType) a).getMemberOfClass())) {
return false;
}
p = ((ICPPPointerToMemberType) p).getType();
a = ((ICPPPointerToMemberType) a).getType();
} else if (p instanceof IPointerType) {
@ -1583,15 +1584,17 @@ public class CPPTemplates {
p = ((IPointerType) p).getType();
a = ((IPointerType) a).getType();
} else if (p instanceof IQualifierType) {
if (!(a instanceof IQualifierType))
return false;
if (a instanceof IQualifierType) {
a = ((IQualifierType) a).getType(); //TODO a = strip qualifiers from p out of a
}
p = ((IQualifierType) p).getType();
} else if (p instanceof IFunctionType) {
if (!(a instanceof IFunctionType))
return false;
if (!deduceTemplateArgument(map, ((IFunctionType) p).getReturnType(), ((IFunctionType) a).getReturnType()))
if (!deduceTemplateArgument(map, ((IFunctionType) p).getReturnType(),
((IFunctionType) a).getReturnType())) {
return false;
}
IType[] pParams = ((IFunctionType) p).getParameterTypes();
IType[] aParams = ((IFunctionType) a).getParameterTypes();
if (pParams.length != aParams.length)
@ -1653,7 +1656,7 @@ public class CPPTemplates {
}
/**
* transform a function template for use in partial ordering, as described in the
* Transforms a function template for use in partial ordering, as described in the
* spec 14.5.5.2-3
* @param template
* @return
@ -1665,7 +1668,8 @@ public class CPPTemplates {
* for each occurrence of that parameter in the function parameter list
* @throws DOMException
*/
static private IType[] createArgsForFunctionTemplateOrdering(ICPPFunctionTemplate template) throws DOMException{
static private IType[] createArgsForFunctionTemplateOrdering(ICPPFunctionTemplate template)
throws DOMException{
ICPPTemplateParameter[] paramList = template.getTemplateParameters();
int size = paramList.length;
IType[] args = new IType[size];
@ -1687,11 +1691,11 @@ public class CPPTemplates {
return args;
}
static protected int orderTemplateFunctions(ICPPFunctionTemplate f1, ICPPFunctionTemplate f2) throws DOMException {
static protected int orderTemplateFunctions(ICPPFunctionTemplate f1, ICPPFunctionTemplate f2)
throws DOMException {
// Using the transformed parameter list, perform argument deduction against the other
// function template
ObjectMap m1 = null;
ObjectMap m2 = null;
if (f1 != null) {
IType[] args = createArgsForFunctionTemplateOrdering(f1);
IBinding function = instantiate(f1, args);
@ -1699,6 +1703,7 @@ public class CPPTemplates {
m1 = deduceTemplateArguments(f2, ((ICPPFunction) function).getType().getParameterTypes());
}
ObjectMap m2 = null;
if (f2 != null) {
IType[] args = createArgsForFunctionTemplateOrdering(f2);
IBinding function = instantiate(f2, args);
@ -1707,21 +1712,40 @@ public class CPPTemplates {
m2 = deduceTemplateArguments(f1, ((ICPPFunction) function).getType().getParameterTypes());
}
// The transformed template is at least as specialized as the other iff the deduction
//succeeds and the deduced parameter types are an exact match
// succeeds and the deduced parameter types are an exact match.
// A template is more specialized than another iff it is at least as specialized as the
// other template and that template is not at least as specialized as the first.
boolean d1 = (m1 != null);
boolean d2 = (m2 != null);
if (d1 && d2 || !d1 && !d2)
if (m1 == null) {
if (m2 == null) {
return 0;
else if (d1 && !d2)
return 1;
else
} else {
return -1;
}
} else {
if (m2 == null) {
return 1;
} else {
// Count the number of cv-qualifications. The function with a lower number
// of cv-qualifications is more specialized.
int d1 = 0;
for (int i = 0; i < m1.size(); i++) {
if (m1.getAt(i) instanceof IQualifierType) {
d1++;
}
}
int d2 = 0;
for (int i = 0; i < m2.size(); i++) {
if (m2.getAt(i) instanceof IQualifierType) {
d2++;
}
}
return d1 - d2;
}
}
}
static public ICPPTemplateDefinition selectSpecialization(ICPPClassTemplate template, IType[] args) throws DOMException{
static public ICPPTemplateDefinition selectSpecialization(ICPPClassTemplate template, IType[] args)
throws DOMException {
if (template == null) {
return null;
}