forked from jckarter/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.cpp
2814 lines (2451 loc) · 85.1 KB
/
analyzer.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
#include "clay.hpp"
#include "evaluator.hpp"
#include "codegen.hpp"
#include "loader.hpp"
#include "operators.hpp"
#include "patterns.hpp"
#include "lambdas.hpp"
#include "analyzer.hpp"
#include "invoketables.hpp"
#include "matchinvoke.hpp"
#include "error.hpp"
#include "literals.hpp"
#include "desugar.hpp"
#include "constructors.hpp"
#include "env.hpp"
#include "clone.hpp"
#include "objects.hpp"
#include "analyzer_op.hpp"
#pragma clang diagnostic ignored "-Wcovered-switch-default"
namespace clay {
GVarInstance::GVarInstance(GlobalVariablePtr gvar,
llvm::ArrayRef<ObjectPtr> params)
: gvar(gvar), params(params),
llGlobal(NULL), debugInfo(NULL), analyzing(false) {}
GVarInstance::~GVarInstance() {}
Procedure::Procedure(Module *module, IdentifierPtr name, Visibility visibility, bool privateOverload)
: TopLevelItem(PROCEDURE, module, name, visibility),
privateOverload(privateOverload)
{}
Procedure::Procedure(Module *module, IdentifierPtr name, Visibility visibility, bool privateOverload, OverloadPtr interface)
: TopLevelItem(PROCEDURE, module, name, visibility),
interface(interface), privateOverload(privateOverload)
{}
Procedure::~Procedure() {}
GlobalVariable::GlobalVariable(Module *module, IdentifierPtr name,
Visibility visibility,
llvm::ArrayRef<PatternVar> patternVars,
ExprPtr predicate,
llvm::ArrayRef<IdentifierPtr> params,
IdentifierPtr varParam,
ExprPtr expr)
: TopLevelItem(GLOBAL_VARIABLE, module, name, visibility),
patternVars(patternVars), predicate(predicate),
params(params), varParam(varParam), expr(expr) {}
GlobalVariable::~GlobalVariable() {}
static StatementAnalysis analyzeStatement(StatementPtr stmt, EnvPtr env, AnalysisContext *ctx);
static EnvPtr analyzeBinding(BindingPtr x, EnvPtr env);
static int analysisCachingDisabled = 0;
void disableAnalysisCaching() { analysisCachingDisabled += 1; }
void enableAnalysisCaching() { analysisCachingDisabled -= 1; }
static TypePtr objectType(ObjectPtr x);
static bool staticToTypeTuple(ObjectPtr x, vector<TypePtr> &out);
static void staticToTypeTuple(MultiStaticPtr x, unsigned index,
vector<TypePtr> &out);
static int staticToInt(MultiStaticPtr x, unsigned index);
//
// utility procs
//
static TypePtr objectType(ObjectPtr x)
{
switch (x->objKind) {
case VALUE_HOLDER : {
ValueHolder *y = (ValueHolder *)x.ptr();
return y->type;
}
case TYPE :
case PRIM_OP :
case PROCEDURE :
case INTRINSIC :
case GLOBAL_ALIAS :
case RECORD_DECL :
case VARIANT_DECL :
case MODULE :
case IDENTIFIER :
return staticType(x);
default :
error("untypeable object");
return NULL;
}
}
ObjectPtr unwrapStaticType(TypePtr t) {
if (t->typeKind != STATIC_TYPE)
return NULL;
StaticType *st = (StaticType *)t.ptr();
return st->obj;
}
static bool staticToTypeTuple(ObjectPtr x, vector<TypePtr> &out)
{
TypePtr t;
if (staticToType(x, t)) {
out.push_back(t);
return true;
}
if (x->objKind != VALUE_HOLDER)
return false;
ValueHolderPtr y = (ValueHolder *)x.ptr();
assert(y->type->typeKind != STATIC_TYPE);
if (y->type->typeKind != TUPLE_TYPE)
return false;
TupleTypePtr z = (TupleType *)y->type.ptr();
for (size_t i = 0; i < z->elementTypes.size(); ++i) {
ObjectPtr obj = unwrapStaticType(z->elementTypes[i]);
if ((!obj) || (obj->objKind != TYPE))
return false;
out.push_back((Type *)obj.ptr());
}
return true;
}
static void staticToTypeTuple(MultiStaticPtr x, unsigned index,
vector<TypePtr> &out)
{
if (!staticToTypeTuple(x->values[index], out))
argumentError(index, "expecting zero-or-more types");
}
static bool staticToInt(ObjectPtr x, int &out, TypePtr &type)
{
if (x->objKind != VALUE_HOLDER)
return false;
ValueHolderPtr vh = (ValueHolder *)x.ptr();
if (vh->type != cIntType) {
type = vh->type;
return false;
}
out = vh->as<int>();
return true;
}
static int staticToInt(MultiStaticPtr x, unsigned index)
{
int out = -1;
TypePtr type;
if (!staticToInt(x->values[index], out, type)){
argumentTypeError(index, "Int", type);
}
return out;
}
bool staticToBool(ObjectPtr x, bool &out, TypePtr &type)
{
if (x->objKind != VALUE_HOLDER)
return false;
ValueHolderPtr vh = (ValueHolder *)x.ptr();
if (vh->type != boolType) {
type = vh->type;
return false;
}
out = vh->as<bool>();
return true;
}
bool staticToBool(MultiStaticPtr x, unsigned index)
{
bool out = false;
TypePtr type;
if (!staticToBool(x->values[index], out, type))
argumentTypeError(index, "Bool", type);
return out;
}
bool staticToCallingConv(ObjectPtr x, CallingConv &out)
{
if (x->objKind != PRIM_OP)
return false;
PrimOp *ccPrim = (PrimOp*)x.ptr();
switch (ccPrim->primOpCode) {
case PRIM_AttributeCCall:
out = CC_DEFAULT;
return true;
case PRIM_AttributeStdCall:
out = CC_STDCALL;
return true;
case PRIM_AttributeFastCall:
out = CC_FASTCALL;
return true;
case PRIM_AttributeThisCall:
out = CC_THISCALL;
return true;
case PRIM_AttributeLLVMCall:
out = CC_LLVM;
return true;
default:
return false;
}
}
CallingConv staticToCallingConv(MultiStaticPtr x, unsigned index)
{
CallingConv out;
if (!staticToCallingConv(x->values[index], out))
argumentError(index, "expecting calling convention attribute");
return out;
}
//
// safe analysis
//
static Location analysisErrorLocation;
static vector<CompileContextEntry> analysisErrorCompileContext;
struct ClearAnalysisError {
ClearAnalysisError() {}
~ClearAnalysisError() {
analysisErrorLocation = Location();
analysisErrorCompileContext.clear();
}
};
static void updateAnalysisErrorLocation()
{
analysisErrorLocation = topLocation();
analysisErrorCompileContext = getCompileContext();
}
static void analysisError()
{
setCompileContext(analysisErrorCompileContext);
LocationContext loc(analysisErrorLocation);
error("type propagation failed due to recursion without base case");
}
PVData safeAnalyzeOne(ExprPtr expr, EnvPtr env)
{
ClearAnalysisError clear;
PVData result = analyzeOne(expr, env);
if (!result.ok())
analysisError();
return result;
}
MultiPValuePtr safeAnalyzeMulti(ExprListPtr exprs, EnvPtr env, size_t wantCount)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeMulti(exprs, env, wantCount);
if (!result)
analysisError();
return result;
}
MultiPValuePtr safeAnalyzeExpr(ExprPtr expr, EnvPtr env)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeExpr(expr, env);
if (!result)
analysisError();
return result;
}
MultiPValuePtr safeAnalyzeIndexingExpr(ExprPtr indexable,
ExprListPtr args,
EnvPtr env)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeIndexingExpr(indexable, args, env);
if (!result)
analysisError();
return result;
}
MultiPValuePtr safeAnalyzeMultiArgs(ExprListPtr exprs,
EnvPtr env,
vector<unsigned> &dispatchIndices)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeMultiArgs(exprs, env, dispatchIndices);
if (!result)
analysisError();
return result;
}
InvokeEntry* safeAnalyzeCallable(ObjectPtr x,
llvm::ArrayRef<TypePtr> argsKey,
llvm::ArrayRef<ValueTempness> argsTempness)
{
ClearAnalysisError clear;
InvokeEntry* entry = analyzeCallable(x, argsKey, argsTempness);
if (!entry->callByName && !entry->analyzed)
analysisError();
return entry;
}
MultiPValuePtr safeAnalyzeCallByName(InvokeEntry* entry,
ExprPtr callable,
ExprListPtr args,
EnvPtr env)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeCallByName(entry, callable, args, env);
if (!entry)
analysisError();
return result;
}
MultiPValuePtr safeAnalyzeGVarInstance(GVarInstancePtr x)
{
ClearAnalysisError clear;
MultiPValuePtr result = analyzeGVarInstance(x);
if (!result)
analysisError();
return result;
}
//
// analyzeMulti
//
static MultiPValuePtr analyzeMulti2(ExprListPtr exprs, EnvPtr env, size_t wantCount);
MultiPValuePtr analyzeMulti(ExprListPtr exprs, EnvPtr env, size_t wantCount)
{
if (analysisCachingDisabled > 0)
return analyzeMulti2(exprs, env, wantCount);
if (!exprs->cachedAnalysis)
exprs->cachedAnalysis = analyzeMulti2(exprs, env, wantCount);
return exprs->cachedAnalysis;
}
static MultiPValuePtr analyzeMulti2(ExprListPtr exprs, EnvPtr env, size_t wantCount)
{
MultiPValuePtr out = new MultiPValue();
ExprPtr unpackExpr = implicitUnpackExpr(wantCount, exprs);
if (unpackExpr != NULL) {
MultiPValuePtr z = analyzeExpr(unpackExpr, env);
if (!z)
return NULL;
out->add(z);
} else for (unsigned i = 0; i < exprs->size(); ++i) {
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiPValuePtr z = analyzeExpr(y->expr, env);
if (!z)
return NULL;
out->add(z);
}
else if (x->exprKind == PAREN) {
MultiPValuePtr y = analyzeExpr(x, env);
if (!y)
return NULL;
out->add(y);
} else {
PVData y = analyzeOne(x, env);
if (!y.ok())
return NULL;
out->add(y);
}
}
return out;
}
//
// analyzeOne
//
PVData analyzeOne(ExprPtr expr, EnvPtr env)
{
MultiPValuePtr x = analyzeExpr(expr, env);
if (!x)
return PVData();
LocationContext loc(expr->location);
ensureArity(x, 1);
return x->values[0];
}
//
// analyzeMultiArgs, analyzeOneArg, analyzeArgExpr
//
static MultiPValuePtr analyzeMultiArgs2(ExprListPtr exprs,
EnvPtr env,
unsigned startIndex,
vector<unsigned> &dispatchIndices);
MultiPValuePtr analyzeMultiArgs(ExprListPtr exprs,
EnvPtr env,
vector<unsigned> &dispatchIndices)
{
if (analysisCachingDisabled > 0)
return analyzeMultiArgs2(exprs, env, 0, dispatchIndices);
if (!exprs->cachedAnalysis) {
MultiPValuePtr mpv = analyzeMultiArgs2(exprs, env, 0, dispatchIndices);
if (mpv.ptr() && dispatchIndices.empty())
exprs->cachedAnalysis = mpv;
return mpv;
}
return exprs->cachedAnalysis;
}
static MultiPValuePtr analyzeMultiArgs2(ExprListPtr exprs,
EnvPtr env,
unsigned startIndex,
vector<unsigned> &dispatchIndices)
{
MultiPValuePtr out = new MultiPValue();
unsigned index = startIndex;
for (unsigned i = 0; i < exprs->size(); ++i) {
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiPValuePtr z = analyzeArgExpr(y->expr, env, index, dispatchIndices);
if (!z)
return NULL;
index += unsigned(z->size());
out->add(z);
}
else if (x->exprKind == PAREN) {
MultiPValuePtr z = analyzeArgExpr(x, env, index, dispatchIndices);
if (!z)
return NULL;
index += unsigned(z->size());
out->add(z);
} else {
PVData y = analyzeOneArg(x, env, index, dispatchIndices);
if (!y.ok())
return NULL;
out->add(y);
index += 1;
}
}
return out;
}
PVData analyzeOneArg(ExprPtr x,
EnvPtr env,
unsigned startIndex,
vector<unsigned> &dispatchIndices)
{
MultiPValuePtr mpv = analyzeArgExpr(x, env, startIndex, dispatchIndices);
if (!mpv)
return PVData();
LocationContext loc(x->location);
ensureArity(mpv, 1);
return mpv->values[0];
}
MultiPValuePtr analyzeArgExpr(ExprPtr x,
EnvPtr env,
unsigned startIndex,
vector<unsigned> &dispatchIndices)
{
if (x->exprKind == DISPATCH_EXPR) {
DispatchExpr *y = (DispatchExpr *)x.ptr();
MultiPValuePtr mpv = analyzeExpr(y->expr, env);
if (!mpv)
return NULL;
for (unsigned i = 0; i < mpv->size(); ++i)
dispatchIndices.push_back(i + startIndex);
return mpv;
}
return analyzeExpr(x, env);
}
//
// analyzeExpr
//
static MultiPValuePtr analyzeExpr2(ExprPtr expr, EnvPtr env);
void appendArgString(Expr *expr, string *outString)
{
ForeignExpr *fexpr;
if (expr->exprKind == FOREIGN_EXPR)
fexpr = (ForeignExpr*)expr;
else if (expr->exprKind == UNPACK) {
Unpack *uexpr = (Unpack*)expr;
if (uexpr->expr->exprKind != FOREIGN_EXPR)
goto notAlias;
fexpr = (ForeignExpr *)uexpr->expr.ptr();
*outString += "..";
} else
goto notAlias;
*outString += fexpr->expr->asString();
return;
notAlias:
error("__ARG__ may only be applied to an alias value or alias function argument");
}
MultiPValuePtr analyzeExpr(ExprPtr expr, EnvPtr env)
{
if (analysisCachingDisabled > 0)
return analyzeExpr2(expr, env);
if (!expr->cachedAnalysis)
expr->cachedAnalysis = analyzeExpr2(expr, env);
return expr->cachedAnalysis;
}
static MultiPValuePtr analyzeExpr2(ExprPtr expr, EnvPtr env)
{
LocationContext loc(expr->location);
switch (expr->exprKind) {
case BOOL_LITERAL : {
return new MultiPValue(PVData(boolType, true));
}
case INT_LITERAL : {
IntLiteral *x = (IntLiteral *)expr.ptr();
ValueHolderPtr v = parseIntLiteral(safeLookupModule(env), x);
return new MultiPValue(PVData(v->type, true));
}
case FLOAT_LITERAL : {
FloatLiteral *x = (FloatLiteral *)expr.ptr();
ValueHolderPtr v = parseFloatLiteral(safeLookupModule(env), x);
return new MultiPValue(PVData(v->type, true));
}
case CHAR_LITERAL : {
CharLiteral *x = (CharLiteral *)expr.ptr();
if (!x->desugared)
x->desugared = desugarCharLiteral(x->value);
return analyzeExpr(x->desugared, env);
}
case STRING_LITERAL : {
StringLiteral *x = (StringLiteral *)expr.ptr();
return new MultiPValue(staticPValue(x->value.ptr()));
}
case NAME_REF : {
NameRef *x = (NameRef *)expr.ptr();
ObjectPtr y = safeLookupEnv(env, x->name);
if (y->objKind == EXPRESSION) {
ExprPtr z = (Expr *)y.ptr();
return analyzeExpr(z, env);
}
else if (y->objKind == EXPR_LIST) {
ExprListPtr z = (ExprList *)y.ptr();
return analyzeMulti(z, env, 0);
}
return analyzeStaticObject(y);
}
case FILE_EXPR : {
Location location = safeLookupCallByNameLocation(env);
string filename = location.source->fileName;
return analyzeStaticObject(Identifier::get(filename));
}
case LINE_EXPR : {
return new MultiPValue(PVData(cSizeTType, true));
}
case COLUMN_EXPR : {
return new MultiPValue(PVData(cSizeTType, true));
}
case ARG_EXPR : {
ARGExpr *arg = (ARGExpr *)expr.ptr();
ObjectPtr obj = safeLookupEnv(env, arg->name);
string argString;
if (obj->objKind == EXPRESSION) {
Expr *expr = (Expr*)obj.ptr();
appendArgString(expr, &argString);
} else if (obj->objKind == EXPR_LIST) {
ExprList *elist = (ExprList*)obj.ptr();
for (vector<ExprPtr>::const_iterator i = elist->exprs.begin(),
end = elist->exprs.end();
i != end;
++i)
{
if (!argString.empty())
argString += ", ";
Expr *expr = i->ptr();
appendArgString(expr, &argString);
}
}
return analyzeStaticObject(Identifier::get(argString));
}
case TUPLE : {
Tuple *x = (Tuple *)expr.ptr();
return analyzeCallExpr(operator_expr_tupleLiteral(),
x->args,
env);
}
case PAREN : {
Paren *x = (Paren *)expr.ptr();
return analyzeMulti(x->args, env, 0);
}
case INDEXING : {
Indexing *x = (Indexing *)expr.ptr();
return analyzeIndexingExpr(x->expr, x->args, env);
}
case CALL : {
Call *x = (Call *)expr.ptr();
return analyzeCallExpr(x->expr, x->allArgs(), env);
}
case FIELD_REF : {
FieldRef *x = (FieldRef *)expr.ptr();
if (!x->desugared)
desugarFieldRef(x, safeLookupModule(env));
if (x->isDottedModuleName)
return analyzeExpr(x->desugared, env);
PVData pv = analyzeOne(x->expr, env);
if (!pv.ok())
return NULL;
if (pv.type->typeKind == STATIC_TYPE) {
StaticType *st = (StaticType *)pv.type.ptr();
if (st->obj->objKind == MODULE) {
Module *m = (Module *)st->obj.ptr();
ObjectPtr obj = safeLookupPublic(m, x->name);
return analyzeStaticObject(obj);
}
}
return analyzeExpr(x->desugared, env);
}
case STATIC_INDEXING : {
StaticIndexing *x = (StaticIndexing *)expr.ptr();
if (!x->desugared)
x->desugared = desugarStaticIndexing(x);
return analyzeExpr(x->desugared, env);
}
case VARIADIC_OP : {
VariadicOp *x = (VariadicOp *)expr.ptr();
if (x->op == ADDRESS_OF) {
PVData pv = analyzeOne(x->exprs->exprs.front(), env);
if (!pv.ok())
return NULL;
if (pv.isTemp)
error("can't take address of a temporary");
}
if (!x->desugared)
x->desugared = desugarVariadicOp(x);
return analyzeExpr(x->desugared, env);
}
case EVAL_EXPR : {
EvalExpr *eval = (EvalExpr *)expr.ptr();
// XXX compilation context
ExprListPtr evaled = desugarEvalExpr(eval, env);
return analyzeMulti(evaled, env, 0);
}
case AND : {
return new MultiPValue(PVData(boolType, true));
}
case OR : {
return new MultiPValue(PVData(boolType, true));
}
case LAMBDA : {
Lambda *x = (Lambda *)expr.ptr();
if (!x->initialized)
initializeLambda(x, env);
return analyzeExpr(x->converted, env);
}
case UNPACK : {
Unpack *unpack = (Unpack *)expr.ptr();
if (unpack->expr->exprKind != FOREIGN_EXPR)
error("incorrect usage of unpack operator");
return analyzeExpr(unpack->expr, env);
}
case STATIC_EXPR : {
StaticExpr *x = (StaticExpr *)expr.ptr();
ObjectPtr obj = evaluateOneStatic(x->expr, env);
TypePtr t = staticType(obj);
return new MultiPValue(PVData(t, true));
}
case DISPATCH_EXPR : {
error("incorrect usage of dispatch operator");
return NULL;
}
case FOREIGN_EXPR : {
ForeignExpr *x = (ForeignExpr *)expr.ptr();
return analyzeExpr(x->expr, x->getEnv());
}
case OBJECT_EXPR : {
ObjectExpr *x = (ObjectExpr *)expr.ptr();
return analyzeStaticObject(x->obj);
}
default :
assert(false);
return NULL;
}
}
//
// analyzeStaticObject
//
MultiPValuePtr analyzeStaticObject(ObjectPtr x)
{
switch (x->objKind) {
case NEW_TYPE_DECL : {
NewTypeDecl *y = (NewTypeDecl *)x.ptr();
assert(y->type->typeKind == NEW_TYPE);
initializeNewType(y->type);
return new MultiPValue(PVData(y->type.ptr(), true));
}
case ENUM_MEMBER : {
EnumMember *y = (EnumMember *)x.ptr();
assert(y->type->typeKind == ENUM_TYPE);
initializeEnumType((EnumType*)y->type.ptr());
return new MultiPValue(PVData(y->type, true));
}
case GLOBAL_VARIABLE : {
GlobalVariable *y = (GlobalVariable *)x.ptr();
if (y->hasParams()) {
TypePtr t = staticType(x);
return new MultiPValue(PVData(t, true));
}
GVarInstancePtr inst = defaultGVarInstance(y);
return analyzeGVarInstance(inst);
}
case EXTERNAL_VARIABLE : {
ExternalVariable *y = (ExternalVariable *)x.ptr();
return new MultiPValue(analyzeExternalVariable(y));
}
case EXTERNAL_PROCEDURE : {
ExternalProcedure *y = (ExternalProcedure *)x.ptr();
analyzeExternalProcedure(y);
return new MultiPValue(PVData(y->ptrType, true));
}
case GLOBAL_ALIAS : {
GlobalAlias *y = (GlobalAlias *)x.ptr();
if (y->hasParams()) {
TypePtr t = staticType(x);
return new MultiPValue(PVData(t, true));
}
evaluatePredicate(y->patternVars, y->predicate, y->env);
return analyzeExpr(y->expr, y->env);
}
case VALUE_HOLDER : {
ValueHolder *y = (ValueHolder *)x.ptr();
return new MultiPValue(PVData(y->type, true));
}
case MULTI_STATIC : {
MultiStatic *y = (MultiStatic *)x.ptr();
MultiPValuePtr mpv = new MultiPValue();
for (size_t i = 0; i < y->size(); ++i) {
TypePtr t = objectType(y->values[i]);
mpv->values.push_back(PVData(t, true));
}
return mpv;
}
case RECORD_DECL : {
RecordDecl *y = (RecordDecl *)x.ptr();
ObjectPtr z;
if (y->params.empty() && !y->varParam)
z = recordType(y, vector<ObjectPtr>()).ptr();
else
z = y;
return new MultiPValue(PVData(staticType(z), true));
}
case VARIANT_DECL : {
VariantDecl *y = (VariantDecl *)x.ptr();
ObjectPtr z;
if (y->params.empty() && !y->varParam)
z = variantType(y, vector<ObjectPtr>()).ptr();
else
z = y;
return new MultiPValue(PVData(staticType(z), true));
}
case TYPE :
case PRIM_OP :
case PROCEDURE :
case MODULE :
case INTRINSIC :
case IDENTIFIER : {
TypePtr t = staticType(x);
return new MultiPValue(PVData(t, true));
}
case EVALUE : {
EValue *y = (EValue *)x.ptr();
return new MultiPValue(PVData(y->type, y->forwardedRValue));
}
case MULTI_EVALUE : {
MultiEValue *y = (MultiEValue *)x.ptr();
MultiPValuePtr z = new MultiPValue();
for (size_t i = 0; i < y->values.size(); ++i) {
EValue *ev = y->values[i].ptr();
z->values.push_back(PVData(ev->type, ev->forwardedRValue));
}
return z;
}
case CVALUE : {
CValue *y = (CValue *)x.ptr();
return new MultiPValue(PVData(y->type, y->forwardedRValue));
}
case MULTI_CVALUE : {
MultiCValue *y = (MultiCValue *)x.ptr();
MultiPValuePtr z = new MultiPValue();
for (size_t i = 0; i < y->values.size(); ++i) {
CValue *cv = y->values[i].ptr();
z->add(PVData(cv->type, cv->forwardedRValue));
}
return z;
}
case PVALUE : {
PValue *y = (PValue *)x.ptr();
return new MultiPValue(y->data);
}
case MULTI_PVALUE : {
MultiPValue *y = (MultiPValue *)x.ptr();
return y;
}
case PATTERN :
case MULTI_PATTERN :
error("pattern variable cannot be used as value");
return NULL;
default :
invalidStaticObjectError(x);
return NULL;
}
}
//
// lookupGVarInstance, defaultGVarInstance
// analyzeGVarIndexing, analyzeGVarInstance
//
GVarInstancePtr lookupGVarInstance(GlobalVariablePtr x,
llvm::ArrayRef<ObjectPtr> params)
{
if (!x->instances)
x->instances = new ObjectTable();
Pointer<RefCounted> &y = x->instances->lookup(params);
if (!y) {
y = new GVarInstance(x, params);
}
return (GVarInstance *)y.ptr();
}
GVarInstancePtr defaultGVarInstance(GlobalVariablePtr x)
{
return lookupGVarInstance(x, vector<ObjectPtr>());
}
GVarInstancePtr analyzeGVarIndexing(GlobalVariablePtr x,
ExprListPtr args,
EnvPtr env)
{
assert(x->hasParams());
MultiStaticPtr params = evaluateMultiStatic(args, env);
if (x->varParam.ptr()) {
if (params->size() < x->params.size())
arityError2(x->params.size(), params->size());
}
else {
ensureArity(params, x->params.size());
}
return lookupGVarInstance(x, params->values);
}
MultiPValuePtr analyzeGVarInstance(GVarInstancePtr x)
{
if (x->analysis.ptr())
return x->analysis;
CompileContextPusher pusher(x->gvar.ptr(), x->params);
if (x->analyzing) {
updateAnalysisErrorLocation();
return NULL;
}
GlobalVariablePtr gvar = x->gvar;
if (!x->expr)
x->expr = clone(gvar->expr);
if (!x->env) {
x->env = new Env(gvar->env);
for (size_t i = 0; i < gvar->params.size(); ++i) {
addLocal(x->env, gvar->params[i], x->params[i]);
}
if (gvar->varParam.ptr()) {
MultiStaticPtr varParams = new MultiStatic();
for (size_t i = gvar->params.size(); i < x->params.size(); ++i)
varParams->add(x->params[i]);
addLocal(x->env, gvar->varParam, varParams.ptr());
}
}
x->analyzing = true;
evaluatePredicate(x->gvar->patternVars, x->gvar->predicate, x->env);
PVData pv = analyzeOne(x->expr, x->env);
x->analyzing = false;
if (!pv.ok())
return NULL;
x->analysis = new MultiPValue(PVData(pv.type, false));
x->type = pv.type;
return x->analysis;
}
//
// analyzeExternalVariable
//
PVData analyzeExternalVariable(ExternalVariablePtr x)
{
if (!x->type2)
x->type2 = evaluateType(x->type, x->env);
return PVData(x->type2, false);
}
//
// analyzeExternalProcedure
//
void analyzeExternalProcedure(ExternalProcedurePtr x)
{
if (x->analyzed)
return;
if (!x->attributesVerified)
verifyAttributes(x);
vector<TypePtr> argTypes;
for (size_t i = 0; i < x->args.size(); ++i) {
ExternalArgPtr y = x->args[i];
y->type2 = evaluateType(y->type, x->env);
argTypes.push_back(y->type2);
}
if (!x->returnType)
x->returnType2 = NULL;