OSDN Git Service

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