forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathidentifierresolution.grace
1122 lines (1043 loc) · 43.8 KB
/
identifierresolution.grace
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
dialect "standard"
import "io" as io
import "sys" as sys
import "ast" as ast
import "util" as util
import "xmodule" as xmodule
import "errormessages" as errormessages
import "scope" as sm
import "constantScope" as constantScope
import "basic" as basic
use basic.open
def completed = singleton "completed"
def inProgress = singleton "inProgress"
def undiscovered = singleton "undiscovered"
// constants used in detecting cyclic inheritance
var stSerial := 100
def keyOrdering = { a, b → a.key.compare(b.key) }
def dialectScope = sm.graceDialectScope.in(constantScope.builtInsScope)
def moduleScope = sm.moduleScope.in(dialectScope)
def varFieldDecls = list [] // a list of nodes that declare var fields
util.setPosition(0, 0)
method transformIdentifier(anIdentifier) ancestors(anc) {
// anIdentifier is a (copy of an) ast node that represents an applied
// occurrence of an identifer id.
// This method may or may not transform anIdentifier into another ast node.
// If anIdentifier refers to a variable in a block or method,
// then it should be left as a variable. However, if it refers to
// a field of an object, then it is tranformed into a method request.
// Note that it would be possible for confidential fields of stale objects to
// be compiled as identifiers. We do not do so, because distinguishing
// this case seems like a lot of work, and V8 will probably inline the
// accessors for us anyway
if (anIdentifier.isAssigned) then {
return anIdentifier // will be transformed to a request by transformBind.
// Transformation is bottom-up, so this has yet to happen
}
def defs = sm.variableResolver.definitionsOf (anIdentifier.name) visibleIn (anIdentifier.scope)
if (defs.isEmpty) then {
errormessages.undeclaredIdentifier (anIdentifier)
}
if (defs.size > 1) then {
errormessages.ambiguityError (defs) node (anIdentifier)
}
def resolution = defs.first
def variable = resolution.definition
if (variable.isMethodOrType) then {
generateOneselfRequestFrom (anIdentifier) using (resolution)
} elseif { variable.definingScope.varsAreMethods } then {
// Anything defined in a fresh scope, including a var, can be overridden,
// so we need to access it via a request.
generateOneselfRequestFrom (anIdentifier) using (resolution)
} else {
anIdentifier
}
}
method generateOneselfRequestFrom (aSourceNode) using (aResolvedVariable) {
// generates and returns some form of "self request" based on aSourceNode.
// The receiver of the generated request may be a yourselfNode, a bind node,
// or the identifier for the module or dialect.
def objectsUp = aResolvedVariable.objectsUp
def nodeScope = aSourceNode.scope
def outerChain = list.empty
var s := nodeScope.currentObjectScope
repeat (objectsUp) times {
outerChain.addLast(s.node)
s := s.enclosingObjectScope
}
def v = s.variety
def receiver = if ("dialect | builtIn | module".contains(v)) then {
ast.identifier("$" ++ v, false).setScope (nodeScope)
} else {
def ynd = ast.yourselfNode(outerChain.size).setScope(nodeScope)
ynd.line := aSourceNode.line
ynd.theObjects := outerChain
ynd
}
def result = if (aSourceNode.numArgs == 0) then {
ast.requestWithoutArgs (aSourceNode.nameString, receiver)
} else {
ast.request (receiver, aSourceNode.parts)
}
result.setScope(nodeScope).
onSelf.
withGenericArgs (aSourceNode.generics).
setPositionFrom (aSourceNode)
}
method resolveIdentifiers(topNode) {
// Recursively replace bare identifiers with their fully-qualified
// equivalents. Creates and returns a new AST; map works
// bottom-up, so by the time a node is mapped, all of its
// descendents have already been mapped.
def newModule = topNode.map { node, anc →
if ( node.isAppliedOccurrence ) then {
transformIdentifier(node) ancestors(anc)
} elseif { node.isCall } then {
transformCall(node)
} elseif { node.isInherits } then {
transformReuse(node) ancestors(anc)
} elseif { node.isAssignment } then {
transformBind(node) ancestors(anc)
} else {
node
}
} ancestors (ast.ancestorChain.empty)
newModule
}
method writeGctForModule(moduleObject) {
// requested by genjs to write the gct to the generated JS file
xmodule.writeGCT(moduleObject.name, generateGctForModule(moduleObject))
}
method generateGctForModule(module) {
// The gct is essentially a representation of module's symbol table.
// We built this representation by iterating over the symbol table.
// Older versions of this method used to iterate over the ast,
// but reused methods are not in the ast, and so were omitted.
//
// The Gct should be thought of as a mapping from keys to collections of
// "gcLines". A plain-text version of any gct can be obtained by running
// tools/eg --gct module.js on the compiled file module.js
//
// Important keys are:
// -- dialect: the name of the dialect in which this module is written
// (empty if no dialect)
// -- freshScopes: the uid of every scope that describes a fresh object
// the uid is tagged with "trait" if the object is a trait
// -- methodTypes:<uid>: the type signatures of all methods defined in scope <uid>
// -- modules: the names of the directly-imported modules (including the dialect)
// -- path: the path in the file system where the source code was found
// -- self: the value is the uid for the moduleScope
// -- scope:<uid>: the entries in that scope
// -- types: the declarations of all named types. The name is prefixed
// by the uid of the scope that defines that type name
//
// The lines in a scope:<uid> entry are parsed by addGctLine(_)toScope(_).
// Each has one of two formats:
// - <typeName> type
// this is used for types
// - <methodName> <declaredType> <kindString> <attributeScope> <attributes>
// used for everything else.
// <attributes> is optional, and consists of a comma-separated list of strings
def gct = dictionary.empty
def theDialect = module.theDialect.moduleName
def methodList = list.empty
def typeList = list.empty
def ms = module.scope
gct.at "self" put [ms.uid]
def scopesToProcess = set.with(ms)
def scopesAlreadyProcessed = set.withAll(sm.predefined.values)
var pathCount := 0
def epl = sys.environ.at "PATH_LIMIT"
def pathLimit = if (epl.isEmpty) then { 1000 } else {epl.asNumber}
while { scopesToProcess.isEmpty.not } do {
def s = scopesToProcess.anyone
scopesToProcess.remove(s)
if (scopesAlreadyProcessed.contains(s).not) then {
scopesAlreadyProcessed.add(s)
def entries = list.empty
s.localAndReusedNamesAndValuesDo { vName, v →
if (v.forGct) then {
entries.add(serializeVariable (v) withName(vName) in (s))
def subScope = v.attributeScope
if (scopesAlreadyProcessed.contains(subScope).not) then {
if (pathCount < pathLimit) then {
scopesToProcess.add(subScope)
pathCount := pathCount + 1
}
}
}
}
gct.at "scope:{s.uid}" put (entries.sort) // yes, this will store an empty scope
if (s.methodTypes.isEmpty.not) then {
gct.at "methodTypes:{s.uid}" put (s.methodTypes.values.sorted)
}
s.types.keysAndValuesDo { eachType, eachDef →
gct.at "typedec:{s.uid}.{eachType}" put [eachDef]
typeList.add "{s.uid}.{eachType}"
}
}
}
gct.at "types" put (typeList.sort)
gct.at "modules" put (xmodule.externalModules.keys.sorted)
def p = util.infile.pathname
gct.at "path" put [ if (p.isEmpty) then {
""
} elseif { p.startsWith "/" } then {
p
} else {
io.realpath(p)
} ]
gct.at "dialect" put (
if (theDialect == "none") then { [] } else { [theDialect] }
)
gct.at "freshScopes" put (
scopesAlreadyProcessed.filter {s -> s.isFresh}.map { s ->
def traitSuffix = if (s.isTrait) then {
" trait"
} else {
""
}
s.uid ++ traitSuffix
}.sorted )
util.log_verbose "{pathCount} paths added to gct (limit = {pathLimit})"
gct
}
method serializeVariable (defn) withName(n) in (s) {
// returns a string representation of the variable defn
// Note that in the case of a writer method for a variable x,
// n will be x:=(1), whereas defn.name will be x
if (defn.isType) then { return "{n} type" }
var anns
if { defn.annotations.isEmpty } then {
anns := ""
} else {
anns := " "
defn.annotations.do { each →
anns := anns ++ each.nameString
} separatedBy { anns := anns ++ "," }
}
def attrScp = defn.attributeScope
def tn = typeName (defn.declaredType) in (attrScp)
"{n} {tn} {defn.tag} {attrScp.uid}{anns}"
}
type HasName = interface { nameString → String }
method typeName (typeNode) in (scope) {
// returns a name for the type expression denoted by typeNode.
// typeNode may be an ast.identifierNode, a constantScope.typeNode
// or a string (such as "Unknown").
// If necessary, creates a name starting with $, and enters it in scope's
// types dictionary
match(typeNode)
case { nd:HasName -> nd.nameString
} case { s:String -> s
} else {
def name = "$type{sequenceNr}"
scope.types.at (name) put (typeNode.toGrace 0)
name
}
}
var seed := 100
method sequenceNr {
seed := seed + 1
seed
}
method methodSignature(methNode) → String {
var s: String := ""
var shouldEmitTypeParams := methNode.hasTypeParams
methNode.signatureParts.do { part →
s := s ++ part.name
if (shouldEmitTypeParams) then {
s := s ++ methNode.typeParams.toGrace 1
shouldEmitTypeParams := false // emit them once, after first part
}
if (part.params.isEmpty.not) then {
s := s ++ "("
part.params.do { p →
s := "{s}{p.toGrace 1}:{p.decType.toGrace 1}"
} separatedBy {
s := "{s}, "
}
s := s ++ ")"
}
}
"{s} → {methNode.decType.toGrace 0}"
}
method readerSignature(declNode) → String {
"{declNode.nameString} → {declNode.decType.toGrace 0}"
}
method writerSignature(declNode) → String {
"{declNode.nameString}:=(_:{declNode.decType.toGrace 0}) → Done"
}
def importedScopes = dictionary.empty
method processGct(gct, importedModuleScope) {
// Populates importedModuleScope with the information in gct,
// which is a dictionary mapping gct keys to collections.
// TODO: make the gct dictionary a real object.
def moduleName = (ast.withoutLeadingComponents (gct.at "path".first)).
replace ".grace" with ""
importedScopes.clear // because we will be importing multiple modules
def moduleScopeId = gct.at "self".first
def scopeKey = "scope:{moduleScopeId}"
importedScopes.at (moduleScopeId) put (importedModuleScope)
gct.at (scopeKey) ifAbsent {
ProgrammingError.raise "gct for \"{moduleName}\" missing \"{scopeKey}\" for the module itself"
}.do { gctLine →
addGctLine (gctLine) toScope (importedModuleScope) for (gct)
}
gct.at "freshScopes".do { eachLine ->
def elems = eachLine.split " "
def eachScope = scopeWithUid(elems.first) for (gct)
eachScope.isFresh := true
if ((elems.size > 1) && {elems.second == "trait"}) then {
eachScope.isTrait := true
}
}
}
method addGctLine (gctLine:String) toScope (s) for (gct) {
// Adds a symbol table entry based on gctLine to scope s
//
// gctLine is generated by the method generateGctForModule, and has format
// <methodName> <declaredType> <kindString> <attributeScope> <attributes>
// where attributes is optional, and consists of a comma-separated list of strings
def split = gctLine.split " "
var newVar
if (split.size == 2) then {
def typeName = split.first
newVar := sm.typeVariableFrom (
constantScope.typeNode (typeName) params (numTypeParams(typeName)))
} elseif { split.size ≥ 4 } then {
def name = split.first
def typeName = split.second
def tag = split.third
def scpdId = split.fourth
newVar := sm.variable (tag) from (
constantScope.pseudoNode (name) typed (typeName) scope (s).
attributeScope (scopeWithUid(scpdId) for (gct)))
if (split.size ≥ 5) then {
def annNames = split.fifth
newVar.annotationNames := annNames.split ","
if (newVar.annotationNames.contains "$fresh") then {
newVar.attributeScope.markAsFresh
}
}
} else {
EnvironmentException.raise "gct line \"{gctLine}\" has wrong number of fields"
}
s.add(newVar)
}
method numTypeParams(typeName) is confidential {
// typeName can contain a parameter list, as in "Dictonary⟦K,T⟧"
var ix := typeName.indexOf "⟦" ifAbsent { return 0 }
var p := 1
while { true } do {
ix := typeName.indexOf "," startingAt (ix + 1) ifAbsent { return p }
p := p + 1
}
}
method scopeWithUid(str) for (gct) {
// find the appropriate external scope, or create it if it
// does not yet exist
def result = importedScopes.at(str) ifAbsent {
sm.predefined.at(str) ifAbsent {
def newScope = sm.externalScope
def scopeKey = "scope:{str}"
importedScopes.at(str) put (newScope)
gct.at (scopeKey) ifAbsent {
def moduleName = (ast.withoutLeadingComponents (gct.at "path".first)).
replace ".grace" with ""
ProgrammingError.raise "gct for module \"{moduleName}\" does not contain \"{scopeKey}\""
}.do { gctLine →
addGctLine (gctLine) toScope (newScope) for (gct)
}
newScope
}
}
result
}
method setupContext(moduleNode) {
util.setPosition(0, 0)
dialectScope.clear // so that resolve can be serially re-used.
moduleScope.clear
varFieldDecls.clear
def dialectName:String = moduleNode.dialectName
if (dialectName ≠ "none") then {
xmodule.checkDialect(moduleNode)
processGct(xmodule.gctDictionaryFor(dialectName), dialectScope)
}
}
method checkTraitBody(traitObjNode) {
traitObjNode.value.do { node →
if (node.isLegalInTrait.not) then {
def badThing = node.statementName
def article = articleFor (badThing)
errormessages.syntaxError("{article} {badThing} cannot appear in " ++
"a trait (defined on line {traitObjNode.line})")
atLine(node.line)
}
}
}
method articleFor(str) {
// the indefinite article to preceed str
if ("aeioAEIO".contains(str.first)) then { "an" } else { "a" }
}
method buildSymbolTableFor(topNode) ancestors(topChain) {
def symbolTableVis = object {
inherit ast.baseVisitor
method visitAssignment (o) up (anc) {
o.scope := anc.parent.scope
def lValue = o.dest
if (lValue.isIdentifier) then {
lValue.isAssigned := true
}
return true
}
method visitRequest (o) up (anc) {
def enclosingNode = anc.parent
def scope = enclosingNode.scope
o.scope := scope
def callee = o.receiver
if (callee.isIdentifier) then {
callee.inRequest := true
}
o.parts.do { each → each.scope := scope }
if (enclosingNode.isMethod) then {
if (enclosingNode.body.last == o) then {
o.isTailCall := true
}
} elseif { enclosingNode.isReturn } then {
o.isTailCall := true
}
return true
}
method visitBlock (o) up (anc) {
o.scope := sm.blockScope.in(anc.parent.scope)
true
}
method visitDefDec (o) up (anc) {
def myParent = anc.parent
def s = myParent.scope
o.scope := s
o.parentKind := myParent.kind
def rhs = o.value
def nameString = o.nameString
if (rhs.isObject) then { rhs.name := nameString }
if (myParent.isObject) then {
if (o.isTyped) then {
s.methodTypes.at(nameString) put(readerSignature(o))
}
}
true
}
method visitVarDec (o) up (anc) {
def myParent = anc.parent
def s = myParent.scope
o.scope := s
o.parentKind := myParent.kind
if (myParent.isObject) then {
if (o.isTyped) then {
s.methodTypes.at (o.nameString) put(readerSignature(o))
s.methodTypes.at (o.writerNameString) put(writerSignature(o))
}
}
true
}
method visitUniversalDec (o) up (anc) {
def myParent = anc.parent
def s = myParent.scope // the scope of the method parameters
o.scope := s
o.parentKind := myParent.kind // methodDec or methodtype (for a signature)
true // hope that visitIdentifier creates the variable
}
method visitIdentifier (o) up (anc) {
var scope := anc.parent.scope
o.scope := scope
def isWild = o.wildcard
if (o.isBindingOccurrence) then {
if (o.isDeclaredByParent.not && isWild.not) then {
def declaringNode = o.declaringNodeWithAncestors(anc)
if (scope.isObjectScope && declaringNode.isVarDec) then {
scope.add(sm.varVariableFrom(declaringNode)) withName (o.writerNameString)
// scope.add will complain if o.writerNameString is already declared.
// We use a variableVar and not a variableMethod to
// distinguish this from a hand-written assignment method
}
scope.add(declaringNode.createVariableFor(o))
}
} elseif {isWild} then {
errormessages.syntaxError("'_' cannot be used in an expression")
atRange(o.range)
}
true
}
method visitAlias (o) up (ac) {
o.scope := ac.parent.scope
o.newSignature.isDeclaredByParent := true
o.newName.accept(self) from (ac.extend(o))
false // no need to visit the aliasNode's other components
}
method visitImport (o) up (anc) {
o.scope := anc.parent.scope
xmodule.checkExternalModule(o)
def gct = xmodule.gctDictionaryFor(o.moduleName)
def importedScope = sm.externalScope
importedScope.node := o
processGct(gct, importedScope)
o.scope.add(sm.importVariableFrom(o).attributeScope(importedScope))
withName(o.nameString)
o.name.isDeclaredByParent := true
// to prevent redeclaration when we visit the identifier
true
}
method visitInherit (o) up (anc) {
o.scope := anc.parent.scope
if (o.isUse) then {
if (anc.parent.canUse.not) then {
errormessages.syntaxError("use statements must " ++
"be inside an object, class, or trait")
atRange(o.range)
}
} else {
if (anc.parent.canInherit.not) then {
errormessages.syntaxError("inherit statements must " ++
"be inside an object or class; a trait cannot inherit")
atRange(o.range)
}
}
true
}
method visitMethodDec (o) up (anc) {
def surroundingScope = anc.parent.scope
if (surroundingScope.isObjectScope.not) then {
// The parser accepts method declarations as statments, and thus
// class and trait declarations as statements too.
// Here we check that they are inside an object.
// This produces better diagnostics than rejecting them in
// the parser, as well as simplifying the parser.
errormessages.syntaxError("{o.description} declarations are " ++
"permitted only inside an object") atRange(o.range)
}
def ident = o.asIdentifier
if (ident.isBindingOccurrence) then {
ident.isDeclaredByParent := true
// aliased and excluded names are appliedOccurrences
o.scope := sm.methodScope.in(surroundingScope)
surroundingScope.add(o.createVariableFor(ident))
if (o.isPublic) then {
if (o.isTyped) then {
surroundingScope.methodTypes.at(ident.nameString) put(methodSignature(o))
}
}
}
if (o.returnsObject) then {
o.returnedObject.name := o.canonicalName
}
true
}
method visitMethodSignature (o) up (anc) {
def surroundingScope = anc.parent.scope
if (o.isAppliedOccurrence) then { // on rhs of an alias, or in an exclude
o.scope := surroundingScope
return true
}
def ident = o.asIdentifier
if (ident.isDeclaredByParent.not) then {
surroundingScope.add(sm.methodVariableFrom (o))
ident.isDeclaredByParent := true
}
o.scope := sm.parameterScope.in(anc.parent.scope)
// the scope for the parameters (including the type parameters,
// if any) of this method.
true
}
method visitObject (o) up (anc) {
def myParent = anc.parent
o.scope := sm.objectScope.in(myParent.scope)
if (o.inTrait) then { checkTraitBody(o) }
true
}
method visitModule(o) up (anc) {
// the module scope was set before the traversal started
true
}
method visitTypeDec (o) up (anc) {
def enclosingScope = anc.parent.scope
if (enclosingScope.isObjectScope.not) then {
errormessages.syntaxError("type declarations are permitted only" ++
" inside an object") atRange(o.range)
}
def ident = o.name
enclosingScope.add(sm.typeVariableFrom(o))
enclosingScope.types.at(ident.nameString) put(o.toGrace 0)
ident.isDeclaredByParent := true
o.scope := sm.typeScope.in(enclosingScope)
// this scope will be the home for any type parameters.
// If there are no parameters, it won't be used.
true
}
method visitInterfaceLiteral (o) up (anc) {
o.scope := sm.interfaceScope.in(anc.parent.scope)
true
}
method visitReturn(o) up (anc) {
o.scope := anc.parent.scope;
def enclosingMethodNode = anc.suchThat { n → n.isMethod } ifAbsent {
errormessages.syntaxError "`return` statements must be inside methods."
atRange(o.range)
}
o.dtype := enclosingMethodNode.dtype
true
}
method visitYourself(o) up (anc) {
def currentScope = anc.parent.scope
o.scope := currentScope
def nodeObjects = list []
o.theObjects := nodeObjects
var currentObjectScope := currentScope.currentObjectScope
repeat (o.numberOfLevels) times {
nodeObjects.addLast(currentObjectScope.node)
currentObjectScope := currentObjectScope.enclosingObjectScope
}
true
}
method visitTypeParameters(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitIf(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitMatchCase(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitTryCatch(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitSignaturePart(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitSequence(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitRequestWithoutArgs(o) up (anc) {
visitRequest(o) up (anc)
}
method visitTypeApplication(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitString(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitNumeral(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitOp(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitDialect(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitCommentNode(o) up (anc) { o.scope := anc.parent.scope ; true }
method visitEllipsis(o) up (anc) { o.scope := anc.parent.scope ; true }
} // end of symbolTableVis
def currentObjectScopesVis = object {
// Puts the scope of returned objects into the symbol table and
// marks variables as being Fresh
// This traversal can't be completed in the buildSymbolTable visitor,
// because the visitation is top-down, and hence the scope of the
// body of a def or method won't have been allocated when the
// declaration is visited.
inherit ast.baseVisitor
method noEarlyReturn(methNode) {
def erv = earlyReturnVis
methNode.accept(erv)
erv.containsEarlyReturn.not
}
method visitMethodDec (o) up (anc) {
def myParent = anc.parent
def surroundingScope = myParent.scope
if (o.returnsObject) then {
if (anc.forebears.forebears.isEmpty.not) then {
// prefixes a dotted name to the name of the returned object
// --- but not if I'm at the module-level
def factoryName = myParent.name
def tailNode = o.returnedObject
if ((factoryName != "object") && (tailNode.isObject)) then {
tailNode.name := factoryName ++ "." ++ tailNode.name
}
}
if (o.isOnceMethod.not) then {
if (noEarlyReturn(o)) then {
o.isFresh := true
def methodVariable = surroundingScope.lookup(o.nameString)
methodVariable.isFresh := true
o.returnedObjectScope.isFresh := true // TODO — is this right?
}
}
}
true
}
}
def inheritanceVis = object {
inherit ast.baseVisitor
method visitObject (o) up (anc) {
collectReusedNames(o)
true
}
method visitModule (o) up (anc) {
collectReusedNames(o)
true
}
}
topNode.accept(symbolTableVis) from(topChain)
topNode.accept(currentObjectScopesVis) from(topChain)
topNode.accept(inheritanceVis) from(topChain)
}
class earlyReturnVis {
inherit ast.baseVisitor
var containsEarlyReturn is readable := false
method visitReturn(o) {
containsEarlyReturn := true
false // stop visitattion
}
method visitObject(o) {
false // stop visitation
}
}
method collectReusedNames(node) is confidential {
// node is an object or class; puts the names that it inherits and uses into
// its scope. In the process, checks for a cycle in the inheritance chain
def nodeScope = node.scope
if (nodeScope.areReusedNamesCompleted) then {
return
}
if (nodeScope.areReusedNamesInProgress) then {
errormessages.syntaxError "cyclic inheritance or trait use."
atRange(node.line, node.column, node.column + 4)
}
nodeScope.markReusedNamesAsInProgress
gatherInheritedNames(node)
gatherUsedNames(node)
nodeScope.markReusedNamesAsCompleted
}
method gatherInheritedNames(node) is confidential {
if (node.isObject) then {
var inhNode := node.superclass
def objScope = node.scope
var superScope
if (false == inhNode) then {
def gO = ast.identifier("graceObject", false).setScope (objScope)
inhNode := ast.inheritStatement(gO).setScope (objScope)
superScope := constantScope.objectScope
} else {
superScope := scopeReferencedByReuseExpr(inhNode.value)
inhNode.reusedScope := superScope
if (superScope.isExternal.not) then { collectReusedNames(superScope.node) }
}
def excludedNames = inhNode.exclusions.map { exMeth → exMeth.nameString } >> list
superScope.localAndReusedNamesAndValuesDo { name, defn →
if ((name ≠ "self") && (excludedNames.contains(name).not)) then {
objScope.addReused(defn) withName (name)
}
}
inhNode.aliases.do { a →
def old = a.oldName.nameString
def new = a.newName.nameString
def defn = superScope.lookupLocallyOrReused(old) ifAbsent {
errormessages.syntaxError("can't define an alias for " ++
a.oldName.canonicalName ++
" because it is not present in the inherited object")
atRange(a.oldName.range)
}
objScope.add (sm.aliasMethodVariableFrom(a) to (defn)) withName (new)
// An alias is added as a local method, not as a reused method
}
inhNode.exclusions.do { exMeth →
if (superScope.definesLocallyOrReuses(exMeth.nameString).not) then {
errormessages.syntaxError("can't exclude {exMeth.canonicalName} " ++
"because it is not present in the inherited object")
atRange(exMeth.range)
}
}
}
}
method gatherUsedNames(objNode) is confidential {
// For each of objNodes's used traits, gather the names
// introduced by that trait, as modified by alias and exclude.
def traitMethods = dictionary.empty
// maps method names to the trait(s) that provide(s) them - for detecting conflicts
def objScope = objNode.scope
objNode.usedTraits.do { t ->
def traitScope = scopeReferencedByReuseExpr(t.value)
util.log 46 verbose "gathering trait {t.value.toGrace 0} with {traitScope}"
t.reusedScope := traitScope
if (traitScope.isTrait.not) then {
errormessages.syntaxError("{t.value.toGrace 0} is not a trait," ++
" so it may not appear in a 'use' statement") atRange(t)
} // TODO: is this necessary? There is another check in transformReuse(_)ancestors(_)
if (traitScope.isExternal.not) then { collectReusedNames(traitScope.node) }
def excludedNames = t.exclusions.map { exMeth → exMeth.nameString } >> list
def requiredNames = list.empty
traitScope.localAndReusedNamesAndValuesDo { nm, defn →
if (defn.forUsers && excludedNames.contains(nm).not) then {
objScope.addReused(defn)
if (defn.isRequired) then {
requiredNames.add(nm)
} else {
def definingTraits = traitMethods.at(nm) ifAbsent { list [] }
definingTraits.add(t)
traitMethods.at(nm)put(definingTraits)
// TODO: Make definingtraits a multi-dictionary
}
}
}
t.aliases.do { a →
def old = a.oldName.nameString
def new = a.newName.nameString
traitScope.lookupLocallyOrReused(old) ifAbsent {
errormessages.reuseError("sorry, you can't define an alias for " ++
"{a.oldName.canonicalName} because it is not present in the trait")
atRange(a.oldName.range)
} ifPresent { defn →
try {
objScope.add (defn) withName (new)
// the spec says: Attributes introduced by an alias clause are
// treated as being introduced by the object under construction,
// and thus do not conflict with (and may therefore override)
// attributes obtained by reuse. They do conflict with attributes
// declared in the object under construction.
} catch {re:errormessages.NamingError →
def priorDecl = objScope.lookupLocally (new)
ifAbsent { ProgrammingError.raise "impossible" }
errormessages.reuseError(
"sorry, you can't declare '{canonicalName(new)}' " ++
"as an alias, because it's also declared as a " ++
"{priorDecl.kind} on {priorDecl.rangeLongString}, " ++
"which is in the same object; use a different name")
atRange (defn.definingParseNode)
}
}
}
t.exclusions.do { exMeth →
if (traitScope.definesLocallyOrReuses(exMeth.nameString).not) then {
errormessages.syntaxError("can't exclude {exMeth.canonicalName} " ++
"because it is not available in the used trait")
atRange(exMeth.range)
}
}
}
checkForConflicts(objNode, traitMethods)
}
method checkForConflicts(objNode, traitMethods) {
// traitMethods is a dictionary with methodNames as keys, and
// a list of sources as values. Multiple sources indicate a conflict,
// unless there is a local definition too.
def conflicts = list.empty
traitMethods.keysDo { methName →
def sources = traitMethods.at(methName)
if (sources.size > 1) then { // a method has more than one source trait
if (objNode.localNames.contains(methName).not) then {
conflicts.addLast (conflictForMethodName(methName) from(sources))
}
}
}
if (conflicts.isEmpty) then { return }
var maxSourceRange:Range := emptyRange
var message := if (conflicts.size > 1) then {
"trait conflicts found.\n "
} else {
"trait conflict found. "
}
conflicts.do { each →
def sourceList = each.sources.map { s → s.nameString }
maxSourceRange := each.sources.fold {a, s → max(a, s.range) }
startingWith(maxSourceRange)
message := message ++ "Method `{each.canonicalName}` is defined in " ++
errormessages.readableStringFrom(sourceList) ++ ".\n "
}
if (maxSourceRange == emptyRange) then {
errormessages.reuseError(message)
} else {
errormessages.reuseError(message) atRange (maxSourceRange)
}
}
class conflictForMethodName(nm) from(srcs) {
def methodName is public = nm
def sources is public = srcs
method canonicalName { canonicalName(methodName) }
}
method scopeReferencedByReuseExpr(nd) {
// answers the scope referenced by astNode nd, which is the
// reuse expresion in an inherit or use clause.
// This is a tricky case: nd cannot reference anything
// in the current object, because that object does not yet exist.
// (The exception is when the current object is a module,
// and nd is an expression starting with the nickname of an import.)
// However, the meaning of self and outer^n depend on the
// lexical position of the reuse expression.
// Note also that, because this method is requested from the
// inheritanceVisitor, nd has not yet been disambiguated.
// If nd references an object, then the result
// scope will have bindings for the methods of that object.
// Otherwise, we raise an error.
def scp = nd.scope
if (nd.isIdentifier) then {
def sought = nd.nameString
if (sought == "outer") then {
return scp.outerScope.enclosingObjectScope
}
def variable = scp.lookupLocally (sought) ifAbsent {
ensureOuterScopesCollected(scp)
return scp.outerScope.lookup (sought) ifAbsent {
errormessages.undeclaredIdentifier(nd)
}.attributeScope
}
if (variable.isImport) then { return variable.attributeScope }
errormessages.syntaxError "a reuse expression cannot refer to an attribute of 'self'"
atRange (nd.range)
} elseif { nd.isOuter } then {
nd.theObjects.last.scope
} elseif {nd.kind == "op"} then {
def receiverScope = scopeReferencedByReuseExpr(nd.left)
receiverScope.lookup (nd.nameString) ifAbsent {
errormessages.syntaxError "no operator {nd.canonicalName} on {nd.left.toGrace 0}"
atRange (nd.range)
}.attributeScope
} elseif {nd.isCall} then { // this includes "memberNodes"
if (nd.receiver.isImplicit) then {
def defs = sm.variableResolver.definitionsOf (nd.nameString) visibleIn (scp.outerScope)
if (defs.isEmpty) then {
errormessages.undeclaredIdentifier(nd)
} elseif { defs.size > 1 } then {
errormessages.ambiguityError (defs) node (nd)
}
defs.first.definition.attributeScope
} else {
scopeReferencedByReuseExpr(nd.receiver)
.attributeScopeOf(nd.nameString) in (nd)
}
} elseif { nd.isObject } then {
// inheriting from a literal object expression — weird, but legal
nd.scope
} else {
errormessages.reuseError ("you can't reuse {nd.pretty 0}; " ++
"it does not return a fresh object") atRange (nd)
}
}
method ensureOuterScopesCollected(s) {
// look at all the scopes surrounding s, and make sure that their
// reused names have been collected.
var scp := s.outerScope.currentObjectScope
while {scp.isDialectScope.not} do {
collectReusedNames(scp.node)
scp := scp.outerScope.currentObjectScope
}
}
method reusedScope (aReuseStatement) {
// answers the scope referenced by the super expression in aReuseStatement
def expr = aReuseStatement.reuseExpr
if (expr.receiver.isSelf) then {
def reuseKind = aReuseStatement.statementName
errormessages.reuseError(
"sorry, it's not possible to {reuseKind} 'self', because 'self' " ++
"does not yet exist when the {reuseKind} statement is executed"
) atRange (aReuseStatement.reuseExpr.range)
}
expr.currentObjectScopeFor (aReuseStatement)
}
method transformBind(bindNode) ancestors(anc) {
// bindNode is (a shallow copy of) a bindNode. If it is binding a
// "member" or field of an object, transform it into a request on a setter
def lhs = bindNode.lhs
def nm = lhs.nameString