-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathtypeCheckExpr.cpp
2375 lines (2183 loc) · 98 KB
/
typeCheckExpr.cpp
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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "constantTypeSubstitution.h"
#include "frontends/p4/enumInstance.h"
#include "lib/algorithm.h"
#include "typeChecker.h"
#include "typeConstraints.h"
namespace P4 {
using namespace literals;
const IR::Node *TypeInferenceBase::postorder(const IR::Parameter *param) {
if (done()) return param;
const IR::Type *paramType = getTypeType(param->type);
if (paramType == nullptr) return param;
BUG_CHECK(!paramType->is<IR::Type_Type>(), "%1%: unexpected type", paramType);
if (paramType->is<IR::P4Control>() || paramType->is<IR::P4Parser>()) {
typeError("%1%: parameter cannot have type %2%", param, paramType);
return param;
}
if (!readOnly && paramType->is<IR::Type_InfInt>()) {
// We only give these errors if we are no in 'readOnly' mode:
// this prevents giving a confusing error message to the user.
if (param->direction != IR::Direction::None) {
typeError("%1%: parameters with type %2% must be directionless", param, paramType);
return param;
}
if (isInContext<IR::P4Action>()) {
typeError("%1%: actions cannot have parameters with type %2%", param, paramType);
return param;
}
}
// The parameter type cannot have free type variables
if (auto *gen = paramType->to<IR::IMayBeGenericType>()) {
auto tp = gen->getTypeParameters();
if (!tp->empty()) {
typeError("Type parameters needed for %1%", param->name);
return param;
}
}
if (param->defaultValue) {
if (!typeMap->isCompileTimeConstant(param->defaultValue))
typeError("%1%: expression must be a compile-time constant", param->defaultValue);
}
setType(getOriginal(), paramType);
setType(param, paramType);
return param;
}
const IR::Node *TypeInferenceBase::postorder(const IR::Constant *expression) {
if (done()) return expression;
auto type = getTypeType(expression->type);
if (type == nullptr) return expression;
setType(getOriginal(), type);
setType(expression, type);
setCompileTimeConstant(getOriginal<IR::Expression>());
setCompileTimeConstant(expression);
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::StringLiteral *expression) {
if (done()) return expression;
setType(getOriginal(), IR::Type_String::get());
setType(expression, IR::Type_String::get());
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::BoolLiteral *expression) {
if (done()) return expression;
setType(getOriginal(), IR::Type_Boolean::get());
setType(expression, IR::Type_Boolean::get());
setCompileTimeConstant(getOriginal<IR::Expression>());
setCompileTimeConstant(expression);
return expression;
}
bool TypeInferenceBase::containsActionEnum(const IR::Type *type) const {
if (auto st = type->to<IR::Type_Struct>()) {
if (auto field = st->getField(IR::Type_Table::action_run)) {
auto ft = getTypeType(field->type);
if (ft->is<IR::Type_ActionEnum>()) return true;
}
}
return false;
}
// Returns false on error
bool TypeInferenceBase::compare(const IR::Node *errorPosition, const IR::Type *ltype,
const IR::Type *rtype, Comparison *compare) {
if (ltype->is<IR::Type_Action>() || rtype->is<IR::Type_Action>()) {
// Actions return Type_Action instead of void.
typeError("%1% and %2% cannot be compared", compare->left, compare->right);
return false;
}
if (ltype->is<IR::Type_Table>() || rtype->is<IR::Type_Table>()) {
typeError("%1% and %2%: tables cannot be compared", compare->left, compare->right);
return false;
}
if (ltype->is<IR::Type_Extern>() || rtype->is<IR::Type_Extern>()) {
typeError("%1% and %2%: externs cannot be compared", compare->left, compare->right);
return false;
}
if (containsActionEnum(ltype) || containsActionEnum(rtype)) {
typeError("%1% and %2%: table application results cannot be compared", compare->left,
compare->right);
return false;
}
bool defined = false;
if (typeMap->equivalent(ltype, rtype) &&
(!ltype->is<IR::Type_Void>() && !ltype->is<IR::Type_Varbits>()) &&
!ltype->to<IR::Type_UnknownStruct>()) {
defined = true;
} else if (ltype->is<IR::Type_Base>() && rtype->is<IR::Type_Base>() &&
typeMap->equivalent(ltype, rtype)) {
defined = true;
} else if (ltype->is<IR::Type_BaseList>() && rtype->is<IR::Type_BaseList>()) {
auto tvs = unify(errorPosition, ltype, rtype);
if (tvs == nullptr) return false;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
compare->left = cts.convert(compare->left, getChildContext());
compare->right = cts.convert(compare->right, getChildContext());
}
defined = true;
} else if (auto se = rtype->to<IR::Type_SerEnum>()) {
// This can only happen in a switch statement, other comparisons
// eliminate SerEnums before calling here.
if (typeMap->equivalent(ltype, se->type)) defined = true;
} else {
auto ls = ltype->to<IR::Type_UnknownStruct>();
auto rs = rtype->to<IR::Type_UnknownStruct>();
if (ls != nullptr || rs != nullptr) {
if (ls != nullptr && rs != nullptr) {
typeError("%1%: cannot compare structure-valued expressions with unknown types",
errorPosition);
return false;
}
bool lcst = isCompileTimeConstant(compare->left);
bool rcst = isCompileTimeConstant(compare->right);
TypeVariableSubstitution *tvs;
if (ls == nullptr) {
tvs = unify(errorPosition, ltype, rtype);
} else {
tvs = unify(errorPosition, rtype, ltype);
}
if (tvs == nullptr) return false;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
compare->left = cts.convert(compare->left, getChildContext());
compare->right = cts.convert(compare->right, getChildContext());
}
if (ls != nullptr) {
auto l = compare->left->to<IR::StructExpression>();
CHECK_NULL(l); // struct initializers are the only expressions that can
// have StructUnknown types
BUG_CHECK(rtype->is<IR::Type_StructLike>(), "%1%: expected a struct", rtype);
auto type = new IR::Type_Name(rtype->to<IR::Type_StructLike>()->name);
compare->left =
new IR::StructExpression(compare->left->srcInfo, type, type, l->components);
setType(compare->left, rtype);
if (lcst) setCompileTimeConstant(compare->left);
} else {
auto r = compare->right->to<IR::StructExpression>();
CHECK_NULL(r); // struct initializers are the only expressions that can
// have StructUnknown types
BUG_CHECK(ltype->is<IR::Type_StructLike>(), "%1%: expected a struct", ltype);
auto type = new IR::Type_Name(ltype->to<IR::Type_StructLike>()->name);
compare->right =
new IR::StructExpression(compare->right->srcInfo, type, type, r->components);
setType(compare->right, rtype);
if (rcst) setCompileTimeConstant(compare->right);
}
defined = true;
}
// comparison between structs and list expressions is allowed
if ((ltype->is<IR::Type_StructLike>() && rtype->is<IR::Type_List>()) ||
(ltype->is<IR::Type_List>() && rtype->is<IR::Type_StructLike>())) {
if (!ltype->is<IR::Type_StructLike>()) {
// swap
auto type = ltype;
ltype = rtype;
rtype = type;
}
auto tvs = unify(errorPosition, ltype, rtype);
if (tvs == nullptr) return false;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
compare->left = cts.convert(compare->left, getChildContext());
compare->right = cts.convert(compare->right, getChildContext());
}
defined = true;
}
}
if (!defined) {
typeError("'%1%' with type '%2%' cannot be compared to '%3%' with type '%4%'",
compare->left, ltype, compare->right, rtype);
return false;
}
return true;
}
const IR::Node *TypeInferenceBase::postorder(const IR::Operation_Relation *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
bool equTest = expression->is<IR::Equ>() || expression->is<IR::Neq>();
if (auto l = ltype->to<IR::Type_SerEnum>()) ltype = getTypeType(l->type);
if (auto r = rtype->to<IR::Type_SerEnum>()) rtype = getTypeType(r->type);
BUG_CHECK(ltype && rtype, "Invalid Type_SerEnum/getTypeType");
if (ltype->is<IR::Type_InfInt>() && rtype->is<IR::Type_InfInt>()) {
// This can happen because we are replacing some constant functions with
// constants during type checking
auto result = constantFold(expression);
setType(getOriginal(), IR::Type_Boolean::get());
setCompileTimeConstant(result);
setCompileTimeConstant(getOriginal<IR::Expression>());
return result;
} else if (ltype->is<IR::Type_InfInt>() && rtype->is<IR::Type_Bits>()) {
auto e = expression->clone();
e->left = new IR::Cast(e->left->srcInfo, rtype, e->left);
setType(e->left, rtype);
ltype = rtype;
expression = e;
} else if (rtype->is<IR::Type_InfInt>() && ltype->is<IR::Type_Bits>()) {
auto e = expression->clone();
e->right = new IR::Cast(e->right->srcInfo, ltype, e->right);
setType(e->right, ltype);
rtype = ltype;
expression = e;
}
if (equTest) {
Comparison c;
c.left = expression->left;
c.right = expression->right;
auto b = compare(expression, ltype, rtype, &c);
if (!b) return expression;
if (expression->left != c.left || expression->right != c.right) {
auto e = expression->clone();
e->left = c.left;
e->right = c.right;
expression = e;
}
} else {
if (!ltype->is<IR::Type_Bits>() || !rtype->is<IR::Type_Bits>() || !(ltype->equiv(*rtype))) {
typeError("%1%: not defined on %2% and %3%", expression, ltype->toString(),
rtype->toString());
return expression;
}
}
setType(getOriginal(), IR::Type_Boolean::get());
setType(expression, IR::Type_Boolean::get());
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::Concat *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
const IR::Type *resultType = nullptr;
if (ltype->is<IR::Type_String>() && rtype->is<IR::Type_String>()) {
// For strings the output type is just string.
resultType = ltype;
} else {
// All the integer concatenations, including with serializable enums.
bool castLeft = false;
bool castRight = false;
if (auto se = ltype->to<IR::Type_SerEnum>()) {
ltype = getTypeType(se->type);
castLeft = true;
}
if (auto se = rtype->to<IR::Type_SerEnum>()) {
rtype = getTypeType(se->type);
castRight = true;
}
if (ltype == nullptr || rtype == nullptr) {
// getTypeType should have already taken care of the error message
return expression;
}
if (!ltype->is<IR::Type_Bits>() || !rtype->is<IR::Type_Bits>()) {
typeError("%1%: Concatenation not defined on %2% and %3%", expression,
ltype->toString(), rtype->toString());
// Provide further clarification in the common case of attempting to concat
// arbitrary-width int.
if (!ltype->is<IR::Type_String>() && !rtype->is<IR::Type_String>()) {
if (ltype->is<IR::Type_InfInt>()) {
typeError("Please specify a width for the operand %1% of a concatenation",
expression->left);
return expression;
}
if (rtype->is<IR::Type_InfInt>()) {
typeError("Please specify a width for the operand %1% of a concatenation",
expression->right);
return expression;
}
}
return expression;
}
auto bl = ltype->to<IR::Type_Bits>();
auto br = rtype->to<IR::Type_Bits>();
resultType = IR::Type_Bits::get(bl->size + br->size, bl->isSigned);
if (castLeft) {
auto e = expression->clone();
e->left = new IR::Cast(e->left->srcInfo, bl, e->left);
if (isCompileTimeConstant(expression->left)) setCompileTimeConstant(e->left);
setType(e->left, ltype);
expression = e;
}
if (castRight) {
auto e = expression->clone();
e->right = new IR::Cast(e->right->srcInfo, br, e->right);
if (isCompileTimeConstant(expression->right)) setCompileTimeConstant(e->right);
setType(e->right, rtype);
expression = e;
}
}
resultType = canonicalize(resultType);
if (resultType != nullptr) {
setType(getOriginal(), resultType);
setType(expression, resultType);
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
}
return expression;
}
/**
* compute the type of table keys.
* Used to typecheck pre-defined entries.
*/
const IR::Node *TypeInferenceBase::postorder(const IR::Key *key) {
// compute the type and store it in typeMap
auto keyTuple = new IR::Type_Tuple;
for (auto ke : key->keyElements) {
auto kt = typeMap->getType(ke->expression);
if (kt == nullptr) {
LOG2("Bailing out for " << dbp(ke));
return key;
}
keyTuple->components.push_back(kt);
}
LOG2("Setting key type to " << dbp(keyTuple));
setType(key, keyTuple);
setType(getOriginal(), keyTuple);
return key;
}
/**
* typecheck a table initializer entry list
*/
TypeInferenceBase::PreorderResult TypeInferenceBase::preorder(const IR::EntriesList *el) {
if (done()) return {el, false};
auto table = findContext<IR::P4Table>();
BUG_CHECK(table != nullptr, "%1% entries not within a table", el);
const IR::Key *key = table->getKey();
if (key == nullptr) {
if (el->size() != 0)
typeError("Entries cannot be specified for a table with no key %1%", table);
return {el, true};
}
auto keyTuple = typeMap->getType(key); // direct typeMap call to skip checks
if (keyTuple == nullptr) {
// The keys have to be before the entries list. If they are not,
// at this point they have not yet been type-checked.
if (key->srcInfo.isValid() && el->srcInfo.isValid() && key->srcInfo >= el->srcInfo) {
typeError("%1%: Entries list must be after table key %2%", el, key);
return {el, true};
}
// otherwise the type-checking of the keys must have failed
}
return {el, false};
}
/**
* typecheck a table initializer entry
*
* The invariants are:
* - table keys and entry keys must have the same length
* - entry key elements must be compile time constants
* - actionRefs in entries must be in the action list
* - table keys must have been type checked before entries
*
* Moreover, the EntriesList visitor should have checked for the table
* invariants.
*/
const IR::Node *TypeInferenceBase::postorder(const IR::Entry *entry) {
if (done()) return entry;
auto table = findContext<IR::P4Table>();
if (table == nullptr) return entry;
const IR::Key *key = table->getKey();
if (key == nullptr) return entry;
auto keyTuple = getType(key);
if (keyTuple == nullptr) return entry;
auto entryKeyType = getType(entry->keys);
if (entryKeyType == nullptr) return entry;
if (auto ts = entryKeyType->to<IR::Type_Set>()) entryKeyType = ts->elementType;
if (entry->singleton) {
if (auto tl = entryKeyType->to<IR::Type_BaseList>()) {
// An entry of _ does not have type Tuple<Type_Dontcare>, but rather Type_Dontcare
if (tl->getSize() == 1 && tl->components.at(0)->is<IR::Type_Dontcare>())
entryKeyType = tl->components.at(0);
}
}
auto keyset = entry->getKeys();
if (keyset == nullptr || !(keyset->is<IR::ListExpression>())) {
typeError("%1%: key expression must be tuple", keyset);
return entry;
}
if (keyset->components.size() < key->keyElements.size()) {
typeError("%1%: Size of entry keyset must match the table key set size", keyset);
return entry;
}
bool nonConstantKeys = false;
for (auto ke : keyset->components)
if (!isCompileTimeConstant(ke)) {
typeError("Key entry must be a compile time constant: %1%", ke);
nonConstantKeys = true;
}
if (nonConstantKeys) return entry;
if (entry->priority && !isCompileTimeConstant(entry->priority)) {
typeError("Entry priority must be a compile time constant: %1%", entry->priority);
return entry;
}
TypeVariableSubstitution *tvs =
unifyCast(entry, keyTuple, entryKeyType,
"Table entry has type '%1%' which is not the expected type '%2%'",
{keyTuple, entryKeyType});
if (tvs == nullptr) return entry;
ConstantTypeSubstitution cts(tvs, typeMap, this);
auto ks = cts.convert(keyset, getChildContext());
if (::P4::errorCount() > 0) return entry;
if (ks != keyset)
entry = new IR::Entry(entry->srcInfo, entry->annotations, entry->isConst, entry->priority,
ks->to<IR::ListExpression>(), entry->action, entry->singleton);
if (auto ale = validateActionInitializer(entry->getAction())) {
if (ale->hasAnnotation(IR::Annotation::defaultOnlyAnnotation)) {
typeError("%1%: Action marked with %2% used in table", entry,
IR::Annotation::defaultOnlyAnnotation);
return entry;
}
}
return entry;
}
const IR::Node *TypeInferenceBase::postorder(const IR::ListExpression *expression) {
if (done()) return expression;
bool constant = true;
IR::Vector<IR::Type> components;
for (auto c : expression->components) {
if (!isCompileTimeConstant(c)) constant = false;
auto type = getType(c);
if (type == nullptr) return expression;
components.push_back(type);
}
auto tupleType = new IR::Type_List(expression->srcInfo, std::move(components));
auto type = canonicalize(tupleType);
if (type == nullptr) return expression;
setType(getOriginal(), type);
setType(expression, type);
if (constant) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::Invalid *expression) {
if (done()) return expression;
auto unk = IR::Type_Unknown::get();
setType(expression, unk);
setType(getOriginal(), unk);
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::InvalidHeader *expression) {
if (done()) return expression;
auto type = getTypeType(expression->headerType);
auto concreteType = type;
if (auto ts = concreteType->to<IR::Type_SpecializedCanonical>()) concreteType = ts->substituted;
if (!concreteType->is<IR::Type_Header>()) {
typeError("%1%: invalid header expression has a non-header type `%2%`", expression, type);
return expression;
}
setType(expression, type);
setType(getOriginal(), type);
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::InvalidHeaderUnion *expression) {
if (done()) return expression;
auto type = getTypeType(expression->headerUnionType);
auto concreteType = type;
if (auto ts = concreteType->to<IR::Type_SpecializedCanonical>()) concreteType = ts->substituted;
if (!concreteType->is<IR::Type_HeaderUnion>()) {
typeError("%1%: does not have a header_union type `%2%`", expression, type);
return expression;
}
setType(expression, type);
setType(getOriginal(), type);
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::P4ListExpression *expression) {
if (done()) return expression;
bool constant = true;
auto elementType = getTypeType(expression->elementType);
IR::Vector<IR::Expression> vec;
bool changed = false;
for (auto c : expression->components) {
if (!isCompileTimeConstant(c)) constant = false;
auto type = getType(c);
if (type == nullptr) return expression;
auto tvs = unify(expression, elementType, type,
"Vector element type '%1%' does not match expected type '%2%'",
{type, elementType});
if (tvs == nullptr) return expression;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
auto converted = cts.convert(c, getChildContext());
vec.push_back(converted);
changed = changed || converted != c;
} else {
vec.push_back(c);
}
}
if (changed)
expression =
new IR::P4ListExpression(expression->srcInfo, std::move(vec), elementType->getP4Type());
auto type = new IR::Type_P4List(expression->srcInfo, elementType);
setType(getOriginal(), type);
setType(expression, type);
if (constant) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::HeaderStackExpression *expression) {
if (done()) return expression;
bool constant = true;
auto stackType = getTypeType(expression->headerStackType);
if (auto st = stackType->to<IR::Type_Stack>()) {
auto elementType = st->elementType;
IR::Vector<IR::Expression> vec;
bool changed = false;
if (expression->size() != st->getSize()) {
typeError("%1%: number of initializers %2% has to match stack size %3%", expression,
expression->size(), st->getSize());
return expression;
}
for (auto c : expression->components) {
if (!isCompileTimeConstant(c)) constant = false;
auto type = getType(c);
if (type == nullptr) return expression;
auto tvs = unify(expression, elementType, type,
"Stack element type '%1%' does not match expected type '%2%'",
{type, elementType});
if (tvs == nullptr) return expression;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
auto converted = cts.convert(c, getChildContext());
vec.push_back(converted);
changed = true;
} else {
vec.push_back(c);
}
if (changed)
expression =
new IR::HeaderStackExpression(expression->srcInfo, std::move(vec), stackType);
}
} else {
typeError("%1%: header stack expression has an incorrect type `%2%`", expression,
stackType);
return expression;
}
setType(getOriginal(), stackType);
setType(expression, stackType);
if (constant) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::postorder(const IR::StructExpression *expression) {
if (done()) return expression;
bool constant = true;
IR::IndexedVector<IR::StructField> components;
for (auto c : expression->components) {
if (!isCompileTimeConstant(c->expression)) constant = false;
auto type = getType(c->expression);
if (type == nullptr) return expression;
components.push_back(new IR::StructField(c->name, type));
}
// This is the type inferred by looking at the fields.
const IR::Type *structType =
new IR::Type_UnknownStruct(expression->srcInfo, "unknown struct", std::move(components));
structType = canonicalize(structType);
const IR::Expression *result = expression;
if (expression->structType != nullptr) {
// We know the exact type of the initializer
auto desired = getTypeType(expression->structType);
if (desired == nullptr) return expression;
auto tvs = unify(expression, desired, structType,
"Initializer type '%1%' does not match expected type '%2%'",
{structType, desired});
if (tvs == nullptr) return expression;
if (!tvs->isIdentity()) {
ConstantTypeSubstitution cts(tvs, typeMap, this);
result = cts.convert(expression, getChildContext());
}
structType = desired;
}
setType(getOriginal(), structType);
setType(expression, structType);
if (constant) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return result;
}
const IR::Node *TypeInferenceBase::postorder(const IR::ArrayIndex *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
auto hst = ltype->to<IR::Type_Stack>();
int index = -1;
if (auto cst = expression->right->to<IR::Constant>()) {
if (hst && checkArrays && !cst->fitsInt()) {
typeError("Index too large: %1%", cst);
return expression;
}
index = cst->asInt();
if (hst && checkArrays && index < 0) {
typeError("%1%: Negative array index %2%", expression, cst);
return expression;
}
}
// if index is negative here it means it's not a constant
if ((index < 0) && !rtype->is<IR::Type_Bits>() && !rtype->is<IR::Type_SerEnum>() &&
!rtype->is<IR::Type_InfInt>()) {
typeError("Array index %1% must be an integer, but it has type %2%", expression->right,
rtype->toString());
return expression;
}
const IR::Type *type = nullptr;
if (hst) {
if (checkArrays && hst->sizeKnown()) {
int size = hst->getSize();
if (index >= 0 && index >= size) {
typeError("Array index %1% larger or equal to array size %2%", expression->right,
hst->size);
return expression;
}
}
type = hst->elementType;
} else if (auto tup = ltype->to<IR::Type_BaseList>()) {
if (index < 0) {
typeError("Tuple index %1% must be constant", expression->right);
return expression;
}
if (static_cast<size_t>(index) >= tup->getSize()) {
typeError("Tuple index %1% larger than tuple size %2%", expression->right,
tup->getSize());
return expression;
}
type = tup->components.at(index);
if (isCompileTimeConstant(expression->left)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
} else {
typeError("Indexing %1% applied to non-array and non-tuple type %2%", expression,
ltype->toString());
return expression;
}
if (isLeftValue(expression->left)) {
setLeftValue(expression);
setLeftValue(getOriginal<IR::Expression>());
}
setType(getOriginal(), type);
setType(expression, type);
return expression;
}
const IR::Node *TypeInferenceBase::binaryBool(const IR::Operation_Binary *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
if (!ltype->is<IR::Type_Boolean>() || !rtype->is<IR::Type_Boolean>()) {
typeError("%1%: not defined on %2% and %3%", expression, ltype->toString(),
rtype->toString());
return expression;
}
setType(getOriginal(), IR::Type_Boolean::get());
setType(expression, IR::Type_Boolean::get());
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::binaryArith(const IR::Operation_Binary *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
if (expression->is<IR::Add>() && ltype->is<IR::Type_String>() && rtype->is<IR::Type_String>()) {
typeError(
"%1%: cannot be applied to expression '%2%' with type '%3%', did you mean to use '++'?",
expression->getStringOp(), expression->right, rtype->toString());
return expression;
}
bool castLeft = false;
bool castRight = false;
if (auto se = ltype->to<IR::Type_SerEnum>()) {
ltype = getTypeType(se->type);
castLeft = true;
}
if (auto se = rtype->to<IR::Type_SerEnum>()) {
rtype = getTypeType(se->type);
castRight = true;
}
BUG_CHECK(ltype && rtype, "Invalid Type_SerEnum/getTypeType");
const IR::Type_Bits *bl = ltype->to<IR::Type_Bits>();
const IR::Type_Bits *br = rtype->to<IR::Type_Bits>();
if (bl == nullptr && !ltype->is<IR::Type_InfInt>()) {
typeError("%1%: cannot be applied to expression '%2%' with type '%3%'",
expression->getStringOp(), expression->left, ltype->toString());
return expression;
} else if (br == nullptr && !rtype->is<IR::Type_InfInt>()) {
typeError("%1%: cannot be applied to expression '%2%' with type '%3%'",
expression->getStringOp(), expression->right, rtype->toString());
return expression;
} else if (ltype->is<IR::Type_InfInt>() && rtype->is<IR::Type_InfInt>()) {
auto t = IR::Type_InfInt::get();
auto result = constantFold(expression);
setType(getOriginal(), t);
setCompileTimeConstant(result);
setCompileTimeConstant(getOriginal<IR::Expression>());
return result;
}
const IR::Type *resultType = ltype;
if (bl != nullptr && br != nullptr) {
if (bl->size != br->size) {
typeError("%1%: Cannot operate on values with different widths %2% and %3%", expression,
bl->size, br->size);
return expression;
}
if (bl->isSigned != br->isSigned) {
typeError("%1%: Cannot operate on values with different signs", expression);
return expression;
}
}
if ((bl == nullptr && br != nullptr) || castLeft) {
// must insert cast on the left
auto leftResultType = br;
if (castLeft && !br) leftResultType = bl;
auto e = expression->clone();
e->left = new IR::Cast(e->left->srcInfo, leftResultType, e->left);
setType(e->left, leftResultType);
if (isCompileTimeConstant(expression->left)) {
e->left = constantFold(e->left);
setCompileTimeConstant(e->left);
}
expression = e;
resultType = leftResultType;
}
if ((bl != nullptr && br == nullptr) || castRight) {
auto e = expression->clone();
auto rightResultType = bl;
if (castRight && !bl) rightResultType = br;
e->right = new IR::Cast(e->right->srcInfo, rightResultType, e->right);
setType(e->right, rightResultType);
if (isCompileTimeConstant(expression->right)) {
e->right = constantFold(e->right);
setCompileTimeConstant(e->right);
}
expression = e;
resultType = rightResultType;
}
setType(getOriginal(), resultType);
setType(expression, resultType);
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return expression;
}
const IR::Node *TypeInferenceBase::unsBinaryArith(const IR::Operation_Binary *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
if (auto se = ltype->to<IR::Type_SerEnum>()) ltype = getTypeType(se->type);
if (auto se = rtype->to<IR::Type_SerEnum>()) rtype = getTypeType(se->type);
const IR::Type_Bits *bl = ltype->to<IR::Type_Bits>();
if (bl != nullptr && bl->isSigned) {
typeError("%1%: Cannot operate on signed values", expression);
return expression;
}
const IR::Type_Bits *br = rtype->to<IR::Type_Bits>();
if (br != nullptr && br->isSigned) {
typeError("%1%: Cannot operate on signed values", expression);
return expression;
}
auto cleft = expression->left->to<IR::Constant>();
if (cleft != nullptr) {
if (cleft->value < 0) {
typeError("%1%: not defined on negative numbers", expression);
return expression;
}
}
auto cright = expression->right->to<IR::Constant>();
if (cright != nullptr) {
if (cright->value < 0) {
typeError("%1%: not defined on negative numbers", expression);
return expression;
}
}
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
}
return binaryArith(expression);
}
const IR::Node *TypeInferenceBase::shift(const IR::Operation_Binary *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
if (auto se = ltype->to<IR::Type_SerEnum>()) ltype = getTypeType(se->type);
if (ltype == nullptr) {
// getTypeType should have already taken care of the error message
return expression;
}
// FIXME: #5100, strip the new-type introduced by `type` keyword for now. According to the spec,
// such code should be rejected, but before #5099 it was accepted, so keep it accepted for now,
// until we discuss what is the right approach here.
if (const auto *rt = rtype->to<IR::Type_Newtype>()) {
rtype = getTypeType(rt->type);
}
auto lt = ltype->to<IR::Type_Bits>();
if (auto cst = expression->right->to<IR::Constant>()) {
if (!cst->fitsInt()) {
typeError("Shift amount too large: %1%", cst);
return expression;
}
int shift = cst->asInt();
if (shift < 0) {
typeError("%1%: Negative shift amount %2%", expression, cst);
return expression;
}
if (lt != nullptr && shift >= lt->size)
warn(ErrorType::WARN_OVERFLOW, "%1%: shifting value with %2% bits by %3%", expression,
lt->size, shift);
// If the amount is signed but positive, make it unsigned
if (auto bt = rtype->to<IR::Type_Bits>()) {
if (bt->isSigned) {
rtype = IR::Type_Bits::get(rtype->srcInfo, bt->width_bits(), false);
auto amt = new IR::Constant(cst->srcInfo, rtype, cst->value, cst->base);
if (expression->is<IR::Shl>()) {
expression = new IR::Shl(expression->srcInfo, expression->left, amt);
} else {
expression = new IR::Shr(expression->srcInfo, expression->left, amt);
}
setCompileTimeConstant(expression->right);
setType(expression->right, rtype);
}
}
}
if (const auto *rbits = rtype->to<IR::Type_Bits>(); rbits && rbits->isSigned) {
typeError("%1%: Shift amount must be an unsigned number", expression->right);
return expression;
} else if (!rbits && !rtype->is<IR::Type_InfInt>()) {
typeError(
"%1%: The right operand of shifts must be either an expression of type bit<S> or int, "
"but is %2%",
expression->right, rtype);
}
if (!lt && !ltype->is<IR::Type_InfInt>()) {
typeError("%1% left operand of shift must be a numeric type, not %2%", expression,
ltype->toString());
return expression;
}
if (ltype->is<IR::Type_InfInt>() && !rtype->is<IR::Type_InfInt>() &&
!isCompileTimeConstant(expression->right)) {
typeError(
"%1%: shift result type is arbitrary-precision int, but right operand is not constant; "
"width of left operand of shift needs to be specified or both operands need to be "
"constant",
expression);
return expression;
}
setType(expression, ltype);
setType(getOriginal(), ltype);
if (isCompileTimeConstant(expression->left) && isCompileTimeConstant(expression->right)) {
auto result = constantFold(expression);
setCompileTimeConstant(result);
setCompileTimeConstant(getOriginal<IR::Expression>());
return result;
}
return expression;
}
// Handle .. and &&&
const IR::Node *TypeInferenceBase::typeSet(const IR::Operation_Binary *expression) {
if (done()) return expression;
auto ltype = getType(expression->left);
auto rtype = getType(expression->right);
if (ltype == nullptr || rtype == nullptr) return expression;
auto leftType = ltype; // save original type
if (auto se = ltype->to<IR::Type_SerEnum>()) ltype = getTypeType(se->type);
if (auto se = rtype->to<IR::Type_SerEnum>()) rtype = getTypeType(se->type);