Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Computing return type for auto fold-expressions

Hi list

I‘m currently implementing support for the upcoming fold-_expression_. Small example:

template<typename... Args>
auto sum_fold(Args... args) { return (... + args); }

int main() { sum_fold(4,8,15,16,23,42); }
Which returns 108.

The parsing part is already done. Now I‘m implementing an ICPPEvaluation to compute the return type if it is auto. It was trivial for the above example with ICPPBasicType instances where I just use with CPPArithmeticConversion.convertCppOperandTypes(fOperator, type1, type2). However, if I have own types with overloaded operators it gets more complex.

template<typename... Args>
auto sum_fold(Args... args) { return (... + args); }

struct T1 {}t1; struct T2 {}t2; struct T3 {}t3; struct T4 {}t4; struct T5 {}t5;

T3 operator+(T1, T2) { return t3; } T5 operator+(T3, T4) { return t5; }

int main() {
auto foo = sum_fold(t1,t2,t4); // return type is T5
}
Looking at EvalBinary I saw useful method calls that I could also use. I get the overloaded operator with CPPSemantics.findOverloadedBinaryOperator(point, getTemplateDefinitionScope(), op, eval1, eval2); and then get the return type of that overload with ExpressionTypes.typeFromFunctionCall(overload);
This works for the first two arguments t1 and t2 and I get T3 as return type. I should then get the overload for T3+T4 where I am stuck now because findOverloadedBinaryOperator needs an evaluation that I don‘t have. I thought about just creating an EvalBinding that I could pass but for that an actual binding is obviously needed. For that I could get the return statement from the operator+ (T1, T2) that I just got before. However, there could be multiple return statements so I should better get the evaluation for the function call to this operator+(T1, T2) which also does not exist.

So my question is, how I best find the correct overloaded operator for T3+T4 then to finally get my needed type T5. Do I create a EvalFunctionCall for my t1+t2 so I can get the EvalBinding for t3? Is there something else that I‘m missing and could be done to make this easier?

Back to the top