Debian Codegraph @

gcc/m2/gm2-compiler/PCSymBuild.mod

raw
1 (* PCSymBuild.mod pass C 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 PCSymBuild ;
23
24
25 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
26 FROM NameKey IMPORT Name, WriteKey, MakeKey, NulName ;
27 FROM StrIO IMPORT WriteString, WriteLn ;
28 FROM NumberIO IMPORT WriteCard ;
29 FROM M2Debug IMPORT Assert, WriteDebug ;
30 FROM M2Error IMPORT WriteFormat0, WriteFormat1, WriteFormat2, FlushErrors, InternalError, NewError, ErrorFormat0 ;
31 FROM M2MetaError IMPORT MetaError1, MetaErrorT1 ;
32 FROM M2LexBuf IMPORT GetTokenNo ;
33 FROM M2Reserved IMPORT NulTok, ImportTok ;
34 FROM M2Const IMPORT constType ;
35 FROM Indexing IMPORT Index, InitIndex, GetIndice, PutIndice, InBounds, IncludeIndiceIntoIndex, HighIndice ;
36
37 FROM M2Quads IMPORT PushT, PopT, OperandT, PopN, PopTF, PushTF, IsAutoPushOn,
38 PopNothing, PushTFn, PopTFn, PushTtok, PopTtok, PushTFtok, PopTFtok, OperandTok ;
39
40 FROM M2Options IMPORT Iso ;
41 FROM StdIO IMPORT Write ;
42 FROM M2System IMPORT Cast, IsPseudoSystemFunctionConstExpression ;
43
44 FROM M2Base IMPORT MixTypes,
45 ZType, RType, Char, Boolean, Val, Max, Min, Convert,
46 IsPseudoBaseFunction, IsRealType, IsComplexType, IsOrdinalType ;
47
48 FROM M2Reserved IMPORT PlusTok, MinusTok, TimesTok, DivTok, ModTok,
49 DivideTok, RemTok,
50 OrTok, AndTok, AmbersandTok,
51 EqualTok, LessEqualTok, GreaterEqualTok,
52 LessTok, GreaterTok, HashTok, LessGreaterTok,
53 InTok, NotTok ;
54
55 FROM SymbolTable IMPORT NulSym, ModeOfAddr, ProcedureKind,
56 StartScope, EndScope, GetScope, GetCurrentScope,
57 GetModuleScope,
58 SetCurrentModule, GetCurrentModule, SetFileModule,
59 GetExported,
60 IsDefImp, IsModule,
61 RequestSym,
62 IsProcedure, PutOptArgInit, IsEnumeration,
63 CheckForUnknownInModule,
64 GetFromOuterModule,
65 CheckForEnumerationInCurrentModule,
66 GetMode, PutVariableAtAddress, ModeOfAddr, SkipType,
67 IsSet, PutConstSet, IsType,
68 IsConst, IsConstructor, PutConst, PutConstructor,
69 PopValue, PushValue,
70 MakeTemporary, PutVar,
71 PutSubrange,
72 GetSymName,
73 CheckAnonymous,
74 IsProcedureBuiltin,
75 MakeProcType,
76 NoOfParamAny,
77 GetParam,
78 IsParameterVar, PutProcTypeParam,
79 PutProcTypeVarParam, IsParameterUnbounded,
80 PutFunction, PutProcTypeParam,
81 GetType, IsVar,
82 IsAModula2Type, GetDeclaredMod ;
83
84 FROM M2Batch IMPORT MakeDefinitionSource,
85 MakeImplementationSource,
86 MakeProgramSource,
87 LookupModule, LookupOuterModule ;
88
89 FROM M2Comp IMPORT CompilingDefinitionModule,
90 CompilingImplementationModule,
91 CompilingProgramModule ;
92
93 FROM M2StackAddress IMPORT StackOfAddress, InitStackAddress, KillStackAddress,
94 PushAddress, PopAddress, PeepAddress,
95 IsEmptyAddress, NoOfItemsInStackAddress ;
96
97 FROM M2StackWord IMPORT StackOfWord, InitStackWord, KillStackWord,
98 PushWord, PopWord, PeepWord,
99 IsEmptyWord, NoOfItemsInStackWord ;
100
101 IMPORT M2Error ;
102
103
104 CONST
105 Debugging = FALSE ;
106
107 TYPE
108 tagType = (leaf, unary, binary, designator, expr, convert, function) ;
109
110 exprNode = POINTER TO eNode ;
111
112 eDes = RECORD
113 type: CARDINAL ;
114 meta: constType ;
115 sym : CARDINAL ;
116 left: exprNode ;
117 END ;
118
119 eLeaf = RECORD
120 type: CARDINAL ;
121 meta: constType ;
122 sym: CARDINAL ;
123 END ;
124
125 eUnary = RECORD
126 type: CARDINAL ;
127 meta: constType ;
128 left: exprNode ;
129 op : Name ;
130 END ;
131
132 eBinary = RECORD
133 type: CARDINAL ;
134 meta: constType ;
135 left,
136 right: exprNode ;
137 op : Name ;
138 END ;
139
140 eExpr = RECORD
141 type: CARDINAL ;
142 meta: constType ;
143 left: exprNode ;
144 END ;
145
146 eFunction = RECORD
147 type : CARDINAL ;
148 meta : constType ;
149 func : CARDINAL ;
150 first,
151 second: exprNode ;
152 third : BOOLEAN ;
153 END ;
154
155 eConvert = RECORD
156 type : CARDINAL ;
157 meta : constType ;
158 totype: exprNode ;
159 expr : exprNode ;
160 END ;
161
162 eNode = RECORD
163 CASE tag: tagType OF
164
165 designator: edes : eDes |
166 leaf : eleaf : eLeaf |
167 unary : eunary : eUnary |
168 binary : ebinary : eBinary |
169 expr : eexpr : eExpr |
170 function : efunction: eFunction |
171 convert : econvert : eConvert
172
173 END
174 END ;
175
176
177 VAR
178 exprStack : StackOfAddress ;
179 constList : Index ;
180 constToken : CARDINAL ;
181 desStack : StackOfWord ;
182 inDesignator: BOOLEAN ;
183
184
185 (*
186 GetSkippedType -
187 *)
188
189 PROCEDURE GetSkippedType (sym: CARDINAL) : CARDINAL ;
190 BEGIN
191 RETURN( SkipType(GetType(sym)) )
192 END GetSkippedType ;
193
194
195 (*
196 CheckNotVar - checks to see that the top of stack is not a variable.
197 *)
198
199 PROCEDURE CheckNotVar (tok: CARDINAL) ;
200 VAR
201 const: CARDINAL ;
202 BEGIN
203 const := OperandT (1) ;
204 IF (const # NulSym) AND IsVar (const)
205 THEN
206 MetaErrorT1 (tok, 'not expecting a variable {%Aad} as a term in a constant expression', const)
207 END
208 END CheckNotVar ;
209
210
211 (*
212 StartBuildDefinitionModule - Creates a definition module and starts
213 a new scope.
214
215 The Stack is expected:
216
217 Entry Exit
218
219 Ptr -> <- Ptr
220 +------------+ +-----------+
221 | NameStart | | NameStart |
222 |------------| |-----------|
223
224 *)
225
226 PROCEDURE PCStartBuildDefModule ;
227 VAR
228 tok : CARDINAL ;
229 name : Name ;
230 ModuleSym: CARDINAL ;
231 BEGIN
232 PopTtok(name, tok) ;
233 ModuleSym := MakeDefinitionSource(tok, name) ;
234 SetCurrentModule(ModuleSym) ;
235 SetFileModule(ModuleSym) ;
236 StartScope(ModuleSym) ;
237 Assert(IsDefImp(ModuleSym)) ;
238 Assert(CompilingDefinitionModule()) ;
239 PushT(name) ;
240 M2Error.EnterDefinitionScope (name)
241 END PCStartBuildDefModule ;
242
243
244 (*
245 EndBuildDefinitionModule - Destroys the definition module scope and
246 checks for correct name.
247
248 The Stack is expected:
249
250 Entry Exit
251
252 Ptr ->
253 +------------+ +-----------+
254 | NameEnd | | |
255 |------------| |-----------|
256 | NameStart | | | <- Ptr
257 |------------| |-----------|
258 *)
259
260 PROCEDURE PCEndBuildDefModule ;
261 VAR
262 NameStart,
263 NameEnd : CARDINAL ;
264 BEGIN
265 Assert(CompilingDefinitionModule()) ;
266 CheckForUnknownInModule ;
267 EndScope ;
268 PopT(NameEnd) ;
269 PopT(NameStart) ;
270 IF NameStart#NameEnd
271 THEN
272 WriteFormat2('inconsistent definition module was named (%a) and concluded as (%a)',
273 NameStart, NameEnd)
274 END ;
275 M2Error.LeaveErrorScope
276 END PCEndBuildDefModule ;
277
278
279 (*
280 StartBuildImplementationModule - Creates an implementation module and starts
281 a new scope.
282
283 The Stack is expected:
284
285 Entry Exit
286
287 Ptr -> <- Ptr
288 +------------+ +-----------+
289 | NameStart | | NameStart |
290 |------------| |-----------|
291
292 *)
293
294 PROCEDURE PCStartBuildImpModule ;
295 VAR
296 tok : CARDINAL ;
297 name : Name ;
298 ModuleSym: CARDINAL ;
299 BEGIN
300 PopTtok(name, tok) ;
301 ModuleSym := MakeImplementationSource(tok, name) ;
302 SetCurrentModule(ModuleSym) ;
303 SetFileModule(ModuleSym) ;
304 StartScope(ModuleSym) ;
305 Assert(IsDefImp(ModuleSym)) ;
306 Assert(CompilingImplementationModule()) ;
307 PushTtok(name, tok) ;
308 M2Error.EnterImplementationScope (name)
309 END PCStartBuildImpModule ;
310
311
312 (*
313 EndBuildImplementationModule - Destroys the implementation module scope and
314 checks for correct name.
315
316 The Stack is expected:
317
318 Entry Exit
319
320 Ptr ->
321 +------------+ +-----------+
322 | NameEnd | | |
323 |------------| |-----------|
324 | NameStart | | | <- Ptr
325 |------------| |-----------|
326 *)
327
328 PROCEDURE PCEndBuildImpModule ;
329 VAR
330 NameStart,
331 NameEnd : Name ;
332 BEGIN
333 Assert(CompilingImplementationModule()) ;
334 CheckForUnknownInModule ;
335 EndScope ;
336 PopT(NameEnd) ;
337 PopT(NameStart) ;
338 IF NameStart#NameEnd
339 THEN
340 (* we dont issue an error based around incorrect module names as this is done in P1 and P2.
341 If we get here then something has gone wrong with our error recovery in PC, so we bail out.
342 *)
343 WriteFormat0('too many errors in pass 3') ;
344 FlushErrors
345 END ;
346 M2Error.LeaveErrorScope
347 END PCEndBuildImpModule ;
348
349
350 (*
351 StartBuildProgramModule - Creates a program module and starts
352 a new scope.
353
354 The Stack is expected:
355
356 Entry Exit
357
358 Ptr -> <- Ptr
359 +------------+ +-----------+
360 | NameStart | | NameStart |
361 |------------| |-----------|
362
363 *)
364
365 PROCEDURE PCStartBuildProgModule ;
366 VAR
367 tok : CARDINAL ;
368 name : Name ;
369 ModuleSym: CARDINAL ;
370 BEGIN
371 (* WriteString('StartBuildProgramModule') ; WriteLn ; *)
372 PopTtok(name, tok) ;
373 ModuleSym := MakeProgramSource(tok, name) ;
374 SetCurrentModule(ModuleSym) ;
375 SetFileModule(ModuleSym) ;
376 (* WriteString('MODULE - ') ; WriteKey(GetSymName(ModuleSym)) ; WriteLn ; *)
377 StartScope(ModuleSym) ;
378 Assert(CompilingProgramModule()) ;
379 Assert(NOT IsDefImp(ModuleSym)) ;
380 PushTtok(name, tok) ;
381 M2Error.EnterProgramScope (name)
382 END PCStartBuildProgModule ;
383
384
385 (*
386 EndBuildProgramModule - Destroys the program module scope and
387 checks for correct name.
388
389 The Stack is expected:
390
391 Entry Exit
392
393 Ptr ->
394 +------------+ +-----------+
395 | NameEnd | | |
396 |------------| |-----------|
397 | NameStart | | | <- Ptr
398 |------------| |-----------|
399 *)
400
401 PROCEDURE PCEndBuildProgModule ;
402 VAR
403 NameStart,
404 NameEnd : Name ;
405 BEGIN
406 Assert(CompilingProgramModule()) ;
407 CheckForUnknownInModule ;
408 EndScope ;
409 PopT(NameEnd) ;
410 PopT(NameStart) ;
411 IF NameStart#NameEnd
412 THEN
413 (* we dont issue an error based around incorrect module names this would be done in P1 and P2.
414 If we get here then something has gone wrong with our error recovery in PC, so we bail out.
415 *)
416 WriteFormat0('too many errors in pass 3') ;
417 FlushErrors
418 END ;
419 M2Error.LeaveErrorScope
420 END PCEndBuildProgModule ;
421
422
423 (*
424 StartBuildInnerModule - Creates an Inner module and starts
425 a new scope.
426
427 The Stack is expected:
428
429 Entry Exit
430
431 Ptr -> <- Ptr
432 +------------+ +-----------+
433 | NameStart | | NameStart |
434 |------------| |-----------|
435
436 *)
437
438 PROCEDURE PCStartBuildInnerModule ;
439 VAR
440 name : Name ;
441 tok : CARDINAL ;
442 ModuleSym: CARDINAL ;
443 BEGIN
444 PopTtok(name, tok) ;
445 ModuleSym := RequestSym(tok, name) ;
446 Assert(IsModule(ModuleSym)) ;
447 StartScope(ModuleSym) ;
448 Assert(NOT IsDefImp(ModuleSym)) ;
449 SetCurrentModule(ModuleSym) ;
450 PushTtok(name, tok) ;
451 M2Error.EnterModuleScope (name)
452 END PCStartBuildInnerModule ;
453
454
455 (*
456 EndBuildInnerModule - Destroys the Inner module scope and
457 checks for correct name.
458
459 The Stack is expected:
460
461 Entry Exit
462
463 Ptr ->
464 +------------+ +-----------+
465 | NameEnd | | |
466 |------------| |-----------|
467 | NameStart | | | <- Ptr
468 |------------| |-----------|
469 *)
470
471 PROCEDURE PCEndBuildInnerModule ;
472 VAR
473 NameStart,
474 NameEnd : Name ;
475 BEGIN
476 CheckForUnknownInModule ;
477 EndScope ;
478 PopT(NameEnd) ;
479 PopT(NameStart) ;
480 IF NameStart#NameEnd
481 THEN
482 (* we dont issue an error based around incorrect module names this would be done in P1 and P2.
483 If we get here then something has gone wrong with our error recovery in PC, so we bail out.
484 *)
485 WriteFormat0('too many errors in pass 3') ;
486 FlushErrors
487 END ;
488 SetCurrentModule(GetModuleScope(GetCurrentModule())) ;
489 M2Error.LeaveErrorScope
490 END PCEndBuildInnerModule ;
491
492
493 (*
494 BuildImportOuterModule - Builds imported identifiers into an outer module
495 from a definition module.
496
497 The Stack is expected:
498
499 Entry OR Entry
500
501 Ptr -> Ptr ->
502 +------------+ +-----------+
503 | # | | # |
504 |------------| |-----------|
505 | Id1 | | Id1 |
506 |------------| |-----------|
507 . . . .
508 . . . .
509 . . . .
510 |------------| |-----------|
511 | Id# | | Id# |
512 |------------| |-----------|
513 | ImportTok | | Ident |
514 |------------| |-----------|
515
516 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
517
518
519 Exit
520
521 All above stack discarded
522 *)
523
524 PROCEDURE PCBuildImportOuterModule ;
525 VAR
526 Sym, ModSym,
527 i, n : CARDINAL ;
528 BEGIN
529 PopT (n) ; (* n = # of the Ident List *)
530 IF OperandT (n+1) # ImportTok
531 THEN
532 (* Ident List contains list of objects imported from ModSym *)
533 ModSym := LookupModule (OperandTok (n+1), OperandT (n+1)) ;
534 i := 1 ;
535 WHILE i<=n DO
536 Sym := GetExported (OperandTok (i), ModSym, OperandT (i)) ;
537 CheckForEnumerationInCurrentModule (Sym) ;
538 INC (i)
539 END
540 END ;
541 PopN (n+1) (* clear stack *)
542 END PCBuildImportOuterModule ;
543
544
545 (*
546 BuildImportInnerModule - Builds imported identifiers into an inner module
547 from the last level of module.
548
549 The Stack is expected:
550
551 Entry OR Entry
552
553 Ptr -> Ptr ->
554 +------------+ +-----------+
555 | # | | # |
556 |------------| |-----------|
557 | Id1 | | Id1 |
558 |------------| |-----------|
559 . . . .
560 . . . .
561 . . . .
562 |------------| |-----------|
563 | Id# | | Id# |
564 |------------| |-----------|
565 | ImportTok | | Ident |
566 |------------| |-----------|
567
568 IMPORT Id1, .. Id# ; FROM Ident IMPORT Id1 .. Id# ;
569
570 Exit
571
572 All above stack discarded
573 *)
574
575 PROCEDURE PCBuildImportInnerModule ;
576 VAR
577 Sym, ModSym,
578 n, i : CARDINAL ;
579 BEGIN
580 PopT (n) ; (* i = # of the Ident List *)
581 IF OperandT (n+1) = ImportTok
582 THEN
583 (* Ident List contains list of objects *)
584 i := 1 ;
585 WHILE i<=n DO
586 Sym := GetFromOuterModule (OperandTok (i), OperandT (i)) ;
587 CheckForEnumerationInCurrentModule (Sym) ;
588 INC (i)
589 END
590 ELSE
591 (* Ident List contains list of objects imported from ModSym *)
592 ModSym := LookupOuterModule (OperandTok (n+1), OperandT (n+1)) ;
593 i := 1 ;
594 WHILE i<=n DO
595 Sym := GetExported (OperandTok (i), ModSym, OperandT (i)) ;
596 CheckForEnumerationInCurrentModule (Sym) ;
597 INC (i)
598 END
599 END ;
600 PopN (n+1) (* Clear Stack *)
601 END PCBuildImportInnerModule ;
602
603
604 (*
605 StartBuildProcedure - Builds a Procedure.
606
607 The Stack:
608
609 Entry Exit
610
611 <- Ptr
612 +------------+
613 Ptr -> | ProcSym |
614 +------------+ |------------|
615 | Name | | Name |
616 |------------| |------------|
617 *)
618
619 PROCEDURE PCStartBuildProcedure ;
620 VAR
621 name : Name ;
622 ProcSym : CARDINAL ;
623 tok : CARDINAL ;
624 BEGIN
625 PopTtok(name, tok) ;
626 PushTtok(name, tok) ; (* Name saved for the EndBuildProcedure name check *)
627 ProcSym := RequestSym (tok, name) ;
628 Assert (IsProcedure (ProcSym)) ;
629 PushTtok (ProcSym, tok) ;
630 StartScope (ProcSym) ;
631 M2Error.EnterProcedureScope (name)
632 END PCStartBuildProcedure ;
633
634
635 (*
636 EndBuildProcedure - Ends building a Procedure.
637 It checks the start procedure name matches the end
638 procedure name.
639
640 The Stack:
641
642 (Procedure Not Defined in definition module)
643
644 Entry Exit
645
646 Ptr ->
647 +------------+
648 | NameEnd |
649 |------------|
650 | ProcSym |
651 |------------|
652 | NameStart |
653 |------------|
654 Empty
655 *)
656
657 PROCEDURE PCEndBuildProcedure ;
658 VAR
659 ProcSym : CARDINAL ;
660 NameEnd,
661 NameStart: Name ;
662 BEGIN
663 PopT(NameEnd) ;
664 PopT(ProcSym) ;
665 PopT(NameStart) ;
666 IF NameEnd#NameStart
667 THEN
668 (* we dont issue an error based around incorrect module names this would be done in P1 and P2.
669 If we get here then something has gone wrong with our error recovery in PC, so we bail out.
670 *)
671 WriteFormat0('too many errors in pass 3') ;
672 FlushErrors
673 END ;
674 EndScope ;
675 M2Error.LeaveErrorScope
676 END PCEndBuildProcedure ;
677
678
679 (*
680 EndBuildForward - Ends building a forward declaration.
681
682 The Stack:
683
684 Entry Exit
685
686 Ptr ->
687 +------------+
688 | ProcSym |
689 |------------|
690 | NameStart |
691 |------------|
692 Empty
693 *)
694
695 PROCEDURE PCEndBuildForward ;
696 BEGIN
697 PopN (2)
698 END PCEndBuildForward ;
699
700
701 (*
702 BuildProcedureHeading - Builds a procedure heading for the definition
703 module procedures.
704
705 Operation only performed if compiling a
706 definition module.
707
708 The Stack:
709
710 Entry Exit
711
712 Ptr ->
713 +------------+
714 | ProcSym |
715 |------------|
716 | NameStart |
717 |------------|
718 Empty
719
720 *)
721
722 PROCEDURE PCBuildProcedureHeading ;
723 VAR
724 ProcSym : CARDINAL ;
725 NameStart: Name ;
726 BEGIN
727 IF CompilingDefinitionModule ()
728 THEN
729 PopT (ProcSym) ;
730 PopT (NameStart) ;
731 EndScope
732 END
733 END PCBuildProcedureHeading ;
734
735
736 (*
737 BuildNulName - Pushes a NulKey onto the top of the stack.
738 The Stack:
739
740
741 Entry Exit
742
743 <- Ptr
744 Empty +------------+
745 | NulKey |
746 |------------|
747 *)
748
749 PROCEDURE BuildNulName ;
750 BEGIN
751 PushT (NulName)
752 END BuildNulName ;
753
754
755 (*
756 BuildConst - builds a constant.
757 Stack
758
759 Entry Exit
760
761 Ptr -> <- Ptr
762 +------------+ +------------+
763 | Name | | Sym |
764 |------------+ |------------|
765 *)
766
767 PROCEDURE BuildConst ;
768 VAR
769 name: Name ;
770 tok : CARDINAL ;
771 Sym : CARDINAL ;
772 BEGIN
773 PopTtok (name, tok) ;
774 Sym := RequestSym (tok, name) ;
775 PushTtok (Sym, tok)
776 END BuildConst ;
777
778
779 (*
780 BuildVarAtAddress - updates the symbol table entry of, variable sym, to be declared
781 at address, address.
782
783 Stack
784
785 Entry Exit
786
787 Ptr ->
788 +--------------+
789 | Expr | EType | <- Ptr
790 |--------------+ +--------------+
791 | name | SType | | name | SType |
792 |--------------+ |--------------|
793 *)
794
795 (*
796 PROCEDURE BuildVarAtAddress ;
797 VAR
798 name : Name ;
799 Sym, SType,
800 Exp, EType: CARDINAL ;
801 etok, ntok: CARDINAL ;
802 BEGIN
803 PopTFtok (Exp, EType, etok) ;
804 PopTFtok (name, SType, ntok) ;
805 PushTFtok (name, SType, ntok) ;
806 Sym := RequestSym (ntok, name) ;
807 IF GetMode(Sym)=LeftValue
808 THEN
809 PutVariableAtAddress(Sym, Exp)
810 ELSE
811 InternalError ('expecting lvalue for this variable which is declared at an explicit address')
812 END
813 END BuildVarAtAddress ;
814 *)
815
816
817 (*
818 BuildOptArgInitializer - assigns the constant value symbol, const, to be the
819 initial value of the optional parameter should it be
820 absent.
821
822 Ptr ->
823 +------------+
824 | const |
825 |------------| <- Ptr
826 *)
827
828 (*
829 PROCEDURE BuildOptArgInitializer ;
830 VAR
831 const: CARDINAL ;
832 BEGIN
833 PopT(const) ;
834 PutOptArgInit(GetCurrentScope(), const)
835 END BuildOptArgInitializer ;
836 *)
837
838
839 (*
840 InitDesExpr -
841 *)
842
843 PROCEDURE InitDesExpr (des: CARDINAL) ;
844 VAR
845 e: exprNode ;
846 BEGIN
847 NEW (e) ;
848 WITH e^ DO
849 tag := designator ;
850 CASE tag OF
851
852 designator: WITH edes DO
853 type := NulSym ;
854 meta := unknown ;
855 tag := designator ;
856 sym := des ;
857 left := NIL
858 END
859
860 ELSE
861 InternalError ('expecting designator')
862 END
863 END ;
864 PushAddress (exprStack, e)
865 END InitDesExpr ;
866
867
868 (*
869 DebugNode -
870 *)
871
872 PROCEDURE DebugNode (d: exprNode) ;
873 BEGIN
874 IF Debugging AND (d#NIL)
875 THEN
876 WITH d^ DO
877 CASE tag OF
878
879 designator: DebugDes(d) |
880 expr : DebugExpr(d) |
881 leaf : DebugLeaf(d) |
882 unary : DebugUnary(d) |
883 binary : DebugBinary(d) |
884 function : DebugFunction(d) |
885 convert : DebugConvert(d)
886
887 END
888 END
889 END
890 END DebugNode ;
891
892
893 (*
894 DebugDes -
895 *)
896
897 PROCEDURE DebugDes (d: exprNode) ;
898 BEGIN
899 WITH d^ DO
900 WITH edes DO
901 DebugSym(sym) ; Write(':') ; DebugMeta(meta) ; Write(':') ; DebugType(type) ;
902 WriteString(' = ') ;
903 DebugNode(left) ;
904 WriteLn
905 END
906 END
907 END DebugDes ;
908
909
910 (*
911 DebugSym -
912 *)
913
914 PROCEDURE DebugSym (sym: CARDINAL) ;
915 VAR
916 n: Name ;
917 BEGIN
918 n := GetSymName(sym) ;
919 IF n#NulName
920 THEN
921 WriteKey(n)
922 END ;
923 Write(':') ; WriteCard(sym, 0)
924 END DebugSym ;
925
926
927 (*
928 DebugMeta -
929 *)
930
931 PROCEDURE DebugMeta (m: constType) ;
932 BEGIN
933 CASE m OF
934
935 unknown : WriteString('unknown') |
936 set : WriteString('set') |
937 str : WriteString('str') |
938 constructor: WriteString('constructor') |
939 array : WriteString('array') |
940 cast : WriteString('cast') |
941 boolean : WriteString('boolean') |
942 ztype : WriteString('ztype') |
943 rtype : WriteString('rtype') |
944 ctype : WriteString('ctype') |
945 procedure : WriteString('procedure') |
946 char : WriteString('ctype')
947
948 END
949 END DebugMeta ;
950
951
952 (*
953 DebugType -
954 *)
955
956 PROCEDURE DebugType (type: CARDINAL) ;
957 VAR
958 n: Name ;
959 BEGIN
960 WriteString('[type:') ;
961 IF type=NulSym
962 THEN
963 WriteString('<nulsym>')
964 ELSE
965 n := GetSymName(type) ;
966 IF n#NulSym
967 THEN
968 WriteKey(n)
969 END ;
970 Write(':') ; WriteCard(type, 0)
971 END ;
972 Write(']')
973 END DebugType ;
974
975
976 (*
977 DebugExpr -
978 *)
979
980 PROCEDURE DebugExpr (e: exprNode) ;
981 BEGIN
982 WITH e^.eexpr DO
983 WriteString('expr (') ;
984 DebugType(type) ; Write(':') ;
985 DebugMeta(meta) ; Write(' ') ;
986 DebugNode(left) ;
987 WriteString(') ')
988 END
989 END DebugExpr ;
990
991
992 (*
993 DebugFunction -
994 *)
995
996 PROCEDURE DebugFunction (f: exprNode) ;
997 BEGIN
998 WITH f^.efunction DO
999 WriteKey(GetSymName(func)) ;
1000 Write('(') ;
1001 IF first#NIL
1002 THEN
1003 DebugNode(first) ;
1004 IF second#NIL
1005 THEN
1006 WriteString(', ') ;
1007 DebugNode(second) ;
1008 IF third
1009 THEN
1010 WriteString(', ...')
1011 END
1012 END
1013 END ;
1014 Write(')')
1015 END
1016 END DebugFunction ;
1017
1018
1019 (*
1020 DebugConvert -
1021 *)
1022
1023 PROCEDURE DebugConvert (f: exprNode) ;
1024 BEGIN
1025 WITH f^.econvert DO
1026 DebugNode(totype) ;
1027 Write('(') ;
1028 DebugNode(expr) ;
1029 Write(')')
1030 END
1031 END DebugConvert ;
1032
1033
1034 (*
1035 DebugLeaf -
1036 *)
1037
1038 PROCEDURE DebugLeaf (l: exprNode) ;
1039 BEGIN
1040 WITH l^.eleaf DO
1041 WriteString('leaf (') ;
1042 DebugType(type) ; Write(':') ;
1043 DebugMeta(meta) ; Write(':') ;
1044 DebugSym(sym) ;
1045 WriteString(') ')
1046 END
1047 END DebugLeaf ;
1048
1049
1050 (*
1051 DebugUnary -
1052 *)
1053
1054 PROCEDURE DebugUnary (l: exprNode) ;
1055 BEGIN
1056 WITH l^.eunary DO
1057 WriteString('unary (') ;
1058 DebugType(type) ; Write(':') ;
1059 DebugMeta(meta) ; Write(' ') ;
1060 DebugOp(op) ; Write(' ') ;
1061 DebugNode(left) ;
1062 WriteString(') ')
1063 END
1064 END DebugUnary ;
1065
1066
1067 (*
1068 DebugBinary -
1069 *)
1070
1071 PROCEDURE DebugBinary (l: exprNode) ;
1072 BEGIN
1073 WITH l^.ebinary DO
1074 WriteString('unary (') ;
1075 DebugType(type) ; Write(':') ;
1076 DebugMeta(meta) ; Write(' ') ;
1077 DebugNode(left) ;
1078 DebugOp(op) ; Write(' ') ;
1079 DebugNode(right) ;
1080 WriteString(') ')
1081 END
1082 END DebugBinary ;
1083
1084
1085 (*
1086 DebugOp -
1087 *)
1088
1089 PROCEDURE DebugOp (op: Name) ;
1090 BEGIN
1091 WriteKey(op)
1092 END DebugOp ;
1093
1094
1095 (*
1096 PushInConstructor -
1097 *)
1098
1099 PROCEDURE PushInConstructor ;
1100 BEGIN
1101 PushWord(desStack, inDesignator) ;
1102 inDesignator := FALSE
1103 END PushInConstructor ;
1104
1105
1106 (*
1107 PopInConstructor -
1108 *)
1109
1110 PROCEDURE PopInConstructor ;
1111 BEGIN
1112 inDesignator := PopWord(desStack)
1113 END PopInConstructor ;
1114
1115
1116 (*
1117 StartDesConst -
1118 *)
1119
1120 PROCEDURE StartDesConst ;
1121 VAR
1122 name: Name ;
1123 tok : CARDINAL ;
1124 BEGIN
1125 inDesignator := TRUE ;
1126 exprStack := KillStackAddress (exprStack) ;
1127 exprStack := InitStackAddress () ;
1128 PopTtok (name, tok) ;
1129 InitDesExpr (RequestSym (tok, name))
1130 END StartDesConst ;
1131
1132
1133 (*
1134 EndDesConst -
1135 *)
1136
1137 PROCEDURE EndDesConst ;
1138 VAR
1139 d, e: exprNode ;
1140 BEGIN
1141 e := PopAddress (exprStack) ;
1142 d := PopAddress (exprStack) ;
1143 Assert(d^.tag=designator) ;
1144 d^.edes.left := e ;
1145 IncludeIndiceIntoIndex(constList, d) ;
1146 inDesignator := FALSE
1147 END EndDesConst ;
1148
1149
1150 (*
1151 fixupProcedureType - creates a proctype from a procedure.
1152 *)
1153
1154 PROCEDURE fixupProcedureType (p: CARDINAL) : CARDINAL ;
1155 VAR
1156 tok : CARDINAL ;
1157 par,
1158 t : CARDINAL ;
1159 n, i: CARDINAL ;
1160 BEGIN
1161 IF IsProcedure(p)
1162 THEN
1163 tok := GetTokenNo () ;
1164 t := MakeProcType (tok, CheckAnonymous (NulName)) ;
1165 i := 1 ;
1166 n := NoOfParamAny (p) ;
1167 WHILE i<=n DO
1168 par := GetParam (p, i) ;
1169 IF IsParameterVar (par)
1170 THEN
1171 PutProcTypeVarParam (tok, t, GetType (par), IsParameterUnbounded (par))
1172 ELSE
1173 PutProcTypeParam (tok, t, GetType (par), IsParameterUnbounded (par))
1174 END ;
1175 INC(i)
1176 END ;
1177 IF GetType (p) # NulSym
1178 THEN
1179 PutFunction (tok, t, ProperProcedure, GetType (p))
1180 END ;
1181 RETURN( t )
1182 ELSE
1183 InternalError ('expecting a procedure')
1184 END ;
1185 RETURN( NulSym )
1186 END fixupProcedureType ;
1187
1188
1189 (*
1190 InitFunction -
1191 *)
1192
1193 PROCEDURE InitFunction (m: constType; p, t: CARDINAL; f, s: exprNode; more: BOOLEAN) ;
1194 VAR
1195 n: exprNode ;
1196 BEGIN
1197 NEW (n) ;
1198 WITH n^ DO
1199 tag := function ;
1200 CASE tag OF
1201
1202 function: WITH efunction DO
1203 meta := m ;
1204 type := t ;
1205 func := p ;
1206 first := f ;
1207 second := s ;
1208 third := more
1209 END
1210
1211 ELSE
1212 InternalError ('expecting function')
1213 END
1214 END ;
1215 PushAddress (exprStack, n)
1216 END InitFunction ;
1217
1218
1219 (*
1220 InitConvert -
1221 *)
1222
1223 PROCEDURE InitConvert (m: constType; t: CARDINAL; to, e: exprNode) ;
1224 VAR
1225 n: exprNode ;
1226 BEGIN
1227 NEW(n) ;
1228 WITH n^ DO
1229 tag := convert ;
1230 CASE tag OF
1231
1232 convert: WITH econvert DO
1233 type := t ;
1234 meta := m ;
1235 totype := to ;
1236 expr := e
1237 END
1238
1239 ELSE
1240 InternalError ('expecting convert')
1241 END
1242 END ;
1243 PushAddress(exprStack, n)
1244 END InitConvert ;
1245
1246
1247 (*
1248 InitLeaf -
1249 *)
1250
1251 PROCEDURE InitLeaf (m: constType; s, t: CARDINAL) ;
1252 VAR
1253 l: exprNode ;
1254 BEGIN
1255 NEW (l) ;
1256 WITH l^ DO
1257 tag := leaf ;
1258 CASE tag OF
1259
1260 leaf: WITH eleaf DO
1261 type := t ;
1262 meta := m ;
1263 sym := s
1264 END
1265
1266 ELSE
1267 InternalError ('expecting leaf')
1268 END
1269 END ;
1270 PushAddress (exprStack, l)
1271 END InitLeaf ;
1272
1273
1274 (*
1275 InitProcedure -
1276 *)
1277
1278 PROCEDURE InitProcedure (s: CARDINAL) ;
1279 BEGIN
1280 InitLeaf(procedure, s, fixupProcedureType(s))
1281 END InitProcedure ;
1282
1283
1284 (*
1285 InitCharType -
1286 *)
1287
1288 PROCEDURE InitCharType (s: CARDINAL) ;
1289 BEGIN
1290 InitLeaf(char, s, Char)
1291 END InitCharType ;
1292
1293
1294 (*
1295 InitZType -
1296 *)
1297
1298 PROCEDURE InitZType (s: CARDINAL) ;
1299 BEGIN
1300 InitLeaf(ztype, s, ZType)
1301 END InitZType ;
1302
1303
1304 (*
1305 InitRType -
1306 *)
1307
1308 PROCEDURE InitRType (s: CARDINAL) ;
1309 BEGIN
1310 InitLeaf(rtype, s, RType)
1311 END InitRType ;
1312
1313
1314 (*
1315 InitUnknown -
1316 *)
1317
1318 PROCEDURE InitUnknown (s: CARDINAL) ;
1319 BEGIN
1320 InitLeaf(unknown, s, NulSym)
1321 END InitUnknown ;
1322
1323
1324 (*
1325 InitBooleanType -
1326 *)
1327
1328 PROCEDURE InitBooleanType (s: CARDINAL) ;
1329 BEGIN
1330 InitLeaf(boolean, s, Boolean)
1331 END InitBooleanType ;
1332
1333
1334 (*
1335 PushConstType - pushes a constant to the expression stack.
1336 *)
1337
1338 PROCEDURE PushConstType ;
1339 VAR
1340 c: CARDINAL ;
1341 BEGIN
1342 PopT(c) ;
1343 PushT(c) ;
1344 IF inDesignator
1345 THEN
1346 IF c=NulSym
1347 THEN
1348 WriteFormat0('module or symbol in qualident is not known') ;
1349 FlushErrors ;
1350 InitUnknown(c)
1351 ELSIF IsProcedure(c)
1352 THEN
1353 InitProcedure(c)
1354 ELSIF GetSkippedType(c)=RType
1355 THEN
1356 InitRType(c)
1357 ELSIF GetSkippedType(c)=ZType
1358 THEN
1359 InitZType(c)
1360 ELSIF GetSkippedType(c)=Boolean
1361 THEN
1362 InitBooleanType(c)
1363 ELSE
1364 InitUnknown(c)
1365 END
1366 END
1367 END PushConstType ;
1368
1369
1370 (*
1371 PushConstructorCastType -
1372 *)
1373
1374 PROCEDURE PushConstructorCastType ;
1375 BEGIN
1376 IF inDesignator
1377 THEN
1378 InitConvert (cast, OperandT (1), NIL, NIL)
1379 END
1380 END PushConstructorCastType ;
1381
1382
1383 (*
1384 TypeToMeta -
1385 *)
1386
1387 PROCEDURE TypeToMeta (type: CARDINAL) : constType ;
1388 BEGIN
1389 IF type=Char
1390 THEN
1391 RETURN char
1392 ELSIF type=Boolean
1393 THEN
1394 RETURN boolean
1395 ELSIF IsRealType (type)
1396 THEN
1397 RETURN rtype
1398 ELSIF IsComplexType (type)
1399 THEN
1400 RETURN ctype
1401 ELSIF IsOrdinalType (type)
1402 THEN
1403 RETURN ztype
1404 ELSE
1405 RETURN unknown
1406 END
1407 END TypeToMeta ;
1408
1409
1410 (*
1411 buildConstFunction - we are only concerned about resolving the return type of
1412 a function, so we can ignore all parameters - except
1413 the first one in the case of VAL(type, foo)
1414 and the type of bar in MIN (bar) and MAX (bar).
1415 buildConstFunction uses a unary exprNode to represent
1416 a function.
1417 *)
1418
1419 PROCEDURE buildConstFunction (func: CARDINAL; n: CARDINAL) ;
1420 VAR
1421 i : CARDINAL ;
1422 first,
1423 second: exprNode ;
1424 BEGIN
1425 first := NIL ;
1426 second := NIL ;
1427 IF n=1
1428 THEN
1429 first := PopAddress (exprStack)
1430 ELSIF n>=2
1431 THEN
1432 i := n ;
1433 WHILE i>2 DO
1434 second := PopAddress (exprStack) ;
1435 DISPOSE (second) ;
1436 DEC (i)
1437 END ;
1438 second := PopAddress (exprStack) ;
1439 first := PopAddress (exprStack)
1440 END ;
1441 IF (func=Val) OR (func=Cast)
1442 THEN
1443 InitConvert (cast, NulSym, first, second)
1444 ELSIF (func=Max) OR (func=Min)
1445 THEN
1446 InitFunction (unknown, func, NulSym, first, second, FALSE)
1447 ELSE
1448 InitFunction (TypeToMeta(GetSkippedType(func)), func, GetSkippedType(func),
1449 first, second, n>2)
1450 END
1451 END buildConstFunction ;
1452
1453
1454 (*
1455 ErrorConstFunction - generate an error message at functok using func in the
1456 error message providing it is not NulSym.
1457 *)
1458
1459 PROCEDURE ErrorConstFunction (func: CARDINAL; functok: CARDINAL) ;
1460 BEGIN
1461 IF func = NulSym
1462 THEN
1463 IF Iso
1464 THEN
1465 ErrorFormat0 (NewError (functok),
1466 'the only functions permissible in a constant expression are: CAP, CAST, CHR, CMPLX, FLOAT, HIGH, IM, LENGTH, MAX, MIN, ODD, ORD, RE, SIZE, TSIZE, TRUNC, VAL and gcc builtins')
1467 ELSE
1468 ErrorFormat0 (NewError (functok),
1469 'the only functions permissible in a constant expression are: CAP, CHR, FLOAT, HIGH, MAX, MIN, ODD, ORD, SIZE, TSIZE, TRUNC, VAL and gcc builtins')
1470 END
1471 ELSE
1472 IF Iso
1473 THEN
1474 MetaErrorT1 (functok,
1475 'the only functions permissible in a constant expression are: CAP, CAST, CHR, CMPLX, FLOAT, HIGH, IM, LENGTH, MAX, MIN, ODD, ORD, RE, SIZE, TSIZE, TRUNC, VAL and gcc builtins, but not {%1Ead}',
1476 func)
1477 ELSE
1478 MetaErrorT1 (functok,
1479 'the only functions permissible in a constant expression are: CAP, CHR, FLOAT, HIGH, MAX, MIN, ODD, ORD, SIZE, TSIZE, TRUNC, VAL and gcc builtins, but not {%1Ead}',
1480 func)
1481 END
1482 END
1483 END ErrorConstFunction ;
1484
1485
1486 (*
1487 PushConstFunctionType -
1488 *)
1489
1490 PROCEDURE PushConstFunctionType ;
1491 VAR
1492 functok,
1493 func : CARDINAL ;
1494 n : CARDINAL ;
1495 BEGIN
1496 PopT (n) ;
1497 PopTtok (func, functok) ;
1498 IF inDesignator
1499 THEN
1500 IF func = NulSym
1501 THEN
1502 ErrorConstFunction (func, functok)
1503 ELSIF (func#Convert) AND
1504 (IsPseudoBaseFunction(func) OR
1505 IsPseudoSystemFunctionConstExpression(func) OR
1506 (IsProcedure(func) AND IsProcedureBuiltin(func)))
1507 THEN
1508 buildConstFunction (func, n)
1509 ELSIF IsAModula2Type(func)
1510 THEN
1511 IF n=1
1512 THEN
1513 (* the top element on the expression stack is the first and only parameter to the cast *)
1514 InitUnary(cast, func, GetSymName(func))
1515 ELSE
1516 WriteFormat0('a constant type conversion can only have one argument')
1517 END
1518 ELSE
1519 ErrorConstFunction (func, functok)
1520 END
1521 END ;
1522 PushTtok (func, functok)
1523 END PushConstFunctionType ;
1524
1525
1526 (*
1527 PushIntegerType -
1528 *)
1529
1530 PROCEDURE PushIntegerType ;
1531 VAR
1532 sym: CARDINAL ;
1533 m : constType ;
1534 BEGIN
1535 PopT(sym) ;
1536 IF inDesignator
1537 THEN
1538 m := TypeToMeta(GetSkippedType(sym)) ;
1539 IF m=char
1540 THEN
1541 InitCharType(sym)
1542 ELSE
1543 InitZType(sym)
1544 END
1545 END
1546 END PushIntegerType ;
1547
1548
1549 (*
1550 PushRType -
1551 *)
1552
1553 PROCEDURE PushRType ;
1554 VAR
1555 sym: CARDINAL ;
1556 BEGIN
1557 PopT(sym) ;
1558 IF inDesignator
1559 THEN
1560 InitRType(sym)
1561 END
1562 END PushRType ;
1563
1564
1565 (*
1566 PushStringType -
1567 *)
1568
1569 PROCEDURE PushStringType ;
1570 VAR
1571 sym: CARDINAL ;
1572 BEGIN
1573 PopT(sym) ;
1574 IF inDesignator
1575 THEN
1576 InitLeaf(str, sym, NulSym)
1577 END
1578 END PushStringType ;
1579
1580
1581 (*
1582 InitBinary -
1583 *)
1584
1585 PROCEDURE InitBinary (m: constType; t: CARDINAL; o: Name) ;
1586 VAR
1587 l, r, b: exprNode ;
1588 BEGIN
1589 r := PopAddress (exprStack) ;
1590 l := PopAddress (exprStack) ;
1591 NEW (b) ;
1592 WITH b^ DO
1593 tag := binary ;
1594 CASE tag OF
1595
1596 binary: WITH ebinary DO
1597 meta := m ;
1598 type := t ;
1599 left := l ;
1600 right := r ;
1601 op := o
1602 END
1603 ELSE
1604 InternalError ('expecting binary')
1605 END
1606 END ;
1607 PushAddress (exprStack, b)
1608 END InitBinary ;
1609
1610
1611 (*
1612 BuildRelationConst - builds a relationship binary operation.
1613 *)
1614
1615 PROCEDURE BuildRelationConst ;
1616 VAR
1617 op: Name ;
1618 BEGIN
1619 PopT (op) ;
1620 IF inDesignator
1621 THEN
1622 InitBinary (boolean, Boolean, op)
1623 END
1624 END BuildRelationConst ;
1625
1626
1627 (*
1628 BuildBinaryConst - builds a binary operator node.
1629 *)
1630
1631 PROCEDURE BuildBinaryConst ;
1632 VAR
1633 op: Name ;
1634 BEGIN
1635 PopT (op) ;
1636 IF inDesignator
1637 THEN
1638 InitBinary (unknown, NulSym, op)
1639 END
1640 END BuildBinaryConst ;
1641
1642
1643 (*
1644 InitUnary -
1645 *)
1646
1647 PROCEDURE InitUnary (m: constType; t: CARDINAL; o: Name) ;
1648 VAR
1649 l, b: exprNode ;
1650 BEGIN
1651 l := PopAddress(exprStack) ;
1652 NEW(b) ;
1653 WITH b^ DO
1654 tag := unary ;
1655 CASE tag OF
1656
1657 unary: WITH eunary DO
1658 meta := m ;
1659 type := t ;
1660 left := l ;
1661 op := o
1662 END
1663
1664 ELSE
1665 InternalError ('expecting unary')
1666 END
1667 END ;
1668 PushAddress(exprStack, b)
1669 END InitUnary ;
1670
1671
1672 (*
1673 BuildUnaryConst - builds a unary operator node.
1674 *)
1675
1676 PROCEDURE BuildUnaryConst ;
1677 VAR
1678 op: Name ;
1679 BEGIN
1680 PopT(op) ;
1681 IF inDesignator
1682 THEN
1683 InitUnary(unknown, NulSym, op)
1684 END
1685 END BuildUnaryConst ;
1686
1687
1688 (*
1689 isTypeResolved -
1690 *)
1691
1692 PROCEDURE isTypeResolved (e: exprNode) : BOOLEAN ;
1693 BEGIN
1694 WITH e^ DO
1695 CASE tag OF
1696
1697 leaf : RETURN( (eleaf.type#NulSym) OR (eleaf.meta=str) ) |
1698 unary : RETURN( (eunary.type#NulSym) OR (eunary.meta=str) ) |
1699 binary : RETURN( (ebinary.type#NulSym) OR (ebinary.meta=str) ) |
1700 designator: RETURN( (edes.type#NulSym) OR (edes.meta=str) ) |
1701 expr : RETURN( (eexpr.type#NulSym) OR (eexpr.meta=str) ) |
1702 convert : RETURN( (econvert.type#NulSym) OR (econvert.meta=str) ) |
1703 function : RETURN( (efunction.type#NulSym) OR (efunction.meta=str) )
1704
1705 END
1706 END
1707 END isTypeResolved ;
1708
1709
1710 (*
1711 getEtype -
1712 *)
1713
1714 PROCEDURE getEtype (e: exprNode) : CARDINAL ;
1715 BEGIN
1716 WITH e^ DO
1717 CASE tag OF
1718
1719 leaf : RETURN( eleaf.type ) |
1720 unary : RETURN( eunary.type ) |
1721 binary : RETURN( ebinary.type ) |
1722 designator: RETURN( edes.type ) |
1723 expr : RETURN( eexpr.type ) |
1724 convert : RETURN( econvert.type ) |
1725 function : RETURN( efunction.type )
1726
1727 END
1728 END
1729 END getEtype ;
1730
1731
1732 (*
1733 getEmeta -
1734 *)
1735
1736 PROCEDURE getEmeta (e: exprNode) : constType ;
1737 BEGIN
1738 WITH e^ DO
1739 CASE tag OF
1740
1741 leaf : RETURN( eleaf.meta ) |
1742 unary : RETURN( eunary.meta ) |
1743 binary : RETURN( ebinary.meta ) |
1744 designator: RETURN( edes.meta ) |
1745 expr : RETURN( eexpr.meta ) |
1746 convert : RETURN( econvert.meta ) |
1747 function : RETURN( efunction.meta )
1748
1749 END
1750 END
1751 END getEmeta ;
1752
1753
1754 (*
1755 assignTM -
1756 *)
1757
1758 PROCEDURE assignTM (VAR td: CARDINAL; VAR md: constType; te: CARDINAL; me: constType) ;
1759 BEGIN
1760 md := me ;
1761 td := te
1762 END assignTM ;
1763
1764
1765 (*
1766 assignType -
1767 *)
1768
1769 PROCEDURE assignType (d, e: exprNode) ;
1770 VAR
1771 t: CARDINAL ;
1772 m: constType ;
1773 BEGIN
1774 m := getEmeta(e) ;
1775 t := getEtype(e) ;
1776 WITH d^ DO
1777 CASE tag OF
1778
1779 leaf : assignTM(eleaf.type, eleaf.meta, t, m) |
1780 unary : assignTM(eunary.type, eunary.meta, t, m) |
1781 binary : assignTM(ebinary.type, ebinary.meta, t, m) |
1782 designator: assignTM(edes.type, edes.meta, t, m) |
1783 expr : assignTM(eexpr.type, eexpr.meta, t, m) |
1784 convert : assignTM(econvert.type, econvert.meta, t, m) |
1785 function : assignTM(efunction.type, efunction.meta, t, m)
1786
1787 END
1788 END
1789 END assignType ;
1790
1791
1792 (*
1793 deduceTypes - works out the type and metatype given, l, and, r.
1794 *)
1795
1796 PROCEDURE deduceTypes (VAR t: CARDINAL;
1797 VAR m: constType;
1798 l, r: exprNode; op: Name) ;
1799 BEGIN
1800 IF r=NIL
1801 THEN
1802 (* function or cast *)
1803 t := getEtype(l) ;
1804 m := getEmeta(l)
1805 ELSIF (op=EqualTok) OR (op=HashTok) OR (op=LessGreaterTok) OR
1806 (op=LessTok) OR (op=LessEqualTok) OR (op=GreaterTok) OR
1807 (op=GreaterEqualTok) OR (op=InTok) OR (op=OrTok) OR
1808 (op=AndTok) OR (op=NotTok) OR (op=AmbersandTok)
1809 THEN
1810 t := Boolean ;
1811 m := boolean
1812 ELSIF (op=PlusTok) OR (op=MinusTok) OR (op=TimesTok) OR (op=ModTok) OR
1813 (op=DivTok) OR (op=RemTok) OR (op=DivideTok)
1814 THEN
1815 t := MixTypes(getEtype(l), getEtype(r), constToken) ;
1816 m := getEmeta(l) ;
1817 IF m=unknown
1818 THEN
1819 m := getEmeta(r)
1820 ELSIF (getEmeta(r)#unknown) AND (m#getEmeta(r))
1821 THEN
1822 ErrorFormat0(NewError(constToken),
1823 'the operands to a binary constant expression have different types')
1824 END
1825 ELSE
1826 InternalError ('unexpected operator')
1827 END
1828 END deduceTypes ;
1829
1830
1831 (*
1832 WalkConvert -
1833 *)
1834
1835 PROCEDURE WalkConvert (e: exprNode) : BOOLEAN ;
1836 BEGIN
1837 IF isTypeResolved(e)
1838 THEN
1839 RETURN( FALSE )
1840 ELSE
1841 WITH e^.econvert DO
1842 IF isTypeResolved(totype)
1843 THEN
1844 assignType(e, totype) ;
1845 RETURN( TRUE )
1846 END ;
1847 RETURN( doWalkNode(totype) )
1848 END
1849 END
1850 END WalkConvert ;
1851
1852
1853 (*
1854 WalkFunctionParam -
1855 *)
1856
1857 PROCEDURE WalkFunctionParam (func: CARDINAL; e: exprNode) : BOOLEAN ;
1858 BEGIN
1859 IF isTypeResolved(e)
1860 THEN
1861 RETURN( FALSE )
1862 ELSE
1863 IF e^.tag=leaf
1864 THEN
1865 WITH e^.eleaf DO
1866 IF (sym#NulSym) AND (type=NulSym)
1867 THEN
1868 IF (func=Min) OR (func=Max)
1869 THEN
1870 IF IsSet (sym) OR (IsType (sym) AND IsSet (SkipType (sym)))
1871 THEN
1872 type := GetType (SkipType (sym))
1873 ELSE
1874 (* sym is the type required for MAX, MIN and VAL. *)
1875 type := sym
1876 END
1877 ELSE
1878 Assert(func=Val) ;
1879 type := sym
1880 END ;
1881 meta := TypeToMeta(sym) ;
1882 RETURN( TRUE )
1883 END
1884 END
1885 END
1886 END ;
1887 RETURN( FALSE )
1888 END WalkFunctionParam ;
1889
1890
1891 (*
1892 WalkFunction -
1893 *)
1894
1895 PROCEDURE WalkFunction (e: exprNode) : BOOLEAN ;
1896 BEGIN
1897 IF isTypeResolved(e)
1898 THEN
1899 RETURN( FALSE )
1900 ELSE
1901 WITH e^.efunction DO
1902 IF (func=Max) OR (func=Min) OR (func=Val)
1903 THEN
1904 IF isTypeResolved(first)
1905 THEN
1906 IF getEmeta(first)=str
1907 THEN
1908 MetaError1('a string parameter cannot be passed to function {%1Dad}', func) ;
1909 RETURN( FALSE )
1910 END ;
1911 type := getEtype(first) ;
1912 RETURN( TRUE )
1913 END ;
1914 RETURN WalkFunctionParam (func, first)
1915 ELSE
1916 MetaError1('not expecting this function inside a constant expression {%1Dad}', func)
1917 END
1918 END ;
1919 RETURN( TRUE )
1920 END
1921 END WalkFunction ;
1922
1923
1924 (*
1925 doWalkNode -
1926 *)
1927
1928 PROCEDURE doWalkNode (e: exprNode) : BOOLEAN ;
1929 BEGIN
1930 WITH e^ DO
1931 CASE tag OF
1932
1933 expr : RETURN( WalkExpr(e) ) |
1934 leaf : RETURN( WalkLeaf(e) ) |
1935 unary : RETURN( WalkUnary(e) ) |
1936 binary : RETURN( WalkBinary(e) ) |
1937 convert : RETURN( WalkConvert(e) ) |
1938 function: RETURN( WalkFunction(e) )
1939
1940 ELSE
1941 InternalError ('unexpected tag value')
1942 END
1943 END ;
1944 RETURN( FALSE )
1945 END doWalkNode ;
1946
1947
1948 (*
1949 WalkLeaf -
1950 *)
1951
1952 PROCEDURE WalkLeaf (e: exprNode) : BOOLEAN ;
1953 VAR
1954 c: exprNode ;
1955 BEGIN
1956 IF isTypeResolved(e)
1957 THEN
1958 RETURN( FALSE )
1959 ELSE
1960 WITH e^.eleaf DO
1961 IF IsConst(sym) AND (GetType(sym)#NulSym)
1962 THEN
1963 type := GetSkippedType(sym) ;
1964 RETURN( TRUE )
1965 END ;
1966 IF IsAModula2Type(sym)
1967 THEN
1968 type := sym ;
1969 RETURN( TRUE )
1970 END ;
1971 c := findConstDes(sym) ;
1972 IF (c#NIL) AND isTypeResolved(c)
1973 THEN
1974 assignType(e, c) ;
1975 RETURN( TRUE )
1976 END
1977 END
1978 END ;
1979 RETURN( FALSE )
1980 END WalkLeaf ;
1981
1982
1983 (*
1984 WalkUnary -
1985 *)
1986
1987 PROCEDURE WalkUnary (e: exprNode) : BOOLEAN ;
1988 BEGIN
1989 IF isTypeResolved(e)
1990 THEN
1991 RETURN( FALSE )
1992 ELSE
1993 WITH e^.eunary DO
1994 IF isTypeResolved(left)
1995 THEN
1996 deduceTypes(type, meta, left, left, op) ;
1997 RETURN( TRUE )
1998 END ;
1999 RETURN( doWalkNode(left) )
2000 END
2001 END
2002 END WalkUnary ;
2003
2004
2005 (*
2006 WalkBinary -
2007 *)
2008
2009 PROCEDURE WalkBinary (e: exprNode) : BOOLEAN ;
2010 VAR
2011 changed: BOOLEAN ;
2012 BEGIN
2013 IF isTypeResolved(e)
2014 THEN
2015 RETURN( FALSE )
2016 ELSE
2017 WITH e^.ebinary DO
2018 IF isTypeResolved(left) AND isTypeResolved(right)
2019 THEN
2020 deduceTypes(type, meta, left, right, op) ;
2021 RETURN( TRUE )
2022 END ;
2023 changed := doWalkNode(left) ;
2024 RETURN( doWalkNode(right) OR changed )
2025 END
2026 END
2027 END WalkBinary ;
2028
2029
2030 (*
2031 WalkExpr -
2032 *)
2033
2034 PROCEDURE WalkExpr (e: exprNode) : BOOLEAN ;
2035 BEGIN
2036 IF isTypeResolved(e)
2037 THEN
2038 RETURN( FALSE )
2039 ELSE
2040 WITH e^.eexpr DO
2041 IF isTypeResolved(left)
2042 THEN
2043 assignType(e, left) ;
2044 RETURN( TRUE )
2045 END ;
2046 RETURN( doWalkNode(left) )
2047 END
2048 END
2049 END WalkExpr ;
2050
2051
2052 (*
2053 doWalkDesExpr - returns TRUE if the expression trees, d, or, e, are changed.
2054 *)
2055
2056 PROCEDURE doWalkDesExpr (d, e: exprNode) : BOOLEAN ;
2057 BEGIN
2058 IF isTypeResolved(e)
2059 THEN
2060 WITH d^.edes DO
2061 type := getEtype(e) ;
2062 IF type=NulSym
2063 THEN
2064 meta := getEmeta(e) ;
2065 IF meta=str
2066 THEN
2067 (* PutConstString(sym, getString(e)) *)
2068 END
2069 ELSE
2070 PutConst(sym, type)
2071 END ;
2072 RETURN( TRUE )
2073 END
2074 END ;
2075 RETURN( doWalkNode(e) )
2076 END doWalkDesExpr ;
2077
2078
2079 (*
2080 doWalkDes - return TRUE if expression, e, is changed.
2081 *)
2082
2083 PROCEDURE doWalkDes (d: exprNode) : BOOLEAN ;
2084 BEGIN
2085 IF isTypeResolved(d)
2086 THEN
2087 RETURN( FALSE )
2088 ELSE
2089 WITH d^ DO
2090 CASE tag OF
2091
2092 designator: WITH edes DO
2093 constToken := GetDeclaredMod(sym) ;
2094 RETURN( doWalkDesExpr(d, left) )
2095 END
2096
2097 ELSE
2098 InternalError ('unexpected tag value')
2099 END
2100 END
2101 END
2102 END doWalkDes ;
2103
2104
2105 (*
2106 findConstDes -
2107 *)
2108
2109 PROCEDURE findConstDes (sym: CARDINAL) : exprNode ;
2110 VAR
2111 i: CARDINAL ;
2112 e: exprNode ;
2113 BEGIN
2114 i := 1 ;
2115 WHILE i<=HighIndice(constList) DO
2116 e := GetIndice(constList, i) ;
2117 WITH e^ DO
2118 CASE tag OF
2119
2120 designator: IF edes.sym=sym
2121 THEN
2122 RETURN( e )
2123 END
2124
2125 ELSE
2126 END
2127 END ;
2128 INC(i)
2129 END ;
2130 RETURN( NIL )
2131 END findConstDes ;
2132
2133
2134 (*
2135 WalkDes - return TRUE if expression, e, is changed.
2136 *)
2137
2138 PROCEDURE WalkDes (d: exprNode) : BOOLEAN ;
2139 BEGIN
2140 IF d=NIL
2141 THEN
2142 RETURN FALSE
2143 ELSE
2144 IF Debugging
2145 THEN
2146 DebugDes (d)
2147 END ;
2148 RETURN doWalkDes (d)
2149 END
2150 END WalkDes ;
2151
2152
2153 (*
2154 WalkConst - returns TRUE if the constant tree associated with, sym,
2155 is changed.
2156 *)
2157
2158 (*
2159 PROCEDURE WalkConst (sym: CARDINAL) : BOOLEAN ;
2160 BEGIN
2161 RETURN( WalkDes(findConstDes(sym)) )
2162 END WalkConst ;
2163 *)
2164
2165
2166 (*
2167 WalkConsts - walk over the constant trees and return TRUE if any tree was changed.
2168 (As a result of a type resolution).
2169 *)
2170
2171 PROCEDURE WalkConsts () : BOOLEAN ;
2172 VAR
2173 changed: BOOLEAN ;
2174 i : CARDINAL ;
2175 BEGIN
2176 changed := FALSE ;
2177 i := 1 ;
2178 WHILE i<=HighIndice(constList) DO
2179 IF WalkDes(GetIndice(constList, i))
2180 THEN
2181 changed := TRUE
2182 END ;
2183 INC(i)
2184 END ;
2185 RETURN( changed )
2186 END WalkConsts ;
2187
2188
2189 (*
2190 DebugNodes -
2191 *)
2192
2193 PROCEDURE DebugNodes ;
2194 VAR
2195 i: CARDINAL ;
2196 BEGIN
2197 i := 1 ;
2198 WHILE i<=HighIndice(constList) DO
2199 IF isTypeResolved(GetIndice(constList, i))
2200 THEN
2201 WriteString('resolved ')
2202 ELSE
2203 WriteString('unresolved ')
2204 END ;
2205 DebugNode(GetIndice(constList, i)) ; WriteLn ;
2206 INC(i)
2207 END
2208 END DebugNodes ;
2209
2210
2211 (*
2212 findAlias -
2213 *)
2214
2215 PROCEDURE findAlias (sym: CARDINAL; e: exprNode) : CARDINAL ;
2216 BEGIN
2217 CASE e^.tag OF
2218
2219 designator: RETURN( findAlias(sym, e^.edes.left) ) |
2220 leaf : RETURN( e^.eleaf.sym ) |
2221 expr : RETURN( findAlias(sym, e^.eexpr.left) ) |
2222 unary,
2223 binary : RETURN( sym )
2224
2225 ELSE
2226 InternalError ('not expecting this tag value')
2227 END
2228 END findAlias ;
2229
2230
2231 (*
2232 SkipConst - returns an alias to constant, sym, if one exists.
2233 Otherwise sym is returned.
2234 *)
2235
2236 PROCEDURE SkipConst (sym: CARDINAL) : CARDINAL ;
2237 VAR
2238 i: CARDINAL ;
2239 e: exprNode ;
2240 BEGIN
2241 i := 1 ;
2242 WHILE i<=HighIndice(constList) DO
2243 e := GetIndice(constList, i) ;
2244 IF (e^.tag=designator) AND (e^.edes.sym=sym)
2245 THEN
2246 RETURN( findAlias(sym, e) )
2247 END ;
2248 INC(i)
2249 END ;
2250 RETURN( sym )
2251 END SkipConst ;
2252
2253
2254 (*
2255 PushConstAttributeType -
2256 *)
2257
2258 PROCEDURE PushConstAttributeType ;
2259 VAR
2260 n: Name ;
2261 BEGIN
2262 PopT(n) ;
2263 PushT(n) ;
2264 InitZType(NulSym) ;
2265 IF (n=MakeKey('BITS_PER_UNIT')) OR (n=MakeKey('BITS_PER_WORD')) OR
2266 (n=MakeKey('BITS_PER_CHAR')) OR (n=MakeKey('UNITS_PER_WORD'))
2267 THEN
2268 (* all ok *)
2269 ELSE
2270 WriteFormat1("unknown constant attribute value '%a'", n)
2271 END
2272 END PushConstAttributeType ;
2273
2274
2275 (*
2276 PushConstAttributePairType -
2277 *)
2278
2279 PROCEDURE PushConstAttributePairType ;
2280 VAR
2281 q, n: Name ;
2282 BEGIN
2283 PopT(n) ;
2284 PopT(q) ;
2285 PushT(q) ;
2286 PushT(n) ;
2287 IF (n=MakeKey('IEC559')) OR (n=MakeKey('LIA1')) OR (n=MakeKey('IEEE')) OR
2288 (n=MakeKey('ISO')) OR (n=MakeKey('rounds')) OR (n=MakeKey('gUnderflow')) OR
2289 (n=MakeKey('exception')) OR (n=MakeKey('extend'))
2290 THEN
2291 InitBooleanType(NulSym)
2292 ELSIF (n=MakeKey('radix')) OR (n=MakeKey('places')) OR (n=MakeKey('expoMin')) OR
2293 (n=MakeKey('expoMax')) OR (n=MakeKey('nModes'))
2294 THEN
2295 InitZType(NulSym)
2296 ELSIF (n=MakeKey('large')) OR (n=MakeKey('small'))
2297 THEN
2298 InitRType(NulSym)
2299 ELSE
2300 WriteFormat1("unknown constant attribute value '%a'", n) ;
2301 InitUnknown(NulSym)
2302 END
2303 END PushConstAttributePairType ;
2304
2305
2306 (*
2307 CheckConsts -
2308 *)
2309
2310 PROCEDURE CheckConsts ;
2311 VAR
2312 i: CARDINAL ;
2313 e: exprNode ;
2314 BEGIN
2315 i := 1 ;
2316 WHILE i<=HighIndice(constList) DO
2317 e := GetIndice(constList, i) ;
2318 IF NOT isTypeResolved(e)
2319 THEN
2320 WITH e^ DO
2321 CASE tag OF
2322
2323 designator: MetaError1('the type of the constant declaration {%1Dad} cannot be determined', edes.sym)
2324
2325 ELSE
2326 END
2327 END
2328 END ;
2329 INC(i)
2330 END
2331 END CheckConsts ;
2332
2333
2334 (*
2335 ResolveConstTypes - resolves the types of all designator declared constants.
2336 *)
2337
2338 PROCEDURE ResolveConstTypes ;
2339 BEGIN
2340 IF Debugging
2341 THEN
2342 WriteString('initially') ; WriteLn ;
2343 DebugNodes
2344 END ;
2345 WHILE WalkConsts() DO
2346 IF Debugging
2347 THEN
2348 WriteString('iteration') ; WriteLn ;
2349 DebugNodes
2350 END
2351 END ;
2352 IF Debugging
2353 THEN
2354 WriteString('finally') ; WriteLn ;
2355 DebugNodes
2356 END ;
2357 CheckConsts
2358 END ResolveConstTypes ;
2359
2360
2361 (*
2362 Init -
2363 *)
2364
2365 PROCEDURE Init ;
2366 BEGIN
2367 exprStack := InitStackAddress () ;
2368 constList := InitIndex (1) ;
2369 desStack := InitStackWord () ;
2370 inDesignator := FALSE
2371 END Init ;
2372
2373
2374 BEGIN
2375 Init
2376 END PCSymBuild.