Debian Codegraph @

gcc/m2/gm2-compiler/P1SymBuild.mod

raw
1 (* P1SymBuild.mod pass 1 symbol creation.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE P1SymBuild ;
23
24
25 FROM ASCII IMPORT nul ;
26 FROM NameKey IMPORT Name, WriteKey, MakeKey, KeyToCharStar, NulName ;
27 FROM M2Debug IMPORT Assert, WriteDebug ;
28 FROM M2LexBuf IMPORT GetFileName, GetTokenNo, UnknownTokenNo ;
29
30 FROM M2MetaError IMPORT MetaErrorString2, MetaError0, MetaError1,
31 MetaError2, MetaErrorT0, MetaErrorT1, MetaErrorT2 ;
32
33 FROM DynamicStrings IMPORT String, Slice, InitString, KillString, EqualCharStar, RIndex, Mark, ConCat ;
34 FROM M2Printf IMPORT printf0, printf1, printf2 ;
35 FROM M2Options IMPORT Iso, GetEnableForward ;
36
37 FROM M2Reserved IMPORT ImportTok, ExportTok, QualifiedTok, UnQualifiedTok,
38 NulTok, VarTok, ArrayTok, BuiltinTok, InlineTok ;
39
40 FROM FifoQueue IMPORT PutEnumerationIntoFifoQueue ;
41 FROM P0SymBuild IMPORT EnterBlock, LeaveBlock ;
42
43 FROM SymbolTable IMPORT NulSym,
44 ModeOfAddr,
45 AppendModuleOnImportStatement,
46 AppendModuleImportStatement,
47 MakeImportStatement, MakeImport,
48
49 StartScope, EndScope, PseudoScope,
50 GetScope, GetCurrentScope,
51 IsDeclaredIn,
52 SetCurrentModule, SetFileModule,
53 MakeInnerModule,
54 MakeEnumeration, MakeSubrange,
55 MakeVar, MakeType, PutType,
56 MakeHiddenType,
57 PutMode,
58 PutFieldEnumeration, PutSubrange, PutVar,
59 IsDefImp, IsModule, IsInnerModule, IsType,
60 GetCurrentModule,
61 AddSymToModuleScope,
62 AddNameToImportList,
63 GetSym, RequestSym, IsUnknown, RenameSym,
64 GetFromOuterModule,
65 GetExported, IsExported,
66 GetLocalSym,
67 PutImported, PutIncludedByDefinition,
68 PutExported, PutExportQualified, PutExportUnQualified,
69 TryMoveUndeclaredSymToInnerModule,
70 PutDefinitionForC,
71 IsDefinitionForC,
72 PutDoesNeedExportList, PutDoesNotNeedExportList,
73 DoesNotNeedExportList,
74 MakeProcedure,
75 IsProcedure, IsConstString,
76 MakePointer, PutPointer,
77 MakeRecord, PutFieldRecord,
78 MakeArray,
79 MakeSubscript, PutSubscript,
80 PutArray, GetType, IsArray,
81 IsProcType, MakeProcType,
82 PutProcedureBuiltin, PutProcedureInline,
83 GetSymName,
84 ResolveImports, PutDeclared,
85 ProcedureKind,
86 PutProcedureDeclaredTok, GetProcedureDeclaredTok,
87 PutProcedureDefined, GetProcedureDefined,
88 MakeError, MakeErrorS,
89 DisplayTrees ;
90
91 FROM M2Batch IMPORT MakeDefinitionSource,
92 MakeImplementationSource,
93 MakeProgramSource,
94 LookupModule, LookupOuterModule ;
95
96 FROM M2Quads IMPORT PushT, PopT, PushTF, PopTF, OperandT, PopN, OperandTok,
97 PopTtok, PushTtok, PushTFtok, PopTFtok ;
98
99 FROM M2Comp IMPORT CompilingDefinitionModule,
100 CompilingImplementationModule,
101 CompilingProgramModule ;
102
103 CONST
104 Debugging = FALSE ;
105
106 VAR
107 importStatementCount: CARDINAL ;
108
109
110 (*
111 StartBuildDefinitionModule - Creates a definition module and starts
112 a new scope.
113
114 he Stack is expected:
115
116 Entry Exit
117
118 Ptr ->
119 +------------+
120 | NameStart | <- Ptr
121 |------------| +------------+
122 | NulName/"C"| | NameStart |
123 |------------| |------------|
124 *)
125
126 PROCEDURE P1StartBuildDefinitionModule ;
127 VAR
128 name : Name ;
129 language,
130 ModuleSym: CARDINAL ;
131 BEGIN
132 importStatementCount := 0 ;
133 PopT(name) ;
134 (* CheckFileName(name, 'definition') ; *)
135 ModuleSym := MakeDefinitionSource(GetTokenNo(), name) ;
136 PutDoesNotNeedExportList(ModuleSym) ;
137 SetCurrentModule(ModuleSym) ;
138 SetFileModule(ModuleSym) ;
139 StartScope(ModuleSym) ;
140 Assert(IsDefImp(ModuleSym)) ;
141 Assert(CompilingDefinitionModule()) ;
142 PopT(language) ;
143 IF (language#NulSym) AND IsConstString(language)
144 THEN
145 IF GetSymName(language)=MakeKey('C')
146 THEN
147 PutDefinitionForC(ModuleSym)
148 ELSIF GetSymName(language)=NulName
149 THEN
150 MetaError0 ('{%E}currently a non modula-2 definition module can only be declared as DEFINITION FOR {%k"C"}')
151 ELSE
152 MetaError1 ('unknown definition module language {%1Ea}, currently a non modula-2 definition module can only be declared as DEFINITION FOR {%k"C"}', language)
153 END
154 END ;
155 PushT(name) ;
156 EnterBlock(name)
157 END P1StartBuildDefinitionModule ;
158
159
160 (*
161 EndBuildDefinitionModule - Destroys the definition module scope and
162 checks for correct name.
163
164 The Stack is expected:
165
166 Entry Exit
167
168 Ptr ->
169 +------------+ +-----------+
170 | NameEnd | | |
171 |------------| |-----------|
172 | NameStart | | | <- Ptr
173 |------------| |-----------|
174 *)
175
176 PROCEDURE P1EndBuildDefinitionModule ;
177 VAR
178 start : CARDINAL ;
179 NameStart,
180 NameEnd : Name ;
181 BEGIN
182 Assert(CompilingDefinitionModule()) ;
183 EndScope ;
184 PopTtok(NameStart, start) ;
185 PopT(NameEnd) ;
186 IF Debugging
187 THEN
188 printf0('pass 1: ') ;
189 DisplayTrees(GetCurrentModule())
190 END ;
191 IF NameStart#NameEnd
192 THEN
193 MetaError1 ('inconsistent definition module name {%1Wa}', MakeError (start, NameStart))
194 END ;
195 LeaveBlock
196 END P1EndBuildDefinitionModule ;
197
198
199 (*
200 StartBuildImplementationModule - Creates an implementation module and starts
201 a new scope.
202
203 The Stack is expected:
204
205 Entry Exit
206
207 Ptr -> <- Ptr
208 +------------+ +-----------+
209 | NameStart | | NameStart |
210 |------------| |-----------|
211
212 *)
213
214 PROCEDURE P1StartBuildImplementationModule ;
215 VAR
216 tok : CARDINAL ;
217 name : Name ;
218 ModuleSym: CARDINAL ;
219 BEGIN
220 importStatementCount := 0 ;
221 PopTtok (name, tok) ;
222 (* CheckFileName(name, 'implementation') ; *)
223 ModuleSym := MakeImplementationSource (tok, name) ;
224 SetCurrentModule (ModuleSym) ;
225 SetFileModule (ModuleSym) ;
226 StartScope (ModuleSym) ;
227 IF NOT IsDefImp (ModuleSym)
228 THEN
229 MetaError1 ('cannot find corresponding definition module for {%1Ea}', ModuleSym)
230 END ;
231 Assert (CompilingImplementationModule()) ;
232 PushTtok (name, tok) ;
233 EnterBlock (name)
234 END P1StartBuildImplementationModule ;
235
236
237 (*
238 EndBuildImplementationModule - Destroys the implementation module scope and
239 checks for correct name.
240
241 The Stack is expected:
242
243 Entry Exit
244
245 Ptr ->
246 +------------+ +-----------+
247 | NameEnd | | |
248 |------------| |-----------|
249 | NameStart | | | <- Ptr
250 |------------| |-----------|
251 *)
252
253 PROCEDURE P1EndBuildImplementationModule ;
254 VAR
255 start, end: CARDINAL ;
256 NameStart,
257 NameEnd : Name ;
258 BEGIN
259 ResolveImports ;
260 Assert(CompilingImplementationModule()) ;
261 EndScope ;
262 PopTtok(NameStart, start) ;
263 PopTtok(NameEnd, end) ;
264 IF NameStart#NameEnd
265 THEN
266 MetaErrorT1 (end,
267 'inconsistent implementation module name {%1Wa}', MakeError (start, NameStart))
268 END ;
269 LeaveBlock
270 END P1EndBuildImplementationModule ;
271
272
273 (*
274 StartBuildProgramModule - Creates a program module and starts
275 a new scope.
276
277 The Stack is expected:
278
279 Entry Exit
280
281 Ptr -> <- Ptr
282 +------------+ +-----------+
283 | NameStart | | NameStart |
284 |------------| |-----------|
285
286 *)
287
288 PROCEDURE P1StartBuildProgramModule ;
289 VAR
290 tok : CARDINAL ;
291 name : Name ;
292 ModuleSym: CARDINAL ;
293 BEGIN
294 importStatementCount := 0 ;
295 PopTtok(name, tok) ;
296 (* CheckFileName(name, 'main') ; *)
297 ModuleSym := MakeProgramSource(tok, name) ;
298 SetCurrentModule(ModuleSym) ;
299 SetFileModule(ModuleSym) ;
300 StartScope(ModuleSym) ;
301 IF (NOT CompilingProgramModule()) OR IsDefImp(ModuleSym)
302 THEN
303 MetaErrorT1 (tok,
304 'module {%1Ea} has a corresponding DEFINITION MODULE but no IMPLEMENTATION keyword in the main module', ModuleSym)
305 END ;
306 PushTtok(name, tok) ;
307 EnterBlock(name)
308 END P1StartBuildProgramModule ;
309
310
311 (*
312 EndBuildProgramModule - Destroys the program module scope and
313 checks for correct name.
314
315 The Stack is expected:
316
317 Entry Exit
318
319 Ptr ->
320 +------------+ +-----------+
321 | NameEnd | | |
322 |------------| |-----------|
323 | NameStart | | | <- Ptr
324 |------------| |-----------|
325 *)
326
327 PROCEDURE P1EndBuildProgramModule ;
328 VAR
329 start,
330 end : CARDINAL ;
331 NameStart,
332 NameEnd : Name ;
333 BEGIN
334 ResolveImports ;
335 Assert(CompilingProgramModule()) ;
336 EndScope ;
337 PopTtok(NameStart, start) ;
338 PopTtok(NameEnd, end) ;
339 IF Debugging
340 THEN
341 printf0('pass 1: ') ;
342 DisplayTrees(GetCurrentModule())
343 END ;
344 IF NameStart#NameEnd
345 THEN
346 MetaErrorT1 (end,
347 'inconsistent program module name {%1Wa}', MakeError (start, NameStart))
348 END ;
349 LeaveBlock
350 END P1EndBuildProgramModule ;
351
352
353 (*
354 StartBuildInnerModule - Creates an Inner module and starts
355 a new scope.
356
357 The Stack is expected:
358
359 Entry Exit
360
361 Ptr -> <- Ptr
362 +------------+ +-----------+
363 | NameStart | | NameStart |
364 |------------| |-----------|
365
366 *)
367
368 PROCEDURE StartBuildInnerModule ;
369 VAR
370 tok : CARDINAL ;
371 name : Name ;
372 ModuleSym: CARDINAL ;
373 BEGIN
374 PopTtok(name, tok) ;
375 ModuleSym := GetSym(name) ;
376 Assert(ModuleSym#NulSym) ;
377 StartScope(ModuleSym) ;
378 Assert(NOT IsDefImp(ModuleSym)) ;
379 PushTtok(name, tok) ;
380 EnterBlock(name)
381 END StartBuildInnerModule ;
382
383
384 (*
385 EndBuildInnerModule - Destroys the Inner module scope and
386 checks for correct name.
387
388 The Stack is expected:
389
390 Entry Exit
391
392 Ptr ->
393 +------------+ +-----------+
394 | NameEnd | | |
395 |------------| |-----------|
396 | NameStart | | | <- Ptr
397 |------------| |-----------|
398 *)
399
400 PROCEDURE EndBuildInnerModule ;
401 VAR
402 start, end: CARDINAL ;
403 NameStart,
404 NameEnd : Name ;
405 BEGIN
406 EndScope ;
407 PopTtok(NameStart, start) ;
408 PopTtok(NameEnd, end) ;
409 IF NameStart#NameEnd
410 THEN
411 MetaErrorT1 (end,
412 'inconsistent inner module name {%1Wa}', MakeError (start, NameStart))
413 END ;
414 LeaveBlock
415 END EndBuildInnerModule ;
416
417
418 (*
419 BuildImportOuterModule - Builds imported identifiers into an outer module
420 from a definition module.
421
422 The Stack is expected:
423
424 Entry OR Entry
425
426 Ptr -> Ptr ->
427 +------------+ +-----------+
428 | # | | # |
429 |------------| |-----------|
430 | Id1 | | Id1 |
431 |------------| |-----------|
432 . . . .
433 . . . .
434 . . . .
435 |------------| |-----------|
436 | Id# | | Id# |
437 |------------| |-----------|
438 | ImportTok | | Ident |
439 |------------| |-----------|
440
441 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
442
443
444 Exit
445
446 All above stack discarded
447 *)
448
449 PROCEDURE BuildImportOuterModule (definition: BOOLEAN) ;
450 VAR
451 Sym, ModSym,
452 i, n : CARDINAL ;
453 BEGIN
454 PopT(n) ; (* n = # of the Ident List *)
455 IF OperandT(n+1)=ImportTok
456 THEN
457 (* Ident list contains Module Names *)
458 i := 1 ;
459 WHILE i<=n DO
460 ModSym := LookupModule(OperandTok(n+1-i),
461 OperandT(n+1-i)) ;
462 PutImported(ModSym) ;
463 IF definition
464 THEN
465 PutIncludedByDefinition(ModSym)
466 END ;
467 INC(i)
468 END
469 ELSE
470 (* Ident List contains list of objects *)
471 ModSym := LookupModule(OperandTok(n+1),
472 OperandT(n+1)) ;
473 i := 1 ;
474 WHILE i<=n DO
475 (*
476 WriteString('Importing ') ; WriteKey(Operand(j)) ; WriteString(' from ') ; WriteKey(GetSymName(ModSym)) ; WriteLn ;
477 *)
478 Sym := GetExported (OperandTok (n+1-i),
479 ModSym, OperandT (n+1-i)) ;
480 PutImported (Sym) ;
481 INC (i)
482 END
483 END ;
484 PopN (n+1) (* clear stack *)
485 END BuildImportOuterModule ;
486
487
488 (*
489 BuildExportOuterModule - Builds exported identifiers from an outer module
490 to the outside world of library modules.
491
492 The Stack is expected:
493
494 Entry OR Entry
495
496 Ptr -> Ptr ->
497 +------------+ +--------------+
498 | # | | # |
499 |------------| |--------------|
500 | Id1 | | Id1 |
501 |------------| |--------------|
502 . . . .
503 . . . .
504 . . . .
505 |------------| |--------------|
506 | Id# | | Id# |
507 |------------| |--------------|
508 | ExportTok | | QualifiedTok |
509 |------------| |--------------|
510
511 EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
512
513 Error Condition
514
515
516 Exit
517
518 All above stack discarded
519 *)
520
521 PROCEDURE BuildExportOuterModule ;
522 VAR
523 i, n: CARDINAL ;
524 BEGIN
525 PopT (n) ; (* n = # of the Ident List *)
526 IF (OperandT(n+1)=QualifiedTok) AND CompilingDefinitionModule()
527 THEN
528 PutDoesNeedExportList(GetCurrentModule()) ;
529 (* Ident List contains list of export qualified objects *)
530 i := 1 ;
531 WHILE i<=n DO
532 PutExportQualified (OperandTok (i), OperandT (i)) ;
533 INC (i)
534 END
535 ELSIF (OperandT(n+1)=UnQualifiedTok) AND CompilingDefinitionModule()
536 THEN
537 PutDoesNeedExportList(GetCurrentModule()) ;
538 (* Ident List contains list of export unqualified objects *)
539 i := 1 ;
540 WHILE i<=n DO
541 PutExportUnQualified (OperandTok (i), OperandT(i)) ;
542 INC (i)
543 END
544 ELSIF CompilingDefinitionModule()
545 THEN
546 MetaError0 ('the {%EkEXPORT} must be either {%kQUALIFIED} or {%kUNQUALIFIED} in a definition module')
547 ELSE
548 MetaError0 ('{%E}only allowed inter module exports in a definition module')
549 END ;
550 PopN (n+1) (* clear stack *)
551 END BuildExportOuterModule ;
552
553
554 (*
555 CheckExplicitExported - checks to see whether we are compiling
556 a definition module and whether the ident
557 is implicitly export qualified or unqualified.
558
559
560 The Stack is expected:
561
562 Entry Exit
563
564 Ptr -> Ptr ->
565 +------------+ +-----------+
566 | Identname | | Identname |
567 |------------| |-----------|
568
569 *)
570
571 PROCEDURE CheckExplicitExported ;
572 BEGIN
573 IF CompilingDefinitionModule() AND DoesNotNeedExportList(GetCurrentModule())
574 THEN
575 (* printf1('exporting identifier %a\n', OperandT(1)) ; *)
576 PutExportQualified (OperandTok (1), OperandT(1))
577 END
578 END CheckExplicitExported ;
579
580
581 (*
582 BuildImportInnerModule - Builds imported identifiers into an inner module
583 from the last level of module.
584
585 The Stack is expected:
586
587 Entry OR Entry
588
589 Ptr -> Ptr ->
590 +------------+ +-----------+
591 | # | | # |
592 |------------| |-----------|
593 | Id1 | | Id1 |
594 |------------| |-----------|
595 . . . .
596 . . . .
597 . . . .
598 |------------| |-----------|
599 | Id# | | Id# |
600 |------------| |-----------|
601 | ImportTok | | Ident |
602 |------------| |-----------|
603
604 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
605
606 Exit
607
608 All above stack discarded
609 *)
610
611 PROCEDURE BuildImportInnerModule ;
612 VAR
613 Sym, ModSym,
614 i, n : CARDINAL ;
615 BEGIN
616 PopT (n) ; (* n = # of the Ident List *)
617 IF OperandT (n+1) = ImportTok
618 THEN
619 (* Ident List contains list of objects *)
620 i := 1 ;
621 WHILE i<=n DO
622 AddNameToImportList (OperandT (i)) ;
623 INC (i)
624 END
625 ELSE
626 (* Ident List contains list of objects *)
627 ModSym := LookupOuterModule (OperandTok(n+1),
628 OperandT(n+1)) ;
629 i := 1 ;
630 WHILE i<=n DO
631 Sym := GetExported (OperandTok (n+1-i), ModSym, OperandT (n+1-i)) ;
632 PutImported (Sym) ;
633 INC (i)
634 END
635 END ;
636 PopN (n+1) (* clear stack *)
637 END BuildImportInnerModule ;
638
639
640 (*
641 BuildExportInnerModule - Builds exported identifiers from an inner module
642 to the next layer module.
643
644 The Stack is expected:
645
646 Entry OR Entry
647
648 Ptr -> Ptr ->
649 +------------+ +--------------+
650 | # | | # |
651 |------------| |--------------|
652 | Id1 | | Id1 |
653 |------------| |--------------|
654 . . . .
655 . . . .
656 . . . .
657 |------------| |--------------|
658 | Id# | | Id# |
659 |------------| |--------------|
660 | ExportTok | | QualifiedTok |
661 |------------| |--------------|
662
663 EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
664
665
666 Exit
667
668
669 All above stack discarded
670 *)
671
672 PROCEDURE BuildExportInnerModule ;
673 VAR
674 tok : CARDINAL ;
675 PrevMod,
676 Sym,
677 i, n : CARDINAL ;
678 BEGIN
679 PopT (n) ; (* n = # of the Ident List *)
680 IF OperandT (n+1) = ExportTok
681 THEN
682 (* Ident List contains list of objects *)
683 i := 1 ;
684 PrevMod := GetScope (GetCurrentScope ()) ;
685 WHILE i<=n DO
686 tok := OperandTok (i) ;
687 IF (PrevMod#NulSym) AND (IsModule(PrevMod) OR IsDefImp(PrevMod))
688 THEN
689 Sym := GetLocalSym (PrevMod, OperandT(i)) ;
690 IF Sym=NulSym
691 THEN
692 Sym := TryMoveUndeclaredSymToInnerModule (PrevMod, GetCurrentScope (), OperandT (i)) ;
693 IF Sym=NulSym
694 THEN
695 Sym := RequestSym (tok, OperandT(i)) ;
696 PutExported (Sym)
697 END
698 ELSE
699 (* use Sym which has already been created in outer scope *)
700 AddSymToModuleScope (GetCurrentScope (), Sym)
701 END
702 ELSE
703 Sym := RequestSym (tok, OperandT(i)) ;
704 PutExported (Sym)
705 END ;
706 INC (i)
707 END
708 ELSE
709 MetaError0 ('{%EkQUALIFIED} not allowed in an inner module')
710 END ;
711 PopN(n+1) (* clear stack *)
712 END BuildExportInnerModule ;
713
714
715 (*
716 StartBuildEnumeration - Builds an Enumeration type Type.
717
718
719 Stack
720
721 Entry Exit
722
723 Ptr ->
724 +------------+
725 | # |
726 |------------|
727 | en 1 |
728 |------------|
729 | en 2 |
730 |------------|
731 . .
732 . .
733 . . <- Ptr
734 |------------| +------------+
735 | en # | | Type |
736 |------------| |------------|
737 | Name | | Name |
738 |------------| |------------|
739 *)
740
741 PROCEDURE StartBuildEnumeration ;
742 VAR
743 name : Name ;
744 n, i,
745 Type : CARDINAL ;
746 tokno: CARDINAL ;
747 BEGIN
748 PopT(n) ; (* No := # *)
749 name := OperandT(n+1) ;
750 tokno := OperandTok(n+1) ;
751 Type := MakeEnumeration(tokno, name) ;
752 i := 1 ;
753 WHILE i<=n DO
754 PutFieldEnumeration(OperandTok(n-i+1), Type, OperandT(n-i+1)) ;
755 INC(i)
756 END ;
757 PutEnumerationIntoFifoQueue(Type) ; (* store enumeration away for pass 2 *)
758 PopN(n+1) ;
759 PushTtok(name, tokno) ;
760 PushTtok(Type, tokno)
761 END StartBuildEnumeration ;
762
763
764 (*
765 EndBuildEnumeration - completes the construction of the enumeration type.
766
767
768 Stack
769
770 Entry Exit
771
772 Ptr ->
773 +------------+
774 | Type | <- Ptr
775 |------------| +---------------+
776 | Name | | Type | Name |
777 |------------| |---------------|
778
779 Empty
780 *)
781
782 PROCEDURE EndBuildEnumeration ;
783 VAR
784 tokno : CARDINAL ;
785 Sym,
786 Type : CARDINAL ;
787 n1, n2,
788 name : Name ;
789 BEGIN
790 (*
791 Two cases
792
793 - the type name the same as Name, or the name is nul. - do nothing.
794 - when type with a name that is different to Name. In which case
795 we create a new type.
796 *)
797 PopTtok(Type, tokno) ;
798 PopT(name) ;
799
800 IF Debugging
801 THEN
802 n1 := GetSymName(GetCurrentModule()) ;
803 printf2('inside module %a declaring type name %a\n',
804 n1, name) ;
805 IF (NOT IsUnknown(Type))
806 THEN
807 n1 := GetSymName(GetScope(Type)) ;
808 n2 := GetSymName(Type) ;
809 printf2('type was created inside scope %a as name %a\n',
810 n1, n2)
811 END
812 END ;
813 IF (name=NulName) OR (GetSymName(Type)=name)
814 THEN
815 (*
816 Typically the declaration that causes this case is:
817
818 VAR
819 a: (blue, green, red) ;
820 ^
821 |
822 +---- type has no name.
823
824 in which case the constructed from StartBuildEnumeration is complete
825 *)
826 PushTFtok(Type, name, tokno)
827 ELSE
828 (* in this case we are seeing:
829
830 TYPE
831 name = (blue, green, red)
832
833 so we construct the type name and define it to have the previously
834 created enumeration type
835 *)
836 Sym := MakeType(tokno, name) ;
837 PutType(Sym, Type) ;
838 PushTFtok(Sym, name, tokno)
839 END
840 END EndBuildEnumeration ;
841
842
843 (*
844 BuildHiddenType - Builds a Hidden Type.
845
846
847 Stack
848
849 Entry Exit
850
851 Ptr ->
852 +------------+
853 | Name | <- Ptr
854 |------------| Empty
855 *)
856
857 PROCEDURE BuildHiddenType ;
858 VAR
859 name : Name ;
860 tokno: CARDINAL ;
861 BEGIN
862 PopTtok (name, tokno) ;
863 (* WriteString('Hidden type encountered: ') ; *)
864 (* WriteKey(Name) ; WriteLn ; *)
865 Assert (MakeHiddenType (tokno, name) # NulSym)
866 END BuildHiddenType ;
867
868
869 (*
870 StartBuildProcedure - Builds a Procedure.
871
872 The Stack:
873
874 Entry Exit
875
876 Ptr -> <- Ptr
877 +------------+ +------------+
878 | Name | | ProcSym |
879 |------------| |------------|
880 | inlinetok | | |
881 | or | | |
882 | builtintok | | |
883 | or name or | | Name |
884 | NulTok | | |
885 |------------| |------------|
886 *)
887
888 PROCEDURE StartBuildProcedure ;
889 VAR
890 tokno : CARDINAL ;
891 builtin,
892 name : Name ;
893 ProcSym : CARDINAL ;
894 BEGIN
895 PopTtok (name, tokno) ;
896 PopT (builtin) ; (* was this procedure defined as a builtin? *)
897 PushTtok (name, tokno) ; (* Name saved for the EndBuildProcedure name check *)
898 ProcSym := RequestSym (tokno, name) ;
899 IF IsUnknown (ProcSym)
900 THEN
901 (* A procedure may be created in a definition or implementation module, remember
902 that an implementation module maybe compiled before the corresponding
903 definition module.
904
905 The procedure can also be created during a forward declaration.
906 We record the forward declaration as the token of creation and adjust this
907 later when we see the proper procedure declaration. Likwwise when the forward
908 keyword is seen we assign the procedure forward token location. *)
909 ProcSym := MakeProcedure (tokno, name)
910 ELSIF IsProcedure (ProcSym)
911 THEN
912 (* Declared in the other module or it could have been declared by a forward decl,
913 we overwrite the declaration to tokno. The forward location is assigned in
914 EndBuildForward. *)
915 PutDeclared (tokno, ProcSym)
916 ELSE
917 MetaError1 ('expecting a procedure name and symbol {%1Ea} has been declared as a {%1d}', ProcSym) ;
918 PushT (ProcSym) ;
919 RETURN
920 END ;
921 IF builtin#NulTok
922 THEN
923 IF builtin=BuiltinTok
924 THEN
925 PutProcedureBuiltin (ProcSym, name)
926 ELSIF builtin=InlineTok
927 THEN
928 PutProcedureInline (ProcSym)
929 ELSE
930 PutProcedureBuiltin (ProcSym, builtin)
931 END
932 END ;
933 PushTtok (ProcSym, tokno) ;
934 StartScope (ProcSym) ;
935 IF CompilingDefinitionModule ()
936 THEN
937 IF GetProcedureDefined (ProcSym, DefProcedure)
938 THEN
939 MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, DefProcedure),
940 'first declaration of procedure {%1Ea} in the definition module', ProcSym) ;
941 MetaErrorT1 (tokno,
942 'duplicate declaration of procedure {%1Ea} in the definition module', ProcSym)
943 ELSE
944 PutProcedureDeclaredTok (ProcSym, DefProcedure, tokno) ;
945 PutProcedureDefined (ProcSym, DefProcedure)
946 END
947 ELSE
948 EnterBlock (name)
949 END
950 END StartBuildProcedure ;
951
952
953 (*
954 EndBuildProcedure - Ends building a Procedure.
955 It checks the start procedure name matches the end
956 procedure name.
957
958 The Stack:
959
960 (Procedure Not Defined in definition module)
961
962 Entry Exit
963
964 Ptr ->
965 +------------+
966 | NameEnd |
967 |------------|
968 | ProcSym |
969 |------------|
970 | NameStart |
971 |------------|
972 Empty
973 *)
974
975 PROCEDURE EndBuildProcedure ;
976 VAR
977 tok,
978 start, end: CARDINAL ;
979 ProcSym : CARDINAL ;
980 NameEnd,
981 NameStart : Name ;
982 BEGIN
983 PopTtok(NameEnd, end) ;
984 PopTtok(ProcSym, tok) ;
985 PopTtok(NameStart, start) ;
986 IF NameEnd # NameStart
987 THEN
988 IF end # UnknownTokenNo
989 THEN
990 MetaErrorT1 (end,
991 'procedure name at end does not match name at beginning {%1EDa}', ProcSym)
992 ELSIF start # UnknownTokenNo
993 THEN
994 MetaErrorT2 (start,
995 'procedure name at end {%1EDa} does not match name at beginning {%2a}',
996 MakeError (end, NameEnd), ProcSym)
997 ELSE
998 MetaError1 ('procedure name at end does not match name at beginning {%1EDa}', ProcSym)
999 END
1000 END ;
1001 EndScope ;
1002 IF GetProcedureDefined (ProcSym, ProperProcedure)
1003 THEN
1004 MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, ProperProcedure),
1005 'first proper declaration of procedure {%1Ea}', ProcSym) ;
1006 MetaErrorT1 (tok, 'procedure {%1Ea} has already been declared', ProcSym)
1007 ELSE
1008 PutProcedureDeclaredTok (ProcSym, ProperProcedure, tok) ;
1009 PutProcedureDefined (ProcSym, ProperProcedure)
1010 END ;
1011 Assert (NOT CompilingDefinitionModule()) ;
1012 LeaveBlock
1013 END EndBuildProcedure ;
1014
1015
1016 (*
1017 EndBuildForward - Ends building a forward procedure declaration.
1018
1019 The Stack:
1020
1021 (This procedure is not defined in definition module)
1022
1023 Entry Exit
1024
1025 Ptr ->
1026 +------------+
1027 | ProcSym |
1028 |------------|
1029 | NameStart |
1030 |------------|
1031 Empty
1032 *)
1033
1034 PROCEDURE EndBuildForward (forwardPos: CARDINAL) ;
1035 VAR
1036 ProcSym: CARDINAL ;
1037 tok : CARDINAL ;
1038 BEGIN
1039 ProcSym := OperandT (1) ;
1040 tok := OperandTok (1) ;
1041 IF NOT GetEnableForward ()
1042 THEN
1043 MetaErrorT0 (forwardPos,
1044 'forward declaration has not been enabled, use -fiso or -fenable-forward to enable forward procedure declarations')
1045 END ;
1046 IF GetProcedureDefined (ProcSym, ForwardProcedure)
1047 THEN
1048 MetaErrorT1 (GetProcedureDeclaredTok (ProcSym, ForwardProcedure),
1049 'first forward declaration of {%1Ea}', ProcSym) ;
1050 MetaErrorT1 (tok, 'forward declaration of procedure {%1Ea} has already occurred', ProcSym)
1051 ELSE
1052 PutProcedureDeclaredTok (ProcSym, ForwardProcedure, tok) ;
1053 PutProcedureDefined (ProcSym, ForwardProcedure)
1054 END ;
1055 PopN (2) ;
1056 EndScope ;
1057 Assert (NOT CompilingDefinitionModule ()) ;
1058 LeaveBlock
1059 END EndBuildForward ;
1060
1061
1062 (*
1063 BuildProcedureHeading - Builds a procedure heading for the definition
1064 module procedures.
1065
1066 Operation only performed if compiling a
1067 definition module.
1068
1069 The Stack:
1070
1071 Entry Exit
1072
1073 Ptr ->
1074 +------------+
1075 | ProcSym |
1076 |------------|
1077 | NameStart |
1078 |------------|
1079 Empty
1080
1081 *)
1082
1083 PROCEDURE BuildProcedureHeading ;
1084 VAR
1085 ProcSym : CARDINAL ;
1086 NameStart: Name ;
1087 BEGIN
1088 IF CompilingDefinitionModule()
1089 THEN
1090 PopT(ProcSym) ;
1091 PopT(NameStart) ;
1092 EndScope
1093 END
1094 END BuildProcedureHeading ;
1095
1096
1097 (*
1098 BuildNulName - Pushes a NulName onto the top of the stack.
1099 The Stack:
1100
1101
1102 Entry Exit
1103
1104 <- Ptr
1105 Empty +------------+
1106 | NulName |
1107 |------------|
1108 *)
1109
1110 PROCEDURE BuildNulName ;
1111 BEGIN
1112 PushT(NulName)
1113 END BuildNulName ;
1114
1115
1116 (*
1117 BuildTypeEnd - Pops the type Type and Name.
1118 The Stack:
1119
1120
1121 Entry Exit
1122
1123
1124 Ptr ->
1125 +-------------+
1126 | Type | Name | Empty
1127 |-------------|
1128 *)
1129
1130 PROCEDURE BuildTypeEnd ;
1131 VAR
1132 Type: CARDINAL ;
1133 name: Name ;
1134 BEGIN
1135 PopTF (Type, name)
1136 END BuildTypeEnd ;
1137
1138
1139 (*
1140 BuildImportStatement - create a new import statement in the current module.
1141 It ignores local modules.
1142
1143 The quadruple stack is not used.
1144 *)
1145
1146 PROCEDURE BuildImportStatement (tok: CARDINAL) ;
1147 VAR
1148 scope: CARDINAL ;
1149 BEGIN
1150 scope := GetCurrentScope () ;
1151 IF IsDefImp (scope) OR (IsModule (scope) AND (NOT IsInnerModule (scope)))
1152 THEN
1153 IF CompilingDefinitionModule () AND (NOT IsDefImp (scope))
1154 THEN
1155 MetaError1 ('module scope should be a definition module rather than {%1EDa}', scope)
1156 ELSE
1157 INC (importStatementCount) ;
1158 AppendModuleImportStatement (scope, MakeImportStatement (tok, importStatementCount))
1159 END
1160 END
1161 END BuildImportStatement ;
1162
1163
1164 (*
1165 AddImportToImportStatement - the top of stack is expected to be a module name.
1166 This is looked up from the module universe and
1167 wrapped in an import symbol and placed into the
1168 current import statement.
1169
1170 The quadruple stack is unchanged.
1171
1172 Entry Exit
1173
1174
1175 Ptr -> <- Ptr
1176 +---------------------+ +---------------------+
1177 | ImportedModuleName | | ImportedModuleName |
1178 |---------------------| |---------------------|
1179 *)
1180
1181 PROCEDURE AddImportToImportStatement (qualified: BOOLEAN) ;
1182 VAR
1183 scope: CARDINAL ;
1184 BEGIN
1185 scope := GetCurrentScope () ;
1186 IF IsDefImp (scope) OR (IsModule (scope) AND (NOT IsInnerModule (scope)))
1187 THEN
1188 IF CompilingDefinitionModule () AND (NOT IsDefImp (scope))
1189 THEN
1190 MetaError1 ('module scope should be a definition module rather than {%1EDa}', scope) ;
1191 ELSE
1192 AppendModuleOnImportStatement (scope, MakeImport (OperandTok (1),
1193 LookupModule (OperandTok (1), OperandT (1)),
1194 importStatementCount, qualified))
1195 END
1196 END
1197 END AddImportToImportStatement ;
1198
1199
1200 END P1SymBuild.