-
Notifications
You must be signed in to change notification settings - Fork 187
/
ValueFactory.java
709 lines (665 loc) · 26.5 KB
/
ValueFactory.java
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*******************************************************************************
* Copyright (c) 2008, 2014, 2023 Wind River Systems, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Markus Schorn - initial API and implementation
* Sergey Prigogin (Google)
* Igor V. Kovalenko
*******************************************************************************/
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_unique_object_representations;
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_aggregate;
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.CVTYPE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
import java.util.Arrays;
import java.util.function.LongBinaryOperator;
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.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBasicType;
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.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNaryTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNaryTypeIdExpression.Operator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
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.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
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) {
try {
CPPSemantics.pushLookupPoint(expr);
IValue val = evaluate(expr);
if (val != null) {
return val;
}
if (expr instanceof ICPPASTInitializerClause clause) {
ICPPEvaluation evaluation = clause.getEvaluation();
return evaluation.getValue();
}
return IntegralValue.UNKNOWN;
} finally {
CPPSemantics.popLookupPoint();
}
}
public static IValue evaluateUnaryExpression(final int unaryOp, final IValue value, final IType type) {
IValue val = applyUnaryOperator(unaryOp, value, type);
if (isInvalidValue(val))
return IntegralValue.UNKNOWN;
return val;
}
public static IValue evaluateBinaryExpression(final int op, final IValue v1, final IValue v2, final IType type) {
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(), type);
}
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, final IType type) {
if ((v1 < 0 || v2 < 0) && type instanceof IBasicType basicType && basicType.isUnsigned()) {
return applyBinaryOperator(op, v1, v2, (x, y) -> Long.compareUnsigned(x, y));
} else {
return applyBinaryOperator(op, v1, v2, (x, y) -> Long.compare(x, y));
}
}
private static IntegralValue applyBinaryOperator(final int op, final long v1, final long v2,
LongBinaryOperator comparator) {
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 = comparator.applyAsLong(v1, v2) < 0 ? 1l : 0l;
break;
case IASTBinaryExpression.op_greaterThan:
value = comparator.applyAsLong(v1, v2) > 0 ? 1l : 0l;
break;
case IASTBinaryExpression.op_lessEqual:
value = comparator.applyAsLong(v1, v2) <= 0 ? 1l : 0l;
break;
case IASTBinaryExpression.op_greaterEqual:
value = comparator.applyAsLong(v1, v2) >= 0 ? 1l : 0l;
break;
case IASTBinaryExpression.op_threewaycomparison:
// TODO: implement for <=>
value = comparator.applyAsLong(v1, v2);
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 = comparator.applyAsLong(v1, v2) >= 0 ? v1 : v2;
break;
case IASTBinaryExpression.op_min:
value = comparator.applyAsLong(v1, v2) <= 0 ? v1 : v2;
break;
}
if (value != null) {
return IntegralValue.create(value);
} else {
return IntegralValue.UNKNOWN;
}
}
public static IValue evaluateUnaryTypeIdExpression(int operator, IType type) {
IValue val = applyUnaryTypeIdOperator(operator, type);
if (isInvalidValue(val))
return IntegralValue.UNKNOWN;
return val;
}
public static IValue evaluateBinaryTypeIdExpression(IASTBinaryTypeIdExpression.Operator operator, IType type1,
IType type2) {
IValue val = applyBinaryTypeIdOperator(operator, type1, type2);
if (isInvalidValue(val))
return IntegralValue.UNKNOWN;
return val;
}
public static IValue evaluateNaryTypeIdExpression(Operator operator, IType[] operands, IBinding pointOfDefinition) {
IValue val = applyNaryTypeIdOperator(operator, operands, pointOfDefinition);
if (isInvalidValue(val))
return IntegralValue.UNKNOWN;
return val;
}
/**
* Computes the canonical representation of the value of the expression.
*/
private static IValue evaluate(IASTExpression exp) {
// Some C++ expression types can involve evaluating functions.
// For these, the value will be computed based on the evaluation.
if (exp instanceof ICPPASTFunctionCallExpression || exp instanceof ICPPASTSimpleTypeConstructorExpression
|| exp instanceof ICPPASTUnaryExpression || exp instanceof ICPPASTBinaryExpression) {
return null;
}
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.
Number numericValue = v.numberValue();
if (numericValue == null)
return IntegralValue.UNKNOWN;
if (v instanceof IntegralValue ? numericValue.longValue() == 0 : numericValue.doubleValue() == 0)
return evaluate(cexpr.getNegativeResultExpression());
final IASTExpression pe = cexpr.getPositiveResultExpression();
if (pe == null) // A 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);
}
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);
}
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) {
type = SemanticUtil.getNestedType(type, TDEF | CVTYPE);
switch (operator) {
case op_sizeof:
return getSize(type);
case op_alignof:
return getAlignment(type);
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) ? 1 : 0);
case op_has_trivial_destructor:
break; // TODO(sprigogin): Implement
case op_has_unique_object_representations:
break; // TODO: Implement
case op_has_virtual_destructor:
break; // TODO(sprigogin): Implement
case op_is_abstract:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isAbstract((ICPPClassType) type) ? 1 : 0);
case op_is_aggregate:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isAggregateClass((ICPPClassType) type) ? 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) ? 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) ? 1 : 0);
case op_is_polymorphic:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isPolymorphic((ICPPClassType) type) ? 1 : 0);
case op_is_standard_layout:
return IntegralValue.create(TypeTraits.isStandardLayout(type) ? 1 : 0);
case op_is_trivial:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isTrivial((ICPPClassType) type) ? 1 : 0);
case op_is_trivially_copyable:
return IntegralValue.create(TypeTraits.isTriviallyCopyable(type) ? 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) {
SizeAndAlignment sizeAndAlignment = SizeofCalculator.getSizeAndAlignment(type);
if (sizeAndAlignment == null)
return IntegralValue.UNKNOWN;
return IntegralValue.create(sizeAndAlignment.alignment);
}
private static IValue getSize(IType type) {
SizeAndAlignment sizeAndAlignment = SizeofCalculator.getSizeAndAlignment(type);
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, exp.getExpressionType());
}
private static IValue applyUnaryOperator(final int unaryOp, final IValue value, final IType type) {
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) {
if (type instanceof IBasicType basicType && basicType.isUnsigned()) {
SizeAndAlignment sizeAndAlignment = SizeofCalculator.getSizeAndAlignment(basicType);
if (sizeAndAlignment != null && sizeAndAlignment.size < 8) {
long range = (1L << (sizeAndAlignment.size * 8));
return IntegralValue.create(range - value.numberValue().longValue());
}
return IntegralValue.create(-value.numberValue().longValue());
}
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
IType exprType = exp.getExpressionType();
return evaluateBinaryExpression(op, o1, o2, exprType);
}
private static IValue applyBinaryTypeIdOperator(IASTBinaryTypeIdExpression.Operator operator, IType type1,
IType type2) {
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))) {
return IntegralValue.create(1);
}
return IntegralValue.create(0);
case __is_assignable:
return IntegralValue.UNKNOWN; // TODO: Implement.
case __is_nothrow_assignable:
return IntegralValue.UNKNOWN; // TODO: Implement.
case __is_same:
if (type1.isSameType(type2)) {
return IntegralValue.create(1);
}
return IntegralValue.create(0);
case __is_trivially_assignable:
return IntegralValue.UNKNOWN; // TODO: Implement.
}
return IntegralValue.UNKNOWN;
}
private static IValue applyNaryTypeIdOperator(ICPPASTNaryTypeIdExpression.Operator operator, IType[] operands,
IBinding pointOfDefinition) {
switch (operator) {
case __is_constructible:
case __is_trivially_constructible:
if (operands.length == 0) {
return IntegralValue.UNKNOWN;
}
boolean checkTrivial = (operator == Operator.__is_trivially_constructible);
IType typeToConstruct = operands[0];
IType[] argumentTypes = Arrays.copyOfRange(operands, 1, operands.length);
return IntegralValue.create(
TypeTraits.isConstructible(typeToConstruct, argumentTypes, pointOfDefinition, checkTrivial) ? 1
: 0);
case __is_nothrow_constructible:
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) {
try {
CPPSemantics.pushLookupPoint(expr);
IValue val = evaluate(expr);
if (val != null) {
return val.numberValue();
}
if (expr instanceof ICPPASTInitializerClause) {
ICPPEvaluation eval = ((ICPPASTInitializerClause) expr).getEvaluation();
if (eval.isConstantExpression() && !eval.isValueDependent())
return eval.getValue().numberValue();
}
return null;
} finally {
CPPSemantics.popLookupPoint();
}
}
}