FSF GCC merge 02/23/03
[official-gcc.git] / gcc / ada / errout.ads
blobbd9f398a5bb6cc12a528fa67e263d6f00e9a5b86
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E R R O U T --
6 -- --
7 -- S p e c --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 -- --
26 ------------------------------------------------------------------------------
28 -- This package contains the routines to output error messages. They
29 -- are basically system independent, however in some environments, e.g.
30 -- when the parser is embedded into an editor, it may be appropriate
31 -- to replace the implementation of this package.
33 with Table;
34 with Types; use Types;
35 with Uintp; use Uintp;
37 package Errout is
39 Serious_Errors_Detected : Nat;
40 -- This is a count of errors that are serious enough to stop expansion,
41 -- and hence to prevent generation of an object file even if the
42 -- switch -gnatQ is set.
44 Total_Errors_Detected : Nat;
45 -- Number of errors detected so far. Includes count of serious errors
46 -- and non-serious errors, so this value is always greater than or
47 -- equal to the Serious_Errors_Detected value.
49 Warnings_Detected : Nat;
50 -- Number of warnings detected
52 type Compiler_State_Type is (Parsing, Analyzing);
53 Compiler_State : Compiler_State_Type;
54 -- Indicates current state of compilation. This is put in the Errout
55 -- spec because it affects the action of the error message handling.
56 -- In particular, an attempt is made by Errout to suppress cascaded
57 -- error messages in Parsing mode, but not in the other modes.
59 Current_Error_Source_File : Source_File_Index;
60 -- Id of current messages. Used to post file name when unit changes. This
61 -- is initialized to Main_Source_File at the start of a compilation, which
62 -- means that no file names will be output unless there are errors in units
63 -- other than the main unit. However, if the main unit has a pragma
64 -- Source_Reference line, then this is initialized to No_Source_File,
65 -- to force an initial reference to the real source file name.
67 Raise_Exception_On_Error : Nat := 0;
68 -- If this value is non-zero, then any attempt to generate an error
69 -- message raises the exception Error_Msg_Exception, and the error
70 -- message is not output. This is used for defending against junk
71 -- resulting from illegalities, and also for substitution of more
72 -- appropriate error messages from higher semantic levels. It is
73 -- a counter so that the increment/decrement protocol nests neatly.
75 Error_Msg_Exception : exception;
76 -- Exception raised if Raise_Exception_On_Error is true
78 -----------------------------------
79 -- Suppression of Error Messages --
80 -----------------------------------
82 -- In an effort to reduce the impact of redundant error messages, the
83 -- error output routines in this package normally suppress certain
84 -- classes of messages as follows:
86 -- 1. Identical messages placed at the same point in the text. Such
87 -- duplicate error message result for example from rescanning
88 -- sections of the text that contain lexical errors. Only one of
89 -- such a set of duplicate messages is output, and the rest are
90 -- suppressed.
92 -- 2. If more than one parser message is generated for a single source
93 -- line, then only the first message is output, the remaining
94 -- messages on the same line are suppressed.
96 -- 3. If a message is posted on a node for which a message has been
97 -- previously posted, then only the first message is retained. The
98 -- Error_Posted flag is used to detect such multiple postings. Note
99 -- that this only applies to semantic messages, since otherwise
100 -- for parser messages, this would be a special case of case 2.
102 -- 4. If a message is posted on a node whose Etype or Entity
103 -- fields reference entities on which an error message has
104 -- already been placed, as indicated by the Error_Posted flag
105 -- being set on these entities, then the message is suppressed.
107 -- 5. If a message attempts to insert an Error node, or a direct
108 -- reference to the Any_Type node, then the message is suppressed.
110 -- This normal suppression action may be overridden in cases 2-5 (but not
111 -- in case 1) by setting All_Errors mode, or by setting the special
112 -- unconditional message insertion character (!) at the end of the message
113 -- text as described below.
115 ---------------------------------------------------------
116 -- Error Message Text and Message Insertion Characters --
117 ---------------------------------------------------------
119 -- Error message text strings are composed of lower case letters, digits
120 -- and the special characters space, comma, period, colon and semicolon,
121 -- apostrophe and parentheses. Special insertion characters can also
122 -- appear which cause the error message circuit to modify the given
123 -- string as follows:
125 -- Insertion character % (Percent: insert name from Names table)
126 -- The character % is replaced by the text for the name specified by
127 -- the Name_Id value stored in Error_Msg_Name_1. A blank precedes
128 -- the name if it is preceded by a non-blank character other than a
129 -- left parenthesis. The name is enclosed in quotes unless manual
130 -- quotation mode is set. If the Name_Id is set to No_Name, then
131 -- no insertion occurs; if the Name_Id is set to Error_Name, then
132 -- the string <error> is inserted. A second and third % may appear
133 -- in a single message, similarly replaced by the names which are
134 -- specified by the Name_Id values stored in Error_Msg_Name_2 and
135 -- Error_Msg_Name_3. The names are decoded and cased according to
136 -- the current identifier casing mode.
138 -- Insertion character $ (Dollar: insert unit name from Names table)
139 -- The character $ is treated similarly to %, except that the name
140 -- is obtained from the Unit_Name_Type value in Error_Msg_Unit_1
141 -- and Error_Msg_Unit_2, as provided by Get_Unit_Name_String in
142 -- package Uname. Note that this name includes the postfix (spec)
143 -- or (body) strings. If this postfix is not required, use the
144 -- normal % insertion for the unit name.
146 -- Insertion character { (Left brace: insert literally from names table)
147 -- The character { is treated similarly to %, except that the
148 -- name is output literally as stored in the names table without
149 -- adjusting the casing. This can be used for file names and in
150 -- other situations where the name string is to be output unchanged.
152 -- Insertion character * (Asterisk, insert reserved word name)
153 -- The insertion character * is treated exactly like % except that
154 -- the resulting name is cased according to the default conventions
155 -- for reserved words (see package Scans).
157 -- Insertion character & (Ampersand: insert name from node)
158 -- The insertion character & is treated similarly to %, except that
159 -- the name is taken from the Chars field of the given node, and may
160 -- refer to a child unit name, or a selected component. The casing
161 -- is, if possible, taken from the original source reference, which
162 -- is obtained from the Sloc field of the given node or nodes. If no
163 -- Sloc is available (happens e.g. for nodes in package Standard),
164 -- then the default case (see Scans spec) is used. The nodes to be
165 -- used are stored in Error_Msg_Node_1, Error_Msg_Node_2. No insertion
166 -- occurs for the Empty node, and the Error node results in the
167 -- insertion of the characters <error>. In addition, if the special
168 -- global variable Error_Msg_Qual_Level is non-zero, then the
169 -- reference will include up to the given number of levels of
170 -- qualification, using the scope chain.
172 -- Insertion character # (Pound: insert line number reference)
173 -- The character # is replaced by the string indicating the source
174 -- position stored in Error_Msg_Sloc. There are three cases:
176 -- for package Standard: in package Standard
177 -- for locations in current file: at line nnn:ccc
178 -- for locations in other files: at filename:nnn:ccc
180 -- By convention, the # insertion character is only used at the end
181 -- of an error message, so the above strings only appear as the last
182 -- characters of an error message.
184 -- Insertion character } (Right brace: insert type reference)
185 -- The character } is replaced by a string describing the type
186 -- referenced by the entity whose Id is stored in Error_Msg_Node_1.
187 -- the string gives the name or description of the type, and also
188 -- where appropriate the location of its declaration. Special
189 -- cases like "some integer type" are handled appropriately. Only
190 -- one } is allowed in a message, since there is not enough room
191 -- for two (the insertion can be quite long, including a file name)
192 -- In addition, if the special global variable Error_Msg_Qual_Level
193 -- is non-zero, then the reference will include up to the given
194 -- number of levels of qualification, using the scope chain.
196 -- Insertion character @ (At: insert column number reference)
197 -- The character @ is replaced by null if the RM_Column_Check mode is
198 -- off (False). If the switch is on (True), then @ is replaced by the
199 -- text string " in column nnn" where nnn is the decimal representation
200 -- of the column number stored in Error_Msg_Col plus one (the plus one
201 -- is because the number is stored 0-origin and displayed 1-origin).
203 -- Insertion character ^ (Carret: insert integer value)
204 -- The character ^ is replaced by the decimal conversion of the Uint
205 -- value stored in Error_Msg_Uint_1, with a possible leading minus.
206 -- A second ^ may occur in the message, in which case it is replaced
207 -- by the decimal conversion of the Uint value in Error_Msg_Uint_2.
209 -- Insertion character ! (Exclamation: unconditional message)
210 -- The character ! appearing as the last character of a message makes
211 -- the message unconditional which means that it is output even if it
212 -- would normally be suppressed. See section above for a description
213 -- of the cases in which messages are normally suppressed.
215 -- Insertion character ? (Question: warning message)
216 -- The character ? appearing anywhere in a message makes the message
217 -- a warning instead of a normal error message, and the text of the
218 -- message will be preceded by "Warning:" instead of "Error:" The
219 -- handling of warnings if further controlled by the Warning_Mode
220 -- option (-w switch), see package Opt for further details, and
221 -- also by the current setting from pragma Warnings. This pragma
222 -- applies only to warnings issued from the semantic phase (not
223 -- the parser), but currently all relevant warnings are posted
224 -- by the semantic phase anyway. Messages starting with (style)
225 -- are also treated as warning messages.
227 -- Insertion character A-Z (Upper case letter: Ada reserved word)
228 -- If two or more upper case letters appear in the message, they are
229 -- taken as an Ada reserved word, and are converted to the default
230 -- case for reserved words (see Scans package spec). Surrounding
231 -- quotes are added unless manual quotation mode is currently set.
233 -- Insertion character ` (Backquote: set manual quotation mode)
234 -- The backquote character always appears in pairs. Each backquote
235 -- of the pair is replaced by a double quote character. In addition,
236 -- Any reserved keywords, or name insertions between these backquotes
237 -- are not surrounded by the usual automatic double quotes. See the
238 -- section below on manual quotation mode for further details.
240 -- Insertion character ' (Quote: literal character)
241 -- Precedes a character which is placed literally into the message.
242 -- Used to insert characters into messages that are one of the
243 -- insertion characters defined here.
245 -- Insertion character \ (Backslash: continuation message)
246 -- Indicates that the message is a continuation of a message
247 -- previously posted. This is used to ensure that such groups
248 -- of messages are treated as a unit. The \ character must be
249 -- the first character of the message text.
251 -- Insertion character | (vertical bar, non-serious error)
252 -- By default, error messages (other than warning messages) are
253 -- considered to be fatal error messages which prevent expansion
254 -- or generation of code in the presence of the -gnatQ switch.
255 -- If the insertion character | appears, the message is considered
256 -- to be non-serious, and does not cause Serious_Errors_Detected
257 -- to be incremented (so expansion is not prevented by such a msg).
259 -----------------------------------------------------
260 -- Global Values Used for Error Message Insertions --
261 -----------------------------------------------------
263 -- The following global variables are essentially additional parameters
264 -- passed to the error message routine for insertion sequences described
265 -- above. The reason these are passed globally is that the insertion
266 -- mechanism is essentially an untyped one in which the appropriate
267 -- variables are set dependingon the specific insertion characters used.
269 Error_Msg_Col : Column_Number;
270 -- Column for @ insertion character in message
272 Error_Msg_Uint_1 : Uint;
273 Error_Msg_Uint_2 : Uint;
274 -- Uint values for ^ insertion characters in message
276 Error_Msg_Sloc : Source_Ptr;
277 -- Source location for # insertion character in message
279 Error_Msg_Name_1 : Name_Id;
280 Error_Msg_Name_2 : Name_Id;
281 Error_Msg_Name_3 : Name_Id;
282 -- Name_Id values for % insertion characters in message
284 Error_Msg_Unit_1 : Name_Id;
285 Error_Msg_Unit_2 : Name_Id;
286 -- Name_Id values for $ insertion characters in message
288 Error_Msg_Node_1 : Node_Id;
289 Error_Msg_Node_2 : Node_Id;
290 -- Node_Id values for & insertion characters in message
292 Error_Msg_Qual_Level : Int := 0;
293 -- Number of levels of qualification required for type name (see the
294 -- description of the } insertion character. Note that this value does
295 -- note get reset by any Error_Msg call, so the caller is responsible
296 -- for resetting it.
298 Warn_On_Instance : Boolean := False;
299 -- Normally if a warning is generated in a generic template from the
300 -- analysis of the template, then the warning really belongs in the
301 -- template, and the default value of False for this Boolean achieves
302 -- that effect. If Warn_On_Instance is set True, then the warnings are
303 -- generated on the instantiation (referring to the template) rather
304 -- than on the template itself.
306 -----------------------------------------------------
307 -- Format of Messages and Manual Quotation Control --
308 -----------------------------------------------------
310 -- Messages are generally all in lower case, except for inserted names
311 -- and appear in one of the following three forms:
313 -- error: text
314 -- warning: text
316 -- The prefixes error and warning are supplied automatically (depending
317 -- on the use of the ? insertion character), and the call to the error
318 -- message routine supplies the text. The "error: " prefix is omitted
319 -- in brief error message formats.
321 -- Reserved Ada keywords in the message are in the default keyword case
322 -- (determined from the given source program), surrounded by quotation
323 -- marks. This is achieved by spelling the reserved word in upper case
324 -- letters, which is recognized as a request for insertion of quotation
325 -- marks by the error text processor. Thus for example:
327 -- Error_Msg_AP ("IS expected");
329 -- would result in the output of one of the following:
331 -- error: "is" expected
332 -- error: "IS" expected
333 -- error: "Is" expected
335 -- the choice between these being made by looking at the casing convention
336 -- used for keywords (actually the first compilation unit keyword) in the
337 -- source file.
339 -- In the case of names, the default mode for the error text processor
340 -- is to surround the name by quotation marks automatically. The case
341 -- used for the identifier names is taken from the source program where
342 -- possible, and otherwise is the default casing convention taken from
343 -- the source file usage.
345 -- In some cases, better control over the placement of quote marks is
346 -- required. This is achieved using manual quotation mode. In this mode,
347 -- one or more insertion sequences is surrounded by backquote characters.
348 -- The backquote characters are output as double quote marks, and normal
349 -- automatic insertion of quotes is suppressed between the double quotes.
350 -- For example:
352 -- Error_Msg_AP ("`END &;` expected");
354 -- generates a message like
356 -- error: "end Open_Scope;" expected
358 -- where the node specifying the name Open_Scope has been stored in
359 -- Error_Msg_Node_1 prior to the call. The great majority of error
360 -- messages operates in normal quotation mode.
362 -- Note: the normal automatic insertion of spaces before insertion
363 -- sequences (such as those that come from & and %) is suppressed in
364 -- manual quotation mode, so blanks, if needed as in the above example,
365 -- must be explicitly present.
367 ----------------------------
368 -- Message ID Definitions --
369 ----------------------------
371 type Error_Msg_Id is new Int;
372 -- A type used to represent specific error messages. Used by the clients
373 -- of this package only in the context of the Get_Error_Id and
374 -- Change_Error_Text subprograms.
376 No_Error_Msg : constant Error_Msg_Id := 0;
377 -- A constant which is different from any value returned by Get_Error_Id.
378 -- Typically used by a client to indicate absense of a saved Id value.
380 function Get_Msg_Id return Error_Msg_Id;
381 -- Returns the Id of the message most recently posted using one of the
382 -- Error_Msg routines.
384 function Get_Location (E : Error_Msg_Id) return Source_Ptr;
385 -- Returns the flag location of the error message with the given id E.
387 ------------------------
388 -- List Pragmas Table --
389 ------------------------
391 -- When a pragma Page or pragma List is encountered by the parser, an
392 -- entry is made in the following table. This table is then used to
393 -- control the full listing if one is being generated. Note that the
394 -- reason we do the processing in the parser is so that we get proper
395 -- listing control even in syntax check only mode.
397 type List_Pragma_Type is (List_On, List_Off, Page);
399 type List_Pragma_Record is record
400 Ptyp : List_Pragma_Type;
401 Ploc : Source_Ptr;
402 end record;
404 -- Note: Ploc points to the terminating semicolon in the List_Off and
405 -- Page cases, and to the pragma keyword for List_On. In the case of
406 -- a pragma List_Off, a List_On entry is also made in the table,
407 -- pointing to the pragma keyword. This ensures that, as required,
408 -- a List (Off) pragma is listed even in list off mode.
410 package List_Pragmas is new Table.Table (
411 Table_Component_Type => List_Pragma_Record,
412 Table_Index_Type => Int,
413 Table_Low_Bound => 1,
414 Table_Initial => 50,
415 Table_Increment => 200,
416 Table_Name => "List_Pragmas");
418 ---------------------------
419 -- Ignore_Errors Feature --
420 ---------------------------
422 -- In certain cases, notably for optional subunits, the compiler operates
423 -- in a mode where errors are to be ignored, and the whole unit is to be
424 -- considered as not present. To implement this we provide the following
425 -- flag to enable special handling, where error messages are suppressed,
426 -- but the Fatal_Error flag will still be set in the normal manner.
428 Ignore_Errors_Enable : Nat := 0;
429 -- Triggering switch. If non-zero, then ignore errors mode is activated.
430 -- This is a counter to allow convenient nesting of enable/disable.
432 ------------------------------
433 -- Error Output Subprograms --
434 ------------------------------
436 procedure Initialize;
437 -- Initializes for output of error messages. Must be called for each
438 -- source file before using any of the other routines in the package.
440 procedure Finalize;
441 -- Finalize processing of error messages for one file and output message
442 -- indicating the number of detected errors.
444 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr);
445 -- Output a message at specified location. Can be called from the parser
446 -- or the semantic analyzer.
448 procedure Error_Msg_S (Msg : String);
449 -- Output a message at current scan pointer location. This routine can be
450 -- called only from the parser, since it references Scan_Ptr.
452 procedure Error_Msg_AP (Msg : String);
453 -- Output a message just after the previous token. This routine can be
454 -- called only from the parser, since it references Prev_Token_Ptr.
456 procedure Error_Msg_BC (Msg : String);
457 -- Output a message just before the current token. Note that the important
458 -- difference between this and the previous routine is that the BC case
459 -- posts a flag on the current line, whereas AP can post a flag at the
460 -- end of the preceding line. This routine can be called only from the
461 -- parser, since it references Token_Ptr.
463 procedure Error_Msg_SC (Msg : String);
464 -- Output a message at the start of the current token, unless we are at
465 -- the end of file, in which case we always output the message after the
466 -- last real token in the file. This routine can be called only from the
467 -- parser, since it references Token_Ptr.
469 procedure Error_Msg_SP (Msg : String);
470 -- Output a message at the start of the previous token. This routine can
471 -- be called only from the parser, since it references Prev_Token_Ptr.
473 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
474 -- Output a message at the Sloc of the given node. This routine can be
475 -- called from the parser or the semantic analyzer, although the call
476 -- from the latter is much more common (and is the most usual way of
477 -- generating error messages from the analyzer). The message text may
478 -- contain a single & insertion, which will reference the given node.
479 -- The message is suppressed if the node N already has a message posted,
480 -- or if it is a warning and warnings and N is an entity node for which
481 -- warnings are suppressed.
483 procedure Error_Msg_NE
484 (Msg : String;
485 N : Node_Or_Entity_Id;
486 E : Node_Or_Entity_Id);
487 -- Output a message at the Sloc of the given node N, with an insertion of
488 -- the name from the given entity node E. This is used by the semantic
489 -- routines, where this is a common error message situation. The Msg
490 -- text will contain a & or } as usual to mark the insertion point.
491 -- This routine can be called from the parser or the analyzer.
493 procedure Error_Msg_NEL
494 (Msg : String;
495 N : Node_Or_Entity_Id;
496 E : Node_Or_Entity_Id;
497 Flag_Location : Source_Ptr);
498 -- Exactly the same as Error_Msg_NE, except that the flag is placed at
499 -- the specified Flag_Location instead of at Sloc (N).
501 procedure Change_Error_Text (Error_Id : Error_Msg_Id; New_Msg : String);
502 -- The error message text of the message identified by Id is replaced by
503 -- the given text. This text may contain insertion characters in the
504 -- usual manner, and need not be the same length as the original text.
506 procedure Purge_Messages (From : Source_Ptr; To : Source_Ptr);
507 -- All error messages whose location is in the range From .. To (not
508 -- including the end points) will be deleted from the error listing.
510 procedure Remove_Warning_Messages (N : Node_Id);
511 -- Remove any warning messages corresponding to the Sloc of N or any
512 -- of its descendent nodes. No effect if no such warnings.
514 procedure Set_Warnings_Mode_Off (Loc : Source_Ptr);
515 -- Called in response to a pragma Warnings (Off) to record the source
516 -- location from which warnings are to be turned off.
518 procedure Set_Warnings_Mode_On (Loc : Source_Ptr);
519 -- Called in response to a pragma Warnings (On) to record the source
520 -- location from which warnings are to be turned back on.
522 function Compilation_Errors return Boolean;
523 -- Returns true if errors have been detected, or warnings in -gnatwe
524 -- (treat warnings as errors) mode.
526 procedure dmsg (Id : Error_Msg_Id);
527 -- Debugging routine to dump an error message
529 end Errout;