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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
(* P1SymBuild.mod pass 1 symbol creation.
Copyright (C) 2001-2025 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Modula-2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Modula-2; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE P1SymBuild ;
FROM ASCII IMPORT nul ;
FROM NameKey IMPORT Name, WriteKey, MakeKey, KeyToCharStar, NulName ;
FROM M2Debug IMPORT Assert, WriteDebug ;
FROM M2LexBuf IMPORT GetFileName, GetTokenNo, UnknownTokenNo ;
FROM M2MetaError IMPORT MetaErrorString2, MetaError0, MetaError1,
MetaError2, MetaErrorT0, MetaErrorT1, MetaErrorT2 ;
FROM DynamicStrings IMPORT String, Slice, InitString, KillString, EqualCharStar, RIndex, Mark, ConCat ;
FROM M2Printf IMPORT printf0, printf1, printf2 ;
FROM M2Options IMPORT Iso, GetEnableForward ;
FROM M2Reserved IMPORT ImportTok, ExportTok, QualifiedTok, UnQualifiedTok,
NulTok, VarTok, ArrayTok, BuiltinTok, InlineTok ;
FROM FifoQueue IMPORT PutEnumerationIntoFifoQueue ;
FROM P0SymBuild IMPORT EnterBlock, LeaveBlock ;
FROM SymbolTable IMPORT NulSym,
ModeOfAddr,
AppendModuleOnImportStatement,
AppendModuleImportStatement,
MakeImportStatement, MakeImport,
StartScope, EndScope, PseudoScope,
GetScope, GetCurrentScope,
IsDeclaredIn,
SetCurrentModule, SetFileModule,
MakeInnerModule,
MakeEnumeration, MakeSubrange,
MakeVar, MakeType, PutType,
MakeHiddenType,
PutMode,
PutFieldEnumeration, PutSubrange, PutVar,
IsDefImp, IsModule, IsInnerModule, IsType,
GetCurrentModule,
AddSymToModuleScope,
AddNameToImportList,
GetSym, RequestSym, IsUnknown, RenameSym,
GetFromOuterModule,
GetExported, IsExported,
GetLocalSym,
PutImported, PutIncludedByDefinition,
PutExported, PutExportQualified, PutExportUnQualified,
TryMoveUndeclaredSymToInnerModule,
PutDefinitionForC,
IsDefinitionForC,
PutDoesNeedExportList, PutDoesNotNeedExportList,
DoesNotNeedExportList,
MakeProcedure,
IsProcedure, IsConstString,
MakePointer, PutPointer,
MakeRecord, PutFieldRecord,
MakeArray,
MakeSubscript, PutSubscript,
PutArray, GetType, IsArray,
IsProcType, MakeProcType,
PutProcedureBuiltin, PutProcedureInline,
GetSymName,
ResolveImports, PutDeclared,
ProcedureKind,
PutProcedureDeclaredTok, GetProcedureDeclaredTok,
PutProcedureDefined, GetProcedureDefined,
MakeError, MakeErrorS,
DisplayTrees ;
FROM M2Batch IMPORT MakeDefinitionSource,
MakeImplementationSource,
MakeProgramSource,
LookupModule, LookupOuterModule ;
FROM M2Quads IMPORT PushT, PopT, PushTF, PopTF, OperandT, PopN, OperandTok,
PopTtok, PushTtok, PushTFtok, PopTFtok ;
FROM M2Comp IMPORT CompilingDefinitionModule,
CompilingImplementationModule,
CompilingProgramModule ;
CONST
Debugging = FALSE ;
VAR
importStatementCount: CARDINAL ;
(*
StartBuildDefinitionModule - Creates a definition module and starts
a new scope.
he Stack is expected:
Entry Exit
Ptr ->
+------------+
| NameStart | <- Ptr
|------------| +------------+
| NulName/"C"| | NameStart |
|------------| |------------|
*)
PROCEDURE P1StartBuildDefinitionModule ;
VAR
name : Name ;
language,
ModuleSym: CARDINAL ;
BEGIN
importStatementCount := 0 ;
PopT(name) ;
(* CheckFileName(name, 'definition') ; *)
ModuleSym := MakeDefinitionSource(GetTokenNo(), name) ;
PutDoesNotNeedExportList(ModuleSym) ;
SetCurrentModule(ModuleSym) ;
SetFileModule(ModuleSym) ;
StartScope(ModuleSym) ;
Assert(IsDefImp(ModuleSym)) ;
Assert(CompilingDefinitionModule()) ;
PopT(language) ;
IF (language#NulSym) AND IsConstString(language)
THEN
IF GetSymName(language)=MakeKey('C')
THEN
PutDefinitionForC(ModuleSym)
ELSIF GetSymName(language)=NulName
THEN
MetaError0 ('{%E}currently a non modula-2 definition module can only be declared as DEFINITION FOR {%k"C"}')
ELSE
MetaError1 ('unknown definition module language {%1Ea}, currently a non modula-2 definition module can only be declared as DEFINITION FOR {%k"C"}', language)
END
END ;
PushT(name) ;
EnterBlock(name)
END P1StartBuildDefinitionModule ;
(*
EndBuildDefinitionModule - Destroys the definition module scope and
checks for correct name.
The Stack is expected:
Entry Exit
Ptr ->
+------------+ +-----------+
| NameEnd | | |
|------------| |-----------|
| NameStart | | | <- Ptr
|------------| |-----------|
*)
PROCEDURE P1EndBuildDefinitionModule ;
VAR
start : CARDINAL ;
NameStart,
NameEnd : Name ;
BEGIN
Assert(CompilingDefinitionModule()) ;
EndScope ;
PopTtok(NameStart, start) ;
PopT(NameEnd) ;
IF Debugging
THEN
printf0('pass 1: ') ;
DisplayTrees(GetCurrentModule())
END ;
IF NameStart#NameEnd
THEN
MetaError1 ('inconsistent definition module name {%1Wa}', MakeError (start, NameStart))
END ;
LeaveBlock
END P1EndBuildDefinitionModule ;
(*
StartBuildImplementationModule - Creates an implementation module and starts
a new scope.
The Stack is expected:
Entry Exit
Ptr -> <- Ptr
+------------+ +-----------+
| NameStart | | NameStart |
|------------| |-----------|
*)
PROCEDURE P1StartBuildImplementationModule ;
VAR
tok : CARDINAL ;
name : Name ;
ModuleSym: CARDINAL ;
BEGIN
importStatementCount := 0 ;
PopTtok (name, tok) ;
(* CheckFileName(name, 'implementation') ; *)
ModuleSym := MakeImplementationSource (tok, name) ;
SetCurrentModule (ModuleSym) ;
SetFileModule (ModuleSym) ;
StartScope (ModuleSym) ;
IF NOT IsDefImp (ModuleSym)
THEN
MetaError1 ('cannot find corresponding definition module for {%1Ea}', ModuleSym)
END ;
Assert (CompilingImplementationModule()) ;
PushTtok (name, tok) ;
EnterBlock (name)
END P1StartBuildImplementationModule ;
(*
EndBuildImplementationModule - Destroys the implementation module scope and
checks for correct name.
The Stack is expected:
Entry Exit
Ptr ->
+------------+ +-----------+
| NameEnd | | |
|------------| |-----------|
| NameStart | | | <- Ptr
|------------| |-----------|
*)
PROCEDURE P1EndBuildImplementationModule ;
VAR
start, end: CARDINAL ;
NameStart,
NameEnd : Name ;
BEGIN
ResolveImports ;
Assert(CompilingImplementationModule()) ;
EndScope ;
PopTtok(NameStart, start) ;
PopTtok(NameEnd, end) ;
IF NameStart#NameEnd
THEN
MetaErrorT1 (end,
'inconsistent implementation module name {%1Wa}', MakeError (start, NameStart))
END ;
LeaveBlock
END P1EndBuildImplementationModule ;
(*
StartBuildProgramModule - Creates a program module and starts
a new scope.
The Stack is expected:
Entry Exit
Ptr -> <- Ptr
+------------+ +-----------+
| NameStart | | NameStart |
|------------| |-----------|
*)
PROCEDURE P1StartBuildProgramModule ;
VAR
tok : CARDINAL ;
name : Name ;
ModuleSym: CARDINAL ;
BEGIN
importStatementCount := 0 ;
PopTtok(name, tok) ;
(* CheckFileName(name, 'main') ; *)
ModuleSym := MakeProgramSource(tok, name) ;
SetCurrentModule(ModuleSym) ;
SetFileModule(ModuleSym) ;
StartScope(ModuleSym) ;
IF (NOT CompilingProgramModule()) OR IsDefImp(ModuleSym)
THEN
MetaErrorT1 (tok,
'module {%1Ea} has a corresponding DEFINITION MODULE but no IMPLEMENTATION keyword in the main module', ModuleSym)
END ;
PushTtok(name, tok) ;
EnterBlock(name)
END P1StartBuildProgramModule ;
(*
EndBuildProgramModule - Destroys the program module scope and
checks for correct name.
The Stack is expected:
Entry Exit
Ptr ->
+------------+ +-----------+
| NameEnd | | |
|------------| |-----------|
| NameStart | | | <- Ptr
|------------| |-----------|
*)
PROCEDURE P1EndBuildProgramModule ;
VAR
start,
end : CARDINAL ;
NameStart,
NameEnd : Name ;
BEGIN
ResolveImports ;
Assert(CompilingProgramModule()) ;
EndScope ;
PopTtok(NameStart, start) ;
PopTtok(NameEnd, end) ;
IF Debugging
THEN
printf0('pass 1: ') ;
DisplayTrees(GetCurrentModule())
END ;
IF NameStart#NameEnd
THEN
MetaErrorT1 (end,
'inconsistent program module name {%1Wa}', MakeError (start, NameStart))
END ;
LeaveBlock
END P1EndBuildProgramModule ;
(*
StartBuildInnerModule - Creates an Inner module and starts
a new scope.
The Stack is expected:
Entry Exit
Ptr -> <- Ptr
+------------+ +-----------+
| NameStart | | NameStart |
|------------| |-----------|
*)
PROCEDURE StartBuildInnerModule ;
VAR
tok : CARDINAL ;
name : Name ;
ModuleSym: CARDINAL ;
BEGIN
PopTtok(name, tok) ;
ModuleSym := GetSym(name) ;
Assert(ModuleSym#NulSym) ;
StartScope(ModuleSym) ;
Assert(NOT IsDefImp(ModuleSym)) ;
PushTtok(name, tok) ;
EnterBlock(name)
END StartBuildInnerModule ;
(*
EndBuildInnerModule - Destroys the Inner module scope and
checks for correct name.
The Stack is expected:
Entry Exit
Ptr ->
+------------+ +-----------+
| NameEnd | | |
|------------| |-----------|
| NameStart | | | <- Ptr
|------------| |-----------|
*)
PROCEDURE EndBuildInnerModule ;
VAR
start, end: CARDINAL ;
NameStart,
NameEnd : Name ;
BEGIN
EndScope ;
PopTtok(NameStart, start) ;
PopTtok(NameEnd, end) ;
IF NameStart#NameEnd
THEN
MetaErrorT1 (end,
'inconsistent inner module name {%1Wa}', MakeError (start, NameStart))
END ;
LeaveBlock
END EndBuildInnerModule ;
(*
BuildImportOuterModule - Builds imported identifiers into an outer module
from a definition module.
The Stack is expected:
Entry OR Entry
Ptr -> Ptr ->
+------------+ +-----------+
| # | | # |
|------------| |-----------|
| Id1 | | Id1 |
|------------| |-----------|
. . . .
. . . .
. . . .
|------------| |-----------|
| Id# | | Id# |
|------------| |-----------|
| ImportTok | | Ident |
|------------| |-----------|
IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
Exit
All above stack discarded
*)
PROCEDURE BuildImportOuterModule (definition: BOOLEAN) ;
VAR
Sym, ModSym,
i, n : CARDINAL ;
BEGIN
PopT(n) ; (* n = # of the Ident List *)
IF OperandT(n+1)=ImportTok
THEN
(* Ident list contains Module Names *)
i := 1 ;
WHILE i<=n DO
ModSym := LookupModule(OperandTok(n+1-i),
OperandT(n+1-i)) ;
PutImported(ModSym) ;
IF definition
THEN
PutIncludedByDefinition(ModSym)
END ;
INC(i)
END
ELSE
(* Ident List contains list of objects *)
ModSym := LookupModule(OperandTok(n+1),
OperandT(n+1)) ;
i := 1 ;
WHILE i<=n DO
(*
WriteString('Importing ') ; WriteKey(Operand(j)) ; WriteString(' from ') ; WriteKey(GetSymName(ModSym)) ; WriteLn ;
*)
Sym := GetExported (OperandTok (n+1-i),
ModSym, OperandT (n+1-i)) ;
PutImported (Sym) ;
INC (i)
END
END ;
PopN (n+1) (* clear stack *)
END BuildImportOuterModule ;
(*
BuildExportOuterModule - Builds exported identifiers from an outer module
to the outside world of library modules.
The Stack is expected:
Entry OR Entry
Ptr -> Ptr ->
+------------+ +--------------+
| # | | # |
|------------| |--------------|
| Id1 | | Id1 |
|------------| |--------------|
. . . .
. . . .
. . . .
|------------| |--------------|
| Id# | | Id# |
|------------| |--------------|
| ExportTok | | QualifiedTok |
|------------| |--------------|
EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
Error Condition
Exit
All above stack discarded
*)
PROCEDURE BuildExportOuterModule ;
VAR
i, n: CARDINAL ;
BEGIN
PopT (n) ; (* n = # of the Ident List *)
IF (OperandT(n+1)=QualifiedTok) AND CompilingDefinitionModule()
THEN
PutDoesNeedExportList(GetCurrentModule()) ;
(* Ident List contains list of export qualified objects *)
i := 1 ;
WHILE i<=n DO
PutExportQualified (OperandTok (i), OperandT (i)) ;
INC (i)
END
ELSIF (OperandT(n+1)=UnQualifiedTok) AND CompilingDefinitionModule()
THEN
PutDoesNeedExportList(GetCurrentModule()) ;
(* Ident List contains list of export unqualified objects *)
i := 1 ;
WHILE i<=n DO
PutExportUnQualified (OperandTok (i), OperandT(i)) ;
INC (i)
END
ELSIF CompilingDefinitionModule()
THEN
MetaError0 ('the {%EkEXPORT} must be either {%kQUALIFIED} or {%kUNQUALIFIED} in a definition module')
ELSE
MetaError0 ('{%E}only allowed inter module exports in a definition module')
END ;
PopN (n+1) (* clear stack *)
END BuildExportOuterModule ;
(*
CheckExplicitExported - checks to see whether we are compiling
a definition module and whether the ident
is implicitly export qualified or unqualified.
The Stack is expected:
Entry Exit
Ptr -> Ptr ->
+------------+ +-----------+
| Identname | | Identname |
|------------| |-----------|
*)
PROCEDURE CheckExplicitExported ;
BEGIN
IF CompilingDefinitionModule() AND DoesNotNeedExportList(GetCurrentModule())
THEN
(* printf1('exporting identifier %a\n', OperandT(1)) ; *)
PutExportQualified (OperandTok (1), OperandT(1))
END
END CheckExplicitExported ;
(*
BuildImportInnerModule - Builds imported identifiers into an inner module
from the last level of module.
The Stack is expected:
Entry OR Entry
Ptr -> Ptr ->
+------------+ +-----------+
| # | | # |
|------------| |-----------|
| Id1 | | Id1 |
|------------| |-----------|
. . . .
. . . .
. . . .
|------------| |-----------|
| Id# | | Id# |
|------------| |-----------|
| ImportTok | | Ident |
|------------| |-----------|
IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
Exit
All above stack discarded
*)
PROCEDURE BuildImportInnerModule ;
VAR
Sym, ModSym,
i, n : CARDINAL ;
BEGIN
PopT (n) ; (* n = # of the Ident List *)
IF OperandT (n+1) = ImportTok
THEN
(* Ident List contains list of objects *)
i := 1 ;
WHILE i<=n DO
AddNameToImportList (OperandT (i)) ;
INC (i)
END
ELSE
(* Ident List contains list of objects *)
ModSym := LookupOuterModule (OperandTok(n+1),
OperandT(n+1)) ;
i := 1 ;
WHILE i<=n DO
Sym := GetExported (OperandTok (n+1-i), ModSym, OperandT (n+1-i)) ;
PutImported (Sym) ;
INC (i)
END
END ;
PopN (n+1) (* clear stack *)
END BuildImportInnerModule ;
(*
BuildExportInnerModule - Builds exported identifiers from an inner module
to the next layer module.
The Stack is expected:
Entry OR Entry
Ptr -> Ptr ->
+------------+ +--------------+
| # | | # |
|------------| |--------------|
| Id1 | | Id1 |
|------------| |--------------|
. . . .
. . . .
. . . .
|------------| |--------------|
| Id# | | Id# |
|------------| |--------------|
| ExportTok | | QualifiedTok |
|------------| |--------------|
EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
Exit
All above stack discarded
*)
PROCEDURE BuildExportInnerModule ;
VAR
tok : CARDINAL ;
PrevMod,
Sym,
i, n : CARDINAL ;
BEGIN
PopT (n) ; (* n = # of the Ident List *)
IF OperandT (n+1) = ExportTok
THEN
(* Ident List contains list of objects *)
i := 1 ;
PrevMod := GetScope (GetCurrentScope ()) ;
WHILE i<=n DO
tok := OperandTok (i) ;
IF (PrevMod#NulSym) AND (IsModule(PrevMod) OR IsDefImp(PrevMod))
THEN
Sym := GetLocalSym (PrevMod, OperandT(i)) ;
IF Sym=NulSym
THEN
Sym := TryMoveUndeclaredSymToInnerModule (PrevMod, GetCurrentScope (), OperandT (i)) ;
IF Sym=NulSym
THEN
Sym := RequestSym (tok, OperandT(i)) ;
PutExported (Sym)
END
ELSE
(* use Sym which has already been created in outer scope *)
AddSymToModuleScope (GetCurrentScope (), Sym)
END
ELSE
Sym := RequestSym (tok, OperandT(i)) ;
PutExported (Sym)
END ;
INC (i)
END
ELSE
MetaError0 ('{%EkQUALIFIED} not allowed in an inner module')
END ;
PopN(n+1) (* clear stack *)
END BuildExportInnerModule ;
(*
StartBuildEnumeration - Builds an Enumeration type Type.
Stack
Entry Exit
Ptr ->
+------------+
| # |
|------------|
| en 1 |
|------------|
| en 2 |
|------------|
. .
. .
. . <- Ptr
|------------| +------------+
| en # | | Type |
|------------| |------------|
| Name | | Name |
|------------| |------------|
*)
PROCEDURE StartBuildEnumeration ;
VAR
name : Name ;
n, i,
Type : CARDINAL ;
tokno: CARDINAL ;
BEGIN
PopT(n) ; (* No := # *)
name := OperandT(n+1) ;
tokno := OperandTok(n+1) ;
Type := MakeEnumeration(tokno, name) ;
i := 1 ;
WHILE i<=n DO
PutFieldEnumeration(OperandTok(n-i+1), Type, OperandT(n-i+1)) ;
INC(i)
END ;
PutEnumerationIntoFifoQueue(Type) ; (* store enumeration away for pass 2 *)
PopN(n+1) ;
PushTtok(name, tokno) ;
PushTtok(Type, tokno)
END StartBuildEnumeration ;
(*
EndBuildEnumeration - completes the construction of the enumeration type.
Stack
Entry Exit
Ptr ->
+------------+
| Type | <- Ptr
|------------| +---------------+
| Name | | Type | Name |
|------------| |---------------|
Empty
*)
PROCEDURE EndBuildEnumeration ;
VAR
tokno : CARDINAL ;
Sym,
Type : CARDINAL ;
n1, n2,
name : Name ;
BEGIN
(*
Two cases
- the type name the same as Name, or the name is nul. - do nothing.
- when type with a name that is different to Name. In which case
we create a new type.
*)
PopTtok(Type, tokno) ;
PopT(name) ;
IF Debugging
THEN
n1 := GetSymName(GetCurrentModule()) ;
printf2('inside module %a declaring type name %a\n',
n1, name) ;
IF (NOT IsUnknown(Type))
THEN
n1 := GetSymName(GetScope(Type)) ;
n2 := GetSymName(Type) ;
printf2('type was created inside scope %a as name %a\n',
n1, n2)
END
END ;
IF (name=NulName) OR (GetSymName(Type)=name)
THEN
(*
Typically the declaration that causes this case is:
VAR
a: (blue, green, red) ;
^
|
+---- type has no name.
in which case the constructed from StartBuildEnumeration is complete
*)
PushTFtok(Type, name, tokno)
ELSE
(* in this case we are seeing:
TYPE
name = (blue, green, red)
so we construct the type name and define it to have the previously
created enumeration type
*)
Sym := MakeType(tokno, name) ;
PutType(Sym, Type) ;
PushTFtok(Sym, name, tokno)
END
END EndBuildEnumeration ;
(*
BuildHiddenType - Builds a Hidden Type.
Stack
Entry Exit
Ptr ->
+------------+
| Name | <- Ptr
|------------| Empty
*)
PROCEDURE BuildHiddenType ;
VAR
name : Name ;
tokno: CARDINAL ;
BEGIN
PopTtok (name, tokno) ;
(* WriteString('Hidden type encountered: ') ; *)
(* WriteKey(Name) ; WriteLn ; *)
Assert (MakeHiddenType (tokno, name) # NulSym)
END BuildHiddenType ;
(*
StartBuildProcedure - Builds a Procedure.
The Stack:
Entry Exit
Ptr -> <- Ptr
+------------+ +------------+
| Name | | ProcSym |
|------------| |------------|
| inlinetok | | |
| or | | |
| builtintok | | |
| or name or | | Name |
| NulTok | | |
|------------| |------------|
*)
PROCEDURE StartBuildProcedure ;
VAR
tokno : CARDINAL ;
builtin,
name : Name ;
ProcSym : CARDINAL ;
BEGIN
PopTtok (name, tokno) ;
PopT (builtin) ; (* was this procedure defined as a builtin? *)
PushTtok (name, tokno) ; (* Name saved for the EndBuildProcedure name check *)
ProcSym := RequestSym (tokno, name) ;
IF IsUnknown (ProcSym)
THEN
(* A procedure may be created in a definition or implementation module, remember
that an implementation module maybe compiled before the corresponding
definition module.
The procedure can also be created during a forward declaration.
We record the forward declaration as the token of creation and adjust this
later when we see the proper procedure declaration. Likwwise when the forward
keyword is seen we assign the procedure forward token location. *)
ProcSym := MakeProcedure (tokno, name)
ELSIF IsProcedure (ProcSym)
THEN
(* Declared in the other module or it could have been declared by a forward decl,
we overwrite the declaration to tokno. The forward location is assigned in
EndBuildForward. *)
PutDeclared (tokno, ProcSym)
ELSE
MetaError1 ('expecting a procedure name and symbol {%1Ea} has been declared as a {%1d}', ProcSym) ;
PushT (ProcSym) ;
RETURN
END ;
IF builtin#NulTok
THEN
IF builtin=BuiltinTok
THEN
PutProcedureBuiltin (ProcSym, name)
ELSIF builtin=InlineTok
THEN
PutProcedureInline (ProcSym)
ELSE
PutProcedureBuiltin (ProcSym, builtin)
END
END ;
PushTtok (ProcSym, tokno) ;
StartScope (ProcSym) ;
IF CompilingDefinitionModule ()
THEN
IF GetProcedureDefined (ProcSym, DefProcedure)
THEN
MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, DefProcedure),
'first declaration of procedure {%1Ea} in the definition module', ProcSym) ;
MetaErrorT1 (tokno,
'duplicate declaration of procedure {%1Ea} in the definition module', ProcSym)
ELSE
PutProcedureDeclaredTok (ProcSym, DefProcedure, tokno) ;
PutProcedureDefined (ProcSym, DefProcedure)
END
ELSE
EnterBlock (name)
END
END StartBuildProcedure ;
(*
EndBuildProcedure - Ends building a Procedure.
It checks the start procedure name matches the end
procedure name.
The Stack:
(Procedure Not Defined in definition module)
Entry Exit
Ptr ->
+------------+
| NameEnd |
|------------|
| ProcSym |
|------------|
| NameStart |
|------------|
Empty
*)
PROCEDURE EndBuildProcedure ;
VAR
tok,
start, end: CARDINAL ;
ProcSym : CARDINAL ;
NameEnd,
NameStart : Name ;
BEGIN
PopTtok(NameEnd, end) ;
PopTtok(ProcSym, tok) ;
PopTtok(NameStart, start) ;
IF NameEnd # NameStart
THEN
IF end # UnknownTokenNo
THEN
MetaErrorT1 (end,
'procedure name at end does not match name at beginning {%1EDa}', ProcSym)
ELSIF start # UnknownTokenNo
THEN
MetaErrorT2 (start,
'procedure name at end {%1EDa} does not match name at beginning {%2a}',
MakeError (end, NameEnd), ProcSym)
ELSE
MetaError1 ('procedure name at end does not match name at beginning {%1EDa}', ProcSym)
END
END ;
EndScope ;
IF GetProcedureDefined (ProcSym, ProperProcedure)
THEN
MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, ProperProcedure),
'first proper declaration of procedure {%1Ea}', ProcSym) ;
MetaErrorT1 (tok, 'procedure {%1Ea} has already been declared', ProcSym)
ELSE
PutProcedureDeclaredTok (ProcSym, ProperProcedure, tok) ;
PutProcedureDefined (ProcSym, ProperProcedure)
END ;
Assert (NOT CompilingDefinitionModule()) ;
LeaveBlock
END EndBuildProcedure ;
(*
EndBuildForward - Ends building a forward procedure declaration.
The Stack:
(This procedure is not defined in definition module)
Entry Exit
Ptr ->
+------------+
| ProcSym |
|------------|
| NameStart |
|------------|
Empty
*)
PROCEDURE EndBuildForward (forwardPos: CARDINAL) ;
VAR
ProcSym: CARDINAL ;
tok : CARDINAL ;
BEGIN
ProcSym := OperandT (1) ;
tok := OperandTok (1) ;
IF NOT GetEnableForward ()
THEN
MetaErrorT0 (forwardPos,
'forward declaration has not been enabled, use -fiso or -fenable-forward to enable forward procedure declarations')
END ;
IF GetProcedureDefined (ProcSym, ForwardProcedure)
THEN
MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, ForwardProcedure),
'first forward declaration of {%1Ea}', ProcSym) ;
MetaErrorT1 (tok, 'forward declaration of procedure {%1Ea} has already occurred', ProcSym)
ELSE
PutProcedureDeclaredTok (ProcSym, ForwardProcedure, tok) ;
PutProcedureDefined (ProcSym, ForwardProcedure)
END ;
PopN (2) ;
EndScope ;
Assert (NOT CompilingDefinitionModule ()) ;
LeaveBlock
END EndBuildForward ;
(*
BuildProcedureHeading - Builds a procedure heading for the definition
module procedures.
Operation only performed if compiling a
definition module.
The Stack:
Entry Exit
Ptr ->
+------------+
| ProcSym |
|------------|
| NameStart |
|------------|
Empty
*)
PROCEDURE BuildProcedureHeading ;
VAR
ProcSym : CARDINAL ;
NameStart: Name ;
BEGIN
IF CompilingDefinitionModule()
THEN
PopT(ProcSym) ;
PopT(NameStart) ;
EndScope
END
END BuildProcedureHeading ;
(*
BuildNulName - Pushes a NulName onto the top of the stack.
The Stack:
Entry Exit
<- Ptr
Empty +------------+
| NulName |
|------------|
*)
PROCEDURE BuildNulName ;
BEGIN
PushT(NulName)
END BuildNulName ;
(*
BuildTypeEnd - Pops the type Type and Name.
The Stack:
Entry Exit
Ptr ->
+-------------+
| Type | Name | Empty
|-------------|
*)
PROCEDURE BuildTypeEnd ;
VAR
Type: CARDINAL ;
name: Name ;
BEGIN
PopTF (Type, name)
END BuildTypeEnd ;
(*
BuildImportStatement - create a new import statement in the current module.
It ignores local modules.
The quadruple stack is not used.
*)
PROCEDURE BuildImportStatement (tok: CARDINAL) ;
VAR
scope: CARDINAL ;
BEGIN
scope := GetCurrentScope () ;
IF IsDefImp (scope) OR (IsModule (scope) AND (NOT IsInnerModule (scope)))
THEN
IF CompilingDefinitionModule () AND (NOT IsDefImp (scope))
THEN
MetaError1 ('module scope should be a definition module rather than {%1EDa}', scope)
ELSE
INC (importStatementCount) ;
AppendModuleImportStatement (scope, MakeImportStatement (tok, importStatementCount))
END
END
END BuildImportStatement ;
(*
AddImportToImportStatement - the top of stack is expected to be a module name.
This is looked up from the module universe and
wrapped in an import symbol and placed into the
current import statement.
The quadruple stack is unchanged.
Entry Exit
Ptr -> <- Ptr
+---------------------+ +---------------------+
| ImportedModuleName | | ImportedModuleName |
|---------------------| |---------------------|
*)
PROCEDURE AddImportToImportStatement (qualified: BOOLEAN) ;
VAR
scope: CARDINAL ;
BEGIN
scope := GetCurrentScope () ;
IF IsDefImp (scope) OR (IsModule (scope) AND (NOT IsInnerModule (scope)))
THEN
IF CompilingDefinitionModule () AND (NOT IsDefImp (scope))
THEN
MetaError1 ('module scope should be a definition module rather than {%1EDa}', scope) ;
ELSE
AppendModuleOnImportStatement (scope, MakeImport (OperandTok (1),
LookupModule (OperandTok (1), OperandT (1)),
importStatementCount, qualified))
END
END
END AddImportToImportStatement ;
END P1SymBuild.
|