Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 393c1f7c3722e47b952c2b2dd079e652cd2faa98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Markus Schorn - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.XVALUE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.prvalueType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.CVTYPE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.REF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getNestedType;

import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPArithmeticConversion;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPReferenceType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.Context;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.UDCMode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Cost.Rank;
import org.eclipse.core.runtime.CoreException;

/**
 * Performs evaluation of an expression.
 */
public class EvalConditional extends CPPEvaluation {
	private final ICPPEvaluation fCondition, fPositive, fNegative;
	private final boolean fPositiveThrows, fNegativeThrows;

	private ValueCategory fValueCategory;
	private IType fType;
	private ICPPFunction fOverload;

	public EvalConditional(ICPPEvaluation condition, ICPPEvaluation positive, ICPPEvaluation negative,
			boolean positiveThrows, boolean negativeThrows) {
    	// Gnu-extension: Empty positive expression is replaced by condition.
		fCondition= condition;
		fPositive= positive;
		fNegative= negative;
		fPositiveThrows= positiveThrows;
		fNegativeThrows= negativeThrows;
	}

	public ICPPEvaluation getCondition() {
		return fCondition;
	}

	public ICPPEvaluation getPositive() {
		return fPositive;
	}

	public ICPPEvaluation getNegative() {
		return fNegative;
	}

	public boolean isPositiveThrows() {
		return fPositiveThrows;
	}

	public boolean isNegativeThrows() {
		return fNegativeThrows;
	}

	@Override
	public boolean isInitializerList() {
		return false;
	}

	@Override
	public boolean isFunctionSet() {
		return false;
	}

	public ICPPFunction getOverload(IASTNode point) {
		evaluate(point);
		return fOverload;
	}

	@Override
	public IType getTypeOrFunctionSet(IASTNode point) {
		evaluate(point);
		return fType;
	}

	@Override
	public IValue getValue(IASTNode point) {
		IValue condValue = fCondition.getValue(point);
		if (condValue == Value.UNKNOWN)
			return Value.UNKNOWN;
		Long cond = condValue.numericalValue();
		if (cond != null) {
			if (cond.longValue() != 0) {
				return fPositive == null ? condValue : fPositive.getValue(point);
			} else {
				return fNegative.getValue(point);
			}
		}
		return Value.create(this);
	}

	@Override
	public ValueCategory getValueCategory(IASTNode point) {
		evaluate(point);
		return fValueCategory;
	}

	@Override
	public boolean isTypeDependent() {
		final ICPPEvaluation positive = fPositive == null ? fCondition : fPositive;
		return positive.isTypeDependent() || fNegative.isTypeDependent();
	}

	@Override
	public boolean isValueDependent() {
		return fCondition.isValueDependent() || (fPositive != null && fPositive.isValueDependent())
				|| fNegative.isValueDependent();
	}

