Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ef448f40c67477c2c6f536c05a0455513f44e42 (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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*******************************************************************************
 * Copyright (c) 2008, 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;

import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_alignof;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_nothrow_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_nothrow_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_assign;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_virtual_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_abstract;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_class;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_empty;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_enum;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_final;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_literal_type;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_pod;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_polymorphic;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_standard_layout;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_trivial;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_trivially_copyable;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_union;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_sizeof;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_typeid;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_typeof;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;

import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTBinaryTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluationOwner;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.TypeTraits;
import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator;
import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator.EvalException;

public class ValueFactory {
	/**
	 * Creates the value for an expression.
	 */
	public static IValue create(IASTExpression expr) {
		IValue val= evaluate(expr);
		if (val != null) {
			return val;
		}

		if (expr instanceof ICPPEvaluationOwner) {
			ICPPEvaluation evaluation = ((ICPPEvaluationOwner) expr).getEvaluation();
			return evaluation.getValue(expr);
		}
		return IntegralValue.UNKNOWN;
	}

	public static IValue evaluateUnaryExpression(final int unaryOp, final IValue value) {
		IValue val = applyUnaryOperator(unaryOp, value);
		if (isInvalidValue(val))
			return IntegralValue.UNKNOWN;
		return val;
	}

	public static IValue evaluateBinaryExpression(final int op, final IValue v1, final IValue v2) {
		if (v1 instanceof FloatingPointValue && v2 instanceof FloatingPointValue) {
			FloatingPointValue fv1 = (FloatingPointValue) v1;
			FloatingPointValue fv2 = (FloatingPointValue) v2;
			return applyBinaryOperator(op, fv1.numberValue().doubleValue(), fv2.numberValue().doubleValue());
		} else if (v1 instanceof FloatingPointValue && v2 instanceof IntegralValue) {
			FloatingPointValue fv1 = (FloatingPointValue) v1;
			IntegralValue iv2 = (IntegralValue) v2;
			return applyBinaryOperator(op, fv1.numberValue().doubleValue(), iv2.numberValue().doubleValue());
		} else if (v1 instanceof IntegralValue && v2 instanceof FloatingPointValue) {
			IntegralValue iv1 = (IntegralValue) v1;
			FloatingPointValue fv2 = (FloatingPointValue) v2;
			return applyBinaryOperator(op, iv1.numberValue().doubleValue(), fv2.numberValue().doubleValue());
		} else if (v1 instanceof IntegralValue && v2 instanceof IntegralValue) {
			IntegralValue iv1 = (IntegralValue) v1;
			IntegralValue iv2 = (IntegralValue) v2;
			return applyBinaryOperator(op, iv1.numberValue().longValue(), iv2.numberValue().longValue());
		}
		return IntegralValue.UNKNOWN;
	}

	private static IValue applyBinaryOperator(final int op, final double v1, final double v2) {
		Double doubleValue = null;
		Long longValue = null;

		switch (op) {
		case IASTBinaryExpression.op_multiply:
			doubleValue = v1 * v2;
			break;
		case IASTBinaryExpression.op_divide:
			if (v2 != 0) {
				doubleValue = v1 / v2;
			}
			break;
		case IASTBinaryExpression.op_plus:
			doubleValue = v1 + v2;
			break;
		case IASTBinaryExpression.op_minus:
			doubleValue = v1 - v2;
			break;
		case IASTBinaryExpression.op_lessThan:
			longValue = v1 < v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_greaterThan:
			longValue = v1 > v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_lessEqual:
			longValue = v1 <= v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_greaterEqual:
			longValue = v1 >= v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_logicalAnd:
			longValue = v1 != 0 && v2 != 0 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_logicalOr:
			longValue = v1 != 0 || v2 != 0 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_equals:
			longValue = v1 == v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_notequals:
			longValue = v1 != v2 ? 1l : 0l;
			break;
		}

		if (doubleValue != null) {
			return FloatingPointValue.create(doubleValue);
		} else if (longValue != null) {
			return IntegralValue.create(longValue);
		} else {
			return IntegralValue.UNKNOWN;
		}
	}

	private static IntegralValue applyBinaryOperator(final int op, final long v1, final long v2) {
		Long value = null;
		switch (op) {
		case IASTBinaryExpression.op_multiply:
			value = v1 * v2;
			break;
		case IASTBinaryExpression.op_divide:
			if (v2 != 0) {
				value = v1 / v2;
			}
			break;
		case IASTBinaryExpression.op_modulo:
			if (v2 != 0) {
				value = v1 % v2;
			}
			break;
		case IASTBinaryExpression.op_plus:
			value = v1 + v2;
			break;
		case IASTBinaryExpression.op_minus:
			value = v1 - v2;
			break;
		case IASTBinaryExpression.op_shiftLeft:
			value = v1 << v2;
			break;
		case IASTBinaryExpression.op_shiftRight:
			value = v1 >> v2;
			break;
		case IASTBinaryExpression.op_lessThan:
			value = v1 < v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_greaterThan:
			value = v1 > v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_lessEqual:
			value = v1 <= v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_greaterEqual:
			value = v1 >= v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_binaryAnd:
			value = v1 & v2;
			break;
		case IASTBinaryExpression.op_binaryXor:
			value = v1 ^ v2;
			break;
		case IASTBinaryExpression.op_binaryOr:
			value = v1 | v2;
			break;
		case IASTBinaryExpression.op_logicalAnd:
			value = v1 != 0 && v2 != 0 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_logicalOr:
			value = v1 != 0 || v2 != 0 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_equals:
			value = v1 == v2 ? 1l : 0l;
			break;
		case IASTBinaryExpression.op_notequals:
			value = v1 != v2 ? 1l : 0l;
			break;
        case IASTBinaryExpression.op_max:
        	value = Math.max(v1, v2);
        	break;
        case IASTBinaryExpression.op_min:
        	value = Math.min(v1, v2);
        	break;
		}

		if (value != null) {
			return IntegralValue.create(value);
		} else {
			return IntegralValue.UNKNOWN;
		}
	}

	public static IValue evaluateUnaryTypeIdExpression(int operator, IType type, IASTNode point) {
		IValue val = applyUnaryTypeIdOperator(operator, type, point);
		if (isInvalidValue(val))
			return IntegralValue.UNKNOWN;
		return val;
	}

	public static IValue evaluateBinaryTypeIdExpression(IASTBinaryTypeIdExpression.Operator operator,
			IType type1, IType type2, IASTNode point) {
		IValue val = applyBinaryTypeIdOperator(operator, type1, type2, point);
		if (isInvalidValue(val))
			return IntegralValue.UNKNOWN;
		return val;
	}

	/**
	 * Computes the canonical representation of the value of the expression.
	 */
	private static IValue evaluate(IASTExpression exp) {
		if (exp == null)
			return IntegralValue.UNKNOWN;

		if (exp instanceof IASTArraySubscriptExpression) {
			return IntegralValue.UNKNOWN;
		}
		if (exp instanceof IASTBinaryExpression) {
			return evaluateBinaryExpression((IASTBinaryExpression) exp);
		}
		if (exp instanceof IASTCastExpression) { // must be ahead of unary
			return evaluate(((IASTCastExpression) exp).getOperand());
		}
		if (exp instanceof IASTUnaryExpression) {
			return evaluateUnaryExpression((IASTUnaryExpression) exp);
		}
		if (exp instanceof IASTConditionalExpression) {
			IASTConditionalExpression cexpr= (IASTConditionalExpression) exp;
			IValue v= evaluate(cexpr.getLogicalConditionExpression());
			if (isInvalidValue(v))
				return v;
			if (isDeferredValue(v))
				return null;  // the value will be computed using the evaluation
			if (v.numberValue().longValue() == 0) {
				return evaluate(cexpr.getNegativeResultExpression());
			}
			final IASTExpression pe = cexpr.getPositiveResultExpression();
			if (pe == null) // gnu-extension allows to omit the positive expression.
				return v;
			return evaluate(pe);
		}
		if (exp instanceof IASTIdExpression) {
			IBinding b= ((IASTIdExpression) exp).getName().resolvePreBinding();
			return evaluateBinding(b);
		}
		if (exp instanceof IASTLiteralExpression) {
			IASTLiteralExpression litEx= (IASTLiteralExpression) exp;
			switch (litEx.getKind()) {
			case IASTLiteralExpression.lk_false:
			case IASTLiteralExpression.lk_nullptr:
				return IntegralValue.create(0);
			case IASTLiteralExpression.lk_true:
				return IntegralValue.create(1);
			case IASTLiteralExpression.lk_integer_constant:
				try {
					return IntegralValue.create(ExpressionEvaluator.getNumber(litEx.getValue()));
				} catch (EvalException e) {
					return IntegralValue.UNKNOWN;
				}
			case IASTLiteralExpression.lk_char_constant:
				try {
					final char[] image= litEx.getValue();
					if (image.length > 1 && image[0] == 'L')
						return IntegralValue.create(ExpressionEvaluator.getChar(image, 2));
					return IntegralValue.create(ExpressionEvaluator.getChar(image, 1));
				} catch (EvalException e) {
					return IntegralValue.UNKNOWN;
				}
			case IASTLiteralExpression.lk_float_constant:
				return FloatingPointValue.create(litEx.getValue());
			case IASTLiteralExpression.lk_string_literal:
				return CStringValue.create(litEx.getValue());
			}
		}

		if (exp instanceof IASTTypeIdExpression) {
			IASTTypeIdExpression typeIdExp = (IASTTypeIdExpression) exp;
			ASTTranslationUnit ast = (ASTTranslationUnit) exp.getTranslationUnit();
			final IType type = ast.createType(typeIdExp.getTypeId());
			if (type instanceof ICPPUnknownType)
				return null;
			return applyUnaryTypeIdOperator(typeIdExp.getOperator(), type, exp);
		}
		if (exp instanceof IASTBinaryTypeIdExpression) {
			IASTBinaryTypeIdExpression typeIdExp = (IASTBinaryTypeIdExpression) exp;
			ASTTranslationUnit ast = (ASTTranslationUnit) exp.getTranslationUnit();
			IType t1= ast.createType(typeIdExp.getOperand1());
			IType t2= ast.createType(typeIdExp.getOperand2());
			if (CPPTemplates.isDependentType(t1) || CPPTemplates.isDependentType(t2))
				return null;
			return applyBinaryTypeIdOperator(typeIdExp.getOperator(), t1, t2, exp);
		}
		if (exp instanceof IASTFunctionCallExpression || exp instanceof ICPPASTSimpleTypeConstructorExpression) {
			return null;  // The value will be obtained from the evaluation.
		}
		return IntegralValue.UNKNOWN;
	}

	/**
	 * Extract a value off a binding.
	 */
	private static IValue evaluateBinding(IBinding b) {
		if (b instanceof IType) {
			return IntegralValue.UNKNOWN;
		}
		if (b instanceof ICPPTemplateNonTypeParameter) {
			return null;
		}

		if (b instanceof ICPPUnknownBinding) {
			return null;
		}

		IValue value= null;
		if (b instanceof IVariable) {
			value= ((IVariable) b).getInitialValue();
		} else if (b instanceof IEnumerator) {
			value= ((IEnumerator) b).getValue();
		}
		if (isInvalidValue(value)) {
			return IntegralValue.UNKNOWN;
		}
		return value;
	}

	private static IValue applyUnaryTypeIdOperator(int operator, IType type, IASTNode point) {
		switch (operator) {
			case op_sizeof:
				return getSize(type, point);
			case op_alignof:
				return getAlignment(type, point);
			case op_typeid:
				break;
			case op_has_nothrow_copy:
				break;  // TODO(sprigogin): Implement
			case op_has_nothrow_constructor:
				break;  // TODO(sprigogin): Implement
			case op_has_trivial_assign:
				break;  // TODO(sprigogin): Implement
			case op_has_trivial_constructor:
				break;  // TODO(sprigogin): Implement
			case op_has_trivial_copy:
				return IntegralValue.create(!(type instanceof ICPPClassType) ||
						TypeTraits.hasTrivialCopyCtor((ICPPClassType) type, point) ? 1 : 0);
			case op_has_trivial_destructor:
				break;  // TODO(sprigogin): Implement
			case op_has_virtual_destructor:
				break;  // TODO(sprigogin): Implement
			case op_is_abstract:
				return IntegralValue.create(type instanceof ICPPClassType &&
						TypeTraits.isAbstract((ICPPClassType) type, point) ? 1 : 0);
			case op_is_class:
				return IntegralValue.create(type instanceof ICompositeType &&
						((ICompositeType) type).getKey() != ICompositeType.k_union ? 1 : 0);
			case op_is_empty:
				return IntegralValue.create(TypeTraits.isEmpty(type, point) ? 1 : 0);
			case op_is_enum:
				return IntegralValue.create(type instanceof IEnumeration ? 1 : 0);
			case op_is_final:
				return IntegralValue.create(type instanceof ICPPClassType && ((ICPPClassType) type).isFinal() ? 1 : 0);
			case op_is_literal_type:
				break;  // TODO(sprigogin): Implement
			case op_is_pod:
				return IntegralValue.create(TypeTraits.isPOD(type, point) ? 1 : 0);
			case op_is_polymorphic:
				return IntegralValue.create(type instanceof ICPPClassType &&
						TypeTraits.isPolymorphic((ICPPClassType) type, point) ? 1 : 0);
			case op_is_standard_layout:
				return IntegralValue.create(TypeTraits.isStandardLayout(type, point) ? 1 : 0);
			case op_is_trivial:
				return IntegralValue.create(type instanceof ICPPClassType &&
						TypeTraits.isTrivial((ICPPClassType) type, point) ? 1 : 0);
            case op_is_trivially_copyable:
            	return IntegralValue.create(TypeTraits.isTriviallyCopyable(type, point) ? 1 : 0);
			case op_is_union:
				return IntegralValue.create(type instanceof ICompositeType &&
						((ICompositeType) type).getKey() == ICompositeType.k_union ? 1 : 0);
			case op_typeof:
				break;
		}
		return IntegralValue.UNKNOWN;
	}

	private static IValue getAlignment(IType type, IASTNode point) {
		SizeAndAlignment sizeAndAlignment = SizeofCalculator.getSizeAndAlignment(type, point);
		if (sizeAndAlignment == null)
			 return IntegralValue.UNKNOWN;
		return IntegralValue.create(sizeAndAlignment.alignment);
	}

	private static IValue getSize(IType type, IASTNode point) {
		SizeAndAlignment sizeAndAlignment = SizeofCalculator.getSizeAndAlignment(type, point);
		if (sizeAndAlignment == null)
			 return IntegralValue.UNKNOWN;
		return IntegralValue.create(sizeAndAlignment.size);
	}

	private static IValue evaluateUnaryExpression(IASTUnaryExpression exp) {
		final int unaryOp= exp.getOperator();

		if (unaryOp == IASTUnaryExpression.op_sizeof) {
			final IASTExpression operand = exp.getOperand();
			if (operand != null) {
				IType type = operand.getExpressionType();
				if (type instanceof ICPPUnknownType)
					return null;
				ASTTranslationUnit ast = (ASTTranslationUnit) exp.getTranslationUnit();
				SizeofCalculator calculator = ast.getSizeofCalculator();
				SizeAndAlignment info = calculator.sizeAndAlignment(type);
				if (info != null)
					return IntegralValue.create(info.size);
			}
			return IntegralValue.UNKNOWN;
		}

		if (unaryOp == IASTUnaryExpression.op_amper || unaryOp == IASTUnaryExpression.op_star ||
				unaryOp == IASTUnaryExpression.op_sizeofParameterPack) {
			return IntegralValue.UNKNOWN;
		}

		final IValue value= evaluate(exp.getOperand());
		if (isInvalidValue(value))
			return value;
		if (isDeferredValue(value))
			return null;  // the value will be computed using the evaluation
		return applyUnaryOperator(unaryOp, value);
	}

	private static IValue applyUnaryOperator(final int unaryOp, final IValue value) {
		if (isInvalidValue(value) || value.numberValue() == null) {
			return IntegralValue.UNKNOWN;
		}

		if (!(value instanceof IntegralValue) && !(value instanceof FloatingPointValue)) {
			return IntegralValue.UNKNOWN;
		}

		switch (unaryOp) {
		case IASTUnaryExpression.op_bracketedPrimary:
		case IASTUnaryExpression.op_plus:
			return value;
		case IASTUnaryExpression.op_prefixIncr:
		case IASTUnaryExpression.op_postFixIncr:
			if (value instanceof IntegralValue) {
				return IntegralValue.create(value.numberValue().longValue() + 1);
			} else {
				FloatingPointValue fpv = (FloatingPointValue) value;
				return FloatingPointValue.create(fpv.numberValue().doubleValue() + 1);
			}
		case IASTUnaryExpression.op_prefixDecr:
		case IASTUnaryExpression.op_postFixDecr:
			if (value instanceof IntegralValue) {
				return IntegralValue.create(value.numberValue().longValue() - 1);
			} else {
				FloatingPointValue fpv = (FloatingPointValue) value;
				return FloatingPointValue.create(fpv.numberValue().doubleValue() - 1);
			}
		case IASTUnaryExpression.op_minus:
			if (value instanceof IntegralValue) {
				return IntegralValue.create(-value.numberValue().longValue());
			} else {
				FloatingPointValue fpv = (FloatingPointValue) value;
				return FloatingPointValue.create(-fpv.numberValue().doubleValue());
			}
		case IASTUnaryExpression.op_tilde:
			if (value instanceof IntegralValue) {
				return IntegralValue.create(~value.numberValue().longValue());
			} else {
				return IntegralValue.UNKNOWN;
			}
		case IASTUnaryExpression.op_not:
			if (value instanceof IntegralValue) {
				Long num = value.numberValue().longValue();
				return IntegralValue.create(num == 0 ? 1 : 0);
			} else {
				FloatingPointValue fpv = (FloatingPointValue) value;
				Double num = fpv.numberValue().doubleValue();
				return IntegralValue.create(num == 0 ? 1 : 0);
			}
		}
		return IntegralValue.UNKNOWN;
	}

	private static IValue evaluateBinaryExpression(IASTBinaryExpression exp) {
		final int op= exp.getOperator();

		// Optimization: if the operator is == or != and the AST nodes
		// themselves are equal, we know the answer without having to
		// do any evaluation.
		if (op == IASTBinaryExpression.op_equals && exp.getOperand1().equals(exp.getOperand2())) {
			return IntegralValue.create(true);
		}
		if (op == IASTBinaryExpression.op_notequals && exp.getOperand1().equals(exp.getOperand2())) {
			return IntegralValue.create(false);
		}

		final IValue o1= evaluate(exp.getOperand1());
		if (isInvalidValue(o1))
			return o1;
		final IValue o2= evaluate(exp.getOperand2());
		if (isInvalidValue(o2))
			return o2;
		if (isDeferredValue(o1) || isDeferredValue(o2))
			return null;  // the value will be computed using the evaluation
		return evaluateBinaryExpression(op, o1, o2);
	}

	private static IValue applyBinaryTypeIdOperator(IASTBinaryTypeIdExpression.Operator operator,
			IType type1, IType type2, IASTNode point) {
		switch (operator) {
		case __is_base_of:
			type1 = SemanticUtil.getNestedType(type1, TDEF);
			type2 = SemanticUtil.getNestedType(type2, TDEF);
			if (type1 instanceof ICPPClassType && type2 instanceof ICPPClassType &&
					(type1.isSameType(type2) ||
							ClassTypeHelper.isSubclass((ICPPClassType) type2, (ICPPClassType) type1, point))) {
				return IntegralValue.create(1);
			}
			return IntegralValue.create(0);
		case __is_trivially_assignable:
			return IntegralValue.UNKNOWN;		// TODO: Implement.
		}
		return IntegralValue.UNKNOWN;
	}

	private static boolean isInvalidValue(IValue value) {
		return value == null || value == IntegralValue.UNKNOWN || value == IntegralValue.ERROR;
	}

	private static boolean isDeferredValue(IValue value) {
		return value instanceof DependentValue || 
			(value instanceof IntegralValue && ((IntegralValue) value).numberValue() == null);
	}

	/**
	 * Returns the numerical value of the given expression if the expression can be evaluated
	 * at compile time.
	 *
	 * @param expr the expression to evaluate
	 * @return the numerical value of the expression, or {@code null} if the expression cannot be evaluated
	 *     to a constant
	 */
	public static Number getConstantNumericalValue(IASTExpression expr) {
		IValue val = evaluate(expr);
		if (val != null) {
			return val.numberValue();
		}

		if (expr instanceof ICPPEvaluationOwner) {
			ICPPEvaluation eval = ((ICPPEvaluationOwner) expr).getEvaluation();
			if (eval.isConstantExpression(expr) && !eval.isValueDependent())
				return eval.getValue(expr).numberValue();
		}
		return null;
	}
}

Back to the top