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