	private void evaluate(IASTNode point) {
    	if (fValueCategory != null)
    		return;

    	fValueCategory= PRVALUE;

		final ICPPEvaluation positive = fPositive == null ? fCondition : fPositive;

		IType t2 = positive.getTypeOrFunctionSet(point);
		IType t3 = fNegative.getTypeOrFunctionSet(point);

		final IType uqt2= getNestedType(t2, TDEF | REF | CVTYPE);
		final IType uqt3= getNestedType(t3, TDEF | REF | CVTYPE);
		if (uqt2 instanceof ISemanticProblem || uqt2 instanceof ICPPUnknownType) {
			fType= uqt2;
			return;
		}
		if (uqt3 instanceof ISemanticProblem || uqt3 instanceof ICPPUnknownType) {
			fType= uqt3;
			return;
		}

		final boolean void2= isVoidType(uqt2);
		final boolean void3= isVoidType(uqt3);

		// Void types: Either both are void or one is a throw expression.
		if (void2 || void3) {
			if (fPositiveThrows) {
				fType= Conversions.lvalue_to_rvalue(t3, false);
			} else if (fNegativeThrows) {
				fType= Conversions.lvalue_to_rvalue(t2, false);
			} else if (void2 && void3) {
				fType= uqt2;
			} else {
				fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
			}
			return;
		}

		final ValueCategory vcat2= positive.getValueCategory(point);
		final ValueCategory vcat3= fNegative.getValueCategory(point);

		// Same type
		if (t2.isSameType(t3)) {
			if (vcat2 == vcat3) {
				fType= t2;
				fValueCategory= vcat2;
			} else {
				fType= prvalueType(t2);
				fValueCategory= PRVALUE;
			}
			return;
		}

		final boolean isClassType2 = uqt2 instanceof ICPPClassType;
		final boolean isClassType3 = uqt3 instanceof ICPPClassType;

		// Different types with at least one class type
		if (isClassType2 || isClassType3) {
			final Cost cost2= convertToMatch(t2, vcat2, uqt2, t3, vcat3, uqt3, point); // sets fType and fValueCategory
			final Cost cost3= convertToMatch(t3, vcat3, uqt3, t2, vcat2, uqt2, point); // sets fType and fValueCategory
			if (cost2.converts() || cost3.converts()) {
				if (cost2.converts()) {
					if (cost3.converts() || cost2.isAmbiguousUDC()) {
						fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
					}
				} else if (cost3.isAmbiguousUDC()) {
					fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
				}
				return;
			}
		} else if (vcat2 == vcat3 && vcat2.isGLValue() && uqt2.isSameType(uqt3)) {
			// Two lvalues or two xvalues with same type up to qualification.
			final CVQualifier cv2 = SemanticUtil.getCVQualifier(t2);
			final CVQualifier cv3 = SemanticUtil.getCVQualifier(t3);
			if (cv2.isAtLeastAsQualifiedAs(cv3)) {
				fType= t2;
				fValueCategory= vcat2;
			} else if (cv3.isAtLeastAsQualifiedAs(cv2)) {
				fType= t3;
				fValueCategory= vcat3;
			} else {
				fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
			}
			return;
		}

		// 5.16-5: At least one class type but no conversion
		if (isClassType2 || isClassType3) {
			fOverload = CPPSemantics.findOverloadedConditionalOperator(point, positive, fNegative);
			if (fOverload != null) {
				fType= ExpressionTypes.typeFromFunctionCall(fOverload);
			} else {
				fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
			}
			return;
		}

		// 5.16-6
		t2= Conversions.lvalue_to_rvalue(t2, false);
		t3= Conversions.lvalue_to_rvalue(t3, false);
		if (t2.isSameType(t3)) {
			fType= t2;
		} else {
	    	fType= CPPArithmeticConversion.convertCppOperandTypes(IASTBinaryExpression.op_plus, t2, t3);
	    	if (fType == null) {
	    		fType= Conversions.compositePointerType(t2, t3);
		    	if (fType == null) {
					fType= ProblemType.UNKNOWN_FOR_EXPRESSION;
		    	}
	    	}
		}
    }

    private Cost convertToMatch(IType t1, ValueCategory vcat1, IType uqt1, IType t2, ValueCategory vcat2, IType uqt2, IASTNode point) {
		// E2 is an lvalue or E2 is an xvalue
		try {
			if (vcat2.isGLValue()) {
				IType target= new CPPReferenceType(t2, vcat2 == XVALUE);
				Cost c= Conversions.checkImplicitConversionSequence(target, t1, vcat1, UDCMode.ALLOWED, Context.REQUIRE_DIRECT_BINDING, point);
				if (c.converts()) {
					fType= t2;
					fValueCategory= vcat2;
					return c;
				}
			}
			// Both are class types and one derives from the other
			if (uqt1 instanceof ICPPClassType && uqt2 instanceof ICPPClassType) {
				int dist= SemanticUtil.calculateInheritanceDepth(uqt1, uqt2, point);
				if (dist >= 0) {
					CVQualifier cv1 = SemanticUtil.getCVQualifier(t1);
					CVQualifier cv2 = SemanticUtil.getCVQualifier(t2);
					if (cv2.isAtLeastAsQualifiedAs(cv1)) {
						fType= t2;
						fValueCategory= PRVALUE;
						return new Cost(t1, t2, Rank.IDENTITY);
					}
					return Cost.NO_CONVERSION;
				}
				if (SemanticUtil.calculateInheritanceDepth(uqt2, uqt1, point) >= 0)
					return Cost.NO_CONVERSION;
			}
			// Unrelated class types or just one class:
			if (vcat2 != PRVALUE) {
				t2= Conversions.lvalue_to_rvalue(t2, false);
			}
			Cost c= Conversions.checkImplicitConversionSequence(t2, t1, vcat1, UDCMode.ALLOWED, Context.ORDINARY, point);
			if (c.converts()) {
				fType= t2;
				fValueCategory= PRVALUE;
				return c;
			}
		} catch (DOMException e) {
		}
		return Cost.NO_CONVERSION;
	}

