forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppExpression.cxx
More file actions
2378 lines (2023 loc) · 56.1 KB
/
cppExpression.cxx
File metadata and controls
2378 lines (2023 loc) · 56.1 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file cppExpression.cxx
* @author drose
* @date 1999-10-25
*/
#include "cppExpression.h"
#include "cppToken.h"
#include "cppIdentifier.h"
#include "cppType.h"
#include "cppSimpleType.h"
#include "cppPointerType.h"
#include "cppEnumType.h"
#include "cppConstType.h"
#include "cppArrayType.h"
#include "cppPreprocessor.h"
#include "cppInstance.h"
#include "cppFunctionGroup.h"
#include "cppFunctionType.h"
#include "cppClosureType.h"
#include "cppReferenceType.h"
#include "cppStructType.h"
#include "cppBison.h"
#include "pdtoa.h"
#include <assert.h>
using std::cerr;
using std::string;
/**
*
*/
CPPExpression::Result::
Result() {
_type = RT_error;
}
/**
*
*/
CPPExpression::Result::
Result(int value) {
_type = RT_integer;
_u._integer = value;
}
/**
*
*/
CPPExpression::Result::
Result(double value) {
_type = RT_real;
_u._real = value;
}
/**
*
*/
CPPExpression::Result::
Result(void *value) {
_type = RT_pointer;
_u._pointer = value;
}
/**
*
*/
int CPPExpression::Result::
as_integer() const {
switch (_type) {
case RT_integer:
return _u._integer;
case RT_real:
return (int)_u._real;
case RT_pointer:
// We don't mind if this loses precision.
return (int)(intptr_t)(_u._pointer);
default:
cerr << "Invalid type\n";
assert(false);
return 0;
}
}
/**
*
*/
double CPPExpression::Result::
as_real() const {
switch (_type) {
case RT_integer:
return (double)_u._integer;
case RT_real:
return _u._real;
case RT_pointer:
// We don't mind if this loses precision.
return (double)(uintptr_t)(_u._pointer);
default:
cerr << "Invalid type\n";
assert(false);
return 0.0;
}
}
/**
*
*/
void *CPPExpression::Result::
as_pointer() const {
switch (_type) {
case RT_integer:
return (void *)(intptr_t)_u._integer;
case RT_real:
return (void *)(uintptr_t)_u._real;
case RT_pointer:
return _u._pointer;
default:
cerr << "Invalid type\n";
assert(false);
return nullptr;
}
}
/**
*
*/
bool CPPExpression::Result::
as_boolean() const {
switch (_type) {
case RT_integer:
return (_u._integer != 0);
case RT_real:
return (_u._real != 0.0);
case RT_pointer:
return (_u._pointer != nullptr);
default:
cerr << "Invalid type\n";
assert(false);
return false;
}
}
/**
*
*/
void CPPExpression::Result::
output(std::ostream &out) const {
switch (_type) {
case RT_integer:
out << _u._integer;
break;
case RT_real:
out << _u._real;
break;
case RT_pointer:
out << _u._pointer;
break;
case RT_error:
out << "(error)";
break;
default:
out << "(**invalid type**)\n";
}
}
/**
*
*/
CPPExpression::
CPPExpression(bool value) :
CPPDeclaration(CPPFile())
{
_type = T_boolean;
_u._boolean = value;
}
/**
*
*/
CPPExpression::
CPPExpression(unsigned long long value) :
CPPDeclaration(CPPFile())
{
_type = T_integer;
_u._integer = value;
}
/**
*
*/
CPPExpression::
CPPExpression(int value) :
CPPDeclaration(CPPFile())
{
_type = T_integer;
_u._integer = value;
}
/**
*
*/
CPPExpression::
CPPExpression(long double value) :
CPPDeclaration(CPPFile())
{
_type = T_real;
_u._real = value;
}
/**
*
*/
CPPExpression::
CPPExpression(const string &value) :
CPPDeclaration(CPPFile())
{
_type = T_string;
_str = value;
}
/**
*
*/
CPPExpression::
CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
CPPScope *global_scope, CPPPreprocessor *error_sink) :
CPPDeclaration(CPPFile())
{
CPPDeclaration *decl =
ident->find_symbol(current_scope, global_scope);
if (decl != nullptr) {
CPPInstance *inst = decl->as_instance();
if (inst != nullptr) {
_type = T_variable;
_u._variable = inst;
return;
}
/*CPPFunctionGroup *fgroup = decl->as_function_group();
if (fgroup != nullptr) {
_type = T_function;
_u._fgroup = fgroup;
return;
}*/
}
_type = T_unknown_ident;
_u._ident = ident;
// _u._ident->_native_scope = current_scope;
}
/**
*
*/
CPPExpression::
CPPExpression(int unary_operator, CPPExpression *op1) :
CPPDeclaration(CPPFile())
{
_type = T_unary_operation;
_u._op._operator = unary_operator;
_u._op._op1 = op1;
_u._op._op2 = nullptr;
_u._op._op3 = nullptr;
}
/**
*
*/
CPPExpression::
CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2) :
CPPDeclaration(CPPFile())
{
_type = T_binary_operation;
_u._op._operator = binary_operator;
_u._op._op1 = op1;
_u._op._op2 = op2;
_u._op._op3 = nullptr;
}
/**
*
*/
CPPExpression::
CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2,
CPPExpression *op3) :
CPPDeclaration(CPPFile())
{
_type = T_trinary_operation;
_u._op._operator = trinary_operator;
_u._op._op1 = op1;
_u._op._op2 = op2;
_u._op._op3 = op3;
}
/**
* Creates an expression that represents a typecast operation.
*/
CPPExpression CPPExpression::
typecast_op(CPPType *type, CPPExpression *op1, Type cast_type) {
assert(cast_type >= T_typecast && cast_type <= T_reinterpret_cast);
CPPExpression expr(0);
expr._type = cast_type;
expr._u._typecast._to = type;
expr._u._typecast._op1 = op1;
return expr;
}
/**
* Creates an expression that represents a constructor call.
*/
CPPExpression CPPExpression::
construct_op(CPPType *type, CPPExpression *op1) {
CPPExpression expr(0);
if (op1 == nullptr) {
// A default constructor call--no parameters.
expr._type = T_default_construct;
expr._u._typecast._to = type;
expr._u._typecast._op1 = nullptr;
} else {
// A normal constructor call, with parameters.
expr._type = T_construct;
expr._u._typecast._to = type;
expr._u._typecast._op1 = op1;
}
return expr;
}
/**
* Creates an expression that represents an aggregate initialization.
*/
CPPExpression CPPExpression::
aggregate_init_op(CPPType *type, CPPExpression *op1) {
CPPExpression expr(0);
if (op1 == nullptr) {
expr._type = T_empty_aggregate_init;
} else {
expr._type = T_aggregate_init;
}
expr._u._typecast._to = type;
expr._u._typecast._op1 = op1;
return expr;
}
/**
* Creates an expression that represents a use of the new operator.
*/
CPPExpression CPPExpression::
new_op(CPPType *type, CPPExpression *op1) {
CPPExpression expr(0);
if (op1 == nullptr) {
// A default new operation--no parameters.
expr._type = T_default_new;
expr._u._typecast._to = type;
expr._u._typecast._op1 = nullptr;
} else {
// A normal new operation, with parameters.
expr._type = T_new;
expr._u._typecast._to = type;
expr._u._typecast._op1 = op1;
}
return expr;
}
/**
* Creates an expression that represents a use of the typeid operator.
*/
CPPExpression CPPExpression::
typeid_op(CPPType *type, CPPType *std_type_info) {
CPPExpression expr(0);
expr._type = T_typeid_type;
expr._u._typeid._type = type;
expr._u._typeid._std_type_info = std_type_info;
return expr;
}
/**
* Creates an expression that represents a use of the typeid operator.
*/
CPPExpression CPPExpression::
typeid_op(CPPExpression *op1, CPPType *std_type_info) {
CPPExpression expr(0);
expr._type = T_typeid_expr;
expr._u._typeid._expr = op1;
expr._u._typeid._std_type_info = std_type_info;
return expr;
}
/**
* Creates an expression that returns a particular type trait.
*/
CPPExpression CPPExpression::
type_trait(int trait, CPPType *type, CPPType *arg) {
CPPExpression expr(0);
expr._type = T_type_trait;
expr._u._type_trait._trait = trait;
expr._u._type_trait._type = type;
expr._u._type_trait._arg = arg;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
sizeof_func(CPPType *type) {
CPPExpression expr(0);
expr._type = T_sizeof;
expr._u._typecast._to = type;
expr._u._typecast._op1 = nullptr;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
sizeof_ellipsis_func(CPPIdentifier *ident) {
CPPExpression expr(0);
expr._type = T_sizeof_ellipsis;
expr._u._ident = ident;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
alignof_func(CPPType *type) {
CPPExpression expr(0);
expr._type = T_alignof;
expr._u._typecast._to = type;
expr._u._typecast._op1 = nullptr;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
lambda(CPPClosureType *type) {
CPPExpression expr(0);
expr._type = T_lambda;
expr._u._closure_type = type;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
literal(unsigned long long value, CPPInstance *lit_op) {
CPPExpression expr(0);
expr._type = T_literal;
expr._u._literal._value = new CPPExpression(value);
expr._u._literal._operator = lit_op;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
literal(long double value, CPPInstance *lit_op) {
CPPExpression expr(0);
expr._type = T_literal;
expr._u._literal._value = new CPPExpression(value);
expr._u._literal._operator = lit_op;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
literal(CPPExpression *value, CPPInstance *lit_op) {
CPPExpression expr(0);
expr._type = T_literal;
expr._u._literal._value = value;
expr._u._literal._operator = lit_op;
return expr;
}
/**
*
*/
CPPExpression CPPExpression::
raw_literal(const string &raw, CPPInstance *lit_op) {
CPPExpression expr(0);
expr._type = T_raw_literal;
expr._str = raw;
expr._u._literal._value = nullptr;
expr._u._literal._operator = lit_op;
return expr;
}
/**
*
*/
const CPPExpression &CPPExpression::
get_nullptr() {
static CPPExpression expr(0);
expr._type = T_nullptr;
return expr;
}
/**
*
*/
const CPPExpression &CPPExpression::
get_default() {
static CPPExpression expr(0);
expr._type = T_default;
return expr;
}
/**
*
*/
const CPPExpression &CPPExpression::
get_delete() {
static CPPExpression expr(0);
expr._type = T_delete;
return expr;
}
/**
*
*/
CPPExpression::Result CPPExpression::
evaluate() const {
Result r1, r2;
switch (_type) {
case T_nullptr:
return Result(nullptr);
case T_boolean:
return Result((int)_u._boolean);
case T_integer:
return Result((int)_u._integer);
case T_real:
return Result((double)_u._real);
case T_string:
case T_wstring:
case T_u8string:
case T_u16string:
case T_u32string:
return Result();
case T_variable:
if (_u._variable->_type != nullptr &&
_u._variable->_initializer != nullptr) {
// A constexpr variable, which is treated as const.
if (_u._variable->_storage_class & CPPInstance::SC_constexpr) {
return _u._variable->_initializer->evaluate();
}
// A const variable. Fetch its assigned value.
CPPConstType *const_type = _u._variable->_type->as_const_type();
if (const_type != nullptr) {
return _u._variable->_initializer->evaluate();
}
}
return Result();
case T_function:
return Result();
case T_unknown_ident:
return Result();
case T_typecast:
case T_static_cast:
case T_dynamic_cast:
case T_const_cast:
case T_reinterpret_cast:
assert(_u._typecast._op1 != nullptr);
r1 = _u._typecast._op1->evaluate();
if (r1._type != RT_error) {
CPPSimpleType *stype = _u._typecast._to->as_simple_type();
if (stype != nullptr) {
if (stype->_type == CPPSimpleType::T_bool) {
return Result(r1.as_boolean());
} else if (stype->_type == CPPSimpleType::T_int) {
return Result(r1.as_integer());
} else if (stype->_type == CPPSimpleType::T_float ||
stype->_type == CPPSimpleType::T_double) {
return Result(r1.as_real());
}
}
if (_u._typecast._to->as_pointer_type()) {
return Result(r1.as_pointer());
}
}
return Result();
case T_construct:
case T_default_construct:
case T_aggregate_init:
case T_empty_aggregate_init:
case T_new:
case T_default_new:
case T_sizeof:
case T_sizeof_ellipsis:
return Result();
case T_alignof:
if (_u._typecast._to != nullptr) {
// Check if the type is defined with an alignas. TODO: this should
// probably be moved to a virtual getter on CPPType.
CPPExtensionType *etype = _u._typecast._to->as_extension_type();
if (etype != nullptr && etype->_alignment != nullptr) {
return etype->_alignment->evaluate();
}
}
return Result();
case T_binary_operation:
assert(_u._op._op2 != nullptr);
r2 = _u._op._op2->evaluate();
// The operators && and || are special cases: these are shirt-circuiting
// operators. Thus, if we are using either of these it might be
// acceptable for the second operand to be invalid, since we might never
// evaluate it.
// In all other cases, both operands must be valid in order for the
// operation to be valid.
if (r2._type == RT_error &&
(_u._op._operator != OROR && _u._op._operator != ANDAND)) {
return r2;
}
// Fall through
case T_trinary_operation:
// The trinary operator is also a short-circuiting operator: we don't test
// the second or third operands until we need them. The only critical one
// is the first operand.
// Fall through
case T_unary_operation:
assert(_u._op._op1 != nullptr);
r1 = _u._op._op1->evaluate();
if (r1._type == RT_error) {
// Here's one more special case: if the first operand is invalid, it
// really means we don't know how to evaluate it. However, if the
// operator is ||, then it might not matter as long as we can evaluate
// the second one *and* that comes out to be true.
if (_u._op._operator == OROR && r2._type == RT_integer &&
r2.as_boolean()) {
return r2;
}
// Ditto for the operator being && and the second one coming out false.
if (_u._op._operator == ANDAND && r2._type == RT_integer &&
!r2.as_boolean()) {
return r2;
}
// Also for the operator being [] and the operand being a string.
if (_u._op._operator == '[' && r2._type == RT_integer &&
(_u._op._op1->_type == T_string ||
_u._op._op1->_type == T_u8string)) {
int index = (int)r2.as_integer();
if ((size_t)index == _u._op._op1->_str.size()) {
return Result(0);
} else if (index >= 0 && (size_t)index < _u._op._op1->_str.size()) {
return Result(_u._op._op1->_str[(size_t)index]);
} else {
cerr << "array index " << index << " out of bounds of string literal "
<< *_u._op._op1 << "\n";
}
}
return r1;
}
switch (_u._op._operator) {
case UNARY_NOT:
return Result(!r1.as_boolean());
case UNARY_NEGATE:
return Result(~r1.as_integer());
case UNARY_MINUS:
return (r1._type == RT_real) ? Result(-r1.as_real()) : Result(-r1.as_integer());
case UNARY_PLUS:
return r1;
case UNARY_STAR:
case UNARY_REF:
return Result();
case '*':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() * r2.as_real());
} else {
return Result(r1.as_integer() * r2.as_integer());
}
case '/':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() / r2.as_real());
} else {
return Result(r1.as_integer() / r2.as_integer());
}
case '%':
return Result(r1.as_integer() % r2.as_integer());
case '+':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() + r2.as_real());
} else {
return Result(r1.as_integer() + r2.as_integer());
}
case '-':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() - r2.as_real());
} else {
return Result(r1.as_integer() - r2.as_integer());
}
case '|':
return Result(r1.as_integer() | r2.as_integer());
case '&':
return Result(r1.as_integer() & r2.as_integer());
case OROR:
if (r1.as_boolean()) {
return r1;
} else {
return r2;
}
case ANDAND:
if (r1.as_boolean()) {
return r2;
} else {
return r1;
}
case EQCOMPARE:
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() == r2.as_real());
} else {
return Result(r1.as_integer() == r2.as_integer());
}
case NECOMPARE:
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() != r2.as_real());
} else {
return Result(r1.as_integer() != r2.as_integer());
}
case LECOMPARE:
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() <= r2.as_real());
} else {
return Result(r1.as_integer() <= r2.as_integer());
}
case GECOMPARE:
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() >= r2.as_real());
} else {
return Result(r1.as_integer() >= r2.as_integer());
}
case '<':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() < r2.as_real());
} else {
return Result(r1.as_integer() < r2.as_integer());
}
case '>':
if (r1._type == RT_real || r2._type == RT_real) {
return Result(r1.as_real() > r2.as_real());
} else {
return Result(r1.as_integer() > r2.as_integer());
}
case LSHIFT:
return Result(r1.as_integer() << r2.as_integer());
case RSHIFT:
return Result(r1.as_integer() >> r2.as_integer());
case '?':
return r1.as_integer() ?
_u._op._op2->evaluate() : _u._op._op3->evaluate();
case '.':
case POINTSAT:
return Result();
case '[': // Array element reference
return Result();
case 'f': // Function evaluation
return Result();
case ',':
return r2;
default:
cerr << "**unexpected operator**\n";
abort();
}
case T_literal:
case T_raw_literal:
return Result();
case T_typeid_type:
case T_typeid_expr:
return Result();
case T_type_trait:
switch (_u._type_trait._trait) {
case KW_HAS_VIRTUAL_DESTRUCTOR:
{
CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
return Result(struct_type != nullptr && struct_type->has_virtual_destructor());
}
case KW_IS_ABSTRACT:
{
CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
return Result(struct_type != nullptr && struct_type->is_abstract());
}
case KW_IS_BASE_OF:
{
CPPStructType *struct_type1 = _u._type_trait._type->as_struct_type();
CPPStructType *struct_type2 = _u._type_trait._arg->as_struct_type();
return Result(struct_type1 != nullptr && struct_type2 != nullptr && struct_type1->is_base_of(struct_type2));
}
case KW_IS_CLASS:
{
CPPExtensionType *ext_type = _u._type_trait._type->as_extension_type();
return Result(ext_type != nullptr && (
ext_type->_type == CPPExtensionType::T_class ||
ext_type->_type == CPPExtensionType::T_struct));
}
case KW_IS_CONSTRUCTIBLE:
if (_u._type_trait._arg == nullptr) {
return Result(_u._type_trait._type->is_default_constructible());
} else {
return Result(_u._type_trait._type->is_constructible(_u._type_trait._arg));
}
case KW_IS_CONVERTIBLE_TO:
assert(_u._type_trait._arg != nullptr);
return Result(_u._type_trait._type->is_convertible_to(_u._type_trait._arg));
case KW_IS_DESTRUCTIBLE:
return Result(_u._type_trait._type->is_destructible());
case KW_IS_EMPTY:
{
CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
return Result(struct_type != nullptr && struct_type->is_empty());
}
case KW_IS_ENUM:
return Result(_u._type_trait._type->is_enum());
case KW_IS_FINAL:
{
CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
return Result(struct_type != nullptr && struct_type->is_final());
}
case KW_IS_FUNDAMENTAL:
return Result(_u._type_trait._type->is_fundamental());
case KW_IS_POD:
return Result(_u._type_trait._type->is_trivial() &&
_u._type_trait._type->is_standard_layout());
case KW_IS_POLYMORPHIC:
{
CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
return Result(struct_type != nullptr && struct_type->is_polymorphic());
}
case KW_IS_STANDARD_LAYOUT:
return Result(_u._type_trait._type->is_standard_layout());
case KW_IS_TRIVIAL:
return Result(_u._type_trait._type->is_trivial());
case KW_IS_UNION:
{
CPPExtensionType *ext_type = _u._type_trait._type->as_extension_type();
return Result(ext_type != nullptr &&
ext_type->_type == CPPExtensionType::T_union);
}
default:
cerr << "**unexpected type trait**\n";
abort();
}
default:
cerr << "**invalid operand**\n";
abort();
}
return Result(); // Compiler kludge; can't get here.
}
/**
* Returns the type of the expression, if it is known, or NULL if the type
* cannot be determined.
*/
CPPType *CPPExpression::
determine_type() const {
CPPType *t1 = nullptr;
CPPType *t2 = nullptr;
static CPPType *nullptr_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_nullptr));
static CPPType *int_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
static CPPType *unsigned_long_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
CPPSimpleType::F_unsigned |
CPPSimpleType::F_long));
static CPPType *bool_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
static CPPType *float_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
static CPPType *char_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
static CPPType *wchar_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
static CPPType *char16_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char16_t));
static CPPType *char32_type =
CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char32_t));
static CPPType *char_str_type = CPPType::new_type(
new CPPPointerType(CPPType::new_type(new CPPConstType(char_type))));
static CPPType *wchar_str_type = CPPType::new_type(
new CPPPointerType(CPPType::new_type(new CPPConstType(wchar_type))));
static CPPType *char16_str_type = CPPType::new_type(
new CPPPointerType(CPPType::new_type(new CPPConstType(char16_type))));
static CPPType *char32_str_type = CPPType::new_type(