Debian Codegraph @

gcc/m2/gm2-compiler/M2Error.mod

raw
1 (* M2Error.mod error reporting interface.
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 M2Error ;
23
24 FROM NameKey IMPORT NulName, Name, KeyToCharStar ;
25 FROM DynamicStrings IMPORT String, InitString, InitStringCharStar, ConCat, ConCatChar, Mark, string, KillString, Dup ;
26 FROM FIO IMPORT StdOut, WriteNBytes, Close, FlushBuffer ;
27 FROM StrLib IMPORT StrLen, StrEqual ;
28 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3 ;
29 FROM M2LexBuf IMPORT FindFileNameFromToken, TokenToLineNo, TokenToColumnNo, GetTokenNo ;
30 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
31 FROM M2Printf IMPORT printf0, printf1, printf2, printf3 ;
32 FROM M2Options IMPORT Xcode ;
33 FROM M2RTS IMPORT ExitOnHalt ;
34 FROM SYSTEM IMPORT ADDRESS ;
35 FROM M2Emit IMPORT EmitError ;
36 FROM M2LexBuf IMPORT UnknownTokenNo ;
37 FROM M2StackAddress IMPORT StackOfAddress, InitStackAddress, PushAddress, PopAddress, NoOfItemsInStackAddress ;
38 FROM Indexing IMPORT Index, HighIndice, InitIndex, GetIndice, PutIndice ;
39 FROM M2Debug IMPORT Assert ;
40 FROM M2Pass IMPORT IsPass0, IsPass1 ;
41 FROM SymbolTable IMPORT NulSym ;
42
43 FROM M2ColorString IMPORT filenameColor, endColor, errorColor, warningColor, noteColor,
44 range1Color, range2Color, quoteOpen, quoteClose ;
45
46 IMPORT M2Emit ;
47
48
49 CONST
50 Debugging = TRUE ;
51 DebugTrace = FALSE ;
52 DebugError = FALSE ;
53
54 TYPE
55 Error = POINTER TO RECORD
56 parent,
57 child,
58 next : Error ;
59 note,
60 fatal : BOOLEAN ;
61 s : String ;
62 (* index of token causing the error *)
63 token : CARDINAL ;
64 color : BOOLEAN ;
65 scope : ErrorScope ;
66 END ;
67
68 KindScope = (noscope, definition, implementation, program, module, procedure) ;
69
70 ErrorScope = POINTER TO RECORD
71 scopeKind: KindScope ;
72 scopeName: Name ;
73 symbol : CARDINAL ; (* symbol table entry. *)
74 END ;
75
76
77 VAR
78 head : Error ;
79 InInternal : BOOLEAN ;
80 lastScope : ErrorScope ;
81 scopeIndex : CARDINAL ;
82 scopeArray : Index ;
83 currentScope: ErrorScope ;
84 scopeStack : StackOfAddress ;
85
86
87 (*
88 SetColor - informs the error module that this error will have had colors
89 assigned to it. If an error is issued without colors assigned
90 then the default colors will be assigned to the legacy error
91 messages.
92 *)
93
94 PROCEDURE SetColor (e: Error) : Error ;
95 BEGIN
96 e^.color := TRUE ;
97 RETURN e
98 END SetColor ;
99
100
101 (*
102 Cast - casts a := b
103 *)
104
105 PROCEDURE Cast (VAR a: ARRAY OF BYTE; b: ARRAY OF BYTE) ;
106 VAR
107 i: CARDINAL ;
108 BEGIN
109 IF HIGH(a)=HIGH(b)
110 THEN
111 FOR i := 0 TO HIGH(a) DO
112 a[i] := b[i]
113 END
114 END
115 END Cast ;
116
117
118 (*
119 TranslateNameToString - takes a format specification string, a, and
120 if they consist of of %a then this is translated
121 into a String and %a is replaced by %s.
122 *)
123
124 PROCEDURE TranslateNameToCharStar (VAR a: ARRAY OF CHAR;
125 n: CARDINAL) : BOOLEAN ;
126 VAR
127 argno,
128 i, h : CARDINAL ;
129 BEGIN
130 argno := 1 ;
131 i := 0 ;
132 h := StrLen(a) ;
133 WHILE i<h DO
134 IF (a[i]='%') AND (i+1<h)
135 THEN
136 IF (a[i+1]='a') AND (argno=n)
137 THEN
138 a[i+1] := 's' ;
139 RETURN( TRUE )
140 END ;
141 INC(argno) ;
142 IF argno>n
143 THEN
144 (* all done *)
145 RETURN( FALSE )
146 END
147 END ;
148 INC(i)
149 END ;
150 RETURN( FALSE )
151 END TranslateNameToCharStar ;
152
153
154 (*
155 InternalError - displays an internal error message together with the compiler source
156 file and line number.
157 This function is not buffered and is used when the compiler is about
158 to give up.
159 *)
160
161 PROCEDURE InternalError (message: ARRAY OF CHAR) <* noreturn *> ;
162 BEGIN
163 IF NOT InInternal
164 THEN
165 InInternal := TRUE ;
166 FlushErrors
167 END ;
168 M2Emit.InternalError (message) ;
169 HALT
170 END InternalError ;
171
172
173 (* ***************************************************************************
174 The following routines are used for normal syntax and semantic error reporting
175 *************************************************************************** *)
176
177
178 (*
179 WriteFormat0 - displays the source module and line together
180 with the encapsulated format string.
181 Used for simple error messages tied to the current token.
182 *)
183
184 PROCEDURE WriteFormat0 (a: ARRAY OF CHAR) ;
185 VAR
186 e: Error ;
187 BEGIN
188 e := NewError(GetTokenNo()) ;
189 WITH e^ DO
190 s := Sprintf0(Mark(InitString(a)))
191 END
192 END WriteFormat0 ;
193
194
195 (*
196 WarnFormat0 - displays the source module and line together
197 with the encapsulated format string.
198 Used for simple warning messages tied to the current token.
199 *)
200
201 PROCEDURE WarnFormat0 (a: ARRAY OF CHAR) ;
202 VAR
203 e: Error ;
204 BEGIN
205 e := NewWarning(GetTokenNo()) ;
206 WITH e^ DO
207 s := Sprintf0(Mark(InitString(a)))
208 END
209 END WarnFormat0 ;
210
211
212 (*
213 DoFormat1 -
214 *)
215
216 PROCEDURE DoFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) : String ;
217 VAR
218 s: String ;
219 n: Name ;
220 BEGIN
221 n := NulName ;
222 IF TranslateNameToCharStar(a, 1)
223 THEN
224 Cast(n, w) ;
225 s := Mark(InitStringCharStar(KeyToCharStar(n))) ;
226 s := Sprintf1(Mark(InitString(a)), s)
227 ELSE
228 s := Sprintf1(Mark(InitString(a)), w)
229 END ;
230 RETURN( s )
231 END DoFormat1 ;
232
233
234 (*
235 WriteFormat1 - displays the source module and line together
236 with the encapsulated format string.
237 Used for simple error messages tied to the current token.
238 *)
239
240 PROCEDURE WriteFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
241 VAR
242 e: Error ;
243 BEGIN
244 e := NewError(GetTokenNo()) ;
245 e^.s := DoFormat1(a, w)
246 END WriteFormat1 ;
247
248
249 (*
250 WarnFormat1 - displays the source module and line together
251 with the encapsulated format string.
252 Used for simple warning messages tied to the current token.
253 *)
254
255 PROCEDURE WarnFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
256 VAR
257 e: Error ;
258 BEGIN
259 e := NewWarning(GetTokenNo()) ;
260 e^.s := DoFormat1(a, w)
261 END WarnFormat1 ;
262
263
264 (*
265 DoFormat2 -
266 *)
267
268 PROCEDURE DoFormat2 (a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) : String ;
269 VAR
270 n : Name ;
271 s,
272 s1, s2: String ;
273 b : BITSET ;
274 BEGIN
275 b := {} ;
276 n := NulName ;
277 IF TranslateNameToCharStar(a, 1)
278 THEN
279 Cast(n, w1) ;
280 s1 := Mark(InitStringCharStar(KeyToCharStar(n))) ;
281 INCL(b, 1)
282 END ;
283 IF TranslateNameToCharStar(a, 2)
284 THEN
285 Cast(n, w2) ;
286 s2 := Mark(InitStringCharStar(KeyToCharStar(n))) ;
287 INCL(b, 2)
288 END ;
289 CASE b OF
290
291 {} : s := Sprintf2(Mark(InitString(a)), w1, w2) |
292 {1} : s := Sprintf2(Mark(InitString(a)), s1, w2) |
293 {2} : s := Sprintf2(Mark(InitString(a)), w1, s2) |
294 {1,2}: s := Sprintf2(Mark(InitString(a)), s1, s2)
295
296 ELSE
297 HALT
298 END ;
299 RETURN( s )
300 END DoFormat2 ;
301
302
303 (*
304 WriteFormat2 - displays the module and line together with the encapsulated
305 format strings.
306 Used for simple error messages tied to the current token.
307 *)
308
309 PROCEDURE WriteFormat2 (a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) ;
310 VAR
311 e: Error ;
312 BEGIN
313 e := NewError(GetTokenNo()) ;
314 e^.s := DoFormat2(a, w1, w2)
315 END WriteFormat2 ;
316
317
318 PROCEDURE DoFormat3 (a: ARRAY OF CHAR; w1, w2, w3: ARRAY OF BYTE) : String ;
319 VAR
320 n : Name ;
321 s, s1, s2, s3: String ;
322 b : BITSET ;
323 BEGIN
324 b := {} ;
325 n := NulName ;
326 IF TranslateNameToCharStar(a, 1)
327 THEN
328 Cast(n, w1) ;
329 s1 := Mark(InitStringCharStar(KeyToCharStar(n))) ;
330 INCL(b, 1)
331 END ;
332 IF TranslateNameToCharStar(a, 2)
333 THEN
334 Cast(n, w2) ;
335 s2 := Mark(InitStringCharStar(KeyToCharStar(n))) ;
336 INCL(b, 2)
337 END ;
338 IF TranslateNameToCharStar(a, 3)
339 THEN
340 Cast(n, w3) ;
341 s3 := Mark(InitStringCharStar(KeyToCharStar(n))) ;
342 INCL(b, 3)
343 END ;
344 CASE b OF
345
346 {} : s := Sprintf3(Mark(InitString(a)), w1, w2, w3) |
347 {1} : s := Sprintf3(Mark(InitString(a)), s1, w2, w3) |
348 {2} : s := Sprintf3(Mark(InitString(a)), w1, s2, w3) |
349 {1,2} : s := Sprintf3(Mark(InitString(a)), s1, s2, w3) |
350 {3} : s := Sprintf3(Mark(InitString(a)), w1, w2, s3) |
351 {1,3} : s := Sprintf3(Mark(InitString(a)), s1, w2, s3) |
352 {2,3} : s := Sprintf3(Mark(InitString(a)), w1, s2, s3) |
353 {1,2,3}: s := Sprintf3(Mark(InitString(a)), s1, s2, s3)
354
355 ELSE
356 HALT
357 END ;
358 RETURN( s )
359 END DoFormat3 ;
360
361
362 (*
363 WriteFormat3 - displays the module and line together with the encapsulated
364 format strings.
365 Used for simple error messages tied to the current token.
366 *)
367
368 PROCEDURE WriteFormat3 (a: ARRAY OF CHAR; w1, w2, w3: ARRAY OF BYTE) ;
369 VAR
370 e: Error ;
371 BEGIN
372 e := NewError(GetTokenNo()) ;
373 e^.s := DoFormat3(a, w1, w2, w3)
374 END WriteFormat3 ;
375
376
377 (*
378 MoveError - repositions an error, e, to token, AtTokenNo, and returns, e.
379 *)
380
381 PROCEDURE MoveError (e: Error; AtTokenNo: CARDINAL) : Error ;
382 BEGIN
383 IF e # NIL
384 THEN
385 e^.token := AtTokenNo
386 END ;
387 RETURN e
388 END MoveError ;
389
390
391 (*
392 NewError - creates and returns a new error handle.
393 *)
394
395 PROCEDURE NewError (AtTokenNo: CARDINAL) : Error ;
396 VAR
397 e, f: Error ;
398 BEGIN
399 IF AtTokenNo = UnknownTokenNo
400 THEN
401 (* This could be used as a useful debugging hook as the front end
402 has forgotten the token no. *)
403 END ;
404 NEW(e) ;
405 WITH e^ DO
406 s := NIL ;
407 token := AtTokenNo ;
408 next := NIL ;
409 parent := NIL ;
410 child := NIL ;
411 note := FALSE ;
412 fatal := TRUE ;
413 color := FALSE ;
414 END ;
415 (* Assert (scopeKind # noscope) ; *)
416 e^.scope := currentScope ;
417 IF (head=NIL) OR (head^.token>AtTokenNo)
418 THEN
419 e^.next := head ;
420 head := e
421 ELSE
422 f := head ;
423 WHILE (f^.next#NIL) AND (f^.next^.token<AtTokenNo) DO
424 f := f^.next
425 END ;
426 e^.next := f^.next ;
427 f^.next := e
428 END ;
429 RETURN( e )
430 END NewError ;
431
432
433 (*
434 NewWarning - creates and returns a new error handle suitable for a warning.
435 A warning will not stop compilation.
436 *)
437
438 PROCEDURE NewWarning (AtTokenNo: CARDINAL) : Error ;
439 VAR
440 e: Error ;
441 BEGIN
442 e := NewError(AtTokenNo) ;
443 e^.fatal := FALSE ;
444 e^.note := FALSE ;
445 RETURN e
446 END NewWarning ;
447
448
449 (*
450 NewNote - creates and returns a new error handle suitable for a note.
451 A note will not stop compilation.
452 *)
453
454 PROCEDURE NewNote (AtTokenNo: CARDINAL) : Error ;
455 VAR
456 e: Error ;
457 BEGIN
458 e := NewError(AtTokenNo) ;
459 e^.fatal := FALSE ;
460 e^.note := TRUE ;
461 RETURN e
462 END NewNote ;
463
464
465 (*
466 ChainError - creates and returns a new error handle, this new error
467 is associated with, e, and is chained onto the end of, e.
468 If, e, is NIL then the result to NewError is returned.
469 *)
470
471 PROCEDURE ChainError (AtTokenNo: CARDINAL; e: Error) : Error ;
472 VAR
473 f: Error ;
474 BEGIN
475 IF e=NIL
476 THEN
477 RETURN NewError (AtTokenNo)
478 ELSE
479 NEW (f) ;
480 WITH f^ DO
481 s := NIL ;
482 token := AtTokenNo ;
483 next := e^.child ;
484 parent := e ;
485 child := NIL ;
486 fatal := e^.fatal ;
487 scope := e^.scope
488 END ;
489 e^.child := f
490 END ;
491 RETURN f
492 END ChainError ;
493
494
495 (*
496 ErrorFormat routines provide a printf capability for the error handle.
497 *)
498
499 PROCEDURE ErrorFormat0 (e: Error; a: ARRAY OF CHAR) ;
500 BEGIN
501 WITH e^ DO
502 IF s=NIL
503 THEN
504 s := Sprintf0(Mark(InitString(a)))
505 ELSE
506 s := ConCat(s, Mark(Sprintf0(Mark(InitString(a)))))
507 END
508 END
509 END ErrorFormat0 ;
510
511
512 PROCEDURE ErrorFormat1 (e: Error; a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
513 VAR
514 s1: String ;
515 BEGIN
516 s1 := DoFormat1(a, w) ;
517 WITH e^ DO
518 IF s=NIL
519 THEN
520 s := s1
521 ELSE
522 s := ConCat(s, Mark(s1))
523 END
524 END
525 END ErrorFormat1 ;
526
527
528 PROCEDURE ErrorFormat2 (e: Error; a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) ;
529 VAR
530 s1: String ;
531 BEGIN
532 s1 := DoFormat2(a, w1, w2) ;
533 WITH e^ DO
534 IF s=NIL
535 THEN
536 s := s1
537 ELSE
538 s := ConCat(s, Mark(s1))
539 END
540 END
541 END ErrorFormat2 ;
542
543
544 PROCEDURE ErrorFormat3 (e: Error; a: ARRAY OF CHAR;
545 w1, w2, w3: ARRAY OF BYTE) ;
546 VAR
547 s1: String ;
548 BEGIN
549 s1 := DoFormat3(a, w1, w2, w3) ;
550 WITH e^ DO
551 IF s=NIL
552 THEN
553 s := s1
554 ELSE
555 s := ConCat(s, Mark(s1))
556 END
557 END
558 END ErrorFormat3 ;
559
560
561 PROCEDURE ErrorString (e: Error; str: String) ;
562 BEGIN
563 WITH e^ DO
564 s := str
565 END
566 END ErrorString ;
567
568
569 (*
570 Init - initializes the error list.
571 *)
572
573 PROCEDURE Init ;
574 BEGIN
575 head := NIL ;
576 InInternal := FALSE ;
577 scopeStack := InitStackAddress () ;
578 scopeArray := InitIndex (1) ;
579 currentScope := NIL ;
580 scopeIndex := 0
581 END Init ;
582
583
584 (*
585 CheckIncludes - generates a sequence of error messages which determine the relevant
586 included file and line number.
587 For example:
588
589 gcc a.c
590 In file included from b.h:1,
591 from a.c:1:
592 c.h:1: parse error before `and'
593
594 where a.c is: #include "b.h"
595 b.h is: #include "c.h"
596 c.h is: and this and that
597
598 we attempt to follow the error messages that gcc issues.
599 *)
600
601 PROCEDURE CheckIncludes (token: CARDINAL; depth: CARDINAL) ;
602 VAR
603 included: String ;
604 lineno : CARDINAL ;
605 BEGIN
606 included := FindFileNameFromToken(token, depth+1) ;
607 IF included#NIL
608 THEN
609 lineno := TokenToLineNo(token, depth+1) ;
610 IF depth=0
611 THEN
612 printf2('In file included from %s:%d', included, lineno)
613 ELSE
614 printf2(' from %s:%d', included, lineno)
615 END ;
616 IF FindFileNameFromToken(token, depth+2)=NIL
617 THEN
618 printf0(':\n')
619 ELSE
620 printf0(',\n')
621 END ;
622 CheckIncludes(token, depth+1)
623 END
624 END CheckIncludes ;
625
626
627 (*
628 FlushAll - flushes all errors in list, e.
629 *)
630
631 PROCEDURE FlushAll (e: Error; FatalStatus: BOOLEAN) : BOOLEAN ;
632 VAR
633 f : Error ;
634 written: BOOLEAN ;
635 BEGIN
636 written := FALSE ;
637 IF e#NIL
638 THEN
639 REPEAT
640 WITH e^ DO
641 IF (FatalStatus=fatal) AND (s#NIL)
642 THEN
643 currentScope := scope ;
644 CheckIncludes (token, 0) ;
645 EmitError (fatal, note, token, AnnounceScope (e, s)) ;
646 IF (child#NIL) AND FlushAll (child, FatalStatus)
647 THEN
648 END ;
649 s := NIL ;
650 written := TRUE
651 END
652 END ;
653 f := e ;
654 e := e^.next ;
655 IF NOT Debugging
656 THEN
657 WITH f^ DO
658 s := KillString (s)
659 END ;
660 DISPOSE (f)
661 END ;
662 UNTIL e=NIL
663 END ;
664 RETURN written
665 END FlushAll ;
666
667
668 (*
669 FlushErrors - switches the output channel to the error channel
670 and then writes out all errors.
671 *)
672
673 PROCEDURE FlushErrors ;
674 BEGIN
675 IF DebugTrace
676 THEN
677 printf0('\nFlushing all errors\n') ;
678 printf0('===================\n')
679 END ;
680 IF FlushAll (head, TRUE)
681 THEN
682 ExitOnHalt(1) ;
683 HALT
684 END
685 END FlushErrors ;
686
687
688 (*
689 FlushWarnings - switches the output channel to the error channel
690 and then writes out all warnings.
691 If an error is present the compilation is terminated,
692 if warnings only were emitted then compilation will
693 continue.
694 *)
695
696 PROCEDURE FlushWarnings ;
697 BEGIN
698 IF FlushAll (head, FALSE)
699 THEN
700 END
701 END FlushWarnings ;
702
703
704 (*
705 ErrorStringsAt2 - given error strings, s1, and, s2, it places these
706 strings at token positions, tok1 and tok2, respectively.
707 Both strings are consumed.
708 *)
709
710 PROCEDURE ErrorStringsAt2 (s1, s2: String; tok1, tok2: CARDINAL) ;
711 VAR
712 e: Error ;
713 BEGIN
714 IF s1=s2
715 THEN
716 s2 := Dup(s1)
717 END ;
718 e := NewError(tok1) ;
719 ErrorString(e, s1) ;
720 ErrorString(ChainError(tok2, e), s2)
721 END ErrorStringsAt2 ;
722
723
724 (*
725 ErrorStringAt2 - given an error string, s, it places this
726 string at token positions, tok1 and tok2, respectively.
727 The string is consumed.
728 *)
729
730 PROCEDURE ErrorStringAt2 (s: String; tok1, tok2: CARDINAL) ;
731 BEGIN
732 ErrorStringsAt2(s, s, tok1, tok2)
733 END ErrorStringAt2 ;
734
735
736 (*
737 ErrorStringAt - given an error string, s, it places this
738 string at token position, tok.
739 The string is consumed.
740 *)
741
742 PROCEDURE ErrorStringAt (s: String; tok: CARDINAL) ;
743 VAR
744 e: Error ;
745 BEGIN
746 e := NewError(tok) ;
747 ErrorString(e, s) ;
748 END ErrorStringAt ;
749
750
751 (*
752 WarnStringsAt2 - given warning strings, s1, and, s2, it places these
753 strings at token positions, tok1 and tok2, respectively.
754 Both strings are consumed.
755 *)
756
757 PROCEDURE WarnStringsAt2 (s1, s2: String; tok1, tok2: CARDINAL) ;
758 VAR
759 e: Error ;
760 BEGIN
761 IF s1=s2
762 THEN
763 s2 := Dup(s1)
764 END ;
765 e := NewWarning(tok1) ;
766 ErrorString(e, s1) ;
767 ErrorString(ChainError(tok2, e), s2)
768 END WarnStringsAt2 ;
769
770
771 (*
772 WarnStringAt2 - given an warning string, s, it places this
773 string at token positions, tok1 and tok2, respectively.
774 The string is consumed.
775 *)
776
777 PROCEDURE WarnStringAt2 (s: String; tok1, tok2: CARDINAL) ;
778 BEGIN
779 WarnStringsAt2(s, s, tok1, tok2)
780 END WarnStringAt2 ;
781
782
783 (*
784 WarnStringAt - given an error string, s, it places this
785 string at token position, tok.
786 The string is consumed.
787 *)
788
789 PROCEDURE WarnStringAt (s: String; tok: CARDINAL) ;
790 VAR
791 e: Error ;
792 BEGIN
793 e := NewWarning(tok) ;
794 ErrorString(e, s) ;
795 END WarnStringAt ;
796
797
798 (*
799 ErrorAbort0 - aborts compiling, it flushes all warnings and errors before aborting.
800 *)
801
802 PROCEDURE ErrorAbort0 (a: ARRAY OF CHAR) ;
803 BEGIN
804 FlushWarnings ;
805 IF NOT StrEqual(a, '')
806 THEN
807 WriteFormat0(a)
808 END ;
809 IF NOT FlushAll (head, TRUE)
810 THEN
811 WriteFormat0('unidentified error') ;
812 IF FlushAll (head, TRUE)
813 THEN
814 END
815 END ;
816 ExitOnHalt(1) ;
817 HALT
818 END ErrorAbort0 ;
819
820
821 (*
822 IsErrorScopeNul - returns TRUE if es is NIL or it has a NulName.
823 *)
824
825 PROCEDURE IsErrorScopeNul (es: ErrorScope) : BOOLEAN ;
826 BEGIN
827 RETURN (es = NIL) OR (es^.scopeName = NulName)
828 END IsErrorScopeNul ;
829
830
831 (*
832 GetAnnounceScope - return message with the error scope attached to message.
833 filename and message are treated as read only by this
834 procedure function.
835 *)
836
837 PROCEDURE GetAnnounceScope (filename, message: String) : String ;
838 VAR
839 pre,
840 fmt,
841 desc,
842 quoted: String ;
843 BEGIN
844 IF filename = NIL
845 THEN
846 pre := InitString ('')
847 ELSE
848 pre := Sprintf1 (Mark (InitString ("%s: ")), filename)
849 END ;
850
851 IF NOT IsErrorScopeNul (currentScope)
852 THEN
853 quoted := InitString ('') ;
854 quoted := quoteOpen (quoted) ;
855 quoted := ConCat (quoted, Mark (InitStringCharStar (KeyToCharStar (currentScope^.scopeName)))) ;
856 quoted := quoteClose (quoted)
857 END ;
858
859 IF currentScope = NIL
860 THEN
861 desc := InitString ("no scope active")
862 ELSE
863 CASE currentScope^.scopeKind OF
864
865 definition : desc := InitString ("In definition module") |
866 implementation: desc := InitString ("In implementation module") |
867 program : desc := InitString ("In program module") |
868 module : desc := InitString ("In inner module") |
869 procedure : desc := InitString ("In procedure") |
870 noscope : desc := InitString ("Unknown scope")
871
872 END
873 END ;
874 fmt := ConCat (pre, Mark (desc)) ;
875 IF IsErrorScopeNul (currentScope)
876 THEN
877 fmt := ConCat (fmt, Sprintf0 (Mark (InitString (": "))))
878 ELSE
879 fmt := ConCat (fmt, Sprintf1 (Mark (InitString (" %s: ")), quoted))
880 END ;
881 RETURN ConCat (fmt, message)
882 END GetAnnounceScope ;
883
884
885 (*
886 IsSameScope - return TRUE if a and b refer to the same scope.
887 *)
888
889 PROCEDURE IsSameScope (a, b: ErrorScope) : BOOLEAN ;
890 BEGIN
891 IF a = b
892 THEN
893 RETURN TRUE
894 ELSIF (a = NIL) OR (b = NIL)
895 THEN
896 RETURN FALSE
897 ELSE
898 (* this does not compare the symbol field. *)
899 RETURN (a^.scopeKind = b^.scopeKind) AND (a^.scopeName = b^.scopeName)
900 END
901 END IsSameScope ;
902
903
904 (*
905 AnnounceScope - return the error string s with a scope description prepended
906 assuming that scope has changed.
907 *)
908
909 PROCEDURE AnnounceScope (e: Error; message: String) : String ;
910 BEGIN
911 IF NOT IsSameScope (lastScope, e^.scope)
912 THEN
913 lastScope := e^.scope ;
914 IF IsErrorScopeNul (lastScope)
915 THEN
916 RETURN ConCat (InitString ("no scope active: "), message)
917 ELSE
918 Assert ((e^.scope # NIL) AND (e^.scope^.scopeKind # noscope)) ;
919 (* filename := FindFileNameFromToken (e^.token, 0) ; *)
920 message := GetAnnounceScope (NIL, message)
921 END
922 END ;
923 RETURN message
924 END AnnounceScope ;
925
926
927 (*
928 newErrorScope - create an ErrorScope of kindScope and return the object.
929 It is also added the a dynamic array.
930 *)
931
932 PROCEDURE newErrorScope (kind: KindScope) : ErrorScope ;
933 VAR
934 es: ErrorScope ;
935 c : CARDINAL ;
936 BEGIN
937 IF IsPass0 ()
938 THEN
939 NEW (es) ;
940 es^.scopeKind := kind ;
941 es^.scopeName := NulName ;
942 es^.symbol := NulSym ;
943 PutIndice (scopeArray, HighIndice (scopeArray) + 1, es) ;
944 IF DebugError
945 THEN
946 c := HighIndice (scopeArray) ;
947 printf2 ("pass 0: %d %d\n", c, kind)
948 END
949 ELSE
950 INC (scopeIndex) ;
951 es := GetIndice (scopeArray, scopeIndex) ;
952 IF DebugError
953 THEN
954 IF IsPass1 ()
955 THEN
956 printf3 ("pass 1: %d %d %d\n", scopeIndex, es^.scopeKind, kind)
957 ELSE
958 printf3 ("pass 2: %d %d %d\n", scopeIndex, es^.scopeKind, kind)
959 END
960 END ;
961 Assert (es^.scopeKind = kind)
962 END ;
963 RETURN es
964 END newErrorScope ;
965
966
967 (*
968 DefaultProgramModule - sets up an unnamed program scope before the Ident is seen.
969 *)
970
971 PROCEDURE DefaultProgramModule ;
972 BEGIN
973 PushAddress (scopeStack, currentScope) ;
974 currentScope := newErrorScope (program)
975 END DefaultProgramModule ;
976
977
978 (*
979 DefaultImplementationModule - sets up an unnamed implementation
980 scope before the Ident is seen.
981 *)
982
983 PROCEDURE DefaultImplementationModule ;
984 BEGIN
985 PushAddress (scopeStack, currentScope) ;
986 currentScope := newErrorScope (implementation)
987 END DefaultImplementationModule ;
988
989
990 (*
991 DefaultDefinitionModule - sets up an unnamed definition
992 scope before the Ident is seen.
993 *)
994
995 PROCEDURE DefaultDefinitionModule ;
996 BEGIN
997 PushAddress (scopeStack, currentScope) ;
998 currentScope := newErrorScope (definition)
999 END DefaultDefinitionModule ;
1000
1001
1002 (*
1003 DefaultInnerModule - sets up an unnamed inner
1004 scope before the Ident is seen.
1005 *)
1006
1007 PROCEDURE DefaultInnerModule ;
1008 BEGIN
1009 PushAddress (scopeStack, currentScope) ;
1010 currentScope := newErrorScope (module)
1011 END DefaultInnerModule ;
1012
1013
1014 (*
1015 DefaultProcedure - sets up an unnamed procedure
1016 scope before the Ident is seen.
1017 *)
1018
1019 PROCEDURE DefaultProcedure ;
1020 BEGIN
1021 PushAddress (scopeStack, currentScope) ;
1022 currentScope := newErrorScope (procedure)
1023 END DefaultProcedure ;
1024
1025
1026 (*
1027 EnterImplementationScope - signifies to the error routines that the front end
1028 has started to compile implementation module scopeName.
1029 *)
1030
1031 PROCEDURE EnterImplementationScope (scopename: Name) ;
1032 BEGIN
1033 Assert (currentScope # NIL) ;
1034 Assert (currentScope^.scopeKind = implementation) ;
1035 IF currentScope^.scopeName = NulName
1036 THEN
1037 IF DebugError
1038 THEN
1039 printf1 ("seen implementation: %a\n", scopename)
1040 END ;
1041 currentScope^.scopeName := scopename
1042 END
1043 END EnterImplementationScope ;
1044
1045
1046 (*
1047 EnterProgramScope - signifies to the error routines that the front end
1048 has started to compile program module scopeName.
1049 *)
1050
1051 PROCEDURE EnterProgramScope (scopename: Name) ;
1052 BEGIN
1053 Assert (currentScope # NIL) ;
1054 Assert (currentScope^.scopeKind = program) ;
1055 IF currentScope^.scopeName = NulName
1056 THEN
1057 IF DebugError
1058 THEN
1059 printf1 ("seen program: %a\n", scopename)
1060 END ;
1061 currentScope^.scopeName := scopename
1062 END
1063 END EnterProgramScope ;
1064
1065
1066 (*
1067 EnterModuleScope - signifies to the error routines that the front end
1068 has started to compile an inner module scopeName.
1069 *)
1070
1071 PROCEDURE EnterModuleScope (scopename: Name) ;
1072 BEGIN
1073 Assert (currentScope # NIL) ;
1074 Assert (currentScope^.scopeKind = module) ;
1075 IF currentScope^.scopeName = NulName
1076 THEN
1077 IF DebugError
1078 THEN
1079 printf1 ("seen module: %a\n", scopename)
1080 END ;
1081 currentScope^.scopeName := scopename
1082 END
1083 END EnterModuleScope ;
1084
1085
1086 (*
1087 EnterDefinitionScope - signifies to the error routines that the front end
1088 has started to compile definition module scopeName.
1089 *)
1090
1091 PROCEDURE EnterDefinitionScope (scopename: Name) ;
1092 BEGIN
1093 Assert (currentScope # NIL) ;
1094 Assert (currentScope^.scopeKind = definition) ;
1095 IF currentScope^.scopeName = NulName
1096 THEN
1097 IF DebugError
1098 THEN
1099 printf1 ("seen definition: %a\n", scopename)
1100 END ;
1101 currentScope^.scopeName := scopename
1102 END
1103 END EnterDefinitionScope ;
1104
1105
1106 (*
1107 EnterProcedureScope - signifies to the error routines that the front end
1108 has started to compile definition module scopeName.
1109 *)
1110
1111 PROCEDURE EnterProcedureScope (scopename: Name) ;
1112 BEGIN
1113 Assert (currentScope # NIL) ;
1114 Assert (currentScope^.scopeKind = procedure) ;
1115 IF currentScope^.scopeName = NulName
1116 THEN
1117 IF DebugError
1118 THEN
1119 printf1 ("seen procedure: %a\n", scopename)
1120 END ;
1121 currentScope^.scopeName := scopename
1122 END
1123 END EnterProcedureScope ;
1124
1125
1126 (*
1127 LeaveErrorScope - leave the current scope and pop into the previous one.
1128 *)
1129
1130 PROCEDURE LeaveErrorScope ;
1131 BEGIN
1132 currentScope := PopAddress (scopeStack)
1133 END LeaveErrorScope ;
1134
1135
1136 (*
1137 EnterErrorScope - pushes the currentScope and sets currentScope to scope.
1138 *)
1139
1140 PROCEDURE EnterErrorScope (scope: ErrorScope) ;
1141 BEGIN
1142 PushAddress (scopeStack, currentScope) ;
1143 currentScope := scope
1144 END EnterErrorScope ;
1145
1146
1147 (*
1148 GetCurrentErrorScope - returns currentScope.
1149 *)
1150
1151 PROCEDURE GetCurrentErrorScope () : ErrorScope ;
1152 BEGIN
1153 RETURN currentScope
1154 END GetCurrentErrorScope ;
1155
1156
1157 (*
1158 DepthScope - returns the depth of the scope stack.
1159 *)
1160
1161 PROCEDURE DepthScope () : CARDINAL ;
1162 BEGIN
1163 RETURN NoOfItemsInStackAddress (scopeStack)
1164 END DepthScope ;
1165
1166
1167 (*
1168 ResetErrorScope - should be called at the start of each pass to
1169 reset the error scope index.
1170 *)
1171
1172 PROCEDURE ResetErrorScope ;
1173 BEGIN
1174 scopeIndex := 0
1175 END ResetErrorScope ;
1176
1177
1178 BEGIN
1179 Init
1180 END M2Error.