	private boolean isVoidType(IType t) {
		return t instanceof ICPPBasicType && ((ICPPBasicType) t).getKind() == ICPPBasicType.Kind.eVoid;
	}

	@Override
	public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
		int firstByte = ITypeMarshalBuffer.EVAL_CONDITIONAL;
		if (fPositiveThrows)
			firstByte |= ITypeMarshalBuffer.FLAG1;
		if (fNegativeThrows)
			firstByte |= ITypeMarshalBuffer.FLAG2;

		buffer.putByte((byte) firstByte);
		buffer.marshalEvaluation(fCondition, includeValue);
		buffer.marshalEvaluation(fPositive, includeValue);
		buffer.marshalEvaluation(fNegative, includeValue);
	}

	public static ISerializableEvaluation unmarshal(int firstByte, ITypeMarshalBuffer buffer) throws CoreException {
		boolean pth= (firstByte & ITypeMarshalBuffer.FLAG1) != 0;
		boolean nth= (firstByte & ITypeMarshalBuffer.FLAG2) != 0;
		ICPPEvaluation cond= (ICPPEvaluation) buffer.unmarshalEvaluation();
		ICPPEvaluation pos= (ICPPEvaluation) buffer.unmarshalEvaluation();
		ICPPEvaluation neg= (ICPPEvaluation) buffer.unmarshalEvaluation();
		return new EvalConditional(cond, pos, neg, pth, nth);
	}

	@Override
	public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
			ICPPClassSpecialization within, int maxdepth, IASTNode point) {
		ICPPEvaluation condition = fCondition.instantiate(tpMap, packOffset, within, maxdepth, point);
		ICPPEvaluation positive = fPositive == null ?
				null : fPositive.instantiate(tpMap, packOffset, within, maxdepth, point);
		ICPPEvaluation negative = fNegative.instantiate(tpMap, packOffset, within, maxdepth, point);
		if (condition == fCondition && positive == fPositive && negative == fNegative)
			return this;
		return new EvalConditional(condition, positive, negative, fPositiveThrows, fNegativeThrows);
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(CPPFunctionParameterMap parameterMap,
			int maxdepth, IASTNode point) {
		ICPPEvaluation condition = fCondition.computeForFunctionCall(parameterMap, maxdepth, point);
		ICPPEvaluation positive = fPositive == null ?
				null : fPositive.computeForFunctionCall(parameterMap, maxdepth, point);
		ICPPEvaluation negative = fNegative.computeForFunctionCall(parameterMap, maxdepth, point);
		if (condition == fCondition && positive == fPositive && negative == fNegative)
			return this;
		return new EvalConditional(condition, positive, negative, fPositiveThrows, fNegativeThrows);
	}

	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		int r = fCondition.determinePackSize(tpMap);
		r = CPPTemplates.combinePackSize(r, fNegative.determinePackSize(tpMap));
		if (fPositive != null)
			r = CPPTemplates.combinePackSize(r, fPositive.determinePackSize(tpMap));
		return r;
	}

	@Override
	public boolean referencesTemplateParameter() {
		return fCondition.referencesTemplateParameter() ||
				(fPositive != null && fPositive.referencesTemplateParameter()) ||
				fNegative.referencesTemplateParameter();
	}
}

Back to the top