Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd429fbfe18ab1e88b77949df43a3f510d98498f (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*******************************************************************************
 * Copyright (c) 2012, 2014 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.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_alignOf;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_amper;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_minus;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_noexcept;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_not;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_plus;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_postFixDecr;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_postFixIncr;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_prefixDecr;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_prefixIncr;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_sizeof;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_sizeofParameterPack;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_star;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_throw;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_tilde;
import static org.eclipse.cdt.core.dom.ast.IASTUnaryExpression.op_typeid;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.glvalueType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.prvalueType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.prvalueTypeWithResolvedTypedefs;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.valueCategoryFromFunctionCall;
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 org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
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.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.DependentValue;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment;
import org.eclipse.cdt.internal.core.dom.parser.ValueFactory;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPArithmeticConversion;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClosureType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerToMemberType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.InstantiationContext;
import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics.LookupMode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalUtil.Pair;
import org.eclipse.core.runtime.CoreException;

public class EvalUnary extends CPPDependentEvaluation {
	private static final ICPPEvaluation ZERO_EVAL = new EvalFixed(CPPSemantics.INT_TYPE, PRVALUE, IntegralValue.create(0));

	private final int fOperator;
	private final ICPPEvaluation fArgument;
	private final IBinding fAddressOfQualifiedNameBinding;
	private ICPPFunction fOverload= CPPFunction.UNINITIALIZED_FUNCTION;
	private IType fType;
	private boolean fCheckedIsConstantExpression;
	private boolean fIsConstantExpression;


	public EvalUnary(int operator, ICPPEvaluation operand, IBinding addressOfQualifiedNameBinding,
			IASTNode pointOfDefinition) {
		this(operator, operand, addressOfQualifiedNameBinding, findEnclosingTemplate(pointOfDefinition));
	}

	public EvalUnary(int operator, ICPPEvaluation operand, IBinding addressOfQualifiedNameBinding,
			IBinding templateDefinition) {
		super(templateDefinition);
		fOperator= operator;
		fArgument= operand;
		fAddressOfQualifiedNameBinding= addressOfQualifiedNameBinding;
	}

	public int getOperator() {
		return fOperator;
	}

	public ICPPEvaluation getArgument() {
		return fArgument;
	}

	public IBinding getAddressOfQualifiedNameBinding() {
		return fAddressOfQualifiedNameBinding;
	}

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

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

	@Override
	public boolean isTypeDependent() {
		if (fType != null)
			return fType instanceof TypeOfDependentExpression;

		switch (fOperator) {
		case op_alignOf:
		case op_not:
		case op_sizeof:
		case op_sizeofParameterPack:
		case op_throw:
		case op_typeid:
			return false;
		default:
			return fArgument.isTypeDependent();
		}
	}

	@Override
	public boolean isValueDependent() {
		switch (fOperator) {
		case op_alignOf:
		case op_sizeof:
		case op_sizeofParameterPack:
		case op_typeid:
			return fArgument.isTypeDependent();
		case op_noexcept:
			return fArgument.referencesTemplateParameter();
		case op_throw:
			return false;
		default:
			return fArgument.isValueDependent();
		}
	}

	@Override
	public boolean isConstantExpression() {
		if (!fCheckedIsConstantExpression) {
			fCheckedIsConstantExpression = true;
			fIsConstantExpression = computeIsConstantExpression();
		}
		return fIsConstantExpression;
	}

	private boolean computeIsConstantExpression() {
		return fArgument.isConstantExpression()
			&& isNullOrConstexprFunc(getOverload());
	}

	@Override
	public boolean isEquivalentTo(ICPPEvaluation other) {
		if (!(other instanceof EvalUnary)) {
			return false;
		}
		EvalUnary o = (EvalUnary) other;
		return fOperator == o.fOperator
			&& fArgument.isEquivalentTo(o.fArgument);
	}
	
	public ICPPFunction getOverload() {
		if (fOverload == CPPFunction.UNINITIALIZED_FUNCTION) {
			fOverload= computeOverload();
		}
		return fOverload;
	}

	private ICPPFunction computeOverload() {
    	OverloadableOperator op = OverloadableOperator.fromUnaryExpression(fOperator);
		if (op == null)
			return null;

		if (fArgument.isTypeDependent())
			return null;

		if (fAddressOfQualifiedNameBinding instanceof ICPPMember) {
			ICPPMember member= (ICPPMember) fAddressOfQualifiedNameBinding;
			if (!member.isStatic())
				return null;
		}

    	IType type = fArgument.getType();
		type = SemanticUtil.getNestedType(type, TDEF | REF | CVTYPE);
		if (!CPPSemantics.isUserDefined(type))
			return null;

		ICPPEvaluation[] args;
	    if (fOperator == op_postFixDecr || fOperator == op_postFixIncr) {
	    	args = new ICPPEvaluation[] { fArgument, ZERO_EVAL };
	    } else {
	    	args = new ICPPEvaluation[] { fArgument };
	    }
    	return CPPSemantics.findOverloadedOperator(getTemplateDefinitionScope(), args, type,
    			op, LookupMode.LIMITED_GLOBALS);
	}

	@Override
	public IType getType() {
		if (fType == null)
			fType= computeType();
		return fType;
	}

	private IType computeType() {
		if (isTypeDependent())
			return new TypeOfDependentExpression(this);

		ICPPFunction overload = getOverload();
		if (overload != null)
			return ExpressionTypes.typeFromFunctionCall(overload);

    	switch (fOperator) {
		case op_sizeof:
		case op_sizeofParameterPack:
			return CPPVisitor.get_SIZE_T();
		case op_typeid:
			return CPPVisitor.get_type_info();
		case op_throw:
			return CPPSemantics.VOID_TYPE;
		case op_amper:
			if (fAddressOfQualifiedNameBinding instanceof ICPPMember) {
				ICPPMember member= (ICPPMember) fAddressOfQualifiedNameBinding;
				if (!member.isStatic()) {
					try {
						return new CPPPointerToMemberType(member.getType(), member.getClassOwner(),
								false, false, false);
					} catch (DOMException e) {
						return e.getProblem();
					}
				}
			}
			return new CPPPointerType(fArgument.getType());
		case op_star:
			IType type= fArgument.getType();
			type = prvalueTypeWithResolvedTypedefs(type);
	    	if (type instanceof IPointerType) {
	    		return glvalueType(((IPointerType) type).getType());
			}
	    	if (type instanceof ISemanticProblem) {
	    		return type;
	    	}
			return ProblemType.UNKNOWN_FOR_EXPRESSION;
		case op_noexcept:
		case op_not:
			return CPPBasicType.BOOLEAN;
		case op_postFixDecr:
		case op_postFixIncr:
			return prvalueType(fArgument.getType());
		case op_plus:
			return promoteType(fArgument.getType(), true);
		case op_minus:
		case op_tilde:
			return promoteType(fArgument.getType(), false);
		}
		return fArgument.getType();
	}

	private IType promoteType(IType type, boolean allowPointer) {
    	final IType t1 = prvalueType(type);
		final IType t2 = SemanticUtil.getNestedType(t1, TDEF);
		if (allowPointer) {
			if (t2 instanceof CPPClosureType) {
				ICPPMethod conversionOperator = ((CPPClosureType) t2).getConversionOperator();
				if (conversionOperator == null)
					return ProblemType.UNKNOWN_FOR_EXPRESSION;
				return new CPPPointerType(conversionOperator.getType().getReturnType());
			}
			if (t2 instanceof IPointerType) {
				return t1;
			}
		}
		final IType t3= CPPArithmeticConversion.promoteCppType(t2);
		if (t3 == null && !(t2 instanceof IBasicType))
			return ProblemType.UNKNOWN_FOR_EXPRESSION;
		return (t3 == null || t3 == t2) ? t1 : t3;
	}

	@Override
	public IValue getValue() {
		if (isValueDependent())
			return DependentValue.create(this);

		ICPPEvaluation arg = fArgument;
		ICPPFunction overload = getOverload();
		if (overload != null) {
			ICPPFunctionType functionType = overload.getType();
			IType[] parameterTypes = functionType.getParameterTypes();
			if (parameterTypes.length == 0)
				return IntegralValue.ERROR;
			IType targetType = parameterTypes[0];
			arg = maybeApplyConversion(arg, targetType, fOperator == op_not);

			if (!(overload instanceof CPPImplicitFunction)) {
				if (!overload.isConstexpr())
					return IntegralValue.ERROR;
				ICPPEvaluation eval = new EvalBinding(overload, null, (IBinding) null);
				arg = new EvalFunctionCall(new ICPPEvaluation[] {eval, arg}, null, (IBinding) null);
				return arg.getValue();
			}
		}

		switch (fOperator) {
			case op_sizeof: {
				SizeAndAlignment info =
						SizeofCalculator.getSizeAndAlignment(fArgument.getType());
				return info == null ? IntegralValue.UNKNOWN : IntegralValue.create(info.size);
			}
			case op_alignOf: {
				SizeAndAlignment info =
						SizeofCalculator.getSizeAndAlignment(fArgument.getType());
				return info == null ? IntegralValue.UNKNOWN : IntegralValue.create(info.alignment);
			}
			case op_noexcept:
				return IntegralValue.UNKNOWN;  // TODO(sprigogin): Implement
			case op_sizeofParameterPack:
				IValue opVal = fArgument.getValue();
				return IntegralValue.create(opVal.numberOfSubValues());
			case op_typeid:
				return IntegralValue.UNKNOWN;  // TODO(sprigogin): Implement
			case op_throw:
				return IntegralValue.UNKNOWN;  // TODO(sprigogin): Implement
		}

		IValue val = arg.getValue();
		if (val == null)
			return IntegralValue.UNKNOWN;

		return ValueFactory.evaluateUnaryExpression(fOperator, val);
	}

	@Override
	public ValueCategory getValueCategory() {
		ICPPFunction overload = getOverload();
    	if (overload != null)
    		return valueCategoryFromFunctionCall(overload);

    	switch (fOperator) {
		case op_typeid:
    	case op_star:
    	case op_prefixDecr:
    	case op_prefixIncr:
			return LVALUE;
    	default:
			return PRVALUE;
    	}
    }

	@Override
	public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
		buffer.putShort(ITypeMarshalBuffer.EVAL_UNARY);
		buffer.putByte((byte) fOperator);
		buffer.marshalEvaluation(fArgument, includeValue);
		buffer.marshalBinding(fAddressOfQualifiedNameBinding);
		marshalTemplateDefinition(buffer);
	}

	public static ICPPEvaluation unmarshal(short firstBytes, ITypeMarshalBuffer buffer)
			throws CoreException {
		int op= buffer.getByte();
		ICPPEvaluation arg= buffer.unmarshalEvaluation();
		IBinding binding= buffer.unmarshalBinding();
		IBinding templateDefinition= buffer.unmarshalBinding();
		return new EvalUnary(op, arg, binding, templateDefinition);
	}

	@Override
	public ICPPEvaluation instantiate(InstantiationContext context, int maxDepth) {
		ICPPEvaluation argument = fArgument.instantiate(context, maxDepth);
		IBinding binding = fAddressOfQualifiedNameBinding;
		if (binding instanceof ICPPUnknownBinding) {
			try {
				binding= CPPTemplates.resolveUnknown((ICPPUnknownBinding) binding, context);
			} catch (DOMException e) {
			}
		}
		if (argument == fArgument && binding == fAddressOfQualifiedNameBinding)
			return this;

		if (binding instanceof IProblemBinding)
			return EvalFixed.INCOMPLETE;

		return new EvalUnary(fOperator, argument, binding, getTemplateDefinition());
	}

	private ICPPEvaluation createOperatorOverloadEvaluation(ICPPFunction overload, ICPPEvaluation arg) {
		IBinding templateDefinition = getTemplateDefinition();
		if (overload instanceof ICPPMethod) {
			EvalMemberAccess opAccess = new EvalMemberAccess(arg.getType(), ValueCategory.LVALUE, overload, 
					arg, false, templateDefinition);
			ICPPEvaluation[] args = new ICPPEvaluation[]{opAccess};
			return new EvalFunctionCall(args, arg, templateDefinition);
		} else {
			EvalBinding op = new EvalBinding(overload, overload.getType(), templateDefinition);
			ICPPEvaluation[] args = new ICPPEvaluation[]{op, arg};
			return new EvalFunctionCall(args, null, templateDefinition);
		}
	}

	private static boolean isModifyingOperation(int op) {
		return op == op_prefixIncr || op == op_prefixDecr || op == op_postFixIncr || op == op_postFixDecr;
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
		ICPPFunction overload = getOverload();
		if (overload != null) {
			ICPPEvaluation operatorCall = createOperatorOverloadEvaluation(overload, fArgument);
			ICPPEvaluation eval = operatorCall.computeForFunctionCall(record, context);
			return eval;
		}

		Pair<ICPPEvaluation, ICPPEvaluation> vp = EvalUtil.getValuePair(fArgument, record, context);
		final ICPPEvaluation updateable = vp.getFirst();
		final ICPPEvaluation fixed = vp.getSecond();

		ICPPEvaluation evalUnary = fixed == fArgument || fixed == EvalFixed.INCOMPLETE ? this : new EvalUnary(fOperator, fixed, fAddressOfQualifiedNameBinding, getTemplateDefinition());
		if (fOperator == op_star) {
			if (fixed instanceof EvalPointer) {
				EvalPointer evalPointer = (EvalPointer) fixed;
				return evalPointer.dereference();
			} else if (updateable instanceof EvalBinding && isStarOperatorOnArrayName(context)) {
				EvalBinding evalBinding = (EvalBinding) updateable;
				IBinding binding = evalBinding.getBinding();
				ICPPEvaluation eval = record.getVariable(binding);
				if (eval == null)
					return EvalFixed.INCOMPLETE;
				EvalCompositeAccess compositeAccess = new EvalCompositeAccess(eval, 0);
				return new EvalReference(record, compositeAccess, getTemplateDefinition());
			}
			return evalUnary;
		} else if (fOperator == op_amper) {
			if (updateable instanceof EvalBinding) {
				EvalBinding evalBinding = (EvalBinding) updateable;
				IBinding binding = evalBinding.getBinding();
				return new EvalPointer(record, binding, getTemplateDefinition());
			} else if (updateable instanceof EvalReference) {
				EvalReference evalRef = (EvalReference) updateable;
				return EvalPointer.createFromAddress(evalRef);
			}
			return evalUnary;
		} else if (isModifyingOperation(fOperator)) {
			if (fixed instanceof EvalPointer) {
				EvalPointer evalPointer = (EvalPointer) fixed;
				applyPointerArithmetics(evalPointer);
				return evalPointer;
			} else {
				EvalFixed newValue = new EvalFixed(evalUnary.getType(), evalUnary.getValueCategory(), evalUnary.getValue());
				if (updateable instanceof EvalReference) {
					EvalReference evalRef = (EvalReference) updateable;
					evalRef.update(newValue);
				} else if (updateable instanceof EvalCompositeAccess) {
					EvalCompositeAccess evalCompAccess = (EvalCompositeAccess) updateable;
					evalCompAccess.update(newValue);
				} else if (updateable instanceof EvalBinding) {
					EvalBinding evalBinding = (EvalBinding) updateable;
					IBinding binding = evalBinding.getBinding();
					record.update(binding, newValue);
				}

				if (this.getValueCategory() == ValueCategory.LVALUE) {
					return updateable;
				} else {
					return fixed;
				}
			}
		} else {
			return evalUnary;
		}
	}

	private boolean isStarOperatorOnArrayName(ConstexprEvaluationContext context) {
		return fOperator == op_star && fArgument.getType() instanceof IArrayType;
	}

	private void applyPointerArithmetics(EvalPointer poiner) {
		switch (fOperator) {
		case op_postFixIncr:
		case op_prefixIncr:
			poiner.setPosition(poiner.getPosition() + 1);
			break;
		case op_postFixDecr:
		case op_prefixDecr:
			poiner.setPosition(poiner.getPosition() - 1);
			break;
		}
	}

	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		return fArgument.determinePackSize(tpMap);
	}

	@Override
	public boolean referencesTemplateParameter() {
		return fArgument.referencesTemplateParameter();
	}
}

Back to the top