* MAINTAINERS: (Write After Approval): Add myself.
[official-gcc.git] / gcc / ada / par.adb
blobe7d8350261a8c3a390783735e088f40050ae4aaa
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2002 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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Csets; use Csets;
31 with Debug; use Debug;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Fname; use Fname;
35 with Lib; use Lib;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Output; use Output;
41 with Scans; use Scans;
42 with Scn; use Scn;
43 with Sinput; use Sinput;
44 with Sinput.L; use Sinput.L;
45 with Sinfo; use Sinfo;
46 with Snames; use Snames;
47 with Style;
48 with Table;
50 function Par (Configuration_Pragmas : Boolean) return List_Id is
52 Num_Library_Units : Natural := 0;
53 -- Count number of units parsed (relevant only in syntax check only mode,
54 -- since in semantics check mode only a single unit is permitted anyway)
56 Unit_Node : Node_Id;
57 -- Stores compilation unit node for current unit
59 Save_Config_Switches : Config_Switches_Type;
60 -- Variable used to save values of config switches while we parse the
61 -- new unit, to be restored on exit for proper recursive behavior.
63 Loop_Block_Count : Nat := 0;
64 -- Counter used for constructing loop/block names (see the routine
65 -- Par.Ch5.Get_Loop_Block_Name)
67 --------------------
68 -- Error Recovery --
69 --------------------
71 -- When an error is encountered, a call is made to one of the Error_Msg
72 -- routines to record the error. If the syntax scan is not derailed by the
73 -- error (e.g. a complaint that logical operators are inconsistent in an
74 -- EXPRESSION), then control returns from the Error_Msg call, and the
75 -- parse continues unimpeded.
77 -- If on the other hand, the Error_Msg represents a situation from which
78 -- the parser cannot recover locally, the exception Error_Resync is raised
79 -- immediately after the call to Error_Msg. Handlers for Error_Resync
80 -- are located at strategic points to resynchronize the parse. For example,
81 -- when an error occurs in a statement, the handler skips to the next
82 -- semicolon and continues the scan from there.
84 -- Each parsing procedure contains a note with the heading "Error recovery"
85 -- which shows if it can propagate the Error_Resync exception. In order
86 -- not to propagate the exception, a procedure must either contain its own
87 -- handler for this exception, or it must not call any other routines which
88 -- propagate the exception.
90 -- Note: the arrangement of Error_Resync handlers is such that it should
91 -- never be possible to transfer control through a procedure which made
92 -- an entry in the scope stack, invalidating the contents of the stack.
94 Error_Resync : exception;
95 -- Exception raised on error that is not handled locally, see above.
97 Last_Resync_Point : Source_Ptr;
98 -- The resynchronization routines in Par.Sync run a risk of getting
99 -- stuck in an infinite loop if they do not skip a token, and the caller
100 -- keeps repeating the same resync call. On the other hand, if they skip
101 -- a token unconditionally, some recovery opportunities are missed. The
102 -- variable Last_Resync_Point records the token location previously set
103 -- by a Resync call, and if a subsequent Resync call occurs at the same
104 -- location, then the Resync routine does guarantee to skip a token.
106 --------------------------------------------
107 -- Handling Semicolon Used in Place of IS --
108 --------------------------------------------
110 -- The following global variables are used in handling the error situation
111 -- of using a semicolon in place of IS in a subprogram declaration as in:
113 -- procedure X (Y : Integer);
114 -- Q : Integer;
115 -- begin
116 -- ...
117 -- end;
119 -- The two contexts in which this can appear are at the outer level, and
120 -- within a declarative region. At the outer level, we know something is
121 -- wrong as soon as we see the Q (or begin, if there are no declarations),
122 -- and we can immediately decide that the semicolon should have been IS.
124 -- The situation in a declarative region is more complex. The declaration
125 -- of Q could belong to the outer region, and we do not know that we have
126 -- an error until we hit the begin. It is still not clear at this point
127 -- from a syntactic point of view that something is wrong, because the
128 -- begin could belong to the enclosing subprogram or package. However, we
129 -- can incorporate a bit of semantic knowledge and note that the body of
130 -- X is missing, so we definitely DO have an error. We diagnose this error
131 -- as semicolon in place of IS on the subprogram line.
133 -- There are two styles for this diagnostic. If the begin immediately
134 -- follows the semicolon, then we can place a flag (IS expected) right
135 -- on the semicolon. Otherwise we do not detect the error until we hit
136 -- the begin which refers back to the line with the semicolon.
138 -- To control the process in the second case, the following global
139 -- variables are set to indicate that we have a subprogram declaration
140 -- whose body is required and has not yet been found. The prefix SIS
141 -- stands for "Subprogram IS" handling.
143 SIS_Entry_Active : Boolean;
144 -- Set True to indicate that an entry is active (i.e. that a subprogram
145 -- declaration has been encountered, and no body for this subprogram has
146 -- been encountered). The remaining fields are valid only if this is True.
148 SIS_Labl : Node_Id;
149 -- Subprogram designator
151 SIS_Sloc : Source_Ptr;
152 -- Source location of FUNCTION/PROCEDURE keyword
154 SIS_Ecol : Column_Number;
155 -- Column number of FUNCTION/PROCEDURE keyword
157 SIS_Semicolon_Sloc : Source_Ptr;
158 -- Source location of semicolon at end of subprogram declaration
160 SIS_Declaration_Node : Node_Id;
161 -- Pointer to tree node for subprogram declaration
163 SIS_Missing_Semicolon_Message : Error_Msg_Id;
164 -- Used to save message ID of missing semicolon message (which will be
165 -- modified to missing IS if necessary). Set to No_Error_Msg in the
166 -- normal (non-error) case.
168 -- Five things can happen to an active SIS entry
170 -- 1. If a BEGIN is encountered with an SIS entry active, then we have
171 -- exactly the situation in which we know the body of the subprogram is
172 -- missing. After posting an error message, we change the spec to a body,
173 -- rechaining the declarations that intervened between the spec and BEGIN.
175 -- 2. Another subprogram declaration or body is encountered. In this
176 -- case the entry gets overwritten with the information for the new
177 -- subprogram declaration. We don't catch some nested cases this way,
178 -- but it doesn't seem worth the effort.
180 -- 3. A nested declarative region (e.g. package declaration or package
181 -- body) is encountered. The SIS active indication is reset at the start
182 -- of such a nested region. Again, like case 2, this causes us to miss
183 -- some nested cases, but it doesn't seen worth the effort to stack and
184 -- unstack the SIS information. Maybe we will reconsider this if we ever
185 -- get a complaint about a missed case :-)
187 -- 4. We encounter a valid pragma INTERFACE or IMPORT that effectively
188 -- supplies the missing body. In this case we reset the entry.
190 -- 5. We encounter the end of the declarative region without encoutering
191 -- a BEGIN first. In this situation we simply reset the entry. We know
192 -- that there is a missing body, but it seems more reasonable to let the
193 -- later semantic checking discover this.
195 --------------------------------------------
196 -- Handling IS Used in Place of Semicolon --
197 --------------------------------------------
199 -- This is a somewhat trickier situation, and we can't catch it in all
200 -- cases, but we do our best to detect common situations resulting from
201 -- a "cut and paste" operation which forgets to change the IS to semicolon.
202 -- Consider the following example:
204 -- package body X is
205 -- procedure A;
206 -- procedure B is
207 -- procedure C;
208 -- ...
209 -- procedure D is
210 -- begin
211 -- ...
212 -- end;
213 -- begin
214 -- ...
215 -- end;
217 -- The trouble is that the section of text from PROCEDURE B through END;
218 -- consitutes a valid procedure body, and the danger is that we find out
219 -- far too late that something is wrong (indeed most compilers will behave
220 -- uncomfortably on the above example).
222 -- We have two approaches to helping to control this situation. First we
223 -- make every attempt to avoid swallowing the last END; if we can be
224 -- sure that some error will result from doing so. In particular, we won't
225 -- accept the END; unless it is exactly correct (in particular it must not
226 -- have incorrect name tokens), and we won't accept it if it is immediately
227 -- followed by end of file, WITH or SEPARATE (all tokens that unmistakeably
228 -- signal the start of a compilation unit, and which therefore allow us to
229 -- reserve the END; for the outer level.) For more details on this aspect
230 -- of the handling, see package Par.Endh.
232 -- If we can avoid eating up the END; then the result in the absense of
233 -- any additional steps would be to post a missing END referring back to
234 -- the subprogram with the bogus IS. Similarly, if the enclosing package
235 -- has no BEGIN, then the result is a missing BEGIN message, which again
236 -- refers back to the subprogram header.
238 -- Such an error message is not too bad (it's already a big improvement
239 -- over what many parsers do), but it's not ideal, because the declarations
240 -- following the IS have been absorbed into the wrong scope. In the above
241 -- case, this could result for example in a bogus complaint that the body
242 -- of D was missing from the package.
244 -- To catch at least some of these cases, we take the following additional
245 -- steps. First, a subprogram body is marked as having a suspicious IS if
246 -- the declaration line is followed by a line which starts with a symbol
247 -- that can start a declaration in the same column, or to the left of the
248 -- column in which the FUNCTION or PROCEDURE starts (normal style is to
249 -- indent any declarations which really belong a subprogram). If such a
250 -- subprogram encounters a missing BEGIN or missing END, then we decide
251 -- that the IS should have been a semicolon, and the subprogram body node
252 -- is marked (by setting the Bad_Is_Detected flag true. Note that we do
253 -- not do this for library level procedures, only for nested procedures,
254 -- since for library level procedures, we must have a body.
256 -- The processing for a declarative part checks to see if the last
257 -- declaration scanned is marked in this way, and if it is, the tree
258 -- is modified to reflect the IS being interpreted as a semicolon.
260 ---------------------------------------------------
261 -- Parser Type Definitions and Control Variables --
262 ---------------------------------------------------
264 -- The following variable and associated type declaration are used by the
265 -- expression parsing routines to return more detailed information about
266 -- the categorization of a parsed expression.
268 type Expr_Form_Type is (
269 EF_Simple_Name, -- Simple name, i.e. possibly qualified identifier
270 EF_Name, -- Simple expression which could also be a name
271 EF_Simple, -- Simple expression which is not call or name
272 EF_Range_Attr, -- Range attribute reference
273 EF_Non_Simple); -- Expression that is not a simple expression
275 Expr_Form : Expr_Form_Type;
277 -- The following type is used for calls to P_Subprogram, P_Package, P_Task,
278 -- P_Protected to indicate which of several possibilities is acceptable.
280 type Pf_Rec is record
281 Spcn : Boolean; -- True if specification OK
282 Decl : Boolean; -- True if declaration OK
283 Gins : Boolean; -- True if generic instantiation OK
284 Pbod : Boolean; -- True if proper body OK
285 Rnam : Boolean; -- True if renaming declaration OK
286 Stub : Boolean; -- True if body stub OK
287 Fil1 : Boolean; -- Filler to fill to 8 bits
288 Fil2 : Boolean; -- Filler to fill to 8 bits
289 end record;
290 pragma Pack (Pf_Rec);
292 function T return Boolean renames True;
293 function F return Boolean renames False;
295 Pf_Decl_Gins_Pbod_Rnam_Stub : constant Pf_Rec :=
296 Pf_Rec'(F, T, T, T, T, T, F, F);
297 Pf_Decl : constant Pf_Rec :=
298 Pf_Rec'(F, T, F, F, F, F, F, F);
299 Pf_Decl_Gins_Pbod_Rnam : constant Pf_Rec :=
300 Pf_Rec'(F, T, T, T, T, F, F, F);
301 Pf_Decl_Pbod : constant Pf_Rec :=
302 Pf_Rec'(F, T, F, T, F, F, F, F);
303 Pf_Pbod : constant Pf_Rec :=
304 Pf_Rec'(F, F, F, T, F, F, F, F);
305 Pf_Spcn : constant Pf_Rec :=
306 Pf_Rec'(T, F, F, F, F, F, F, F);
307 -- The above are the only allowed values of Pf_Rec arguments
309 type SS_Rec is record
310 Eftm : Boolean; -- ELSIF can terminate sequence
311 Eltm : Boolean; -- ELSE can terminate sequence
312 Extm : Boolean; -- EXCEPTION can terminate sequence
313 Ortm : Boolean; -- OR can terminate sequence
314 Sreq : Boolean; -- at least one statement required
315 Tatm : Boolean; -- THEN ABORT can terminate sequence
316 Whtm : Boolean; -- WHEN can terminate sequence
317 Unco : Boolean; -- Unconditional terminate after one statement
318 end record;
319 pragma Pack (SS_Rec);
321 SS_Eftm_Eltm_Sreq : constant SS_Rec := SS_Rec'(T, T, F, F, T, F, F, F);
322 SS_Eltm_Ortm_Tatm : constant SS_Rec := SS_Rec'(F, T, F, T, F, T, F, F);
323 SS_Extm_Sreq : constant SS_Rec := SS_Rec'(F, F, T, F, T, F, F, F);
324 SS_None : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, F);
325 SS_Ortm_Sreq : constant SS_Rec := SS_Rec'(F, F, F, T, T, F, F, F);
326 SS_Sreq : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, F, F);
327 SS_Sreq_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, T, F);
328 SS_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, T, F);
329 SS_Unco : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, T);
331 Label_List : Elist_Id;
332 -- List of label nodes for labels appearing in the current compilation.
333 -- Used by Par.Labl to construct the corresponding implicit declarations.
335 -----------------
336 -- Scope Table --
337 -----------------
339 -- The scope table, also referred to as the scope stack, is used to
340 -- record the current scope context. It is organized as a stack, with
341 -- inner nested entries corresponding to higher entries on the stack.
342 -- An entry is made when the parser encounters the opening of a nested
343 -- construct (such as a record, task, package etc.), and then package
344 -- Par.Endh uses this stack to deal with END lines (including properly
345 -- dealing with END nesting errors).
347 type SS_End_Type is
348 -- Type of end entry required for this scope. The last two entries are
349 -- used only in the subprogram body case to mark the case of a suspicious
350 -- IS, or a bad IS (i.e. suspicions confirmed by missing BEGIN or END).
351 -- See separate section on dealing with IS used in place of semicolon.
352 -- Note that for many purposes E_Name, E_Suspicious_Is and E_Bad_Is are
353 -- treated the same (E_Suspicious_Is and E_Bad_Is are simply special cases
354 -- of E_Name). They are placed at the end of the enumeration so that a
355 -- test for >= E_Name catches all three cases efficiently.
357 (E_Dummy, -- dummy entry at outer level
358 E_Case, -- END CASE;
359 E_If, -- END IF;
360 E_Loop, -- END LOOP;
361 E_Record, -- END RECORD;
362 E_Select, -- END SELECT;
363 E_Name, -- END [name];
364 E_Suspicious_Is, -- END [name]; (case of suspicious IS)
365 E_Bad_Is); -- END [name]; (case of bad IS)
367 -- The following describes a single entry in the scope table
369 type Scope_Table_Entry is record
370 Etyp : SS_End_Type;
371 -- Type of end entry, as per above description
373 Lreq : Boolean;
374 -- A flag indicating whether the label, if present, is required to
375 -- appear on the end line. It is referenced only in the case of
376 -- Etyp = E_Name or E_Suspicious_Is where the name may or may not be
377 -- required (yes for labeled block, no in other cases). Note that for
378 -- all cases except begin, the question of whether a label is required
379 -- can be determined from the other fields (for loop, it is required if
380 -- it is present, and for the other constructs it is never required or
381 -- allowed).
383 Ecol : Column_Number;
384 -- Contains the absolute column number (with tabs expanded) of the
385 -- the expected column of the end assuming normal Ada indentation
386 -- usage. If the RM_Column_Check mode is set, this value is used for
387 -- generating error messages about indentation. Otherwise it is used
388 -- only to control heuristic error recovery actions.
390 Labl : Node_Id;
391 -- This field is used only for the LOOP and BEGIN cases, and is the
392 -- Node_Id value of the label name. For all cases except child units,
393 -- this value is an entity whose Chars field contains the name pointer
394 -- that identifies the label uniquely. For the child unit case the Labl
395 -- field references an N_Defining_Program_Unit_Name node for the name.
396 -- For cases other than LOOP or BEGIN, the Label field is set to Error,
397 -- indicating that it is an error to have a label on the end line.
398 -- (this is really a misuse of Error since there is no Error ???)
400 Decl : List_Id;
401 -- Points to the list of declarations (i.e. the declarative part)
402 -- associated with this construct. It is set only in the END [name]
403 -- cases, and is set to No_List for all other cases which do not have a
404 -- declarative unit associated with them. This is used for determining
405 -- the proper location for implicit label declarations.
407 Node : Node_Id;
408 -- Empty except in the case of entries for IF and CASE statements,
409 -- in which case it contains the N_If_Statement or N_Case_Statement
410 -- node. This is used for setting the End_Span field.
412 Sloc : Source_Ptr;
413 -- Source location of the opening token of the construct. This is
414 -- used to refer back to this line in error messages (such as missing
415 -- or incorrect end lines). The Sloc field is not used, and is not set,
416 -- if a label is present (the Labl field provides the text name of the
417 -- label in this case, which is fine for error messages).
419 S_Is : Source_Ptr;
420 -- S_Is is relevant only if Etyp is set to E_Suspicious_Is or
421 -- E_Bad_Is. It records the location of the IS that is considered
422 -- to be suspicious.
424 Junk : Boolean;
425 -- A boolean flag that is set true if the opening entry is the dubious
426 -- result of some prior error, e.g. a record entry where the record
427 -- keyword was missing. It is used to suppress the issuing of a
428 -- corresponding junk complaint about the end line (we do not want
429 -- to complain about a missing end record when there was no record).
430 end record;
432 -- The following declares the scope table itself. The Last field is the
433 -- stack pointer, so that Scope.Table (Scope.Last) is the top entry. The
434 -- oldest entry, at Scope_Stack (0), is a dummy entry with Etyp set to
435 -- E_Dummy, and the other fields undefined. This dummy entry ensures that
436 -- Scope_Stack (Scope_Stack_Ptr).Etyp can always be tested, and that the
437 -- scope stack pointer is always in range.
439 package Scope is new Table.Table (
440 Table_Component_Type => Scope_Table_Entry,
441 Table_Index_Type => Int,
442 Table_Low_Bound => 0,
443 Table_Initial => 50,
444 Table_Increment => 100,
445 Table_Name => "Scope");
447 ---------------------------------
448 -- Parsing Routines by Chapter --
449 ---------------------------------
451 -- Uncommented declarations in this section simply parse the construct
452 -- corresponding to their name, and return an ID value for the Node or
453 -- List that is created.
455 package Ch2 is
456 function P_Identifier return Node_Id;
457 function P_Pragma return Node_Id;
459 function P_Pragmas_Opt return List_Id;
460 -- This function scans for a sequence of pragmas in other than a
461 -- declaration sequence or statement sequence context. All pragmas
462 -- can appear except pragmas Assert and Debug, which are only allowed
463 -- in a declaration or statement sequence context.
465 procedure P_Pragmas_Misplaced;
466 -- Skips misplaced pragmas with a complaint
468 procedure P_Pragmas_Opt (List : List_Id);
469 -- Parses optional pragmas and appends them to the List
470 end Ch2;
472 package Ch3 is
473 Missing_Begin_Msg : Error_Msg_Id;
474 -- This variable is set by a call to P_Declarative_Part. Normally it
475 -- is set to No_Error_Msg, indicating that no special processing is
476 -- required by the caller. The special case arises when a statement
477 -- is found in the sequence of declarations. In this case the Id of
478 -- the message issued ("declaration expected") is preserved in this
479 -- variable, then the caller can change it to an appropriate missing
480 -- begin message if indeed the BEGIN is missing.
482 function P_Access_Definition return Node_Id;
483 function P_Access_Type_Definition return Node_Id;
484 function P_Array_Type_Definition return Node_Id;
485 function P_Basic_Declarative_Items return List_Id;
486 function P_Constraint_Opt return Node_Id;
487 function P_Declarative_Part return List_Id;
488 function P_Defining_Identifier return Node_Id;
489 function P_Discrete_Choice_List return List_Id;
490 function P_Discrete_Range return Node_Id;
491 function P_Discrete_Subtype_Definition return Node_Id;
492 function P_Known_Discriminant_Part_Opt return List_Id;
493 function P_Signed_Integer_Type_Definition return Node_Id;
494 function P_Range return Node_Id;
495 function P_Range_Or_Subtype_Mark return Node_Id;
496 function P_Range_Constraint return Node_Id;
497 function P_Record_Definition return Node_Id;
498 function P_Subtype_Indication return Node_Id;
499 function P_Subtype_Mark return Node_Id;
500 function P_Subtype_Mark_Resync return Node_Id;
501 function P_Unknown_Discriminant_Part_Opt return Boolean;
503 procedure P_Component_Items (Decls : List_Id);
504 -- Scan out one or more component items and append them to the
505 -- given list. Only scans out more than one declaration in the
506 -- case where the source has a single declaration with multiple
507 -- defining identifiers.
509 function Init_Expr_Opt (P : Boolean := False) return Node_Id;
510 -- If an initialization expression is present (:= expression), then
511 -- it is scanned out and returned, otherwise Empty is returned if no
512 -- initialization expression is present. This procedure also handles
513 -- certain common error cases cleanly. The parameter P indicates if
514 -- a right paren can follow the expression (default = no right paren
515 -- allowed).
517 procedure Skip_Declaration (S : List_Id);
518 -- Used when scanning statements to skip past a mispaced declaration
519 -- The declaration is scanned out and appended to the given list.
520 -- Token is known to be a declaration token (in Token_Class_Declk)
521 -- on entry, so there definition is a declaration to be scanned.
523 function P_Subtype_Indication (Subtype_Mark : Node_Id) return Node_Id;
524 -- This version of P_Subtype_Indication is called when the caller has
525 -- already scanned out the subtype mark which is passed as a parameter.
527 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id;
528 -- Parse a subtype mark attribute. The caller has already parsed the
529 -- subtype mark, which is passed in as the argument, and has checked
530 -- that the current token is apostrophe.
532 end Ch3;
534 package Ch4 is
535 function P_Aggregate return Node_Id;
536 function P_Expression return Node_Id;
537 function P_Expression_No_Right_Paren return Node_Id;
538 function P_Expression_Or_Range_Attribute return Node_Id;
539 function P_Function_Name return Node_Id;
540 function P_Name return Node_Id;
541 function P_Qualified_Simple_Name return Node_Id;
542 function P_Qualified_Simple_Name_Resync return Node_Id;
543 function P_Simple_Expression return Node_Id;
544 function P_Simple_Expression_Or_Range_Attribute return Node_Id;
546 function P_Qualified_Expression
547 (Subtype_Mark : Node_Id)
548 return Node_Id;
549 -- This routine scans out a qualified expression when the caller has
550 -- already scanned out the name and apostrophe of the construct.
552 end Ch4;
554 package Ch5 is
556 function P_Statement_Name (Name_Node : Node_Id) return Node_Id;
557 -- Given a node representing a name (which is a call), converts it
558 -- to the syntactically corresponding procedure call statement.
560 function P_Sequence_Of_Statements (SS_Flags : SS_Rec) return List_Id;
561 -- The argument indicates the acceptable termination tokens.
562 -- See body in Par.Ch5 for details of the use of this parameter.
564 procedure Parse_Decls_Begin_End (Parent : Node_Id);
565 -- Parses declarations and handled statement sequence, setting
566 -- fields of Parent node appropriately.
568 end Ch5;
570 package Ch6 is
571 function P_Designator return Node_Id;
572 function P_Defining_Program_Unit_Name return Node_Id;
573 function P_Formal_Part return List_Id;
574 function P_Parameter_Profile return List_Id;
575 function P_Return_Statement return Node_Id;
576 function P_Subprogram_Specification return Node_Id;
578 procedure P_Mode (Node : Node_Id);
579 -- Sets In_Present and/or Out_Present flags in Node scanning past
580 -- IN, OUT or IN OUT tokens in the source.
582 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id;
583 -- Scans out any construct starting with either of the keywords
584 -- PROCEDURE or FUNCTION. The parameter indicates which possible
585 -- possible kinds of construct (body, spec, instantiation etc.)
586 -- are permissible in the current context.
588 end Ch6;
590 package Ch7 is
591 function P_Package (Pf_Flags : Pf_Rec) return Node_Id;
592 -- Scans out any construct starting with the keyword PACKAGE. The
593 -- parameter indicates which possible kinds of construct (body, spec,
594 -- instantiation etc.) are permissible in the current context.
595 end Ch7;
597 package Ch8 is
598 function P_Use_Clause return Node_Id;
599 end Ch8;
601 package Ch9 is
602 function P_Abort_Statement return Node_Id;
603 function P_Abortable_Part return Node_Id;
604 function P_Accept_Statement return Node_Id;
605 function P_Delay_Statement return Node_Id;
606 function P_Entry_Body return Node_Id;
607 function P_Protected return Node_Id;
608 function P_Requeue_Statement return Node_Id;
609 function P_Select_Statement return Node_Id;
610 function P_Task return Node_Id;
611 function P_Terminate_Alternative return Node_Id;
612 end Ch9;
614 package Ch10 is
615 function P_Compilation_Unit return Node_Id;
616 -- Note: this function scans a single compilation unit, and
617 -- checks that an end of file follows this unit, diagnosing
618 -- any unexpected input as an error, and then skipping it, so
619 -- that Token is set to Tok_EOF on return. An exception is in
620 -- syntax-only mode, where multiple compilation units are
621 -- permitted. In this case, P_Compilation_Unit does not check
622 -- for end of file and there may be more compilation units to
623 -- scan. The caller can uniquely detect this situation by the
624 -- fact that Token is not set to Tok_EOF on return.
625 end Ch10;
627 package Ch11 is
628 function P_Handled_Sequence_Of_Statements return Node_Id;
629 function P_Raise_Statement return Node_Id;
631 function Parse_Exception_Handlers return List_Id;
632 -- Parses the partial construct EXCEPTION followed by a list of
633 -- exception handlers which appears in a number of productions,
634 -- and returns the list of exception handlers.
636 end Ch11;
638 package Ch12 is
639 function P_Generic return Node_Id;
640 function P_Generic_Actual_Part_Opt return List_Id;
641 end Ch12;
643 package Ch13 is
644 function P_Representation_Clause return Node_Id;
646 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id;
647 -- Function to parse a code statement. The caller has scanned out
648 -- the name to be used as the subtype mark (but has not checked that
649 -- it is suitable for use as a subtype mark, i.e. is either an
650 -- identifier or a selected component). The current token is an
651 -- apostrophe and the following token is either a left paren or
652 -- RANGE (the latter being an error to be caught by P_Code_Statement.
653 end Ch13;
655 -- Note: the parsing for annexe J features (i.e. obsolescent features)
656 -- is found in the logical section where these features would be if
657 -- they were not obsolescent. In particular:
659 -- Delta constraint is parsed by P_Delta_Constraint (3.5.9)
660 -- At clause is parsed by P_At_Clause (13.1)
661 -- Mod clause is parsed by P_Mod_Clause (13.5.1)
663 ------------------
664 -- End Handling --
665 ------------------
667 -- Routines for handling end lines, including scope recovery
669 package Endh is
671 function Check_End return Boolean;
672 -- Called when an end sequence is required. In the absence of an error
673 -- situation, Token contains Tok_End on entry, but in a missing end
674 -- case, this may not be the case. Pop_End_Context is used to determine
675 -- the appropriate action to be taken. The returned result is True if
676 -- an End sequence was encountered and False if no End sequence was
677 -- present. This occurs if the END keyword encountered was determined
678 -- to be improper and deleted (i.e. Pop_End_Context set End_Action to
679 -- Skip_And_Reject). Note that the END sequence includes a semicolon,
680 -- except in the case of END RECORD, where a semicolon follows the END
681 -- RECORD, but is not part of the record type definition itself.
683 procedure End_Skip;
684 -- Skip past an end sequence. On entry Token contains Tok_End, and we
685 -- we know that the end sequence is syntactically incorrect, and that
686 -- an appropriate error message has already been posted. The mission
687 -- is simply to position the scan pointer to be the best guess of the
688 -- position after the end sequence. We do not issue any additional
689 -- error messages while carrying this out.
691 procedure End_Statements (Parent : Node_Id := Empty);
692 -- Called when an end is required or expected to terminate a sequence
693 -- of statements. The caller has already made an appropriate entry in
694 -- the Scope.Table to describe the expected form of the end. This can
695 -- only be used in cases where the only appropriate terminator is end.
696 -- If Parent is non-empty, then if a correct END line is encountered,
697 -- the End_Label field of Parent is set appropriately.
699 end Endh;
701 ------------------------------------
702 -- Resynchronization After Errors --
703 ------------------------------------
705 -- These procedures are used to resynchronize after errors. Following an
706 -- error which is not immediately locally recoverable, the exception
707 -- Error_Resync is raised. The handler for Error_Resync typically calls
708 -- one of these recovery procedures to resynchronize the source position
709 -- to a point from which parsing can be restarted.
711 -- Note: these procedures output an information message that tokens are
712 -- being skipped, but this message is output only if the option for
713 -- Multiple_Errors_Per_Line is set in Options.
715 package Sync is
717 procedure Resync_Choice;
718 -- Used if an error occurs scanning a choice. The scan pointer is
719 -- advanced to the next vertical bar, arrow, or semicolon, whichever
720 -- comes first. We also quit if we encounter an end of file.
722 procedure Resync_Expression;
723 -- Used if an error is detected during the parsing of an expression.
724 -- It skips past tokens until either a token which cannot be part of
725 -- an expression is encountered (an expression terminator), or if a
726 -- comma or right parenthesis or vertical bar is encountered at the
727 -- current parenthesis level (a parenthesis level counter is maintained
728 -- to carry out this test).
730 procedure Resync_Past_Semicolon;
731 -- Used if an error occurs while scanning a sequence of declarations.
732 -- The scan pointer is positioned past the next semicolon and the scan
733 -- resumes. The scan is also resumed on encountering a token which
734 -- starts a declaration (but we make sure to skip at least one token
735 -- in this case, to avoid getting stuck in a loop).
737 procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then;
738 -- Used if an error occurs while scanning a sequence of statements.
739 -- The scan pointer is positioned past the next semicolon, or to the
740 -- next occurrence of either then or loop, and the scan resumes.
742 procedure Resync_To_When;
743 -- Used when an error occurs scanning an entry index specification.
744 -- The scan pointer is positioned to the next WHEN (or to IS or
745 -- semicolon if either of these appear before WHEN, indicating
746 -- another error has occurred).
748 procedure Resync_Semicolon_List;
749 -- Used if an error occurs while scanning a parenthesized list of items
750 -- separated by semicolons. The scan pointer is advanced to the next
751 -- semicolon or right parenthesis at the outer parenthesis level, or
752 -- to the next is or RETURN keyword occurrence, whichever comes first.
754 procedure Resync_Cunit;
755 -- Synchronize to next token which could be the start of a compilation
756 -- unit, or to the end of file token.
758 end Sync;
760 -------------------------
761 -- Token Scan Routines --
762 -------------------------
764 -- Routines to check for expected tokens
766 package Tchk is
768 -- Procedures with names of the form T_xxx, where Tok_xxx is a token
769 -- name, check that the current token matches the required token, and
770 -- if so, scan past it. If not, an error is issued indicating that
771 -- the required token is not present (xxx expected). In most cases, the
772 -- scan pointer is not moved in the not-found case, but there are some
773 -- exceptions to this, see for example T_Id, where the scan pointer is
774 -- moved across a literal appearing where an identifier is expected.
776 procedure T_Abort;
777 procedure T_Arrow;
778 procedure T_At;
779 procedure T_Body;
780 procedure T_Box;
781 procedure T_Colon;
782 procedure T_Colon_Equal;
783 procedure T_Comma;
784 procedure T_Dot_Dot;
785 procedure T_For;
786 procedure T_Greater_Greater;
787 procedure T_Identifier;
788 procedure T_In;
789 procedure T_Is;
790 procedure T_Left_Paren;
791 procedure T_Loop;
792 procedure T_Mod;
793 procedure T_New;
794 procedure T_Of;
795 procedure T_Or;
796 procedure T_Private;
797 procedure T_Range;
798 procedure T_Record;
799 procedure T_Right_Paren;
800 procedure T_Semicolon;
801 procedure T_Then;
802 procedure T_Type;
803 procedure T_Use;
804 procedure T_When;
805 procedure T_With;
807 -- Procedures have names of the form TF_xxx, where Tok_xxx is a token
808 -- name check that the current token matches the required token, and
809 -- if so, scan past it. If not, an error message is issued indicating
810 -- that the required token is not present (xxx expected).
812 -- If the missing token is at the end of the line, then control returns
813 -- immediately after posting the message. If there are remaining tokens
814 -- on the current line, a search is conducted to see if the token
815 -- appears later on the current line, as follows:
817 -- A call to Scan_Save is issued and a forward search for the token
818 -- is carried out. If the token is found on the current line before a
819 -- semicolon, then it is scanned out and the scan continues from that
820 -- point. If not the scan is restored to the point where it was missing.
822 procedure TF_Arrow;
823 procedure TF_Is;
824 procedure TF_Loop;
825 procedure TF_Return;
826 procedure TF_Semicolon;
827 procedure TF_Then;
828 procedure TF_Use;
830 end Tchk;
832 ----------------------
833 -- Utility Routines --
834 ----------------------
836 package Util is
838 function Bad_Spelling_Of (T : Token_Type) return Boolean;
839 -- This function is called in an error situation. It checks if the
840 -- current token is an identifier whose name is a plausible bad
841 -- spelling of the given keyword token, and if so, issues an error
842 -- message, sets Token from T, and returns True. Otherwise Token is
843 -- unchanged, and False is returned.
845 procedure Check_Bad_Layout;
846 -- Check for bad indentation in RM checking mode. Used for statements
847 -- and declarations. Checks if current token is at start of line and
848 -- is exdented from the current expected end column, and if so an
849 -- error message is generated.
851 procedure Check_Misspelling_Of (T : Token_Type);
852 pragma Inline (Check_Misspelling_Of);
853 -- This is similar to the function above, except that it does not
854 -- return a result. It is typically used in a situation where any
855 -- identifier is an error, and it makes sense to simply convert it
856 -- to the given token if it is a plausible misspelling of it.
858 procedure Check_95_Keyword (Token_95, Next : Token_Type);
859 -- This routine checks if the token after the current one matches the
860 -- Next argument. If so, the scan is backed up to the current token
861 -- and Token_Type is changed to Token_95 after issuing an appropriate
862 -- error message ("(Ada 83) keyword xx cannot be used"). If not,
863 -- the scan is backed up with Token_Type unchanged. This routine
864 -- is used to deal with an attempt to use a 95 keyword in Ada 83
865 -- mode. The caller has typically checked that the current token,
866 -- an identifier, matches one of the 95 keywords.
868 procedure Check_Simple_Expression (E : Node_Id);
869 -- Given an expression E, that has just been scanned, so that Expr_Form
870 -- is still set, outputs an error if E is a non-simple expression. E is
871 -- not modified by this call.
873 procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id);
874 -- Like Check_Simple_Expression, except that the error message is only
875 -- given when operating in Ada 83 mode, and includes "in Ada 83".
877 function Check_Subtype_Mark (Mark : Node_Id) return Node_Id;
878 -- Called to check that a node representing a name (or call) is
879 -- suitable for a subtype mark, i.e, that it is an identifier or
880 -- a selected component. If so, or if it is already Error, then
881 -- it is returned unchanged. Otherwise an error message is issued
882 -- and Error is returned.
884 function Comma_Present return Boolean;
885 -- Used in comma delimited lists to determine if a comma is present, or
886 -- can reasonably be assumed to have been present (an error message is
887 -- generated in the latter case). If True is returned, the scan has been
888 -- positioned past the comma. If False is returned, the scan position
889 -- is unchanged. Note that all comma-delimited lists are terminated by
890 -- a right paren, so the only legitimate tokens when Comma_Present is
891 -- called are right paren and comma. If some other token is found, then
892 -- Comma_Present has the job of deciding whether it is better to pretend
893 -- a comma was present, post a message for a missing comma and return
894 -- True, or return False and let the caller diagnose the missing right
895 -- parenthesis.
897 procedure Discard_Junk_Node (N : Node_Id);
898 procedure Discard_Junk_List (L : List_Id);
899 pragma Inline (Discard_Junk_Node);
900 pragma Inline (Discard_Junk_List);
901 -- These procedures do nothing at all, their effect is simply to discard
902 -- the argument. A typical use is to skip by some junk that is not
903 -- expected in the current context.
905 procedure Ignore (T : Token_Type);
906 -- If current token matches T, then give an error message and skip
907 -- past it, otherwise the call has no effect at all. T may be any
908 -- reserved word token, or comma, left or right paren, or semicolon.
910 function Is_Reserved_Identifier return Boolean;
911 -- Test if current token is a reserved identifier. This test is based
912 -- on the token being a keyword and being spelled in typical identifier
913 -- style (i.e. starting with an upper case letter).
915 procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type);
916 -- Called when the previous token is an identifier (whose Token_Node
917 -- value is given by Prev) to check if current token is an identifier
918 -- that can be merged with the previous one adding an underscore. The
919 -- merge is only attempted if the following token matches Nxt. If all
920 -- conditions are met, an error message is issued, and the merge is
921 -- carried out, modifying the Chars field of Prev.
923 procedure No_Constraint;
924 -- Called in a place where no constraint is allowed, but one might
925 -- appear due to a common error (e.g. after the type mark in a procedure
926 -- parameter. If a constraint is present, an error message is posted,
927 -- and the constraint is scanned and discarded.
929 function No_Right_Paren (Expr : Node_Id) return Node_Id;
930 -- Function to check for no right paren at end of expression, returns
931 -- its argument if no right paren, else flags paren and returns Error.
933 procedure Push_Scope_Stack;
934 pragma Inline (Push_Scope_Stack);
935 -- Push a new entry onto the scope stack. Scope.Last (the stack pointer)
936 -- is incremented. The Junk field is preinitialized to False. The caller
937 -- is expected to fill in all remaining entries of the new new top stack
938 -- entry at Scope.Table (Scope.Last).
940 procedure Pop_Scope_Stack;
941 -- Pop an entry off the top of the scope stack. Scope_Last (the scope
942 -- table stack pointer) is decremented by one. It is a fatal error to
943 -- try to pop off the dummy entry at the bottom of the stack (i.e.
944 -- Scope.Last must be non-zero at the time of call).
946 function Separate_Present return Boolean;
947 -- Determines if the current token is either Tok_Separate, or an
948 -- identifier that is a possible misspelling of "separate" followed
949 -- by a semicolon. True is returned if so, otherwise False.
951 procedure Signal_Bad_Attribute;
952 -- The current token is an identifier that is supposed to be an
953 -- attribute identifier but is not. This routine posts appropriate
954 -- error messages, including a check for a near misspelling.
956 function Token_Is_At_Start_Of_Line return Boolean;
957 pragma Inline (Token_Is_At_Start_Of_Line);
958 -- Determines if the current token is the first token on the line
960 end Util;
962 ---------------------------------------
963 -- Specialized Syntax Check Routines --
964 ---------------------------------------
966 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id;
967 -- This function is passed a tree for a pragma that has been scanned out.
968 -- The pragma is syntactically well formed according to the general syntax
969 -- for pragmas and the pragma identifier is for one of the recognized
970 -- pragmas. It performs specific syntactic checks for specific pragmas.
971 -- The result is the input node if it is OK, or Error otherwise. The
972 -- reason that this is separated out is to facilitate the addition
973 -- of implementation defined pragmas. The second parameter records the
974 -- location of the semicolon following the pragma (this is needed for
975 -- correct processing of the List and Page pragmas). The returned value
976 -- is a copy of Pragma_Node, or Error if an error is found. Note that
977 -- at the point where Prag is called, the right paren ending the pragma
978 -- has been scanned out, and except in the case of pragma Style_Checks,
979 -- so has the following semicolon. For Style_Checks, the caller delays
980 -- the scanning of the semicolon so that it will be scanned using the
981 -- settings from the Style_Checks pragma preceding it.
983 -------------------------
984 -- Subsidiary Routines --
985 -------------------------
987 procedure Labl;
988 -- This procedure creates implicit label declarations for all label that
989 -- are declared in the current unit. Note that this could conceptually
990 -- be done at the point where the labels are declared, but it is tricky
991 -- to do it then, since the tree is not hooked up at the point where the
992 -- label is declared (e.g. a sequence of statements is not yet attached
993 -- to its containing scope at the point a label in the sequence is found)
995 procedure Load;
996 -- This procedure loads all subsidiary units that are required by this
997 -- unit, including with'ed units, specs for bodies, and parents for child
998 -- units. It does not load bodies for inlined procedures and generics,
999 -- since we don't know till semantic analysis is complete what is needed.
1001 -----------
1002 -- Stubs --
1003 -----------
1005 -- The package bodies can see all routines defined in all other subpackages
1007 use Ch2;
1008 use Ch3;
1009 use Ch4;
1010 use Ch5;
1011 use Ch6;
1012 use Ch7;
1013 use Ch8;
1014 use Ch9;
1015 use Ch10;
1016 use Ch11;
1017 use Ch12;
1018 use Ch13;
1020 use Endh;
1021 use Tchk;
1022 use Sync;
1023 use Util;
1025 package body Ch2 is separate;
1026 package body Ch3 is separate;
1027 package body Ch4 is separate;
1028 package body Ch5 is separate;
1029 package body Ch6 is separate;
1030 package body Ch7 is separate;
1031 package body Ch8 is separate;
1032 package body Ch9 is separate;
1033 package body Ch10 is separate;
1034 package body Ch11 is separate;
1035 package body Ch12 is separate;
1036 package body Ch13 is separate;
1038 package body Endh is separate;
1039 package body Tchk is separate;
1040 package body Sync is separate;
1041 package body Util is separate;
1043 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id
1044 is separate;
1046 procedure Labl is separate;
1047 procedure Load is separate;
1049 ---------
1050 -- Par --
1051 ---------
1053 -- This function is the parse routine called at the outer level. It parses
1054 -- the current compilation unit and adds implicit label declarations.
1056 begin
1057 -- Deal with configuration pragmas case first
1059 if Configuration_Pragmas then
1060 declare
1061 Ecount : constant Int := Total_Errors_Detected;
1062 Pragmas : List_Id := Empty_List;
1063 P_Node : Node_Id;
1065 begin
1066 loop
1067 if Token = Tok_EOF then
1068 return Pragmas;
1070 elsif Token /= Tok_Pragma then
1071 Error_Msg_SC ("only pragmas allowed in configuration file");
1072 return Error_List;
1074 else
1075 P_Node := P_Pragma;
1077 if Total_Errors_Detected > Ecount then
1078 return Error_List;
1079 end if;
1081 if Chars (P_Node) > Last_Configuration_Pragma_Name
1082 and then Chars (P_Node) /= Name_Source_Reference
1083 then
1084 Error_Msg_SC
1085 ("only configuration pragmas allowed " &
1086 "in configuration file");
1087 return Error_List;
1088 end if;
1090 Append (P_Node, Pragmas);
1091 end if;
1092 end loop;
1093 end;
1095 -- Normal case of compilation unit
1097 else
1098 Save_Opt_Config_Switches (Save_Config_Switches);
1100 -- Special processing for language defined units. For this purpose
1101 -- we do NOT consider the renamings in annex J as predefined. That
1102 -- allows users to compile their own versions of these files, and
1103 -- in particular, in the VMS implementation, the DEC versions can
1104 -- be substituted for the standard Ada 95 versions.
1106 if Is_Predefined_File_Name
1107 (Fname => File_Name (Current_Source_File),
1108 Renamings_Included => False)
1109 then
1110 Set_Opt_Config_Switches
1111 (Is_Internal_File_Name (File_Name (Current_Source_File)));
1113 -- If this is the main unit, disallow compilation unless the -gnatg
1114 -- (GNAT mode) switch is set (from a user point of view, the rule is
1115 -- that language defined units cannot be recompiled).
1117 -- However, an exception is s-rpc, and its children. We test this
1118 -- by looking at the character after the minus, the rule is that
1119 -- System.RPC and its children are the only children in System
1120 -- whose second level name can start with the letter r.
1122 Get_Name_String (File_Name (Current_Source_File));
1124 if (Name_Len < 3 or else Name_Buffer (1 .. 3) /= "s-r")
1125 and then Current_Source_Unit = Main_Unit
1126 and then not GNAT_Mode
1127 and then Operating_Mode = Generate_Code
1128 then
1129 Error_Msg_SC ("language defined units may not be recompiled");
1130 end if;
1131 end if;
1133 -- The following loop runs more than once only in syntax check mode
1134 -- where we allow multiple compilation units in the same file.
1136 loop
1137 Set_Opt_Config_Switches
1138 (Is_Internal_File_Name (File_Name (Current_Source_File)));
1140 -- Initialize scope table and other parser control variables
1142 Compiler_State := Parsing;
1143 Scope.Init;
1144 Scope.Increment_Last;
1145 Scope.Table (0).Etyp := E_Dummy;
1146 SIS_Entry_Active := False;
1147 Last_Resync_Point := No_Location;
1149 Label_List := New_Elmt_List;
1150 Unit_Node := P_Compilation_Unit;
1152 -- If we are not at an end of file, then this means that we are
1153 -- in syntax scan mode, and we can have another compilation unit,
1154 -- otherwise we will exit from the loop.
1156 exit when Token = Tok_EOF;
1157 Restore_Opt_Config_Switches (Save_Config_Switches);
1158 Set_Comes_From_Source_Default (False);
1159 end loop;
1161 -- Now that we have completely parsed the source file, we can
1162 -- complete the source file table entry.
1164 Complete_Source_File_Entry;
1166 -- An internal error check, the scope stack should now be empty
1168 pragma Assert (Scope.Last = 0);
1170 -- Remaining steps are to create implicit label declarations and to
1171 -- load required subsidiary sources. These steps are required only
1172 -- if we are doing semantic checking.
1174 if Operating_Mode /= Check_Syntax or else Debug_Flag_F then
1175 Par.Labl;
1176 Par.Load;
1177 end if;
1179 -- Restore settings of switches saved on entry
1181 Restore_Opt_Config_Switches (Save_Config_Switches);
1182 Set_Comes_From_Source_Default (False);
1183 return Empty_List;
1184 end if;
1186 end Par;