-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSymbolTable.java
More file actions
1197 lines (1058 loc) · 36.1 KB
/
SymbolTable.java
File metadata and controls
1197 lines (1058 loc) · 36.1 KB
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
/*
* xtc - The eXTensible Compiler
* Copyright (C) 2005-2010 Robert Grimm
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package qimpp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import qimpp.Constants;
import qimpp.Utilities;
import xtc.tree.GNode;
import xtc.tree.Node;
import xtc.tree.Printer;
import xtc.tree.Visitor;
import xtc.util.EmptyIterator;
/**
* A symbol table. This class implements a symbol table, which maps
* symbols represented as strings to values of any type. The mapping
* is organized into hierarchical {@link Scope scopes}, which allows
* for multiple definitions of the same symbol across different
* scopes. Additionally, a symbol may have multiple definitions
* within the same scope: if the corresponding value is a Java
* collections framework list, it is recognized as a multiply defined
* symbol. Scopes are named, with names being represented as strings.
* Both scope names and symbols can be unqualified — that is,
* they need to be resolved relative to the {@link #current() current
* scope} — or qualified by the {@link Constants#QUALIFIER
* qualification character} "<code>::</code>" — that is, they are
* resolved relative to the symbol table's {@link #root() root}. Once
* {@link #enter(String) created}, a scope remains in the symbol table
* and the corresponding AST node should be associated with that scope
* by setting the corresponding {@link Constants#SCOPE property} to
* the scope's qualified name. Subsequent traversals over that node
* can then automatically {@link #enter(Node) enter} and {@link
* #exit(Node) exit} that scope. Alternatively, if traversing out of
* tree order, the current scope can be set {@link
* #setScope(SymbolTable.Scope) explicitly}.
*
* <p />To support different name spaces within the same scope, this
* class can optionally {@link #toNameSpace mangle} and {@link
* #fromNameSpace unmangle} unqualified symbols. By convention, a
* name in any name space besides the default name space is prefixed
* by the name of the name space and an opening parenthesis
* '<code>(</code>' and suffixed by a closing parenthesis
* '<code>)</code>'.
*
* @author Robert Grimm, adapted by Qimpp
* @version N/A
*/
public class SymbolTable {
/**
* A symbol table scope. A scope has a name and may have a parent
* (unless it is the root scope), one or more nested scopes, and one
* or more definitions.
*/
public static class Scope {
/** The name. */
String name;
/** The fully qualified name. */
String qName;
/** The parent scope. */
Scope parent;
/** The nested scopes, if any. */
Map<String, Scope> scopes;
/** The map from symbols to values, if any. */
Map<String, Object> symbols;
/** The node where the scope is declared (to get Type and other info ) */
Node node;
/**
* Create a new root scope with the specified name, which may be
* the empty string.
*
* @param name The name.
*/
Scope(String name) {
this.name = name;
this.qName = name;
// the root scope has no declaration
this.node = null;
}
/**
* Create a new nested scope with the specified unqualified name
* and parent.
*
* @param name The unqualified name.
* @param parent The parent.
* @param node the AST Node where this scope starts / is declared
* @throws IllegalArgumentException
* Signals that the specified parent already has a nested scope
* with the specified name.
*/
Scope(String name, Scope parent, Node node) {
if ((null != parent.scopes) && parent.scopes.containsKey(name)) {
throw new IllegalArgumentException("Scope " + parent.qName +
" already contains scope " + name);
}
this.name = name;
this.qName = Utilities.qualify(parent.qName, name);
this.parent = parent;
this.node = node;
if (null == parent.scopes) {
parent.scopes = new HashMap<String, Scope>();
}
parent.scopes.put(name, this);
}
/**
* Get this scope's unqualfied name.
*
* @return This scope's unqualified name.
*/
public String getName() {
return name;
}
/**
* Get this scope's qualified name.
*
* @return This scope's qualified name.
*/
public String getQualifiedName() {
return qName;
}
/**
* Update this scope's qualified name relative to the parent
* scope's qualified name. This method also requalifies any
* nested scopes' qualified names. It must not be called on the
* root scope.
*/
void requalify() {
qName = Utilities.qualify(parent.qName, name);
if (null != scopes) {
for (Scope scope : scopes.values()) {
scope.requalify();
}
}
}
/**
* Determine whether this scope is the root scope.
*
* @return <code>true</code> if this scope is the root scope.
*/
public boolean isRoot() {
return (null == parent);
}
/**
* Get this scope's parent.
*
* @return This scope's parent scope or <code>null</code> if this
* scope does not have a parent (i.e., is the root scope).
*/
public Scope getParent() {
return parent;
}
/**
* Determine whether this scope has any nested scopes.
*
* @return <code>true</code> if this scope has any nested scopes.
*/
public boolean hasNested() {
return ((null != scopes) && (0 < scopes.size()));
}
/**
* Get an iterator over the names of all nested scopes.
*
* @return An iterator over the nested scopes.
*/
public Iterator<String> nested() {
if (null == scopes) {
return EmptyIterator.value();
} else {
return scopes.keySet().iterator();
}
}
/**
* Determine whether this scope has the specified unqualified
* nested scope.
*
* @param name The nested scope's unqualified name.
* @return <code>true</code> if the corresponding scope exists.
*/
public boolean hasNested(String name) {
return (null != getNested(name));
}
/**
* Get the nested scope with the specified unqualified name.
*
* @param name The nested scope's unqualified name.
* @return The corresponding scope or <code>null</code> if there is
* no such scope.
*/
public Scope getNested(String name) {
return (null == scopes)? null : scopes.get(name);
}
/**
* Determine whether the scope with the specified unqualified name
* can be merged into this scope. A nested scope can be merged if
* it (1) does not contain any bindings with the same names as
* this scope's bindings and (2) does not have any children with
* the same names as this scope's children.
*
* @param name The nested scope's unqualified name.
* @return <code>true</code> if the scope can be merged.
* @throws IllegalArgumentException Signals that this scope does
* not have a nested scope with the specified name.
*/
public boolean isMergeable(String name) {
Scope nested = getNested(name);
if (null == nested) {
throw new IllegalArgumentException("Scope " + qName + " does not " +
" contain scope " + name);
}
if (null != nested.scopes) {
// Note that this scope must have nested scopes, since we just
// looked one up.
for (String s : nested.scopes.keySet()) {
if ((! s.equals(name)) && this.scopes.containsKey(s)) {
return false;
}
}
}
if ((null != this.symbols) && (null != nested.symbols)) {
for (String s : nested.symbols.keySet()) {
if (this.symbols.containsKey(s)) {
return false;
}
}
}
return true;
}
/**
* Merge the nested scope with the specified unqualified name into
* this scope.
*
* @param name The nested scope's unqualified name.
* @throws IllegalArgumentException Signals that (1) this scope
* does not have a nested scope with the specified name, (2) any
* of the nested scope's children has the same name as one of
* this scope's children, or (3) any of the nested scope's
* bindings has the same name as one of this scope's bindings.
*/
public void merge(String name) {
final Scope nested = getNested(name);
// Make sure the nested scope is mergeable. Note that the
// nested scope must exist in the consequence of the
// if-statement, since isMergeable signals an exception for
// non-existent scopes.
if (! isMergeable(name)) {
throw new IllegalArgumentException("Scope " + nested.qName +
" cannot be merged into the parent");
}
// Remove the nested scope.
this.scopes.remove(name);
// Add the nested scope's children.
if (null != nested.scopes) {
this.scopes.putAll(nested.scopes);
for (Scope s : nested.scopes.values()) {
s.parent = this;
s.requalify();
}
}
// Add the nested scope's bindings.
if (null != nested.symbols) {
if (null == this.symbols) {
this.symbols = nested.symbols;
} else {
this.symbols.putAll(nested.symbols);
}
}
// Invalidate the nested scope.
nested.parent = null;
nested.name = null;
nested.qName = null;
nested.scopes = null;
nested.symbols = null;
}
/**
* Determine whether this scope has any local definitions.
*
* @return <code>true</code> if this scope has any local
* definitions.
*/
public boolean hasSymbols() {
return ((null != symbols) && (0 < symbols.size()));
}
/**
* Get an iterator over the all locally defined symbols.
*
* @return An iterator over the locally defined symbols.
*/
public Iterator<String> symbols() {
if (null == symbols) {
return EmptyIterator.value();
} else {
return symbols.keySet().iterator();
}
}
/**
* Determine whether the specified symbol is defined in this
* scope.
*
* @param symbol The unqualified symbol.
* @return <code>true</code> if the symbol is defined in this
* scope.
*/
public boolean isDefinedLocally(String symbol) {
return (null == symbols)? false : symbols.containsKey(symbol);
}
/**
* Determine whether the specified unqualified symbol is defined
* in this scope or any of its ancestors.
*
* @param symbol The unqualified symbol.
* @return <code>true</code> if the symbol is defined in this scope
* or any of its ancestors.
*/
public boolean isDefined(String symbol) {
return (null != lookupScope(symbol));
}
/**
* Determine whether the specified symbol is defined multiple
* times in this scope or any of its ancestors.
*
* @param symbol The unqualified symbol.
* @return <code>true</code> if the symbol is defined multiple
* times.
*/
public boolean isDefinedMultiply(String symbol) {
Scope scope = lookupScope(symbol);
return (null == scope)? false : scope.symbols.get(symbol) instanceof List;
}
/**
* Get the scope defining the specified unqualified symbol. This
* method searches this scope and all its ancestors, returning the
* first defining scope.
*
* @param symbol The unqualified symbol.
* @return The definining scope or <code>null</code> if there is
* no such scope.
*/
public Scope lookupScope(String symbol) {
Scope scope = this;
do {
if ((null != scope.symbols) && (scope.symbols.containsKey(symbol))) {
return scope;
}
scope = scope.parent;
} while (null != scope);
return null;
}
/**
* Get the value for the specified unqualified symbol. This
* method searches this scope and all its ancestors, returning the
* value of the first definition.
*
* @param symbol The unqualified symbol.
* @return The corresponding value or <code>null</code> if there is
* no definition.
*/
public Object lookup(String symbol) {
Scope scope = lookupScope(symbol);
return (null == scope)? null : scope.symbols.get(symbol);
}
/**
* Get the scope named by the specified unqualified symbol, which
* is nested in the scope defining the symbol. This method
* searches this scope and all its ancestors, up to the first
* defining scope. It then looks for the nested scope with the
* same name.
*
* @param symbol The unqualified symbol.
* @return The bound scope or <code>null</code> if there is no
* definition or nested scope with the same name.
*/
public Scope lookupBoundScope(String symbol) {
Scope scope = lookupScope(symbol);
return (null == scope)? null : scope.getNested(symbol);
}
/**
* Get the local value for the specified unqualified symbol.
*
* @param symbol The unqualified symbol.
* @return The corresponding value or <code>null</code> if there is
* no local definition.
*/
public Object lookupLocally(String symbol) {
return (null == symbols)? null : symbols.get(symbol);
}
/**
* Set the specified symbol's value to the specified value in this
* scope.
*
* @param symbol The unqualified symbol.
* @param value The value.
*/
public void define(String symbol, Object value) {
if (null == symbols) {
symbols = new HashMap<String, Object>();
}
symbols.put(symbol, value);
}
/**
* Add the specified value to the specified symbol's values in
* this scope.
*
* @param symbol The unqualified symbol.
* @param value The value.
*/
@SuppressWarnings("unchecked")
public void addDefinition(String symbol, Object value) {
if (null == symbols) {
symbols = new HashMap<String, Object>();
}
if (symbols.containsKey(symbol)) {
Object o = symbols.get(symbol);
if (o instanceof List) {
((List<Object>)o).add(value);
} else {
List<Object> l = new ArrayList<Object>();
l.add(o);
l.add(value);
symbols.put(symbol, l);
}
} else {
symbols.put(symbol, value);
}
}
/**
* Undefine the specified unqualified symbol. If the symbol is
* defined in this scope, this method removes all its values.
*
* @param symbol The unqualified symbol.
*/
public void undefine(String symbol) {
if (null != symbols) {
symbols.remove(symbol);
}
}
/**
* Qualify the specified unqualified symbol with this scope's
* name.
*
* @param symbol The unqualified symbol.
* @return The qualified symbol.
*/
public String qualify(String symbol) {
return Utilities.qualify(qName, symbol);
}
public Node node() {
return this.node;
}
public void node(Node node) {
this.node = node;
}
/**
* Dump the contents of this scope. This method pretty prints the
* contents of this scope and all nested scopes with the specified
* printer. If the printer is registered with a visitor, that
* visitor is used for formatting any node values.
*
* @param printer The printer, which need not be registered with a
* visitor.
*/
public void dump(Printer printer) {
boolean hasVisitor = (null != printer.visitor());
printer.indent().p("::").p(name).pln(" = {").incr();
if (null != symbols) {
List<String> keys = new ArrayList<String>(symbols.keySet());
Collections.sort(keys);
for (String symbol : keys) {
Object value = symbols.get(symbol);
printer.indent().p(symbol).p(" = ");
if (null == value) {
printer.p("null");
} else if (hasVisitor && (value instanceof Node)) {
printer.p((Node)value);
} else if (value instanceof String) {
printer.p('"').escape((String)value, Utilities.JAVA_ESCAPES).p('"');
} else {
try {
printer.p(value.toString());
} catch (final Exception e) {
printer.p(value.getClass().getName() + "@?");
}
}
printer.pln(';');
Scope nested = getNested(symbol);
if (null != nested) {
nested.dump(printer);
}
}
}
if (null != scopes) {
List<String> keys = new ArrayList<String>(scopes.keySet());
Collections.sort(keys);
for (String name : keys) {
if ((null == symbols) || (! symbols.containsKey(name))) {
scopes.get(name).dump(printer);
}
}
}
printer.decr().indent().pln("};");
}
}
// =========================================================================
/** The root scope. */
protected Scope root;
/** The current scope. */
protected Scope current;
/** The fresh name count. */
protected int freshNameCount;
/** The fresh identifier count. */
protected int freshIdCount;
// =========================================================================
/**
* Create a new symbol table with the empty string as the root
* scope's name.
*/
public SymbolTable() {
this("");
}
/**
* Create a new symbol table.
*
* @param root The name of the root scope.
*/
public SymbolTable(String root) {
this.root = new Scope(root);
current = this.root;
freshNameCount = 0;
freshIdCount = 0;
}
// =========================================================================
/**
* Clear this symbol table. This method deletes all scopes and
* their definitions from this symbol table.
*/
public void reset() {
root.scopes = null;
root.symbols = null;
current = root;
freshNameCount = 0;
freshIdCount = 0;
}
/**
* Get the root scope.
*
* @return The root scope.
*/
public Scope root() {
return root;
}
/**
* Get the current scope.
*
* @return The current scope.
*/
public Scope current() {
return current;
}
/**
* Get the scope with the specified qualified name.
*
* @param name The qualified name.
* @return The corresponding scope or <code>null</code> if no such
* scope exits.
*/
public Scope getScope(String name) {
// Optimize for the common case where the specified name denotes a
// scope directly nested in the current scope.
Scope scope = current;
if (name.startsWith(scope.qName) &&
(name.lastIndexOf(Constants.QUALIFIER) == scope.qName.length()-1)) { // TODO: Vivek changed this to -1 to reflect :: versus .
return scope.getNested(Utilities.getName(name));
}
String[] components = Utilities.toComponents(name);
scope = root.name.equals(components[0])? root : null;
int index = 1;
while ((null != scope) && (index < components.length)) {
scope = scope.getNested(components[index]);
index++;
}
return scope;
}
/**
* Set the current scope to the specified scope.
*
* @param scope The new current scope.
* @throws IllegalArgumentException Signals that this symbol table's
* root is not the specified scope's root.
*/
public void setScope(Scope scope) {
// Check the specified scope.
Scope s = scope;
while (null != s.parent) s = s.parent;
if (s != root) {
throw new IllegalArgumentException("Scope " + scope.qName + " not " +
"in this symbol table " + this);
}
// Make the scope the current scope.
current = scope;
}
/**
* Determine whether the specified symbol is defined. If the symbol
* is qualified, this method checks whether the symbol is defined in
* the named scope. Otherwise, it checks whether the symbol is
* defined in the current scope or one of its ancestors.
*
* @param symbol The symbol.
* @return <code>true</code> if the specified symbol is defined.
*/
public boolean isDefined(String symbol) {
Scope scope = lookupScope(symbol);
if ((null == scope) || (null == scope.symbols)) {
return false;
} else {
return scope.symbols.containsKey(Utilities.unqualify(symbol));
}
}
/**
* Determine whether the specified symbol is define multiple times.
* If the symbol is qualified, this method checks whether the symbol
* has multiple definitions in the named scope. Otherwise, it
* checks whether the symbol has multiple definitions in the current
* scope or one of its ancestors.
*
* @param symbol The symbol.
* @return <code>true</code> if the specified symbol is multiply
* defined.
*/
public boolean isDefinedMultiply(String symbol) {
Scope scope = lookupScope(symbol);
if ((null == scope) || (null == scope.symbols)) {
return false;
} else {
return scope.symbols.get(Utilities.unqualify(symbol)) instanceof List;
}
}
/**
* Get the scope for the specified symbol. If the symbol is
* qualified, this method returns the named scope (without checking
* whether the symbol is defined in that scope). Otherwise, it
* searches the current scope and all its ancestors, returning the
* first defining scope.
*
* @param symbol The symbol.
* @return The corresponding scope or <code>null</code> if no such
* scope exits.
*/
public Scope lookupScope(String symbol) {
if (Utilities.isQualified(symbol)) {
return getScope(Utilities.getQualifier(symbol));
} else {
return current.lookupScope(symbol);
}
}
/**
* Get the value for the specified symbol. If the symbol is
* qualified, this method returns the definition within the named
* scope. Otherwise, it searches the current scope and all its
* ancestors, returning the value of the first definition.
*
* @param symbol The symbol.
* @return The corresponding value or <code>null</code> if no such
* definition exists.
*/
public Object lookup(String symbol) {
Scope scope = lookupScope(symbol);
if ((null == scope) || (null == scope.symbols)) {
return null;
} else {
return scope.symbols.get(Utilities.unqualify(symbol));
}
}
/**
* Enter the scope with the specified unqualified name. If the
* current scope does not have a scope with the specified name, a
* new scope with the specified name is created. In either case,
* the scope with that name becomes the current scope.
*
* @param name The unqualified name.
* @param node The node where the scope begins
*/
public void enter(String name, Node node) {
Scope parent = current;
Scope child = parent.getNested(name);
if (null == child) {
child = new Scope(name, parent, node);
}
current = child;
}
/**
* Exit the current scope.
*
* @throws IllegalStateException
* Signals that the current scope is the root scope.
*/
public void exit() {
if (null == current.parent) {
throw new IllegalStateException("Unable to exit root scope");
}
current = current.parent;
}
/**
* Delete the scope with the specified unqualified name. If the
* current scope contains a nested scope with the specified name,
* this method deletes that scope and <em>all its contents</em>,
* including nested scopes.
*
* @param name The unqualified name.
*/
public void delete(String name) {
if (null != current.scopes) {
current.scopes.remove(name);
}
}
/**
* Determine whether the specified node has an associated {@link
* Constants#SCOPE scope}.
*
* @param n The node.
* @return <code>true</code> if the node has an associated scope.
*/
public static boolean hasScope(Node n) {
return n.hasProperty(Constants.SCOPE);
}
/**
* Mark the specified node. If the node does not have an associated
* {@link Constants#SCOPE scope}, this method set the property with
* the current scope.
*
* @param n The node.
*/
public void mark(Node n) {
if (! n.hasProperty(Constants.SCOPE)) {
n.setProperty(Constants.SCOPE, current);
}
}
/**
* Enter the specified node. If the node has an associated {@link
* Constants#SCOPE scope}, this method tries to enter the scope.
* Otherwise, it does not change the scope.
*
* @param n The node.
* @throws IllegalStateException Signals that the node's scope is
* invalid or not nested within the current scope.
*/
public void enter(Node n) {
if (n.hasProperty(Constants.SCOPE)) {
String name = n.getStringProperty(Constants.SCOPE);
Scope scope = getScope(name);
if (null == scope) {
throw new IllegalStateException("Invalid scope " + name);
} else if (scope.getParent() != current) {
throw new IllegalStateException("Scope " + name + " not nested in " +
current.getQualifiedName());
}
current = scope;
}
}
/**
* Exit the specified node. If the node has an associated {@link
* Constants#SCOPE scope}, the current scope is exited.
*
* @param n The node.
*/
public void exit(Node n) {
if (n.hasProperty(Constants.SCOPE)) {
exit();
}
}
/**
* Create a fresh name. The returned name has
* "<code>anonymous</code>" as it base name.
*
* @see #freshName(String)
*
* @return A fresh name.
*/
public String freshName() {
return freshName("anonymous");
}
/**
* Create a fresh name incorporating the specified base name. The
* returned name is of the form
* <code><i>name</i>(<i>count</i>)</code>.
*
* @param base The base name.
* @return The corresponding fresh name.
*/
public String freshName(String base) {
StringBuilder buf = new StringBuilder();
buf.append(base);
buf.append(Constants.START_OPAQUE);
buf.append(freshNameCount++);
buf.append(Constants.END_OPAQUE);
return buf.toString();
}
/**
* Create a fresh C identifier. The returned identifier has
* "<code>tmp</code>" as its base name.
*
* @see #freshCId(String)
*
* @return A fresh C identifier.
*/
public String freshCId() {
return freshCId("tmp");
}
/**
* Create a fresh C identifier incorporating the specified base
* name. The returned name is of the form
* <code>__<i>name</i>_<i>count</i></code>.
*
* @param base The base name.
* @return The corresponding fresh C identifier.
*/
public String freshCId(String base) {
StringBuilder buf = new StringBuilder();
buf.append("__");
buf.append(base);
buf.append('_');
buf.append(freshIdCount++);
return buf.toString();
}
/** The end of opaqueness marker as a string. */
private static final String END_OPAQUE =
Character.toString(Constants.END_OPAQUE);
// ===================================================================
/**
* Incorporate a Node tree into the instance. This method looks for
* identifiers, user-defined names contained in QualifiedIdentifier,
* PrimaryIdentifier, etc.
*
* @param node a tree to incorporate into the SymbolTable
*/
public void incorporate(Node node) {
final SymbolTable table = this;
new Visitor() {
// TODO: Inheritance.
//
// What I can think of:
// 1. runtime should be visible anywhere within the body since it gets
// gets inherited from Tool.
// 2. classes from the import should be visible from anywhere in
// the class.
// 3. Keep track of class versus stack scope.
// State variables
boolean inMethod = false;
// root of static scope tree
public void visitCompilationUnit(GNode n) {
visit(n);
}
public void visitClassDeclaration(GNode n) {
table.enter(n.getString(0), n);
table.mark(n);
visit(n.getGeneric(4));
visit(n.getGeneric(1));
table.exit();
}
// TODO: Handle interfaces?
public void visitInterfaceDeclaration(GNode n) {
table.enter(table.freshCId(n.getName()), n);
table.mark(n);
visit(n.getNode(4));
table.exit();
}
public void visitConstructorDeclaration(GNode n) {
table.enter(table.freshCId("constructor"), n);
table.mark(n);
visit(n.getNode(0)); // block
visit(n.getNode(1)); // parameters