/
Type.cpp
1159 lines (985 loc) · 43.9 KB
/
Type.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
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file Type.cpp
*
* Implements a collection of type analyses operating on AST constructs.
*
***********************************************************************/
#include "ast/analysis/Type.h"
#include "AggregateOp.h"
#include "ConstraintSystem.h"
#include "FunctorOps.h"
#include "Global.h"
#include "ast/Aggregator.h"
#include "ast/Argument.h"
#include "ast/Atom.h"
#include "ast/Attribute.h"
#include "ast/BinaryConstraint.h"
#include "ast/BranchInit.h"
#include "ast/Clause.h"
#include "ast/Constant.h"
#include "ast/Counter.h"
#include "ast/Functor.h"
#include "ast/IntrinsicFunctor.h"
#include "ast/Negation.h"
#include "ast/Node.h"
#include "ast/NumericConstant.h"
#include "ast/Program.h"
#include "ast/QualifiedName.h"
#include "ast/RecordInit.h"
#include "ast/Relation.h"
#include "ast/StringConstant.h"
#include "ast/TranslationUnit.h"
#include "ast/TypeCast.h"
#include "ast/UnnamedVariable.h"
#include "ast/UserDefinedFunctor.h"
#include "ast/Variable.h"
#include "ast/analysis/Constraint.h"
#include "ast/analysis/Functor.h"
#include "ast/analysis/SumTypeBranches.h"
#include "ast/analysis/TypeEnvironment.h"
#include "ast/analysis/TypeSystem.h"
#include "ast/utility/NodeMapper.h"
#include "ast/utility/Utils.h"
#include "souffle/TypeAttribute.h"
#include "souffle/utility/ContainerUtil.h"
#include "souffle/utility/FunctionalUtil.h"
#include "souffle/utility/MiscUtil.h"
#include "souffle/utility/StringUtil.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
namespace souffle::ast::analysis {
namespace {
// -----------------------------------------------------------------------------
// Type Deduction Lattice
// -----------------------------------------------------------------------------
/**
* An implementation of a meet operation between sets of types computing
* the set of pair-wise greatest common subtypes.
*/
struct sub_type {
bool operator()(TypeSet& a, const TypeSet& b) const {
// compute result set
TypeSet greatestCommonSubtypes = getGreatestCommonSubtypes(a, b);
// check whether a should change
if (greatestCommonSubtypes == a) {
return false;
}
// update a
a = greatestCommonSubtypes;
return true;
}
};
/**
* A factory for computing sets of types covering all potential types.
*/
struct all_type_factory {
TypeSet operator()() const {
return TypeSet(true);
}
};
/**
* The type lattice forming the property space for the Type analysis. The
* value set is given by sets of types and the meet operator is based on the
* pair-wise computation of greatest common subtypes. Correspondingly, the
* bottom element has to be the set of all types.
*/
struct type_lattice : public property_space<TypeSet, sub_type, all_type_factory> {};
/** The definition of the type of variable to be utilized in the type analysis */
using TypeVar = ConstraintAnalysisVar<type_lattice>;
/** The definition of the type of constraint to be utilized in the type analysis */
using TypeConstraint = std::shared_ptr<Constraint<TypeVar>>;
/**
* A constraint factory ensuring that all the types associated to the variable
* a are subtypes of the variable b.
*/
TypeConstraint isSubtypeOf(const TypeVar& a, const TypeVar& b) {
return sub(a, b, "<:");
}
/**
* A constraint factory ensuring that all the types associated to the variable
* a are subtypes of type b.
*/
TypeConstraint isSubtypeOf(const TypeVar& variable, const Type& type) {
struct C : public Constraint<TypeVar> {
TypeVar variable;
const Type& type;
C(TypeVar variable, const Type& type) : variable(std::move(variable)), type(type) {}
bool update(Assignment<TypeVar>& assignments) const override {
TypeSet& assignment = assignments[variable];
if (assignment.isAll()) {
assignment = TypeSet(type);
return true;
}
TypeSet newAssignment;
for (const Type& t : assignment) {
newAssignment.insert(getGreatestCommonSubtypes(t, type));
}
// check whether there was a change
if (assignment == newAssignment) {
return false;
}
assignment = newAssignment;
return true;
}
void print(std::ostream& out) const override {
out << variable << " <: " << type.getName();
}
};
return std::make_shared<C>(variable, type);
}
/**
* A constraint factory ensuring that all the types associated to the variable
* are subtypes of some type in the provided set (values)
*
* Values can't be all.
*/
TypeConstraint hasSuperTypeInSet(const TypeVar& var, TypeSet values) {
struct C : public Constraint<TypeVar> {
TypeVar var;
TypeSet values;
C(TypeVar var, TypeSet values) : var(std::move(var)), values(std::move(values)) {}
bool update(Assignment<TypeVar>& assigment) const override {
// get current value of variable a
TypeSet& assigments = assigment[var];
// remove all types that are not sub-types of b
if (assigments.isAll()) {
assigments = values;
return true;
}
TypeSet newAssigments;
for (const Type& type : assigments) {
bool existsSuperTypeInValues =
any_of(values, [&type](const Type& value) { return isSubtypeOf(type, value); });
if (existsSuperTypeInValues) {
newAssigments.insert(type);
}
}
// check whether there was a change
if (newAssigments == assigments) {
return false;
}
assigments = newAssigments;
return true;
}
void print(std::ostream& out) const override {
out << "∃ t ∈ " << values << ": " << var << " <: t";
}
};
return std::make_shared<C>(var, values);
}
const Type& getBaseType(const Type* type) {
while (auto subset = dynamic_cast<const SubsetType*>(type)) {
type = &subset->getBaseType();
};
assert((isA<ConstantType>(type) || isA<RecordType>(type)) &&
"Root must be a constant type or a record type");
return *type;
}
/**
* Ensure that types of left and right have the same base types.
*/
TypeConstraint subtypesOfTheSameBaseType(const TypeVar& left, const TypeVar& right) {
struct C : public Constraint<TypeVar> {
TypeVar left;
TypeVar right;
C(TypeVar left, TypeVar right) : left(std::move(left)), right(std::move(right)) {}
bool update(Assignment<TypeVar>& assigment) const override {
// get current value of variable a
TypeSet& assigmentsLeft = assigment[left];
TypeSet& assigmentsRight = assigment[right];
// Base types common to left and right variables.
TypeSet baseTypes;
// Base types present in left/right variable.
TypeSet baseTypesLeft;
TypeSet baseTypesRight;
// Iterate over possible types extracting base types.
// Left
if (!assigmentsLeft.isAll()) {
for (const Type& type : assigmentsLeft) {
if (isA<SubsetType>(type) || isA<ConstantType>(type)) {
baseTypesLeft.insert(getBaseType(&type));
}
}
}
// Right
if (!assigmentsRight.isAll()) {
for (const Type& type : assigmentsRight) {
if (isA<SubsetType>(type) || isA<ConstantType>(type)) {
baseTypesRight.insert(getBaseType(&type));
}
}
}
TypeSet resultLeft;
TypeSet resultRight;
// Handle all
if (assigmentsLeft.isAll() && assigmentsRight.isAll()) {
return false;
}
// If left xor right is all, assign base types of the other side as possible values.
if (assigmentsLeft.isAll()) {
assigmentsLeft = baseTypesRight;
return true;
}
if (assigmentsRight.isAll()) {
assigmentsRight = baseTypesLeft;
return true;
}
baseTypes = TypeSet::intersection(baseTypesLeft, baseTypesRight);
// Allow types if they are subtypes of any of the common base types.
for (const Type& type : assigmentsLeft) {
bool isSubtypeOfCommonBaseType = any_of(baseTypes.begin(), baseTypes.end(),
[&type](const Type& baseType) { return isSubtypeOf(type, baseType); });
if (isSubtypeOfCommonBaseType) {
resultLeft.insert(type);
}
}
for (const Type& type : assigmentsRight) {
bool isSubtypeOfCommonBaseType = any_of(baseTypes.begin(), baseTypes.end(),
[&type](const Type& baseType) { return isSubtypeOf(type, baseType); });
if (isSubtypeOfCommonBaseType) {
resultRight.insert(type);
}
}
// check whether there was a change
if (resultLeft == assigmentsLeft && resultRight == assigmentsRight) {
return false;
}
assigmentsLeft = resultLeft;
assigmentsRight = resultRight;
return true;
}
//
void print(std::ostream& out) const override {
out << "∃ t : (" << left << " <: t)"
<< " ∧ "
<< "(" << right << " <: t)"
<< " where t is a base type";
}
};
return std::make_shared<C>(left, right);
}
/**
* Given a set of overloads, wait the list of candidates to reduce to one and then apply its constraints.
* NOTE: `subtypeResult` implies that `func <: overload-return-type`, rather than
* `func = overload-return-type`. This is required for old type semantics.
* See #1296 and tests/semantic/type_system4
*/
TypeConstraint satisfiesOverload(const TypeEnvironment& typeEnv, IntrinsicFunctors overloads, TypeVar result,
std::vector<TypeVar> args, bool subtypeResult) {
struct C : public Constraint<TypeVar> {
// Check if there already was a non-monotonic update
mutable bool nonMonotonicUpdate = false;
const TypeEnvironment& typeEnv;
mutable IntrinsicFunctors overloads;
TypeVar result;
std::vector<TypeVar> args;
bool subtypeResult;
C(const TypeEnvironment& typeEnv, IntrinsicFunctors overloads, TypeVar result,
std::vector<TypeVar> args, bool subtypeResult)
: typeEnv(typeEnv), overloads(std::move(overloads)), result(std::move(result)),
args(std::move(args)), subtypeResult(subtypeResult) {}
bool update(Assignment<TypeVar>& assigment) const override {
auto subtypesOf = [&](const TypeSet& src, TypeAttribute tyAttr) {
auto& ty = typeEnv.getConstantType(tyAttr);
return src.filter(TypeSet(true), [&](auto&& x) { return isSubtypeOf(x, ty); });
};
auto possible = [&](TypeAttribute ty, const TypeVar& var) {
auto& curr = assigment[var];
return curr.isAll() || any_of(curr, [&](auto&& t) { return getTypeAttribute(t) == ty; });
};
overloads = filterNot(std::move(overloads), [&](const IntrinsicFunctorInfo& x) -> bool {
if (!x.variadic && args.size() != x.params.size()) return true; // arity mismatch?
for (size_t i = 0; i < args.size(); ++i)
if (!possible(x.params[x.variadic ? 0 : i], args[i])) return true;
return !possible(x.result, result);
});
bool changed = false;
auto newResult = [&]() -> std::optional<TypeSet> {
if (0 == overloads.size()) return TypeSet();
if (1 < overloads.size()) return {};
auto& overload = overloads.front().get();
// `ord` is freakin' magical: it has the signature `a -> Int`.
// As a consequence, we might be given non-primitive arguments (i.e. types for which
// `TypeEnv::getConstantType` is undefined).
// Handle this by not imposing constraints on the arguments.
if (overload.op != FunctorOp::ORD) {
for (size_t i = 0; i < args.size(); ++i) {
auto argTy = overload.params[overload.variadic ? 0 : i];
auto& currArg = assigment[args[i]];
auto newArg = subtypesOf(currArg, argTy);
changed |= currArg != newArg;
// 2020-05-09: CI linter says to remove `std::move`, but clang-tidy-10 is happy.
currArg = std::move(newArg); // NOLINT
}
}
if (nonMonotonicUpdate || subtypeResult) {
return subtypesOf(assigment[result], overload.result);
} else {
nonMonotonicUpdate = true;
return TypeSet{typeEnv.getConstantType(overload.result)};
}
}();
if (newResult) {
auto& curr = assigment[result];
changed |= curr != *newResult;
// 2020-05-09: CI linter says to remove `std::move`, but clang-tidy-10 is happy.
curr = std::move(*newResult); // NOLINT
}
return changed;
}
void print(std::ostream& out) const override {
// TODO (darth_tytus): is this description correct?
out << "∃ t : " << result << " <: t where t is a base type";
}
};
return std::make_shared<C>(
typeEnv, std::move(overloads), std::move(result), std::move(args), subtypeResult);
}
/**
* Constraint on record type and its elements.
*/
TypeConstraint isSubtypeOfComponent(
const TypeVar& elementVariable, const TypeVar& recordVariable, size_t index) {
struct C : public Constraint<TypeVar> {
TypeVar elementVariable;
TypeVar recordVariable;
unsigned index;
C(TypeVar elementVariable, TypeVar recordVariable, int index)
: elementVariable(std::move(elementVariable)), recordVariable(std::move(recordVariable)),
index(index) {}
bool update(Assignment<TypeVar>& assignment) const override {
// get list of types for b
const TypeSet& recordTypes = assignment[recordVariable];
// if it is (not yet) constrainted => skip
if (recordTypes.isAll()) {
return false;
}
// compute new types for element and record
TypeSet newElementTypes;
TypeSet newRecordTypes;
for (const Type& type : recordTypes) {
// A type must be either a record type or a subset of a record type
if (!isOfKind(type, TypeAttribute::Record)) {
continue;
}
const auto& typeAsRecord = *as<RecordType>(type);
// Wrong size => skip.
if (typeAsRecord.getFields().size() <= index) {
continue;
}
// Valid type for record.
newRecordTypes.insert(type);
// and its corresponding field.
newElementTypes.insert(*typeAsRecord.getFields()[index]);
}
// combine with current types assigned to element
newElementTypes = getGreatestCommonSubtypes(assignment[elementVariable], newElementTypes);
// update values
bool changed = false;
if (newRecordTypes != recordTypes) {
assignment[recordVariable] = newRecordTypes;
changed = true;
}
if (assignment[elementVariable] != newElementTypes) {
assignment[elementVariable] = newElementTypes;
changed = true;
}
return changed;
}
void print(std::ostream& out) const override {
out << elementVariable << " <: " << recordVariable << "::" << index;
}
};
return std::make_shared<C>(elementVariable, recordVariable, index);
}
} // namespace
/* Return a new clause with type-annotated variables */
Own<Clause> createAnnotatedClause(
const Clause* clause, const std::map<const Argument*, TypeSet> argumentTypes) {
// Annotates each variable with its type based on a given type analysis result
struct TypeAnnotator : public NodeMapper {
const std::map<const Argument*, TypeSet>& types;
TypeAnnotator(const std::map<const Argument*, TypeSet>& types) : types(types) {}
Own<Node> operator()(Own<Node> node) const override {
if (auto* var = dynamic_cast<ast::Variable*>(node.get())) {
std::stringstream newVarName;
newVarName << var->getName() << "∈" << types.find(var)->second;
return mk<ast::Variable>(newVarName.str());
} else if (auto* var = dynamic_cast<UnnamedVariable*>(node.get())) {
std::stringstream newVarName;
newVarName << "_"
<< "∈" << types.find(var)->second;
return mk<ast::Variable>(newVarName.str());
}
node->apply(*this);
return node;
}
};
/* Note:
* Because the type of each argument is stored in the form [address -> type-set],
* the type-analysis result does not immediately apply to the clone due to differing
* addresses.
* Two ways around this:
* (1) Perform the type-analysis again for the cloned clause
* (2) Keep track of the addresses of equivalent arguments in the cloned clause
* Method (2) was chosen to avoid having to recompute the analysis each time.
*/
auto annotatedClause = souffle::clone(clause);
// Maps x -> y, where x is the address of an argument in the original clause, and y
// is the address of the equivalent argument in the clone.
std::map<const Argument*, const Argument*> memoryMap;
std::vector<const Argument*> originalAddresses;
visitDepthFirst(*clause, [&](const Argument& arg) { originalAddresses.push_back(&arg); });
std::vector<const Argument*> cloneAddresses;
visitDepthFirst(*annotatedClause, [&](const Argument& arg) { cloneAddresses.push_back(&arg); });
assert(cloneAddresses.size() == originalAddresses.size());
for (size_t i = 0; i < originalAddresses.size(); i++) {
memoryMap[originalAddresses[i]] = cloneAddresses[i];
}
// Map the types to the clause clone
std::map<const Argument*, TypeSet> cloneArgumentTypes;
for (auto& pair : argumentTypes) {
cloneArgumentTypes[memoryMap[pair.first]] = pair.second;
}
// Create the type-annotated clause
TypeAnnotator annotator(cloneArgumentTypes);
annotatedClause->apply(annotator);
return annotatedClause;
}
/**
* Constraint analysis framework for types.
*
* The analysis operates on the concept of sinks and sources.
* If the atom is negated or is a head then it's a sink,
* and we can only extract the kind constraint from it
* Otherwise it is a source, and the type of the element must
* be a subtype of source attribute.
*/
class TypeConstraintsAnalysis : public ConstraintAnalysis<TypeVar> {
public:
TypeConstraintsAnalysis(const TranslationUnit& tu) : tu(tu) {}
private:
const TranslationUnit& tu;
const TypeEnvironment& typeEnv = tu.getAnalysis<TypeEnvironmentAnalysis>()->getTypeEnvironment();
const Program& program = tu.getProgram();
const SumTypeBranchesAnalysis& sumTypesBranches = *tu.getAnalysis<SumTypeBranchesAnalysis>();
const TypeAnalysis& typeAnalysis = *tu.getAnalysis<TypeAnalysis>();
// Sinks = {head} ∪ {negated atoms}
std::set<const Atom*> sinks;
void collectConstraints(const Clause& clause) override {
sinks.insert(clause.getHead());
visitDepthFirstPreOrder(clause, *this);
}
void visitSink(const Atom& atom) {
iterateOverAtom(atom, [&](const Argument& argument, const Type& attributeType) {
if (isA<RecordType>(attributeType)) {
addConstraint(isSubtypeOf(getVar(argument), getBaseType(&attributeType)));
return;
}
for (auto& constantType : typeEnv.getConstantTypes()) {
if (isSubtypeOf(attributeType, constantType)) {
addConstraint(isSubtypeOf(getVar(argument), constantType));
}
}
});
}
void visitAtom(const Atom& atom) override {
if (contains(sinks, &atom)) {
visitSink(atom);
return;
}
iterateOverAtom(atom, [&](const Argument& argument, const Type& attributeType) {
addConstraint(isSubtypeOf(getVar(argument), attributeType));
});
}
void visitNegation(const Negation& cur) override {
sinks.insert(cur.getAtom());
}
void visitStringConstant(const StringConstant& cnst) override {
addConstraint(isSubtypeOf(getVar(cnst), typeEnv.getConstantType(TypeAttribute::Symbol)));
}
void visitNumericConstant(const NumericConstant& constant) override {
TypeSet possibleTypes;
// Check if the type is given.
if (constant.getFixedType().has_value()) {
switch (constant.getFixedType().value()) {
// Insert a type, but only after checking that parsing is possible.
case NumericConstant::Type::Int:
if (canBeParsedAsRamSigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Signed));
}
break;
case NumericConstant::Type::Uint:
if (canBeParsedAsRamUnsigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Unsigned));
}
break;
case NumericConstant::Type::Float:
if (canBeParsedAsRamFloat(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Float));
}
break;
}
} else if (contains(typeAnalysis.getNumericConstantTypes(), &constant)) {
switch (typeAnalysis.getNumericConstantTypes().at(&constant)) {
// Insert a type, but only after checking that parsing is possible.
case NumericConstant::Type::Int:
if (canBeParsedAsRamSigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Signed));
}
break;
case NumericConstant::Type::Uint:
if (canBeParsedAsRamUnsigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Unsigned));
}
break;
case NumericConstant::Type::Float:
if (canBeParsedAsRamFloat(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Float));
}
break;
}
} else {
// Else: all numeric types that can be parsed are valid.
if (canBeParsedAsRamSigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Signed));
}
if (canBeParsedAsRamUnsigned(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Unsigned));
}
if (canBeParsedAsRamFloat(constant.getConstant())) {
possibleTypes.insert(typeEnv.getConstantType(TypeAttribute::Float));
}
}
addConstraint(hasSuperTypeInSet(getVar(constant), possibleTypes));
}
void visitBinaryConstraint(const BinaryConstraint& rel) override {
auto lhs = getVar(rel.getLHS());
auto rhs = getVar(rel.getRHS());
addConstraint(isSubtypeOf(lhs, rhs));
addConstraint(isSubtypeOf(rhs, lhs));
}
void visitFunctor(const Functor& fun) override {
auto functorVar = getVar(fun);
auto intrFun = as<IntrinsicFunctor>(fun);
if (intrFun) {
auto argVars = map(intrFun->getArguments(), [&](auto&& x) { return getVar(x); });
// The type of the user-defined function might not be set at this stage.
// If so then add overloads as alternatives
if (!typeAnalysis.hasValidTypeInfo(intrFun))
addConstraint(satisfiesOverload(typeEnv, functorBuiltIn(intrFun->getBaseFunctionOp()),
functorVar, argVars, isInfixFunctorOp(intrFun->getBaseFunctionOp())));
// In polymorphic case
// We only require arguments to share a base type with a return type.
// (instead of, for example, requiring them to be of the same type)
// This approach is related to old type semantics
// See #1296 and tests/semantic/type_system4
if (isInfixFunctorOp(intrFun->getBaseFunctionOp())) {
for (auto&& var : argVars)
addConstraint(subtypesOfTheSameBaseType(var, functorVar));
return;
}
if (!typeAnalysis.hasValidTypeInfo(intrFun)) return;
}
// Skip constraint adding if type info is not available
if (!typeAnalysis.hasValidTypeInfo(&fun)) return;
// add a constraint for the return type of the functor
TypeAttribute returnType = typeAnalysis.getFunctorReturnType(&fun);
addConstraint(isSubtypeOf(functorVar, typeEnv.getConstantType(returnType)));
// Special case. Ord returns the ram representation of any object.
if (intrFun && typeAnalysis.getPolymorphicOperator(intrFun) == FunctorOp::ORD) return;
// Add constraints on arguments
auto arguments = fun.getArguments();
for (size_t i = 0; i < arguments.size(); ++i) {
TypeAttribute argType = typeAnalysis.getFunctorArgType(&fun, i);
addConstraint(isSubtypeOf(getVar(arguments[i]), typeEnv.getConstantType(argType)));
}
}
void visitCounter(const Counter& counter) override {
addConstraint(isSubtypeOf(getVar(counter), typeEnv.getConstantType(TypeAttribute::Signed)));
}
void visitTypeCast(const ast::TypeCast& typeCast) override {
auto& typeName = typeCast.getType();
if (!typeEnv.isType(typeName)) {
return;
}
addConstraint(isSubtypeOf(getVar(typeCast), typeEnv.getType(typeName)));
// If we are dealing with a constant then its type must be deduced from the cast
// Otherwise, expression like: to_string(as(2, float)) couldn't be typed.
auto* value = typeCast.getValue();
if (isA<Constant>(value)) {
addConstraint(isSubtypeOf(getVar(*value), typeEnv.getType(typeName)));
}
}
void visitRecordInit(const RecordInit& record) override {
auto arguments = record.getArguments();
for (size_t i = 0; i < arguments.size(); ++i) {
addConstraint(isSubtypeOfComponent(getVar(arguments[i]), getVar(record), i));
}
}
void visitBranchInit(const BranchInit& adt) override {
auto* correspondingType = sumTypesBranches.getType(adt.getConstructor());
if (correspondingType == nullptr) {
return; // malformed program.
}
// Sanity check
assert(isA<AlgebraicDataType>(correspondingType));
// Constraint on the whole branch. $Branch(...) <: ADTtype
addConstraint(isSubtypeOf(getVar(adt), *correspondingType));
// Constraints on arguments
try {
auto branchTypes = as<AlgebraicDataType>(correspondingType)->getBranchTypes(adt.getConstructor());
auto branchArgs = adt.getArguments();
if (branchTypes.size() != branchArgs.size()) {
// handled by semantic checker later.
throw std::invalid_argument("Invalid arity");
}
// Add constraints for each of the branch arguments.
for (size_t i = 0; i < branchArgs.size(); ++i) {
auto argVar = getVar(branchArgs[i]);
addConstraint(isSubtypeOf(argVar, *branchTypes[i]));
}
} catch (...) {
return; // Invalid program.
}
}
void visitAggregator(const Aggregator& agg) override {
if (agg.getBaseOperator() == AggregateOp::COUNT) {
addConstraint(isSubtypeOf(getVar(agg), typeEnv.getConstantType(TypeAttribute::Signed)));
} else if (agg.getBaseOperator() == AggregateOp::MEAN) {
addConstraint(isSubtypeOf(getVar(agg), typeEnv.getConstantType(TypeAttribute::Float)));
} else {
addConstraint(hasSuperTypeInSet(getVar(agg), typeEnv.getConstantNumericTypes()));
}
// If there is a target expression - it should be of the same type as the aggregator.
if (auto expr = agg.getTargetExpression()) {
addConstraint(isSubtypeOf(getVar(expr), getVar(agg)));
addConstraint(isSubtypeOf(getVar(agg), getVar(expr)));
}
}
/**
* Utility function.
* Iterate over atoms valid pairs of (argument, type-attribute) and apply procedure `map` for its
* side-effects.
*/
void iterateOverAtom(const Atom& atom, std::function<void(const Argument&, const Type&)> map) {
// get relation
auto rel = getAtomRelation(&atom, &program);
if (rel == nullptr) {
return; // error in input program
}
auto atts = rel->getAttributes();
auto args = atom.getArguments();
if (atts.size() != args.size()) {
return; // error in input program
}
for (size_t i = 0; i < atts.size(); i++) {
const auto& typeName = atts[i]->getTypeName();
if (typeEnv.isType(typeName)) {
map(*args[i], typeEnv.getType(typeName));
}
}
}
};
std::map<const Argument*, TypeSet> TypeAnalysis::analyseTypes(
const TranslationUnit& tu, const Clause& clause, std::ostream* logs) {
return TypeConstraintsAnalysis(tu).analyse(clause, logs);
}
void TypeAnalysis::print(std::ostream& os) const {
os << "-- Analysis logs --" << std::endl;
os << analysisLogs.str() << std::endl;
os << "-- Result --" << std::endl;
for (auto& cur : annotatedClauses) {
os << *cur << std::endl;
}
}
TypeAttribute TypeAnalysis::getFunctorReturnType(const Functor* functor) const {
assert(hasValidTypeInfo(functor) && "functor not yet processed");
if (auto* intrinsic = as<IntrinsicFunctor>(functor)) {
return functorInfo.at(intrinsic)->result;
} else if (const auto* udf = as<UserDefinedFunctor>(functor)) {
return udfDeclaration.at(udf->getName())->getReturnType();
}
fatal("Missing functor type.");
}
TypeAttribute TypeAnalysis::getFunctorArgType(const Functor* functor, const size_t idx) const {
assert(hasValidTypeInfo(functor) && "functor not yet processed");
if (auto* intrinsic = as<IntrinsicFunctor>(functor)) {
auto* info = functorInfo.at(intrinsic);
return info->params.at(info->variadic ? 0 : idx);
} else if (auto* udf = as<UserDefinedFunctor>(functor)) {
return udfDeclaration.at(udf->getName())->getArgsTypes().at(idx);
}
fatal("Missing functor type.");
}
const std::vector<TypeAttribute>& TypeAnalysis::getFunctorArgTypes(const UserDefinedFunctor& udf) const {
return udfDeclaration.at(udf.getName())->getArgsTypes();
}
bool TypeAnalysis::isStatefulFunctor(const UserDefinedFunctor* udf) const {
return udfDeclaration.at(udf->getName())->isStateful();
}
const std::map<const NumericConstant*, NumericConstant::Type>& TypeAnalysis::getNumericConstantTypes() const {
return numericConstantType;
}
bool TypeAnalysis::isMultiResultFunctor(const Functor& functor) {
if (isA<UserDefinedFunctor>(functor)) {
return false;
} else if (auto* intrinsic = as<IntrinsicFunctor>(functor)) {
auto candidates = functorBuiltIn(intrinsic->getBaseFunctionOp());
assert(!candidates.empty() && "at least one op should match");
return candidates[0].get().multipleResults;
}
fatal("Missing functor type.");
}
std::set<TypeAttribute> TypeAnalysis::getTypeAttributes(const Argument* arg) const {
std::set<TypeAttribute> typeAttributes;
if (const auto* inf = dynamic_cast<const IntrinsicFunctor*>(arg)) {
// intrinsic functor type is its return type if its set
if (hasValidTypeInfo(inf)) {
typeAttributes.insert(getFunctorReturnType(inf));
return typeAttributes;
}
}
const auto& types = getTypes(arg);
if (types.isAll()) {
return {TypeAttribute::Signed, TypeAttribute::Unsigned, TypeAttribute::Float, TypeAttribute::Symbol,
TypeAttribute::Record};
}
for (const auto& type : types) {
typeAttributes.insert(getTypeAttribute(type));
}
return typeAttributes;
}
IntrinsicFunctors TypeAnalysis::validOverloads(const IntrinsicFunctor& inf) const {
auto retTys = getTypeAttributes(&inf);
auto argTys = map(inf.getArguments(), [&](const Argument* arg) { return getTypeAttributes(arg); });
IntrinsicFunctors functorInfos = contains(functorInfo, &inf)
? functorBuiltIn(getPolymorphicOperator(&inf))
: functorBuiltIn(inf.getBaseFunctionOp());
auto candidates = filterNot(functorInfos, [&](const IntrinsicFunctorInfo& x) -> bool {
if (!x.variadic && argTys.size() != x.params.size()) return true; // arity mismatch?
for (size_t i = 0; i < argTys.size(); ++i)
if (!contains(argTys[i], x.params[x.variadic ? 0 : i])) return true;
return !contains(retTys, x.result);
});
std::sort(candidates.begin(), candidates.end(),
[&](const IntrinsicFunctorInfo& a, const IntrinsicFunctorInfo& b) {
if (a.result != b.result) return a.result < b.result;
if (a.variadic != b.variadic) return a.variadic < b.variadic;
return std::lexicographical_compare(
a.params.begin(), a.params.end(), b.params.begin(), b.params.end());
});
return candidates;
}
bool TypeAnalysis::hasValidTypeInfo(const Argument* argument) const {
if (auto* inf = as<IntrinsicFunctor>(argument)) {
return contains(functorInfo, inf);
} else if (auto* udf = as<UserDefinedFunctor>(argument)) {
return contains(udfDeclaration, udf->getName());
} else if (auto* nc = as<NumericConstant>(argument)) {
return contains(numericConstantType, nc);
} else if (auto* agg = as<Aggregator>(argument)) {
return contains(aggregatorType, agg);
}
return true;
}
NumericConstant::Type TypeAnalysis::getPolymorphicNumericConstantType(const NumericConstant* nc) const {
assert(hasValidTypeInfo(nc) && "numeric constant type not set");
return numericConstantType.at(nc);
}
BinaryConstraintOp TypeAnalysis::getPolymorphicOperator(const BinaryConstraint* bc) const {
assert(contains(constraintType, bc) && "binary constraint operator not set");
return constraintType.at(bc);
}
AggregateOp TypeAnalysis::getPolymorphicOperator(const Aggregator* agg) const {
assert(contains(aggregatorType, agg) && "aggregator operator not set");
return aggregatorType.at(agg);
}
FunctorOp TypeAnalysis::getPolymorphicOperator(const IntrinsicFunctor* inf) const {
assert(hasValidTypeInfo(inf) && "functor type not set");
return functorInfo.at(inf)->op;
}
bool TypeAnalysis::analyseIntrinsicFunctors(const TranslationUnit& translationUnit) {
bool changed = false;
const auto& program = translationUnit.getProgram();
visitDepthFirst(program, [&](const IntrinsicFunctor& functor) {
auto candidates = validOverloads(functor);
if (candidates.empty()) {
// No valid overloads - mark it as an invalid functor
if (contains(functorInfo, &functor)) {
functorInfo.erase(&functor);
changed = true;
}
return;
}
// Update to the canonic representation if different
const auto* curInfo = &candidates.front().get();
if (contains(functorInfo, &functor) && functorInfo.at(&functor) == curInfo) return;
functorInfo[&functor] = curInfo;
changed = true;
});
return changed;
}
bool TypeAnalysis::analyseNumericConstants(const TranslationUnit& translationUnit) {
bool changed = false;
const auto& program = translationUnit.getProgram();
auto setNumericConstantType = [&](const NumericConstant& nc, NumericConstant::Type ncType) {
if (contains(numericConstantType, &nc) && numericConstantType.at(&nc) == ncType) return;
changed = true;
numericConstantType[&nc] = ncType;
};
visitDepthFirst(program, [&](const NumericConstant& numericConstant) {
// Constant has a fixed type