Debian Codegraph @

gcc/m2/gm2-compiler/P2SymBuild.mod

raw
1 (* P2SymBuild.mod pass 2 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 P2SymBuild ;
23
24
25 FROM libc IMPORT strlen ;
26 FROM NameKey IMPORT Name, MakeKey, makekey, KeyToCharStar, NulName, LengthKey, WriteKey ;
27 FROM StrLib IMPORT StrEqual ;
28 FROM M2Debug IMPORT Assert, WriteDebug ;
29 FROM M2LexBuf IMPORT UnknownTokenNo, GetTokenNo, MakeVirtual2Tok ;
30 FROM M2Error IMPORT InternalError, WriteFormat1, WriteFormat2, WriteFormat0, ErrorStringAt, ErrorStringAt2, WarnStringAt ;
31 FROM DynamicStrings IMPORT String, InitString, InitStringCharStar, Mark, Slice, ConCat, KillString, string ;
32 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3, Sprintf4 ;
33 FROM M2Printf IMPORT printf0, printf1, printf2, printf3 ;
34 FROM M2StackWord IMPORT StackOfWord, InitStackWord, PushWord, PopWord ;
35 FROM M2Options IMPORT PedanticParamNames, ExtendedOpaque ;
36 FROM StrIO IMPORT WriteString, WriteLn ;
37 FROM M2Base IMPORT ZType ;
38 FROM Storage IMPORT ALLOCATE ;
39 FROM gcctypes IMPORT location_t ;
40 FROM M2LexBuf IMPORT TokenToLocation ;
41
42 FROM M2Reserved IMPORT ImportTok, ExportTok, QualifiedTok, UnQualifiedTok,
43 NulTok, VarTok, ArrayTok ;
44
45 FROM M2MetaError IMPORT MetaError1, MetaError2, MetaError3,
46 MetaErrorsT2, MetaErrors1, MetaErrorT1,
47 MetaErrors2, MetaErrorString1, MetaErrorStringT1,
48 MetaErrorString3, MetaErrorStringT3 ;
49
50 FROM FifoQueue IMPORT GetEnumerationFromFifoQueue, PutSubrangeIntoFifoQueue,
51 PutConstructorIntoFifoQueue, PutConstIntoFifoQueue ;
52
53 FROM SymbolTable IMPORT NulSym,
54 ModeOfAddr, ProcedureKind,
55 StartScope, EndScope, PseudoScope,
56 GetCurrentScope, GetScope,
57 IsDeclaredIn,
58 SetCurrentModule, SetFileModule,
59 GetCurrentModule, GetMainModule,
60 MakeTemporary, CheckAnonymous, IsNameAnonymous,
61 MakeConstLit,
62 MakeConstString,
63 MakeSubrange,
64 MakeVar, MakeType, PutType,
65 MakeModuleCtor,
66 PutMode, PutDeclared, GetParameterShadowVar,
67 PutFieldEnumeration, PutSubrange, PutVar, PutVarTok, PutConst,
68 PutConstSet, PutConstructor,
69 IsDefImp, IsType, IsRecord, IsRecordField, IsPointer,
70 IsSubrange, IsEnumeration, IsConstString,
71 IsError, IsAModula2Type, IsParameterVar, IsParameterUnbounded,
72 GetSym, GetDeclareSym, IsUnknown, RenameSym,
73 GetLocalSym, GetParent, IsRecord, GetRecord,
74 GetFromOuterModule,
75 GetExported,
76 PutExported, PutExportQualified, PutExportUnQualified,
77 PutExportUnImplemented,
78 PutFieldVarient, PutVarientTag,
79 IsFieldVarient, IsVarient,
80 CheckForEnumerationInCurrentModule,
81 CheckForExportedImplementation,
82 MakeProcedure,
83 PutFunction, PutOptFunction,
84 PutParam, PutVarParam,
85 GetNthParam,
86 IsProcedure,
87 NoOfElements,
88 MakePointer, PutPointer,
89 MakeSet, PutSet,
90 MakeRecord, PutFieldRecord,
91 MakeVarient, MakeFieldVarient,
92 MakeArray, PutArraySubscript,
93 MakeSubscript, PutSubscript,
94 MakeError,
95 PutConstStringKnown, GetString,
96 PutArray, IsArray,
97 GetType, SkipType,
98 IsProcType, MakeProcType,
99 PutProcTypeVarParam, PutProcTypeParam,
100 MakeConstVar,
101 PutVariableAtAddress, IsVariableAtAddress,
102 GetAlignment, PutAlignment,
103 PutDefaultRecordFieldAlignment,
104 GetDefaultRecordFieldAlignment,
105 PutUnused,
106 MakeUnbounded, IsUnbounded,
107 NoOfParam,
108 PutParamName,
109 GetParam, GetDimension,
110 PutProcedureParametersDefined,
111 GetProcedureParametersDefined,
112 PutProcedureParameterHeapVars,
113 CheckForUnImplementedExports,
114 CheckForUndeclaredExports,
115 IsHiddenTypeDeclared,
116 IsUnboundedParam,
117 IsVarParam,
118 PutUseVarArgs,
119 UsesVarArgs,
120 PutUseOptArg,
121 UsesOptArg,
122 IsDefinitionForC,
123 GetSymName,
124 GetDeclaredDef, GetDeclaredMod,
125 RequestSym,
126 PutDeclared,
127 GetPackedEquivalent,
128 GetVarDeclTok,
129 PutVarDeclTok,
130 GetVarDeclTypeTok,
131 GetProcedureKindDesc,
132 GetProcedureDeclaredTok,
133 GetProcedureKind,
134 GetReturnTypeTok,
135 SetReturnOptional,
136 IsReturnOptional,
137 PutProcedureNoReturn,
138 PutProcedureDefined,
139 DisplayTrees ;
140
141 FROM M2Batch IMPORT MakeDefinitionSource,
142 MakeImplementationSource,
143 MakeProgramSource,
144 LookupModule, LookupOuterModule ;
145
146 FROM M2Quads IMPORT PushT, PopT,
147 PushTF, PopTF, PopTtok, PushTFtok, PushTtok, PopTFtok,
148 OperandT, OperandF, OperandA, OperandTok, PopN, DisplayStack, Annotate,
149 AddVarientFieldToList, Top ;
150
151 FROM M2Comp IMPORT CompilingDefinitionModule,
152 CompilingImplementationModule,
153 CompilingProgramModule ;
154
155 FROM M2Const IMPORT constType ;
156 FROM M2Students IMPORT CheckVariableAgainstKeyword ;
157 IMPORT M2Error ;
158
159
160 CONST
161 Debugging = FALSE ;
162
163 VAR
164 alignTypeNo : CARDINAL ;
165 castType : CARDINAL ;
166 type : constType ;
167 RememberedConstant: CARDINAL ;
168 RememberStack,
169 TypeStack : StackOfWord ;
170 curModuleSym : CARDINAL ;
171 curBeginTok,
172 curFinallyTok,
173 curStartTok,
174 curEndTok : CARDINAL ;
175 BlockStack : StackOfWord ;
176
177
178 PROCEDURE stop ; BEGIN END stop ;
179
180
181 (*
182 Debug - call stop if symbol name is name.
183 *)
184
185 (*
186 PROCEDURE Debug (tok: CARDINAL; sym: CARDINAL; name: ARRAY OF CHAR) ;
187 BEGIN
188 IF MakeKey (name) = GetSymName (sym)
189 THEN
190 stop
191 END ;
192 MetaErrorT1 (tok, 'procedure {%1Wa}', sym)
193 END Debug ;
194 *)
195
196
197 (*
198 BlockStart - tokno is the module/procedure/implementation/definition token
199 *)
200
201 PROCEDURE BlockStart (tokno: CARDINAL) ;
202 BEGIN
203 PushBlock (tokno) ;
204 END BlockStart ;
205
206
207 (*
208 propageteTokenPosition - if laterTokPos is unknown then return knownTokPos.
209 else return laterTokPos.
210 *)
211
212 PROCEDURE propageteTokenPosition (knownTokPos, laterTokPos: CARDINAL) : CARDINAL ;
213 BEGIN
214 IF laterTokPos = UnknownTokenNo
215 THEN
216 RETURN knownTokPos
217 ELSE
218 RETURN laterTokPos
219 END
220 END propageteTokenPosition ;
221
222
223 (*
224 BlockEnd - declare module ctor/init/fini/dep procedures.
225 *)
226
227 PROCEDURE BlockEnd (tokno: CARDINAL) ;
228 BEGIN
229 curBeginTok := propageteTokenPosition (curStartTok, curBeginTok) ;
230 curFinallyTok := propageteTokenPosition (tokno, curFinallyTok) ;
231 Assert (curModuleSym # NulSym) ;
232 MakeModuleCtor (curStartTok, curBeginTok, curFinallyTok,
233 curModuleSym) ;
234 PopBlock
235 END BlockEnd ;
236
237
238 (*
239 BlockBegin - assign curBeginTok to tokno.
240 *)
241
242 PROCEDURE BlockBegin (tokno: CARDINAL) ;
243 BEGIN
244 curBeginTok := tokno
245 END BlockBegin ;
246
247
248 (*
249 BlockFinally - assign curFinallyTok to tokno.
250 *)
251
252 PROCEDURE BlockFinally (tokno: CARDINAL) ;
253 BEGIN
254 curFinallyTok := tokno
255 END BlockFinally ;
256
257
258 (*
259 PushBlock - push the block variables to the block stack.
260 *)
261
262 PROCEDURE PushBlock (tokno: CARDINAL) ;
263 BEGIN
264 PushWord (BlockStack, curStartTok) ; (* module/implementation/definition/procedure token pos. *)
265 PushWord (BlockStack, curBeginTok) ; (* BEGIN keyword pos. *)
266 PushWord (BlockStack, curEndTok) ; (* END keyword pos. *)
267 PushWord (BlockStack, curFinallyTok) ; (* FINALLY keyword pos. *)
268 PushWord (BlockStack, curModuleSym) ; (* current module. *)
269 curStartTok := tokno ;
270 curBeginTok := UnknownTokenNo ;
271 curEndTok := UnknownTokenNo ;
272 curFinallyTok := UnknownTokenNo ;
273 curModuleSym := NulSym
274 END PushBlock ;
275
276
277 (*
278 PopBlock - pop the block variables from the block stack.
279 *)
280
281 PROCEDURE PopBlock ;
282 BEGIN
283 curModuleSym := PopWord (BlockStack) ;
284 curFinallyTok := PopWord (BlockStack) ;
285 curEndTok := PopWord (BlockStack) ;
286 curBeginTok := PopWord (BlockStack) ;
287 curStartTok := PopWord (BlockStack)
288 END PopBlock ;
289
290
291 (*
292 StartBuildDefinitionModule - Creates a definition module and starts
293 a new scope.
294
295 he Stack is expected:
296
297 Entry Exit
298
299 Ptr -> <- Ptr
300 +------------+ +-----------+
301 | NameStart | | NameStart |
302 |------------| |-----------|
303
304 *)
305
306 PROCEDURE P2StartBuildDefModule ;
307 VAR
308 name : Name ;
309 ModuleSym: CARDINAL ;
310 tokno : CARDINAL ;
311 BEGIN
312 PopTtok (name, tokno) ;
313 ModuleSym := MakeDefinitionSource(tokno, name) ;
314 curModuleSym := ModuleSym ;
315 SetCurrentModule(ModuleSym) ;
316 SetFileModule(ModuleSym) ;
317 StartScope(ModuleSym) ;
318 Assert(IsDefImp(ModuleSym)) ;
319 Assert(CompilingDefinitionModule()) ;
320 PushT(name) ;
321 Annotate("%1n||definition module name") ;
322 M2Error.EnterDefinitionScope (name)
323 END P2StartBuildDefModule ;
324
325
326 (*
327 EndBuildDefinitionModule - Destroys the definition module scope and
328 checks for correct name.
329
330 The Stack is expected:
331
332 Entry Exit
333
334 Ptr ->
335 +------------+ +-----------+
336 | NameEnd | | |
337 |------------| |-----------|
338 | NameStart | | | <- Ptr
339 |------------| |-----------|
340 *)
341
342 PROCEDURE P2EndBuildDefModule ;
343 VAR
344 NameStart,
345 NameEnd : Name ;
346 BEGIN
347 Assert(CompilingDefinitionModule()) ;
348 CheckForUndeclaredExports(GetCurrentModule()) ;
349 EndScope ;
350 PopT(NameStart) ;
351 PopT(NameEnd) ;
352 IF Debugging
353 THEN
354 printf0('pass 2: ') ;
355 DisplayTrees(GetCurrentModule())
356 END ;
357 IF NameStart#NameEnd
358 THEN
359 WriteFormat2('inconsistent definition module name, module began as (%a) and ended with (%a)', NameStart, NameEnd)
360 END ;
361 M2Error.LeaveErrorScope
362 END P2EndBuildDefModule ;
363
364
365 (*
366 StartBuildImplementationModule - Creates an implementation module and starts
367 a new scope.
368
369 The Stack is expected:
370
371 Entry Exit
372
373 Ptr -> <- Ptr
374 +------------+ +-----------+
375 | NameStart | | NameStart |
376 |------------| |-----------|
377
378 *)
379
380 PROCEDURE P2StartBuildImplementationModule ;
381 VAR
382 name : Name ;
383 ModuleSym: CARDINAL ;
384 tokno : CARDINAL ;
385 BEGIN
386 PopTtok (name, tokno) ;
387 ModuleSym := MakeImplementationSource(tokno, name) ;
388 curModuleSym := ModuleSym ;
389 SetCurrentModule(ModuleSym) ;
390 SetFileModule(ModuleSym) ;
391 StartScope(ModuleSym) ;
392 Assert(IsDefImp(ModuleSym)) ;
393 Assert(CompilingImplementationModule()) ;
394 PushT(name) ;
395 Annotate("%1n||implementation module name") ;
396 M2Error.EnterImplementationScope (name)
397 END P2StartBuildImplementationModule ;
398
399
400 (*
401 EndBuildImplementationModule - Destroys the implementation module scope and
402 checks for correct name.
403
404 The Stack is expected:
405
406 Entry Exit
407
408 Ptr ->
409 +------------+ +-----------+
410 | NameEnd | | |
411 |------------| |-----------|
412 | NameStart | | | <- Ptr
413 |------------| |-----------|
414 *)
415
416 PROCEDURE P2EndBuildImplementationModule ;
417 VAR
418 NameStart,
419 NameEnd : Name ;
420 BEGIN
421 Assert(CompilingImplementationModule()) ;
422 CheckForUnImplementedExports ;
423 EndScope ;
424 PopT (NameStart) ;
425 PopT (NameEnd) ;
426 IF NameStart#NameEnd
427 THEN
428 WriteFormat1('inconsistent implementation module name %a', NameStart)
429 END ;
430 M2Error.LeaveErrorScope
431 END P2EndBuildImplementationModule ;
432
433
434 (*
435 StartBuildProgramModule - Creates a program module and starts
436 a new scope.
437
438 The Stack is expected:
439
440 Entry Exit
441
442 Ptr -> <- Ptr
443 +------------+ +-----------+
444 | NameStart | | NameStart |
445 |------------| |-----------|
446
447 *)
448
449 PROCEDURE P2StartBuildProgramModule ;
450 VAR
451 name : Name ;
452 ModuleSym: CARDINAL ;
453 tokno : CARDINAL ;
454 BEGIN
455 PopTtok (name, tokno) ;
456 ModuleSym := MakeProgramSource(tokno, name) ;
457 curModuleSym := ModuleSym ;
458 SetCurrentModule(ModuleSym) ;
459 SetFileModule(ModuleSym) ;
460 StartScope(ModuleSym) ;
461 Assert(CompilingProgramModule()) ;
462 Assert(NOT IsDefImp(ModuleSym)) ;
463 PushT(name) ;
464 Annotate("%1n||program module name") ;
465 M2Error.EnterProgramScope (name)
466 END P2StartBuildProgramModule ;
467
468
469 (*
470 EndBuildProgramModule - Destroys the program module scope and
471 checks for correct name.
472
473 The Stack is expected:
474
475 Entry Exit
476
477 Ptr ->
478 +------------+ +-----------+
479 | NameEnd | | |
480 |------------| |-----------|
481 | NameStart | | | <- Ptr
482 |------------| |-----------|
483 *)
484
485 PROCEDURE P2EndBuildProgramModule ;
486 VAR
487 NameStart,
488 NameEnd : Name ;
489 BEGIN
490 Assert(CompilingProgramModule()) ;
491 CheckForUndeclaredExports(GetCurrentModule()) ; (* Not really allowed exports here though! *)
492 EndScope ;
493 PopT (NameStart) ;
494 PopT (NameEnd) ;
495 IF Debugging
496 THEN
497 printf0('pass 2: ') ;
498 DisplayTrees(GetCurrentModule())
499 END ;
500 IF NameStart#NameEnd
501 THEN
502 WriteFormat2('inconsistent program module name %a does not match %a', NameStart, NameEnd)
503 END ;
504 M2Error.LeaveErrorScope
505 END P2EndBuildProgramModule ;
506
507
508 (*
509 StartBuildInnerModule - Creates an Inner module and starts
510 a new scope.
511
512 The Stack is expected:
513
514 Entry Exit
515
516 Ptr -> <- Ptr
517 +------------+ +-----------+
518 | NameStart | | NameStart |
519 |------------| |-----------|
520
521 *)
522
523 PROCEDURE StartBuildInnerModule ;
524 VAR
525 name : Name ;
526 tok : CARDINAL ;
527 ModuleSym: CARDINAL ;
528 BEGIN
529 PopTtok (name, tok) ;
530 ModuleSym := GetDeclareSym (tok, name) ;
531 curModuleSym := ModuleSym ;
532 StartScope (ModuleSym) ;
533 Assert(NOT IsDefImp (ModuleSym)) ;
534 PushTtok (name, tok) ;
535 Annotate ("%1n||inner module name") ;
536 M2Error.EnterModuleScope (name)
537 END StartBuildInnerModule ;
538
539
540 (*
541 EndBuildInnerModule - Destroys the Inner module scope and
542 checks for correct name.
543
544 The Stack is expected:
545
546 Entry Exit
547
548 Ptr ->
549 +------------+ +-----------+
550 | NameEnd | | |
551 |------------| |-----------|
552 | NameStart | | | <- Ptr
553 |------------| |-----------|
554 *)
555
556 PROCEDURE EndBuildInnerModule ;
557 VAR
558 NameStart,
559 NameEnd : Name ;
560 BEGIN
561 CheckForUndeclaredExports(GetCurrentModule()) ;
562 EndScope ;
563 PopT(NameStart) ;
564 PopT(NameEnd) ;
565 IF NameStart#NameEnd
566 THEN
567 WriteFormat2('inconsistent inner module name %a does not match %a',
568 NameStart, NameEnd)
569 END ;
570 M2Error.LeaveErrorScope
571 END EndBuildInnerModule ;
572
573
574 (*
575 BuildImportOuterModule - Builds imported identifiers into an outer module
576 from a definition module.
577
578 The Stack is expected:
579
580 Entry OR Entry
581
582 Ptr -> Ptr ->
583 +------------+ +-----------+
584 | # | | # |
585 |------------| |-----------|
586 | Id1 | | Id1 |
587 |------------| |-----------|
588 . . . .
589 . . . .
590 . . . .
591 |------------| |-----------|
592 | Id# | | Id# |
593 |------------| |-----------|
594 | ImportTok | | Ident |
595 |------------| |-----------|
596
597 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
598
599
600 Exit
601
602 All above stack discarded
603 *)
604
605 PROCEDURE BuildImportOuterModule ;
606 VAR
607 Sym, ModSym,
608 i, n : CARDINAL ;
609 BEGIN
610 PopT (n) ; (* n = # of the Ident List *)
611 IF OperandT (n+1) # ImportTok
612 THEN
613 (* Ident List contains list of objects imported from ModSym *)
614 ModSym := LookupModule (OperandTok(n+1), OperandT (n+1)) ;
615 i := 1 ;
616 WHILE i<=n DO
617 Sym := GetExported (OperandTok(i), ModSym, OperandT (i)) ;
618 CheckForEnumerationInCurrentModule (Sym) ;
619 INC (i)
620 END
621 END ;
622 PopN (n+1) (* clear stack *)
623 END BuildImportOuterModule ;
624
625
626 (*
627 BuildExportOuterModule - Builds exported identifiers from an outer module
628 to the outside world of library modules.
629
630 The Stack is expected:
631
632 Entry OR Entry
633
634 Ptr -> Ptr ->
635 +------------+ +--------------+
636 | # | | # |
637 |------------| |--------------|
638 | Id1 | | Id1 |
639 |------------| |--------------|
640 . . . .
641 . . . .
642 . . . .
643 |------------| |--------------|
644 | Id# | | Id# |
645 |------------| |--------------|
646 | ExportTok | | QualifiedTok |
647 |------------| |--------------|
648
649 EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
650
651 Error Condition
652
653
654 Exit
655
656 All above stack discarded
657 *)
658
659 PROCEDURE BuildExportOuterModule ;
660 VAR
661 n: CARDINAL ;
662 BEGIN
663 PopT(n) ; (* n = # of the Ident List *)
664 PopN(n+1)
665 END BuildExportOuterModule ;
666
667
668 (*
669 BuildImportInnerModule - Builds imported identifiers into an inner module
670 from the last level of module.
671
672 The Stack is expected:
673
674 Entry OR Entry
675
676 Ptr -> Ptr ->
677 +------------+ +-----------+
678 | # | | # |
679 |------------| |-----------|
680 | Id1 | | Id1 |
681 |------------| |-----------|
682 . . . .
683 . . . .
684 . . . .
685 |------------| |-----------|
686 | Id# | | Id# |
687 |------------| |-----------|
688 | ImportTok | | Ident |
689 |------------| |-----------|
690
691 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
692
693 Exit
694
695 All above stack discarded
696 *)
697
698 PROCEDURE BuildImportInnerModule ;
699 VAR
700 Sym, ModSym,
701 n, i : CARDINAL ;
702 BEGIN
703 PopT (n) ; (* i = # of the Ident List *)
704 IF OperandT(n+1)=ImportTok
705 THEN
706 (* Ident List contains list of objects *)
707 i := 1 ;
708 WHILE i<=n DO
709 Sym := GetFromOuterModule (OperandTok (i), OperandT (i)) ;
710 CheckForEnumerationInCurrentModule (Sym) ;
711 INC (i)
712 END
713 ELSE
714 (* Ident List contains list of objects from ModSym *)
715 ModSym := LookupOuterModule(OperandTok(n+1), OperandT(n+1)) ;
716 i := 1 ;
717 WHILE i<=n DO
718 Sym := GetExported (OperandTok (i), ModSym, OperandT(i)) ;
719 CheckForEnumerationInCurrentModule (Sym) ;
720 INC (i)
721 END
722 END ;
723 PopN (n+1) (* Clear Stack *)
724 END BuildImportInnerModule ;
725
726
727 (*
728 BuildExportInnerModule - Builds exported identifiers from an inner module
729 to the next layer module.
730
731 The Stack is expected:
732
733 Entry OR Entry
734
735 Ptr -> Ptr ->
736 +------------+ +--------------+
737 | # | | # |
738 |------------| |--------------|
739 | Id1 | | Id1 |
740 |------------| |--------------|
741 . . . .
742 . . . .
743 . . . .
744 |------------| |--------------|
745 | Id# | | Id# |
746 |------------| |--------------|
747 | ExportTok | | QualifiedTok |
748 |------------| |--------------|
749
750 EXPORT Id1, .. Id# ; EXPORT QUALIFIED Id1 .. Id# ;
751
752
753 Exit
754
755 All above stack discarded
756 *)
757
758 PROCEDURE BuildExportInnerModule ;
759 VAR
760 n: CARDINAL ;
761 BEGIN
762 PopT(n) ;
763 PopN(n+1) (* clear stack *)
764 END BuildExportInnerModule ;
765
766
767 (*
768 BuildNumber - Converts a number into a symbol.
769
770
771 Stack
772
773 Entry Exit
774
775 Ptr -> <- Ptr
776 +------------+ +-------------------+
777 | Name | tok | | Sym | Type | tok |
778 |------------+ |-------------------|
779 *)
780
781 PROCEDURE BuildNumber ;
782 VAR
783 name: Name ;
784 Sym : CARDINAL ;
785 tok : CARDINAL ;
786 BEGIN
787 PopTtok (name, tok) ;
788 Sym := MakeConstLit (tok, name, NulSym) ;
789 PushTFtok (Sym, GetType (Sym), tok) ;
790 Annotate ("%1s(%1d)||constant number")
791 END BuildNumber ;
792
793
794 (*
795 BuildString - Converts a string into a symbol.
796
797
798 Stack
799
800 Entry Exit
801
802 Ptr -> <- Ptr
803 +-------------+ +--------------------+
804 | Name | | tok| | Sym | NulSym | tok |
805 |-------------+ |--------------------|
806 *)
807
808 PROCEDURE BuildString ;
809 VAR
810 name: Name ;
811 Sym : CARDINAL ;
812 tok : CARDINAL ;
813 BEGIN
814 PopTtok (name, tok) ;
815 (* slice off the leading and trailing quotes *)
816 IF name = 1140
817 THEN
818 stop
819 END ;
820 Sym := MakeConstString (tok, makekey (string (Mark (Slice (Mark (InitStringCharStar (KeyToCharStar (name))), 1, -1))))) ;
821 PushTFtok (Sym, NulSym, tok) ;
822 Annotate ("%1s(%1d)|%3d||constant string")
823 END BuildString ;
824
825
826 (*
827 BuildConst - builds a constant.
828 Stack
829
830 Entry Exit
831
832 Ptr ->
833 +------------+
834 | Name |
835 |------------+ <- Ptr
836 *)
837
838 PROCEDURE BuildConst ;
839 VAR
840 name: Name ;
841 sym : CARDINAL ;
842 tok : CARDINAL ;
843 BEGIN
844 PopTtok(name, tok) ;
845 sym := MakeConstVar(tok, name) ;
846 PushTtok(sym, tok) ;
847 RememberConstant(sym) ;
848 Annotate("%1s(%1d)|%3d||remembered constant")
849 END BuildConst ;
850
851
852 (*
853 StartBuildEnumeration - Builds an Enumeration type Type.
854
855
856 Stack
857
858 Entry Exit
859
860 Ptr ->
861 +------------+
862 | # |
863 |------------|
864 | en 1 |
865 |------------|
866 | en 2 |
867 |------------|
868 . .
869 . .
870 . . <- Ptr
871 |------------| +------------+
872 | en # | | Type |
873 |------------| |------------|
874 | Name | | Name |
875 |------------| |------------|
876 *)
877
878 PROCEDURE StartBuildEnumeration ;
879 VAR
880 n,
881 Type: CARDINAL ;
882 tok : CARDINAL ;
883 BEGIN
884 PopT (n) ; (* n := # *)
885 (* name is in OperandT(n+1) but we dont need it here. *)
886 tok := OperandTok (n+1) ;
887 GetEnumerationFromFifoQueue (Type) ;
888 CheckForExportedImplementation (Type) ; (* May be an exported hidden type *)
889 PopN (n) ;
890 PushTtok (Type, tok) ;
891 Annotate ("%1s(%1d)|%3d||enumerated type")
892 END StartBuildEnumeration ;
893
894
895 (*
896 BuildSubrange - Builds a Subrange type Symbol, the base type can also be
897 supplied if known.
898
899 Stack
900
901 Entry Exit
902
903
904 <- Ptr
905 +------------+
906 Ptr -> | Type |
907 +------------+ |------------|
908 | Name | | Name |
909 |------------| |------------|
910 *)
911
912 PROCEDURE BuildSubrange (tok: CARDINAL; Base: CARDINAL) ;
913 VAR
914 name: Name ;
915 Type: CARDINAL ;
916 BEGIN
917 PopT (name) ;
918 Type := MakeSubrange (tok, name) ;
919 PutSubrangeIntoFifoQueue(Type) ; (* Store Subrange away so that we can fill in *)
920 (* its bounds during pass 3. *)
921 PutSubrangeIntoFifoQueue(Base) ; (* store Base type of subrange away as well. *)
922 CheckForExportedImplementation(Type) ; (* May be an exported hidden type *)
923 PushTtok(name, tok) ;
924 Annotate("%1n|%3d||subrange name|token no") ;
925 PushTtok(Type, tok) ;
926 Annotate("%1s(%1d)|%3d||subrange type|token no")
927 END BuildSubrange ;
928
929
930 (*
931 BuildDefaultFieldAlignment -
932
933 The Stack:
934
935 Entry Exit
936 ===== ====
937
938
939 Ptr ->
940 +-----------+
941 | Alignment |
942 |-----------| +-----------+
943 | RecordSym | | RecordSym |
944 |-----------| |-----------|
945 | Name | | Name |
946 |-----------| |-----------|
947
948 *)
949
950 PROCEDURE P2BuildDefaultFieldAlignment ;
951 VAR
952 tok : CARDINAL ;
953 alignment: Name ;
954 align : CARDINAL ;
955 BEGIN
956 PopTtok(alignment, tok) ;
957 align := MakeTemporary(tok, ImmediateValue) ;
958 PutConst(align, ZType) ;
959 PutConstIntoFifoQueue(align) ; (* store align away ready for pass 3 *)
960 PutDefaultRecordFieldAlignment(OperandT(1), align)
961 END P2BuildDefaultFieldAlignment ;
962
963
964 (*
965 BuildPragmaConst - pushes a constant to the stack and stores it away into the
966 const fifo queue ready for pass 3.
967 *)
968
969 PROCEDURE BuildPragmaConst ;
970 VAR
971 value : CARDINAL ;
972 BEGIN
973 value := MakeTemporary(GetTokenNo (), ImmediateValue) ;
974 PutConst(value, ZType) ;
975 PutConstIntoFifoQueue(value) ; (* Store value away so that we can fill it in *)
976 PushT(value) ; (* during pass 3. *)
977 Annotate("%1s(%1d)||pragma constant")
978 END BuildPragmaConst ;
979
980
981 (*
982 BuildAligned - builds an alignment constant symbol which is placed onto
983 the stack. It expects the ident ALIGNED to be on the
984 stack.
985
986 Stack
987
988 Entry Exit
989
990
991 Ptr -> <- Ptr
992 +---------------+ +-----------------+
993 | bytealignment | | AlignmentConst |
994 +---------------+ |-----------------|
995 *)
996
997 PROCEDURE BuildAligned ;
998 VAR
999 tok : CARDINAL ;
1000 name : Name ;
1001 align: CARDINAL ;
1002 BEGIN
1003 PopTtok (name, tok) ;
1004 IF name=MakeKey('bytealignment')
1005 THEN
1006 align := MakeTemporary (tok, ImmediateValue) ;
1007 PutConst(align, ZType) ;
1008 PutConstIntoFifoQueue(align) ; (* Store align away so that we can fill in its *)
1009 PushT(align) ; (* value during pass 3. *)
1010 Annotate("%1s(%1d)|%3d||bytealignment constant generated from <* *>|token no") ;
1011 PushTtok(name, tok)
1012 ELSE
1013 WriteFormat1('expecting bytealignment identifier, rather than %a', name) ;
1014 PushT(NulSym)
1015 END ;
1016 Annotate("%1n(%1d)||bytealignment constant generated from <* *>")
1017 END BuildAligned ;
1018
1019
1020 (*
1021 BuildTypeAlignment - the AlignmentConst is either a temporary or NulSym.
1022 In the case of NulSym it is popped from the stack
1023 and the procedure returns. Otherwise the temporary
1024 is popped and recorded as the alignment value for this
1025 type. A type may only have one alignment value and
1026 error checking is performed.
1027
1028 Stack
1029
1030 Entry Exit
1031
1032
1033 Ptr ->
1034 +-----------------+
1035 | AlignmentConst |
1036 |-----------------|
1037 | Type | Empty
1038 |-----------------|
1039 *)
1040
1041 PROCEDURE BuildTypeAlignment ;
1042 VAR
1043 alignment: Name ;
1044 type,
1045 align : CARDINAL ;
1046 BEGIN
1047 PopT (alignment) ;
1048 IF alignment = MakeKey ('bytealignment')
1049 THEN
1050 PopT (align) ;
1051 PopT (type) ;
1052 IF align # NulSym
1053 THEN
1054 IF IsRecord (type) OR IsRecordField (type) OR IsType (type) OR
1055 IsArray (type) OR IsPointer( type) OR IsSubrange (type)
1056 THEN
1057 PutAlignment (type, align)
1058 ELSE
1059 MetaError1 ('not allowed to add an alignment attribute to type {%1ad}', type)
1060 END
1061 END
1062 ELSIF alignment # NulName
1063 THEN
1064 WriteFormat1 ('unknown type alignment attribute, %a', alignment)
1065 ELSE
1066 PopT (type)
1067 END
1068 END BuildTypeAlignment ;
1069
1070
1071 (*
1072 BuildVarAlignment - the AlignmentConst is either a temporary or NulSym.
1073 A type may only have one alignment value and
1074 error checking is performed.
1075
1076 Stack
1077
1078 Entry Exit
1079
1080
1081 Ptr ->
1082 +-----------------+
1083 | AlignmentConst | <- Ptr
1084 |-----------------| +------------------+
1085 | Type | | Type | TypeName |
1086 |-----------------| |------------------|
1087 *)
1088
1089 PROCEDURE BuildVarAlignment ;
1090 VAR
1091 tokno : CARDINAL ;
1092 alignment,
1093 newname : Name ;
1094 new,
1095 type,
1096 align : CARDINAL ;
1097 s : String ;
1098 BEGIN
1099 PopT(alignment) ;
1100 IF alignment=MakeKey('bytealignment')
1101 THEN
1102 PopT(align) ;
1103 PopTtok(type, tokno) ;
1104 IF IsRecord(type) OR IsRecordField(type) OR IsType(type) OR IsArray(type) OR IsPointer(type)
1105 THEN
1106 stop ;
1107 IF IsNameAnonymous(type)
1108 THEN
1109 PutAlignment(type, align) ;
1110 PushTFtok(type, GetSymName(type), tokno) ;
1111 Annotate("%1s(%1d)|%2n|%3d||aligned type|aligned type name|token no")
1112 ELSE
1113 (* create a pseudonym *)
1114 s := Sprintf1(Mark(InitString('_$A%d')), alignTypeNo) ;
1115 INC(alignTypeNo) ;
1116 newname := makekey(string(s)) ;
1117 IF IsPointer(type)
1118 THEN
1119 new := MakePointer(tokno, newname)
1120 ELSE
1121 new := MakeType(tokno, newname)
1122 END ;
1123 s := KillString(s) ;
1124 PutType(new, type) ;
1125 PutAlignment(new, align) ;
1126 PushTFtok(new, GetSymName(new), tokno) ;
1127 Annotate("%1s(%1d)|%2n|%3d||aligned type|aligned type name")
1128 END
1129 ELSE
1130 MetaError1('not allowed to add an alignment attribute to type {%1ad}', type) ;
1131 PushTFtok(type, GetSymName(type), tokno) ;
1132 Annotate("%1s(%1d)|%2n|%3d||error aligned type|error aligned type name")
1133 END
1134 ELSIF alignment#NulName
1135 THEN
1136 WriteFormat1('unknown variable alignment attribute, %a', alignment)
1137 END
1138 END BuildVarAlignment ;
1139
1140
1141 (*
1142 BuildVariable - Builds variables listed in an IdentList with a Type.
1143
1144 Stack
1145
1146 Entry Exit
1147
1148 Ptr ->
1149 +------------+ +------------+
1150 | Type | Name| | |
1151 |------------| |------------|
1152 | # | | |
1153 |------------| |------------|
1154 | Ident 1 | | |
1155 |------------| |------------|
1156 | Ident 2 | | |
1157 |------------| |------------|
1158 . . . .
1159 . . . .
1160 . . . .
1161 |------------| |------------|
1162 | Ident # | | | <- Ptr
1163 |------------| |------------|
1164
1165 Empty
1166 *)
1167
1168 PROCEDURE BuildVariable ;
1169 VAR
1170 name : Name ;
1171 tok,
1172 typetok,
1173 AtAddress,
1174 Type,
1175 Var,
1176 i, n : CARDINAL ;
1177 BEGIN
1178 PopTFtok (Type, name, typetok) ;
1179 PopT (n) ;
1180 i := 1 ;
1181 WHILE i <= n DO
1182 CheckVariableAgainstKeyword (OperandT (n+1-i)) ;
1183 tok := OperandTok (n+1-i) ;
1184 Var := MakeVar (tok, OperandT (n+1-i)) ;
1185 AtAddress := OperandA (n+1-i) ;
1186 IF AtAddress # NulSym
1187 THEN
1188 PutVariableAtAddress (Var, NulSym) ;
1189 PutMode (Var, LeftValue)
1190 END ;
1191 PutVarTok (Var, Type, typetok) ;
1192 IF tok # UnknownTokenNo
1193 THEN
1194 PutDeclared (tok, Var) ;
1195 PutVarDeclTok (Var, tok)
1196 END ;
1197 INC (i)
1198 END ;
1199 PopN (n)
1200 END BuildVariable ;
1201
1202
1203 (*
1204 BuildType - Builds a Type.
1205
1206
1207 Stack
1208
1209 Entry Exit
1210
1211 Ptr ->
1212 +------------+
1213 | Type | <- Ptr
1214 |------------| +---------------+
1215 | Name | | Type | Name |
1216 |------------| |---------------|
1217
1218 Empty
1219 *)
1220
1221 PROCEDURE BuildType ;
1222 VAR
1223 isunknown: BOOLEAN ;
1224 n1, n2 : Name ;
1225 Sym,
1226 Type : CARDINAL ;
1227 name : Name ;
1228 nametokno,
1229 typetokno: CARDINAL ;
1230 BEGIN
1231 (*
1232 Two cases
1233
1234 - the type name the same as Name, or the name is nul. - do nothing.
1235 - when type with a name that is different to Name. In which case
1236 we create a new type.
1237 *)
1238 PopTtok (Type, typetokno) ;
1239 PopTtok (name, nametokno) ;
1240 IF Debugging
1241 THEN
1242 n1 := GetSymName(GetCurrentModule()) ;
1243 printf2('inside module %a declaring type name %a\n',
1244 n1, name) ;
1245 IF (NOT IsUnknown(Type))
1246 THEN
1247 n1 := GetSymName(GetScope(Type)) ;
1248 n2 := GetSymName(Type) ;
1249 printf2('type was created inside scope %a as name %a\n',
1250 n1, n2)
1251 END
1252 END ;
1253 IF name=NulName
1254 THEN
1255 (*
1256 Typically the declaration that causes this case is:
1257
1258 VAR
1259 a: RECORD
1260 etc
1261 END ;
1262 ^
1263 |
1264 +---- type has no name.
1265
1266 *)
1267 (* WriteString('Blank name type') ; WriteLn ; *)
1268 PushTFtok(Type, name, typetokno) ;
1269 Annotate("%1s(%1d)|%2n|%3d||type|type name|token no")
1270 ELSIF IsError(Type)
1271 THEN
1272 PushTFtok(Type, name, typetokno) ;
1273 Annotate("%1s(%1d)|%2n|%3d||error type|error type name|token no")
1274 ELSIF GetSymName(Type)=name
1275 THEN
1276 isunknown := IsUnknown(Type) ;
1277 IF isunknown OR
1278 (NOT IsDeclaredIn(GetCurrentScope(), Type))
1279 THEN
1280 Sym := MakeType (typetokno, name) ;
1281 IF NOT IsError(Sym)
1282 THEN
1283 IF Sym=Type
1284 THEN
1285 IF isunknown
1286 THEN
1287 MetaError2('attempting to declare a type {%1ad} to a type which is itself and also unknown {%2ad}',
1288 Sym, Type)
1289 ELSE
1290 MetaError1('attempting to declare a type {%1ad} as itself', Sym)
1291 END
1292 ELSE
1293 PutType(Sym, Type) ;
1294 CheckForExportedImplementation(Sym) ; (* May be an exported hidden type *)
1295 (* if Type is an enumerated type then add its contents to the pseudo scope *)
1296 CheckForEnumerationInCurrentModule(Type)
1297 END
1298 END ;
1299 PushTFtok(Sym, name, typetokno) ;
1300 Annotate("%1s(%1d)|%2n|%3d||type|type name|token no")
1301 ELSE
1302 PushTFtok(Type, name, typetokno) ;
1303 Annotate("%1s(%1d)|%2n|%3d||type|type name|token no")
1304 END
1305 ELSE
1306 (* example TYPE a = CARDINAL *)
1307 Sym := MakeType (nametokno, name) ;
1308 PutType (Sym, Type) ;
1309 CheckForExportedImplementation (Sym) ; (* May be an exported hidden type *)
1310 PushTFtok (Sym, name, nametokno) ;
1311 Annotate ("%1s(%1d)|%2n|%3d||type|type name|token no") ;
1312 IF Debugging
1313 THEN
1314 MetaErrorT1 (nametokno, 'type pos {%1Wa}', Sym)
1315 END
1316 END
1317 END BuildType ;
1318
1319
1320 (*
1321 StartBuildProcedure - Builds a Procedure.
1322
1323 The Stack:
1324
1325 Entry Exit
1326
1327 <- Ptr
1328 +------------+
1329 Ptr -> | ProcSym |
1330 +------------+ |------------|
1331 | Name | | Name |
1332 |------------| |------------|
1333 *)
1334
1335 PROCEDURE StartBuildProcedure ;
1336 VAR
1337 name : Name ;
1338 ProcSym: CARDINAL ;
1339 tokno : CARDINAL ;
1340 BEGIN
1341 PopTtok (name, tokno) ;
1342 PushTtok (name, tokno) ; (* name saved for the EndBuildProcedure name check *)
1343 Annotate ("%1n|(%1d)||procedure name saved by StartBuildProcedure") ;
1344 ProcSym := GetDeclareSym (tokno, name) ;
1345 IF IsUnknown (ProcSym)
1346 THEN
1347 (* May have been compiled in the definition or implementation module.
1348 Note we always see an implementation module before its corresponding
1349 definition module. *)
1350 ProcSym := MakeProcedure (tokno, name)
1351 ELSIF IsProcedure (ProcSym)
1352 THEN
1353 PutDeclared (tokno, ProcSym)
1354 ELSE
1355 ErrorStringAt2 (Sprintf1(Mark(InitString('procedure name (%a) has been declared as another object elsewhere')),
1356 name), tokno, GetDeclaredMod (ProcSym))
1357 END ;
1358 IF CompilingDefinitionModule ()
1359 THEN
1360 PutExportUnImplemented (tokno, ProcSym) (* Defined but not yet implemented *)
1361 ELSE
1362 CheckForExportedImplementation (ProcSym) (* May be exported procedure *)
1363 END ;
1364 PushTtok (ProcSym, tokno) ;
1365 Annotate ("%1s(%1d)||procedure start symbol") ;
1366 StartScope (ProcSym) ;
1367 M2Error.EnterProcedureScope (name)
1368 END StartBuildProcedure ;
1369
1370
1371 (*
1372 EndBuildProcedure - Ends building a Procedure.
1373 It checks the start procedure name matches the end
1374 procedure name.
1375
1376 The Stack:
1377
1378 (Procedure Not Defined in definition module)
1379
1380 Entry Exit
1381
1382 Ptr ->
1383 +------------+
1384 | NameEnd |
1385 |------------|
1386 | ProcSym |
1387 |------------|
1388 | NameStart |
1389 |------------|
1390 Empty
1391 *)
1392
1393 PROCEDURE EndBuildProcedure ;
1394 VAR
1395 NameEnd,
1396 NameStart: Name ;
1397 tok : CARDINAL ;
1398 ProcSym : CARDINAL ;
1399 kind : ProcedureKind ;
1400 BEGIN
1401 PopT (NameEnd) ;
1402 PopTtok (ProcSym, tok) ;
1403 Assert (IsProcedure(ProcSym)) ;
1404 kind := GetProcedureKind (ProcSym, tok) ;
1405 PopT (NameStart) ;
1406 IF NameEnd # NameStart
1407 THEN
1408 WriteFormat2 ('end procedure name does not match beginning %a name %a', NameStart, NameEnd)
1409 END ;
1410 PutProcedureParameterHeapVars (ProcSym) ;
1411 PutProcedureDefined (ProcSym, kind) ;
1412 EndScope ;
1413 M2Error.LeaveErrorScope
1414 END EndBuildProcedure ;
1415
1416
1417 (*
1418 EndBuildForward - ends building a forward procedure.
1419 *)
1420
1421 PROCEDURE EndBuildForward ;
1422 BEGIN
1423 PopN (2) ;
1424 EndScope ;
1425 M2Error.LeaveErrorScope
1426 END EndBuildForward ;
1427
1428
1429 (*
1430 BuildProcedureHeading - Builds a procedure heading for the definition
1431 module procedures.
1432
1433 Operation only performed if compiling a
1434 definition module.
1435
1436 The Stack:
1437
1438 Entry Exit
1439
1440 Ptr ->
1441 +------------+
1442 | ProcSym |
1443 |------------|
1444 | NameStart |
1445 |------------|
1446 Empty
1447
1448 *)
1449
1450 PROCEDURE BuildProcedureHeading ;
1451 VAR
1452 tok,
1453 ProcSym : CARDINAL ;
1454 NameStart: Name ;
1455 BEGIN
1456 ProcSym := OperandT (1) ;
1457 tok := OperandTok (1) ;
1458 PutProcedureParametersDefined (ProcSym, GetProcedureKind (ProcSym, tok)) ;
1459 IF CompilingDefinitionModule ()
1460 THEN
1461 PopT (ProcSym) ;
1462 Assert (IsProcedure (ProcSym)) ;
1463 PopT (NameStart) ;
1464 EndScope
1465 END
1466 END BuildProcedureHeading ;
1467
1468
1469 (*
1470 BuildFPSection - Builds a Formal Parameter in a procedure.
1471
1472 The Stack:
1473
1474 Entry Exit
1475
1476 Ptr ->
1477 +------------+
1478 | ParamTotal |
1479 |------------|
1480 | TypeSym |
1481 |------------|
1482 | Array/Nul |
1483 |------------|
1484 | NoOfIds |
1485 |------------|
1486 | Id 1 |
1487 |------------|
1488 . .
1489 . .
1490 . .
1491 |------------|
1492 | Id n | <- Ptr
1493 |------------| +------------+
1494 | Var / Nul | | ParamTotal |
1495 |------------| |------------|
1496 | ProcSym | | ProcSym |
1497 |------------| |------------|
1498 *)
1499
1500 PROCEDURE BuildFPSection ;
1501 VAR
1502 kind,
1503 curkind : ProcedureKind ;
1504 tok : CARDINAL ;
1505 top,
1506 ProcSym,
1507 ParamTotal: CARDINAL ;
1508 BEGIN
1509 top := Top () ;
1510 PopT (ParamTotal) ;
1511 ProcSym := CARDINAL (OperandT (3 + CARDINAL (OperandT (3)) + 2)) ;
1512 tok := CARDINAL (OperandTok (3 + CARDINAL (OperandT (3)) + 2)) ;
1513 (* Debug (tok, ProcSym, 'foo') ; *)
1514 curkind := GetProcedureKind (ProcSym, tok) ;
1515 PushT (ParamTotal) ;
1516 Annotate ("%1d||running total of no. of parameters") ;
1517 Assert (IsProcedure (ProcSym) OR IsProcType (ProcSym)) ;
1518 Assert (top = Top ()) ;
1519 ProcSym := CARDINAL (OperandT (3 + CARDINAL (OperandT (3 + 1)) + 2 + 1)) ;
1520 Assert (IsProcedure (ProcSym) OR IsProcType (ProcSym)) ;
1521
1522 IF NOT GetProcedureParametersDefined (ProcSym, curkind)
1523 THEN
1524 BuildFormalParameterSection (curkind)
1525 END ;
1526 (* Check against any previous declaration. *)
1527 FOR kind := MIN (ProcedureKind) TO MAX (ProcedureKind) DO
1528 IF (kind # curkind) AND GetProcedureParametersDefined (ProcSym, kind)
1529 THEN
1530 Assert (top = Top ()) ;
1531 CheckFormalParameterSection (curkind, kind) ;
1532 Assert (top = Top ())
1533 END ;
1534 ProcSym := CARDINAL (OperandT (3 + CARDINAL (OperandT (3 + 1)) + 2 + 1)) ;
1535 Assert (IsProcedure (ProcSym) OR IsProcType (ProcSym))
1536 END ;
1537 RemoveFPParameters ;
1538 Assert (IsProcedure (OperandT (2)))
1539 END BuildFPSection ;
1540
1541
1542 (*
1543 BuildVarArgs - indicates that the ProcSym takes varargs
1544 after ParamTotal.
1545 <- Ptr
1546 +------------+ +------------+
1547 | ParamTotal | | ParamTotal |
1548 |------------| |------------|
1549 | ProcSym | | ProcSym |
1550 |------------| |------------|
1551
1552 *)
1553
1554 PROCEDURE BuildVarArgs ;
1555 VAR
1556 kind : ProcedureKind ;
1557 tok : CARDINAL ;
1558 ProcSym,
1559 ParamTotal: CARDINAL ;
1560 BEGIN
1561 PopT (ParamTotal) ;
1562 PopTtok (ProcSym, tok) ;
1563 kind := GetProcedureKind (ProcSym, tok) ;
1564 IF UsesOptArg (ProcSym, kind)
1565 THEN
1566 WriteFormat0('procedure can use either a single optional argument or a single vararg section ... at the end of the formal parameter list')
1567 END ;
1568 IF UsesVarArgs (ProcSym)
1569 THEN
1570 WriteFormat0('procedure can only have one vararg section ... at the end of the formal parameter list')
1571 END ;
1572 PutUseVarArgs (ProcSym) ;
1573 IF IsDefImp(GetCurrentModule())
1574 THEN
1575 IF NOT IsDefinitionForC(GetCurrentModule())
1576 THEN
1577 WriteFormat0('the definition module must be declared as DEFINITION MODULE FOR "C" if varargs are to be used')
1578 END
1579 ELSE
1580 WriteFormat0('varargs can only be used in the module declared as DEFINITION MODULE FOR "C"')
1581 END ;
1582 PushTtok (ProcSym, tok) ;
1583 PushT (ParamTotal)
1584 END BuildVarArgs ;
1585
1586
1587 (*
1588 BuildOptArg - indicates that the ProcSym takes a single optarg
1589 after ParamTotal.
1590
1591 <- Ptr
1592 +------------+ +------------+
1593 | ParamTotal | | ParamTotal |
1594 |------------| |------------|
1595 | ProcSym | | ProcSym |
1596 |------------| |------------|
1597 *)
1598
1599 PROCEDURE BuildOptArg ;
1600 VAR
1601 kind : ProcedureKind ;
1602 tok : CARDINAL ;
1603 ProcSym,
1604 ParamTotal: CARDINAL ;
1605 BEGIN
1606 PopT (ParamTotal) ;
1607 PopTtok (ProcSym, tok) ;
1608 kind := GetProcedureKind (ProcSym, tok) ;
1609 IF UsesVarArgs (ProcSym)
1610 THEN
1611 WriteFormat0('procedure can not use an optional argument after a vararg ...')
1612 END ;
1613 PutUseOptArg (ProcSym, kind) ;
1614 PushTtok (ProcSym, tok) ;
1615 PushT (ParamTotal)
1616 END BuildOptArg ;
1617
1618
1619 (*
1620 BuildFormalVarArgs - indicates that the procedure type takes varargs.
1621
1622 <- Ptr
1623 +------------+ +------------+
1624 | ProcSym | | ProcSym |
1625 |------------| |------------|
1626
1627 *)
1628
1629 PROCEDURE BuildFormalVarArgs ;
1630 VAR
1631 ProcSym: CARDINAL ;
1632 BEGIN
1633 PopT(ProcSym) ;
1634 IF UsesVarArgs(ProcSym)
1635 THEN
1636 WriteFormat0('procedure type can only have one vararg section ... at the end of the formal parameter list')
1637 END ;
1638 PutUseVarArgs(ProcSym) ;
1639 IF IsDefImp(GetCurrentModule())
1640 THEN
1641 IF NOT IsDefinitionForC(GetCurrentModule())
1642 THEN
1643 WriteFormat0('the definition module must be declared as DEFINITION MODULE FOR "C" if varargs are to be used')
1644 END
1645 ELSE
1646 WriteFormat0('varargs can only be used in the module declared as DEFINITION MODULE FOR "C"')
1647 END ;
1648 PushT (ProcSym)
1649 END BuildFormalVarArgs ;
1650
1651
1652 (*
1653 BuildFormalParameterSection - Builds a Formal Parameter in a procedure.
1654
1655 The Stack:
1656
1657 Entry and Exit
1658
1659 Ptr -> <- Ptr
1660 +------------+
1661 | ParamTotal |
1662 |------------|
1663 | TypeSym |
1664 |------------|
1665 | Array/Nul |
1666 |------------|
1667 | NoOfIds |
1668 |------------|
1669 | Id 1 |
1670 |------------|
1671 . .
1672 . .
1673 . .
1674 |------------|
1675 | Id n |
1676 |------------|
1677 | Var / Nul |
1678 |------------|
1679 | ProcSym |
1680 |------------|
1681 *)
1682
1683 PROCEDURE BuildFormalParameterSection (kind: ProcedureKind) ;
1684 VAR
1685 ParamName,
1686 Var,
1687 Array : Name ;
1688 tok : CARDINAL ;
1689 pi,
1690 TypeTok,
1691 ParamTotal,
1692 TypeSym,
1693 UnBoundedSym,
1694 NoOfIds,
1695 ProcSym,
1696 i, ndim : CARDINAL ;
1697 BEGIN
1698 PopT (ParamTotal) ;
1699 PopTtok (TypeSym, TypeTok) ;
1700 PopTF (Array, ndim) ;
1701 Assert ((Array=ArrayTok) OR (Array=NulTok)) ;
1702 PopT (NoOfIds) ;
1703 ProcSym := OperandT (NoOfIds + 2) ;
1704 Assert (IsProcedure (ProcSym)) ;
1705 Var := OperandT (NoOfIds + 1) ;
1706 tok := OperandTok (NoOfIds + 2) ;
1707 Assert ((Var=VarTok) OR (Var=NulTok)) ;
1708 (* Restore popped elements. *)
1709 PushT (NoOfIds) ;
1710 PushTF (Array, ndim) ;
1711 PushTtok (TypeSym, TypeTok) ;
1712 PushT (ParamTotal) ;
1713
1714 IF Array = ArrayTok
1715 THEN
1716 UnBoundedSym := MakeUnbounded (tok, TypeSym, ndim) ;
1717 TypeSym := UnBoundedSym
1718 END ;
1719 i := 1 ;
1720 (* +4 to skip over the top restored elements. *)
1721 pi := NoOfIds + 4 ; (* Stack index referencing stacked parameter i. *)
1722 WHILE i <= NoOfIds DO
1723 IF CompilingDefinitionModule () AND (NOT PedanticParamNames) AND
1724 (* We will see the parameters in the implementation module. *)
1725 ((GetMainModule () = GetCurrentModule ()) OR
1726 (IsHiddenTypeDeclared (GetCurrentModule ()) AND ExtendedOpaque))
1727 THEN
1728 ParamName := NulName
1729 ELSE
1730 ParamName := OperandT (pi)
1731 END ;
1732 tok := OperandTok (pi) ;
1733 IF Var=VarTok
1734 THEN
1735 (* VAR parameter. *)
1736 IF NOT PutVarParam (tok, ProcSym, kind, ParamTotal + i, ParamName,
1737 TypeSym, Array = ArrayTok, TypeTok)
1738 THEN
1739 InternalError ('problems adding a VarParameter - wrong param number?')
1740 END
1741 ELSE
1742 (* Non VAR parameter. *)
1743 IF NOT PutParam (tok, ProcSym, kind, ParamTotal + i, ParamName,
1744 TypeSym, Array = ArrayTok, TypeTok)
1745 THEN
1746 InternalError ('problems adding a Parameter - wrong param number?')
1747 END
1748 END ;
1749 (*
1750 IF kind = ProperProcedure
1751 THEN
1752 PutDeclared (OperandTok (pi), GetParameterShadowVar (GetNthParam (ProcSym, kind, ParamTotal + i)))
1753 END ;
1754 *)
1755 INC (i) ;
1756 DEC (pi)
1757 END
1758 END BuildFormalParameterSection ;
1759
1760
1761 (*
1762 CheckFormalParameterSection - Checks a Formal Parameter in a procedure.
1763 The stack is unaffected.
1764
1765 The Stack:
1766
1767 Entry and Exit
1768
1769 Ptr ->
1770 +------------+
1771 | ParamTotal |
1772 |------------|
1773 | TypeSym |
1774 |------------|
1775 | Array/Nul |
1776 |------------|
1777 | NoOfIds |
1778 |------------|
1779 | Id 1 |
1780 |------------|
1781 . .
1782 . .
1783 . .
1784 |------------|
1785 | Id n |
1786 |------------|
1787 | Var / Nul |
1788 |------------|
1789 | ProcSym |
1790 |------------|
1791 *)
1792
1793 PROCEDURE CheckFormalParameterSection (curkind, prevkind: ProcedureKind) ;
1794 VAR
1795 Array, Var: Name ;
1796 isVarParam,
1797 Unbounded : BOOLEAN ;
1798 ParamI,
1799 ParamIType,
1800 ParamTotal,
1801 TypeTok,
1802 TypeSym,
1803 NoOfIds,
1804 ProcSym,
1805 pi, i, ndim: CARDINAL ;
1806 BEGIN
1807 PopT(ParamTotal) ;
1808 PopTtok(TypeSym, TypeTok) ;
1809 PopTF(Array, ndim) ;
1810 Assert( (Array=ArrayTok) OR (Array=NulTok) ) ;
1811 PopT(NoOfIds) ;
1812 ProcSym := OperandT (NoOfIds+2) ;
1813 Assert (IsProcedure (ProcSym)) ;
1814 Var := OperandT (NoOfIds+1) ;
1815 Assert ((Var = NulName) OR (Var = VarTok)) ;
1816 isVarParam := (Var # NulName) ;
1817
1818 (* Restore popped elements. *)
1819 PushT (NoOfIds) ;
1820 PushTF (Array, ndim) ;
1821 PushTtok (TypeSym, TypeTok) ;
1822 PushT (ParamTotal) ;
1823
1824 Assert( (Var=VarTok) OR (Var=NulTok) ) ;
1825 Unbounded := (Array=ArrayTok) ; (* ARRAY OF Type, parameter. *)
1826 i := 1 ;
1827 (* +4 to skip over the top restored elements. *)
1828 pi := NoOfIds + 4 ; (* Stack index referencing stacked parameter i. *)
1829
1830 (* If there are an incorrect number of parameters specified then this
1831 will be detcted by EndBuildFormalParameters. *)
1832 WHILE i<=NoOfIds DO
1833 IF ParamTotal+i <= NoOfParam (ProcSym, prevkind)
1834 THEN
1835 (* WarnStringAt (InitString ('parampos?'), OperandTok (pi)) ; *)
1836 IF Unbounded AND (NOT IsUnboundedParam (ProcSym, prevkind, ParamTotal+i))
1837 THEN
1838 ParameterError ('declaration of procedure {%%1a} in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1839 'the parameter {%3EHa} was not declared as an ARRAY OF type',
1840 'the parameter {%3EVa} was declared as an ARRAY OF type',
1841 ParamTotal+i, ProcSym, curkind, prevkind)
1842 ELSIF (NOT Unbounded) AND IsUnboundedParam (ProcSym, prevkind, ParamTotal+i)
1843 THEN
1844 ParameterError ('declaration of procedure {%%1a} in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1845 'the parameter {%3EHa} was declared as an ARRAY OF type',
1846 'the parameter {%3EVa} was not declared as an ARRAY OF type',
1847 ParamTotal+i, ProcSym, curkind, prevkind)
1848 END ;
1849 IF Unbounded
1850 THEN
1851 IF GetDimension (GetNthParam (ProcSym, prevkind, ParamTotal+1)) # ndim
1852 THEN
1853 ParameterError ('declaration of procedure {%%1a} in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1854 'the dynamic array parameter {%3EHa} was declared with a different of dimensions',
1855 'the dynamic array parameter {%3EVa} was declared with a different of dimensions',
1856 ParamTotal+i, ProcSym, curkind, prevkind)
1857 END
1858 END ;
1859 IF isVarParam AND (NOT IsVarParam (ProcSym, prevkind, ParamTotal+i))
1860 THEN
1861 (* Expecting non VAR parameter. *)
1862 ParameterError ('declaration of procedure {%%1a} in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1863 '{%3EHa} was not declared as a {%kVAR} parameter',
1864 '{%3EVa} was declared as a {%kVAR} parameter',
1865 ParamTotal+i, ProcSym, curkind, prevkind)
1866 ELSIF (NOT isVarParam) AND IsVarParam (ProcSym, prevkind, ParamTotal+i)
1867 THEN
1868 (* Expecting VAR pamarater. *)
1869 ParameterError ('declaration of procedure {%%1a} in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1870 '{%3EHa} was declared as a {%kVAR} parameter',
1871 '{%3EVa} was not declared as a {%kVAR} parameter',
1872 ParamTotal+i, ProcSym, curkind, prevkind)
1873 END ;
1874 ParamI := GetNthParam (ProcSym, prevkind, ParamTotal+i) ;
1875 IF PedanticParamNames
1876 THEN
1877 IF GetSymName (ParamI) # OperandT (pi)
1878 THEN
1879 (* Different parameter names. *)
1880 ParameterError ('procedure {%%1a} in the %s differs from the %s, {%%2N} parameter name is inconsistent, %s',
1881 'named as {%3EVa}',
1882 'named as {%3EVa}',
1883 ParamTotal+i, ProcSym, curkind, prevkind)
1884 END
1885 END ;
1886 IF Unbounded
1887 THEN
1888 (* GetType(ParamI) yields an UnboundedSym or a PartialUnboundedSym,
1889 depending whether it has been resolved.. *)
1890 ParamIType := GetType (GetType (ParamI))
1891 ELSE
1892 ParamIType := GetType (ParamI)
1893 END ;
1894 IF ((SkipType(ParamIType)#SkipType(TypeSym)) OR
1895 (PedanticParamNames AND (ParamIType#TypeSym))) AND
1896 (NOT IsUnknown(SkipType(TypeSym))) AND
1897 (NOT IsUnknown(SkipType(ParamIType)))
1898 THEN
1899 (* Different parameter types. *)
1900 ParameterError ('declaration in the %s differs from the %s, {%%2N} parameter is inconsistent, %s',
1901 'the parameter {%3EHa} was declared with a different type',
1902 'the parameter {%3EVa} was declared with a different type',
1903 ParamTotal+i, ProcSym, curkind, prevkind)
1904 END
1905 END ;
1906 INC (i) ;
1907 DEC (pi)
1908 END
1909 END CheckFormalParameterSection ;
1910
1911
1912 (*
1913 RemoveFPParameters - remove the FPSection parameters from the stack and
1914 increment the param total with the NoOfIds.
1915
1916 The Stack:
1917
1918 Entry Exit
1919
1920 Ptr ->
1921 +------------+
1922 | ParamTotal |
1923 |------------|
1924 | TypeSym |
1925 |------------|
1926 | Array/Nul |
1927 |------------|
1928 | NoOfIds |
1929 |------------|
1930 | Id 1 |
1931 |------------|
1932 . .
1933 . .
1934 . .
1935 |------------|
1936 | Id n | <- Ptr
1937 |------------| +------------+
1938 | Var / Nul | | ParamTotal |
1939 |------------| |------------|
1940 | ProcSym | | ProcSym |
1941 |------------| |------------|
1942 *)
1943
1944 PROCEDURE RemoveFPParameters ;
1945 VAR
1946 ParamTotal,
1947 Array,
1948 TypeSym,
1949 NoOfIds,
1950 ProcSym : CARDINAL ;
1951 BEGIN
1952 PopT (ParamTotal) ;
1953 PopT (TypeSym) ;
1954 PopT (Array) ;
1955 Assert ((Array=ArrayTok) OR (Array=NulTok)) ;
1956 PopT (NoOfIds) ;
1957 ProcSym := OperandT (NoOfIds+2) ;
1958 Assert (IsProcedure (ProcSym)) ;
1959 PopN (NoOfIds+1) ; (* +1 for the Var/Nul. *)
1960 PushT (ParamTotal + NoOfIds) ;
1961 Annotate ("%1d||running total of no. of parameters") ;
1962 Assert (IsProcedure (OperandT (2)))
1963 END RemoveFPParameters ;
1964
1965
1966 (*
1967 ParameterError - create two error strings chained together.
1968 *)
1969
1970 PROCEDURE ParameterError (FmtHeader, PrevDesc, CurDesc: ARRAY OF CHAR;
1971 ParamNo, ProcSym: CARDINAL;
1972 curkind, prevkind: ProcedureKind) ;
1973 VAR
1974 PrevParam,
1975 CurParam : CARDINAL ;
1976 CurStr,
1977 PrevStr,
1978 Msg,
1979 CurKindStr,
1980 PrevKindStr: String ;
1981 BEGIN
1982 CurParam := GetNthParam (ProcSym, curkind, ParamNo) ;
1983 CurKindStr := GetProcedureKindDesc (curkind) ;
1984 PrevKindStr := GetProcedureKindDesc (prevkind) ;
1985 PrevParam := GetNthParam (ProcSym, prevkind, ParamNo) ;
1986 PrevStr := InitString (PrevDesc) ;
1987 CurStr := InitString (CurDesc) ;
1988 Msg := Sprintf3 (Mark (InitString (FmtHeader)), CurKindStr, PrevKindStr, PrevStr) ;
1989 MetaErrorString3 (Msg, ProcSym, ParamNo, PrevParam) ;
1990 Msg := Sprintf3 (Mark (InitString (FmtHeader)), CurKindStr, PrevKindStr, CurStr) ;
1991 MetaErrorString3 (Msg, ProcSym, ParamNo, CurParam)
1992 END ParameterError ;
1993
1994
1995 (*
1996 StartBuildFormalParameters - Initialises the quadruple stack for
1997 Formal Parameters.
1998
1999 The Stack:
2000
2001 Entry Exit
2002
2003 <- Ptr
2004 +------------+
2005 Empty | 0 |
2006 |------------|
2007 *)
2008
2009 PROCEDURE StartBuildFormalParameters ;
2010 BEGIN
2011 PushT (0) ;
2012 Annotate ("%1d||running total of no. of parameters")
2013 END StartBuildFormalParameters ;
2014
2015
2016 (*
2017 ParameterMismatch - generate a parameter mismatch error between the current
2018 declaration at tok and a previous ProcSym declaration.
2019 NoOfPar is the current number of parameters.
2020 *)
2021
2022 PROCEDURE ParameterMismatch (tok: CARDINAL; ProcSym: CARDINAL;
2023 NoOfPar: CARDINAL; prevkind, curkind: ProcedureKind) ;
2024 VAR
2025 MsgCurrent,
2026 MsgPrev,
2027 CompCur,
2028 CompPrev,
2029 CurDesc,
2030 PrevDesc : String ;
2031 BEGIN
2032 CurDesc := GetProcedureKindDesc (curkind) ;
2033 PrevDesc := GetProcedureKindDesc (prevkind) ;
2034 CompPrev := GetComparison (NoOfParam (ProcSym, prevkind), NoOfPar) ;
2035 CompCur := GetComparison (NoOfPar, NoOfParam (ProcSym, prevkind)) ;
2036 MsgCurrent := Sprintf3 (Mark (InitString ('the %s for {%%1ad} has %s parameters than the %s')),
2037 CurDesc, CompCur, PrevDesc) ;
2038 MsgPrev := Sprintf3 (Mark (InitString ('the %s for {%%1ad} has %s parameters than the %s')),
2039 PrevDesc, CompPrev, CurDesc) ;
2040 MetaErrorStringT1 (GetProcedureDeclaredTok (ProcSym, prevkind), MsgPrev, ProcSym) ;
2041 MetaErrorStringT1 (tok, MsgCurrent, ProcSym) ;
2042 CurDesc := KillString (CurDesc) ;
2043 PrevDesc := KillString (PrevDesc) ;
2044 CompCur := KillString (CompCur) ;
2045 CompPrev := KillString (CompPrev)
2046 END ParameterMismatch ;
2047
2048
2049 (*
2050 EndBuildFormalParameters - Resets the quadruple stack after building
2051 Formal Parameters.
2052
2053 The Stack:
2054
2055 Entry Exit
2056
2057 Ptr ->
2058 +------------+
2059 | NoOfParam | <- Ptr
2060 |------------| +------------+
2061 | ProcSym | | ProcSym |
2062 |------------| |------------|
2063 *)
2064
2065 PROCEDURE EndBuildFormalParameters ;
2066 VAR
2067 kind,
2068 curkind: ProcedureKind ;
2069 tok : CARDINAL ;
2070 NoOfPar: CARDINAL ;
2071 ProcSym: CARDINAL ;
2072 BEGIN
2073 PopT (NoOfPar) ;
2074 PopTtok (ProcSym, tok) ;
2075 PushTtok (ProcSym, tok) ;
2076 Annotate ("%1s(%1d)||procedure start symbol") ;
2077 Assert (IsProcedure (ProcSym)) ;
2078 curkind := GetProcedureKind (ProcSym, tok) ;
2079 FOR kind := MIN (ProcedureKind) TO MAX (ProcedureKind) DO
2080 IF GetProcedureParametersDefined (ProcSym, kind) AND
2081 (curkind # kind) AND (NoOfParam (ProcSym, kind) # NoOfPar)
2082 THEN
2083 ParameterMismatch (tok, ProcSym, NoOfPar, kind, curkind)
2084 END
2085 END ;
2086 (* All parameter seen so set procedure defined. *)
2087 PutProcedureParametersDefined (ProcSym, curkind) ;
2088 Assert (IsProcedure (OperandT (1)))
2089 END EndBuildFormalParameters ;
2090
2091
2092 (*
2093 GetComparison - return a simple description from the result of
2094 a comparison between left and right.
2095 *)
2096
2097 PROCEDURE GetComparison (left, right: CARDINAL) : String ;
2098 BEGIN
2099 IF left < right
2100 THEN
2101 RETURN InitString ('fewer')
2102 ELSIF left > right
2103 THEN
2104 RETURN InitString ('more')
2105 ELSE
2106 RETURN InitString ('same')
2107 END
2108 END GetComparison ;
2109
2110
2111 (*
2112 ReturnTypeMismatch - generate two errors showing the return type mismatches between
2113 ProcSym and ReturnType at procedure location tok.
2114 *)
2115
2116 PROCEDURE ReturnTypeMismatch (curtok: CARDINAL; ProcSym, CurRetType: CARDINAL;
2117 curtypetok: CARDINAL;
2118 curkind, prevkind: ProcedureKind;
2119 PrevRetType: CARDINAL) ;
2120 VAR
2121 prevtok : CARDINAL ;
2122 CurDesc,
2123 PrevDesc,
2124 MsgCurrent,
2125 MsgPrev : String ;
2126 BEGIN
2127 CurDesc := GetProcedureKindDesc (curkind) ;
2128 PrevDesc := GetProcedureKindDesc (prevkind) ;
2129 prevtok := GetProcedureDeclaredTok (ProcSym, prevkind) ;
2130 IF CurRetType = NulSym
2131 THEN
2132 MsgCurrent := Sprintf2 (Mark (InitString ('there is no return type for {%%1ad} specified in the %s whereas a return type is specified in the %s')),
2133 CurDesc, PrevDesc) ;
2134 MsgPrev := Sprintf2 (Mark (InitString ('there is no return type for {%%1ad} specified in the %s whereas a return type is specified in the %s')),
2135 CurDesc, PrevDesc) ;
2136 prevtok := GetReturnTypeTok (ProcSym, prevkind)
2137 ELSIF PrevRetType = NulSym
2138 THEN
2139 MsgCurrent := Sprintf2 (Mark (InitString ('there is no return type for {%%1ad} specified in the %s whereas a return type is specified in the %s')),
2140 PrevDesc, CurDesc) ;
2141 MsgPrev := Sprintf2 (Mark (InitString ('there is no return type for {%%1ad} specified in the %s whereas a return type is specified in the %s')),
2142 PrevDesc, CurDesc) ;
2143 curtok := curtypetok
2144 ELSE
2145 MsgCurrent := Sprintf2 (Mark (InitString ('the return type for {%%1ad} specified in the %s differs in the %s')),
2146 CurDesc, PrevDesc) ;
2147 MsgPrev := Sprintf2 (Mark (InitString ('the return type for {%%1ad} specified in the %s differs in the %s')),
2148 CurDesc, PrevDesc) ;
2149 curtok := curtypetok ;
2150 prevtok := GetReturnTypeTok (ProcSym, prevkind)
2151 END ;
2152 MetaErrorStringT1 (curtok, MsgCurrent, ProcSym) ;
2153 MetaErrorStringT1 (prevtok, MsgPrev, ProcSym)
2154 END ReturnTypeMismatch ;
2155
2156
2157 (*
2158 BuildFunction - Builds a procedures return type.
2159 Procedure becomes a function.
2160
2161 The Stack:
2162
2163 Entry Exit
2164
2165 Ptr ->
2166 +------------+
2167 | TypeSym | <- Ptr
2168 |------------| +------------+
2169 | ProcSym | | ProcSym |
2170 |------------| |------------|
2171 *)
2172
2173 PROCEDURE BuildFunction ;
2174 VAR
2175 tok : CARDINAL ;
2176 ProcSym,
2177 typetok: CARDINAL ;
2178 RetType: CARDINAL ;
2179 BEGIN
2180 PopTtok (RetType, typetok) ;
2181 PopTtok (ProcSym, tok) ;
2182 PushTtok (ProcSym, tok) ;
2183 PutFunction (typetok, ProcSym, GetProcedureKind (ProcSym, tok), RetType) ;
2184 CheckOptFunction (tok, ProcSym, GetProcedureKind (ProcSym, tok), FALSE) ;
2185 CheckProcedureReturn (RetType, typetok)
2186 END BuildFunction ;
2187
2188
2189 (*
2190 BuildOptFunction - Builds a procedures optional return type.
2191 Procedure becomes a function and the user
2192 can either call it as a function or a procedure.
2193
2194 The Stack:
2195
2196 Entry Exit
2197
2198 Ptr ->
2199 +------------+
2200 | TypeSym | <- Ptr
2201 |------------| +------------+
2202 | ProcSym | | ProcSym |
2203 |------------| |------------|
2204 *)
2205
2206 PROCEDURE BuildOptFunction ;
2207 VAR
2208 typetok,
2209 tok : CARDINAL ;
2210 RetType,
2211 ProcSym : CARDINAL ;
2212 BEGIN
2213 PopTtok (RetType, typetok) ;
2214 PopTtok (ProcSym, tok) ;
2215 PutOptFunction (typetok, ProcSym, GetProcedureKind (ProcSym, tok), RetType) ;
2216 CheckOptFunction (tok, ProcSym, GetProcedureKind (ProcSym, tok), TRUE) ;
2217 PushTtok (ProcSym, tok)
2218 END BuildOptFunction ;
2219
2220
2221 (*
2222 CheckOptFunction - checks to see whether the optional return value
2223 has been set before and if it differs it will
2224 generate an error message. It will set the
2225 new value to isopt.
2226 *)
2227
2228 PROCEDURE CheckOptFunction (tok: CARDINAL; sym: CARDINAL; kind: ProcedureKind;
2229 isopt: BOOLEAN) ;
2230 VAR
2231 other: ProcedureKind ;
2232 BEGIN
2233 IF GetType (sym) # NulSym
2234 THEN
2235 (* Procedure sym has been declared as a function. *)
2236 FOR other := MIN (ProcedureKind) TO MAX (ProcedureKind) DO
2237 IF (kind # other) AND GetProcedureParametersDefined (sym, other)
2238 THEN
2239 IF IsReturnOptional (sym, kind) AND (NOT isopt)
2240 THEN
2241 MetaErrorT1 (tok, 'procedure {%1Ea} is not declared with an optional return type here', sym) ;
2242 MetaErrorT1 (GetReturnTypeTok (sym, kind),
2243 'previously procedure {%1Ea} was declared with an optional return type', sym)
2244 ELSIF (NOT IsReturnOptional (sym, kind)) AND isopt
2245 THEN
2246 MetaErrorT1 (tok, 'procedure {%1Ea} is declared with an optional return type here', sym) ;
2247 MetaErrorT1 (GetReturnTypeTok (sym, kind),
2248 'previously procedure {%1Ea} was declared without an optional return type', sym)
2249 END
2250 END
2251 END
2252 END ;
2253 SetReturnOptional (sym, kind, isopt)
2254 END CheckOptFunction ;
2255
2256
2257 (*
2258 BuildNoReturnAttribute - provide an interface to the symbol table module.
2259 *)
2260
2261 PROCEDURE BuildNoReturnAttribute ;
2262 VAR
2263 kind : ProcedureKind ;
2264 ProcSym,
2265 tok : CARDINAL ;
2266 BEGIN
2267 PopTtok (ProcSym, tok) ;
2268 PushTtok (ProcSym, tok) ;
2269 kind := GetProcedureKind (ProcSym, tok) ;
2270 Assert (IsProcedure (ProcSym)) ;
2271 PutProcedureNoReturn (ProcSym, kind, TRUE)
2272 END BuildNoReturnAttribute ;
2273
2274
2275 (*
2276 CheckProcedure - checks to see that the top of stack procedure
2277 has not been declared as a procedure function.
2278
2279 The Stack:
2280
2281 Entry Exit
2282
2283 Ptr -> <- Ptr
2284 +------------+ +------------+
2285 | ProcSym | | ProcSym |
2286 |------------| |------------|
2287 *)
2288
2289 PROCEDURE CheckProcedure ;
2290 BEGIN
2291 CheckProcedureReturn (NulSym, UnknownTokenNo)
2292 END CheckProcedure ;
2293
2294
2295
2296 PROCEDURE CheckProcedureReturn (RetType: CARDINAL; typetok: CARDINAL) ;
2297 VAR
2298 curkind,
2299 kind : ProcedureKind ;
2300 tok : CARDINAL ;
2301 PrevRetType,
2302 ProcSym : CARDINAL ;
2303 BEGIN
2304 PopTtok (ProcSym, tok) ;
2305 PushTtok (ProcSym, tok) ;
2306 Annotate ("%1s(%1d)||procedure start symbol") ;
2307 IF IsProcedure (ProcSym)
2308 THEN
2309 curkind := GetProcedureKind (ProcSym, tok) ;
2310 (* Check against any previously declared kinds. *)
2311 FOR kind := MIN (ProcedureKind) TO MAX (ProcedureKind) DO
2312 IF (kind # curkind) AND GetProcedureParametersDefined (ProcSym, kind)
2313 THEN
2314 PrevRetType := GetType (ProcSym) ;
2315 IF PrevRetType # RetType
2316 THEN
2317 ReturnTypeMismatch (tok, ProcSym, RetType, typetok,
2318 curkind, kind, PrevRetType)
2319 END
2320 END
2321 END ;
2322 PutFunction (tok, ProcSym, curkind, RetType)
2323 END
2324 END CheckProcedureReturn ;
2325
2326
2327 (*
2328 BuildPointerType - builds a pointer type.
2329 The Stack:
2330
2331 Entry Exit
2332 ===== ====
2333
2334
2335 Ptr -> <- Ptr
2336 +------------+ +-------------+
2337 | Type | | PointerType |
2338 |------------| |-------------|
2339 | Name | | Name |
2340 |------------| |-------------|
2341 *)
2342
2343 PROCEDURE BuildPointerType (pointerpos: CARDINAL) ;
2344 VAR
2345 combined,
2346 namepos,
2347 typepos : CARDINAL ;
2348 name : Name ;
2349 Type,
2350 PtrToType: CARDINAL ;
2351 BEGIN
2352 PopTtok (Type, typepos) ;
2353 PopTtok (name, namepos) ;
2354 name := CheckAnonymous (name) ;
2355
2356 combined := MakeVirtual2Tok (pointerpos, typepos) ;
2357 PtrToType := MakePointer (combined, name) ;
2358 PutPointer (PtrToType, Type) ;
2359 CheckForExportedImplementation(PtrToType) ; (* May be an exported hidden type *)
2360 PushTtok (name, namepos) ;
2361 Annotate("%1n|%3d||pointer type name") ;
2362 PushTtok(PtrToType, combined) ;
2363 Annotate("%1s(%1d)|%3d||pointer type")
2364 END BuildPointerType ;
2365
2366
2367 (*
2368 BuildSetType - builds a set type.
2369 The Stack:
2370
2371 Entry Exit
2372 ===== ====
2373
2374
2375 Ptr -> <- Ptr
2376 +------------+ +-------------+
2377 | Type | | SetType |
2378 |------------| |-------------|
2379 | Name | | Name |
2380 |------------| |-------------|
2381 *)
2382
2383 PROCEDURE BuildSetType (setpos: CARDINAL; ispacked: BOOLEAN) ;
2384 VAR
2385 combined,
2386 namepos,
2387 typepos : CARDINAL ;
2388 name : Name ;
2389 Type,
2390 SetType : CARDINAL ;
2391 BEGIN
2392 PopTtok (Type, typepos) ;
2393 PopTtok (name, namepos) ;
2394 combined := MakeVirtual2Tok (setpos, typepos) ;
2395 SetType := MakeSet (combined, name) ;
2396 CheckForExportedImplementation(SetType) ; (* May be an exported hidden type *)
2397 PutSet(SetType, Type, ispacked) ;
2398 PushTtok (name, namepos) ;
2399 Annotate("%1n||set type name") ;
2400 PushTtok (SetType, combined) ;
2401 Annotate ("%1s(%1d)|%3d||set type|token no")
2402 END BuildSetType ;
2403
2404
2405 (*
2406 BuildRecord - Builds a record type.
2407 The Stack:
2408
2409 Entry Exit
2410 ===== ====
2411
2412
2413 <- Ptr
2414 +-----------+
2415 Ptr -> | RecordSym |
2416 +------------------+ |-----------|
2417 | Name | | Name |
2418 |------------------| |-----------|
2419 *)
2420
2421 PROCEDURE BuildRecord ;
2422 VAR
2423 tokno : CARDINAL ;
2424 name : Name ;
2425 RecordType: CARDINAL ;
2426 BEGIN
2427 name := OperandT(1) ;
2428 name := CheckAnonymous(name) ;
2429 tokno := OperandTok(1) ;
2430 RecordType := MakeRecord(tokno, name) ;
2431 CheckForExportedImplementation(RecordType) ; (* May be an exported hidden type *)
2432 PushT(RecordType) ;
2433 (* ; WriteKey(name) ; WriteString(' RECORD made') ; WriteLn *)
2434 Annotate("%1s(%1d)||record type")
2435 END BuildRecord ;
2436
2437
2438 (*
2439 HandleRecordFieldPragmas -
2440
2441 Entry Exit
2442 ===== ====
2443
2444 Ptr -> <- Ptr
2445
2446 |-------------| |-------------|
2447 | Const1 | | Const1 |
2448 |-------------| |-------------|
2449 | PragmaName1 | | PragmaName1 |
2450 |-------------| |-------------|
2451 *)
2452
2453 PROCEDURE HandleRecordFieldPragmas (record, field: CARDINAL; n: CARDINAL) ;
2454 VAR
2455 seenAlignment : BOOLEAN ;
2456 defaultAlignment,
2457 sym : CARDINAL ;
2458 i : CARDINAL ;
2459 name : Name ;
2460 s : String ;
2461 BEGIN
2462 seenAlignment := FALSE ;
2463 defaultAlignment := GetDefaultRecordFieldAlignment(record) ;
2464 i := 1 ;
2465 WHILE i<=n DO
2466 name := OperandT(i*2) ;
2467 sym := OperandT(i*2-1) ;
2468 IF name=MakeKey('unused')
2469 THEN
2470 IF sym=NulSym
2471 THEN
2472 PutUnused(field)
2473 ELSE
2474 WriteFormat0("not expecting pragma 'unused' to contain an expression")
2475 END
2476 ELSIF name=MakeKey('bytealignment')
2477 THEN
2478 IF sym=NulSym
2479 THEN
2480 WriteFormat0("expecting an expression with the pragma 'bytealignment'")
2481 ELSE
2482 PutAlignment(field, sym) ;
2483 seenAlignment := TRUE
2484 END
2485 ELSE
2486 s := InitString("cannot use pragma '") ;
2487 s := ConCat(s, Mark(InitStringCharStar(KeyToCharStar(name)))) ;
2488 s := ConCat(s, Mark(InitString("' on record field {%1ad}"))) ;
2489 MetaErrorString1(s, field)
2490 END ;
2491 INC(i)
2492 END ;
2493 IF (NOT seenAlignment) AND (defaultAlignment#NulSym)
2494 THEN
2495 PutAlignment(field, defaultAlignment)
2496 END
2497 END HandleRecordFieldPragmas ;
2498
2499
2500 (*
2501 BuildFieldRecord - Builds a field into a record sym.
2502 The Stack:
2503
2504
2505 Entry Exit
2506 ===== ====
2507
2508 Ptr ->
2509 +-------------+
2510 | NoOfPragmas |
2511 |-------------|
2512 | Const1 |
2513 |-------------|
2514 | PragmaName1 |
2515 |-------------|
2516 | Type | Name |
2517 |-------------|
2518 | n |
2519 |-------------|
2520 | Id 1 |
2521 |-------------|
2522 . .
2523 . .
2524 . .
2525 |-------------|
2526 | Id n | <- Ptr
2527 |-------------| +-------------+
2528 | RecordSym | | RecordSym |
2529 |-------------| |-------------|
2530 | RecordName | | RecordName |
2531 |-------------| |-------------|
2532 *)
2533
2534 PROCEDURE BuildFieldRecord ;
2535 VAR
2536 n1 : Name ;
2537 tok,
2538 fsym,
2539 Field,
2540 Varient,
2541 Parent,
2542 Type,
2543 NoOfPragmas,
2544 NoOfFields,
2545 Record,
2546 i : CARDINAL ;
2547 BEGIN
2548 PopT(NoOfPragmas) ;
2549 Type := OperandT(NoOfPragmas*2+1) ;
2550 (* name := OperandF(NoOfPragmas*2+1) ; *)
2551 NoOfFields := OperandT(NoOfPragmas*2+2) ;
2552 Record := OperandT(NoOfPragmas*2+NoOfFields+3) ;
2553 IF IsRecord(Record)
2554 THEN
2555 Parent := Record ;
2556 Varient := NulSym
2557 ELSE
2558 (* Record maybe FieldVarient *)
2559 Parent := GetRecord(GetParent(Record)) ;
2560 Assert(IsFieldVarient(Record)) ;
2561 Varient := OperandT(NoOfPragmas*2+NoOfFields+4) ;
2562 Assert(IsVarient(Varient)) ;
2563 PutFieldVarient(Record, Varient) ;
2564 IF Debugging
2565 THEN
2566 n1 := GetSymName(Record) ;
2567 WriteString('Record ') ;
2568 WriteKey(n1) ;
2569 WriteString(' has varient ') ;
2570 n1 := GetSymName(Varient) ;
2571 WriteKey(n1) ; WriteLn
2572 END
2573 END ;
2574 Field := NulSym ;
2575 i := 1 ;
2576 WHILE i<=NoOfFields DO
2577 IF Debugging
2578 THEN
2579 n1 := GetSymName(Record) ;
2580 WriteString('Record ') ;
2581 WriteKey(n1) ;
2582 WriteString(' ') ;
2583 WriteKey(OperandT(NoOfPragmas*2+NoOfFields+3-i)) ; WriteString(' is a Field with type ') ;
2584 WriteKey(GetSymName(Type)) ; WriteLn ;
2585 END ;
2586 fsym := GetLocalSym(Parent, OperandT(NoOfPragmas*2+NoOfFields+3-i)) ;
2587 IF fsym=NulSym
2588 THEN
2589 Field := PutFieldRecord(Record, OperandT(NoOfPragmas*2+NoOfFields+3-i), Type, Varient) ;
2590 HandleRecordFieldPragmas(Record, Field, NoOfPragmas)
2591 ELSE
2592 MetaErrors2('record field {%1ad} has already been declared inside a {%2Dd} {%2a}',
2593 'attempting to declare a duplicate record field', fsym, Parent)
2594 END ;
2595 (* adjust the location of declaration to the one on the stack (rather than GetTokenNo). *)
2596 tok := OperandTok(NoOfPragmas*2+NoOfFields+3-i) ;
2597 IF (tok # UnknownTokenNo) AND (Field # NulSym)
2598 THEN
2599 PutDeclared (tok, Field)
2600 END ;
2601 INC(i)
2602 END ;
2603 PopN(NoOfPragmas*2+NoOfFields+3) ;
2604 PushT(Record) ;
2605 IF IsRecord(Record)
2606 THEN
2607 Annotate("%1s(%1d)||record type")
2608 ELSE
2609 Assert(IsFieldVarient(Record)) ;
2610 Annotate("%1s(%1d)||varient field type")
2611 END
2612 END BuildFieldRecord ;
2613
2614
2615 (*
2616 BuildVarientSelector - Builds a field into a record sym.
2617 The Stack:
2618
2619
2620 Entry Exit
2621 ===== ====
2622
2623 Ptr ->
2624 +-------------+
2625 | Type |
2626 |-------------|
2627 | Tag | <- Ptr
2628 |-------------| +-------------+
2629 | RecordSym | | RecordSym |
2630 |-------------| |-------------|
2631 *)
2632
2633 PROCEDURE BuildVarientSelector ;
2634 VAR
2635 tagtok : CARDINAL ;
2636 tag : Name ;
2637 Field,
2638 Type,
2639 Varient,
2640 VarField,
2641 Record : CARDINAL ;
2642 BEGIN
2643 PopT(Type) ;
2644 PopTtok (tag, tagtok) ;
2645 Record := OperandT(1) ;
2646 IF IsRecord(Record)
2647 THEN
2648 Varient := NulSym ;
2649 InternalError ('not expecting a record symbol')
2650 ELSIF IsVarient(Record)
2651 THEN
2652 Varient := Record ;
2653 VarField := GetParent(Varient) ;
2654 IF (Type=NulSym) AND (tag=NulName)
2655 THEN
2656 MetaError1('expecting a tag field in the declaration of a varient record {%1Ua}', Record)
2657 ELSIF Type=NulSym
2658 THEN
2659 PutVarientTag (Varient, RequestSym (tagtok, tag))
2660 ELSE
2661 Field := PutFieldRecord (VarField, tag, Type, Varient) ;
2662 PutVarientTag(Varient, Field) ;
2663 IF Debugging
2664 THEN
2665 WriteString('varient field ') ; WriteKey(GetSymName(VarField)) ;
2666 WriteString('varient ') ; WriteKey(GetSymName(Varient)) ; WriteLn
2667 END
2668 END
2669 ELSE
2670 (* Record maybe FieldVarient *)
2671 Assert(IsFieldVarient(Record)) ;
2672 Varient := OperandT(1+2) ;
2673 Assert(IsVarient(Varient)) ;
2674 PutFieldVarient(Record, Varient) ;
2675 IF Debugging
2676 THEN
2677 WriteString('record ') ; WriteKey(GetSymName(Record)) ;
2678 WriteString('varient ') ; WriteKey(GetSymName(Varient)) ; WriteLn
2679 END ;
2680 IF (Type=NulSym) AND (tag=NulName)
2681 THEN
2682 MetaError1('expecting a tag field in the declaration of a varient record {%1Ua}', Record)
2683 ELSIF Type=NulSym
2684 THEN
2685 PutVarientTag(Varient, RequestSym (tagtok, tag))
2686 ELSE
2687 Field := PutFieldRecord(Record, tag, Type, Varient) ;
2688 PutVarientTag(Varient, Field) ;
2689 IF Debugging
2690 THEN
2691 WriteString('record ') ; WriteKey(GetSymName(Record)) ;
2692 WriteString('varient ') ; WriteKey(GetSymName(Varient)) ; WriteLn
2693 END
2694 END
2695 END
2696 END BuildVarientSelector ;
2697
2698
2699 (*
2700 StartBuildVarientFieldRecord - Builds a varient field into a varient sym.
2701 The Stack:
2702
2703
2704 Entry Exit
2705 ===== ====
2706
2707 <- Ptr
2708 +-------------+
2709 Ptr -> | VarientField|
2710 +-------------+ |-------------|
2711 | VarientSym | | VarientSym |
2712 |-------------| |-------------|
2713 *)
2714
2715 PROCEDURE StartBuildVarientFieldRecord ;
2716 VAR
2717 VarientSym,
2718 FieldSym : CARDINAL ;
2719 BEGIN
2720 VarientSym := OperandT(1) ;
2721 FieldSym := MakeFieldVarient(CheckAnonymous(NulName), VarientSym) ;
2722 Annotate("%1s(%1d)||varient sym") ;
2723 PushT(FieldSym) ;
2724 Annotate("%1s(%1d)||varient field type") ;
2725 Assert(IsFieldVarient(FieldSym)) ;
2726 PutFieldVarient(FieldSym, VarientSym) ;
2727 AddVarientFieldToList(FieldSym)
2728 END StartBuildVarientFieldRecord ;
2729
2730
2731 (*
2732 EndBuildVarientFieldRecord - Removes a varient field from the stack.
2733 The Stack:
2734
2735
2736 Entry Exit
2737 ===== ====
2738
2739 Ptr ->
2740 +-------------+
2741 | VarientField| <- Ptr
2742 |-------------| +-------------+
2743 | VarientSym | | VarientSym |
2744 |-------------| |-------------|
2745 *)
2746
2747 PROCEDURE EndBuildVarientFieldRecord ;
2748 VAR
2749 FieldSym: CARDINAL ;
2750 BEGIN
2751 PopT(FieldSym) ;
2752 (* GCFieldVarient(FieldSym) *)
2753 END EndBuildVarientFieldRecord ;
2754
2755
2756 (*
2757 StartBuildVarient - Builds a varient symbol on top of a record sym.
2758 The Stack:
2759
2760
2761 Entry Exit
2762 ===== ====
2763
2764 <- Ptr
2765 +-------------+
2766 Ptr -> | VarientSym |
2767 +-------------+ |-------------|
2768 | RecordSym | | RecordSym |
2769 |-------------| |-------------|
2770 | RecordName | | RecordName |
2771 |-------------| |-------------|
2772 *)
2773
2774 PROCEDURE StartBuildVarient ;
2775 VAR
2776 tokno : CARDINAL ;
2777 RecordSym,
2778 Sym : CARDINAL ;
2779 BEGIN
2780 RecordSym := OperandT(1) ;
2781 tokno := OperandTok(1) ;
2782 Sym := MakeVarient(tokno, RecordSym) ;
2783 PushT(Sym) ;
2784 Annotate("%1s(%1d)||varient type")
2785 END StartBuildVarient ;
2786
2787
2788 (*
2789 EndBuildVarient - Removes the varient symbol from the stack.
2790 The Stack:
2791
2792 Entry Exit
2793 ===== ====
2794
2795 Ptr ->
2796 +-------------+
2797 | VarientSym | <- Ptr
2798 |-------------| +-------------+
2799 | RecordSym | | RecordSym |
2800 |-------------| |-------------|
2801 | RecordName | | RecordName |
2802 |-------------| |-------------|
2803 *)
2804
2805 PROCEDURE EndBuildVarient ;
2806 VAR
2807 Sym: CARDINAL ;
2808 BEGIN
2809 PopT(Sym)
2810 END EndBuildVarient ;
2811
2812
2813 (*
2814 BuildNulName - Pushes a NulName onto the top of the stack.
2815 The Stack:
2816
2817
2818 Entry Exit
2819
2820 <- Ptr
2821 Empty +------------+
2822 | NulName |
2823 |------------|
2824 *)
2825
2826 PROCEDURE BuildNulName ;
2827 BEGIN
2828 PushTtok (NulName, GetTokenNo ()) ;
2829 Annotate ("%1n|%3d||NulName|token no")
2830 END BuildNulName ;
2831
2832
2833 (*
2834 BuildTypeEnd - Pops the type Type and Name.
2835 The Stack:
2836
2837
2838 Entry Exit
2839
2840
2841 Ptr ->
2842 +-------------+
2843 | Type | Name | Empty
2844 |-------------|
2845 *)
2846
2847 PROCEDURE BuildTypeEnd ;
2848 VAR
2849 Type: CARDINAL ;
2850 name: Name ;
2851 BEGIN
2852 PopTF(Type, name)
2853 END BuildTypeEnd ;
2854
2855
2856 (*
2857 StartBuildArray - Builds an array type.
2858 The Stack:
2859
2860 Entry Exit
2861 ===== ====
2862
2863 <- Ptr
2864 +-----------+
2865 Ptr -> | ArraySym |
2866 +------------+ |-----------|
2867 | Name | | Name |
2868 |------------| |-----------|
2869 *)
2870
2871 PROCEDURE StartBuildArray ;
2872 VAR
2873 tok : CARDINAL ;
2874 name : Name ;
2875 ArrayType: CARDINAL ;
2876 BEGIN
2877 name := OperandT(1) ;
2878 tok := OperandTok(1) ;
2879 ArrayType := MakeArray (tok, name) ;
2880 CheckForExportedImplementation (ArrayType) ; (* May be an exported hidden type *)
2881 PushTtok(ArrayType, tok) ;
2882 Annotate("%1s(%1d)|%3d||array type|token no")
2883 (* ; WriteKey(Name) ; WriteString(' ARRAY made') ; WriteLn *)
2884 END StartBuildArray ;
2885
2886
2887 (*
2888 EndBuildArray - Builds an array type.
2889 The Stack:
2890
2891 Entry Exit
2892 ===== ====
2893
2894 Ptr ->
2895 +------------+
2896 | TypeSym | <- Ptr
2897 |------------| +------------+
2898 | ArraySym | | ArraySym |
2899 |------------| |------------|
2900 | Name | | Name |
2901 |------------| |------------|
2902 *)
2903
2904 PROCEDURE EndBuildArray ;
2905 VAR
2906 typetok,
2907 arraytok,
2908 combinedtok: CARDINAL ;
2909 TypeSym,
2910 ArraySym : CARDINAL ;
2911 BEGIN
2912 PopTtok (TypeSym, typetok) ;
2913 PopTtok (ArraySym, arraytok) ;
2914 Assert (IsArray (ArraySym)) ;
2915 combinedtok := MakeVirtual2Tok (arraytok, typetok) ;
2916 PutArray (ArraySym, TypeSym) ;
2917 PutDeclared (combinedtok, ArraySym) ;
2918 PushTtok (ArraySym, combinedtok) ;
2919 Annotate ("%1s(%1d)||array type")
2920 END EndBuildArray ;
2921
2922
2923 (*
2924 BuildFieldArray - Builds a field into an array sym.
2925 The Stack:
2926
2927
2928 Entry Exit
2929 ===== ====
2930
2931 Ptr ->
2932 +-------------+
2933 | Type | Name | <- Ptr
2934 |-------------| +-------------+
2935 | ArraySym | | ArraySym |
2936 |-------------| |-------------|
2937 | ArrayName | | ArrayName |
2938 |-------------| |-------------|
2939 *)
2940
2941 PROCEDURE BuildFieldArray ;
2942 VAR
2943 typetok,
2944 arraytok : CARDINAL ;
2945 Subscript,
2946 Type,
2947 Array : CARDINAL ;
2948 name : Name ;
2949 BEGIN
2950 PopTFtok (Type, name, typetok) ;
2951 PopTtok (Array, arraytok) ;
2952 Assert (IsArray (Array)) ;
2953 Subscript := MakeSubscript () ;
2954 (*
2955 We cannot Assert(IsSubrange(Type)) as the subrange type might be
2956 declared later on in the file.
2957 We also note it could be an ordinal type or enumerated type.
2958 Therefore we must save this information and deal with the
2959 different cases in M2GCCDeclare.mod and M2GenGCC.mod.
2960 However this works to our advantage as it preserves the
2961 actual declaration as specified by the source file.
2962 *)
2963 PutSubscript (Subscript, Type) ;
2964 PutArraySubscript (Array, Subscript) ;
2965 PushTtok (Array, arraytok) ;
2966 Annotate ("%1s(%1d)||array type")
2967 (* ; WriteString('Field Placed in Array') ; WriteLn *)
2968 END BuildFieldArray ;
2969
2970
2971 (*
2972 BuildArrayComma - converts ARRAY [..], [..] OF into ARRAY [..] OF ARRAY [..]
2973
2974
2975 Ptr -> <- Ptr
2976 +-------------+ +-------------+
2977 | ArraySym1 | | ArraySym2 |
2978 |-------------| |-------------|
2979 | ArrayName | | ArrayName |
2980 |-------------| |-------------|
2981 *)
2982
2983 PROCEDURE BuildArrayComma ;
2984 VAR
2985 Nothing,
2986 ArraySym1,
2987 ArraySym2: CARDINAL ;
2988 BEGIN
2989 PushT(NulName) ;
2990 StartBuildArray ;
2991 PopT(ArraySym2) ;
2992 PopT(Nothing) ;
2993 PushT(ArraySym2) ;
2994 EndBuildArray ;
2995 PopT(ArraySym1) ;
2996 PushT(ArraySym2) ;
2997 Annotate("%1s(%1d)||array type comma")
2998 END BuildArrayComma ;
2999
3000
3001 (*
3002 BuildProcedureType - builds a procedure type symbol.
3003 The Stack:
3004
3005
3006 <- Ptr
3007 +-------------+
3008 Ptr -> | ProcTypeSym |
3009 +-------------+ |-------------|
3010 | Name | | Name |
3011 |-------------| |-------------|
3012 *)
3013
3014 PROCEDURE BuildProcedureType ;
3015 VAR
3016 tok : CARDINAL ;
3017 name : Name ;
3018 ProcTypeSym: CARDINAL ;
3019 BEGIN
3020 name := OperandT (1) ;
3021 tok := OperandTok (1) ;
3022 ProcTypeSym := MakeProcType (tok, name) ;
3023 CheckForExportedImplementation (ProcTypeSym) ; (* May be an exported hidden type *)
3024 Annotate ("%1n||procedure type name") ;
3025 PushTtok (ProcTypeSym, tok) ;
3026 Annotate ("%1s(%1d)|%3d||proc type|token no")
3027 END BuildProcedureType ;
3028
3029
3030 (*
3031 BuildFormalType - Builds a Formal Parameter in a procedure type.
3032
3033 The Stack:
3034
3035 Entry Exit
3036
3037 Ptr ->
3038 +------------+
3039 | TypeSym |
3040 |------------|
3041 | Array/Nul |
3042 |------------|
3043 | Var / Nul | <- Ptr
3044 |------------| +--------------+
3045 | ProcTypeSym| | ProcTypeSym |
3046 |------------| |--------------|
3047 *)
3048
3049 PROCEDURE BuildFormalType ;
3050 VAR
3051 tok : CARDINAL ;
3052 Array, Var : Name ;
3053 TypeSym,
3054 UnboundedSym,
3055 ProcTypeSym: CARDINAL ;
3056 BEGIN
3057 PopT(TypeSym) ;
3058 PopT(Array) ;
3059 PopT(Var) ;
3060 PopT(ProcTypeSym) ;
3061 tok := GetTokenNo () ;
3062
3063 Assert( (Array=ArrayTok) OR (Array=NulTok) ) ;
3064 Assert(IsProcType(ProcTypeSym)) ;
3065 Assert( (Var=VarTok) OR (Var=NulTok) ) ;
3066
3067 IF Array=ArrayTok
3068 THEN
3069 UnboundedSym := MakeUnbounded(tok, TypeSym, 1) ;
3070 TypeSym := UnboundedSym
3071 END ;
3072 IF Var=VarTok
3073 THEN
3074 (* VAR parameter *)
3075 PutProcTypeVarParam (tok, ProcTypeSym, TypeSym, IsUnbounded (TypeSym))
3076 ELSE
3077 (* Non VAR parameter *)
3078 PutProcTypeParam (tok, ProcTypeSym, TypeSym, IsUnbounded (TypeSym))
3079 END ;
3080 PushT(ProcTypeSym) ;
3081 Annotate("%1s(%1d)||proc type")
3082 END BuildFormalType ;
3083
3084
3085 (*
3086 SaveRememberedConstructor -
3087 *)
3088
3089 PROCEDURE SaveRememberedConstructor ;
3090 BEGIN
3091 (*
3092 IF RememberedConstant=NulSym
3093 THEN
3094 RememberedConstant := MakeTemporary(ImmediateValue)
3095 END ;
3096 PutConstructorIntoFifoQueue(RememberedConstant)
3097 *)
3098
3099 END SaveRememberedConstructor ;
3100
3101
3102 (*
3103 GetSeenString - returns a string corresponding to, s.
3104 *)
3105
3106 PROCEDURE GetSeenString (s: constType) : String ;
3107 BEGIN
3108 CASE s OF
3109
3110 unknown : RETURN( InitString('unknown') ) |
3111 set : RETURN( InitString('SET') ) |
3112 str : RETURN( InitString('string') ) |
3113 constructor: RETURN( InitString('constructor') ) |
3114 array : RETURN( InitString('ARRAY') ) |
3115 cast : RETURN( InitStringCharStar(KeyToCharStar(GetSymName(castType))) ) |
3116 boolean : RETURN( InitString('BOOLEAN') ) |
3117 ztype : RETURN( InitString('Z type') ) |
3118 rtype : RETURN( InitString('R type') ) |
3119 ctype : RETURN( InitString('C type') ) |
3120 procedure : RETURN( InitString('PROCEDURE') )
3121
3122 ELSE
3123 InternalError ('unexpected value of type')
3124 END ;
3125 RETURN( NIL )
3126 END GetSeenString ;
3127
3128
3129 (*
3130 SetTypeTo - attempts to set, type, to, s.
3131 *)
3132
3133 PROCEDURE SetTypeTo (s: constType) ;
3134 VAR
3135 s1, s2, s3: String ;
3136 BEGIN
3137 IF type=unknown
3138 THEN
3139 type := s
3140 ELSIF (type=constructor) AND (s#str)
3141 THEN
3142 type := s
3143 ELSIF (s=constructor) AND ((type=array) OR (type=set))
3144 THEN
3145 (* leave it alone *)
3146 ELSIF type#s
3147 THEN
3148 s1 := GetSeenString(type) ;
3149 s2 := GetSeenString(s) ;
3150 s3 := Sprintf2(InitString('cannot create a %s constant together with a %s constant'), s1, s2) ;
3151 ErrorStringAt(s3, GetTokenNo()) ;
3152 s1 := KillString(s1) ;
3153 s2 := KillString(s2)
3154 END
3155 END SetTypeTo ;
3156
3157
3158 (*
3159 SeenUnknown - sets the operand type to unknown.
3160 *)
3161
3162 PROCEDURE SeenUnknown ;
3163 BEGIN
3164 type := unknown
3165 END SeenUnknown ;
3166
3167
3168 (*
3169 SeenCast - sets the operand type to cast.
3170 *)
3171
3172 PROCEDURE SeenCast (sym: CARDINAL) ;
3173 BEGIN
3174 type := cast ;
3175 castType := sym ;
3176 Assert(IsAModula2Type(sym)) ;
3177 END SeenCast ;
3178
3179
3180 (*
3181 SeenBoolean - sets the operand type to a BOOLEAN.
3182 *)
3183
3184 PROCEDURE SeenBoolean ;
3185 BEGIN
3186 type := boolean
3187 END SeenBoolean ;
3188
3189
3190 (*
3191 SeenZType - sets the operand type to a Z type.
3192 *)
3193
3194 PROCEDURE SeenZType ;
3195 BEGIN
3196 type := ztype
3197 END SeenZType ;
3198
3199
3200 (*
3201 SeenRType - sets the operand type to a R type.
3202 *)
3203
3204 PROCEDURE SeenRType ;
3205 BEGIN
3206 type := rtype
3207 END SeenRType ;
3208
3209
3210 (*
3211 SeenCType - sets the operand type to a C type.
3212 *)
3213
3214 PROCEDURE SeenCType ;
3215 BEGIN
3216 type := ctype
3217 END SeenCType ;
3218
3219
3220 (*
3221 SeenSet - sets the operand type to set.
3222 *)
3223
3224 PROCEDURE SeenSet ;
3225 BEGIN
3226 SetTypeTo(set) ;
3227 SaveRememberedConstructor
3228 END SeenSet ;
3229
3230
3231 (*
3232 SeenArray - sets the operand type to array.
3233 *)
3234
3235 PROCEDURE SeenArray ;
3236 BEGIN
3237 SetTypeTo(array)
3238 END SeenArray ;
3239
3240
3241 (*
3242 SeenConstructor - sets the operand type to constructor.
3243 *)
3244
3245 PROCEDURE SeenConstructor ;
3246 BEGIN
3247 SetTypeTo(constructor) ;
3248 SaveRememberedConstructor
3249 END SeenConstructor ;
3250
3251
3252 (*
3253 SeenString - sets the operand type to string.
3254 *)
3255
3256 PROCEDURE SeenString ;
3257 BEGIN
3258 SetTypeTo(str)
3259 END SeenString ;
3260
3261
3262 (*
3263 DetermineType - assigns the top of stack symbol with the type of
3264 constant expression, if known.
3265 *)
3266
3267 PROCEDURE DetermineType ;
3268 VAR
3269 Sym: CARDINAL ;
3270 BEGIN
3271 Sym := OperandT(1) ;
3272 CASE type OF
3273
3274 set : PutConstSet(Sym) |
3275 str : PutConstStringKnown (GetTokenNo(), Sym, MakeKey(''), FALSE, FALSE) |
3276 array,
3277 constructor: PutConstructor(Sym) |
3278 cast : PutConst(Sym, castType) |
3279 unknown :
3280
3281 ELSE
3282 END
3283 END DetermineType ;
3284
3285
3286 (*
3287 PushType -
3288 *)
3289
3290 PROCEDURE PushType ;
3291 BEGIN
3292 PushWord(TypeStack, type)
3293 END PushType ;
3294
3295
3296 (*
3297 PopType -
3298 *)
3299
3300 PROCEDURE PopType ;
3301 BEGIN
3302 type := PopWord(TypeStack)
3303 END PopType ;
3304
3305
3306 (*
3307 PushRememberConstant -
3308 *)
3309
3310 PROCEDURE PushRememberConstant ;
3311 BEGIN
3312 PushWord(RememberStack, RememberedConstant) ;
3313 RememberConstant(NulSym)
3314 END PushRememberConstant ;
3315
3316
3317 (*
3318 PopRememberConstant -
3319 *)
3320
3321 PROCEDURE PopRememberConstant ;
3322 BEGIN
3323 RememberedConstant := PopWord(RememberStack)
3324 END PopRememberConstant ;
3325
3326
3327 (*
3328 RememberConstant -
3329 *)
3330
3331 PROCEDURE RememberConstant (sym: CARDINAL) ;
3332 BEGIN
3333 RememberedConstant := sym
3334 END RememberConstant ;
3335
3336
3337 BEGIN
3338 alignTypeNo := 0 ;
3339 TypeStack := InitStackWord () ;
3340 RememberStack := InitStackWord () ;
3341 BlockStack := InitStackWord () ;
3342 castType := NulSym
3343 END P2SymBuild.