OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / par.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                  P A R                                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Casing;   use Casing;
28 with Debug;    use Debug;
29 with Elists;   use Elists;
30 with Errout;   use Errout;
31 with Fname;    use Fname;
32 with Lib;      use Lib;
33 with Namet;    use Namet;
34 with Nlists;   use Nlists;
35 with Nmake;    use Nmake;
36 with Opt;      use Opt;
37 with Output;   use Output;
38 with Scans;    use Scans;
39 with Scn;      use Scn;
40 with Sinput;   use Sinput;
41 with Sinput.L; use Sinput.L;
42 with Sinfo;    use Sinfo;
43 with Snames;   use Snames;
44 with Style;
45 with Table;
46 with Tbuild;   use Tbuild;
47
48 ---------
49 -- Par --
50 ---------
51
52 function Par
53   (Configuration_Pragmas : Boolean) return List_Id
54 is
55    Num_Library_Units : Natural := 0;
56    --  Count number of units parsed (relevant only in syntax check only mode,
57    --  since in semantics check mode only a single unit is permitted anyway)
58
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.
62
63    Loop_Block_Count : Nat := 0;
64    --  Counter used for constructing loop/block names (see the routine
65    --  Par.Ch5.Get_Loop_Block_Name)
66
67    --------------------
68    -- Error Recovery --
69    --------------------
70
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.
76
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.
83
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.
89
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.
93
94    Error_Resync : exception;
95    --  Exception raised on error that is not handled locally, see above
96
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.
105
106    --------------------------------------------
107    -- Handling Semicolon Used in Place of IS --
108    --------------------------------------------
109
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:
112
113    --    procedure X (Y : Integer);
114    --       Q : Integer;
115    --    begin
116    --       ...
117    --    end;
118
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.
123
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.
132
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.
137
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.
142
143    SIS_Entry_Active : Boolean := False;
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.
147
148    SIS_Labl : Node_Id;
149    --  Subprogram designator
150
151    SIS_Sloc : Source_Ptr;
152    --  Source location of FUNCTION/PROCEDURE keyword
153
154    SIS_Ecol : Column_Number;
155    --  Column number of FUNCTION/PROCEDURE keyword
156
157    SIS_Semicolon_Sloc : Source_Ptr;
158    --  Source location of semicolon at end of subprogram declaration
159
160    SIS_Declaration_Node : Node_Id;
161    --  Pointer to tree node for subprogram declaration
162
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.
167
168    --  Five things can happen to an active SIS entry
169
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.
174
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.
179
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.
186
187    --   4. We encounter a valid pragma INTERFACE or IMPORT that effectively
188    --   supplies the missing body. In this case we reset the entry.
189
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.
194
195    ----------------------------------------------------
196    -- Handling of Reserved Words Used as Identifiers --
197    ----------------------------------------------------
198
199    --  Note: throughout the parser, the terms reserved word and keyword
200    --  are used interchangably to refer to the same set of reserved
201    --  keywords (including until, protected, etc).
202
203    --  If a reserved word is used in place of an identifier, the parser
204    --  where possible tries to recover gracefully. In particular, if the
205    --  keyword is clearly spelled using identifier casing, e.g. Until in
206    --  a source program using mixed case identifiers and lower case keywords,
207    --  then the keyword is treated as an identifier if it appears in a place
208    --  where an identifier is required.
209
210    --  The situation is more complex if the keyword is spelled with normal
211    --  keyword casing. In this case, the parser is more reluctant to
212    --  consider it to be intended as an identifier, unless it has some
213    --  further confirmation.
214
215    --  In the case of an identifier appearing in the identifier list of a
216    --  declaration, the appearence of a comma or colon right after the
217    --  keyword on the same line is taken as confirmation. For an enumeration
218    --  literal, a comma or right paren right after the identifier is also
219    --  treated as adequate confirmation.
220
221    --  The following type is used in calls to Is_Reserved_Identifier and
222    --  also to P_Defining_Identifier and P_Identifier. The default for all
223    --  these functins is that reserved words in reserved word case are not
224    --  considered to be reserved identifiers. The Id_Check value indicates
225    --  tokens, which if they appear immediately after the identifier, are
226    --  taken as confirming that the use of an identifier was expected
227
228    type Id_Check is
229      (None,
230       --  Default, no special token test
231
232       C_Comma_Right_Paren,
233       --  Consider as identifier if followed by comma or right paren
234
235       C_Comma_Colon,
236       --  Consider as identifier if followed by comma or colon
237
238       C_Do,
239       --  Consider as identifier if followed by DO
240
241       C_Dot,
242       --  Consider as identifier if followed by period
243
244       C_Greater_Greater,
245       --  Consider as identifier if followed by >>
246
247       C_In,
248       --  Consider as identifier if followed by IN
249
250       C_Is,
251       --  Consider as identifier if followed by IS
252
253       C_Left_Paren_Semicolon,
254       --  Consider as identifier if followed by left paren or semicolon
255
256       C_Use,
257       --  Consider as identifier if followed by USE
258
259       C_Vertical_Bar_Arrow);
260       --  Consider as identifier if followed by | or =>
261
262    --------------------------------------------
263    -- Handling IS Used in Place of Semicolon --
264    --------------------------------------------
265
266    --  This is a somewhat trickier situation, and we can't catch it in all
267    --  cases, but we do our best to detect common situations resulting from
268    --  a "cut and paste" operation which forgets to change the IS to semicolon.
269    --  Consider the following example:
270
271    --    package body X is
272    --      procedure A;
273    --      procedure B is
274    --      procedure C;
275    --      ...
276    --      procedure D is
277    --      begin
278    --         ...
279    --      end;
280    --    begin
281    --      ...
282    --    end;
283
284    --  The trouble is that the section of text from PROCEDURE B through END;
285    --  consitutes a valid procedure body, and the danger is that we find out
286    --  far too late that something is wrong (indeed most compilers will behave
287    --  uncomfortably on the above example).
288
289    --  We have two approaches to helping to control this situation. First we
290    --  make every attempt to avoid swallowing the last END; if we can be
291    --  sure that some error will result from doing so. In particular, we won't
292    --  accept the END; unless it is exactly correct (in particular it must not
293    --  have incorrect name tokens), and we won't accept it if it is immediately
294    --  followed by end of file, WITH or SEPARATE (all tokens that unmistakeably
295    --  signal the start of a compilation unit, and which therefore allow us to
296    --  reserve the END; for the outer level.) For more details on this aspect
297    --  of the handling, see package Par.Endh.
298
299    --  If we can avoid eating up the END; then the result in the absense of
300    --  any additional steps would be to post a missing END referring back to
301    --  the subprogram with the bogus IS. Similarly, if the enclosing package
302    --  has no BEGIN, then the result is a missing BEGIN message, which again
303    --  refers back to the subprogram header.
304
305    --  Such an error message is not too bad (it's already a big improvement
306    --  over what many parsers do), but it's not ideal, because the declarations
307    --  following the IS have been absorbed into the wrong scope. In the above
308    --  case, this could result for example in a bogus complaint that the body
309    --  of D was missing from the package.
310
311    --  To catch at least some of these cases, we take the following additional
312    --  steps. First, a subprogram body is marked as having a suspicious IS if
313    --  the declaration line is followed by a line which starts with a symbol
314    --  that can start a declaration in the same column, or to the left of the
315    --  column in which the FUNCTION or PROCEDURE starts (normal style is to
316    --  indent any declarations which really belong a subprogram). If such a
317    --  subprogram encounters a missing BEGIN or missing END, then we decide
318    --  that the IS should have been a semicolon, and the subprogram body node
319    --  is marked (by setting the Bad_Is_Detected flag true. Note that we do
320    --  not do this for library level procedures, only for nested procedures,
321    --  since for library level procedures, we must have a body.
322
323    --  The processing for a declarative part checks to see if the last
324    --  declaration scanned is marked in this way, and if it is, the tree
325    --  is modified to reflect the IS being interpreted as a semicolon.
326
327    ---------------------------------------------------
328    -- Parser Type Definitions and Control Variables --
329    ---------------------------------------------------
330
331    --  The following variable and associated type declaration are used by the
332    --  expression parsing routines to return more detailed information about
333    --  the categorization of a parsed expression.
334
335    type Expr_Form_Type is (
336       EF_Simple_Name,  -- Simple name, i.e. possibly qualified identifier
337       EF_Name,         -- Simple expression which could also be a name
338       EF_Simple,       -- Simple expression which is not call or name
339       EF_Range_Attr,   -- Range attribute reference
340       EF_Non_Simple);  -- Expression that is not a simple expression
341
342    Expr_Form : Expr_Form_Type;
343
344    --  The following type is used for calls to P_Subprogram, P_Package, P_Task,
345    --  P_Protected to indicate which of several possibilities is acceptable.
346
347    type Pf_Rec is record
348       Spcn : Boolean;                  -- True if specification OK
349       Decl : Boolean;                  -- True if declaration OK
350       Gins : Boolean;                  -- True if generic instantiation OK
351       Pbod : Boolean;                  -- True if proper body OK
352       Rnam : Boolean;                  -- True if renaming declaration OK
353       Stub : Boolean;                  -- True if body stub OK
354       Fil1 : Boolean;                  -- Filler to fill to 8 bits
355       Fil2 : Boolean;                  -- Filler to fill to 8 bits
356    end record;
357    pragma Pack (Pf_Rec);
358
359    function T return Boolean renames True;
360    function F return Boolean renames False;
361
362    Pf_Decl_Gins_Pbod_Rnam_Stub : constant Pf_Rec :=
363                                              Pf_Rec'(F, T, T, T, T, T, F, F);
364    Pf_Decl                     : constant Pf_Rec :=
365                                              Pf_Rec'(F, T, F, F, F, F, F, F);
366    Pf_Decl_Gins_Pbod_Rnam      : constant Pf_Rec :=
367                                              Pf_Rec'(F, T, T, T, T, F, F, F);
368    Pf_Decl_Pbod                : constant Pf_Rec :=
369                                              Pf_Rec'(F, T, F, T, F, F, F, F);
370    Pf_Pbod                     : constant Pf_Rec :=
371                                              Pf_Rec'(F, F, F, T, F, F, F, F);
372    Pf_Spcn                     : constant Pf_Rec :=
373                                              Pf_Rec'(T, F, F, F, F, F, F, F);
374    --  The above are the only allowed values of Pf_Rec arguments
375
376    type SS_Rec is record
377       Eftm : Boolean;      -- ELSIF can terminate sequence
378       Eltm : Boolean;      -- ELSE can terminate sequence
379       Extm : Boolean;      -- EXCEPTION can terminate sequence
380       Ortm : Boolean;      -- OR can terminate sequence
381       Sreq : Boolean;      -- at least one statement required
382       Tatm : Boolean;      -- THEN ABORT can terminate sequence
383       Whtm : Boolean;      -- WHEN can terminate sequence
384       Unco : Boolean;      -- Unconditional terminate after one statement
385    end record;
386    pragma Pack (SS_Rec);
387
388    SS_Eftm_Eltm_Sreq : constant SS_Rec := SS_Rec'(T, T, F, F, T, F, F, F);
389    SS_Eltm_Ortm_Tatm : constant SS_Rec := SS_Rec'(F, T, F, T, F, T, F, F);
390    SS_Extm_Sreq      : constant SS_Rec := SS_Rec'(F, F, T, F, T, F, F, F);
391    SS_None           : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, F);
392    SS_Ortm_Sreq      : constant SS_Rec := SS_Rec'(F, F, F, T, T, F, F, F);
393    SS_Sreq           : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, F, F);
394    SS_Sreq_Whtm      : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, T, F);
395    SS_Whtm           : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, T, F);
396    SS_Unco           : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, T);
397
398    Goto_List : Elist_Id;
399    --  List of goto nodes appearing in the current compilation. Used to
400    --  recognize natural loops and convert them into bona fide loops for
401    --  optimization purposes.
402
403    Label_List : Elist_Id;
404    --  List of label nodes for labels appearing in the current compilation.
405    --  Used by Par.Labl to construct the corresponding implicit declarations.
406
407    -----------------
408    -- Scope Table --
409    -----------------
410
411    --  The scope table, also referred to as the scope stack, is used to
412    --  record the current scope context. It is organized as a stack, with
413    --  inner nested entries corresponding to higher entries on the stack.
414    --  An entry is made when the parser encounters the opening of a nested
415    --  construct (such as a record, task, package etc.), and then package
416    --  Par.Endh uses this stack to deal with END lines (including properly
417    --  dealing with END nesting errors).
418
419    type SS_End_Type is
420    --  Type of end entry required for this scope. The last two entries are
421    --  used only in the subprogram body case to mark the case of a suspicious
422    --  IS, or a bad IS (i.e. suspicions confirmed by missing BEGIN or END).
423    --  See separate section on dealing with IS used in place of semicolon.
424    --  Note that for many purposes E_Name, E_Suspicious_Is and E_Bad_Is are
425    --  treated the same (E_Suspicious_Is and E_Bad_Is are simply special cases
426    --  of E_Name). They are placed at the end of the enumeration so that a
427    --  test for >= E_Name catches all three cases efficiently.
428
429       (E_Dummy,           -- dummy entry at outer level
430        E_Case,            -- END CASE;
431        E_If,              -- END IF;
432        E_Loop,            -- END LOOP;
433        E_Record,          -- END RECORD;
434        E_Return,          -- END RETURN;
435        E_Select,          -- END SELECT;
436        E_Name,            -- END [name];
437        E_Suspicious_Is,   -- END [name]; (case of suspicious IS)
438        E_Bad_Is);         -- END [name]; (case of bad IS)
439
440    --  The following describes a single entry in the scope table
441
442    type Scope_Table_Entry is record
443       Etyp : SS_End_Type;
444       --  Type of end entry, as per above description
445
446       Lreq : Boolean;
447       --  A flag indicating whether the label, if present, is required to
448       --  appear on the end line. It is referenced only in the case of
449       --  Etyp = E_Name or E_Suspicious_Is where the name may or may not be
450       --  required (yes for labeled block, no in other cases). Note that for
451       --  all cases except begin, the question of whether a label is required
452       --  can be determined from the other fields (for loop, it is required if
453       --  it is present, and for the other constructs it is never required or
454       --  allowed).
455
456       Ecol : Column_Number;
457       --  Contains the absolute column number (with tabs expanded) of the
458       --  the expected column of the end assuming normal Ada indentation
459       --  usage. If the RM_Column_Check mode is set, this value is used for
460       --  generating error messages about indentation. Otherwise it is used
461       --  only to control heuristic error recovery actions.
462
463       Labl : Node_Id;
464       --  This field is used only for the LOOP and BEGIN cases, and is the
465       --  Node_Id value of the label name. For all cases except child units,
466       --  this value is an entity whose Chars field contains the name pointer
467       --  that identifies the label uniquely. For the child unit case the Labl
468       --  field references an N_Defining_Program_Unit_Name node for the name.
469       --  For cases other than LOOP or BEGIN, the Label field is set to Error,
470       --  indicating that it is an error to have a label on the end line.
471       --  (this is really a misuse of Error since there is no Error ???)
472
473       Decl : List_Id;
474       --  Points to the list of declarations (i.e. the declarative part)
475       --  associated with this construct. It is set only in the END [name]
476       --  cases, and is set to No_List for all other cases which do not have a
477       --  declarative unit associated with them. This is used for determining
478       --  the proper location for implicit label declarations.
479
480       Node : Node_Id;
481       --  Empty except in the case of entries for IF and CASE statements,
482       --  in which case it contains the N_If_Statement or N_Case_Statement
483       --  node. This is used for setting the End_Span field.
484
485       Sloc : Source_Ptr;
486       --  Source location of the opening token of the construct. This is
487       --  used to refer back to this line in error messages (such as missing
488       --  or incorrect end lines). The Sloc field is not used, and is not set,
489       --  if a label is present (the Labl field provides the text name of the
490       --  label in this case, which is fine for error messages).
491
492       S_Is : Source_Ptr;
493       --  S_Is is relevant only if Etyp is set to E_Suspicious_Is or
494       --  E_Bad_Is. It records the location of the IS that is considered
495       --  to be suspicious.
496
497       Junk : Boolean;
498       --  A boolean flag that is set true if the opening entry is the dubious
499       --  result of some prior error, e.g. a record entry where the record
500       --  keyword was missing. It is used to suppress the issuing of a
501       --  corresponding junk complaint about the end line (we do not want
502       --  to complain about a missing end record when there was no record).
503    end record;
504
505    --  The following declares the scope table itself. The Last field is the
506    --  stack pointer, so that Scope.Table (Scope.Last) is the top entry. The
507    --  oldest entry, at Scope_Stack (0), is a dummy entry with Etyp set to
508    --  E_Dummy, and the other fields undefined. This dummy entry ensures that
509    --  Scope_Stack (Scope_Stack_Ptr).Etyp can always be tested, and that the
510    --  scope stack pointer is always in range.
511
512    package Scope is new Table.Table (
513      Table_Component_Type => Scope_Table_Entry,
514      Table_Index_Type     => Int,
515      Table_Low_Bound      => 0,
516      Table_Initial        => 50,
517      Table_Increment      => 100,
518      Table_Name           => "Scope");
519
520    ---------------------------------
521    -- Parsing Routines by Chapter --
522    ---------------------------------
523
524    --  Uncommented declarations in this section simply parse the construct
525    --  corresponding to their name, and return an ID value for the Node or
526    --  List that is created.
527
528    -------------
529    -- Par.Ch2 --
530    -------------
531
532    package Ch2 is
533       function P_Pragma (Skipping : Boolean := False) return Node_Id;
534       --  Scan out a pragma. If Skipping is True, then the caller is skipping
535       --  the pragma in the context of illegal placement (this is used to avoid
536       --  some junk cascaded messages).
537
538       function P_Identifier (C : Id_Check := None) return Node_Id;
539       --  Scans out an identifier. The parameter C determines the treatment
540       --  of reserved identifiers. See declaration of Id_Check for details.
541
542       function P_Pragmas_Opt return List_Id;
543       --  This function scans for a sequence of pragmas in other than a
544       --  declaration sequence or statement sequence context. All pragmas
545       --  can appear except pragmas Assert and Debug, which are only allowed
546       --  in a declaration or statement sequence context.
547
548       procedure P_Pragmas_Misplaced;
549       --  Skips misplaced pragmas with a complaint
550
551       procedure P_Pragmas_Opt (List : List_Id);
552       --  Parses optional pragmas and appends them to the List
553    end Ch2;
554
555    -------------
556    -- Par.Ch3 --
557    -------------
558
559    package Ch3 is
560       Missing_Begin_Msg : Error_Msg_Id;
561       --  This variable is set by a call to P_Declarative_Part. Normally it
562       --  is set to No_Error_Msg, indicating that no special processing is
563       --  required by the caller. The special case arises when a statement
564       --  is found in the sequence of declarations. In this case the Id of
565       --  the message issued ("declaration expected") is preserved in this
566       --  variable, then the caller can change it to an appropriate missing
567       --  begin message if indeed the BEGIN is missing.
568
569       function P_Array_Type_Definition                return Node_Id;
570       function P_Basic_Declarative_Items              return List_Id;
571       function P_Constraint_Opt                       return Node_Id;
572       function P_Declarative_Part                     return List_Id;
573       function P_Discrete_Choice_List                 return List_Id;
574       function P_Discrete_Range                       return Node_Id;
575       function P_Discrete_Subtype_Definition          return Node_Id;
576       function P_Known_Discriminant_Part_Opt          return List_Id;
577       function P_Signed_Integer_Type_Definition       return Node_Id;
578       function P_Range                                return Node_Id;
579       function P_Range_Or_Subtype_Mark                return Node_Id;
580       function P_Range_Constraint                     return Node_Id;
581       function P_Record_Definition                    return Node_Id;
582       function P_Subtype_Mark                         return Node_Id;
583       function P_Subtype_Mark_Resync                  return Node_Id;
584       function P_Unknown_Discriminant_Part_Opt        return Boolean;
585
586       function P_Access_Definition
587         (Null_Exclusion_Present : Boolean) return Node_Id;
588       --  Ada 2005 (AI-231/AI-254): The caller parses the null-exclusion part
589       --  and indicates if it was present
590
591       function P_Access_Type_Definition
592         (Header_Already_Parsed : Boolean := False) return Node_Id;
593       --  Ada 2005 (AI-254): The formal is used to indicate if the caller has
594       --  parsed the null_exclusion part. In this case the caller has also
595       --  removed the ACCESS token
596
597       procedure P_Component_Items (Decls : List_Id);
598       --  Scan out one or more component items and append them to the
599       --  given list. Only scans out more than one declaration in the
600       --  case where the source has a single declaration with multiple
601       --  defining identifiers.
602
603       function P_Defining_Identifier (C : Id_Check := None) return Node_Id;
604       --  Scan out a defining identifier. The parameter C controls the
605       --  treatment of errors in case a reserved word is scanned. See the
606       --  declaration of this type for details.
607
608       function P_Interface_Type_Definition
609         (Abstract_Present : Boolean) return Node_Id;
610       --  Ada 2005 (AI-251): Parse the interface type definition part. Abstract
611       --  Present indicates if the reserved word "abstract" has been previously
612       --  found. It is used to report an error message because interface types
613       --  are by definition abstract tagged. We generate a record_definition
614       --  node if the list of interfaces is empty; otherwise we generate a
615       --  derived_type_definition node (the first interface in this list is the
616       --  ancestor interface).
617
618       function P_Null_Exclusion
619         (Allow_Anonymous_In_95 : Boolean := False) return Boolean;
620       --  Ada 2005 (AI-231): Parse the null-excluding part. A True result
621       --  indicates that the null-excluding part was present.
622       --  Allow_Anonymous_In_95 is True if we are in a context that allows
623       --  anonymous access types in Ada 95, in which case "not null" is legal
624       --  if it precedes "access".
625
626       function P_Subtype_Indication
627         (Not_Null_Present : Boolean := False) return Node_Id;
628       --  Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
629       --  null-excluding part has been scanned out and it was present.
630
631       function Init_Expr_Opt (P : Boolean := False) return Node_Id;
632       --  If an initialization expression is present (:= expression), then
633       --  it is scanned out and returned, otherwise Empty is returned if no
634       --  initialization expression is present. This procedure also handles
635       --  certain common error cases cleanly. The parameter P indicates if
636       --  a right paren can follow the expression (default = no right paren
637       --  allowed).
638
639       procedure Skip_Declaration (S : List_Id);
640       --  Used when scanning statements to skip past a mispaced declaration
641       --  The declaration is scanned out and appended to the given list.
642       --  Token is known to be a declaration token (in Token_Class_Declk)
643       --  on entry, so there definition is a declaration to be scanned.
644
645       function P_Subtype_Indication
646         (Subtype_Mark     : Node_Id;
647          Not_Null_Present : Boolean := False) return Node_Id;
648       --  This version of P_Subtype_Indication is called when the caller has
649       --  already scanned out the subtype mark which is passed as a parameter.
650       --  Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
651       --  null-excluding part has been scanned out and it was present.
652
653       function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id;
654       --  Parse a subtype mark attribute. The caller has already parsed the
655       --  subtype mark, which is passed in as the argument, and has checked
656       --  that the current token is apostrophe.
657    end Ch3;
658
659    -------------
660    -- Par.Ch4 --
661    -------------
662
663    package Ch4 is
664       function P_Aggregate                            return Node_Id;
665       function P_Expression                           return Node_Id;
666       function P_Expression_No_Right_Paren            return Node_Id;
667       function P_Expression_Or_Range_Attribute        return Node_Id;
668       function P_Function_Name                        return Node_Id;
669       function P_Name                                 return Node_Id;
670       function P_Qualified_Simple_Name                return Node_Id;
671       function P_Qualified_Simple_Name_Resync         return Node_Id;
672       function P_Simple_Expression                    return Node_Id;
673       function P_Simple_Expression_Or_Range_Attribute return Node_Id;
674
675       function P_Qualified_Expression
676         (Subtype_Mark : Node_Id)
677          return         Node_Id;
678       --  This routine scans out a qualified expression when the caller has
679       --  already scanned out the name and apostrophe of the construct.
680    end Ch4;
681
682    -------------
683    -- Par.Ch5 --
684    -------------
685
686    package Ch5 is
687       function P_Statement_Name (Name_Node : Node_Id) return Node_Id;
688       --  Given a node representing a name (which is a call), converts it
689       --  to the syntactically corresponding procedure call statement.
690
691       function P_Sequence_Of_Statements (SS_Flags : SS_Rec) return List_Id;
692       --  The argument indicates the acceptable termination tokens.
693       --  See body in Par.Ch5 for details of the use of this parameter.
694
695       procedure Parse_Decls_Begin_End (Parent : Node_Id);
696       --  Parses declarations and handled statement sequence, setting
697       --  fields of Parent node appropriately.
698    end Ch5;
699
700    -------------
701    -- Par.Ch6 --
702    -------------
703
704    package Ch6 is
705       function P_Designator                           return Node_Id;
706       function P_Defining_Program_Unit_Name           return Node_Id;
707       function P_Formal_Part                          return List_Id;
708       function P_Parameter_Profile                    return List_Id;
709       function P_Return_Statement                     return Node_Id;
710       function P_Subprogram_Specification             return Node_Id;
711
712       procedure P_Mode (Node : Node_Id);
713       --  Sets In_Present and/or Out_Present flags in Node scanning past
714       --  IN, OUT or IN OUT tokens in the source.
715
716       function P_Subprogram (Pf_Flags : Pf_Rec)       return Node_Id;
717       --  Scans out any construct starting with either of the keywords
718       --  PROCEDURE or FUNCTION. The parameter indicates which possible
719       --  possible kinds of construct (body, spec, instantiation etc.)
720       --  are permissible in the current context.
721    end Ch6;
722
723    -------------
724    -- Par.Ch7 --
725    -------------
726
727    package Ch7 is
728       function P_Package (Pf_Flags : Pf_Rec) return Node_Id;
729       --  Scans out any construct starting with the keyword PACKAGE. The
730       --  parameter indicates which possible kinds of construct (body, spec,
731       --  instantiation etc.) are permissible in the current context.
732    end Ch7;
733
734    -------------
735    -- Par.Ch8 --
736    -------------
737
738    package Ch8 is
739       function P_Use_Clause                           return Node_Id;
740    end Ch8;
741
742    -------------
743    -- Par.Ch9 --
744    -------------
745
746    package Ch9 is
747       function P_Abort_Statement                      return Node_Id;
748       function P_Abortable_Part                       return Node_Id;
749       function P_Accept_Statement                     return Node_Id;
750       function P_Delay_Statement                      return Node_Id;
751       function P_Entry_Body                           return Node_Id;
752       function P_Protected                            return Node_Id;
753       function P_Requeue_Statement                    return Node_Id;
754       function P_Select_Statement                     return Node_Id;
755       function P_Task                                 return Node_Id;
756       function P_Terminate_Alternative                return Node_Id;
757    end Ch9;
758
759    --------------
760    -- Par.Ch10 --
761    --------------
762
763    package Ch10 is
764       function P_Compilation_Unit                     return Node_Id;
765       --  Note: this function scans a single compilation unit, and
766       --  checks that an end of file follows this unit, diagnosing
767       --  any unexpected input as an error, and then skipping it, so
768       --  that Token is set to Tok_EOF on return. An exception is in
769       --  syntax-only mode, where multiple compilation units are
770       --  permitted. In this case, P_Compilation_Unit does not check
771       --  for end of file and there may be more compilation units to
772       --  scan. The caller can uniquely detect this situation by the
773       --  fact that Token is not set to Tok_EOF on return.
774       --
775       --  The Ignore parameter is normally set False. It is set True
776       --  in multiple unit per file mode if we are skipping past a unit
777       --  that we are not interested in.
778    end Ch10;
779
780    --------------
781    -- Par.Ch11 --
782    --------------
783
784    package Ch11 is
785       function P_Handled_Sequence_Of_Statements       return Node_Id;
786       function P_Raise_Statement                      return Node_Id;
787
788       function Parse_Exception_Handlers               return List_Id;
789       --  Parses the partial construct EXCEPTION followed by a list of
790       --  exception handlers which appears in a number of productions,
791       --  and returns the list of exception handlers.
792    end Ch11;
793
794    --------------
795    -- Par.Ch12 --
796    --------------
797
798    package Ch12 is
799       function P_Generic                              return Node_Id;
800       function P_Generic_Actual_Part_Opt              return List_Id;
801    end Ch12;
802
803    --------------
804    -- Par.Ch13 --
805    --------------
806
807    package Ch13 is
808       function P_Representation_Clause                return Node_Id;
809
810       function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id;
811       --  Function to parse a code statement. The caller has scanned out
812       --  the name to be used as the subtype mark (but has not checked that
813       --  it is suitable for use as a subtype mark, i.e. is either an
814       --  identifier or a selected component). The current token is an
815       --  apostrophe and the following token is either a left paren or
816       --  RANGE (the latter being an error to be caught by P_Code_Statement.
817    end Ch13;
818
819    --  Note: the parsing for annexe J features (i.e. obsolescent features)
820    --  is found in the logical section where these features would be if
821    --  they were not obsolescent. In particular:
822
823    --    Delta constraint is parsed by P_Delta_Constraint (3.5.9)
824    --    At clause is parsed by P_At_Clause (13.1)
825    --    Mod clause is parsed by P_Mod_Clause (13.5.1)
826
827    --------------
828    -- Par.Endh --
829    --------------
830
831    --  Routines for handling end lines, including scope recovery
832
833    package Endh is
834       function Check_End return Boolean;
835       --  Called when an end sequence is required. In the absence of an error
836       --  situation, Token contains Tok_End on entry, but in a missing end
837       --  case, this may not be the case. Pop_End_Context is used to determine
838       --  the appropriate action to be taken. The returned result is True if
839       --  an End sequence was encountered and False if no End sequence was
840       --  present. This occurs if the END keyword encountered was determined
841       --  to be improper and deleted (i.e. Pop_End_Context set End_Action to
842       --  Skip_And_Reject). Note that the END sequence includes a semicolon,
843       --  except in the case of END RECORD, where a semicolon follows the END
844       --  RECORD, but is not part of the record type definition itself.
845
846       procedure End_Skip;
847       --  Skip past an end sequence. On entry Token contains Tok_End, and we
848       --  we know that the end sequence is syntactically incorrect, and that
849       --  an appropriate error message has already been posted. The mission
850       --  is simply to position the scan pointer to be the best guess of the
851       --  position after the end sequence. We do not issue any additional
852       --  error messages while carrying this out.
853
854       procedure End_Statements (Parent : Node_Id := Empty);
855       --  Called when an end is required or expected to terminate a sequence
856       --  of statements. The caller has already made an appropriate entry in
857       --  the Scope.Table to describe the expected form of the end. This can
858       --  only be used in cases where the only appropriate terminator is end.
859       --  If Parent is non-empty, then if a correct END line is encountered,
860       --  the End_Label field of Parent is set appropriately.
861    end Endh;
862
863    --------------
864    -- Par.Sync --
865    --------------
866
867    --  These procedures are used to resynchronize after errors. Following an
868    --  error which is not immediately locally recoverable, the exception
869    --  Error_Resync is raised. The handler for Error_Resync typically calls
870    --  one of these recovery procedures to resynchronize the source position
871    --  to a point from which parsing can be restarted.
872
873    --  Note: these procedures output an information message that tokens are
874    --  being skipped, but this message is output only if the option for
875    --  Multiple_Errors_Per_Line is set in Options.
876
877    package Sync is
878       procedure Resync_Choice;
879       --  Used if an error occurs scanning a choice. The scan pointer is
880       --  advanced to the next vertical bar, arrow, or semicolon, whichever
881       --  comes first. We also quit if we encounter an end of file.
882
883       procedure Resync_Expression;
884       --  Used if an error is detected during the parsing of an expression.
885       --  It skips past tokens until either a token which cannot be part of
886       --  an expression is encountered (an expression terminator), or if a
887       --  comma or right parenthesis or vertical bar is encountered at the
888       --  current parenthesis level (a parenthesis level counter is maintained
889       --  to carry out this test).
890
891       procedure Resync_Past_Semicolon;
892       --  Used if an error occurs while scanning a sequence of declarations.
893       --  The scan pointer is positioned past the next semicolon and the scan
894       --  resumes. The scan is also resumed on encountering a token which
895       --  starts a declaration (but we make sure to skip at least one token
896       --  in this case, to avoid getting stuck in a loop).
897
898       procedure Resync_To_Semicolon;
899       --  Similar to Resync_Past_Semicolon, except that the scan pointer is
900       --  left pointing to the semicolon rather than past it.
901
902       procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then;
903       --  Used if an error occurs while scanning a sequence of statements.
904       --  The scan pointer is positioned past the next semicolon, or to the
905       --  next occurrence of either then or loop, and the scan resumes.
906
907       procedure Resync_To_When;
908       --  Used when an error occurs scanning an entry index specification.
909       --  The scan pointer is positioned to the next WHEN (or to IS or
910       --  semicolon if either of these appear before WHEN, indicating
911       --  another error has occurred).
912
913       procedure Resync_Semicolon_List;
914       --  Used if an error occurs while scanning a parenthesized list of items
915       --  separated by semicolons. The scan pointer is advanced to the next
916       --  semicolon or right parenthesis at the outer parenthesis level, or
917       --  to the next is or RETURN keyword occurence, whichever comes first.
918
919       procedure Resync_Cunit;
920       --  Synchronize to next token which could be the start of a compilation
921       --  unit, or to the end of file token.
922    end Sync;
923
924    --------------
925    -- Par.Tchk --
926    --------------
927
928    --  Routines to check for expected tokens
929
930    package Tchk is
931
932       --  Procedures with names of the form T_xxx, where Tok_xxx is a token
933       --  name, check that the current token matches the required token, and
934       --  if so, scan past it. If not, an error is issued indicating that
935       --  the required token is not present (xxx expected). In most cases, the
936       --  scan pointer is not moved in the not-found case, but there are some
937       --  exceptions to this, see for example T_Id, where the scan pointer is
938       --  moved across a literal appearing where an identifier is expected.
939
940       procedure T_Abort;
941       procedure T_Arrow;
942       procedure T_At;
943       procedure T_Body;
944       procedure T_Box;
945       procedure T_Colon;
946       procedure T_Colon_Equal;
947       procedure T_Comma;
948       procedure T_Dot_Dot;
949       procedure T_For;
950       procedure T_Greater_Greater;
951       procedure T_Identifier;
952       procedure T_In;
953       procedure T_Is;
954       procedure T_Left_Paren;
955       procedure T_Loop;
956       procedure T_Mod;
957       procedure T_New;
958       procedure T_Of;
959       procedure T_Or;
960       procedure T_Private;
961       procedure T_Range;
962       procedure T_Record;
963       procedure T_Right_Paren;
964       procedure T_Semicolon;
965       procedure T_Then;
966       procedure T_Type;
967       procedure T_Use;
968       procedure T_When;
969       procedure T_With;
970
971       --  Procedures having names of the form TF_xxx, where Tok_xxx is a token
972       --  name check that the current token matches the required token, and
973       --  if so, scan past it. If not, an error message is issued indicating
974       --  that the required token is not present (xxx expected).
975
976       --  If the missing token is at the end of the line, then control returns
977       --  immediately after posting the message. If there are remaining tokens
978       --  on the current line, a search is conducted to see if the token
979       --  appears later on the current line, as follows:
980
981       --  A call to Scan_Save is issued and a forward search for the token
982       --  is carried out. If the token is found on the current line before a
983       --  semicolon, then it is scanned out and the scan continues from that
984       --  point. If not the scan is restored to the point where it was missing.
985
986       procedure TF_Arrow;
987       procedure TF_Is;
988       procedure TF_Loop;
989       procedure TF_Return;
990       procedure TF_Semicolon;
991       procedure TF_Then;
992       procedure TF_Use;
993
994       --  Procedures with names of the form U_xxx, where Tok_xxx is a token
995       --  name, are just like the corresponding T_xxx procedures except that
996       --  an error message, if given, is unconditional.
997
998       procedure U_Left_Paren;
999       procedure U_Right_Paren;
1000    end Tchk;
1001
1002    --------------
1003    -- Par.Util --
1004    --------------
1005
1006    package Util is
1007       function Bad_Spelling_Of (T : Token_Type) return Boolean;
1008       --  This function is called in an error situation. It checks if the
1009       --  current token is an identifier whose name is a plausible bad
1010       --  spelling of the given keyword token, and if so, issues an error
1011       --  message, sets Token from T, and returns True. Otherwise Token is
1012       --  unchanged, and False is returned.
1013
1014       procedure Check_Bad_Layout;
1015       --  Check for bad indentation in RM checking mode. Used for statements
1016       --  and declarations. Checks if current token is at start of line and
1017       --  is exdented from the current expected end column, and if so an
1018       --  error message is generated.
1019
1020       procedure Check_Misspelling_Of (T : Token_Type);
1021       pragma Inline (Check_Misspelling_Of);
1022       --  This is similar to the function above, except that it does not
1023       --  return a result. It is typically used in a situation where any
1024       --  identifier is an error, and it makes sense to simply convert it
1025       --  to the given token if it is a plausible misspelling of it.
1026
1027       procedure Check_95_Keyword (Token_95, Next : Token_Type);
1028       --  This routine checks if the token after the current one matches the
1029       --  Next argument. If so, the scan is backed up to the current token
1030       --  and Token_Type is changed to Token_95 after issuing an appropriate
1031       --  error message ("(Ada 83) keyword xx cannot be used"). If not,
1032       --  the scan is backed up with Token_Type unchanged. This routine
1033       --  is used to deal with an attempt to use a 95 keyword in Ada 83
1034       --  mode. The caller has typically checked that the current token,
1035       --  an identifier, matches one of the 95 keywords.
1036
1037       procedure Check_Simple_Expression (E : Node_Id);
1038       --  Given an expression E, that has just been scanned, so that Expr_Form
1039       --  is still set, outputs an error if E is a non-simple expression. E is
1040       --  not modified by this call.
1041
1042       procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id);
1043       --  Like Check_Simple_Expression, except that the error message is only
1044       --  given when operating in Ada 83 mode, and includes "in Ada 83".
1045
1046       function Check_Subtype_Mark (Mark : Node_Id) return Node_Id;
1047       --  Called to check that a node representing a name (or call) is
1048       --  suitable for a subtype mark, i.e, that it is an identifier or
1049       --  a selected component. If so, or if it is already Error, then
1050       --  it is returned unchanged. Otherwise an error message is issued
1051       --  and Error is returned.
1052
1053       procedure Check_No_Right_Paren;
1054       --  Called to check that the current token is not a right paren. If it
1055       --  is, then an error is given, and the right parenthesis is scanned out.
1056
1057       function Comma_Present return Boolean;
1058       --  Used in comma delimited lists to determine if a comma is present, or
1059       --  can reasonably be assumed to have been present (an error message is
1060       --  generated in the latter case). If True is returned, the scan has been
1061       --  positioned past the comma. If False is returned, the scan position
1062       --  is unchanged. Note that all comma-delimited lists are terminated by
1063       --  a right paren, so the only legitimate tokens when Comma_Present is
1064       --  called are right paren and comma. If some other token is found, then
1065       --  Comma_Present has the job of deciding whether it is better to pretend
1066       --  a comma was present, post a message for a missing comma and return
1067       --  True, or return False and let the caller diagnose the missing right
1068       --  parenthesis.
1069
1070       procedure Discard_Junk_Node (N : Node_Id);
1071       procedure Discard_Junk_List (L : List_Id);
1072       pragma Inline (Discard_Junk_Node);
1073       pragma Inline (Discard_Junk_List);
1074       --  These procedures do nothing at all, their effect is simply to discard
1075       --  the argument. A typical use is to skip by some junk that is not
1076       --  expected in the current context.
1077
1078       procedure Ignore (T : Token_Type);
1079       --  If current token matches T, then give an error message and skip
1080       --  past it, otherwise the call has no effect at all. T may be any
1081       --  reserved word token, or comma, left or right paren, or semicolon.
1082
1083       function Is_Reserved_Identifier (C : Id_Check := None) return Boolean;
1084       --  Test if current token is a reserved identifier. This test is based
1085       --  on the token being a keyword and being spelled in typical identifier
1086       --  style (i.e. starting with an upper case letter). The parameter C
1087       --  determines the special treatment if a reserved word is encountered
1088       --  that has the normal casing of a reserved word.
1089
1090       procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type);
1091       --  Called when the previous token is an identifier (whose Token_Node
1092       --  value is given by Prev) to check if current token is an identifier
1093       --  that can be merged with the previous one adding an underscore. The
1094       --  merge is only attempted if the following token matches Nxt. If all
1095       --  conditions are met, an error message is issued, and the merge is
1096       --  carried out, modifying the Chars field of Prev.
1097
1098       function Next_Token_Is (Tok : Token_Type) return Boolean;
1099       --  Looks at token after current one and returns True if the token type
1100       --  matches Tok. The scan is unconditionally restored on return.
1101
1102       procedure No_Constraint;
1103       --  Called in a place where no constraint is allowed, but one might
1104       --  appear due to a common error (e.g. after the type mark in a procedure
1105       --  parameter. If a constraint is present, an error message is posted,
1106       --  and the constraint is scanned and discarded.
1107
1108       procedure Push_Scope_Stack;
1109       pragma Inline (Push_Scope_Stack);
1110       --  Push a new entry onto the scope stack. Scope.Last (the stack pointer)
1111       --  is incremented. The Junk field is preinitialized to False. The caller
1112       --  is expected to fill in all remaining entries of the new new top stack
1113       --  entry at Scope.Table (Scope.Last).
1114
1115       procedure Pop_Scope_Stack;
1116       --  Pop an entry off the top of the scope stack. Scope_Last (the scope
1117       --  table stack pointer) is decremented by one. It is a fatal error to
1118       --  try to pop off the dummy entry at the bottom of the stack (i.e.
1119       --  Scope.Last must be non-zero at the time of call).
1120
1121       function Separate_Present return Boolean;
1122       --  Determines if the current token is either Tok_Separate, or an
1123       --  identifier that is a possible misspelling of "separate" followed
1124       --  by a semicolon. True is returned if so, otherwise False.
1125
1126       procedure Signal_Bad_Attribute;
1127       --  The current token is an identifier that is supposed to be an
1128       --  attribute identifier but is not. This routine posts appropriate
1129       --  error messages, including a check for a near misspelling.
1130
1131       function Token_Is_At_Start_Of_Line return Boolean;
1132       pragma Inline (Token_Is_At_Start_Of_Line);
1133       --  Determines if the current token is the first token on the line
1134
1135       function Token_Is_At_End_Of_Line return Boolean;
1136       --  Determines if the current token is the last token on the line
1137    end Util;
1138
1139    --------------
1140    -- Par.Prag --
1141    --------------
1142
1143    --  The processing for pragmas is split off from chapter 2
1144
1145    function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id;
1146    --  This function is passed a tree for a pragma that has been scanned out.
1147    --  The pragma is syntactically well formed according to the general syntax
1148    --  for pragmas and the pragma identifier is for one of the recognized
1149    --  pragmas. It performs specific syntactic checks for specific pragmas.
1150    --  The result is the input node if it is OK, or Error otherwise. The
1151    --  reason that this is separated out is to facilitate the addition
1152    --  of implementation defined pragmas. The second parameter records the
1153    --  location of the semicolon following the pragma (this is needed for
1154    --  correct processing of the List and Page pragmas). The returned value
1155    --  is a copy of Pragma_Node, or Error if an error is found. Note that
1156    --  at the point where Prag is called, the right paren ending the pragma
1157    --  has been scanned out, and except in the case of pragma Style_Checks,
1158    --  so has the following semicolon. For Style_Checks, the caller delays
1159    --  the scanning of the semicolon so that it will be scanned using the
1160    --  settings from the Style_Checks pragma preceding it.
1161
1162    --------------
1163    -- Par.Labl --
1164    --------------
1165
1166    procedure Labl;
1167    --  This procedure creates implicit label declarations for all label that
1168    --  are declared in the current unit. Note that this could conceptually
1169    --  be done at the point where the labels are declared, but it is tricky
1170    --  to do it then, since the tree is not hooked up at the point where the
1171    --  label is declared (e.g. a sequence of statements is not yet attached
1172    --  to its containing scope at the point a label in the sequence is found)
1173
1174    --------------
1175    -- Par.Load --
1176    --------------
1177
1178    procedure Load;
1179    --  This procedure loads all subsidiary units that are required by this
1180    --  unit, including with'ed units, specs for bodies, and parents for child
1181    --  units. It does not load bodies for inlined procedures and generics,
1182    --  since we don't know till semantic analysis is complete what is needed.
1183
1184    -----------
1185    -- Stubs --
1186    -----------
1187
1188    --  The package bodies can see all routines defined in all other subpackages
1189
1190    use Ch2;
1191    use Ch3;
1192    use Ch4;
1193    use Ch5;
1194    use Ch6;
1195    use Ch7;
1196    use Ch8;
1197    use Ch9;
1198    use Ch10;
1199    use Ch11;
1200    use Ch12;
1201    use Ch13;
1202
1203    use Endh;
1204    use Tchk;
1205    use Sync;
1206    use Util;
1207
1208    package body Ch2 is separate;
1209    package body Ch3 is separate;
1210    package body Ch4 is separate;
1211    package body Ch5 is separate;
1212    package body Ch6 is separate;
1213    package body Ch7 is separate;
1214    package body Ch8 is separate;
1215    package body Ch9 is separate;
1216    package body Ch10 is separate;
1217    package body Ch11 is separate;
1218    package body Ch12 is separate;
1219    package body Ch13 is separate;
1220
1221    package body Endh is separate;
1222    package body Tchk is separate;
1223    package body Sync is separate;
1224    package body Util is separate;
1225
1226    function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id
1227      is separate;
1228
1229    procedure Labl is separate;
1230    procedure Load is separate;
1231
1232 --  Start of processing for Par
1233
1234 begin
1235
1236    --  Deal with configuration pragmas case first
1237
1238    if Configuration_Pragmas then
1239       declare
1240          Pragmas : constant List_Id := Empty_List;
1241          P_Node  : Node_Id;
1242
1243       begin
1244          loop
1245             if Token = Tok_EOF then
1246                return Pragmas;
1247
1248             elsif Token /= Tok_Pragma then
1249                Error_Msg_SC ("only pragmas allowed in configuration file");
1250                return Error_List;
1251
1252             else
1253                P_Node := P_Pragma;
1254
1255                if Nkind (P_Node) = N_Pragma then
1256
1257                   --  Give error if bad pragma
1258
1259                   if not Is_Configuration_Pragma_Name (Chars (P_Node))
1260                     and then Chars (P_Node) /= Name_Source_Reference
1261                   then
1262                      if Is_Pragma_Name (Chars (P_Node)) then
1263                         Error_Msg_N
1264                           ("only configuration pragmas allowed " &
1265                            "in configuration file", P_Node);
1266                      else
1267                         Error_Msg_N
1268                           ("unrecognized pragma in configuration file",
1269                            P_Node);
1270                      end if;
1271
1272                   --  Pragma is OK config pragma, so collect it
1273
1274                   else
1275                      Append (P_Node, Pragmas);
1276                   end if;
1277                end if;
1278             end if;
1279          end loop;
1280       end;
1281
1282    --  Normal case of compilation unit
1283
1284    else
1285       Save_Opt_Config_Switches (Save_Config_Switches);
1286
1287       --  The following loop runs more than once in syntax check mode
1288       --  where we allow multiple compilation units in the same file
1289       --  and in Multiple_Unit_Per_file mode where we skip units till
1290       --  we get to the unit we want.
1291
1292       for Ucount in Pos loop
1293          Set_Opt_Config_Switches
1294            (Is_Internal_File_Name (File_Name (Current_Source_File)),
1295             Current_Source_Unit = Main_Unit);
1296
1297          --  Initialize scope table and other parser control variables
1298
1299          Compiler_State := Parsing;
1300          Scope.Init;
1301          Scope.Increment_Last;
1302          Scope.Table (0).Etyp := E_Dummy;
1303          SIS_Entry_Active := False;
1304          Last_Resync_Point := No_Location;
1305
1306          Goto_List  := New_Elmt_List;
1307          Label_List := New_Elmt_List;
1308
1309          --  If in multiple unit per file mode, skip past ignored unit
1310
1311          if Ucount < Multiple_Unit_Index then
1312
1313             --  We skip in syntax check only mode, since we don't want
1314             --  to do anything more than skip past the unit and ignore it.
1315             --  This causes processing like setting up a unit table entry
1316             --  to be skipped.
1317
1318             declare
1319                Save_Operating_Mode : constant Operating_Mode_Type :=
1320                                        Operating_Mode;
1321
1322                Save_Style_Check : constant Boolean := Style_Check;
1323
1324             begin
1325                Operating_Mode := Check_Syntax;
1326                Style_Check := False;
1327                Discard_Node (P_Compilation_Unit);
1328                Operating_Mode := Save_Operating_Mode;
1329                Style_Check := Save_Style_Check;
1330
1331                --  If we are at an end of file, and not yet at the right
1332                --  unit, then we have a fatal error. The unit is missing.
1333
1334                if Token = Tok_EOF then
1335                   Error_Msg_SC ("file has too few compilation units");
1336                   raise Unrecoverable_Error;
1337                end if;
1338             end;
1339
1340          --  Here if we are not skipping a file in multiple unit per file
1341          --  mode. Parse the unit that we are interested in. Note that in
1342          --  check syntax mode we are interested in all units in the file.
1343
1344          else
1345             declare
1346                Comp_Unit_Node : constant Node_Id := P_Compilation_Unit;
1347
1348             begin
1349                --  If parsing was successful and we are not in check syntax
1350                --  mode, check that language defined units are compiled in
1351                --  GNAT mode. For this purpose we do NOT consider renamings
1352                --  in annex J as predefined. That allows users to compile
1353                --  their own versions of these files, and in particular,
1354                --  in the VMS implementation, the DEC versions can be
1355                --  substituted for the standard Ada 95 versions. Another
1356                --  exception is System.RPC and its children. This allows
1357                --  a user to supply their own communication layer.
1358
1359                if Comp_Unit_Node /= Error
1360                  and then Operating_Mode = Generate_Code
1361                  and then Current_Source_Unit = Main_Unit
1362                  and then not GNAT_Mode
1363                then
1364                   declare
1365                      Uname : constant String :=
1366                                Get_Name_String
1367                                  (Unit_Name (Current_Source_Unit));
1368                      Name  : String (1 .. Uname'Length - 2);
1369
1370                   begin
1371                      --  Because Unit_Name includes "%s" or "%b", we need to
1372                      --  strip the last two characters to get the real unit
1373                      --  name.
1374
1375                      Name := Uname (Uname'First .. Uname'Last - 2);
1376
1377                      if Name = "ada"         or else
1378                         Name = "interfaces"  or else
1379                         Name = "system"
1380                      then
1381                         Error_Msg
1382                           ("language defined units may not be recompiled",
1383                            Sloc (Unit (Comp_Unit_Node)));
1384
1385                      elsif Name'Length > 4
1386                        and then
1387                          Name (Name'First .. Name'First + 3) = "ada."
1388                      then
1389                         Error_Msg
1390                           ("descendents of package Ada " &
1391                              "may not be compiled",
1392                            Sloc (Unit (Comp_Unit_Node)));
1393
1394                      elsif Name'Length > 11
1395                        and then
1396                          Name (Name'First .. Name'First + 10) = "interfaces."
1397                      then
1398                         Error_Msg
1399                           ("descendents of package Interfaces " &
1400                              "may not be compiled",
1401                            Sloc (Unit (Comp_Unit_Node)));
1402
1403                      elsif Name'Length > 7
1404                        and then Name (Name'First .. Name'First + 6) = "system."
1405                        and then Name /= "system.rpc"
1406                        and then
1407                          (Name'Length < 11
1408                             or else Name (Name'First .. Name'First + 10) /=
1409                                                                  "system.rpc.")
1410                      then
1411                         Error_Msg
1412                           ("descendents of package System " &
1413                              "may not be compiled",
1414                            Sloc (Unit (Comp_Unit_Node)));
1415                      end if;
1416                   end;
1417                end if;
1418             end;
1419
1420             --  All done if at end of file
1421
1422             exit when Token = Tok_EOF;
1423
1424             --  If we are not at an end of file, it means we are in syntax
1425             --  check only mode, and we keep the loop going to parse all
1426             --  remaining units in the file.
1427
1428          end if;
1429
1430          Restore_Opt_Config_Switches (Save_Config_Switches);
1431       end loop;
1432
1433       --  Now that we have completely parsed the source file, we can
1434       --  complete the source file table entry.
1435
1436       Complete_Source_File_Entry;
1437
1438       --  An internal error check, the scope stack should now be empty
1439
1440       pragma Assert (Scope.Last = 0);
1441
1442       --  Remaining steps are to create implicit label declarations and to
1443       --  load required subsidiary sources. These steps are required only
1444       --  if we are doing semantic checking.
1445
1446       if Operating_Mode /= Check_Syntax or else Debug_Flag_F then
1447          Par.Labl;
1448          Par.Load;
1449       end if;
1450
1451       --  Restore settings of switches saved on entry
1452
1453       Restore_Opt_Config_Switches (Save_Config_Switches);
1454       Set_Comes_From_Source_Default (False);
1455       return Empty_List;
1456    end if;
1457 end Par;