-
Notifications
You must be signed in to change notification settings - Fork 94
/
dec.cc
1489 lines (1257 loc) · 35.5 KB
/
dec.cc
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
/*****
* dec.cc
* Andy Hammerlindl 2002/8/29
*
* Represents the abstract syntax tree for declarations in the language.
* Also included is an abstract syntax for types as they are most often
* used with declarations.
*****/
#include "errormsg.h"
#include "coenv.h"
#include "dec.h"
#include "fundec.h"
#include "newexp.h"
#include "stm.h"
#include "exp.h"
#include "modifier.h"
#include "runtime.h"
#include "locate.h"
#include "asyparser.h"
// #include "builtin.h" // for trans::addRecordOps
namespace absyntax {
using namespace trans;
using namespace types;
using mem::list;
trans::tyEntry *astType::transAsTyEntry(coenv &e, record *where)
{
return new trans::tyEntry(trans(e, false), 0, where, getPos());
}
void nameTy::prettyprint(ostream &out, Int indent)
{
prettyname(out, "nameTy",indent, getPos());
id->prettyprint(out, indent+1);
}
types::ty *nameTy::trans(coenv &e, bool tacit)
{
return id->typeTrans(e, tacit);
}
trans::tyEntry *nameTy::transAsTyEntry(coenv &e, record *)
{
return id->tyEntryTrans(e);
}
nameTy::operator string() const
{
return static_cast<string>(id->getName());
}
void dimensions::prettyprint(ostream &out, Int indent)
{
prettyindent(out, indent);
out << "dimensions (" << depth << ")\n";
}
types::array *dimensions::truetype(types::ty *base, bool tacit)
{
if (!tacit && base->kind == ty_void) {
em.error(getPos());
em << "cannot declare array of type void";
}
assert(depth >= 1);
size_t d=depth;
types::array *a=new types::array(base); d--;
for (; d > 0; d--) {
a = new types::array(a);
}
return a;
}
void arrayTy::prettyprint(ostream &out, Int indent)
{
prettyname(out, "arrayTy",indent, getPos());
cell->prettyprint(out, indent+1);
dims->prettyprint(out, indent+1);
}
// NOTE: Can this be merged with trans somehow?
void arrayTy::addOps(coenv &e, record *r)
{
types::ty *t=trans(e, true);
// Only add ops if it is an array (and not, say, an error)
if (t->kind == types::ty_array) {
types::array *at=dynamic_cast<types::array *>(t);
assert(at);
e.e.addArrayOps(at);
if (r)
r->e.addArrayOps(at);
}
}
types::ty *arrayTy::trans(coenv &e, bool tacit)
{
types::ty *ct = cell->trans(e, tacit);
assert(ct);
// Don't make an array of errors.
if (ct->kind == types::ty_error)
return ct;
types::array *t = dims->truetype(ct,tacit);
assert(t);
return t;
}
arrayTy::operator string() const
{
stringstream ss;
ss << static_cast<string>(*cell);
for (size_t i = 0; i < dims->size(); i++)
{
ss << "[]";
}
return ss.str();
}
tyEntryTy::tyEntryTy(position pos, types::ty *t)
: astType(pos), ent(new trans::tyEntry(t, 0, 0, position()))
{
}
void tyEntryTy::prettyprint(ostream &out, Int indent)
{
prettyindent(out,indent);
out << "tyEntryTy: " << *(ent->t) << "\n";
}
types::ty *tyEntryTy::trans(coenv &, bool) {
return ent->t;
}
tyEntryTy::operator string() const
{
return "<unknown-type>";
}
vm::lambda *runnable::transAsCodelet(coenv &e)
{
coder c=e.c.newCodelet(getPos());
coenv ce(c, e.e);
markTrans(ce);
return c.close();
}
void block::prettystms(ostream &out, Int indent)
{
for (list<runnable *>::iterator p = stms.begin(); p != stms.end(); ++p)
(*p)->prettyprint(out, indent);
}
void block::prettyprint(ostream &out, Int indent)
{
prettyname(out,"block",indent,getPos());
prettystms(out, indent+1);
}
// Uses RAII to ensure scope is ended when function returns.
class Scope {
coenv* e;
public:
Scope(coenv &e, bool scope) : e(scope ? &e : nullptr) {
if (this->e) this->e->e.beginScope();
}
~Scope() {
if (this->e) e->e.endScope();
}
};
void block::trans(coenv &e)
{
Scope scopeHolder(e, scope);
for (list<runnable *>::iterator p = stms.begin(); p != stms.end(); ++p) {
(*p)->markTrans(e);
}
}
void block::transAsField(coenv &e, record *r)
{
Scope scopeHolder(e, scope);
for (list<runnable *>::iterator p = stms.begin(); p != stms.end(); ++p) {
(*p)->markTransAsField(e, r);
if (em.errors() && !settings::getSetting<bool>("debug"))
break;
}
}
bool block::transAsTemplatedField(
coenv &e, record *r, mem::vector<absyntax::namedTyEntry*>* args,
frame *caller
) {
Scope scopeHolder(e, scope);
auto p = stms.begin();
if (p == stms.end()) {
return true; // empty file
}
receiveTypedefDec *dec = dynamic_cast<receiveTypedefDec *>(*p);
if (!dec) {
em.error(getPos());
em << "expected 'typedef import(<types>);'";
em.sync(true);
return false;
}
if(!dec->transAsParamMatcher(e, r, args, caller))
return false;
while (++p != stms.end()) {
(*p)->markTransAsField(e, r);
if (em.errors() && !settings::getSetting<bool>("debug")) {
return false;
}
}
em.sync();
return true;
}
void block::transAsRecordBody(coenv &e, record *r)
{
transAsField(e, r);
e.c.closeRecord();
}
bool block::transAsTemplatedRecordBody(
coenv &e, record *r, mem::vector<absyntax::namedTyEntry*> *args,
frame *caller
) {
bool succeeded = transAsTemplatedField(e, r, args, caller);
e.c.closeRecord();
return succeeded;
}
record *block::transAsFile(genv& ge, symbol id)
{
// Create the new module.
record *r = new record(id, new frame(id,0,0));
// Create coder and environment to translate the module.
// File-level modules have dynamic fields by default.
coder c(getPos(), r, 0);
env e(ge);
coenv ce(c, e);
// Translate the abstract syntax.
if (settings::getSetting<bool>("autoplain")) {
autoplainRunnable()->transAsField(ce, r);
}
transAsRecordBody(ce, r);
em.sync();
if (em.errors()) return nullptr;
return r;
}
record *block::transAsTemplatedFile(
genv& ge,
symbol id,
mem::vector<absyntax::namedTyEntry*>* args,
coenv& cE
) {
for (auto p = args->rbegin(); p != args->rend(); ++p) {
namedTyEntry *arg = *p;
tyEntry *ent = arg->ent;
if(ent->t->kind == types::ty_record) {
varEntry *v = ent->v;
if (v) {
// Push the value of v to the stack.
v->getLocation()->encode(READ, arg->pos, cE.c);
} else {
// Push the appropriate frame to the stack.
newRecordExp::encodeLevel(arg->pos,cE,ent);
}
}
}
// Create the new module.
record *r = new record(id, new frame(id, 0, 0));
// Create coder and environment to translate the module.
// File-level modules have dynamic fields by default.
coder c(getPos(), r, 0);
env e(ge);
coenv ce(c, e);
// Translate the abstract syntax.
if (settings::getSetting<bool>("autoplain")) {
autoplainRunnable()->transAsField(ce, r);
}
bool succeeded = transAsTemplatedRecordBody(ce, r, args, cE.c.getFrame());
if (!succeeded) {
return nullptr;
}
return r;
}
bool block::returns() {
// Search for a returning runnable, starting at the end for efficiency.
for (list<runnable *>::reverse_iterator p=stms.rbegin();
p != stms.rend();
++p)
if ((*p)->returns())
return true;
return false;
}
vardec *block::asVardec()
{
vardec *var = 0;
for (list<runnable *>::iterator p=stms.begin();
p != stms.end();
++p)
{
vardec *v = dynamic_cast<vardec *>(*p);
if (v) {
if (var)
// Multiple vardecs.
return 0;
var = v;
}
else if (!dynamic_cast<emptyStm *>(*p))
// Failure due to another runnable in the block.
return 0;
}
return var;
}
void block::createSymMap(AsymptoteLsp::SymbolContext* symContext) {
#ifdef HAVE_LSP
for (auto const& p : stms)
{
p->createSymMap(symContext);
}
#endif
}
void dec::prettyprint(ostream &out, Int indent)
{
prettyname(out, "dec", indent, getPos());
}
void modifierList::prettyprint(ostream &out, Int indent)
{
prettyindent(out,indent);
out << "modifierList (";
for (list<modifier>::iterator p = mods.begin(); p != mods.end(); ++p) {
if (p != mods.begin())
out << ", ";
switch (*p) {
case EXPLICIT_STATIC:
out << "static";
break;
#if 0
case EXPLICIT_DYNAMIC:
out << "dynamic";
break;
#endif
default:
out << "invalid code";
}
}
for (list<permission>::iterator p = perms.begin(); p != perms.end(); ++p) {
if (p != perms.begin() || !mods.empty())
out << ", ";
switch (*p) {
case PUBLIC:
out << "public";
break;
case PRIVATE:
out << "private";
break;
default:
out << "invalid code";
}
}
out << ")\n";
}
bool modifierList::staticSet()
{
return !mods.empty();
}
modifier modifierList::getModifier()
{
if (mods.size() > 1) {
em.error(getPos());
em << "too many modifiers";
}
assert(staticSet());
return mods.front();
}
permission modifierList::getPermission()
{
if (perms.size() > 1) {
em.error(getPos());
em << "too many modifiers";
}
return perms.empty() ? DEFAULT_PERM : perms.front();
}
void modifiedRunnable::prettyprint(ostream &out, Int indent)
{
prettyname(out, "modifierRunnable",indent, getPos());
mods->prettyprint(out, indent+1);
body->prettyprint(out, indent+1);
}
void modifiedRunnable::transAsField(coenv &e, record *r)
{
if (mods->staticSet()) {
if (e.c.isTopLevel()) {
em.warning(getPos());
em << "static modifier is meaningless at top level";
}
e.c.pushModifier(mods->getModifier());
}
permission p = mods->getPermission();
#if 0 // This is innocuous
if (p != DEFAULT_PERM && (!r || !body->allowPermissions())) {
em.warning(pos);
em << "permission modifier is meaningless";
}
#endif
e.c.setPermission(p);
body->transAsField(e,r);
e.c.clearPermission();
if (mods->staticSet())
e.c.popModifier();
}
void decidstart::prettyprint(ostream &out, Int indent)
{
prettyindent(out, indent);
out << "decidstart '" << id << "'\n";
if (dims)
dims->prettyprint(out, indent+1);
}
types::ty *decidstart::getType(types::ty *base, coenv &, bool)
{
return dims ? dims->truetype(base) : base;
}
trans::tyEntry *decidstart::getTyEntry(trans::tyEntry *base, coenv &e,
record *where)
{
return dims ? new trans::tyEntry(getType(base->t,e,false), 0,
where, getPos()) :
base;
}
void decidstart::addOps(types::ty *base, coenv &e, record *r)
{
if (dims) {
array *a=dims->truetype(base);
e.e.addArrayOps(a);
if (r)
r->e.addArrayOps(a);
}
}
void decidstart::createSymMap(AsymptoteLsp::SymbolContext* symContext)
{
#ifdef HAVE_LSP
std::string name(static_cast<std::string>(getName()));
AsymptoteLsp::posInFile pos(getPos().LineColumn());
if (auto decCtx=dynamic_cast<AsymptoteLsp::AddDeclContexts*>(symContext))
{
decCtx->additionalDecs.emplace(std::piecewise_construct,
std::forward_as_tuple(name), std::forward_as_tuple(name, pos));
}
else
{
symContext->symMap.varDec[name] = AsymptoteLsp::SymbolInfo(name, pos);
}
#endif
}
void decidstart::createSymMapWType(
AsymptoteLsp::SymbolContext* symContext, absyntax::astType* base
) {
#ifdef HAVE_LSP
std::string name(static_cast<std::string>(getName()));
AsymptoteLsp::posInFile pos(getPos().LineColumn());
if (auto decCtx=dynamic_cast<AsymptoteLsp::AddDeclContexts*>(symContext))
{
if (base == nullptr)
{
decCtx->additionalDecs.emplace(std::piecewise_construct,
std::forward_as_tuple(name),
std::forward_as_tuple(name, pos));
}
else
{
decCtx->additionalDecs.emplace(
std::piecewise_construct,
std::forward_as_tuple(name),
std::forward_as_tuple(name, static_cast<std::string>(*base), pos)
);
}
}
else
{
symContext->symMap.varDec[name] = base == nullptr ?
AsymptoteLsp::SymbolInfo(name, pos) :
AsymptoteLsp::SymbolInfo(name,
static_cast<std::string>(*base),
pos);
}
#endif
}
void fundecidstart::prettyprint(ostream &out, Int indent)
{
prettyindent(out, indent);
out << "fundecidstart '" << id << "'\n";
if (dims)
dims->prettyprint(out, indent+1);
if (params)
params->prettyprint(out, indent+1);
}
types::ty *fundecidstart::getType(types::ty *base, coenv &e, bool tacit)
{
types::ty *result = decidstart::getType(base, e, tacit);
if (params) {
return params->getType(result, e, true, tacit);
}
else {
types::ty *t = new function(base);
return t;
}
}
trans::tyEntry *fundecidstart::getTyEntry(trans::tyEntry *base, coenv &e,
record *where)
{
return new trans::tyEntry(getType(base->t,e,false), 0, where, getPos());
}
void fundecidstart::addOps(types::ty *base, coenv &e, record *r)
{
decidstart::addOps(base, e, r);
params->addOps(e, r);
types::function *ft=dynamic_cast<types::function *>(getType(base, e, true));
assert(ft);
e.e.addFunctionOps(ft);
if (r)
r->e.addFunctionOps(ft);
}
void decid::prettyprint(ostream &out, Int indent)
{
prettyname(out, "decid",indent, getPos());
start->prettyprint(out, indent+1);
if (init)
init->prettyprint(out, indent+1);
}
varEntry *makeVarEntryWhere(coenv &e, record *r, types::ty *t,
record *where, position pos)
{
access *a = r ? r->allocField(e.c.isStatic()) :
e.c.allocLocal();
return r ? new varEntry(t, a, e.c.getPermission(), r, where, pos) :
new varEntry(t, a, where, pos);
}
varEntry *makeVarEntry(position pos, coenv &e, record *r, types::ty *t) {
return makeVarEntryWhere(e, r, t, r, pos);
}
// Defined in constructor.cc.
bool definesImplicitConstructor(coenv &e, record *r, varEntry *v, symbol id);
void addConstructorFromInitializer(position pos, coenv &e, record *r,
varEntry *init);
void addVar(coenv &e, record *r, varEntry *v, symbol id)
{
// Test for 'operator init' definitions that implicitly define constructors:
if (definesImplicitConstructor(e, r, v, id))
addConstructorFromInitializer(position(), e, r, v);
// Add to the record so it can be accessed when qualified; add to the
// environment so it can be accessed unqualified in the scope of the
// record definition.
if (r)
r->e.addVar(id, v);
e.e.addVar(id, v);
}
void initializeVar(position pos, coenv &e, varEntry *v, varinit *init)
{
types::ty *t=v->getType();
if (init)
init->transToType(e, t);
else {
definit d(pos);
d.transToType(e, t);
}
v->getLocation()->encode(WRITE, pos, e.c);
e.c.encodePop();
}
types::ty *inferType(position pos, coenv &e, varinit *init)
{
if (!init) {
em.error(pos);
em << "inferred variable declaration without initializer";
return primError();
}
exp *base = dynamic_cast<exp *>(init);
bool Void=false;
if (base) {
types::ty *t = base->cgetType(e);
Void=t->kind == ty_void;
if (t->kind != ty_overloaded && !Void)
return t;
}
em.error(pos);
em << (Void ? "cannot infer from void" :
"could not infer type of initializer");
return primError();
}
void createVar(position pos, coenv &e, record *r,
symbol id, types::ty *t, varinit *init)
{
// I'm not sure how to handle inferred types in these cases.
assert(t->kind != types::ty_inferred);
varEntry *v=makeVarEntry(pos, e, r, t);
addVar(e, r, v, id);
initializeVar(pos, e, v, init);
}
void createVarOutOfOrder(position pos, coenv &e, record *r,
symbol id, types::ty *t, varinit *init)
{
/* For declarations such as "var x = 5;", infer the type from the
* initializer.
*/
if (t->kind == types::ty_inferred)
t = inferType(pos, e, init);
varEntry *v=makeVarEntry(pos, e, r, t);
initializeVar(pos, e, v, init);
addVar(e, r, v, id);
}
void addTypeWithPermission(coenv &e, record *r, tyEntry *base, symbol id)
{
// Only bother encoding permissions for private types.
tyEntry *ent = (r && e.c.getPermission()==PRIVATE) ?
new trans::tyEntry(base, PRIVATE, r) :
base;
if (r)
r->e.addType(id, ent);
e.e.addType(id, ent);
}
void decid::transAsField(coenv &e, record *r, types::ty *base)
{
types::ty *t = start->getType(base, e);
assert(t);
if (t->kind == ty_void) {
em.error(getPos());
em << "cannot declare variable of type void";
}
start->addOps(base, e, r);
createVarOutOfOrder(getPos(), e, r, start->getName(), t, init);
}
void decid::transAsTypedefField(coenv &e, trans::tyEntry *base, record *r)
{
trans::tyEntry *ent = start->getTyEntry(base, e, r);
assert(ent && ent->t);
if (init) {
em.error(getPos());
em << "type definition cannot have initializer";
}
start->addOps(base->t, e, r);
addTypeWithPermission(e, r, ent, start->getName());
}
void decid::createSymMap(AsymptoteLsp::SymbolContext* symContext)
{
#ifdef HAVE_LSP
start->createSymMap(symContext);
if (init)
{
init->createSymMap(symContext);
}
#endif
}
void decid::createSymMapWType(
AsymptoteLsp::SymbolContext* symContext, absyntax::astType* base
) {
#ifdef HAVE_LSP
start->createSymMapWType(symContext, base);
if (init)
{
init->createSymMap(symContext);
}
#endif
}
void decidlist::prettyprint(ostream &out, Int indent)
{
prettyname(out, "decidlist",indent, getPos());
for (list<decid *>::iterator p = decs.begin(); p != decs.end(); ++p)
(*p)->prettyprint(out, indent+1);
}
void decidlist::transAsField(coenv &e, record *r, types::ty *base)
{
for (list<decid *>::iterator p = decs.begin(); p != decs.end(); ++p)
(*p)->transAsField(e, r, base);
}
void decidlist::transAsTypedefField(coenv &e, trans::tyEntry *base, record *r)
{
for (list<decid *>::iterator p = decs.begin(); p != decs.end(); ++p)
(*p)->transAsTypedefField(e, base, r);
}
void decidlist::createSymMap(AsymptoteLsp::SymbolContext* symContext) {
#ifdef HAVE_LSP
for (auto const& p : decs)
{
p->createSymMap(symContext);
}
#endif
}
void decidlist::createSymMapWType(
AsymptoteLsp::SymbolContext* symContext, absyntax::astType* base
) {
#ifdef HAVE_LSP
for (auto const& p : decs)
{
p->createSymMapWType(symContext, base);
}
#endif
}
void vardec::prettyprint(ostream &out, Int indent)
{
prettyname(out, "vardec",indent, getPos());
base->prettyprint(out, indent+1);
decs->prettyprint(out, indent+1);
}
void vardec::transAsTypedefField(coenv &e, record *r)
{
base->addOps(e, r);
decs->transAsTypedefField(e, base->transAsTyEntry(e, r), r);
}
symbol vardec::singleName()
{
decid *did = decs->singleEntry();
if (!did)
return symbol::nullsym;
return did->getStart()->getName();
}
types::ty *vardec::singleGetType(coenv &e)
{
decid *did = decs->singleEntry();
if (!did)
return 0;
return did->getStart()->getType(base->trans(e), e);
}
void vardec::createSymMap(AsymptoteLsp::SymbolContext* symContext)
{
#ifdef HAVE_LSP
decs->createSymMapWType(symContext, base);
#endif
}
// Helper class for imports. This essentially evaluates to the run::loadModule
// function. However, that function returns different types of records
// depending on the filename given to it, so we cannot add it to the
// environment. Instead, we explicitly tell it what types::record it is
// returning for each use.
class loadModuleExp : public exp {
function *ft;
public:
loadModuleExp(position pos, record *imp)
: exp(pos) {ft=new function(imp,primString());}
void prettyprint(ostream &out, Int indent) {
prettyname(out, "loadModuleExp", indent, getPos());
}
types::ty *trans(coenv &) {
em.compiler(getPos());
em << "trans called for loadModuleExp";
return primError();
}
void transCall(coenv &e, types::ty *t) {
assert(equivalent(t, ft));
e.c.encode(inst::builtin, run::loadModule);
}
types::ty *getType(coenv &) {
return ft;
}
exp *evaluate(coenv &, types::ty *) {
// Don't alias.
return this;
}
};
// Creates a local variable to hold the import and translate the accessing of
// the import, but doesn't add the import to the environment.
varEntry *accessModule(position pos, coenv &e, record *r, symbol id)
{
string filename=(string) id;
record *imp=e.e.getModule(id, filename);
if (!imp) {
em.error(pos);
em << "could not load module '" << filename << "'";
em.sync(true);
return 0;
}
else {
// Create a varinit that evaluates to the module.
// This is effectively the expression 'loadModule(filename)'.
callExp init(pos, new loadModuleExp(pos, imp),
new stringExp(pos, filename));
// The varEntry should have whereDefined()==0 as it is not defined inside
// the record r.
varEntry *v=makeVarEntryWhere(e, r, imp, 0, pos);
initializeVar(pos, e, v, &init);
return v;
}
}
// Creates a local variable to hold the import and translate the accessing of
// the import, but doesn't add the import to the environment.
varEntry *accessTemplatedModule(position pos, coenv &e, record *r, symbol id,
formals *args)
{
string filename=(string) id;
stringstream buf;
buf << id << '/' << args->getSignature(e)->handle() << '/';
symbol index=symbol::literalTrans(buf.str());
auto *computedArgs = new mem::vector<namedTyEntry*>();
mem::vector<tySymbolPair> *fields = args->getFields();
for (auto p = fields->begin(); p != fields->end(); ++p) {
astType* theType = p->ty;
symbol theName = p->sym;
if (theName == symbol::nullsym) {
em.error(theType->getPos());
em << "expected typename=";
em.sync(true);
return nullptr;
}
computedArgs->push_back(new namedTyEntry(
theType->getPos(), theName, theType->transAsTyEntry(e, r)
));
}
record *imp=e.e.getTemplatedModule(index,filename,computedArgs,e);
if (!imp) {
em.error(pos);
em << "could not load module '" << id << "'";
em.sync(true);
return nullptr;
}
else {
// Create a varinit that evaluates to the module.
// This is effectively the expression 'loadModule(index)'.
callExp init(pos, new loadModuleExp(pos, imp), new stringExp(pos, index));
// The varEntry should have whereDefined()==0 as it is not defined inside
// the record r.
varEntry *v=makeVarEntryWhere(e, r, imp, 0, pos);
initializeVar(pos, e, v, &init);
return v;
}
}
void idpair::prettyprint(ostream &out, Int indent)
{
prettyindent(out, indent);
out << "idpair (" << "'" << src << "' as " << dest << ")\n";
}
void idpair::transAsAccess(coenv &e, record *r)
{
checkValidity();
varEntry *v=accessModule(getPos(), e, r, src);
if (v)
addVar(e, r, v, dest);
}
void idpair::transAsUnravel(coenv &e, record *r,
protoenv &source, varEntry *qualifier)
{
checkValidity();
if (r)
r->e.add(src, dest, source, qualifier, e.c);
if (!e.e.add(src, dest, source, qualifier, e.c)) {
em.error(getPos());
em << "no matching types or fields of name '" << src << "'";
}
}
void idpair::createSymMap(AsymptoteLsp::SymbolContext* symContext)
{
#ifdef HAVE_LSP
if (valid)
{
string fullSrc(settings::locateFile(src, true));
if (not AsymptoteLsp::isVirtualFile((std::string)(fullSrc.c_str())))
{
if (not fullSrc.empty())
{
symContext->addEmptyExtRef((std::string)(fullSrc.c_str()));
}
// add (dest, source) to reference map.
auto s = symContext->extRefs.fileIdPair.emplace(
dest, (std::string) fullSrc.c_str()
);
auto it=std::get<0>(s);