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