mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 17:05:26 +02:00
Bug 302412: Direct list initialization.
This commit is contained in:
parent
477c241751
commit
286e04711a
5 changed files with 31 additions and 24 deletions
|
@ -8110,7 +8110,7 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
// fs({1,2,3});
|
||||
// fs({1.0,2.0,3.0});
|
||||
// }
|
||||
public void _testListInitialization_302412a() throws Exception {
|
||||
public void testListInitialization_302412a() throws Exception {
|
||||
String code= getAboveComment();
|
||||
parseAndCheckBindings(code, ParserLanguage.CPP);
|
||||
}
|
||||
|
|
|
@ -528,21 +528,23 @@ public class CPPSemantics {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static IBinding selectConstructor(ICPPClassType classTarget, ICPPASTInitializerList initializerList) {
|
||||
LookupData data= new LookupData();
|
||||
IASTName name = new CPPASTName(classTarget.getNameCharArray());
|
||||
name.setParent(initializerList);
|
||||
name.setPropertyInParent(CPPSemantics.STRING_LOOKUP_PROPERTY);
|
||||
data.setFunctionArguments(initializerList);
|
||||
try {
|
||||
return CPPSemantics.selectConstructor(classTarget, data);
|
||||
} catch (DOMException e) {
|
||||
return e.getProblem();
|
||||
}
|
||||
}
|
||||
|
||||
private static IBinding selectConstructor(ICPPClassType cls, LookupData data) throws DOMException {
|
||||
// Force resolution of constructor bindings
|
||||
final IType[] types = data.getFunctionArgumentTypes();
|
||||
if (types != null && types.length == 1 && types[0] instanceof InitializerListType) {
|
||||
Cost cost= Conversions.listInitializationSequence((InitializerListType) types[0], cls, UDCMode.allowUDC, true);
|
||||
if (cost.converts()) {
|
||||
if (cost.isAmbiguousUDC()) {
|
||||
return new ProblemBinding(data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, cls.getConstructors());
|
||||
}
|
||||
IBinding result= cost.getUserDefinedConversion();
|
||||
if (result == null) {
|
||||
return cls;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return new ProblemBinding(data.astName, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, cls.getConstructors());
|
||||
}
|
||||
|
||||
final ICPPConstructor[] constructors= cls.getConstructors();
|
||||
if (constructors.length > 0) {
|
||||
data.foundItems= constructors;
|
||||
|
|
|
@ -272,7 +272,7 @@ public class Conversions {
|
|||
|
||||
private static Cost nonReferenceConversion(boolean sourceIsLValue, IType source, IType target, UDCMode udc, boolean isImpliedObject) throws DOMException {
|
||||
if (source instanceof InitializerListType) {
|
||||
return listInitializationSequence(((InitializerListType) source), target, udc);
|
||||
return listInitializationSequence(((InitializerListType) source), target, udc, false);
|
||||
}
|
||||
// [13.3.3.1-6] Subsume cv-qualifications
|
||||
IType uqSource= SemanticUtil.getNestedType(source, TDEF | ALLCVQ);
|
||||
|
@ -280,13 +280,13 @@ public class Conversions {
|
|||
if (cost.converts() || udc == UDCMode.noUDC)
|
||||
return cost;
|
||||
|
||||
return checkUserDefinedConversionSequence(sourceIsLValue, source, target, udc == UDCMode.deferUDC);
|
||||
return checkUserDefinedConversionSequence(sourceIsLValue, source, target, udc == UDCMode.deferUDC, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 13.3.3.1.5 List-initialization sequence [over.ics.list]
|
||||
*/
|
||||
private static Cost listInitializationSequence(InitializerListType arg, IType target, UDCMode udc) throws DOMException {
|
||||
static Cost listInitializationSequence(InitializerListType arg, IType target, UDCMode udc, boolean isDirect) throws DOMException {
|
||||
IType listType= getInitListType(target);
|
||||
if (listType != null) {
|
||||
IType[] exprTypes= arg.getExpressionTypes();
|
||||
|
@ -319,7 +319,7 @@ public class Conversions {
|
|||
cost.setUserDefinedConversion(null);
|
||||
return cost;
|
||||
}
|
||||
return checkUserDefinedConversionSequence(false, arg, target, udc == UDCMode.deferUDC);
|
||||
return checkUserDefinedConversionSequence(false, arg, target, udc == UDCMode.deferUDC, isDirect);
|
||||
}
|
||||
|
||||
IASTInitializerClause[] args = arg.getInitializerList().getClauses();
|
||||
|
@ -508,7 +508,7 @@ public class Conversions {
|
|||
/**
|
||||
* [13.3.3.1.2] User-defined conversions
|
||||
*/
|
||||
static final Cost checkUserDefinedConversionSequence(boolean sourceIsLValue, IType source, IType target, boolean deferUDC) throws DOMException {
|
||||
static final Cost checkUserDefinedConversionSequence(boolean sourceIsLValue, IType source, IType target, boolean deferUDC, boolean isDirect) throws DOMException {
|
||||
IType s= getNestedType(source, TDEF | CVTYPE | REF);
|
||||
IType t= getNestedType(target, TDEF | CVTYPE | REF);
|
||||
|
||||
|
@ -525,7 +525,7 @@ public class Conversions {
|
|||
if (t instanceof ICPPClassType) {
|
||||
if (s instanceof InitializerListType) {
|
||||
// 13.3.1.7 Initialization by list-initialization
|
||||
return listInitializationOfClass((InitializerListType) s, (ICPPClassType) t, false);
|
||||
return listInitializationOfClass((InitializerListType) s, (ICPPClassType) t, isDirect);
|
||||
}
|
||||
// 13.3.1.4 Copy initialization of class by user-defined conversion
|
||||
return copyInitalizationOfClass(sourceIsLValue, source, s, target, (ICPPClassType) t);
|
||||
|
@ -552,7 +552,7 @@ public class Conversions {
|
|||
final IType target = parTypes[0];
|
||||
if (getInitListType(target) != null) {
|
||||
hasInitListConstructor= true;
|
||||
Cost cost= listInitializationSequence(arg, target, UDCMode.noUDC);
|
||||
Cost cost= listInitializationSequence(arg, target, UDCMode.noUDC, isDirect);
|
||||
if (cost.converts()) {
|
||||
int cmp= cost.compareTo(bestCost);
|
||||
if (bestCost == null || cmp < 0) {
|
||||
|
|
|
@ -150,6 +150,10 @@ class Cost {
|
|||
fCouldNarrow= false;
|
||||
}
|
||||
|
||||
public ICPPFunction getUserDefinedConversion() {
|
||||
return fUserDefinedConversion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer < 0 if other cost is <code>null</code>, or this cost is smaller than the other cost,
|
||||
* 0 if this cost is equal to the other cost,
|
||||
|
|
|
@ -77,8 +77,9 @@ class FunctionCost {
|
|||
for (int i = 0; i < fCosts.length; i++) {
|
||||
Cost cost = fCosts[i];
|
||||
if (cost.isDeferredUDC()) {
|
||||
Cost udcCost= Conversions.checkUserDefinedConversionSequence(fSourceIsLValue.get(i), cost.source, cost.target, false);
|
||||
fCosts[i]= udcCost;
|
||||
Cost udcCost = Conversions.checkUserDefinedConversionSequence(fSourceIsLValue.get(i),
|
||||
cost.source, cost.target, false, false);
|
||||
fCosts[i] = udcCost;
|
||||
if (!udcCost.converts()) {
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue