OSDN Git Service

2007-09-26 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / errout.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               E R R O U T                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 --  Warning! Error messages can be generated during Gigi processing by direct
27 --  calls to error message routines, so it is essential that the processing
28 --  in this body be consistent with the requirements for the Gigi processing
29 --  environment, and that in particular, no disallowed table expansion is
30 --  allowed to occur.
31
32 with Atree;    use Atree;
33 with Casing;   use Casing;
34 with Csets;    use Csets;
35 with Debug;    use Debug;
36 with Einfo;    use Einfo;
37 with Erroutc;  use Erroutc;
38 with Fname;    use Fname;
39 with Gnatvsn;  use Gnatvsn;
40 with Hostparm; use Hostparm;
41 with Lib;      use Lib;
42 with Opt;      use Opt;
43 with Nlists;   use Nlists;
44 with Output;   use Output;
45 with Scans;    use Scans;
46 with Sinput;   use Sinput;
47 with Sinfo;    use Sinfo;
48 with Snames;   use Snames;
49 with Stand;    use Stand;
50 with Style;
51 with Uname;    use Uname;
52
53 with Unchecked_Conversion;
54
55 package body Errout is
56
57    Errors_Must_Be_Ignored : Boolean := False;
58    --  Set to True by procedure Set_Ignore_Errors (True), when calls to
59    --  error message procedures should be ignored (when parsing irrelevant
60    --  text in sources being preprocessed).
61
62    Finalize_Called : Boolean := False;
63    --  Set True if the Finalize routine has been called
64
65    Warn_On_Instance : Boolean;
66    --  Flag set true for warning message to be posted on instance
67
68    ------------------------------------
69    -- Table of Non-Instance Messages --
70    ------------------------------------
71
72    --  This table contains an entry for every error message processed by the
73    --  Error_Msg routine that is not posted on generic (or inlined) instance.
74    --  As explained in further detail in the Error_Msg procedure body, this
75    --  table is used to avoid posting redundant messages on instances.
76
77    type NIM_Record is record
78       Msg : String_Ptr;
79       Loc : Source_Ptr;
80    end record;
81    --  Type used to store text and location of one message
82
83    package Non_Instance_Msgs is new Table.Table (
84      Table_Component_Type => NIM_Record,
85      Table_Index_Type     => Int,
86      Table_Low_Bound      => 1,
87      Table_Initial        => 100,
88      Table_Increment      => 100,
89      Table_Name           => "Non_Instance_Msgs");
90
91    -----------------------
92    -- Local Subprograms --
93    -----------------------
94
95    procedure Error_Msg_Internal
96      (Msg      : String;
97       Sptr     : Source_Ptr;
98       Optr     : Source_Ptr;
99       Msg_Cont : Boolean);
100    --  This is the low level routine used to post messages after dealing with
101    --  the issue of messages placed on instantiations (which get broken up
102    --  into separate calls in Error_Msg). Sptr is the location on which the
103    --  flag will be placed in the output. In the case where the flag is on
104    --  the template, this points directly to the template, not to one of the
105    --  instantiation copies of the template. Optr is the original location
106    --  used to flag the error, and this may indeed point to an instantiation
107    --  copy. So typically we can see Optr pointing to the template location
108    --  in an instantiation copy when Sptr points to the source location of
109    --  the actual instantiation (i.e the line with the new). Msg_Cont is
110    --  set true if this is a continuation message.
111
112    function No_Warnings (N : Node_Or_Entity_Id) return Boolean;
113    --  Determines if warnings should be suppressed for the given node
114
115    function OK_Node (N : Node_Id) return Boolean;
116    --  Determines if a node is an OK node to place an error message on (return
117    --  True) or if the error message should be suppressed (return False). A
118    --  message is suppressed if the node already has an error posted on it,
119    --  or if it refers to an Etype that has an error posted on it, or if
120    --  it references an Entity that has an error posted on it.
121
122    procedure Output_Source_Line
123      (L     : Physical_Line_Number;
124       Sfile : Source_File_Index;
125       Errs  : Boolean);
126    --  Outputs text of source line L, in file S, together with preceding line
127    --  number, as described above for Output_Line_Number. The Errs parameter
128    --  indicates if there are errors attached to the line, which forces
129    --  listing on, even in the presence of pragma List (Off).
130
131    procedure Set_Msg_Insertion_Column;
132    --  Handle column number insertion (@ insertion character)
133
134    procedure Set_Msg_Insertion_Node;
135    --  Handle node (name from node) insertion (& insertion character)
136
137    procedure Set_Msg_Insertion_Type_Reference (Flag : Source_Ptr);
138    --  Handle type reference (right brace insertion character). Flag is the
139    --  location of the flag, which is provided for the internal call to
140    --  Set_Msg_Insertion_Line_Number,
141
142    procedure Set_Msg_Insertion_Unit_Name (Suffix : Boolean := True);
143    --  Handle unit name insertion ($ insertion character). Depending on Boolean
144    --  parameter Suffix, (spec) or (body) is appended after the unit name.
145
146    procedure Set_Msg_Node (Node : Node_Id);
147    --  Add the sequence of characters for the name associated with the
148    --  given node to the current message.
149
150    procedure Set_Msg_Text (Text : String; Flag : Source_Ptr);
151    --  Add a sequence of characters to the current message. The characters may
152    --  be one of the special insertion characters (see documentation in spec).
153    --  Flag is the location at which the error is to be posted, which is used
154    --  to determine whether or not the # insertion needs a file name. The
155    --  variables Msg_Buffer, Msglen, Is_Style_Msg, Is_Warning_Msg, and
156    --  Is_Unconditional_Msg are set on return.
157
158    procedure Set_Posted (N : Node_Id);
159    --  Sets the Error_Posted flag on the given node, and all its parents
160    --  that are subexpressions and then on the parent non-subexpression
161    --  construct that contains the original expression (this reduces the
162    --  number of cascaded messages). Note that this call only has an effect
163    --  for a serious error. For a non-serious error, it has no effect.
164
165    procedure Set_Qualification (N : Nat; E : Entity_Id);
166    --  Outputs up to N levels of qualification for the given entity. For
167    --  example, the entity A.B.C.D will output B.C. if N = 2.
168
169    function Special_Msg_Delete
170      (Msg : String;
171       N   : Node_Or_Entity_Id;
172       E   : Node_Or_Entity_Id) return Boolean;
173    --  This function is called from Error_Msg_NEL, passing the message Msg,
174    --  node N on which the error is to be posted, and the entity or node E
175    --  to be used for an & insertion in the message if any. The job of this
176    --  procedure is to test for certain cascaded messages that we would like
177    --  to suppress. If the message is to be suppressed then we return True.
178    --  If the message should be generated (the normal case) False is returned.
179
180    procedure Unwind_Internal_Type (Ent : in out Entity_Id);
181    --  This procedure is given an entity id for an internal type, i.e.
182    --  a type with an internal name. It unwinds the type to try to get
183    --  to something reasonably printable, generating prefixes like
184    --  "subtype of", "access to", etc along the way in the buffer. The
185    --  value in Ent on return is the final name to be printed. Hopefully
186    --  this is not an internal name, but in some internal name cases, it
187    --  is an internal name, and has to be printed anyway (although in this
188    --  case the message has been killed if possible). The global variable
189    --  Class_Flag is set to True if the resulting entity should have
190    --  'Class appended to its name (see Add_Class procedure), and is
191    --  otherwise unchanged.
192
193    procedure VMS_Convert;
194    --  This procedure has no effect if called when the host is not OpenVMS.
195    --  If the host is indeed OpenVMS, then the error message stored in
196    --  Msg_Buffer is scanned for appearences of switch names which need
197    --  converting to corresponding VMS qualifer names. See Gnames/Vnames
198    --  table in Errout spec for precise definition of the conversion that
199    --  is performed by this routine in OpenVMS mode.
200
201    -----------------------
202    -- Change_Error_Text --
203    -----------------------
204
205    procedure Change_Error_Text (Error_Id : Error_Msg_Id; New_Msg : String) is
206       Save_Next : Error_Msg_Id;
207       Err_Id    : Error_Msg_Id := Error_Id;
208
209    begin
210       Set_Msg_Text (New_Msg, Errors.Table (Error_Id).Sptr);
211       Errors.Table (Error_Id).Text := new String'(Msg_Buffer (1 .. Msglen));
212
213       --  If in immediate error message mode, output modified error message now
214       --  This is just a bit tricky, because we want to output just a single
215       --  message, and the messages we modified is already linked in. We solve
216       --  this by temporarily resetting its forward pointer to empty.
217
218       if Debug_Flag_OO then
219          Save_Next := Errors.Table (Error_Id).Next;
220          Errors.Table (Error_Id).Next := No_Error_Msg;
221          Write_Eol;
222          Output_Source_Line
223            (Errors.Table (Error_Id).Line, Errors.Table (Error_Id).Sfile, True);
224          Output_Error_Msgs (Err_Id);
225          Errors.Table (Error_Id).Next := Save_Next;
226       end if;
227    end Change_Error_Text;
228
229    ------------------------
230    -- Compilation_Errors --
231    ------------------------
232
233    function Compilation_Errors return Boolean is
234    begin
235       if not Finalize_Called then
236          raise Program_Error;
237       else
238          return Erroutc.Compilation_Errors;
239       end if;
240    end Compilation_Errors;
241
242    ---------------
243    -- Error_Msg --
244    ---------------
245
246    --  Error_Msg posts a flag at the given location, except that if the
247    --  Flag_Location points within a generic template and corresponds
248    --  to an instantiation of this generic template, then the actual
249    --  message will be posted on the generic instantiation, along with
250    --  additional messages referencing the generic declaration.
251
252    procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
253       Sindex : Source_File_Index;
254       --  Source index for flag location
255
256       Orig_Loc : Source_Ptr;
257       --  Original location of Flag_Location (i.e. location in original
258       --  template in instantiation case, otherwise unchanged).
259
260    begin
261       --  It is a fatal error to issue an error message when scanning from
262       --  the internal source buffer (see Sinput for further documentation)
263
264       pragma Assert (Sinput.Source /= Internal_Source_Ptr);
265
266       --  Return if all errors are to be ignored
267
268       if Errors_Must_Be_Ignored then
269          return;
270       end if;
271
272       --  If we already have messages, and we are trying to place a message
273       --  at No_Location or in package Standard, then just ignore the attempt
274       --  since we assume that what is happening is some cascaded junk. Note
275       --  that this is safe in the sense that proceeding will surely bomb.
276
277       if Flag_Location < First_Source_Ptr
278         and then Total_Errors_Detected > 0
279       then
280          return;
281       end if;
282
283       --  Start processing of new message
284
285       Sindex := Get_Source_File_Index (Flag_Location);
286       Test_Style_Warning_Serious_Msg (Msg);
287       Orig_Loc := Original_Location (Flag_Location);
288
289       --  If the current location is in an instantiation, the issue arises
290       --  of whether to post the message on the template or the instantiation.
291
292       --  The way we decide is to see if we have posted the same message
293       --  on the template when we compiled the template (the template is
294       --  always compiled before any instantiations). For this purpose,
295       --  we use a separate table of messages. The reason we do this is
296       --  twofold:
297
298       --     First, the messages can get changed by various processing
299       --     including the insertion of tokens etc, making it hard to
300       --     do the comparison.
301
302       --     Second, we will suppress a warning on a template if it is
303       --     not in the current extended source unit. That's reasonable
304       --     and means we don't want the warning on the instantiation
305       --     here either, but it does mean that the main error table
306       --     would not in any case include the message.
307
308       if Flag_Location = Orig_Loc then
309          Non_Instance_Msgs.Append ((new String'(Msg), Flag_Location));
310          Warn_On_Instance := False;
311
312       --  Here we have an instance message
313
314       else
315          --  Delete if debug flag off, and this message duplicates a
316          --  message already posted on the corresponding template
317
318          if not Debug_Flag_GG then
319             for J in Non_Instance_Msgs.First .. Non_Instance_Msgs.Last loop
320                if Msg = Non_Instance_Msgs.Table (J).Msg.all
321                  and then Non_Instance_Msgs.Table (J).Loc = Orig_Loc
322                then
323                   return;
324                end if;
325             end loop;
326          end if;
327
328          --  No duplicate, so error/warning will be posted on instance
329
330          Warn_On_Instance := Is_Warning_Msg;
331       end if;
332
333       --  Ignore warning message that is suppressed. Note that style
334       --  checks are not considered warning messages for this purpose
335
336       if Is_Warning_Msg and then Warnings_Suppressed (Orig_Loc) then
337          return;
338       end if;
339
340       --  The idea at this stage is that we have two kinds of messages
341
342       --  First, we have those messages that are to be placed as requested at
343       --  Flag_Location. This includes messages that have nothing to do with
344       --  generics, and also messages placed on generic templates that reflect
345       --  an error in the template itself. For such messages we simply call
346       --  Error_Msg_Internal to place the message in the requested location.
347
348       if Instantiation (Sindex) = No_Location then
349          Error_Msg_Internal (Msg, Flag_Location, Flag_Location, False);
350          return;
351       end if;
352
353       --  If we are trying to flag an error in an instantiation, we may have
354       --  a generic contract violation. What we generate in this case is:
355
356       --     instantiation error at ...
357       --     original error message
358
359       --  or
360
361       --     warning: in instantiation at
362       --     warning: original warning message
363
364       --  All these messages are posted at the location of the top level
365       --  instantiation. If there are nested instantiations, then the
366       --  instantiation error message can be repeated, pointing to each
367       --  of the relevant instantiations.
368
369       --  Note: the instantiation mechanism is also shared for inlining
370       --  of subprogram bodies when front end inlining is done. In this
371       --  case the messages have the form:
372
373       --     in inlined body at ...
374       --     original error message
375
376       --  or
377
378       --     warning: in inlined body at
379       --     warning: original warning message
380
381       --  OK, this is the case where we have an instantiation error, and
382       --  we need to generate the error on the instantiation, rather than
383       --  on the template.
384
385       declare
386          Actual_Error_Loc : Source_Ptr;
387          --  Location of outer level instantiation in instantiation case, or
388          --  just a copy of Flag_Location in the normal case. This is the
389          --  location where all error messages will actually be posted.
390
391          Save_Error_Msg_Sloc : constant Source_Ptr := Error_Msg_Sloc;
392          --  Save possible location set for caller's message. We need to
393          --  use Error_Msg_Sloc for the location of the instantiation error
394          --  but we have to preserve a possible original value.
395
396          X : Source_File_Index;
397
398          Msg_Cont_Status : Boolean;
399          --  Used to label continuation lines in instantiation case with
400          --  proper Msg_Cont status.
401
402       begin
403          --  Loop to find highest level instantiation, where all error
404          --  messages will be placed.
405
406          X := Sindex;
407          loop
408             Actual_Error_Loc := Instantiation (X);
409             X := Get_Source_File_Index (Actual_Error_Loc);
410             exit when Instantiation (X) = No_Location;
411          end loop;
412
413          --  Since we are generating the messages at the instantiation
414          --  point in any case, we do not want the references to the
415          --  bad lines in the instance to be annotated with the location
416          --  of the instantiation.
417
418          Suppress_Instance_Location := True;
419          Msg_Cont_Status := False;
420
421          --  Loop to generate instantiation messages
422
423          Error_Msg_Sloc := Flag_Location;
424          X := Get_Source_File_Index (Flag_Location);
425
426          while Instantiation (X) /= No_Location loop
427
428             --  Suppress instantiation message on continuation lines
429
430             if Msg (Msg'First) /= '\' then
431
432                --  Case of inlined body
433
434                if Inlined_Body (X) then
435                   if Is_Warning_Msg then
436                      Error_Msg_Internal
437                        ("?in inlined body #",
438                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
439
440                   else
441                      Error_Msg_Internal
442                        ("error in inlined body #",
443                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
444                   end if;
445
446                --  Case of generic instantiation
447
448                else
449                   if Is_Warning_Msg then
450                      Error_Msg_Internal
451                        ("?in instantiation #",
452                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
453
454                   else
455                      Error_Msg_Internal
456                        ("instantiation error #",
457                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
458                   end if;
459                end if;
460             end if;
461
462             Error_Msg_Sloc := Instantiation (X);
463             X := Get_Source_File_Index (Error_Msg_Sloc);
464             Msg_Cont_Status := True;
465          end loop;
466
467          Suppress_Instance_Location := False;
468          Error_Msg_Sloc := Save_Error_Msg_Sloc;
469
470          --  Here we output the original message on the outer instantiation
471
472          Error_Msg_Internal
473            (Msg, Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
474       end;
475    end Error_Msg;
476
477    ------------------
478    -- Error_Msg_AP --
479    ------------------
480
481    procedure Error_Msg_AP (Msg : String) is
482       S1 : Source_Ptr;
483       C  : Character;
484
485    begin
486       --  If we had saved the Scan_Ptr value after scanning the previous
487       --  token, then we would have exactly the right place for putting
488       --  the flag immediately at hand. However, that would add at least
489       --  two instructions to a Scan call *just* to service the possibility
490       --  of an Error_Msg_AP call. So instead we reconstruct that value.
491
492       --  We have two possibilities, start with Prev_Token_Ptr and skip over
493       --  the current token, which is made harder by the possibility that this
494       --  token may be in error, or start with Token_Ptr and work backwards.
495       --  We used to take the second approach, but it's hard because of
496       --  comments, and harder still because things that look like comments
497       --  can appear inside strings. So now we take the first approach.
498
499       --  Note: in the case where there is no previous token, Prev_Token_Ptr
500       --  is set to Source_First, which is a reasonable position for the
501       --  error flag in this situation.
502
503       S1 := Prev_Token_Ptr;
504       C := Source (S1);
505
506       --  If the previous token is a string literal, we need a special approach
507       --  since there may be white space inside the literal and we don't want
508       --  to stop on that white space.
509
510       --  Note: since this is an error recovery issue anyway, it is not worth
511       --  worrying about special UTF_32 line terminator characters here.
512
513       if Prev_Token = Tok_String_Literal then
514          loop
515             S1 := S1 + 1;
516
517             if Source (S1) = C then
518                S1 := S1 + 1;
519                exit when Source (S1) /= C;
520             elsif Source (S1) in Line_Terminator then
521                exit;
522             end if;
523          end loop;
524
525       --  Character literal also needs special handling
526
527       elsif Prev_Token = Tok_Char_Literal then
528          S1 := S1 + 3;
529
530       --  Otherwise we search forward for the end of the current token, marked
531       --  by a line terminator, white space, a comment symbol or if we bump
532       --  into the following token (i.e. the current token).
533
534       --  Again, it is not worth worrying about UTF_32 special line terminator
535       --  characters in this context, since this is only for error recovery.
536
537       else
538          while Source (S1) not in Line_Terminator
539            and then Source (S1) /= ' '
540            and then Source (S1) /= ASCII.HT
541            and then (Source (S1) /= '-' or else Source (S1 + 1) /= '-')
542            and then S1 /= Token_Ptr
543          loop
544             S1 := S1 + 1;
545          end loop;
546       end if;
547
548       --  S1 is now set to the location for the flag
549
550       Error_Msg (Msg, S1);
551    end Error_Msg_AP;
552
553    ------------------
554    -- Error_Msg_BC --
555    ------------------
556
557    procedure Error_Msg_BC (Msg : String) is
558    begin
559       --  If we are at end of file, post the flag after the previous token
560
561       if Token = Tok_EOF then
562          Error_Msg_AP (Msg);
563
564       --  If we are at start of file, post the flag at the current token
565
566       elsif Token_Ptr = Source_First (Current_Source_File) then
567          Error_Msg_SC (Msg);
568
569       --  If the character before the current token is a space or a horizontal
570       --  tab, then we place the flag on this character (in the case of a tab
571       --  we would really like to place it in the "last" character of the tab
572       --  space, but that it too much trouble to worry about).
573
574       elsif Source (Token_Ptr - 1) = ' '
575          or else Source (Token_Ptr - 1) = ASCII.HT
576       then
577          Error_Msg (Msg, Token_Ptr - 1);
578
579       --  If there is no space or tab before the current token, then there is
580       --  no room to place the flag before the token, so we place it on the
581       --  token instead (this happens for example at the start of a line).
582
583       else
584          Error_Msg (Msg, Token_Ptr);
585       end if;
586    end Error_Msg_BC;
587
588    -------------------
589    -- Error_Msg_CRT --
590    -------------------
591
592    procedure Error_Msg_CRT (Feature : String; N : Node_Id) is
593       CNRT : constant String := " not allowed in no run time mode";
594       CCRT : constant String := " not supported by configuration>";
595
596       S : String (1 .. Feature'Length + 1 + CCRT'Length);
597       L : Natural;
598
599    begin
600       S (1) := '|';
601       S (2 .. Feature'Length + 1) := Feature;
602       L := Feature'Length + 2;
603
604       if No_Run_Time_Mode then
605          S (L .. L + CNRT'Length - 1) := CNRT;
606          L := L + CNRT'Length - 1;
607
608       else pragma Assert (Configurable_Run_Time_Mode);
609          S (L .. L + CCRT'Length - 1) := CCRT;
610          L := L + CCRT'Length - 1;
611       end if;
612
613       Error_Msg_N (S (1 .. L), N);
614       Configurable_Run_Time_Violations := Configurable_Run_Time_Violations + 1;
615    end Error_Msg_CRT;
616
617    -----------------
618    -- Error_Msg_F --
619    -----------------
620
621    procedure Error_Msg_F (Msg : String; N : Node_Id) is
622    begin
623       Error_Msg_NEL (Msg, N, N, Sloc (First_Node (N)));
624    end Error_Msg_F;
625
626    ------------------
627    -- Error_Msg_FE --
628    ------------------
629
630    procedure Error_Msg_FE
631      (Msg : String;
632       N   : Node_Id;
633       E   : Node_Or_Entity_Id)
634    is
635    begin
636       Error_Msg_NEL (Msg, N, E, Sloc (First_Node (N)));
637    end Error_Msg_FE;
638
639    ------------------------
640    -- Error_Msg_Internal --
641    ------------------------
642
643    procedure Error_Msg_Internal
644      (Msg      : String;
645       Sptr     : Source_Ptr;
646       Optr     : Source_Ptr;
647       Msg_Cont : Boolean)
648    is
649       Next_Msg : Error_Msg_Id;
650       --  Pointer to next message at insertion point
651
652       Prev_Msg : Error_Msg_Id;
653       --  Pointer to previous message at insertion point
654
655       Temp_Msg : Error_Msg_Id;
656
657       procedure Handle_Serious_Error;
658       --  Internal procedure to do all error message handling for a serious
659       --  error message, other than bumping the error counts and arranging
660       --  for the message to be output.
661
662       --------------------------
663       -- Handle_Serious_Error --
664       --------------------------
665
666       procedure Handle_Serious_Error is
667       begin
668          --  Turn off code generation if not done already
669
670          if Operating_Mode = Generate_Code then
671             Operating_Mode := Check_Semantics;
672             Expander_Active := False;
673          end if;
674
675          --  Set the fatal error flag in the unit table unless we are
676          --  in Try_Semantics mode. This stops the semantics from being
677          --  performed if we find a serious error. This is skipped if we
678          --  are currently dealing with the configuration pragma file.
679
680          if not Try_Semantics
681            and then Current_Source_Unit /= No_Unit
682          then
683             Set_Fatal_Error (Get_Source_Unit (Sptr));
684          end if;
685       end Handle_Serious_Error;
686
687    --  Start of processing for Error_Msg_Internal
688
689    begin
690       if Raise_Exception_On_Error /= 0 then
691          raise Error_Msg_Exception;
692       end if;
693
694       Continuation := Msg_Cont;
695       Continuation_New_Line := False;
696       Suppress_Message := False;
697       Kill_Message := False;
698       Set_Msg_Text (Msg, Sptr);
699
700       --  Kill continuation if parent message killed
701
702       if Continuation and Last_Killed then
703          return;
704       end if;
705
706       --  Return without doing anything if message is suppressed
707
708       if Suppress_Message
709         and not All_Errors_Mode
710         and not (Msg (Msg'Last) = '!')
711         and not Is_Warning_Msg
712       then
713          if not Continuation then
714             Last_Killed := True;
715          end if;
716
717          return;
718       end if;
719
720       --  Return without doing anything if message is killed and this
721       --  is not the first error message. The philosophy is that if we
722       --  get a weird error message and we already have had a message,
723       --  then we hope the weird message is a junk cascaded message
724
725       if Kill_Message
726         and then not All_Errors_Mode
727         and then Total_Errors_Detected /= 0
728       then
729          if not Continuation then
730             Last_Killed := True;
731          end if;
732
733          return;
734       end if;
735
736       --  Special check for warning message to see if it should be output
737
738       if Is_Warning_Msg then
739
740          --  Immediate return if warning message and warnings are suppressed
741
742          if Warnings_Suppressed (Optr)
743            or else Warnings_Suppressed (Sptr)
744          then
745             Cur_Msg := No_Error_Msg;
746             return;
747          end if;
748
749          --  If the flag location is in the main extended source unit
750          --  then for sure we want the warning since it definitely belongs
751
752          if In_Extended_Main_Source_Unit (Sptr) then
753             null;
754
755          --  If the flag location is not in the main extended source unit,
756          --  then we want to eliminate the warning, unless it is in the
757          --  extended main code unit and we want warnings on the instance.
758
759          elsif In_Extended_Main_Code_Unit (Sptr)
760            and then Warn_On_Instance
761          then
762             null;
763
764          --  Keep warning if debug flag G set
765
766          elsif Debug_Flag_GG then
767             null;
768
769          --  Here is where we delete a warning from a with'ed unit
770
771          else
772             Cur_Msg := No_Error_Msg;
773
774             if not Continuation then
775                Last_Killed := True;
776             end if;
777
778             return;
779          end if;
780       end if;
781
782       --  If message is to be ignored in special ignore message mode, this is
783       --  where we do this special processing, bypassing message output.
784
785       if Ignore_Errors_Enable > 0 then
786          if Is_Serious_Error then
787             Handle_Serious_Error;
788          end if;
789
790          return;
791       end if;
792
793       --  If error message line length set, and this is a continuation message
794       --  then all we do is to append the text to the text of the last message
795       --  with a comma space separator.
796
797       if Error_Msg_Line_Length /= 0
798         and then Continuation
799       then
800          Cur_Msg := Errors.Last;
801
802          declare
803             Oldm : String_Ptr := Errors.Table (Cur_Msg).Text;
804             Newm : String (1 .. Oldm'Last + 2 + Msglen);
805             Newl : Natural;
806
807          begin
808             --  First copy old message to new one and free it
809
810             Newm (Oldm'Range) := Oldm.all;
811             Newl := Oldm'Length;
812             Free (Oldm);
813
814             --  Now deal with separation between messages. Normally this
815             --  is simply comma space, but there are some special cases.
816
817             --  If continuation new line, then put actual NL character in msg
818
819             if Continuation_New_Line then
820                Newl := Newl + 1;
821                Newm (Newl) := ASCII.LF;
822
823             --  If continuation message is enclosed in parentheses, then
824             --  special treatment (don't need a comma, and we want to combine
825             --  successive parenthetical remarks into a single one with
826             --  separating commas).
827
828             elsif Msg_Buffer (1) = '(' and then Msg_Buffer (Msglen) = ')' then
829
830                --  Case where existing message ends in right paren, remove
831                --  and separate parenthetical remarks with a comma.
832
833                if Newm (Newl) = ')' then
834                   Newm (Newl) := ',';
835                   Msg_Buffer (1) := ' ';
836
837                   --  Case where we are adding new parenthetical comment
838
839                else
840                   Newl := Newl + 1;
841                   Newm (Newl) := ' ';
842                end if;
843
844             --  Case where continuation not in parens and no new line
845
846             else
847                Newm (Newl + 1 .. Newl + 2) := ", ";
848                Newl := Newl + 2;
849             end if;
850
851             --  Append new message
852
853             Newm (Newl + 1 .. Newl + Msglen) := Msg_Buffer (1 .. Msglen);
854             Newl := Newl + Msglen;
855             Errors.Table (Cur_Msg).Text := new String'(Newm (1 .. Newl));
856          end;
857
858          return;
859       end if;
860
861       --  Otherwise build error message object for new message
862
863       Errors.Increment_Last;
864       Cur_Msg := Errors.Last;
865       Errors.Table (Cur_Msg).Text     := new String'(Msg_Buffer (1 .. Msglen));
866       Errors.Table (Cur_Msg).Next     := No_Error_Msg;
867       Errors.Table (Cur_Msg).Sptr     := Sptr;
868       Errors.Table (Cur_Msg).Optr     := Optr;
869       Errors.Table (Cur_Msg).Sfile    := Get_Source_File_Index (Sptr);
870       Errors.Table (Cur_Msg).Line     := Get_Physical_Line_Number (Sptr);
871       Errors.Table (Cur_Msg).Col      := Get_Column_Number (Sptr);
872       Errors.Table (Cur_Msg).Warn     := Is_Warning_Msg;
873       Errors.Table (Cur_Msg).Style    := Is_Style_Msg;
874       Errors.Table (Cur_Msg).Serious  := Is_Serious_Error;
875       Errors.Table (Cur_Msg).Uncond   := Is_Unconditional_Msg;
876       Errors.Table (Cur_Msg).Msg_Cont := Continuation;
877       Errors.Table (Cur_Msg).Deleted  := False;
878
879       --  If immediate errors mode set, output error message now. Also output
880       --  now if the -d1 debug flag is set (so node number message comes out
881       --  just before actual error message)
882
883       if Debug_Flag_OO or else Debug_Flag_1 then
884          Write_Eol;
885          Output_Source_Line
886            (Errors.Table (Cur_Msg).Line, Errors.Table (Cur_Msg).Sfile, True);
887          Temp_Msg := Cur_Msg;
888          Output_Error_Msgs (Temp_Msg);
889
890       --  If not in immediate errors mode, then we insert the message in the
891       --  error chain for later output by Finalize. The messages are sorted
892       --  first by unit (main unit comes first), and within a unit by source
893       --  location (earlier flag location first in the chain).
894
895       else
896          --  First a quick check, does this belong at the very end of the chain
897          --  of error messages. This saves a lot of time in the normal case if
898          --  there are lots of messages.
899
900          if Last_Error_Msg /= No_Error_Msg
901            and then Errors.Table (Cur_Msg).Sfile =
902                     Errors.Table (Last_Error_Msg).Sfile
903            and then (Sptr > Errors.Table (Last_Error_Msg).Sptr
904                        or else
905                           (Sptr = Errors.Table (Last_Error_Msg).Sptr
906                              and then
907                                Optr > Errors.Table (Last_Error_Msg).Optr))
908          then
909             Prev_Msg := Last_Error_Msg;
910             Next_Msg := No_Error_Msg;
911
912          --  Otherwise do a full sequential search for the insertion point
913
914          else
915             Prev_Msg := No_Error_Msg;
916             Next_Msg := First_Error_Msg;
917             while Next_Msg /= No_Error_Msg loop
918                exit when
919                  Errors.Table (Cur_Msg).Sfile < Errors.Table (Next_Msg).Sfile;
920
921                if Errors.Table (Cur_Msg).Sfile =
922                     Errors.Table (Next_Msg).Sfile
923                then
924                   exit when Sptr < Errors.Table (Next_Msg).Sptr
925                               or else
926                                 (Sptr = Errors.Table (Next_Msg).Sptr
927                                    and then
928                                  Optr < Errors.Table (Next_Msg).Optr);
929                end if;
930
931                Prev_Msg := Next_Msg;
932                Next_Msg := Errors.Table (Next_Msg).Next;
933             end loop;
934          end if;
935
936          --  Now we insert the new message in the error chain. The insertion
937          --  point for the message is after Prev_Msg and before Next_Msg.
938
939          --  The possible insertion point for the new message is after Prev_Msg
940          --  and before Next_Msg. However, this is where we do a special check
941          --  for redundant parsing messages, defined as messages posted on the
942          --  same line. The idea here is that probably such messages are junk
943          --  from the parser recovering. In full errors mode, we don't do this
944          --  deletion, but otherwise such messages are discarded at this stage.
945
946          if Prev_Msg /= No_Error_Msg
947            and then Errors.Table (Prev_Msg).Line =
948                                              Errors.Table (Cur_Msg).Line
949            and then Errors.Table (Prev_Msg).Sfile =
950                                              Errors.Table (Cur_Msg).Sfile
951            and then Compiler_State = Parsing
952            and then not All_Errors_Mode
953          then
954             --  Don't delete unconditional messages and at this stage,
955             --  don't delete continuation lines (we attempted to delete
956             --  those earlier if the parent message was deleted.
957
958             if not Errors.Table (Cur_Msg).Uncond
959               and then not Continuation
960             then
961                --  Don't delete if prev msg is warning and new msg is an error.
962                --  This is because we don't want a real error masked by a
963                --  warning. In all other cases (that is parse errors for the
964                --  same line that are not unconditional) we do delete the
965                --  message. This helps to avoid junk extra messages from
966                --  cascaded parsing errors
967
968                if not (Errors.Table (Prev_Msg).Warn
969                          or
970                        Errors.Table (Prev_Msg).Style)
971                  or else
972                       (Errors.Table (Cur_Msg).Warn
973                          or
974                        Errors.Table (Cur_Msg).Style)
975                then
976                   --  All tests passed, delete the message by simply returning
977                   --  without any further processing.
978
979                   if not Continuation then
980                      Last_Killed := True;
981                   end if;
982
983                   return;
984                end if;
985             end if;
986          end if;
987
988          --  Come here if message is to be inserted in the error chain
989
990          if not Continuation then
991             Last_Killed := False;
992          end if;
993
994          if Prev_Msg = No_Error_Msg then
995             First_Error_Msg := Cur_Msg;
996          else
997             Errors.Table (Prev_Msg).Next := Cur_Msg;
998          end if;
999
1000          Errors.Table (Cur_Msg).Next := Next_Msg;
1001
1002          if Next_Msg = No_Error_Msg then
1003             Last_Error_Msg := Cur_Msg;
1004          end if;
1005       end if;
1006
1007       --  Bump appropriate statistics count
1008
1009       if Errors.Table (Cur_Msg).Warn
1010         or else Errors.Table (Cur_Msg).Style
1011       then
1012          Warnings_Detected := Warnings_Detected + 1;
1013       else
1014          Total_Errors_Detected := Total_Errors_Detected + 1;
1015
1016          if Errors.Table (Cur_Msg).Serious then
1017             Serious_Errors_Detected := Serious_Errors_Detected + 1;
1018             Handle_Serious_Error;
1019          end if;
1020       end if;
1021
1022       --  Terminate if max errors reached
1023
1024       if Total_Errors_Detected + Warnings_Detected = Maximum_Errors then
1025          raise Unrecoverable_Error;
1026       end if;
1027    end Error_Msg_Internal;
1028
1029    -----------------
1030    -- Error_Msg_N --
1031    -----------------
1032
1033    procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
1034    begin
1035       Error_Msg_NEL (Msg, N, N, Sloc (N));
1036    end Error_Msg_N;
1037
1038    ------------------
1039    -- Error_Msg_NE --
1040    ------------------
1041
1042    procedure Error_Msg_NE
1043      (Msg : String;
1044       N   : Node_Or_Entity_Id;
1045       E   : Node_Or_Entity_Id)
1046    is
1047    begin
1048       Error_Msg_NEL (Msg, N, E, Sloc (N));
1049    end Error_Msg_NE;
1050
1051    -------------------
1052    -- Error_Msg_NEL --
1053    -------------------
1054
1055    procedure Error_Msg_NEL
1056      (Msg           : String;
1057       N             : Node_Or_Entity_Id;
1058       E             : Node_Or_Entity_Id;
1059       Flag_Location : Source_Ptr)
1060    is
1061    begin
1062       if Special_Msg_Delete (Msg, N, E) then
1063          return;
1064       end if;
1065
1066       Test_Style_Warning_Serious_Msg (Msg);
1067
1068       --  Special handling for warning messages
1069
1070       if Is_Warning_Msg then
1071
1072          --  Suppress if no warnings set for either entity or node
1073
1074          if No_Warnings (N) or else No_Warnings (E) then
1075             return;
1076          end if;
1077
1078          --  Suppress if inside loop that is known to be null
1079
1080          declare
1081             P : Node_Id;
1082
1083          begin
1084             P := Parent (N);
1085             while Present (P) loop
1086                if Nkind (P) = N_Loop_Statement and then Is_Null_Loop (P) then
1087                   return;
1088                end if;
1089
1090                P := Parent (P);
1091             end loop;
1092          end;
1093       end if;
1094
1095       --  Test for message to be output
1096
1097       if All_Errors_Mode
1098         or else Msg (Msg'Last) = '!'
1099         or else Is_Warning_Msg
1100         or else OK_Node (N)
1101         or else (Msg (Msg'First) = '\' and not Last_Killed)
1102       then
1103          Debug_Output (N);
1104          Error_Msg_Node_1 := E;
1105          Error_Msg (Msg, Flag_Location);
1106
1107       else
1108          Last_Killed := True;
1109       end if;
1110
1111       if not Is_Warning_Msg and then not Is_Style_Msg then
1112          Set_Posted (N);
1113       end if;
1114    end Error_Msg_NEL;
1115
1116    ------------------
1117    -- Error_Msg_NW --
1118    ------------------
1119
1120    procedure Error_Msg_NW
1121      (Eflag : Boolean;
1122       Msg   : String;
1123       N     : Node_Or_Entity_Id)
1124    is
1125    begin
1126       if Eflag
1127         and then In_Extended_Main_Source_Unit (N)
1128         and then Comes_From_Source (N)
1129       then
1130          Error_Msg_NEL (Msg, N, N, Sloc (N));
1131       end if;
1132    end Error_Msg_NW;
1133
1134    -----------------
1135    -- Error_Msg_S --
1136    -----------------
1137
1138    procedure Error_Msg_S (Msg : String) is
1139    begin
1140       Error_Msg (Msg, Scan_Ptr);
1141    end Error_Msg_S;
1142
1143    ------------------
1144    -- Error_Msg_SC --
1145    ------------------
1146
1147    procedure Error_Msg_SC (Msg : String) is
1148    begin
1149       --  If we are at end of file, post the flag after the previous token
1150
1151       if Token = Tok_EOF then
1152          Error_Msg_AP (Msg);
1153
1154       --  For all other cases the message is posted at the current token
1155       --  pointer position
1156
1157       else
1158          Error_Msg (Msg, Token_Ptr);
1159       end if;
1160    end Error_Msg_SC;
1161
1162    ------------------
1163    -- Error_Msg_SP --
1164    ------------------
1165
1166    procedure Error_Msg_SP (Msg : String) is
1167    begin
1168       --  Note: in the case where there is no previous token, Prev_Token_Ptr
1169       --  is set to Source_First, which is a reasonable position for the
1170       --  error flag in this situation
1171
1172       Error_Msg (Msg, Prev_Token_Ptr);
1173    end Error_Msg_SP;
1174
1175    --------------
1176    -- Finalize --
1177    --------------
1178
1179    procedure Finalize (Last_Call : Boolean) is
1180       Cur : Error_Msg_Id;
1181       Nxt : Error_Msg_Id;
1182       F   : Error_Msg_Id;
1183
1184    begin
1185       --  Eliminate any duplicated error messages from the list. This is
1186       --  done after the fact to avoid problems with Change_Error_Text.
1187
1188       Cur := First_Error_Msg;
1189       while Cur /= No_Error_Msg loop
1190          Nxt := Errors.Table (Cur).Next;
1191
1192          F := Nxt;
1193          while F /= No_Error_Msg
1194            and then Errors.Table (F).Sptr = Errors.Table (Cur).Sptr
1195          loop
1196             Check_Duplicate_Message (Cur, F);
1197             F := Errors.Table (F).Next;
1198          end loop;
1199
1200          Cur := Nxt;
1201       end loop;
1202
1203       --  Mark any messages suppressed by specific warnings as Deleted
1204
1205       Cur := First_Error_Msg;
1206       while Cur /= No_Error_Msg loop
1207          if not Errors.Table (Cur).Deleted
1208            and then Warning_Specifically_Suppressed
1209                      (Errors.Table (Cur).Sptr,
1210                       Errors.Table (Cur).Text)
1211          then
1212             Errors.Table (Cur).Deleted := True;
1213             Warnings_Detected := Warnings_Detected - 1;
1214          end if;
1215
1216          Cur := Errors.Table (Cur).Next;
1217       end loop;
1218
1219       Finalize_Called := True;
1220
1221       --  Check consistency of specific warnings (may add warnings). We only
1222       --  do this on the last call, after all possible warnings are posted.
1223
1224       if Last_Call then
1225          Validate_Specific_Warnings (Error_Msg'Access);
1226       end if;
1227    end Finalize;
1228
1229    ----------------
1230    -- First_Node --
1231    ----------------
1232
1233    function First_Node (C : Node_Id) return Node_Id is
1234       L        : constant Source_Ptr        := Sloc (Original_Node (C));
1235       Sfile    : constant Source_File_Index := Get_Source_File_Index (L);
1236       Earliest : Node_Id;
1237       Eloc     : Source_Ptr;
1238       Discard  : Traverse_Result;
1239
1240       pragma Warnings (Off, Discard);
1241
1242       function Test_Earlier (N : Node_Id) return Traverse_Result;
1243       --  Function applied to every node in the construct
1244
1245       function Search_Tree_First is new Traverse_Func (Test_Earlier);
1246       --  Create traversal function
1247
1248       ------------------
1249       -- Test_Earlier --
1250       ------------------
1251
1252       function Test_Earlier (N : Node_Id) return Traverse_Result is
1253          Loc : constant Source_Ptr := Sloc (Original_Node (N));
1254
1255       begin
1256          --  Check for earlier. The tests for being in the same file ensures
1257          --  against strange cases of foreign code somehow being present. We
1258          --  don't want wild placement of messages if that happens, so it is
1259          --  best to just ignore this situation.
1260
1261          if Loc < Eloc
1262            and then Get_Source_File_Index (Loc) = Sfile
1263          then
1264             Earliest := Original_Node (N);
1265             Eloc     := Loc;
1266          end if;
1267
1268          return OK_Orig;
1269       end Test_Earlier;
1270
1271    --  Start of processing for First_Node
1272
1273    begin
1274       Earliest := Original_Node (C);
1275       Eloc := Sloc (Earliest);
1276       Discard := Search_Tree_First (Original_Node (C));
1277       return Earliest;
1278    end First_Node;
1279
1280    ----------------
1281    -- First_Sloc --
1282    ----------------
1283
1284    function First_Sloc (N : Node_Id) return Source_Ptr is
1285       SI : constant Source_File_Index := Source_Index (Get_Source_Unit (N));
1286       SF : constant Source_Ptr        := Source_First (SI);
1287       F  : Node_Id;
1288       S  : Source_Ptr;
1289
1290    begin
1291       F := First_Node (N);
1292       S := Sloc (F);
1293
1294       --  The following circuit is a bit subtle. When we have parenthesized
1295       --  expressions, then the Sloc will not record the location of the
1296       --  paren, but we would like to post the flag on the paren. So what
1297       --  we do is to crawl up the tree from the First_Node, adjusting the
1298       --  Sloc value for any parentheses we know are present. Yes, we know
1299       --  this circuit is not 100% reliable (e.g. because we don't record
1300       --  all possible paren level values), but this is only for an error
1301       --  message so it is good enough.
1302
1303       Node_Loop : loop
1304          Paren_Loop : for J in 1 .. Paren_Count (F) loop
1305
1306             --  We don't look more than 12 characters behind the current
1307             --  location, and in any case not past the front of the source.
1308
1309             Search_Loop : for K in 1 .. 12 loop
1310                exit Search_Loop when S = SF;
1311
1312                if Source_Text (SI) (S - 1) = '(' then
1313                   S := S - 1;
1314                   exit Search_Loop;
1315
1316                elsif Source_Text (SI) (S - 1) <= ' ' then
1317                   S := S - 1;
1318
1319                else
1320                   exit Search_Loop;
1321                end if;
1322             end loop Search_Loop;
1323          end loop Paren_Loop;
1324
1325          exit Node_Loop when F = N;
1326          F := Parent (F);
1327          exit Node_Loop when Nkind (F) not in N_Subexpr;
1328       end loop Node_Loop;
1329
1330       return S;
1331    end First_Sloc;
1332
1333    ----------------
1334    -- Initialize --
1335    ----------------
1336
1337    procedure Initialize is
1338    begin
1339       Errors.Init;
1340       First_Error_Msg := No_Error_Msg;
1341       Last_Error_Msg := No_Error_Msg;
1342       Serious_Errors_Detected := 0;
1343       Total_Errors_Detected := 0;
1344       Warnings_Detected := 0;
1345       Cur_Msg := No_Error_Msg;
1346       List_Pragmas.Init;
1347
1348       --  Initialize warnings table, if all warnings are suppressed, supply
1349       --  an initial dummy entry covering all possible source locations.
1350
1351       Warnings.Init;
1352       Specific_Warnings.Init;
1353
1354       if Warning_Mode = Suppress then
1355          Warnings.Increment_Last;
1356          Warnings.Table (Warnings.Last).Start := Source_Ptr'First;
1357          Warnings.Table (Warnings.Last).Stop  := Source_Ptr'Last;
1358       end if;
1359    end Initialize;
1360
1361    -----------------
1362    -- No_Warnings --
1363    -----------------
1364
1365    function No_Warnings (N : Node_Or_Entity_Id) return Boolean is
1366    begin
1367       if Error_Posted (N) then
1368          return True;
1369
1370       elsif Nkind (N) in N_Entity and then Warnings_Off (N) then
1371          return True;
1372
1373       elsif Is_Entity_Name (N)
1374         and then Present (Entity (N))
1375         and then Warnings_Off (Entity (N))
1376       then
1377          return True;
1378
1379       else
1380          return False;
1381       end if;
1382    end No_Warnings;
1383
1384    -------------
1385    -- OK_Node --
1386    -------------
1387
1388    function OK_Node (N : Node_Id) return Boolean is
1389       K : constant Node_Kind := Nkind (N);
1390
1391    begin
1392       if Error_Posted (N) then
1393          return False;
1394
1395       elsif K in N_Has_Etype
1396         and then Present (Etype (N))
1397         and then Error_Posted (Etype (N))
1398       then
1399          return False;
1400
1401       elsif (K in N_Op
1402               or else K = N_Attribute_Reference
1403               or else K = N_Character_Literal
1404               or else K = N_Expanded_Name
1405               or else K = N_Identifier
1406               or else K = N_Operator_Symbol)
1407         and then Present (Entity (N))
1408         and then Error_Posted (Entity (N))
1409       then
1410          return False;
1411       else
1412          return True;
1413       end if;
1414    end OK_Node;
1415
1416    ---------------------
1417    -- Output_Messages --
1418    ---------------------
1419
1420    procedure Output_Messages is
1421       E        : Error_Msg_Id;
1422       Err_Flag : Boolean;
1423
1424       procedure Write_Error_Summary;
1425       --  Write error summary
1426
1427       procedure Write_Header (Sfile : Source_File_Index);
1428       --  Write header line (compiling or checking given file)
1429
1430       procedure Write_Max_Errors;
1431       --  Write message if max errors reached
1432
1433       -------------------------
1434       -- Write_Error_Summary --
1435       -------------------------
1436
1437       procedure Write_Error_Summary is
1438       begin
1439          --  Extra blank line if error messages or source listing were output
1440
1441          if Total_Errors_Detected + Warnings_Detected > 0
1442            or else Full_List
1443          then
1444             Write_Eol;
1445          end if;
1446
1447          --  Message giving number of lines read and number of errors detected.
1448          --  This normally goes to Standard_Output. The exception is when brief
1449          --  mode is not set, verbose mode (or full list mode) is set, and
1450          --  there are errors. In this case we send the message to standard
1451          --  error to make sure that *something* appears on standard error in
1452          --  an error situation.
1453
1454          --  Formerly, only the "# errors" suffix was sent to stderr, whereas
1455          --  "# lines:" appeared on stdout. This caused problems on VMS when
1456          --  the stdout buffer was flushed, giving an extra line feed after
1457          --  the prefix.
1458
1459          if Total_Errors_Detected + Warnings_Detected /= 0
1460            and then not Brief_Output
1461            and then (Verbose_Mode or Full_List)
1462          then
1463             Set_Standard_Error;
1464          end if;
1465
1466          --  Message giving total number of lines
1467
1468          Write_Str (" ");
1469          Write_Int (Num_Source_Lines (Main_Source_File));
1470
1471          if Num_Source_Lines (Main_Source_File) = 1 then
1472             Write_Str (" line: ");
1473          else
1474             Write_Str (" lines: ");
1475          end if;
1476
1477          if Total_Errors_Detected = 0 then
1478             Write_Str ("No errors");
1479
1480          elsif Total_Errors_Detected = 1 then
1481             Write_Str ("1 error");
1482
1483          else
1484             Write_Int (Total_Errors_Detected);
1485             Write_Str (" errors");
1486          end if;
1487
1488          if Warnings_Detected /= 0 then
1489             Write_Str (", ");
1490             Write_Int (Warnings_Detected);
1491             Write_Str (" warning");
1492
1493             if Warnings_Detected /= 1 then
1494                Write_Char ('s');
1495             end if;
1496
1497             if Warning_Mode = Treat_As_Error then
1498                Write_Str (" (treated as error");
1499
1500                if Warnings_Detected /= 1 then
1501                   Write_Char ('s');
1502                end if;
1503
1504                Write_Char (')');
1505             end if;
1506          end if;
1507
1508          Write_Eol;
1509          Set_Standard_Output;
1510       end Write_Error_Summary;
1511
1512       ------------------
1513       -- Write_Header --
1514       ------------------
1515
1516       procedure Write_Header (Sfile : Source_File_Index) is
1517       begin
1518          if Verbose_Mode or Full_List then
1519             if Original_Operating_Mode = Generate_Code then
1520                Write_Str ("Compiling: ");
1521             else
1522                Write_Str ("Checking: ");
1523             end if;
1524
1525             Write_Name (Full_File_Name (Sfile));
1526
1527             if not Debug_Flag_7 then
1528                Write_Str (" (source file time stamp: ");
1529                Write_Time_Stamp (Sfile);
1530                Write_Char (')');
1531             end if;
1532
1533             Write_Eol;
1534          end if;
1535       end Write_Header;
1536
1537       ----------------------
1538       -- Write_Max_Errors --
1539       ----------------------
1540
1541       procedure Write_Max_Errors is
1542       begin
1543          if Maximum_Errors /= 0
1544            and then Total_Errors_Detected + Warnings_Detected = Maximum_Errors
1545          then
1546             Set_Standard_Error;
1547             Write_Str ("fatal error: maximum errors reached");
1548             Write_Eol;
1549             Set_Standard_Output;
1550          end if;
1551       end Write_Max_Errors;
1552
1553    --  Start of processing for Output_Messages
1554
1555    begin
1556       --  Error if Finalize has not been called
1557
1558       if not Finalize_Called then
1559          raise Program_Error;
1560       end if;
1561
1562       --  Reset current error source file if the main unit has a pragma
1563       --  Source_Reference. This ensures outputting the proper name of
1564       --  the source file in this situation.
1565
1566       if Main_Source_File = No_Source_File
1567         or else Num_SRef_Pragmas (Main_Source_File) /= 0
1568       then
1569          Current_Error_Source_File := No_Source_File;
1570       end if;
1571
1572       --  Brief Error mode
1573
1574       if Brief_Output or (not Full_List and not Verbose_Mode) then
1575          Set_Standard_Error;
1576
1577          E := First_Error_Msg;
1578          while E /= No_Error_Msg loop
1579             if not Errors.Table (E).Deleted and then not Debug_Flag_KK then
1580                if Full_Path_Name_For_Brief_Errors then
1581                   Write_Name (Full_Ref_Name (Errors.Table (E).Sfile));
1582                else
1583                   Write_Name (Reference_Name (Errors.Table (E).Sfile));
1584                end if;
1585
1586                Write_Char (':');
1587                Write_Int (Int (Physical_To_Logical
1588                                 (Errors.Table (E).Line,
1589                                  Errors.Table (E).Sfile)));
1590                Write_Char (':');
1591
1592                if Errors.Table (E).Col < 10 then
1593                   Write_Char ('0');
1594                end if;
1595
1596                Write_Int (Int (Errors.Table (E).Col));
1597                Write_Str (": ");
1598                Output_Msg_Text (E);
1599                Write_Eol;
1600             end if;
1601
1602             E := Errors.Table (E).Next;
1603          end loop;
1604
1605          Set_Standard_Output;
1606       end if;
1607
1608       --  Full source listing case
1609
1610       if Full_List then
1611          List_Pragmas_Index := 1;
1612          List_Pragmas_Mode := True;
1613          E := First_Error_Msg;
1614
1615          --  Normal case, to stdout (copyright notice already output)
1616
1617          if Full_List_File_Name = null then
1618             if not Debug_Flag_7 then
1619                Write_Eol;
1620             end if;
1621
1622          --  Output to file
1623
1624          else
1625             Create_List_File_Access.all (Full_List_File_Name.all);
1626             Set_Special_Output (Write_List_Info_Access.all'Access);
1627
1628             --  Write copyright notice to file
1629
1630             if not Debug_Flag_7 then
1631                Write_Str ("GNAT ");
1632                Write_Str (Gnat_Version_String);
1633                Write_Eol;
1634                Write_Str ("Copyright 1992-" &
1635                           Current_Year &
1636                           ", Free Software Foundation, Inc.");
1637                Write_Eol;
1638             end if;
1639          end if;
1640
1641          --  First list extended main source file units with errors
1642
1643          --  Note: if debug flag d.m is set, only the main source is listed
1644
1645          for U in Main_Unit .. Last_Unit loop
1646             if In_Extended_Main_Source_Unit (Cunit_Entity (U))
1647               and then (U = Main_Unit or else not Debug_Flag_Dot_M)
1648             then
1649                declare
1650                   Sfile : constant Source_File_Index := Source_Index (U);
1651
1652                begin
1653                   Write_Eol;
1654                   Write_Header (Sfile);
1655                   Write_Eol;
1656
1657                   --  Normally, we don't want an "error messages from file"
1658                   --  message when listing the entire file, so we set the
1659                   --  current source file as the current error source file.
1660                   --  However, the old style of doing things was to list this
1661                   --  message if pragma Source_Reference is present, even for
1662                   --  the main unit. Since the purpose of the -gnatd.m switch
1663                   --  is to duplicate the old behavior, we skip the reset if
1664                   --  this debug flag is set.
1665
1666                   if not Debug_Flag_Dot_M then
1667                      Current_Error_Source_File := Sfile;
1668                   end if;
1669
1670                   for N in 1 .. Last_Source_Line (Sfile) loop
1671                      while E /= No_Error_Msg
1672                        and then Errors.Table (E).Deleted
1673                      loop
1674                         E := Errors.Table (E).Next;
1675                      end loop;
1676
1677                      Err_Flag :=
1678                        E /= No_Error_Msg
1679                          and then Errors.Table (E).Line = N
1680                          and then Errors.Table (E).Sfile = Sfile;
1681
1682                      Output_Source_Line (N, Sfile, Err_Flag);
1683
1684                      if Err_Flag then
1685                         Output_Error_Msgs (E);
1686
1687                         if not Debug_Flag_2 then
1688                            Write_Eol;
1689                         end if;
1690                      end if;
1691                   end loop;
1692                end;
1693             end if;
1694          end loop;
1695
1696          --  Then output errors, if any, for subsidiary units not in the
1697          --  main extended unit.
1698
1699          --  Note: if debug flag d.m set, include errors for any units other
1700          --  than the main unit in the extended source unit (e.g. spec and
1701          --  subunits for a body).
1702
1703          while E /= No_Error_Msg
1704            and then (not In_Extended_Main_Source_Unit (Errors.Table (E).Sptr)
1705                        or else
1706                         (Debug_Flag_Dot_M
1707                           and then Get_Source_Unit
1708                                      (Errors.Table (E).Sptr) /= Main_Unit))
1709          loop
1710             if Errors.Table (E).Deleted then
1711                E := Errors.Table (E).Next;
1712
1713             else
1714                Write_Eol;
1715                Output_Source_Line
1716                  (Errors.Table (E).Line, Errors.Table (E).Sfile, True);
1717                Output_Error_Msgs (E);
1718             end if;
1719          end loop;
1720
1721          --  If output to file, write extra copy of error summary to the
1722          --  output file, and then close it.
1723
1724          if Full_List_File_Name /= null then
1725             Write_Error_Summary;
1726             Write_Max_Errors;
1727             Close_List_File_Access.all;
1728             Cancel_Special_Output;
1729          end if;
1730       end if;
1731
1732       --  Verbose mode (error lines only with error flags). Normally this is
1733       --  ignored in full list mode, unless we are listing to a file, in which
1734       --  case we still generate -gnatv output to standard output.
1735
1736       if Verbose_Mode
1737         and then (not Full_List or else Full_List_File_Name /= null)
1738       then
1739          Write_Eol;
1740          Write_Header (Main_Source_File);
1741          E := First_Error_Msg;
1742
1743          --  Loop through error lines
1744
1745          while E /= No_Error_Msg loop
1746             if Errors.Table (E).Deleted then
1747                E := Errors.Table (E).Next;
1748             else
1749                Write_Eol;
1750                Output_Source_Line
1751                  (Errors.Table (E).Line, Errors.Table (E).Sfile, True);
1752                Output_Error_Msgs (E);
1753             end if;
1754          end loop;
1755       end if;
1756
1757       --  Output error summary if verbose or full list mode
1758
1759       if Verbose_Mode or else Full_List then
1760          Write_Error_Summary;
1761       end if;
1762
1763       Write_Max_Errors;
1764
1765       if Warning_Mode = Treat_As_Error then
1766          Total_Errors_Detected := Total_Errors_Detected + Warnings_Detected;
1767          Warnings_Detected := 0;
1768       end if;
1769    end Output_Messages;
1770
1771    ------------------------
1772    -- Output_Source_Line --
1773    ------------------------
1774
1775    procedure Output_Source_Line
1776      (L     : Physical_Line_Number;
1777       Sfile : Source_File_Index;
1778       Errs  : Boolean)
1779    is
1780       S : Source_Ptr;
1781       C : Character;
1782
1783       Line_Number_Output : Boolean := False;
1784       --  Set True once line number is output
1785
1786    begin
1787       if Sfile /= Current_Error_Source_File then
1788          Write_Str ("==============Error messages for ");
1789
1790          case Sinput.File_Type (Sfile) is
1791             when Sinput.Src =>
1792                Write_Str ("source");
1793
1794             when Sinput.Config =>
1795                Write_Str ("configuration pragmas");
1796
1797             when Sinput.Def =>
1798                Write_Str ("symbol definition");
1799
1800             when Sinput.Preproc =>
1801                Write_Str ("preprocessing data");
1802          end case;
1803
1804          Write_Str (" file: ");
1805          Write_Name (Full_File_Name (Sfile));
1806          Write_Eol;
1807
1808          if Num_SRef_Pragmas (Sfile) > 0 then
1809             Write_Str ("--------------Line numbers from file: ");
1810             Write_Name (Full_Ref_Name (Sfile));
1811             Write_Str (" (starting at line ");
1812             Write_Int (Int (First_Mapped_Line (Sfile)));
1813             Write_Char (')');
1814             Write_Eol;
1815          end if;
1816
1817          Current_Error_Source_File := Sfile;
1818       end if;
1819
1820       if Errs or List_Pragmas_Mode then
1821          Output_Line_Number (Physical_To_Logical (L, Sfile));
1822          Line_Number_Output := True;
1823       end if;
1824
1825       S := Line_Start (L, Sfile);
1826
1827       loop
1828          C := Source_Text (Sfile) (S);
1829          exit when C = ASCII.LF or else C = ASCII.CR or else C = EOF;
1830
1831          --  Deal with matching entry in List_Pragmas table
1832
1833          if Full_List
1834            and then List_Pragmas_Index <= List_Pragmas.Last
1835            and then S = List_Pragmas.Table (List_Pragmas_Index).Ploc
1836          then
1837             case List_Pragmas.Table (List_Pragmas_Index).Ptyp is
1838                when Page =>
1839                   Write_Char (C);
1840
1841                   --  Ignore if on line with errors so that error flags
1842                   --  get properly listed with the error line .
1843
1844                   if not Errs then
1845                      Write_Char (ASCII.FF);
1846                   end if;
1847
1848                when List_On =>
1849                   List_Pragmas_Mode := True;
1850
1851                   if not Line_Number_Output then
1852                      Output_Line_Number (Physical_To_Logical (L, Sfile));
1853                      Line_Number_Output := True;
1854                   end if;
1855
1856                   Write_Char (C);
1857
1858                when List_Off =>
1859                   Write_Char (C);
1860                   List_Pragmas_Mode := False;
1861             end case;
1862
1863             List_Pragmas_Index := List_Pragmas_Index + 1;
1864
1865          --  Normal case (no matching entry in List_Pragmas table)
1866
1867          else
1868             if Errs or List_Pragmas_Mode then
1869                Write_Char (C);
1870             end if;
1871          end if;
1872
1873          S := S + 1;
1874       end loop;
1875
1876       --  If we have output a source line, then add the line terminator, with
1877       --  training spaces preserved (so we output the line exactly as input).
1878
1879       if Line_Number_Output then
1880          Write_Eol_Keep_Blanks;
1881       end if;
1882    end Output_Source_Line;
1883
1884    -----------------------------
1885    -- Remove_Warning_Messages --
1886    -----------------------------
1887
1888    procedure Remove_Warning_Messages (N : Node_Id) is
1889
1890       function Check_For_Warning (N : Node_Id) return Traverse_Result;
1891       --  This function checks one node for a possible warning message
1892
1893       function Check_All_Warnings is new Traverse_Func (Check_For_Warning);
1894       --  This defines the traversal operation
1895
1896       -----------------------
1897       -- Check_For_Warning --
1898       -----------------------
1899
1900       function Check_For_Warning (N : Node_Id) return Traverse_Result is
1901          Loc : constant Source_Ptr := Sloc (N);
1902          E   : Error_Msg_Id;
1903
1904          function To_Be_Removed (E : Error_Msg_Id) return Boolean;
1905          --  Returns True for a message that is to be removed. Also adjusts
1906          --  warning count appropriately.
1907
1908          -------------------
1909          -- To_Be_Removed --
1910          -------------------
1911
1912          function To_Be_Removed (E : Error_Msg_Id) return Boolean is
1913          begin
1914             if E /= No_Error_Msg
1915
1916                --  Don't remove if location does not match
1917
1918                and then Errors.Table (E).Optr = Loc
1919
1920                --  Don't remove if not warning message. Note that we do not
1921                --  remove style messages here. They are warning messages but
1922                --  not ones we want removed in this context.
1923
1924                and then Errors.Table (E).Warn
1925
1926                --  Don't remove unconditional messages
1927
1928                and then not Errors.Table (E).Uncond
1929             then
1930                Warnings_Detected := Warnings_Detected - 1;
1931                return True;
1932
1933             --  No removal required
1934
1935             else
1936                return False;
1937             end if;
1938          end To_Be_Removed;
1939
1940       --  Start of processing for Check_For_Warnings
1941
1942       begin
1943          while To_Be_Removed (First_Error_Msg) loop
1944             First_Error_Msg := Errors.Table (First_Error_Msg).Next;
1945          end loop;
1946
1947          if First_Error_Msg = No_Error_Msg then
1948             Last_Error_Msg := No_Error_Msg;
1949          end if;
1950
1951          E := First_Error_Msg;
1952          while E /= No_Error_Msg loop
1953             while To_Be_Removed (Errors.Table (E).Next) loop
1954                Errors.Table (E).Next :=
1955                  Errors.Table (Errors.Table (E).Next).Next;
1956
1957                if Errors.Table (E).Next = No_Error_Msg then
1958                   Last_Error_Msg := E;
1959                end if;
1960             end loop;
1961
1962             E := Errors.Table (E).Next;
1963          end loop;
1964
1965          if Nkind (N) = N_Raise_Constraint_Error
1966            and then Original_Node (N) /= N
1967            and then No (Condition (N))
1968          then
1969             --  Warnings may have been posted on subexpressions of
1970             --  the original tree. We place the original node back
1971             --  on the tree to remove those warnings, whose sloc
1972             --  do not match those of any node in the current tree.
1973             --  Given that we are in unreachable code, this modification
1974             --  to the tree is harmless.
1975
1976             declare
1977                Status : Traverse_Result;
1978
1979             begin
1980                if Is_List_Member (N) then
1981                   Set_Condition (N, Original_Node (N));
1982                   Status := Check_All_Warnings (Condition (N));
1983                else
1984                   Rewrite (N, Original_Node (N));
1985                   Status := Check_All_Warnings (N);
1986                end if;
1987
1988                return Status;
1989             end;
1990
1991          else
1992             return OK;
1993          end if;
1994       end Check_For_Warning;
1995
1996    --  Start of processing for Remove_Warning_Messages
1997
1998    begin
1999       if Warnings_Detected /= 0 then
2000          declare
2001             Discard : Traverse_Result;
2002             pragma Warnings (Off, Discard);
2003
2004          begin
2005             Discard := Check_All_Warnings (N);
2006          end;
2007       end if;
2008    end Remove_Warning_Messages;
2009
2010    procedure Remove_Warning_Messages (L : List_Id) is
2011       Stat : Node_Id;
2012    begin
2013       if Is_Non_Empty_List (L) then
2014          Stat := First (L);
2015
2016          while Present (Stat) loop
2017             Remove_Warning_Messages (Stat);
2018             Next (Stat);
2019          end loop;
2020       end if;
2021    end Remove_Warning_Messages;
2022
2023    ---------------------------
2024    -- Set_Identifier_Casing --
2025    ---------------------------
2026
2027    procedure Set_Identifier_Casing
2028      (Identifier_Name : System.Address;
2029       File_Name       : System.Address)
2030    is
2031       type Big_String is array (Positive) of Character;
2032       type Big_String_Ptr is access all Big_String;
2033
2034       function To_Big_String_Ptr is new Unchecked_Conversion
2035         (System.Address, Big_String_Ptr);
2036
2037       Ident : constant Big_String_Ptr := To_Big_String_Ptr (Identifier_Name);
2038       File  : constant Big_String_Ptr := To_Big_String_Ptr (File_Name);
2039       Flen  : Natural;
2040
2041       Desired_Case : Casing_Type := Mixed_Case;
2042       --  Casing required for result. Default value of Mixed_Case is used if
2043       --  for some reason we cannot find the right file name in the table.
2044
2045    begin
2046       --  Get length of file name
2047
2048       Flen := 0;
2049       while File (Flen + 1) /= ASCII.NUL loop
2050          Flen := Flen + 1;
2051       end loop;
2052
2053       --  Loop through file names to find matching one. This is a bit slow,
2054       --  but we only do it in error situations so it is not so terrible.
2055       --  Note that if the loop does not exit, then the desired case will
2056       --  be left set to Mixed_Case, this can happen if the name was not
2057       --  in canonical form, and gets canonicalized on VMS. Possibly we
2058       --  could fix this by unconditinally canonicalizing these names ???
2059
2060       for J in 1 .. Last_Source_File loop
2061          Get_Name_String (Full_Debug_Name (J));
2062
2063          if Name_Len = Flen
2064            and then Name_Buffer (1 .. Name_Len) = String (File (1 .. Flen))
2065          then
2066             Desired_Case := Identifier_Casing (J);
2067             exit;
2068          end if;
2069       end loop;
2070
2071       --  Copy identifier as given to Name_Buffer
2072
2073       for J in Name_Buffer'Range loop
2074          Name_Buffer (J) := Ident (J);
2075
2076          if Name_Buffer (J) = ASCII.Nul then
2077             Name_Len := J - 1;
2078             exit;
2079          end if;
2080       end loop;
2081
2082       Set_Casing (Desired_Case);
2083    end Set_Identifier_Casing;
2084
2085    -----------------------
2086    -- Set_Ignore_Errors --
2087    -----------------------
2088
2089    procedure Set_Ignore_Errors (To : Boolean) is
2090    begin
2091       Errors_Must_Be_Ignored := To;
2092    end Set_Ignore_Errors;
2093
2094    ------------------------------
2095    -- Set_Msg_Insertion_Column --
2096    ------------------------------
2097
2098    procedure Set_Msg_Insertion_Column is
2099    begin
2100       if Style.RM_Column_Check then
2101          Set_Msg_Str (" in column ");
2102          Set_Msg_Int (Int (Error_Msg_Col) + 1);
2103       end if;
2104    end Set_Msg_Insertion_Column;
2105
2106    ----------------------------
2107    -- Set_Msg_Insertion_Node --
2108    ----------------------------
2109
2110    procedure Set_Msg_Insertion_Node is
2111       K : Node_Kind;
2112
2113    begin
2114       Suppress_Message :=
2115         Error_Msg_Node_1 = Error
2116           or else Error_Msg_Node_1 = Any_Type;
2117
2118       if Error_Msg_Node_1 = Empty then
2119          Set_Msg_Blank_Conditional;
2120          Set_Msg_Str ("<empty>");
2121
2122       elsif Error_Msg_Node_1 = Error then
2123          Set_Msg_Blank;
2124          Set_Msg_Str ("<error>");
2125
2126       elsif Error_Msg_Node_1 = Standard_Void_Type then
2127          Set_Msg_Blank;
2128          Set_Msg_Str ("procedure name");
2129
2130       else
2131          Set_Msg_Blank_Conditional;
2132
2133          --  Output name
2134
2135          K := Nkind (Error_Msg_Node_1);
2136
2137          --  If we have operator case, skip quotes since name of operator
2138          --  itself will supply the required quotations. An operator can be
2139          --  an applied use in an expression or an explicit operator symbol,
2140          --  or an identifier whose name indicates it is an operator.
2141
2142          if K in N_Op
2143            or else K = N_Operator_Symbol
2144            or else K = N_Defining_Operator_Symbol
2145            or else ((K = N_Identifier or else K = N_Defining_Identifier)
2146                        and then Is_Operator_Name (Chars (Error_Msg_Node_1)))
2147          then
2148             Set_Msg_Node (Error_Msg_Node_1);
2149
2150          --  Normal case, not an operator, surround with quotes
2151
2152          else
2153             Set_Msg_Quote;
2154             Set_Qualification (Error_Msg_Qual_Level, Error_Msg_Node_1);
2155             Set_Msg_Node (Error_Msg_Node_1);
2156             Set_Msg_Quote;
2157          end if;
2158       end if;
2159
2160       --  The following assignment ensures that a second ampersand insertion
2161       --  character will correspond to the Error_Msg_Node_2 parameter. We
2162       --  suppress possible validity checks in case operating in -gnatVa mode,
2163       --  and Error_Msg_Node_2 is not needed and has not been set.
2164
2165       declare
2166          pragma Suppress (Range_Check);
2167       begin
2168          Error_Msg_Node_1 := Error_Msg_Node_2;
2169       end;
2170    end Set_Msg_Insertion_Node;
2171
2172    --------------------------------------
2173    -- Set_Msg_Insertion_Type_Reference --
2174    --------------------------------------
2175
2176    procedure Set_Msg_Insertion_Type_Reference (Flag : Source_Ptr) is
2177       Ent : Entity_Id;
2178
2179    begin
2180       Set_Msg_Blank;
2181
2182       if Error_Msg_Node_1 = Standard_Void_Type then
2183          Set_Msg_Str ("package or procedure name");
2184          return;
2185
2186       elsif Error_Msg_Node_1 = Standard_Exception_Type then
2187          Set_Msg_Str ("exception name");
2188          return;
2189
2190       elsif     Error_Msg_Node_1 = Any_Access
2191         or else Error_Msg_Node_1 = Any_Array
2192         or else Error_Msg_Node_1 = Any_Boolean
2193         or else Error_Msg_Node_1 = Any_Character
2194         or else Error_Msg_Node_1 = Any_Composite
2195         or else Error_Msg_Node_1 = Any_Discrete
2196         or else Error_Msg_Node_1 = Any_Fixed
2197         or else Error_Msg_Node_1 = Any_Integer
2198         or else Error_Msg_Node_1 = Any_Modular
2199         or else Error_Msg_Node_1 = Any_Numeric
2200         or else Error_Msg_Node_1 = Any_Real
2201         or else Error_Msg_Node_1 = Any_Scalar
2202         or else Error_Msg_Node_1 = Any_String
2203       then
2204          Get_Unqualified_Decoded_Name_String (Chars (Error_Msg_Node_1));
2205          Set_Msg_Name_Buffer;
2206          return;
2207
2208       elsif Error_Msg_Node_1 = Universal_Real then
2209          Set_Msg_Str ("type universal real");
2210          return;
2211
2212       elsif Error_Msg_Node_1 = Universal_Integer then
2213          Set_Msg_Str ("type universal integer");
2214          return;
2215
2216       elsif Error_Msg_Node_1 = Universal_Fixed then
2217          Set_Msg_Str ("type universal fixed");
2218          return;
2219       end if;
2220
2221       --  Special case of anonymous array
2222
2223       if Nkind (Error_Msg_Node_1) in N_Entity
2224         and then Is_Array_Type (Error_Msg_Node_1)
2225         and then Present (Related_Array_Object (Error_Msg_Node_1))
2226       then
2227          Set_Msg_Str ("type of ");
2228          Set_Msg_Node (Related_Array_Object (Error_Msg_Node_1));
2229          Set_Msg_Str (" declared");
2230          Set_Msg_Insertion_Line_Number
2231            (Sloc (Related_Array_Object (Error_Msg_Node_1)), Flag);
2232          return;
2233       end if;
2234
2235       --  If we fall through, it is not a special case, so first output
2236       --  the name of the type, preceded by private for a private type
2237
2238       if Is_Private_Type (Error_Msg_Node_1) then
2239          Set_Msg_Str ("private type ");
2240       else
2241          Set_Msg_Str ("type ");
2242       end if;
2243
2244       Ent := Error_Msg_Node_1;
2245
2246       if Is_Internal_Name (Chars (Ent)) then
2247          Unwind_Internal_Type (Ent);
2248       end if;
2249
2250       --  Types in Standard are displayed as "Standard.name"
2251
2252       if Sloc (Ent) <= Standard_Location then
2253          Set_Msg_Quote;
2254          Set_Msg_Str ("Standard.");
2255          Set_Msg_Node (Ent);
2256          Add_Class;
2257          Set_Msg_Quote;
2258
2259       --  Types in other language defined units are displayed as
2260       --  "package-name.type-name"
2261
2262       elsif
2263         Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Ent)))
2264       then
2265          Get_Unqualified_Decoded_Name_String
2266            (Unit_Name (Get_Source_Unit (Ent)));
2267          Name_Len := Name_Len - 2;
2268          Set_Msg_Quote;
2269          Set_Casing (Mixed_Case);
2270          Set_Msg_Name_Buffer;
2271          Set_Msg_Char ('.');
2272          Set_Casing (Mixed_Case);
2273          Set_Msg_Node (Ent);
2274          Add_Class;
2275          Set_Msg_Quote;
2276
2277       --  All other types display as "type name" defined at line xxx
2278       --  possibly qualified if qualification is requested.
2279
2280       else
2281          Set_Msg_Quote;
2282          Set_Qualification (Error_Msg_Qual_Level, Ent);
2283          Set_Msg_Node (Ent);
2284          Add_Class;
2285
2286          --  If Ent is an anonymous subprogram type, there is no name
2287          --  to print, so remove enclosing quotes.
2288
2289          if Buffer_Ends_With ("""") then
2290             Buffer_Remove ("""");
2291          else
2292             Set_Msg_Quote;
2293          end if;
2294       end if;
2295
2296       --  If the original type did not come from a predefined
2297       --  file, add the location where the type was defined.
2298
2299       if Sloc (Error_Msg_Node_1) > Standard_Location
2300         and then
2301           not Is_Predefined_File_Name
2302                 (Unit_File_Name (Get_Source_Unit (Error_Msg_Node_1)))
2303       then
2304          Set_Msg_Str (" defined");
2305          Set_Msg_Insertion_Line_Number (Sloc (Error_Msg_Node_1), Flag);
2306
2307       --  If it did come from a predefined file, deal with the case where
2308       --  this was a file with a generic instantiation from elsewhere.
2309
2310       else
2311          if Sloc (Error_Msg_Node_1) > Standard_Location then
2312             declare
2313                Iloc : constant Source_Ptr :=
2314                         Instantiation_Location (Sloc (Error_Msg_Node_1));
2315
2316             begin
2317                if Iloc /= No_Location
2318                  and then not Suppress_Instance_Location
2319                then
2320                   Set_Msg_Str (" from instance");
2321                   Set_Msg_Insertion_Line_Number (Iloc, Flag);
2322                end if;
2323             end;
2324          end if;
2325       end if;
2326    end Set_Msg_Insertion_Type_Reference;
2327
2328    ---------------------------------
2329    -- Set_Msg_Insertion_Unit_Name --
2330    ---------------------------------
2331
2332    procedure Set_Msg_Insertion_Unit_Name (Suffix : Boolean := True) is
2333    begin
2334       if Error_Msg_Unit_1 = No_Unit_Name then
2335          null;
2336
2337       elsif Error_Msg_Unit_1 = Error_Unit_Name then
2338          Set_Msg_Blank;
2339          Set_Msg_Str ("<error>");
2340
2341       else
2342          Get_Unit_Name_String (Error_Msg_Unit_1, Suffix);
2343          Set_Msg_Blank;
2344          Set_Msg_Quote;
2345          Set_Msg_Name_Buffer;
2346          Set_Msg_Quote;
2347       end if;
2348
2349       --  The following assignment ensures that a second percent insertion
2350       --  character will correspond to the Error_Msg_Unit_2 parameter. We
2351       --  suppress possible validity checks in case operating in -gnatVa mode,
2352       --  and Error_Msg_Unit_2 is not needed and has not been set.
2353
2354       declare
2355          pragma Suppress (Range_Check);
2356       begin
2357          Error_Msg_Unit_1 := Error_Msg_Unit_2;
2358       end;
2359    end Set_Msg_Insertion_Unit_Name;
2360
2361    ------------------
2362    -- Set_Msg_Node --
2363    ------------------
2364
2365    procedure Set_Msg_Node (Node : Node_Id) is
2366       Ent : Entity_Id;
2367       Nam : Name_Id;
2368
2369    begin
2370       if Nkind (Node) = N_Designator then
2371          Set_Msg_Node (Name (Node));
2372          Set_Msg_Char ('.');
2373          Set_Msg_Node (Identifier (Node));
2374          return;
2375
2376       elsif Nkind (Node) = N_Defining_Program_Unit_Name then
2377          Set_Msg_Node (Name (Node));
2378          Set_Msg_Char ('.');
2379          Set_Msg_Node (Defining_Identifier (Node));
2380          return;
2381
2382       elsif Nkind (Node) = N_Selected_Component then
2383          Set_Msg_Node (Prefix (Node));
2384          Set_Msg_Char ('.');
2385          Set_Msg_Node (Selector_Name (Node));
2386          return;
2387       end if;
2388
2389       --  The only remaining possibilities are identifiers, defining
2390       --  identifiers, pragmas, and pragma argument associations, i.e.
2391       --  nodes that have a Chars field.
2392
2393       --  Internal names generally represent something gone wrong. An exception
2394       --  is the case of internal type names, where we try to find a reasonable
2395       --  external representation for the external name
2396
2397       if Is_Internal_Name (Chars (Node))
2398         and then
2399           ((Is_Entity_Name (Node)
2400                           and then Present (Entity (Node))
2401                           and then Is_Type (Entity (Node)))
2402               or else
2403            (Nkind (Node) = N_Defining_Identifier and then Is_Type (Node)))
2404       then
2405          if Nkind (Node) = N_Identifier then
2406             Ent := Entity (Node);
2407          else
2408             Ent := Node;
2409          end if;
2410
2411          --  If the type is the designated type of an access_to_subprogram,
2412          --  there is no name to provide in the call.
2413
2414          if Ekind (Ent) = E_Subprogram_Type then
2415             return;
2416          else
2417             Unwind_Internal_Type (Ent);
2418             Nam := Chars (Ent);
2419          end if;
2420
2421       else
2422          Nam := Chars (Node);
2423       end if;
2424
2425       --  At this stage, the name to output is in Nam
2426
2427       Get_Unqualified_Decoded_Name_String (Nam);
2428
2429       --  Remove trailing upper case letters from the name (useful for
2430       --  dealing with some cases of internal names.
2431
2432       while Name_Len > 1 and then Name_Buffer (Name_Len) in 'A' .. 'Z' loop
2433          Name_Len := Name_Len  - 1;
2434       end loop;
2435
2436       --  If we have any of the names from standard that start with the
2437       --  characters "any " (e.g. Any_Type), then kill the message since
2438       --  almost certainly it is a junk cascaded message.
2439
2440       if Name_Len > 4
2441         and then Name_Buffer (1 .. 4) = "any "
2442       then
2443          Kill_Message := True;
2444       end if;
2445
2446       --  Now we have to set the proper case. If we have a source location
2447       --  then do a check to see if the name in the source is the same name
2448       --  as the name in the Names table, except for possible differences
2449       --  in case, which is the case when we can copy from the source.
2450
2451       declare
2452          Src_Loc : constant Source_Ptr := Sloc (Error_Msg_Node_1);
2453          Sbuffer : Source_Buffer_Ptr;
2454          Ref_Ptr : Integer;
2455          Src_Ptr : Source_Ptr;
2456
2457       begin
2458          Ref_Ptr := 1;
2459          Src_Ptr := Src_Loc;
2460
2461          --  For standard locations, always use mixed case
2462
2463          if Src_Loc <= No_Location
2464            or else Sloc (Node) <= No_Location
2465          then
2466             Set_Casing (Mixed_Case);
2467
2468          else
2469             --  Determine if the reference we are dealing with corresponds
2470             --  to text at the point of the error reference. This will often
2471             --  be the case for simple identifier references, and is the case
2472             --  where we can copy the spelling from the source.
2473
2474             Sbuffer := Source_Text (Get_Source_File_Index (Src_Loc));
2475
2476             while Ref_Ptr <= Name_Len loop
2477                exit when
2478                  Fold_Lower (Sbuffer (Src_Ptr)) /=
2479                  Fold_Lower (Name_Buffer (Ref_Ptr));
2480                Ref_Ptr := Ref_Ptr + 1;
2481                Src_Ptr := Src_Ptr + 1;
2482             end loop;
2483
2484             --  If we get through the loop without a mismatch, then output
2485             --  the name the way it is spelled in the source program
2486
2487             if Ref_Ptr > Name_Len then
2488                Src_Ptr := Src_Loc;
2489
2490                for J in 1 .. Name_Len loop
2491                   Name_Buffer (J) := Sbuffer (Src_Ptr);
2492                   Src_Ptr := Src_Ptr + 1;
2493                end loop;
2494
2495             --  Otherwise set the casing using the default identifier casing
2496
2497             else
2498                Set_Casing (Identifier_Casing (Flag_Source), Mixed_Case);
2499             end if;
2500          end if;
2501       end;
2502
2503       Set_Msg_Name_Buffer;
2504       Add_Class;
2505    end Set_Msg_Node;
2506
2507    ------------------
2508    -- Set_Msg_Text --
2509    ------------------
2510
2511    procedure Set_Msg_Text (Text : String; Flag : Source_Ptr) is
2512       C : Character;   -- Current character
2513       P : Natural;     -- Current index;
2514
2515    begin
2516       Manual_Quote_Mode := False;
2517       Is_Unconditional_Msg := False;
2518       Msglen := 0;
2519       Flag_Source := Get_Source_File_Index (Flag);
2520       P := Text'First;
2521
2522       while P <= Text'Last loop
2523          C := Text (P);
2524          P := P + 1;
2525
2526          --  Check for insertion character or sequence
2527
2528          case C is
2529             when '%' =>
2530                if P <= Text'Last and then Text (P) = '%' then
2531                   P := P + 1;
2532                   Set_Msg_Insertion_Name_Literal;
2533                else
2534                   Set_Msg_Insertion_Name;
2535                end if;
2536
2537             when '$' =>
2538                if P <= Text'Last and then Text (P) = '$' then
2539                   P := P + 1;
2540                   Set_Msg_Insertion_Unit_Name (Suffix => False);
2541
2542                else
2543                   Set_Msg_Insertion_Unit_Name;
2544                end if;
2545
2546             when '{' =>
2547                Set_Msg_Insertion_File_Name;
2548
2549             when '}' =>
2550                Set_Msg_Insertion_Type_Reference (Flag);
2551
2552             when '*' =>
2553                Set_Msg_Insertion_Reserved_Name;
2554
2555             when '&' =>
2556                Set_Msg_Insertion_Node;
2557
2558             when '#' =>
2559                Set_Msg_Insertion_Line_Number (Error_Msg_Sloc, Flag);
2560
2561             when '\' =>
2562                Continuation := True;
2563
2564                if Text (P) = '\' then
2565                   Continuation_New_Line := True;
2566                   P := P + 1;
2567                end if;
2568
2569             when '@' =>
2570                Set_Msg_Insertion_Column;
2571
2572             when '>' =>
2573                Set_Msg_Insertion_Run_Time_Name;
2574
2575             when '^' =>
2576                Set_Msg_Insertion_Uint;
2577
2578             when '`' =>
2579                Manual_Quote_Mode := not Manual_Quote_Mode;
2580                Set_Msg_Char ('"');
2581
2582             when '!' =>
2583                Is_Unconditional_Msg := True;
2584
2585             when '?' =>
2586                null; -- already dealt with
2587
2588             when '<' =>
2589                null; -- already dealt with
2590
2591             when '|' =>
2592                null; -- already dealt with
2593
2594             when ''' =>
2595                Set_Msg_Char (Text (P));
2596                P := P + 1;
2597
2598             when '~' =>
2599                Set_Msg_Str (Error_Msg_String (1 .. Error_Msg_Strlen));
2600
2601             --  Upper case letter
2602
2603             when 'A' .. 'Z' =>
2604
2605                --  Start of reserved word if two or more
2606
2607                if P <= Text'Last and then Text (P) in 'A' .. 'Z' then
2608                   P := P - 1;
2609                   Set_Msg_Insertion_Reserved_Word (Text, P);
2610
2611                --  Single upper case letter is just inserted
2612
2613                else
2614                   Set_Msg_Char (C);
2615                end if;
2616
2617             --  Normal character with no special treatment
2618
2619             when others =>
2620                Set_Msg_Char (C);
2621          end case;
2622       end loop;
2623
2624       VMS_Convert;
2625    end Set_Msg_Text;
2626
2627    ----------------
2628    -- Set_Posted --
2629    ----------------
2630
2631    procedure Set_Posted (N : Node_Id) is
2632       P : Node_Id;
2633
2634    begin
2635       if Is_Serious_Error then
2636
2637          --  We always set Error_Posted on the node itself
2638
2639          Set_Error_Posted (N);
2640
2641          --  If it is a subexpression, then set Error_Posted on parents
2642          --  up to and including the first non-subexpression construct. This
2643          --  helps avoid cascaded error messages within a single expression.
2644
2645          P := N;
2646          loop
2647             P := Parent (P);
2648             exit when No (P);
2649             Set_Error_Posted (P);
2650             exit when Nkind (P) not in N_Subexpr;
2651          end loop;
2652
2653          --  A special check, if we just posted an error on an attribute
2654          --  definition clause, then also set the entity involved as posted.
2655          --  For example, this stops complaining about the alignment after
2656          --  complaining about the size, which is likely to be useless.
2657
2658          if Nkind (P) = N_Attribute_Definition_Clause then
2659             if Is_Entity_Name (Name (P)) then
2660                Set_Error_Posted (Entity (Name (P)));
2661             end if;
2662          end if;
2663       end if;
2664    end Set_Posted;
2665
2666    -----------------------
2667    -- Set_Qualification --
2668    -----------------------
2669
2670    procedure Set_Qualification (N : Nat; E : Entity_Id) is
2671    begin
2672       if N /= 0 and then Scope (E) /= Standard_Standard then
2673          Set_Qualification (N - 1, Scope (E));
2674          Set_Msg_Node (Scope (E));
2675          Set_Msg_Char ('.');
2676       end if;
2677    end Set_Qualification;
2678
2679    ------------------------
2680    -- Special_Msg_Delete --
2681    ------------------------
2682
2683    function Special_Msg_Delete
2684      (Msg : String;
2685       N   : Node_Or_Entity_Id;
2686       E   : Node_Or_Entity_Id) return Boolean
2687    is
2688    begin
2689       --  Never delete messages in -gnatdO mode
2690
2691       if Debug_Flag_OO then
2692          return False;
2693
2694       --  When an atomic object refers to a non-atomic type in the same
2695       --  scope, we implicitly make the type atomic. In the non-error
2696       --  case this is surely safe (and in fact prevents an error from
2697       --  occurring if the type is not atomic by default). But if the
2698       --  object cannot be made atomic, then we introduce an extra junk
2699       --  message by this manipulation, which we get rid of here.
2700
2701       --  We identify this case by the fact that it references a type for
2702       --  which Is_Atomic is set, but there is no Atomic pragma setting it.
2703
2704       elsif Msg = "atomic access to & cannot be guaranteed"
2705         and then Is_Type (E)
2706         and then Is_Atomic (E)
2707         and then No (Get_Rep_Pragma (E, Name_Atomic))
2708       then
2709          return True;
2710
2711       --  When a size is wrong for a frozen type there is no explicit
2712       --  size clause, and other errors have occurred, suppress the
2713       --  message, since it is likely that this size error is a cascaded
2714       --  result of other errors. The reason we eliminate unfrozen types
2715       --  is that messages issued before the freeze type are for sure OK.
2716
2717       elsif Msg = "size for& too small, minimum allowed is ^"
2718         and then Is_Frozen (E)
2719         and then Serious_Errors_Detected > 0
2720         and then Nkind (N) /= N_Component_Clause
2721         and then Nkind (Parent (N)) /= N_Component_Clause
2722         and then
2723           No (Get_Attribute_Definition_Clause (E, Attribute_Size))
2724         and then
2725           No (Get_Attribute_Definition_Clause (E, Attribute_Object_Size))
2726         and then
2727           No (Get_Attribute_Definition_Clause (E, Attribute_Value_Size))
2728       then
2729          return True;
2730
2731       --  All special tests complete, so go ahead with message
2732
2733       else
2734          return False;
2735       end if;
2736    end Special_Msg_Delete;
2737
2738    --------------------------
2739    -- Unwind_Internal_Type --
2740    --------------------------
2741
2742    procedure Unwind_Internal_Type (Ent : in out Entity_Id) is
2743       Derived : Boolean := False;
2744       Mchar   : Character;
2745       Old_Ent : Entity_Id;
2746
2747    begin
2748       --  Undo placement of a quote, since we will put it back later
2749
2750       Mchar := Msg_Buffer (Msglen);
2751
2752       if Mchar = '"' then
2753          Msglen := Msglen - 1;
2754       end if;
2755
2756       --  The loop here deals with recursive types, we are trying to
2757       --  find a related entity that is not an implicit type. Note
2758       --  that the check with Old_Ent stops us from getting "stuck".
2759       --  Also, we don't output the "type derived from" message more
2760       --  than once in the case where we climb up multiple levels.
2761
2762       loop
2763          Old_Ent := Ent;
2764
2765          --  Implicit access type, use directly designated type
2766          --  In Ada 2005, the designated type may be an anonymous access to
2767          --  subprogram, in which case we can only point to its definition.
2768
2769          if Is_Access_Type (Ent) then
2770             if Ekind (Ent) = E_Access_Subprogram_Type
2771               or else Ekind (Ent) = E_Anonymous_Access_Subprogram_Type
2772               or else Is_Access_Protected_Subprogram_Type (Ent)
2773             then
2774                Ent := Directly_Designated_Type (Ent);
2775
2776                if not Comes_From_Source (Ent) then
2777                   if Buffer_Ends_With ("type ") then
2778                      Buffer_Remove ("type ");
2779                   end if;
2780
2781                   Set_Msg_Str ("access to subprogram with profile ");
2782
2783                elsif Ekind (Ent) = E_Function then
2784                   Set_Msg_Str ("access to function ");
2785                else
2786                   Set_Msg_Str ("access to procedure ");
2787                end if;
2788                exit;
2789
2790             --  Type is access to object, named or anonymous
2791
2792             else
2793                Set_Msg_Str ("access to ");
2794                Ent := Directly_Designated_Type (Ent);
2795             end if;
2796
2797          --  Classwide type
2798
2799          elsif Is_Class_Wide_Type (Ent) then
2800             Class_Flag := True;
2801             Ent := Root_Type (Ent);
2802
2803          --  Use base type if this is a subtype
2804
2805          elsif Ent /= Base_Type (Ent) then
2806             Buffer_Remove ("type ");
2807
2808             --  Avoid duplication "subtype of subtype of", and also replace
2809             --  "derived from subtype of" simply by "derived from"
2810
2811             if not Buffer_Ends_With ("subtype of ")
2812               and then not Buffer_Ends_With ("derived from ")
2813             then
2814                Set_Msg_Str ("subtype of ");
2815             end if;
2816
2817             Ent := Base_Type (Ent);
2818
2819          --  If this is a base type with a first named subtype, use the
2820          --  first named subtype instead. This is not quite accurate in
2821          --  all cases, but it makes too much noise to be accurate and
2822          --  add 'Base in all cases. Note that we only do this is the
2823          --  first named subtype is not itself an internal name. This
2824          --  avoids the obvious loop (subtype->basetype->subtype) which
2825          --  would otherwise occur!)
2826
2827          elsif Present (Freeze_Node (Ent))
2828            and then Present (First_Subtype_Link (Freeze_Node (Ent)))
2829            and then
2830              not Is_Internal_Name
2831                    (Chars (First_Subtype_Link (Freeze_Node (Ent))))
2832          then
2833             Ent := First_Subtype_Link (Freeze_Node (Ent));
2834
2835          --  Otherwise use root type
2836
2837          else
2838             if not Derived then
2839                Buffer_Remove ("type ");
2840
2841                --  Test for "subtype of type derived from" which seems
2842                --  excessive and is replaced by simply "type derived from"
2843
2844                Buffer_Remove ("subtype of");
2845
2846                --  Avoid duplication "type derived from type derived from"
2847
2848                if not Buffer_Ends_With ("type derived from ") then
2849                   Set_Msg_Str ("type derived from ");
2850                end if;
2851
2852                Derived := True;
2853             end if;
2854
2855             Ent := Etype (Ent);
2856          end if;
2857
2858          --  If we are stuck in a loop, get out and settle for the internal
2859          --  name after all. In this case we set to kill the message if it
2860          --  is not the first error message (we really try hard not to show
2861          --  the dirty laundry of the implementation to the poor user!)
2862
2863          if Ent = Old_Ent then
2864             Kill_Message := True;
2865             exit;
2866          end if;
2867
2868          --  Get out if we finally found a non-internal name to use
2869
2870          exit when not Is_Internal_Name (Chars (Ent));
2871       end loop;
2872
2873       if Mchar = '"' then
2874          Set_Msg_Char ('"');
2875       end if;
2876    end Unwind_Internal_Type;
2877
2878    -----------------
2879    -- VMS_Convert --
2880    -----------------
2881
2882    procedure VMS_Convert is
2883       P : Natural;
2884       L : Natural;
2885       N : Natural;
2886
2887    begin
2888       if not OpenVMS then
2889          return;
2890       end if;
2891
2892       P := Msg_Buffer'First;
2893       loop
2894          if P >= Msglen then
2895             return;
2896          end if;
2897
2898          if Msg_Buffer (P) = '-' then
2899             for G in Gnames'Range loop
2900                L := Gnames (G)'Length;
2901
2902                --  See if we have "-ggg switch", where ggg is Gnames entry
2903
2904                if P + L + 7 <= Msglen
2905                  and then Msg_Buffer (P + 1 .. P + L) = Gnames (G).all
2906                  and then Msg_Buffer (P + L + 1 .. P + L + 7) = " switch"
2907                then
2908                   --  Replace by "/vvv qualifier", where vvv is Vnames entry
2909
2910                   N := Vnames (G)'Length;
2911                   Msg_Buffer (P + N + 11 .. Msglen + N - L + 3) :=
2912                     Msg_Buffer (P + L + 8 .. Msglen);
2913                   Msg_Buffer (P) := '/';
2914                   Msg_Buffer (P + 1 .. P + N) := Vnames (G).all;
2915                   Msg_Buffer (P + N + 1 .. P + N + 10) := " qualifier";
2916                   P := P + N + 10;
2917                   Msglen := Msglen + N - L + 3;
2918                   exit;
2919                end if;
2920             end loop;
2921          end if;
2922
2923          P := P + 1;
2924       end loop;
2925    end VMS_Convert;
2926
2927 end Errout;