OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order
28 --  by RM section rather than alphabetical.
29
30 with Sinfo.CN; use Sinfo.CN;
31
32 separate (Par)
33
34 package body Ch3 is
35
36    -----------------------
37    -- Local Subprograms --
38    -----------------------
39
40    function P_Component_List                               return Node_Id;
41    function P_Defining_Character_Literal                   return Node_Id;
42    function P_Delta_Constraint                             return Node_Id;
43    function P_Derived_Type_Def_Or_Private_Ext_Decl         return Node_Id;
44    function P_Digits_Constraint                            return Node_Id;
45    function P_Discriminant_Association                     return Node_Id;
46    function P_Enumeration_Literal_Specification            return Node_Id;
47    function P_Enumeration_Type_Definition                  return Node_Id;
48    function P_Fixed_Point_Definition                       return Node_Id;
49    function P_Floating_Point_Definition                    return Node_Id;
50    function P_Index_Or_Discriminant_Constraint             return Node_Id;
51    function P_Real_Range_Specification_Opt                 return Node_Id;
52    function P_Subtype_Declaration                          return Node_Id;
53    function P_Type_Declaration                             return Node_Id;
54    function P_Modular_Type_Definition                      return Node_Id;
55    function P_Variant                                      return Node_Id;
56    function P_Variant_Part                                 return Node_Id;
57
58    procedure P_Declarative_Items
59      (Decls   : List_Id;
60       Done    : out Boolean;
61       In_Spec : Boolean);
62    --  Scans out a single declarative item, or, in the case of a declaration
63    --  with a list of identifiers, a list of declarations, one for each of the
64    --  identifiers in the list. The declaration or declarations scanned are
65    --  appended to the given list. Done indicates whether or not there may be
66    --  additional declarative items to scan. If Done is True, then a decision
67    --  has been made that there are no more items to scan. If Done is False,
68    --  then there may be additional declarations to scan. In_Spec is true if
69    --  we are scanning a package declaration, and is used to generate an
70    --  appropriate message if a statement is encountered in such a context.
71
72    procedure P_Identifier_Declarations
73      (Decls   : List_Id;
74       Done    : out Boolean;
75       In_Spec : Boolean);
76    --  Scans out a set of declarations for an identifier or list of
77    --  identifiers, and appends them to the given list. The parameters have
78    --  the same significance as for P_Declarative_Items.
79
80    procedure Statement_When_Declaration_Expected
81      (Decls   : List_Id;
82       Done    : out Boolean;
83       In_Spec : Boolean);
84    --  Called when a statement is found at a point where a declaration was
85    --  expected. The parameters are as described for P_Declarative_Items.
86
87    procedure Set_Declaration_Expected;
88    --  Posts a "declaration expected" error messages at the start of the
89    --  current token, and if this is the first such message issued, saves
90    --  the message id in Missing_Begin_Msg, for possible later replacement.
91
92    -------------------
93    -- Init_Expr_Opt --
94    -------------------
95
96    function Init_Expr_Opt (P : Boolean := False) return Node_Id is
97    begin
98       --  For colon, assume it means := unless it is at the end of
99       --  a line, in which case guess that it means a semicolon.
100
101       if Token = Tok_Colon then
102          if Token_Is_At_End_Of_Line then
103             T_Semicolon;
104             return Empty;
105          end if;
106
107       --  Here if := or something that we will take as equivalent
108
109       elsif Token = Tok_Colon_Equal
110         or else Token = Tok_Equal
111         or else Token = Tok_Is
112       then
113          null;
114
115       --  Another possibility. If we have a literal followed by a semicolon,
116       --  we assume that we have a missing colon-equal.
117
118       elsif Token in Token_Class_Literal then
119          declare
120             Scan_State : Saved_Scan_State;
121
122          begin
123             Save_Scan_State (Scan_State);
124             Scan; -- past literal or identifier
125
126             if Token = Tok_Semicolon then
127                Restore_Scan_State (Scan_State);
128             else
129                Restore_Scan_State (Scan_State);
130                return Empty;
131             end if;
132          end;
133
134       --  Otherwise we definitely have no initialization expression
135
136       else
137          return Empty;
138       end if;
139
140       --  Merge here if we have an initialization expression
141
142       T_Colon_Equal;
143
144       if P then
145          return P_Expression;
146       else
147          return P_Expression_No_Right_Paren;
148       end if;
149    end Init_Expr_Opt;
150
151    ----------------------------
152    -- 3.1  Basic Declaration --
153    ----------------------------
154
155    --  Parsed by P_Basic_Declarative_Items (3.9)
156
157    ------------------------------
158    -- 3.1  Defining Identifier --
159    ------------------------------
160
161    --  DEFINING_IDENTIFIER ::= IDENTIFIER
162
163    --  Error recovery: can raise Error_Resync
164
165    function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
166       Ident_Node : Node_Id;
167
168    begin
169       --  Scan out the identifier. Note that this code is essentially identical
170       --  to P_Identifier, except that in the call to Scan_Reserved_Identifier
171       --  we set Force_Msg to True, since we want at least one message for each
172       --  separate declaration (but not use) of a reserved identifier.
173
174       if Token = Tok_Identifier then
175
176          --  Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
177          --  OVERRIDING, and SYNCHRONIZED are new reserved words.
178
179          if Ada_Version = Ada_95
180            and then Warn_On_Ada_2005_Compatibility
181          then
182             if Token_Name = Name_Overriding
183               or else Token_Name = Name_Synchronized
184               or else (Token_Name = Name_Interface
185                         and then Prev_Token /= Tok_Pragma)
186             then
187                Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
188             end if;
189          end if;
190
191       --  If we have a reserved identifier, manufacture an identifier with
192       --  a corresponding name after posting an appropriate error message
193
194       elsif Is_Reserved_Identifier (C) then
195          Scan_Reserved_Identifier (Force_Msg => True);
196
197       --  Otherwise we have junk that cannot be interpreted as an identifier
198
199       else
200          T_Identifier; -- to give message
201          raise Error_Resync;
202       end if;
203
204       Ident_Node := Token_Node;
205       Scan; -- past the reserved identifier
206
207       if Ident_Node /= Error then
208          Change_Identifier_To_Defining_Identifier (Ident_Node);
209       end if;
210
211       return Ident_Node;
212    end P_Defining_Identifier;
213
214    -----------------------------
215    -- 3.2.1  Type Declaration --
216    -----------------------------
217
218    --  TYPE_DECLARATION ::=
219    --    FULL_TYPE_DECLARATION
220    --  | INCOMPLETE_TYPE_DECLARATION
221    --  | PRIVATE_TYPE_DECLARATION
222    --  | PRIVATE_EXTENSION_DECLARATION
223
224    --  FULL_TYPE_DECLARATION ::=
225    --    type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION;
226    --  | CONCURRENT_TYPE_DECLARATION
227
228    --  INCOMPLETE_TYPE_DECLARATION ::=
229    --    type DEFINING_IDENTIFIER [DISCRIMINANT_PART] [is tagged];
230
231    --  PRIVATE_TYPE_DECLARATION ::=
232    --    type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
233    --      is [abstract] [tagged] [limited] private;
234
235    --  PRIVATE_EXTENSION_DECLARATION ::=
236    --    type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
237    --      [abstract] [limited | synchronized]
238    --        new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
239    --          with private;
240
241    --  TYPE_DEFINITION ::=
242    --    ENUMERATION_TYPE_DEFINITION  | INTEGER_TYPE_DEFINITION
243    --  | REAL_TYPE_DEFINITION         | ARRAY_TYPE_DEFINITION
244    --  | RECORD_TYPE_DEFINITION       | ACCESS_TYPE_DEFINITION
245    --  | DERIVED_TYPE_DEFINITION      | INTERFACE_TYPE_DEFINITION
246
247    --  INTEGER_TYPE_DEFINITION ::=
248    --    SIGNED_INTEGER_TYPE_DEFINITION
249    --    MODULAR_TYPE_DEFINITION
250
251    --  INTERFACE_TYPE_DEFINITION ::=
252    --    [limited | task | protected | synchronized ] interface
253    --      [and INTERFACE_LIST]
254
255    --  Error recovery: can raise Error_Resync
256
257    --  Note: The processing for full type declaration, incomplete type
258    --  declaration, private type declaration and type definition is
259    --  included in this function. The processing for concurrent type
260    --  declarations is NOT here, but rather in chapter 9 (i.e. this
261    --  function handles only declarations starting with TYPE).
262
263    function P_Type_Declaration return Node_Id is
264       Abstract_Present : Boolean := False;
265       Abstract_Loc     : Source_Ptr := No_Location;
266       Decl_Node        : Node_Id;
267       Discr_List       : List_Id;
268       Discr_Sloc       : Source_Ptr;
269       End_Labl         : Node_Id;
270       Ident_Node       : Node_Id;
271       Is_Derived_Iface : Boolean := False;
272       Type_Loc         : Source_Ptr;
273       Type_Start_Col   : Column_Number;
274       Unknown_Dis      : Boolean;
275
276       Typedef_Node     : Node_Id;
277       --  Normally holds type definition, except in the case of a private
278       --  extension declaration, in which case it holds the declaration itself
279
280    begin
281       Type_Loc := Token_Ptr;
282       Type_Start_Col := Start_Column;
283
284       --  If we have TYPE, then proceed ahead and scan identifier
285
286       if Token = Tok_Type then
287          Type_Token_Location := Type_Loc;
288          Scan; -- past TYPE
289          Ident_Node := P_Defining_Identifier (C_Is);
290
291       --  Otherwise this is an error case, and we may already have converted
292       --  the current token to a defining identifier, so don't do it again!
293
294       else
295          T_Type;
296
297          if Token = Tok_Identifier
298            and then Nkind (Token_Node) = N_Defining_Identifier
299          then
300             Ident_Node := Token_Node;
301             Scan; -- past defining identifier
302          else
303             Ident_Node := P_Defining_Identifier (C_Is);
304          end if;
305       end if;
306
307       Discr_Sloc := Token_Ptr;
308
309       if P_Unknown_Discriminant_Part_Opt then
310          Unknown_Dis := True;
311          Discr_List := No_List;
312       else
313          Unknown_Dis := False;
314          Discr_List := P_Known_Discriminant_Part_Opt;
315       end if;
316
317       --  Incomplete type declaration. We complete the processing for this
318       --  case here and return the resulting incomplete type declaration node
319
320       if Token = Tok_Semicolon then
321          Scan; -- past ;
322          Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
323          Set_Defining_Identifier (Decl_Node, Ident_Node);
324          Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
325          Set_Discriminant_Specifications (Decl_Node, Discr_List);
326          return Decl_Node;
327
328       else
329          Decl_Node := Empty;
330       end if;
331
332       --  Full type declaration or private type declaration, must have IS
333
334       if Token = Tok_Equal then
335          TF_Is;
336          Scan; -- past = used in place of IS
337
338       elsif Token = Tok_Renames then
339          Error_Msg_SC ("RENAMES should be IS");
340          Scan; -- past RENAMES used in place of IS
341
342       else
343          TF_Is;
344       end if;
345
346       --  First an error check, if we have two identifiers in a row, a likely
347       --  possibility is that the first of the identifiers is an incorrectly
348       --  spelled keyword.
349
350       if Token = Tok_Identifier then
351          declare
352             SS : Saved_Scan_State;
353             I2 : Boolean;
354
355          begin
356             Save_Scan_State (SS);
357             Scan; -- past initial identifier
358             I2 := (Token = Tok_Identifier);
359             Restore_Scan_State (SS);
360
361             if I2
362               and then
363                 (Bad_Spelling_Of (Tok_Abstract) or else
364                  Bad_Spelling_Of (Tok_Access)   or else
365                  Bad_Spelling_Of (Tok_Aliased)  or else
366                  Bad_Spelling_Of (Tok_Constant))
367             then
368                null;
369             end if;
370          end;
371       end if;
372
373       --  Check for misuse of Ada 95 keyword abstract in Ada 83 mode
374
375       if Token_Name = Name_Abstract then
376          Check_95_Keyword (Tok_Abstract, Tok_Tagged);
377          Check_95_Keyword (Tok_Abstract, Tok_New);
378       end if;
379
380       --  Check cases of misuse of ABSTRACT
381
382       if Token = Tok_Abstract then
383          Abstract_Present := True;
384          Abstract_Loc     := Token_Ptr;
385          Scan; -- past ABSTRACT
386
387          --  Ada 2005 (AI-419): AARM 3.4 (2/2)
388
389          if (Ada_Version < Ada_05 and then Token = Tok_Limited)
390            or else Token = Tok_Private
391            or else Token = Tok_Record
392            or else Token = Tok_Null
393          then
394             Error_Msg_AP ("TAGGED expected");
395          end if;
396       end if;
397
398       --  Check for misuse of Ada 95 keyword Tagged
399
400       if Token_Name = Name_Tagged then
401          Check_95_Keyword (Tok_Tagged, Tok_Private);
402          Check_95_Keyword (Tok_Tagged, Tok_Limited);
403          Check_95_Keyword (Tok_Tagged, Tok_Record);
404       end if;
405
406       --  Special check for misuse of Aliased
407
408       if Token = Tok_Aliased or else Token_Name = Name_Aliased then
409          Error_Msg_SC ("ALIASED not allowed in type definition");
410          Scan; -- past ALIASED
411       end if;
412
413       --  The following procesing deals with either a private type declaration
414       --  or a full type declaration. In the private type case, we build the
415       --  N_Private_Type_Declaration node, setting its Tagged_Present and
416       --  Limited_Present flags, on encountering the Private keyword, and
417       --  leave Typedef_Node set to Empty. For the full type declaration
418       --  case, Typedef_Node gets set to the type definition.
419
420       Typedef_Node := Empty;
421
422       --  Switch on token following the IS. The loop normally runs once. It
423       --  only runs more than once if an error is detected, to try again after
424       --  detecting and fixing up the error.
425
426       loop
427          case Token is
428
429             when Tok_Access |
430                  Tok_Not    => --  Ada 2005 (AI-231)
431                Typedef_Node := P_Access_Type_Definition;
432                TF_Semicolon;
433                exit;
434
435             when Tok_Array =>
436                Typedef_Node := P_Array_Type_Definition;
437                TF_Semicolon;
438                exit;
439
440             when Tok_Delta =>
441                Typedef_Node := P_Fixed_Point_Definition;
442                TF_Semicolon;
443                exit;
444
445             when Tok_Digits =>
446                Typedef_Node := P_Floating_Point_Definition;
447                TF_Semicolon;
448                exit;
449
450             when Tok_In =>
451                Ignore (Tok_In);
452
453             when Tok_Integer_Literal =>
454                T_Range;
455                Typedef_Node := P_Signed_Integer_Type_Definition;
456                TF_Semicolon;
457                exit;
458
459             when Tok_Null =>
460                Typedef_Node := P_Record_Definition;
461                TF_Semicolon;
462                exit;
463
464             when Tok_Left_Paren =>
465                Typedef_Node := P_Enumeration_Type_Definition;
466
467                End_Labl :=
468                  Make_Identifier (Token_Ptr,
469                    Chars => Chars (Ident_Node));
470                Set_Comes_From_Source (End_Labl, False);
471
472                Set_End_Label (Typedef_Node, End_Labl);
473                TF_Semicolon;
474                exit;
475
476             when Tok_Mod =>
477                Typedef_Node := P_Modular_Type_Definition;
478                TF_Semicolon;
479                exit;
480
481             when Tok_New =>
482                Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
483
484                if Nkind (Typedef_Node) = N_Derived_Type_Definition
485                  and then Present (Record_Extension_Part (Typedef_Node))
486                then
487                   End_Labl :=
488                     Make_Identifier (Token_Ptr,
489                       Chars => Chars (Ident_Node));
490                   Set_Comes_From_Source (End_Labl, False);
491
492                   Set_End_Label
493                     (Record_Extension_Part (Typedef_Node), End_Labl);
494                end if;
495
496                TF_Semicolon;
497                exit;
498
499             when Tok_Range =>
500                Typedef_Node := P_Signed_Integer_Type_Definition;
501                TF_Semicolon;
502                exit;
503
504             when Tok_Record =>
505                Typedef_Node := P_Record_Definition;
506
507                End_Labl :=
508                  Make_Identifier (Token_Ptr,
509                    Chars => Chars (Ident_Node));
510                Set_Comes_From_Source (End_Labl, False);
511
512                Set_End_Label (Typedef_Node, End_Labl);
513                TF_Semicolon;
514                exit;
515
516             when Tok_Tagged =>
517                Scan; -- past TAGGED
518
519                --  Ada 2005 (AI-326): If the words IS TAGGED appear, the type
520                --  is a tagged incomplete type.
521
522                if Ada_Version >= Ada_05
523                  and then Token = Tok_Semicolon
524                then
525                   Scan; -- past ;
526
527                   Decl_Node :=
528                     New_Node (N_Incomplete_Type_Declaration, Type_Loc);
529                   Set_Defining_Identifier           (Decl_Node, Ident_Node);
530                   Set_Tagged_Present                (Decl_Node);
531                   Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
532                   Set_Discriminant_Specifications   (Decl_Node, Discr_List);
533
534                   return Decl_Node;
535                end if;
536
537                if Token = Tok_Abstract then
538                   Error_Msg_SC ("ABSTRACT must come before TAGGED");
539                   Abstract_Present := True;
540                   Abstract_Loc := Token_Ptr;
541                   Scan; -- past ABSTRACT
542                end if;
543
544                if Token = Tok_Limited then
545                   Scan; -- past LIMITED
546
547                   --  TAGGED LIMITED PRIVATE case
548
549                   if Token = Tok_Private then
550                      Decl_Node :=
551                        New_Node (N_Private_Type_Declaration, Type_Loc);
552                      Set_Tagged_Present (Decl_Node, True);
553                      Set_Limited_Present (Decl_Node, True);
554                      Scan; -- past PRIVATE
555
556                   --  TAGGED LIMITED RECORD
557
558                   else
559                      Typedef_Node := P_Record_Definition;
560                      Set_Tagged_Present (Typedef_Node, True);
561                      Set_Limited_Present (Typedef_Node, True);
562
563                      End_Labl :=
564                        Make_Identifier (Token_Ptr,
565                          Chars => Chars (Ident_Node));
566                      Set_Comes_From_Source (End_Labl, False);
567
568                      Set_End_Label (Typedef_Node, End_Labl);
569                   end if;
570
571                else
572                   --  TAGGED PRIVATE
573
574                   if Token = Tok_Private then
575                      Decl_Node :=
576                        New_Node (N_Private_Type_Declaration, Type_Loc);
577                      Set_Tagged_Present (Decl_Node, True);
578                      Scan; -- past PRIVATE
579
580                   --  TAGGED RECORD
581
582                   else
583                      Typedef_Node := P_Record_Definition;
584                      Set_Tagged_Present (Typedef_Node, True);
585
586                      End_Labl :=
587                        Make_Identifier (Token_Ptr,
588                          Chars => Chars (Ident_Node));
589                      Set_Comes_From_Source (End_Labl, False);
590
591                      Set_End_Label (Typedef_Node, End_Labl);
592                   end if;
593                end if;
594
595                TF_Semicolon;
596                exit;
597
598             when Tok_Limited =>
599                Scan; -- past LIMITED
600
601                loop
602                   if Token = Tok_Tagged then
603                      Error_Msg_SC ("TAGGED must come before LIMITED");
604                      Scan; -- past TAGGED
605
606                   elsif Token = Tok_Abstract then
607                      Error_Msg_SC ("ABSTRACT must come before LIMITED");
608                      Scan; -- past ABSTRACT
609
610                   else
611                      exit;
612                   end if;
613                end loop;
614
615                --  LIMITED RECORD or LIMITED NULL RECORD
616
617                if Token = Tok_Record or else Token = Tok_Null then
618                   if Ada_Version = Ada_83 then
619                      Error_Msg_SP
620                        ("(Ada 83) limited record declaration not allowed!");
621                   end if;
622
623                   Typedef_Node := P_Record_Definition;
624                   Set_Limited_Present (Typedef_Node, True);
625
626                --  Ada 2005 (AI-251): LIMITED INTERFACE
627
628                --  If we are compiling in Ada 83 or Ada 95 mode, "interface"
629                --  is not a reserved word but we force its analysis to
630                --  generate the corresponding usage error.
631
632                elsif Token = Tok_Interface
633                  or else (Token = Tok_Identifier
634                            and then Chars (Token_Node) = Name_Interface)
635                then
636                   Typedef_Node :=
637                     P_Interface_Type_Definition (Abstract_Present);
638                   Abstract_Present := True;
639                   Set_Limited_Present (Typedef_Node);
640
641                   if Nkind (Typedef_Node) = N_Derived_Type_Definition then
642                      Is_Derived_Iface := True;
643                   end if;
644
645                   --  Ada 2005 (AI-419): LIMITED NEW
646
647                elsif Token = Tok_New then
648                   if Ada_Version < Ada_05 then
649                      Error_Msg_SP
650                        ("LIMITED in derived type is an Ada 2005 extension");
651                      Error_Msg_SP
652                        ("\unit must be compiled with -gnat05 switch");
653                   end if;
654
655                   Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
656                   Set_Limited_Present (Typedef_Node);
657
658                   if Nkind (Typedef_Node) = N_Derived_Type_Definition
659                     and then Present (Record_Extension_Part (Typedef_Node))
660                   then
661                      End_Labl :=
662                        Make_Identifier (Token_Ptr,
663                                         Chars => Chars (Ident_Node));
664                      Set_Comes_From_Source (End_Labl, False);
665
666                      Set_End_Label
667                        (Record_Extension_Part (Typedef_Node), End_Labl);
668                   end if;
669
670                --  LIMITED PRIVATE is the only remaining possibility here
671
672                else
673                   Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
674                   Set_Limited_Present (Decl_Node, True);
675                   T_Private; -- past PRIVATE (or complain if not there!)
676                end if;
677
678                TF_Semicolon;
679                exit;
680
681             --  Here we have an identifier after the IS, which is certainly
682             --  wrong and which might be one of several different mistakes.
683
684             when Tok_Identifier =>
685
686                --  First case, if identifier is on same line, then probably we
687                --  have something like "type X is Integer .." and the best
688                --  diagnosis is a missing NEW. Note: the missing new message
689                --  will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
690
691                if not Token_Is_At_Start_Of_Line then
692                   Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
693                   TF_Semicolon;
694
695                --  If the identifier is at the start of the line, and is in the
696                --  same column as the type declaration itself then we consider
697                --  that we had a missing type definition on the previous line
698
699                elsif Start_Column <= Type_Start_Col then
700                   Error_Msg_AP ("type definition expected");
701                   Typedef_Node := Error;
702
703                --  If the identifier is at the start of the line, and is in
704                --  a column to the right of the type declaration line, then we
705                --  may have something like:
706
707                --    type x is
708                --       r : integer
709
710                --  and the best diagnosis is a missing record keyword
711
712                else
713                   Typedef_Node := P_Record_Definition;
714                   TF_Semicolon;
715                end if;
716
717                exit;
718
719             --  Ada 2005 (AI-251): INTERFACE
720
721             when Tok_Interface =>
722                Typedef_Node := P_Interface_Type_Definition (Abstract_Present);
723                Abstract_Present := True;
724                TF_Semicolon;
725                exit;
726
727             when Tok_Private =>
728                Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
729                Scan; -- past PRIVATE
730                TF_Semicolon;
731                exit;
732
733             --  Ada 2005 (AI-345): Protected, synchronized or task interface
734             --  or Ada 2005 (AI-443): Synchronized private extension.
735
736             when Tok_Protected    |
737                  Tok_Synchronized |
738                  Tok_Task         =>
739
740                declare
741                   Saved_Token : constant Token_Type := Token;
742
743                begin
744                   Scan; -- past TASK, PROTECTED or SYNCHRONIZED
745
746                   --  Synchronized private extension
747
748                   if Token = Tok_New then
749                      Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
750
751                      if Saved_Token = Tok_Synchronized then
752                         Set_Synchronized_Present (Typedef_Node);
753                      else
754                         Error_Msg_SC ("invalid kind of private extension");
755                      end if;
756
757                   --  Interface
758
759                   else
760                      Typedef_Node :=
761                        P_Interface_Type_Definition (Abstract_Present);
762                      Abstract_Present := True;
763
764                      case Saved_Token is
765                         when Tok_Task =>
766                            Set_Task_Present         (Typedef_Node);
767
768                         when Tok_Protected =>
769                            Set_Protected_Present    (Typedef_Node);
770
771                         when Tok_Synchronized =>
772                            Set_Synchronized_Present (Typedef_Node);
773
774                         when others =>
775                            pragma Assert (False);
776                            null;
777                      end case;
778                   end if;
779                end;
780
781                TF_Semicolon;
782                exit;
783
784             --  Anything else is an error
785
786             when others =>
787                if Bad_Spelling_Of (Tok_Access)
788                     or else
789                   Bad_Spelling_Of (Tok_Array)
790                     or else
791                   Bad_Spelling_Of (Tok_Delta)
792                     or else
793                   Bad_Spelling_Of (Tok_Digits)
794                     or else
795                   Bad_Spelling_Of (Tok_Limited)
796                     or else
797                   Bad_Spelling_Of (Tok_Private)
798                     or else
799                   Bad_Spelling_Of (Tok_Range)
800                     or else
801                   Bad_Spelling_Of (Tok_Record)
802                     or else
803                   Bad_Spelling_Of (Tok_Tagged)
804                then
805                   null;
806
807                else
808                   Error_Msg_AP ("type definition expected");
809                   raise Error_Resync;
810                end if;
811
812          end case;
813       end loop;
814
815       --  For the private type declaration case, the private type declaration
816       --  node has been built, with the Tagged_Present and Limited_Present
817       --  flags set as needed, and Typedef_Node is left set to Empty.
818
819       if No (Typedef_Node) then
820          Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
821          Set_Abstract_Present (Decl_Node, Abstract_Present);
822
823       --  For a private extension declaration, Typedef_Node contains the
824       --  N_Private_Extension_Declaration node, which we now complete. Note
825       --  that the private extension declaration, unlike a full type
826       --  declaration, does permit unknown discriminants.
827
828       elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
829          Decl_Node := Typedef_Node;
830          Set_Sloc (Decl_Node, Type_Loc);
831          Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
832          Set_Abstract_Present (Typedef_Node, Abstract_Present);
833
834       --  In the full type declaration case, Typedef_Node has the type
835       --  definition and here is where we build the full type declaration
836       --  node. This is also where we check for improper use of an unknown
837       --  discriminant part (not allowed for full type declaration).
838
839       else
840          if Nkind (Typedef_Node) = N_Record_Definition
841            or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
842                       and then Present (Record_Extension_Part (Typedef_Node)))
843            or else Is_Derived_Iface
844          then
845             Set_Abstract_Present (Typedef_Node, Abstract_Present);
846
847          elsif Abstract_Present then
848             Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
849          end if;
850
851          Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
852          Set_Type_Definition (Decl_Node, Typedef_Node);
853
854          if Unknown_Dis then
855             Error_Msg
856               ("Full type declaration cannot have unknown discriminants",
857                 Discr_Sloc);
858          end if;
859       end if;
860
861       --  Remaining processing is common for all three cases
862
863       Set_Defining_Identifier (Decl_Node, Ident_Node);
864       Set_Discriminant_Specifications (Decl_Node, Discr_List);
865       return Decl_Node;
866    end P_Type_Declaration;
867
868    ----------------------------------
869    -- 3.2.1  Full Type Declaration --
870    ----------------------------------
871
872    --  Parsed by P_Type_Declaration (3.2.1)
873
874    ----------------------------
875    -- 3.2.1  Type Definition --
876    ----------------------------
877
878    --  Parsed by P_Type_Declaration (3.2.1)
879
880    --------------------------------
881    -- 3.2.2  Subtype Declaration --
882    --------------------------------
883
884    --  SUBTYPE_DECLARATION ::=
885    --    subtype DEFINING_IDENTIFIER is [NULL_EXCLUSION] SUBTYPE_INDICATION;
886
887    --  The caller has checked that the initial token is SUBTYPE
888
889    --  Error recovery: can raise Error_Resync
890
891    function P_Subtype_Declaration return Node_Id is
892       Decl_Node        : Node_Id;
893       Not_Null_Present : Boolean := False;
894
895    begin
896       Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
897       Scan; -- past SUBTYPE
898       Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
899       TF_Is;
900
901       if Token = Tok_New then
902          Error_Msg_SC ("NEW ignored (only allowed in type declaration)");
903          Scan; -- past NEW
904       end if;
905
906       Not_Null_Present := P_Null_Exclusion; --  Ada 2005 (AI-231)
907       Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
908
909       Set_Subtype_Indication
910         (Decl_Node, P_Subtype_Indication (Not_Null_Present));
911       TF_Semicolon;
912       return Decl_Node;
913    end P_Subtype_Declaration;
914
915    -------------------------------
916    -- 3.2.2  Subtype Indication --
917    -------------------------------
918
919    --  SUBTYPE_INDICATION ::=
920    --    [not null] SUBTYPE_MARK [CONSTRAINT]
921
922    --  Error recovery: can raise Error_Resync
923
924    function P_Null_Exclusion
925      (Allow_Anonymous_In_95 : Boolean := False) return Boolean
926    is
927       Not_Loc : constant Source_Ptr := Token_Ptr;
928       --  Source position of "not", if present
929
930    begin
931       if Token /= Tok_Not then
932          return False;
933
934       else
935          Scan; --  past NOT
936
937          if Token = Tok_Null then
938             Scan; --  past NULL
939
940             --  Ada 2005 (AI-441, AI-447): null_exclusion is illegal in Ada 95,
941             --  except in the case of anonymous access types.
942
943             --  Allow_Anonymous_In_95 will be True if we're parsing a formal
944             --  parameter or discriminant, which are the only places where
945             --  anonymous access types occur in Ada 95. "Formal : not null
946             --  access ..." is legal in Ada 95, whereas "Formal : not null
947             --  Named_Access_Type" is not.
948
949             if Ada_Version >= Ada_05
950               or else (Ada_Version >= Ada_95
951                         and then Allow_Anonymous_In_95
952                         and then Token = Tok_Access)
953             then
954                null; -- OK
955
956             else
957                Error_Msg
958                  ("`NOT NULL` access type is an Ada 2005 extension", Not_Loc);
959                Error_Msg
960                  ("\unit should be compiled with -gnat05 switch", Not_Loc);
961             end if;
962
963          else
964             Error_Msg_SP ("NULL expected");
965          end if;
966
967          if Token = Tok_New then
968             Error_Msg ("`NOT NULL` comes after NEW, not before", Not_Loc);
969          end if;
970
971          return True;
972       end if;
973    end P_Null_Exclusion;
974
975    function P_Subtype_Indication
976      (Not_Null_Present : Boolean := False) return Node_Id
977    is
978       Type_Node : Node_Id;
979
980    begin
981       if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
982          Type_Node := P_Subtype_Mark;
983          return P_Subtype_Indication (Type_Node, Not_Null_Present);
984
985       else
986          --  Check for error of using record definition and treat it nicely,
987          --  otherwise things are really messed up, so resynchronize.
988
989          if Token = Tok_Record then
990             Error_Msg_SC ("anonymous record definitions are not permitted");
991             Discard_Junk_Node (P_Record_Definition);
992             return Error;
993
994          else
995             Error_Msg_AP ("subtype indication expected");
996             raise Error_Resync;
997          end if;
998       end if;
999    end P_Subtype_Indication;
1000
1001    --  The following function is identical except that it is called with
1002    --  the subtype mark already scanned out, and it scans out the constraint
1003
1004    --  Error recovery: can raise Error_Resync
1005
1006    function P_Subtype_Indication
1007      (Subtype_Mark     : Node_Id;
1008       Not_Null_Present : Boolean := False) return Node_Id
1009    is
1010       Indic_Node  : Node_Id;
1011       Constr_Node : Node_Id;
1012
1013    begin
1014       Constr_Node := P_Constraint_Opt;
1015
1016       if No (Constr_Node) then
1017          return Subtype_Mark;
1018       else
1019          if Not_Null_Present then
1020             Error_Msg_SP ("`NOT NULL` not allowed if constraint given");
1021          end if;
1022
1023          Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
1024          Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
1025          Set_Constraint (Indic_Node, Constr_Node);
1026          return Indic_Node;
1027       end if;
1028    end P_Subtype_Indication;
1029
1030    -------------------------
1031    -- 3.2.2  Subtype Mark --
1032    -------------------------
1033
1034    --  SUBTYPE_MARK ::= subtype_NAME;
1035
1036    --  Note: The subtype mark which appears after an IN or NOT IN
1037    --  operator is parsed by P_Range_Or_Subtype_Mark (3.5)
1038
1039    --  Error recovery: cannot raise Error_Resync
1040
1041    function P_Subtype_Mark return Node_Id is
1042    begin
1043       return P_Subtype_Mark_Resync;
1044    exception
1045       when Error_Resync =>
1046          return Error;
1047    end P_Subtype_Mark;
1048
1049    --  This routine differs from P_Subtype_Mark in that it insists that an
1050    --  identifier be present, and if it is not, it raises Error_Resync.
1051
1052    --  Error recovery: can raise Error_Resync
1053
1054    function P_Subtype_Mark_Resync return Node_Id is
1055       Type_Node : Node_Id;
1056
1057    begin
1058       if Token = Tok_Access then
1059          Error_Msg_SC ("anonymous access type definition not allowed here");
1060          Scan; -- past ACCESS
1061       end if;
1062
1063       if Token = Tok_Array then
1064          Error_Msg_SC ("anonymous array definition not allowed here");
1065          Discard_Junk_Node (P_Array_Type_Definition);
1066          return Error;
1067
1068       else
1069          Type_Node := P_Qualified_Simple_Name_Resync;
1070
1071          --  Check for a subtype mark attribute. The only valid possibilities
1072          --  are 'CLASS and 'BASE. Anything else is a definite error. We may
1073          --  as well catch it here.
1074
1075          if Token = Tok_Apostrophe then
1076             return P_Subtype_Mark_Attribute (Type_Node);
1077          else
1078             return Type_Node;
1079          end if;
1080       end if;
1081    end P_Subtype_Mark_Resync;
1082
1083    --  The following function is called to scan out a subtype mark attribute.
1084    --  The caller has already scanned out the subtype mark, which is passed in
1085    --  as the argument, and has checked that the current token is apostrophe.
1086
1087    --  Only a special subclass of attributes, called type attributes
1088    --  (see Snames package) are allowed in this syntactic position.
1089
1090    --  Note: if the apostrophe is followed by other than an identifier, then
1091    --  the input expression is returned unchanged, and the scan pointer is
1092    --  left pointing to the apostrophe.
1093
1094    --  Error recovery: can raise Error_Resync
1095
1096    function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
1097       Attr_Node  : Node_Id := Empty;
1098       Scan_State : Saved_Scan_State;
1099       Prefix     : Node_Id;
1100
1101    begin
1102       Prefix := Check_Subtype_Mark (Type_Node);
1103
1104       if Prefix = Error then
1105          raise Error_Resync;
1106       end if;
1107
1108       --  Loop through attributes appearing (more than one can appear as for
1109       --  for example in X'Base'Class). We are at an apostrophe on entry to
1110       --  this loop, and it runs once for each attribute parsed, with
1111       --  Prefix being the current possible prefix if it is an attribute.
1112
1113       loop
1114          Save_Scan_State (Scan_State); -- at Apostrophe
1115          Scan; -- past apostrophe
1116
1117          if Token /= Tok_Identifier then
1118             Restore_Scan_State (Scan_State); -- to apostrophe
1119             return Prefix; -- no attribute after all
1120
1121          elsif not Is_Type_Attribute_Name (Token_Name) then
1122             Error_Msg_N
1123               ("attribute & may not be used in a subtype mark", Token_Node);
1124             raise Error_Resync;
1125
1126          else
1127             Attr_Node :=
1128               Make_Attribute_Reference (Prev_Token_Ptr,
1129                 Prefix => Prefix,
1130                 Attribute_Name => Token_Name);
1131             Delete_Node (Token_Node);
1132             Scan; -- past type attribute identifier
1133          end if;
1134
1135          exit when Token /= Tok_Apostrophe;
1136          Prefix := Attr_Node;
1137       end loop;
1138
1139       --  Fall through here after scanning type attribute
1140
1141       return Attr_Node;
1142    end P_Subtype_Mark_Attribute;
1143
1144    -----------------------
1145    -- 3.2.2  Constraint --
1146    -----------------------
1147
1148    --  CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
1149
1150    --  SCALAR_CONSTRAINT ::=
1151    --    RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
1152
1153    --  COMPOSITE_CONSTRAINT ::=
1154    --    INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
1155
1156    --  If no constraint is present, this function returns Empty
1157
1158    --  Error recovery: can raise Error_Resync
1159
1160    function P_Constraint_Opt return Node_Id is
1161    begin
1162       if Token = Tok_Range
1163         or else Bad_Spelling_Of (Tok_Range)
1164       then
1165          return P_Range_Constraint;
1166
1167       elsif Token = Tok_Digits
1168         or else Bad_Spelling_Of (Tok_Digits)
1169       then
1170          return P_Digits_Constraint;
1171
1172       elsif Token = Tok_Delta
1173         or else Bad_Spelling_Of (Tok_Delta)
1174       then
1175          return P_Delta_Constraint;
1176
1177       elsif Token = Tok_Left_Paren then
1178          return P_Index_Or_Discriminant_Constraint;
1179
1180       elsif Token = Tok_In then
1181          Ignore (Tok_In);
1182          return P_Constraint_Opt;
1183
1184       else
1185          return Empty;
1186       end if;
1187    end P_Constraint_Opt;
1188
1189    ------------------------------
1190    -- 3.2.2  Scalar Constraint --
1191    ------------------------------
1192
1193    --  Parsed by P_Constraint_Opt (3.2.2)
1194
1195    ---------------------------------
1196    -- 3.2.2  Composite Constraint --
1197    ---------------------------------
1198
1199    --  Parsed by P_Constraint_Opt (3.2.2)
1200
1201    --------------------------------------------------------
1202    -- 3.3  Identifier Declarations (Also 7.4, 8.5, 11.1) --
1203    --------------------------------------------------------
1204
1205    --  This routine scans out a declaration starting with an identifier:
1206
1207    --  OBJECT_DECLARATION ::=
1208    --    DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1209    --      [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1210    --  | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1211    --      ACCESS_DEFINITION [:= EXPRESSION];
1212    --  | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1213    --      ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1214
1215    --  NUMBER_DECLARATION ::=
1216    --    DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
1217
1218    --  OBJECT_RENAMING_DECLARATION ::=
1219    --    DEFINING_IDENTIFIER :
1220    --      [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1221    --  | DEFINING_IDENTIFIER :
1222    --      ACCESS_DEFINITION renames object_NAME;
1223
1224    --  EXCEPTION_RENAMING_DECLARATION ::=
1225    --    DEFINING_IDENTIFIER : exception renames exception_NAME;
1226
1227    --  EXCEPTION_DECLARATION ::=
1228    --    DEFINING_IDENTIFIER_LIST : exception;
1229
1230    --  Note that the ALIASED indication in an object declaration is
1231    --  marked by a flag in the parent node.
1232
1233    --  The caller has checked that the initial token is an identifier
1234
1235    --  The value returned is a list of declarations, one for each identifier
1236    --  in the list (as described in Sinfo, we always split up multiple
1237    --  declarations into the equivalent sequence of single declarations
1238    --  using the More_Ids and Prev_Ids flags to preserve the source).
1239
1240    --  If the identifier turns out to be a probable statement rather than
1241    --  an identifier, then the scan is left pointing to the identifier and
1242    --  No_List is returned.
1243
1244    --  Error recovery: can raise Error_Resync
1245
1246    procedure P_Identifier_Declarations
1247      (Decls   : List_Id;
1248       Done    : out Boolean;
1249       In_Spec : Boolean)
1250    is
1251       Acc_Node         : Node_Id;
1252       Decl_Node        : Node_Id;
1253       Type_Node        : Node_Id;
1254       Ident_Sloc       : Source_Ptr;
1255       Scan_State       : Saved_Scan_State;
1256       List_OK          : Boolean := True;
1257       Ident            : Nat;
1258       Init_Expr        : Node_Id;
1259       Init_Loc         : Source_Ptr;
1260       Con_Loc          : Source_Ptr;
1261       Not_Null_Present : Boolean := False;
1262
1263       Idents : array (Int range 1 .. 4096) of Entity_Id;
1264       --  Used to save identifiers in the identifier list. The upper bound
1265       --  of 4096 is expected to be infinite in practice, and we do not even
1266       --  bother to check if this upper bound is exceeded.
1267
1268       Num_Idents : Nat := 1;
1269       --  Number of identifiers stored in Idents
1270
1271       procedure No_List;
1272       --  This procedure is called in renames cases to make sure that we do
1273       --  not have more than one identifier. If we do have more than one
1274       --  then an error message is issued (and the declaration is split into
1275       --  multiple declarations)
1276
1277       function Token_Is_Renames return Boolean;
1278       --  Checks if current token is RENAMES, and if so, scans past it and
1279       --  returns True, otherwise returns False. Includes checking for some
1280       --  common error cases.
1281
1282       procedure No_List is
1283       begin
1284          if Num_Idents > 1 then
1285             Error_Msg ("identifier list not allowed for RENAMES",
1286                        Sloc (Idents (2)));
1287          end if;
1288
1289          List_OK := False;
1290       end No_List;
1291
1292       function Token_Is_Renames return Boolean is
1293          At_Colon : Saved_Scan_State;
1294
1295       begin
1296          if Token = Tok_Colon then
1297             Save_Scan_State (At_Colon);
1298             Scan; -- past colon
1299             Check_Misspelling_Of (Tok_Renames);
1300
1301             if Token = Tok_Renames then
1302                Error_Msg_SP ("extra "":"" ignored");
1303                Scan; -- past RENAMES
1304                return True;
1305             else
1306                Restore_Scan_State (At_Colon);
1307                return False;
1308             end if;
1309
1310          else
1311             Check_Misspelling_Of (Tok_Renames);
1312
1313             if Token = Tok_Renames then
1314                Scan; -- past RENAMES
1315                return True;
1316             else
1317                return False;
1318             end if;
1319          end if;
1320       end Token_Is_Renames;
1321
1322    --  Start of processing for P_Identifier_Declarations
1323
1324    begin
1325       Ident_Sloc := Token_Ptr;
1326       Save_Scan_State (Scan_State); -- at first identifier
1327       Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1328
1329       --  If we have a colon after the identifier, then we can assume that
1330       --  this is in fact a valid identifier declaration and can steam ahead.
1331
1332       if Token = Tok_Colon then
1333          Scan; -- past colon
1334
1335       --  If we have a comma, then scan out the list of identifiers
1336
1337       elsif Token = Tok_Comma then
1338
1339          while Comma_Present loop
1340             Num_Idents := Num_Idents + 1;
1341             Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1342          end loop;
1343
1344          Save_Scan_State (Scan_State); -- at colon
1345          T_Colon;
1346
1347       --  If we have identifier followed by := then we assume that what is
1348       --  really meant is an assignment statement. The assignment statement
1349       --  is scanned out and added to the list of declarations. An exception
1350       --  occurs if the := is followed by the keyword constant, in which case
1351       --  we assume it was meant to be a colon.
1352
1353       elsif Token = Tok_Colon_Equal then
1354          Scan; -- past :=
1355
1356          if Token = Tok_Constant then
1357             Error_Msg_SP ("colon expected");
1358
1359          else
1360             Restore_Scan_State (Scan_State);
1361             Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1362             return;
1363          end if;
1364
1365       --  If we have an IS keyword, then assume the TYPE keyword was missing
1366
1367       elsif Token = Tok_Is then
1368          Restore_Scan_State (Scan_State);
1369          Append_To (Decls, P_Type_Declaration);
1370          Done := False;
1371          return;
1372
1373       --  Otherwise we have an error situation
1374
1375       else
1376          Restore_Scan_State (Scan_State);
1377
1378          --  First case is possible misuse of PROTECTED in Ada 83 mode. If
1379          --  so, fix the keyword and return to scan the protected declaration.
1380
1381          if Token_Name = Name_Protected then
1382             Check_95_Keyword (Tok_Protected, Tok_Identifier);
1383             Check_95_Keyword (Tok_Protected, Tok_Type);
1384             Check_95_Keyword (Tok_Protected, Tok_Body);
1385
1386             if Token = Tok_Protected then
1387                Done := False;
1388                return;
1389             end if;
1390
1391          --  Check misspelling possibilities. If so, correct the misspelling
1392          --  and return to scan out the resulting declaration.
1393
1394          elsif Bad_Spelling_Of (Tok_Function)
1395            or else Bad_Spelling_Of (Tok_Procedure)
1396            or else Bad_Spelling_Of (Tok_Package)
1397            or else Bad_Spelling_Of (Tok_Pragma)
1398            or else Bad_Spelling_Of (Tok_Protected)
1399            or else Bad_Spelling_Of (Tok_Generic)
1400            or else Bad_Spelling_Of (Tok_Subtype)
1401            or else Bad_Spelling_Of (Tok_Type)
1402            or else Bad_Spelling_Of (Tok_Task)
1403            or else Bad_Spelling_Of (Tok_Use)
1404            or else Bad_Spelling_Of (Tok_For)
1405          then
1406             Done := False;
1407             return;
1408
1409          --  Otherwise we definitely have an ordinary identifier with a junk
1410          --  token after it. Just complain that we expect a declaration, and
1411          --  skip to a semicolon
1412
1413          else
1414             Set_Declaration_Expected;
1415             Resync_Past_Semicolon;
1416             Done := False;
1417             return;
1418          end if;
1419       end if;
1420
1421       --  Come here with an identifier list and colon scanned out. We now
1422       --  build the nodes for the declarative items. One node is built for
1423       --  each identifier in the list, with the type information being
1424       --  repeated by rescanning the appropriate section of source.
1425
1426       --  First an error check, if we have two identifiers in a row, a likely
1427       --  possibility is that the first of the identifiers is an incorrectly
1428       --  spelled keyword.
1429
1430       if Token = Tok_Identifier then
1431          declare
1432             SS : Saved_Scan_State;
1433             I2 : Boolean;
1434
1435          begin
1436             Save_Scan_State (SS);
1437             Scan; -- past initial identifier
1438             I2 := (Token = Tok_Identifier);
1439             Restore_Scan_State (SS);
1440
1441             if I2
1442               and then
1443                 (Bad_Spelling_Of (Tok_Access)   or else
1444                  Bad_Spelling_Of (Tok_Aliased)  or else
1445                  Bad_Spelling_Of (Tok_Constant))
1446             then
1447                null;
1448             end if;
1449          end;
1450       end if;
1451
1452       --  Loop through identifiers
1453
1454       Ident := 1;
1455       Ident_Loop : loop
1456
1457          --  Check for some cases of misused Ada 95 keywords
1458
1459          if Token_Name = Name_Aliased then
1460             Check_95_Keyword (Tok_Aliased, Tok_Array);
1461             Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1462             Check_95_Keyword (Tok_Aliased, Tok_Constant);
1463          end if;
1464
1465          --  Constant cases
1466
1467          if Token = Tok_Constant then
1468             Con_Loc := Token_Ptr;
1469             Scan; -- past CONSTANT
1470
1471             --  Number declaration, initialization required
1472
1473             Init_Expr := Init_Expr_Opt;
1474
1475             if Present (Init_Expr) then
1476                if Not_Null_Present then
1477                   Error_Msg_SP
1478                     ("`NOT NULL` not allowed in numeric expression");
1479                end if;
1480
1481                Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1482                Set_Expression (Decl_Node, Init_Expr);
1483
1484             --  Constant object declaration
1485
1486             else
1487                Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1488                Set_Constant_Present (Decl_Node, True);
1489
1490                if Token_Name = Name_Aliased then
1491                   Check_95_Keyword (Tok_Aliased, Tok_Array);
1492                   Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1493                end if;
1494
1495                if Token = Tok_Aliased then
1496                   Error_Msg_SC ("ALIASED should be before CONSTANT");
1497                   Scan; -- past ALIASED
1498                   Set_Aliased_Present (Decl_Node, True);
1499                end if;
1500
1501                if Token = Tok_Array then
1502                   Set_Object_Definition
1503                     (Decl_Node, P_Array_Type_Definition);
1504
1505                else
1506                   Not_Null_Present := P_Null_Exclusion; --  Ada 2005 (AI-231)
1507                   Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1508
1509                   if Token = Tok_Access then
1510                      if Ada_Version < Ada_05 then
1511                         Error_Msg_SP
1512                           ("generalized use of anonymous access types " &
1513                            "is an Ada 2005 extension");
1514                         Error_Msg_SP
1515                           ("\unit must be compiled with -gnat05 switch");
1516                      end if;
1517
1518                      Set_Object_Definition
1519                        (Decl_Node, P_Access_Definition (Not_Null_Present));
1520                   else
1521                      Set_Object_Definition
1522                        (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1523                   end if;
1524                end if;
1525
1526                if Token = Tok_Renames then
1527                   Error_Msg
1528                     ("CONSTANT not permitted in renaming declaration",
1529                      Con_Loc);
1530                   Scan; -- Past renames
1531                   Discard_Junk_Node (P_Name);
1532                end if;
1533             end if;
1534
1535          --  Exception cases
1536
1537          elsif Token = Tok_Exception then
1538             Scan; -- past EXCEPTION
1539
1540             if Token_Is_Renames then
1541                No_List;
1542                Decl_Node :=
1543                  New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1544                Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1545                No_Constraint;
1546             else
1547                Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1548             end if;
1549
1550          --  Aliased case (note that an object definition is required)
1551
1552          elsif Token = Tok_Aliased then
1553             Scan; -- past ALIASED
1554             Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1555             Set_Aliased_Present (Decl_Node, True);
1556
1557             if Token = Tok_Constant then
1558                Scan; -- past CONSTANT
1559                Set_Constant_Present (Decl_Node, True);
1560             end if;
1561
1562             if Token = Tok_Array then
1563                Set_Object_Definition
1564                  (Decl_Node, P_Array_Type_Definition);
1565
1566             else
1567                Not_Null_Present := P_Null_Exclusion; --  Ada 2005 (AI-231)
1568                Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1569
1570                --  Access definition (AI-406) or subtype indication
1571
1572                if Token = Tok_Access then
1573                   if Ada_Version < Ada_05 then
1574                      Error_Msg_SP
1575                        ("generalized use of anonymous access types " &
1576                         "is an Ada 2005 extension");
1577                      Error_Msg_SP
1578                        ("\unit must be compiled with -gnat05 switch");
1579                   end if;
1580
1581                   Set_Object_Definition
1582                     (Decl_Node, P_Access_Definition (Not_Null_Present));
1583                else
1584                   Set_Object_Definition
1585                     (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1586                end if;
1587             end if;
1588
1589          --  Array case
1590
1591          elsif Token = Tok_Array then
1592             Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1593             Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1594
1595          --  Ada 2005 (AI-254, AI-406)
1596
1597          elsif Token = Tok_Not then
1598
1599             --  OBJECT_DECLARATION ::=
1600             --    DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1601             --      [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1602             --  | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1603             --      ACCESS_DEFINITION [:= EXPRESSION];
1604
1605             --  OBJECT_RENAMING_DECLARATION ::=
1606             --    DEFINING_IDENTIFIER :
1607             --      [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1608             --  | DEFINING_IDENTIFIER :
1609             --      ACCESS_DEFINITION renames object_NAME;
1610
1611             Not_Null_Present := P_Null_Exclusion;  --  Ada 2005 (AI-231/423)
1612
1613             if Token = Tok_Access then
1614                if Ada_Version < Ada_05 then
1615                   Error_Msg_SP
1616                     ("generalized use of anonymous access types " &
1617                      "is an Ada 2005 extension");
1618                   Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1619                end if;
1620
1621                Acc_Node := P_Access_Definition (Not_Null_Present);
1622
1623                if Token /= Tok_Renames then
1624                   Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1625                   Set_Object_Definition (Decl_Node, Acc_Node);
1626
1627                else
1628                   Scan; --  past renames
1629                   No_List;
1630                   Decl_Node :=
1631                     New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1632                   Set_Access_Definition (Decl_Node, Acc_Node);
1633                   Set_Name (Decl_Node, P_Name);
1634                end if;
1635
1636             else
1637                Type_Node := P_Subtype_Mark;
1638
1639                --  Object renaming declaration
1640
1641                if Token_Is_Renames then
1642                   if Ada_Version < Ada_05 then
1643                      Error_Msg_SP
1644                        ("`NOT NULL` not allowed in object renaming");
1645                      raise Error_Resync;
1646
1647                   --  Ada 2005 (AI-423): Object renaming declaration with
1648                   --  a null exclusion.
1649
1650                   else
1651                      No_List;
1652                      Decl_Node :=
1653                        New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1654                      Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1655                      Set_Subtype_Mark (Decl_Node, Type_Node);
1656                      Set_Name (Decl_Node, P_Name);
1657                   end if;
1658
1659                --  Object declaration
1660
1661                else
1662                   Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1663                   Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1664                   Set_Object_Definition
1665                     (Decl_Node,
1666                      P_Subtype_Indication (Type_Node, Not_Null_Present));
1667
1668                   --  RENAMES at this point means that we had the combination
1669                   --  of a constraint on the Type_Node and renames, which is
1670                   --  illegal
1671
1672                   if Token_Is_Renames then
1673                      Error_Msg_N ("constraint not allowed in object renaming "
1674                                   & "declaration",
1675                                   Constraint (Object_Definition (Decl_Node)));
1676                      raise Error_Resync;
1677                   end if;
1678                end if;
1679             end if;
1680
1681          --  Ada 2005 (AI-230): Access Definition case
1682
1683          elsif Token = Tok_Access then
1684             if Ada_Version < Ada_05 then
1685                Error_Msg_SP
1686                  ("generalized use of anonymous access types " &
1687                   "is an Ada 2005 extension");
1688                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1689             end if;
1690
1691             Acc_Node := P_Access_Definition (Null_Exclusion_Present => False);
1692
1693             --  Object declaration with access definition, or renaming
1694
1695             if Token /= Tok_Renames then
1696                Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1697                Set_Object_Definition (Decl_Node, Acc_Node);
1698
1699             else
1700                Scan; --  past renames
1701                No_List;
1702                Decl_Node :=
1703                  New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1704                Set_Access_Definition (Decl_Node, Acc_Node);
1705                Set_Name (Decl_Node, P_Name);
1706             end if;
1707
1708          --  Subtype indication case
1709
1710          else
1711             Type_Node := P_Subtype_Mark;
1712
1713             --  Object renaming declaration
1714
1715             if Token_Is_Renames then
1716                No_List;
1717                Decl_Node :=
1718                  New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1719                Set_Subtype_Mark (Decl_Node, Type_Node);
1720                Set_Name (Decl_Node, P_Name);
1721
1722             --  Object declaration
1723
1724             else
1725                Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1726                Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1727                Set_Object_Definition
1728                  (Decl_Node,
1729                   P_Subtype_Indication (Type_Node, Not_Null_Present));
1730
1731                --  RENAMES at this point means that we had the combination of
1732                --  a constraint on the Type_Node and renames, which is illegal
1733
1734                if Token_Is_Renames then
1735                   Error_Msg_N
1736                     ("constraint not allowed in object renaming declaration",
1737                      Constraint (Object_Definition (Decl_Node)));
1738                   raise Error_Resync;
1739                end if;
1740             end if;
1741          end if;
1742
1743          --  Scan out initialization, allowed only for object declaration
1744
1745          Init_Loc := Token_Ptr;
1746          Init_Expr := Init_Expr_Opt;
1747
1748          if Present (Init_Expr) then
1749             if Nkind (Decl_Node) = N_Object_Declaration then
1750                Set_Expression (Decl_Node, Init_Expr);
1751                Set_Has_Init_Expression (Decl_Node);
1752             else
1753                Error_Msg ("initialization not allowed here", Init_Loc);
1754             end if;
1755          end if;
1756
1757          TF_Semicolon;
1758          Set_Defining_Identifier (Decl_Node, Idents (Ident));
1759
1760          if List_OK then
1761             if Ident < Num_Idents then
1762                Set_More_Ids (Decl_Node, True);
1763             end if;
1764
1765             if Ident > 1 then
1766                Set_Prev_Ids (Decl_Node, True);
1767             end if;
1768          end if;
1769
1770          Append (Decl_Node, Decls);
1771          exit Ident_Loop when Ident = Num_Idents;
1772          Restore_Scan_State (Scan_State);
1773          T_Colon;
1774          Ident := Ident + 1;
1775       end loop Ident_Loop;
1776
1777       Done := False;
1778    end P_Identifier_Declarations;
1779
1780    -------------------------------
1781    -- 3.3.1  Object Declaration --
1782    -------------------------------
1783
1784    --  OBJECT DECLARATION ::=
1785    --    DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1786    --      SUBTYPE_INDICATION [:= EXPRESSION];
1787    --  | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1788    --      ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1789    --  | SINGLE_TASK_DECLARATION
1790    --  | SINGLE_PROTECTED_DECLARATION
1791
1792    --  Cases starting with TASK are parsed by P_Task (9.1)
1793    --  Cases starting with PROTECTED are parsed by P_Protected (9.4)
1794    --  All other cases are parsed by P_Identifier_Declarations (3.3)
1795
1796    -------------------------------------
1797    -- 3.3.1  Defining Identifier List --
1798    -------------------------------------
1799
1800    --  DEFINING_IDENTIFIER_LIST ::=
1801    --    DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1802
1803    --  Always parsed by the construct in which it appears. See special
1804    --  section on "Handling of Defining Identifier Lists" in this unit.
1805
1806    -------------------------------
1807    -- 3.3.2  Number Declaration --
1808    -------------------------------
1809
1810    --  Parsed by P_Identifier_Declarations (3.3)
1811
1812    -------------------------------------------------------------------------
1813    -- 3.4  Derived Type Definition or Private Extension Declaration (7.3) --
1814    -------------------------------------------------------------------------
1815
1816    --  DERIVED_TYPE_DEFINITION ::=
1817    --    [abstract] [limited] new [NULL_EXCLUSION] parent_SUBTYPE_INDICATION
1818    --    [[and INTERFACE_LIST] RECORD_EXTENSION_PART]
1819
1820    --  PRIVATE_EXTENSION_DECLARATION ::=
1821    --     type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1822    --       [abstract] [limited | synchronized]
1823    --          new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
1824    --            with private;
1825
1826    --  RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1827
1828    --  The caller has already scanned out the part up to the NEW, and Token
1829    --  either contains Tok_New (or ought to, if it doesn't this procedure
1830    --  will post an appropriate "NEW expected" message).
1831
1832    --  Note: the caller is responsible for filling in the Sloc field of
1833    --  the returned node in the private extension declaration case as
1834    --  well as the stuff relating to the discriminant part.
1835
1836    --  Error recovery: can raise Error_Resync;
1837
1838    function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
1839       Typedef_Node     : Node_Id;
1840       Typedecl_Node    : Node_Id;
1841       Not_Null_Present : Boolean := False;
1842
1843    begin
1844       Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
1845
1846       if Ada_Version < Ada_05
1847         and then Token = Tok_Identifier
1848         and then Token_Name = Name_Interface
1849       then
1850          Error_Msg_SP
1851            ("abstract interface is an Ada 2005 extension");
1852          Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1853       else
1854          T_New;
1855       end if;
1856
1857       if Token = Tok_Abstract then
1858          Error_Msg_SC ("ABSTRACT must come before NEW, not after");
1859          Scan;
1860       end if;
1861
1862       Not_Null_Present := P_Null_Exclusion; --  Ada 2005 (AI-231)
1863       Set_Null_Exclusion_Present (Typedef_Node, Not_Null_Present);
1864       Set_Subtype_Indication (Typedef_Node,
1865          P_Subtype_Indication (Not_Null_Present));
1866
1867       --  Ada 2005 (AI-251): Deal with interfaces
1868
1869       if Token = Tok_And then
1870          Scan; -- past AND
1871
1872          if Ada_Version < Ada_05 then
1873             Error_Msg_SP
1874               ("abstract interface is an Ada 2005 extension");
1875             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1876          end if;
1877
1878          Set_Interface_List (Typedef_Node, New_List);
1879
1880          loop
1881             Append (P_Qualified_Simple_Name, Interface_List (Typedef_Node));
1882             exit when Token /= Tok_And;
1883             Scan; -- past AND
1884          end loop;
1885
1886          if Token /= Tok_With then
1887             Error_Msg_SC ("WITH expected");
1888             raise Error_Resync;
1889          end if;
1890       end if;
1891
1892       --  Deal with record extension, note that we assume that a WITH is
1893       --  missing in the case of "type X is new Y record ..." or in the
1894       --  case of "type X is new Y null record".
1895
1896       if Token = Tok_With
1897         or else Token = Tok_Record
1898         or else Token = Tok_Null
1899       then
1900          T_With; -- past WITH or give error message
1901
1902          if Token = Tok_Limited then
1903             Error_Msg_SC
1904               ("LIMITED keyword not allowed in private extension");
1905             Scan; -- ignore LIMITED
1906          end if;
1907
1908          --  Private extension declaration
1909
1910          if Token = Tok_Private then
1911             Scan; -- past PRIVATE
1912
1913             --  Throw away the type definition node and build the type
1914             --  declaration node. Note the caller must set the Sloc,
1915             --  Discriminant_Specifications, Unknown_Discriminants_Present,
1916             --  and Defined_Identifier fields in the returned node.
1917
1918             Typedecl_Node :=
1919               Make_Private_Extension_Declaration (No_Location,
1920                 Defining_Identifier => Empty,
1921                 Subtype_Indication  => Subtype_Indication (Typedef_Node),
1922                 Abstract_Present    => Abstract_Present (Typedef_Node),
1923                 Interface_List      => Interface_List (Typedef_Node));
1924
1925             Delete_Node (Typedef_Node);
1926             return Typedecl_Node;
1927
1928          --  Derived type definition with record extension part
1929
1930          else
1931             Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
1932             return Typedef_Node;
1933          end if;
1934
1935       --  Derived type definition with no record extension part
1936
1937       else
1938          return Typedef_Node;
1939       end if;
1940    end P_Derived_Type_Def_Or_Private_Ext_Decl;
1941
1942    ---------------------------
1943    -- 3.5  Range Constraint --
1944    ---------------------------
1945
1946    --  RANGE_CONSTRAINT ::= range RANGE
1947
1948    --  The caller has checked that the initial token is RANGE
1949
1950    --  Error recovery: cannot raise Error_Resync
1951
1952    function P_Range_Constraint return Node_Id is
1953       Range_Node : Node_Id;
1954
1955    begin
1956       Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
1957       Scan; -- past RANGE
1958       Set_Range_Expression (Range_Node, P_Range);
1959       return Range_Node;
1960    end P_Range_Constraint;
1961
1962    ----------------
1963    -- 3.5  Range --
1964    ----------------
1965
1966    --  RANGE ::=
1967    --    RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1968
1969    --  Note: the range that appears in a membership test is parsed by
1970    --  P_Range_Or_Subtype_Mark (3.5).
1971
1972    --  Error recovery: cannot raise Error_Resync
1973
1974    function P_Range return Node_Id is
1975       Expr_Node  : Node_Id;
1976       Range_Node : Node_Id;
1977
1978    begin
1979       Expr_Node := P_Simple_Expression_Or_Range_Attribute;
1980
1981       if Expr_Form = EF_Range_Attr then
1982          return Expr_Node;
1983
1984       elsif Token = Tok_Dot_Dot then
1985          Range_Node := New_Node (N_Range, Token_Ptr);
1986          Set_Low_Bound (Range_Node, Expr_Node);
1987          Scan; -- past ..
1988          Expr_Node := P_Expression;
1989          Check_Simple_Expression (Expr_Node);
1990          Set_High_Bound (Range_Node, Expr_Node);
1991          return Range_Node;
1992
1993       --  Anything else is an error
1994
1995       else
1996          T_Dot_Dot; -- force missing .. message
1997          return Error;
1998       end if;
1999    end P_Range;
2000
2001    ----------------------------------
2002    -- 3.5  P_Range_Or_Subtype_Mark --
2003    ----------------------------------
2004
2005    --  RANGE ::=
2006    --    RANGE_ATTRIBUTE_REFERENCE
2007    --  | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2008
2009    --  This routine scans out the range or subtype mark that forms the right
2010    --  operand of a membership test (it is not used in any other contexts, and
2011    --  error messages are specialized with this knowledge in mind).
2012
2013    --  Note: as documented in the Sinfo interface, although the syntax only
2014    --  allows a subtype mark, we in fact allow any simple expression to be
2015    --  returned from this routine. The semantics is responsible for issuing
2016    --  an appropriate message complaining if the argument is not a name.
2017    --  This simplifies the coding and error recovery processing in the
2018    --  parser, and in any case it is preferable not to consider this a
2019    --  syntax error and to continue with the semantic analysis.
2020
2021    --  Error recovery: cannot raise Error_Resync
2022
2023    function P_Range_Or_Subtype_Mark return Node_Id is
2024       Expr_Node  : Node_Id;
2025       Range_Node : Node_Id;
2026       Save_Loc   : Source_Ptr;
2027
2028    --  Start of processing for P_Range_Or_Subtype_Mark
2029
2030    begin
2031       --  Save location of possible junk parentheses
2032
2033       Save_Loc := Token_Ptr;
2034
2035       --  Scan out either a simple expression or a range (this accepts more
2036       --  than is legal here, but as explained above, we like to allow more
2037       --  with a proper diagnostic.
2038
2039       Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2040
2041       --  Range attribute
2042
2043       if Expr_Form = EF_Range_Attr then
2044          return Expr_Node;
2045
2046       --  Simple_Expression .. Simple_Expression
2047
2048       elsif Token = Tok_Dot_Dot then
2049          Check_Simple_Expression (Expr_Node);
2050          Range_Node := New_Node (N_Range, Token_Ptr);
2051          Set_Low_Bound (Range_Node, Expr_Node);
2052          Scan; -- past ..
2053          Set_High_Bound (Range_Node, P_Simple_Expression);
2054          return Range_Node;
2055
2056       --  Case of subtype mark (optionally qualified simple name or an
2057       --  attribute whose prefix is an optionally qualifed simple name)
2058
2059       elsif Expr_Form = EF_Simple_Name
2060         or else Nkind (Expr_Node) = N_Attribute_Reference
2061       then
2062          --  Check for error of range constraint after a subtype mark
2063
2064          if Token = Tok_Range then
2065             Error_Msg_SC ("range constraint not allowed in membership test");
2066             Scan; -- past RANGE
2067             raise Error_Resync;
2068
2069          --  Check for error of DIGITS or DELTA after a subtype mark
2070
2071          elsif Token = Tok_Digits or else Token = Tok_Delta then
2072             Error_Msg_SC
2073               ("accuracy definition not allowed in membership test");
2074             Scan; -- past DIGITS or DELTA
2075             raise Error_Resync;
2076
2077          --  Attribute reference, may or may not be OK, but in any case we
2078          --  will scan it out
2079
2080          elsif Token = Tok_Apostrophe then
2081             return P_Subtype_Mark_Attribute (Expr_Node);
2082
2083          --  OK case of simple name, just return it
2084
2085          else
2086             return Expr_Node;
2087          end if;
2088
2089       --  Here we have some kind of error situation. Check for junk parens
2090       --  then return what we have, caller will deal with other errors.
2091
2092       else
2093          if Nkind (Expr_Node) in N_Subexpr
2094            and then Paren_Count (Expr_Node) /= 0
2095          then
2096             Error_Msg ("|parentheses not allowed for subtype mark", Save_Loc);
2097             Set_Paren_Count (Expr_Node, 0);
2098          end if;
2099
2100          return Expr_Node;
2101       end if;
2102    end P_Range_Or_Subtype_Mark;
2103
2104    ----------------------------------------
2105    -- 3.5.1  Enumeration Type Definition --
2106    ----------------------------------------
2107
2108    --  ENUMERATION_TYPE_DEFINITION ::=
2109    --    (ENUMERATION_LITERAL_SPECIFICATION
2110    --      {, ENUMERATION_LITERAL_SPECIFICATION})
2111
2112    --  The caller has already scanned out the TYPE keyword
2113
2114    --  Error recovery: can raise Error_Resync;
2115
2116    function P_Enumeration_Type_Definition return Node_Id is
2117       Typedef_Node : Node_Id;
2118
2119    begin
2120       Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
2121       Set_Literals (Typedef_Node, New_List);
2122
2123       T_Left_Paren;
2124
2125       loop
2126          Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
2127          exit when not Comma_Present;
2128       end loop;
2129
2130       T_Right_Paren;
2131       return Typedef_Node;
2132    end P_Enumeration_Type_Definition;
2133
2134    ----------------------------------------------
2135    -- 3.5.1  Enumeration Literal Specification --
2136    ----------------------------------------------
2137
2138    --  ENUMERATION_LITERAL_SPECIFICATION ::=
2139    --    DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
2140
2141    --  Error recovery: can raise Error_Resync
2142
2143    function P_Enumeration_Literal_Specification return Node_Id is
2144    begin
2145       if Token = Tok_Char_Literal then
2146          return P_Defining_Character_Literal;
2147       else
2148          return P_Defining_Identifier (C_Comma_Right_Paren);
2149       end if;
2150    end P_Enumeration_Literal_Specification;
2151
2152    ---------------------------------------
2153    -- 3.5.1  Defining_Character_Literal --
2154    ---------------------------------------
2155
2156    --  DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
2157
2158    --  Error recovery: cannot raise Error_Resync
2159
2160    --  The caller has checked that the current token is a character literal
2161
2162    function P_Defining_Character_Literal return Node_Id is
2163       Literal_Node : Node_Id;
2164
2165    begin
2166       Literal_Node := Token_Node;
2167       Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
2168       Scan; -- past character literal
2169       return Literal_Node;
2170    end P_Defining_Character_Literal;
2171
2172    ------------------------------------
2173    -- 3.5.4  Integer Type Definition --
2174    ------------------------------------
2175
2176    --  Parsed by P_Type_Declaration (3.2.1)
2177
2178    -------------------------------------------
2179    -- 3.5.4  Signed Integer Type Definition --
2180    -------------------------------------------
2181
2182    --  SIGNED_INTEGER_TYPE_DEFINITION ::=
2183    --    range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2184
2185    --  Normally the initial token on entry is RANGE, but in some
2186    --  error conditions, the range token was missing and control is
2187    --  passed with Token pointing to first token of the first expression.
2188
2189    --  Error recovery: cannot raise Error_Resync
2190
2191    function P_Signed_Integer_Type_Definition return Node_Id is
2192       Typedef_Node : Node_Id;
2193       Expr_Node    : Node_Id;
2194
2195    begin
2196       Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
2197
2198       if Token = Tok_Range then
2199          Scan; -- past RANGE
2200       end if;
2201
2202       Expr_Node := P_Expression;
2203       Check_Simple_Expression (Expr_Node);
2204       Set_Low_Bound (Typedef_Node, Expr_Node);
2205       T_Dot_Dot;
2206       Expr_Node := P_Expression;
2207       Check_Simple_Expression (Expr_Node);
2208       Set_High_Bound (Typedef_Node, Expr_Node);
2209       return Typedef_Node;
2210    end P_Signed_Integer_Type_Definition;
2211
2212    ------------------------------------
2213    -- 3.5.4  Modular Type Definition --
2214    ------------------------------------
2215
2216    --  MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
2217
2218    --  The caller has checked that the initial token is MOD
2219
2220    --  Error recovery: cannot raise Error_Resync
2221
2222    function P_Modular_Type_Definition return Node_Id is
2223       Typedef_Node : Node_Id;
2224
2225    begin
2226       if Ada_Version = Ada_83 then
2227          Error_Msg_SC ("(Ada 83): modular types not allowed");
2228       end if;
2229
2230       Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
2231       Scan; -- past MOD
2232       Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2233
2234       --  Handle mod L..R cleanly
2235
2236       if Token = Tok_Dot_Dot then
2237          Error_Msg_SC ("range not allowed for modular type");
2238          Scan; -- past ..
2239          Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2240       end if;
2241
2242       return Typedef_Node;
2243    end P_Modular_Type_Definition;
2244
2245    ---------------------------------
2246    -- 3.5.6  Real Type Definition --
2247    ---------------------------------
2248
2249    --  Parsed by P_Type_Declaration (3.2.1)
2250
2251    --------------------------------------
2252    -- 3.5.7  Floating Point Definition --
2253    --------------------------------------
2254
2255    --  FLOATING_POINT_DEFINITION ::=
2256    --    digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2257
2258    --  Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
2259
2260    --  The caller has checked that the initial token is DIGITS
2261
2262    --  Error recovery: cannot raise Error_Resync
2263
2264    function P_Floating_Point_Definition return Node_Id is
2265       Digits_Loc : constant Source_Ptr := Token_Ptr;
2266       Def_Node   : Node_Id;
2267       Expr_Node  : Node_Id;
2268
2269    begin
2270       Scan; -- past DIGITS
2271       Expr_Node := P_Expression_No_Right_Paren;
2272       Check_Simple_Expression_In_Ada_83 (Expr_Node);
2273
2274       --  Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
2275
2276       if Token = Tok_Delta then
2277          Error_Msg_SC ("DELTA must come before DIGITS");
2278          Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
2279          Scan; -- past DELTA
2280          Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
2281
2282       --  OK floating-point definition
2283
2284       else
2285          Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
2286       end if;
2287
2288       Set_Digits_Expression (Def_Node, Expr_Node);
2289       Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2290       return Def_Node;
2291    end P_Floating_Point_Definition;
2292
2293    -------------------------------------
2294    -- 3.5.7  Real Range Specification --
2295    -------------------------------------
2296
2297    --  REAL_RANGE_SPECIFICATION ::=
2298    --    range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2299
2300    --  Error recovery: cannot raise Error_Resync
2301
2302    function P_Real_Range_Specification_Opt return Node_Id is
2303       Specification_Node : Node_Id;
2304       Expr_Node          : Node_Id;
2305
2306    begin
2307       if Token = Tok_Range then
2308          Specification_Node :=
2309            New_Node (N_Real_Range_Specification, Token_Ptr);
2310          Scan; -- past RANGE
2311          Expr_Node := P_Expression_No_Right_Paren;
2312          Check_Simple_Expression (Expr_Node);
2313          Set_Low_Bound (Specification_Node, Expr_Node);
2314          T_Dot_Dot;
2315          Expr_Node := P_Expression_No_Right_Paren;
2316          Check_Simple_Expression (Expr_Node);
2317          Set_High_Bound (Specification_Node, Expr_Node);
2318          return Specification_Node;
2319       else
2320          return Empty;
2321       end if;
2322    end P_Real_Range_Specification_Opt;
2323
2324    -----------------------------------
2325    -- 3.5.9  Fixed Point Definition --
2326    -----------------------------------
2327
2328    --  FIXED_POINT_DEFINITION ::=
2329    --    ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
2330
2331    --  ORDINARY_FIXED_POINT_DEFINITION ::=
2332    --    delta static_EXPRESSION REAL_RANGE_SPECIFICATION
2333
2334    --  DECIMAL_FIXED_POINT_DEFINITION ::=
2335    --    delta static_EXPRESSION
2336    --      digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2337
2338    --  The caller has checked that the initial token is DELTA
2339
2340    --  Error recovery: cannot raise Error_Resync
2341
2342    function P_Fixed_Point_Definition return Node_Id is
2343       Delta_Node : Node_Id;
2344       Delta_Loc  : Source_Ptr;
2345       Def_Node   : Node_Id;
2346       Expr_Node  : Node_Id;
2347
2348    begin
2349       Delta_Loc := Token_Ptr;
2350       Scan; -- past DELTA
2351       Delta_Node := P_Expression_No_Right_Paren;
2352       Check_Simple_Expression_In_Ada_83 (Delta_Node);
2353
2354       if Token = Tok_Digits then
2355          if Ada_Version = Ada_83 then
2356             Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
2357          end if;
2358
2359          Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
2360          Scan; -- past DIGITS
2361          Expr_Node := P_Expression_No_Right_Paren;
2362          Check_Simple_Expression_In_Ada_83 (Expr_Node);
2363          Set_Digits_Expression (Def_Node, Expr_Node);
2364
2365       else
2366          Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
2367
2368          --  Range is required in ordinary fixed point case
2369
2370          if Token /= Tok_Range then
2371             Error_Msg_AP ("range must be given for fixed-point type");
2372             T_Range;
2373          end if;
2374       end if;
2375
2376       Set_Delta_Expression (Def_Node, Delta_Node);
2377       Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2378       return Def_Node;
2379    end P_Fixed_Point_Definition;
2380
2381    --------------------------------------------
2382    -- 3.5.9  Ordinary Fixed Point Definition --
2383    --------------------------------------------
2384
2385    --  Parsed by P_Fixed_Point_Definition (3.5.9)
2386
2387    -------------------------------------------
2388    -- 3.5.9  Decimal Fixed Point Definition --
2389    -------------------------------------------
2390
2391    --  Parsed by P_Decimal_Point_Definition (3.5.9)
2392
2393    ------------------------------
2394    -- 3.5.9  Digits Constraint --
2395    ------------------------------
2396
2397    --  DIGITS_CONSTRAINT ::=
2398    --    digits static_EXPRESSION [RANGE_CONSTRAINT]
2399
2400    --  Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2401
2402    --  The caller has checked that the initial token is DIGITS
2403
2404    function P_Digits_Constraint return Node_Id is
2405       Constraint_Node : Node_Id;
2406       Expr_Node : Node_Id;
2407
2408    begin
2409       Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
2410       Scan; -- past DIGITS
2411       Expr_Node := P_Expression;
2412       Check_Simple_Expression_In_Ada_83 (Expr_Node);
2413       Set_Digits_Expression (Constraint_Node, Expr_Node);
2414
2415       if Token = Tok_Range then
2416          Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2417       end if;
2418
2419       return Constraint_Node;
2420    end P_Digits_Constraint;
2421
2422    -----------------------------
2423    -- 3.5.9  Delta Constraint --
2424    -----------------------------
2425
2426    --  DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2427
2428    --  Note: this is an obsolescent feature in Ada 95 (I.3)
2429
2430    --  Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2431
2432    --  The caller has checked that the initial token is DELTA
2433
2434    --  Error recovery: cannot raise Error_Resync
2435
2436    function P_Delta_Constraint return Node_Id is
2437       Constraint_Node : Node_Id;
2438       Expr_Node : Node_Id;
2439
2440    begin
2441       Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2442       Scan; -- past DELTA
2443       Expr_Node := P_Expression;
2444       Check_Simple_Expression_In_Ada_83 (Expr_Node);
2445       Set_Delta_Expression (Constraint_Node, Expr_Node);
2446
2447       if Token = Tok_Range then
2448          Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2449       end if;
2450
2451       return Constraint_Node;
2452    end P_Delta_Constraint;
2453
2454    --------------------------------
2455    -- 3.6  Array Type Definition --
2456    --------------------------------
2457
2458    --  ARRAY_TYPE_DEFINITION ::=
2459    --    UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2460
2461    --  UNCONSTRAINED_ARRAY_DEFINITION ::=
2462    --    array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2463    --      COMPONENT_DEFINITION
2464
2465    --  INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2466
2467    --  CONSTRAINED_ARRAY_DEFINITION ::=
2468    --    array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2469    --      COMPONENT_DEFINITION
2470
2471    --  DISCRETE_SUBTYPE_DEFINITION ::=
2472    --    DISCRETE_SUBTYPE_INDICATION | RANGE
2473
2474    --  COMPONENT_DEFINITION ::=
2475    --    [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
2476
2477    --  The caller has checked that the initial token is ARRAY
2478
2479    --  Error recovery: can raise Error_Resync
2480
2481    function P_Array_Type_Definition return Node_Id is
2482       Array_Loc        : Source_Ptr;
2483       CompDef_Node     : Node_Id;
2484       Def_Node         : Node_Id;
2485       Not_Null_Present : Boolean := False;
2486       Subs_List        : List_Id;
2487       Scan_State       : Saved_Scan_State;
2488       Aliased_Present  : Boolean := False;
2489
2490    begin
2491       Array_Loc := Token_Ptr;
2492       Scan; -- past ARRAY
2493       Subs_List := New_List;
2494       T_Left_Paren;
2495
2496       --  It's quite tricky to disentangle these two possibilities, so we do
2497       --  a prescan to determine which case we have and then reset the scan.
2498       --  The prescan skips past possible subtype mark tokens.
2499
2500       Save_Scan_State (Scan_State); -- just after paren
2501
2502       while Token in Token_Class_Desig or else
2503             Token = Tok_Dot or else
2504             Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2505       loop
2506          Scan;
2507       end loop;
2508
2509       --  If we end up on RANGE <> then we have the unconstrained case. We
2510       --  will also allow the RANGE to be omitted, just to improve error
2511       --  handling for a case like array (integer <>) of integer;
2512
2513       Scan; -- past possible RANGE or <>
2514
2515       if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2516          Prev_Token = Tok_Box
2517       then
2518          Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2519          Restore_Scan_State (Scan_State); -- to first subtype mark
2520
2521          loop
2522             Append (P_Subtype_Mark_Resync, Subs_List);
2523             T_Range;
2524             T_Box;
2525             exit when Token = Tok_Right_Paren or else Token = Tok_Of;
2526             T_Comma;
2527          end loop;
2528
2529          Set_Subtype_Marks (Def_Node, Subs_List);
2530
2531       else
2532          Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2533          Restore_Scan_State (Scan_State); -- to first discrete range
2534
2535          loop
2536             Append (P_Discrete_Subtype_Definition, Subs_List);
2537             exit when not Comma_Present;
2538          end loop;
2539
2540          Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2541       end if;
2542
2543       T_Right_Paren;
2544       T_Of;
2545
2546       CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2547
2548       if Token_Name = Name_Aliased then
2549          Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2550       end if;
2551
2552       if Token = Tok_Aliased then
2553          Aliased_Present := True;
2554          Scan; -- past ALIASED
2555       end if;
2556
2557       Not_Null_Present := P_Null_Exclusion; --  Ada 2005 (AI-231/AI-254)
2558
2559       --  Ada 2005 (AI-230): Access Definition case
2560
2561       if Token = Tok_Access then
2562          if Ada_Version < Ada_05 then
2563             Error_Msg_SP
2564               ("generalized use of anonymous access types " &
2565                "is an Ada 2005 extension");
2566             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
2567          end if;
2568
2569          if Aliased_Present then
2570             Error_Msg_SP ("ALIASED not allowed here");
2571          end if;
2572
2573          Set_Subtype_Indication     (CompDef_Node, Empty);
2574          Set_Aliased_Present        (CompDef_Node, False);
2575          Set_Access_Definition      (CompDef_Node,
2576            P_Access_Definition (Not_Null_Present));
2577       else
2578
2579          Set_Access_Definition      (CompDef_Node, Empty);
2580          Set_Aliased_Present        (CompDef_Node, Aliased_Present);
2581          Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
2582          Set_Subtype_Indication     (CompDef_Node,
2583            P_Subtype_Indication (Not_Null_Present));
2584       end if;
2585
2586       Set_Component_Definition (Def_Node, CompDef_Node);
2587
2588       return Def_Node;
2589    end P_Array_Type_Definition;
2590
2591    -----------------------------------------
2592    -- 3.6  Unconstrained Array Definition --
2593    -----------------------------------------
2594
2595    --  Parsed by P_Array_Type_Definition (3.6)
2596
2597    ---------------------------------------
2598    -- 3.6  Constrained Array Definition --
2599    ---------------------------------------
2600
2601    --  Parsed by P_Array_Type_Definition (3.6)
2602
2603    --------------------------------------
2604    -- 3.6  Discrete Subtype Definition --
2605    --------------------------------------
2606
2607    --  DISCRETE_SUBTYPE_DEFINITION ::=
2608    --    discrete_SUBTYPE_INDICATION | RANGE
2609
2610    --  Note: the discrete subtype definition appearing in a constrained
2611    --  array definition is parsed by P_Array_Type_Definition (3.6)
2612
2613    --  Error recovery: cannot raise Error_Resync
2614
2615    function P_Discrete_Subtype_Definition return Node_Id is
2616    begin
2617       --  The syntax of a discrete subtype definition is identical to that
2618       --  of a discrete range, so we simply share the same parsing code.
2619
2620       return P_Discrete_Range;
2621    end P_Discrete_Subtype_Definition;
2622
2623    -------------------------------
2624    -- 3.6  Component Definition --
2625    -------------------------------
2626
2627    --  For the array case, parsed by P_Array_Type_Definition (3.6)
2628    --  For the record case, parsed by P_Component_Declaration (3.8)
2629
2630    -----------------------------
2631    -- 3.6.1  Index Constraint --
2632    -----------------------------
2633
2634    --  Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2635
2636    ---------------------------
2637    -- 3.6.1  Discrete Range --
2638    ---------------------------
2639
2640    --  DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2641
2642    --  The possible forms for a discrete range are:
2643
2644       --   Subtype_Mark                           (SUBTYPE_INDICATION, 3.2.2)
2645       --   Subtype_Mark range Range               (SUBTYPE_INDICATION, 3.2.2)
2646       --   Range_Attribute                        (RANGE, 3.5)
2647       --   Simple_Expression .. Simple_Expression (RANGE, 3.5)
2648
2649    --  Error recovery: cannot raise Error_Resync
2650
2651    function P_Discrete_Range return Node_Id is
2652       Expr_Node  : Node_Id;
2653       Range_Node : Node_Id;
2654
2655    begin
2656       Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2657
2658       if Expr_Form = EF_Range_Attr then
2659          return Expr_Node;
2660
2661       elsif Token = Tok_Range then
2662          if Expr_Form /= EF_Simple_Name then
2663             Error_Msg_SC ("range must be preceded by subtype mark");
2664          end if;
2665
2666          return P_Subtype_Indication (Expr_Node);
2667
2668       --  Check Expression .. Expression case
2669
2670       elsif Token = Tok_Dot_Dot then
2671          Range_Node := New_Node (N_Range, Token_Ptr);
2672          Set_Low_Bound (Range_Node, Expr_Node);
2673          Scan; -- past ..
2674          Expr_Node := P_Expression;
2675          Check_Simple_Expression (Expr_Node);
2676          Set_High_Bound (Range_Node, Expr_Node);
2677          return Range_Node;
2678
2679       --  Otherwise we must have a subtype mark
2680
2681       elsif Expr_Form = EF_Simple_Name then
2682          return Expr_Node;
2683
2684       --  If incorrect, complain that we expect ..
2685
2686       else
2687          T_Dot_Dot;
2688          return Expr_Node;
2689       end if;
2690    end P_Discrete_Range;
2691
2692    ----------------------------
2693    -- 3.7  Discriminant Part --
2694    ----------------------------
2695
2696    --  DISCRIMINANT_PART ::=
2697    --    UNKNOWN_DISCRIMINANT_PART
2698    --  | KNOWN_DISCRIMINANT_PART
2699
2700    --  A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2701    --  or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2702
2703    ------------------------------------
2704    -- 3.7  Unknown Discriminant Part --
2705    ------------------------------------
2706
2707    --  UNKNOWN_DISCRIMINANT_PART ::= (<>)
2708
2709    --  If no unknown discriminant part is present, then False is returned,
2710    --  otherwise the unknown discriminant is scanned out and True is returned.
2711
2712    --  Error recovery: cannot raise Error_Resync
2713
2714    function P_Unknown_Discriminant_Part_Opt return Boolean is
2715       Scan_State : Saved_Scan_State;
2716
2717    begin
2718       if Token /= Tok_Left_Paren then
2719          return False;
2720
2721       else
2722          Save_Scan_State (Scan_State);
2723          Scan; -- past the left paren
2724
2725          if Token = Tok_Box then
2726             if Ada_Version = Ada_83 then
2727                Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
2728             end if;
2729
2730             Scan; -- past the box
2731             T_Right_Paren; -- must be followed by right paren
2732             return True;
2733
2734          else
2735             Restore_Scan_State (Scan_State);
2736             return False;
2737          end if;
2738       end if;
2739    end P_Unknown_Discriminant_Part_Opt;
2740
2741    ----------------------------------
2742    -- 3.7  Known Discriminant Part --
2743    ----------------------------------
2744
2745    --  KNOWN_DISCRIMINANT_PART ::=
2746    --    (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2747
2748    --  DISCRIMINANT_SPECIFICATION ::=
2749    --    DEFINING_IDENTIFIER_LIST : [NULL_EXCLUSION] SUBTYPE_MARK
2750    --      [:= DEFAULT_EXPRESSION]
2751    --  | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2752    --      [:= DEFAULT_EXPRESSION]
2753
2754    --  If no known discriminant part is present, then No_List is returned
2755
2756    --  Error recovery: cannot raise Error_Resync
2757
2758    function P_Known_Discriminant_Part_Opt return List_Id is
2759       Specification_Node : Node_Id;
2760       Specification_List : List_Id;
2761       Ident_Sloc         : Source_Ptr;
2762       Scan_State         : Saved_Scan_State;
2763       Num_Idents         : Nat;
2764       Not_Null_Present   : Boolean;
2765       Ident              : Nat;
2766
2767       Idents : array (Int range 1 .. 4096) of Entity_Id;
2768       --  This array holds the list of defining identifiers. The upper bound
2769       --  of 4096 is intended to be essentially infinite, and we do not even
2770       --  bother to check for it being exceeded.
2771
2772    begin
2773       if Token = Tok_Left_Paren then
2774          Specification_List := New_List;
2775          Scan; -- past (
2776          P_Pragmas_Misplaced;
2777
2778          Specification_Loop : loop
2779
2780             Ident_Sloc := Token_Ptr;
2781             Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2782             Num_Idents := 1;
2783
2784             while Comma_Present loop
2785                Num_Idents := Num_Idents + 1;
2786                Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2787             end loop;
2788
2789             --  If there are multiple identifiers, we repeatedly scan the
2790             --  type and initialization expression information by resetting
2791             --  the scan pointer (so that we get completely separate trees
2792             --  for each occurrence).
2793
2794             if Num_Idents > 1 then
2795                Save_Scan_State (Scan_State);
2796             end if;
2797
2798             T_Colon;
2799
2800             --  Loop through defining identifiers in list
2801
2802             Ident := 1;
2803             Ident_Loop : loop
2804                Specification_Node :=
2805                  New_Node (N_Discriminant_Specification, Ident_Sloc);
2806                Set_Defining_Identifier (Specification_Node, Idents (Ident));
2807                Not_Null_Present :=  --  Ada 2005 (AI-231, AI-447)
2808                  P_Null_Exclusion (Allow_Anonymous_In_95 => True);
2809
2810                if Token = Tok_Access then
2811                   if Ada_Version = Ada_83 then
2812                      Error_Msg_SC
2813                        ("(Ada 83) access discriminant not allowed!");
2814                   end if;
2815
2816                   Set_Discriminant_Type
2817                     (Specification_Node,
2818                      P_Access_Definition (Not_Null_Present));
2819                else
2820
2821                   Set_Discriminant_Type
2822                     (Specification_Node, P_Subtype_Mark);
2823                   No_Constraint;
2824                   Set_Null_Exclusion_Present  -- Ada 2005 (AI-231)
2825                     (Specification_Node, Not_Null_Present);
2826                end if;
2827
2828                Set_Expression
2829                  (Specification_Node, Init_Expr_Opt (True));
2830
2831                if Ident > 1 then
2832                   Set_Prev_Ids (Specification_Node, True);
2833                end if;
2834
2835                if Ident < Num_Idents then
2836                   Set_More_Ids (Specification_Node, True);
2837                end if;
2838
2839                Append (Specification_Node, Specification_List);
2840                exit Ident_Loop when Ident = Num_Idents;
2841                Ident := Ident + 1;
2842                Restore_Scan_State (Scan_State);
2843                T_Colon;
2844             end loop Ident_Loop;
2845
2846             exit Specification_Loop when Token /= Tok_Semicolon;
2847             Scan; -- past ;
2848             P_Pragmas_Misplaced;
2849          end loop Specification_Loop;
2850
2851          T_Right_Paren;
2852          return Specification_List;
2853
2854       else
2855          return No_List;
2856       end if;
2857    end P_Known_Discriminant_Part_Opt;
2858
2859    -------------------------------------
2860    -- 3.7  DIscriminant Specification --
2861    -------------------------------------
2862
2863    --  Parsed by P_Known_Discriminant_Part_Opt (3.7)
2864
2865    -----------------------------
2866    -- 3.7  Default Expression --
2867    -----------------------------
2868
2869    --  Always parsed (simply as an Expression) by the parent construct
2870
2871    ------------------------------------
2872    -- 3.7.1  Discriminant Constraint --
2873    ------------------------------------
2874
2875    --  Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2876
2877    --------------------------------------------------------
2878    -- 3.7.1  Index or Discriminant Constraint (also 3.6) --
2879    --------------------------------------------------------
2880
2881    --  DISCRIMINANT_CONSTRAINT ::=
2882    --    (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
2883
2884    --  DISCRIMINANT_ASSOCIATION ::=
2885    --    [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2886    --      EXPRESSION
2887
2888    --  This routine parses either an index or a discriminant constraint. As
2889    --  is clear from the above grammar, it is often possible to clearly
2890    --  determine which of the two possibilities we have, but there are
2891    --  cases (those in which we have a series of expressions of the same
2892    --  syntactic form as subtype indications), where we cannot tell. Since
2893    --  this means that in any case the semantic phase has to distinguish
2894    --  between the two, there is not much point in the parser trying to
2895    --  distinguish even those cases where the difference is clear. In any
2896    --  case, if we have a situation like:
2897
2898    --     (A => 123, 235 .. 500)
2899
2900    --  it is not clear which of the two items is the wrong one, better to
2901    --  let the semantic phase give a clear message. Consequently, this
2902    --  routine in general returns a list of items which can be either
2903    --  discrete ranges or discriminant associations.
2904
2905    --  The caller has checked that the initial token is a left paren
2906
2907    --  Error recovery: can raise Error_Resync
2908
2909    function P_Index_Or_Discriminant_Constraint return Node_Id is
2910       Scan_State  : Saved_Scan_State;
2911       Constr_Node : Node_Id;
2912       Constr_List : List_Id;
2913       Expr_Node   : Node_Id;
2914       Result_Node : Node_Id;
2915
2916    begin
2917       Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
2918       Scan; -- past (
2919       Constr_List := New_List;
2920       Set_Constraints (Result_Node, Constr_List);
2921
2922       --  The two syntactic forms are a little mixed up, so what we are doing
2923       --  here is looking at the first entry to determine which case we have
2924
2925       --  A discriminant constraint is a list of discriminant associations,
2926       --  which have one of the following possible forms:
2927
2928       --    Expression
2929       --    Id => Expression
2930       --    Id | Id | .. | Id => Expression
2931
2932       --  An index constraint is a list of discrete ranges which have one
2933       --  of the following possible forms:
2934
2935       --    Subtype_Mark
2936       --    Subtype_Mark range Range
2937       --    Range_Attribute
2938       --    Simple_Expression .. Simple_Expression
2939
2940       --  Loop through discriminants in list
2941
2942       loop
2943          --  Check cases of Id => Expression or Id | Id => Expression
2944
2945          if Token = Tok_Identifier then
2946             Save_Scan_State (Scan_State); -- at Id
2947             Scan; -- past Id
2948
2949             if Token = Tok_Arrow or else Token = Tok_Vertical_Bar then
2950                Restore_Scan_State (Scan_State); -- to Id
2951                Append (P_Discriminant_Association, Constr_List);
2952                goto Loop_Continue;
2953             else
2954                Restore_Scan_State (Scan_State); -- to Id
2955             end if;
2956          end if;
2957
2958          --  Otherwise scan out an expression and see what we have got
2959
2960          Expr_Node := P_Expression_Or_Range_Attribute;
2961
2962          if Expr_Form = EF_Range_Attr then
2963             Append (Expr_Node, Constr_List);
2964
2965          elsif Token = Tok_Range then
2966             if Expr_Form /= EF_Simple_Name then
2967                Error_Msg_SC ("subtype mark required before RANGE");
2968             end if;
2969
2970             Append (P_Subtype_Indication (Expr_Node), Constr_List);
2971             goto Loop_Continue;
2972
2973          --  Check Simple_Expression .. Simple_Expression case
2974
2975          elsif Token = Tok_Dot_Dot then
2976             Check_Simple_Expression (Expr_Node);
2977             Constr_Node := New_Node (N_Range, Token_Ptr);
2978             Set_Low_Bound (Constr_Node, Expr_Node);
2979             Scan; -- past ..
2980             Expr_Node := P_Expression;
2981             Check_Simple_Expression (Expr_Node);
2982             Set_High_Bound (Constr_Node, Expr_Node);
2983             Append (Constr_Node, Constr_List);
2984             goto Loop_Continue;
2985
2986          --  Case of an expression which could be either form
2987
2988          else
2989             Append (Expr_Node, Constr_List);
2990             goto Loop_Continue;
2991          end if;
2992
2993          --  Here with a single entry scanned
2994
2995          <<Loop_Continue>>
2996             exit when not Comma_Present;
2997
2998       end loop;
2999
3000       T_Right_Paren;
3001       return Result_Node;
3002    end P_Index_Or_Discriminant_Constraint;
3003
3004    -------------------------------------
3005    -- 3.7.1  Discriminant Association --
3006    -------------------------------------
3007
3008    --  DISCRIMINANT_ASSOCIATION ::=
3009    --    [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3010    --      EXPRESSION
3011
3012    --  This routine is used only when the name list is present and the caller
3013    --  has already checked this (by scanning ahead and repositioning the
3014    --  scan).
3015
3016    --  Error_Recovery: cannot raise Error_Resync;
3017
3018    function P_Discriminant_Association return Node_Id is
3019       Discr_Node : Node_Id;
3020       Names_List : List_Id;
3021       Ident_Sloc : Source_Ptr;
3022
3023    begin
3024       Ident_Sloc := Token_Ptr;
3025       Names_List := New_List;
3026
3027       loop
3028          Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
3029          exit when Token /= Tok_Vertical_Bar;
3030          Scan; -- past |
3031       end loop;
3032
3033       Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
3034       Set_Selector_Names (Discr_Node, Names_List);
3035       TF_Arrow;
3036       Set_Expression (Discr_Node, P_Expression);
3037       return Discr_Node;
3038    end P_Discriminant_Association;
3039
3040    ---------------------------------
3041    -- 3.8  Record Type Definition --
3042    ---------------------------------
3043
3044    --  RECORD_TYPE_DEFINITION ::=
3045    --    [[abstract] tagged] [limited] RECORD_DEFINITION
3046
3047    --  There is no node in the tree for a record type definition. Instead
3048    --  a record definition node appears, with possible Abstract_Present,
3049    --  Tagged_Present, and Limited_Present flags set appropriately.
3050
3051    ----------------------------
3052    -- 3.8  Record Definition --
3053    ----------------------------
3054
3055    --  RECORD_DEFINITION ::=
3056    --    record
3057    --      COMPONENT_LIST
3058    --    end record
3059    --  | null record
3060
3061    --  Note: in the case where a record definition node is used to represent
3062    --  a record type definition, the caller sets the Tagged_Present and
3063    --  Limited_Present flags in the resulting N_Record_Definition node as
3064    --  required.
3065
3066    --  Note that the RECORD token at the start may be missing in certain
3067    --  error situations, so this function is expected to post the error
3068
3069    --  Error recovery: can raise Error_Resync
3070
3071    function P_Record_Definition return Node_Id is
3072       Rec_Node : Node_Id;
3073
3074    begin
3075       Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
3076
3077       --  Null record case
3078
3079       if Token = Tok_Null then
3080          Scan; -- past NULL
3081          T_Record;
3082          Set_Null_Present (Rec_Node, True);
3083
3084       --  Case starting with RECORD keyword. Build scope stack entry. For the
3085       --  column, we use the first non-blank character on the line, to deal
3086       --  with situations such as:
3087
3088       --    type X is record
3089       --      ...
3090       --    end record;
3091
3092       --  which is not official RM indentation, but is not uncommon usage
3093
3094       else
3095          Push_Scope_Stack;
3096          Scope.Table (Scope.Last).Etyp := E_Record;
3097          Scope.Table (Scope.Last).Ecol := Start_Column;
3098          Scope.Table (Scope.Last).Sloc := Token_Ptr;
3099          Scope.Table (Scope.Last).Labl := Error;
3100          Scope.Table (Scope.Last).Junk := (Token /= Tok_Record);
3101
3102          T_Record;
3103
3104          Set_Component_List (Rec_Node, P_Component_List);
3105
3106          loop
3107             exit when Check_End;
3108             Discard_Junk_Node (P_Component_List);
3109          end loop;
3110       end if;
3111
3112       return Rec_Node;
3113    end P_Record_Definition;
3114
3115    -------------------------
3116    -- 3.8  Component List --
3117    -------------------------
3118
3119    --  COMPONENT_LIST ::=
3120    --    COMPONENT_ITEM {COMPONENT_ITEM}
3121    --  | {COMPONENT_ITEM} VARIANT_PART
3122    --  | null;
3123
3124    --  Error recovery: cannot raise Error_Resync
3125
3126    function P_Component_List return Node_Id is
3127       Component_List_Node : Node_Id;
3128       Decls_List          : List_Id;
3129       Scan_State          : Saved_Scan_State;
3130
3131    begin
3132       Component_List_Node := New_Node (N_Component_List, Token_Ptr);
3133       Decls_List := New_List;
3134
3135       if Token = Tok_Null then
3136          Scan; -- past NULL
3137          TF_Semicolon;
3138          P_Pragmas_Opt (Decls_List);
3139          Set_Null_Present (Component_List_Node, True);
3140          return Component_List_Node;
3141
3142       else
3143          P_Pragmas_Opt (Decls_List);
3144
3145          if Token /= Tok_Case then
3146             Component_Scan_Loop : loop
3147                P_Component_Items (Decls_List);
3148                P_Pragmas_Opt (Decls_List);
3149
3150                exit Component_Scan_Loop when Token = Tok_End
3151                  or else Token = Tok_Case
3152                  or else Token = Tok_When;
3153
3154                --  We are done if we do not have an identifier. However, if
3155                --  we have a misspelled reserved identifier that is in a column
3156                --  to the right of the record definition, we will treat it as
3157                --  an identifier. It turns out to be too dangerous in practice
3158                --  to accept such a mis-spelled identifier which does not have
3159                --  this additional clue that confirms the incorrect spelling.
3160
3161                if Token /= Tok_Identifier then
3162                   if Start_Column > Scope.Table (Scope.Last).Ecol
3163                     and then Is_Reserved_Identifier
3164                   then
3165                      Save_Scan_State (Scan_State); -- at reserved id
3166                      Scan; -- possible reserved id
3167
3168                      if Token = Tok_Comma or else Token = Tok_Colon then
3169                         Restore_Scan_State (Scan_State);
3170                         Scan_Reserved_Identifier (Force_Msg => True);
3171
3172                      --  Note reserved identifier used as field name after
3173                      --  all because not followed by colon or comma
3174
3175                      else
3176                         Restore_Scan_State (Scan_State);
3177                         exit Component_Scan_Loop;
3178                      end if;
3179
3180                   --  Non-identifier that definitely was not reserved id
3181
3182                   else
3183                      exit Component_Scan_Loop;
3184                   end if;
3185                end if;
3186             end loop Component_Scan_Loop;
3187          end if;
3188
3189          if Token = Tok_Case then
3190             Set_Variant_Part (Component_List_Node, P_Variant_Part);
3191
3192             --  Check for junk after variant part
3193
3194             if Token = Tok_Identifier then
3195                Save_Scan_State (Scan_State);
3196                Scan; -- past identifier
3197
3198                if Token = Tok_Colon then
3199                   Restore_Scan_State (Scan_State);
3200                   Error_Msg_SC ("component may not follow variant part");
3201                   Discard_Junk_Node (P_Component_List);
3202
3203                elsif Token = Tok_Case then
3204                   Restore_Scan_State (Scan_State);
3205                   Error_Msg_SC ("only one variant part allowed in a record");
3206                   Discard_Junk_Node (P_Component_List);
3207
3208                else
3209                   Restore_Scan_State (Scan_State);
3210                end if;
3211             end if;
3212          end if;
3213       end if;
3214
3215       Set_Component_Items (Component_List_Node, Decls_List);
3216       return Component_List_Node;
3217    end P_Component_List;
3218
3219    -------------------------
3220    -- 3.8  Component Item --
3221    -------------------------
3222
3223    --  COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
3224
3225    --  COMPONENT_DECLARATION ::=
3226    --    DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
3227    --      [:= DEFAULT_EXPRESSION];
3228
3229    --  COMPONENT_DEFINITION ::=
3230    --    [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
3231
3232    --  Error recovery: cannot raise Error_Resync, if an error occurs,
3233    --  the scan is positioned past the following semicolon.
3234
3235    --  Note: we do not yet allow representation clauses to appear as component
3236    --  items, do we need to add this capability sometime in the future ???
3237
3238    procedure P_Component_Items (Decls : List_Id) is
3239       Aliased_Present  : Boolean := False;
3240       CompDef_Node     : Node_Id;
3241       Decl_Node        : Node_Id;
3242       Scan_State       : Saved_Scan_State;
3243       Not_Null_Present : Boolean := False;
3244       Num_Idents       : Nat;
3245       Ident            : Nat;
3246       Ident_Sloc       : Source_Ptr;
3247
3248       Idents : array (Int range 1 .. 4096) of Entity_Id;
3249       --  This array holds the list of defining identifiers. The upper bound
3250       --  of 4096 is intended to be essentially infinite, and we do not even
3251       --  bother to check for it being exceeded.
3252
3253    begin
3254       if Token /= Tok_Identifier then
3255          Error_Msg_SC ("component declaration expected");
3256          Resync_Past_Semicolon;
3257          return;
3258       end if;
3259
3260       Ident_Sloc := Token_Ptr;
3261       Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3262       Num_Idents := 1;
3263
3264       while Comma_Present loop
3265          Num_Idents := Num_Idents + 1;
3266          Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3267       end loop;
3268
3269       --  If there are multiple identifiers, we repeatedly scan the
3270       --  type and initialization expression information by resetting
3271       --  the scan pointer (so that we get completely separate trees
3272       --  for each occurrence).
3273
3274       if Num_Idents > 1 then
3275          Save_Scan_State (Scan_State);
3276       end if;
3277
3278       T_Colon;
3279
3280       --  Loop through defining identifiers in list
3281
3282       Ident := 1;
3283       Ident_Loop : loop
3284
3285          --  The following block is present to catch Error_Resync
3286          --  which causes the parse to be reset past the semicolon
3287
3288          begin
3289             Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
3290             Set_Defining_Identifier (Decl_Node, Idents (Ident));
3291
3292             if Token = Tok_Constant then
3293                Error_Msg_SC ("constant components are not permitted");
3294                Scan;
3295             end if;
3296
3297             CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
3298
3299             if Token_Name = Name_Aliased then
3300                Check_95_Keyword (Tok_Aliased, Tok_Identifier);
3301             end if;
3302
3303             if Token = Tok_Aliased then
3304                Aliased_Present := True;
3305                Scan; -- past ALIASED
3306             end if;
3307
3308             Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
3309
3310             --  Ada 2005 (AI-230): Access Definition case
3311
3312             if Token = Tok_Access then
3313                if Ada_Version < Ada_05 then
3314                   Error_Msg_SP
3315                     ("generalized use of anonymous access types " &
3316                      "is an Ada 2005 extension");
3317                   Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3318                end if;
3319
3320                if Aliased_Present then
3321                   Error_Msg_SP ("ALIASED not allowed here");
3322                end if;
3323
3324                Set_Subtype_Indication (CompDef_Node, Empty);
3325                Set_Aliased_Present    (CompDef_Node, False);
3326                Set_Access_Definition  (CompDef_Node,
3327                  P_Access_Definition (Not_Null_Present));
3328             else
3329
3330                Set_Access_Definition      (CompDef_Node, Empty);
3331                Set_Aliased_Present        (CompDef_Node, Aliased_Present);
3332                Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
3333
3334                if Token = Tok_Array then
3335                   Error_Msg_SC
3336                     ("anonymous arrays not allowed as components");
3337                   raise Error_Resync;
3338                end if;
3339
3340                Set_Subtype_Indication (CompDef_Node,
3341                  P_Subtype_Indication (Not_Null_Present));
3342             end if;
3343
3344             Set_Component_Definition (Decl_Node, CompDef_Node);
3345             Set_Expression           (Decl_Node, Init_Expr_Opt);
3346
3347             if Ident > 1 then
3348                Set_Prev_Ids (Decl_Node, True);
3349             end if;
3350
3351             if Ident < Num_Idents then
3352                Set_More_Ids (Decl_Node, True);
3353             end if;
3354
3355             Append (Decl_Node, Decls);
3356
3357          exception
3358             when Error_Resync =>
3359                if Token /= Tok_End then
3360                   Resync_Past_Semicolon;
3361                end if;
3362          end;
3363
3364          exit Ident_Loop when Ident = Num_Idents;
3365          Ident := Ident + 1;
3366          Restore_Scan_State (Scan_State);
3367          T_Colon;
3368
3369       end loop Ident_Loop;
3370
3371       TF_Semicolon;
3372    end P_Component_Items;
3373
3374    --------------------------------
3375    -- 3.8  Component Declaration --
3376    --------------------------------
3377
3378    --  Parsed by P_Component_Items (3.8)
3379
3380    -------------------------
3381    -- 3.8.1  Variant Part --
3382    -------------------------
3383
3384    --  VARIANT_PART ::=
3385    --    case discriminant_DIRECT_NAME is
3386    --      VARIANT
3387    --      {VARIANT}
3388    --    end case;
3389
3390    --  The caller has checked that the initial token is CASE
3391
3392    --  Error recovery: cannot raise Error_Resync
3393
3394    function P_Variant_Part return Node_Id is
3395       Variant_Part_Node : Node_Id;
3396       Variants_List     : List_Id;
3397       Case_Node         : Node_Id;
3398       Ident_Token       : Token_Type;
3399
3400    begin
3401       Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
3402       Push_Scope_Stack;
3403       Scope.Table (Scope.Last).Etyp := E_Case;
3404       Scope.Table (Scope.Last).Sloc := Token_Ptr;
3405       Scope.Table (Scope.Last).Ecol := Start_Column;
3406
3407       Scan; -- past CASE
3408
3409       --  A discriminant name between parentheses will be returned as
3410       --  a N_Identifier although it is not allowed by RM 3.8.1. We
3411       --  save the token type to check it later. However, in case of
3412       --  a discriminant name with parentheses, we can continue the
3413       --  analysis as if only the discriminant name had been given.
3414
3415       Ident_Token := Token;
3416       Case_Node := P_Expression;
3417
3418       if Nkind (Case_Node) = N_Identifier then
3419          Set_Name (Variant_Part_Node, Case_Node);
3420       else
3421          Set_Name (Variant_Part_Node, Error);
3422       end if;
3423
3424       if Nkind (Case_Node) /= N_Identifier
3425         or else Ident_Token /= Tok_Identifier
3426       then
3427          Error_Msg ("discriminant name expected", Sloc (Case_Node));
3428       end if;
3429
3430       TF_Is;
3431       Variants_List := New_List;
3432       P_Pragmas_Opt (Variants_List);
3433
3434       --  Test missing variant
3435
3436       if Token = Tok_End then
3437          Error_Msg_BC ("WHEN expected (must have at least one variant)");
3438       else
3439          Append (P_Variant, Variants_List);
3440       end if;
3441
3442       --  Loop through variants, note that we allow if in place of when,
3443       --  this error will be detected and handled in P_Variant.
3444
3445       loop
3446          P_Pragmas_Opt (Variants_List);
3447
3448          if Token /= Tok_When
3449            and then Token /= Tok_If
3450            and then Token /= Tok_Others
3451          then
3452             exit when Check_End;
3453          end if;
3454
3455          Append (P_Variant, Variants_List);
3456       end loop;
3457
3458       Set_Variants (Variant_Part_Node, Variants_List);
3459       return Variant_Part_Node;
3460    end P_Variant_Part;
3461
3462    --------------------
3463    -- 3.8.1  Variant --
3464    --------------------
3465
3466    --  VARIANT ::=
3467    --    when DISCRETE_CHOICE_LIST =>
3468    --      COMPONENT_LIST
3469
3470    --  Error recovery: cannot raise Error_Resync
3471
3472    --  The initial token on entry is either WHEN, IF or OTHERS
3473
3474    function P_Variant return Node_Id is
3475       Variant_Node : Node_Id;
3476
3477    begin
3478       --  Special check to recover nicely from use of IF in place of WHEN
3479
3480       if Token = Tok_If then
3481          T_When;
3482          Scan; -- past IF
3483       else
3484          T_When;
3485       end if;
3486
3487       Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3488       Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3489       TF_Arrow;
3490       Set_Component_List (Variant_Node, P_Component_List);
3491       return Variant_Node;
3492    end P_Variant;
3493
3494    ---------------------------------
3495    -- 3.8.1  Discrete Choice List --
3496    ---------------------------------
3497
3498    --  DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
3499
3500    --  DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
3501
3502    --  Note: in Ada 83, the expression must be a simple expression
3503
3504    --  Error recovery: cannot raise Error_Resync
3505
3506    function P_Discrete_Choice_List return List_Id is
3507       Choices     : List_Id;
3508       Expr_Node   : Node_Id;
3509       Choice_Node : Node_Id;
3510
3511    begin
3512       Choices := New_List;
3513
3514       loop
3515          if Token = Tok_Others then
3516             Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
3517             Scan; -- past OTHERS
3518
3519          else
3520             begin
3521                Expr_Node := P_Expression_Or_Range_Attribute;
3522                Check_No_Right_Paren;
3523
3524                if Token = Tok_Colon
3525                  and then Nkind (Expr_Node) = N_Identifier
3526                then
3527                   Error_Msg_SP ("label not permitted in this context");
3528                   Scan; -- past colon
3529
3530                elsif Expr_Form = EF_Range_Attr then
3531                   Append (Expr_Node, Choices);
3532
3533                elsif Token = Tok_Dot_Dot then
3534                   Check_Simple_Expression (Expr_Node);
3535                   Choice_Node := New_Node (N_Range, Token_Ptr);
3536                   Set_Low_Bound (Choice_Node, Expr_Node);
3537                   Scan; -- past ..
3538                   Expr_Node := P_Expression_No_Right_Paren;
3539                   Check_Simple_Expression (Expr_Node);
3540                   Set_High_Bound (Choice_Node, Expr_Node);
3541                   Append (Choice_Node, Choices);
3542
3543                elsif Expr_Form = EF_Simple_Name then
3544                   if Token = Tok_Range then
3545                      Append (P_Subtype_Indication (Expr_Node), Choices);
3546
3547                   elsif Token in Token_Class_Consk then
3548                      Error_Msg_SC
3549                         ("the only constraint allowed here " &
3550                          "is a range constraint");
3551                      Discard_Junk_Node (P_Constraint_Opt);
3552                      Append (Expr_Node, Choices);
3553
3554                   else
3555                      Append (Expr_Node, Choices);
3556                   end if;
3557
3558                else
3559                   Check_Simple_Expression_In_Ada_83 (Expr_Node);
3560                   Append (Expr_Node, Choices);
3561                end if;
3562
3563             exception
3564                when Error_Resync =>
3565                   Resync_Choice;
3566                   return Error_List;
3567             end;
3568          end if;
3569
3570          if Token = Tok_Comma then
3571             Error_Msg_SC (""","" should be ""'|""");
3572          else
3573             exit when Token /= Tok_Vertical_Bar;
3574          end if;
3575
3576          Scan; -- past | or comma
3577       end loop;
3578
3579       return Choices;
3580    end P_Discrete_Choice_List;
3581
3582    ----------------------------
3583    -- 3.8.1  Discrete Choice --
3584    ----------------------------
3585
3586    --  Parsed by P_Discrete_Choice_List (3.8.1)
3587
3588    ----------------------------------
3589    -- 3.9.1  Record Extension Part --
3590    ----------------------------------
3591
3592    --  RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3593
3594    --  Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3595
3596    --------------------------------------
3597    -- 3.9.4  Interface Type Definition --
3598    --------------------------------------
3599
3600    --  INTERFACE_TYPE_DEFINITION ::=
3601    --    [limited | task | protected | synchronized] interface
3602    --      [and INTERFACE_LIST]
3603
3604    --  Error recovery: cannot raise Error_Resync
3605
3606    function P_Interface_Type_Definition
3607      (Abstract_Present : Boolean) return Node_Id
3608    is
3609       Typedef_Node : Node_Id;
3610
3611    begin
3612       if Ada_Version < Ada_05 then
3613          Error_Msg_SP ("abstract interface is an Ada 2005 extension");
3614          Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3615       end if;
3616
3617       if Abstract_Present then
3618          Error_Msg_SP ("ABSTRACT not allowed in interface type definition " &
3619                        "(RM 3.9.4(2/2))");
3620       end if;
3621
3622       Scan; -- past INTERFACE
3623
3624       --  Ada 2005 (AI-345): In case of interfaces with a null list of
3625       --  interfaces we build a record_definition node.
3626
3627       if Token = Tok_Semicolon then
3628          Typedef_Node := New_Node (N_Record_Definition, Token_Ptr);
3629
3630          Set_Abstract_Present  (Typedef_Node);
3631          Set_Tagged_Present    (Typedef_Node);
3632          Set_Null_Present      (Typedef_Node);
3633          Set_Interface_Present (Typedef_Node);
3634
3635       --  Ada 2005 (AI-251): In case of not-synchronized interfaces that have
3636       --  a list of interfaces we build a derived_type_definition node. This
3637       --  simplifies the semantic analysis (and hence further mainteinance)
3638
3639       else
3640          if Token /= Tok_And then
3641             Error_Msg_AP ("AND expected");
3642          else
3643             Scan; -- past AND
3644          end if;
3645
3646          Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
3647
3648          Set_Abstract_Present   (Typedef_Node);
3649          Set_Interface_Present  (Typedef_Node);
3650          Set_Subtype_Indication (Typedef_Node, P_Qualified_Simple_Name);
3651
3652          Set_Record_Extension_Part (Typedef_Node,
3653            New_Node (N_Record_Definition, Token_Ptr));
3654          Set_Null_Present (Record_Extension_Part (Typedef_Node));
3655
3656          if Token = Tok_And then
3657             Set_Interface_List (Typedef_Node, New_List);
3658             Scan; -- past AND
3659
3660             loop
3661                Append (P_Qualified_Simple_Name,
3662                        Interface_List (Typedef_Node));
3663                exit when Token /= Tok_And;
3664                Scan; -- past AND
3665             end loop;
3666          end if;
3667       end if;
3668
3669       return Typedef_Node;
3670    end P_Interface_Type_Definition;
3671
3672    ----------------------------------
3673    -- 3.10  Access Type Definition --
3674    ----------------------------------
3675
3676    --  ACCESS_TYPE_DEFINITION ::=
3677    --    ACCESS_TO_OBJECT_DEFINITION
3678    --  | ACCESS_TO_SUBPROGRAM_DEFINITION
3679
3680    --  ACCESS_TO_OBJECT_DEFINITION ::=
3681    --    [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3682
3683    --  GENERAL_ACCESS_MODIFIER ::= all | constant
3684
3685    --  ACCESS_TO_SUBPROGRAM_DEFINITION
3686    --    [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3687    --  | [NULL_EXCLUSION] access [protected] function
3688    --    PARAMETER_AND_RESULT_PROFILE
3689
3690    --  PARAMETER_PROFILE ::= [FORMAL_PART]
3691
3692    --  PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3693
3694    --  Ada 2005 (AI-254): If Header_Already_Parsed then the caller has already
3695    --  parsed the null_exclusion part and has also removed the ACCESS token;
3696    --  otherwise the caller has just checked that the initial token is ACCESS
3697
3698    --  Error recovery: can raise Error_Resync
3699
3700    function P_Access_Type_Definition
3701      (Header_Already_Parsed : Boolean := False) return Node_Id
3702    is
3703       Access_Loc       : constant Source_Ptr := Token_Ptr;
3704       Prot_Flag        : Boolean;
3705       Not_Null_Present : Boolean := False;
3706       Type_Def_Node    : Node_Id;
3707       Result_Not_Null  : Boolean;
3708       Result_Node      : Node_Id;
3709
3710       procedure Check_Junk_Subprogram_Name;
3711       --  Used in access to subprogram definition cases to check for an
3712       --  identifier or operator symbol that does not belong.
3713
3714       --------------------------------
3715       -- Check_Junk_Subprogram_Name --
3716       --------------------------------
3717
3718       procedure Check_Junk_Subprogram_Name is
3719          Saved_State : Saved_Scan_State;
3720
3721       begin
3722          if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
3723             Save_Scan_State (Saved_State);
3724             Scan; -- past possible junk subprogram name
3725
3726             if Token = Tok_Left_Paren or else Token = Tok_Semicolon then
3727                Error_Msg_SP ("unexpected subprogram name ignored");
3728                return;
3729
3730             else
3731                Restore_Scan_State (Saved_State);
3732             end if;
3733          end if;
3734       end Check_Junk_Subprogram_Name;
3735
3736    --  Start of processing for P_Access_Type_Definition
3737
3738    begin
3739       if not Header_Already_Parsed then
3740          Not_Null_Present := P_Null_Exclusion;         --  Ada 2005 (AI-231)
3741          Scan; -- past ACCESS
3742       end if;
3743
3744       if Token_Name = Name_Protected then
3745          Check_95_Keyword (Tok_Protected, Tok_Procedure);
3746          Check_95_Keyword (Tok_Protected, Tok_Function);
3747       end if;
3748
3749       Prot_Flag := (Token = Tok_Protected);
3750
3751       if Prot_Flag then
3752          Scan; -- past PROTECTED
3753
3754          if Token /= Tok_Procedure and then Token /= Tok_Function then
3755             Error_Msg_SC ("FUNCTION or PROCEDURE expected");
3756          end if;
3757       end if;
3758
3759       if Token = Tok_Procedure then
3760          if Ada_Version = Ada_83 then
3761             Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
3762          end if;
3763
3764          Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
3765          Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3766          Scan; -- past PROCEDURE
3767          Check_Junk_Subprogram_Name;
3768          Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3769          Set_Protected_Present (Type_Def_Node, Prot_Flag);
3770
3771       elsif Token = Tok_Function then
3772          if Ada_Version = Ada_83 then
3773             Error_Msg_SC ("(Ada 83) access to function not allowed!");
3774          end if;
3775
3776          Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
3777          Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3778          Scan; -- past FUNCTION
3779          Check_Junk_Subprogram_Name;
3780          Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3781          Set_Protected_Present (Type_Def_Node, Prot_Flag);
3782          TF_Return;
3783
3784          Result_Not_Null := P_Null_Exclusion;     --  Ada 2005 (AI-231)
3785
3786          --  Ada 2005 (AI-318-02)
3787
3788          if Token = Tok_Access then
3789             if Ada_Version < Ada_05 then
3790                Error_Msg_SC
3791                  ("anonymous access result type is an Ada 2005 extension");
3792                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
3793             end if;
3794
3795             Result_Node := P_Access_Definition (Result_Not_Null);
3796
3797          else
3798             Result_Node := P_Subtype_Mark;
3799             No_Constraint;
3800          end if;
3801
3802          --  Note: A null exclusion given on the result type needs to
3803          --  be coded by a distinct flag, since Null_Exclusion_Present
3804          --  on an access-to-function type pertains to a null exclusion
3805          --  on the access type itself (as set above). ???
3806          --  Set_Null_Exclusion_Present??? (Type_Def_Node, Result_Not_Null);
3807
3808          Set_Result_Definition (Type_Def_Node, Result_Node);
3809
3810       else
3811          Type_Def_Node :=
3812            New_Node (N_Access_To_Object_Definition, Access_Loc);
3813          Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3814
3815          if Token = Tok_All or else Token = Tok_Constant then
3816             if Ada_Version = Ada_83 then
3817                Error_Msg_SC ("(Ada 83) access modifier not allowed!");
3818             end if;
3819
3820             if Token = Tok_All then
3821                Set_All_Present (Type_Def_Node, True);
3822
3823             else
3824                Set_Constant_Present (Type_Def_Node, True);
3825             end if;
3826
3827             Scan; -- past ALL or CONSTANT
3828          end if;
3829
3830          Set_Subtype_Indication (Type_Def_Node,
3831             P_Subtype_Indication (Not_Null_Present));
3832       end if;
3833
3834       return Type_Def_Node;
3835    end P_Access_Type_Definition;
3836
3837    ---------------------------------------
3838    -- 3.10  Access To Object Definition --
3839    ---------------------------------------
3840
3841    --  Parsed by P_Access_Type_Definition (3.10)
3842
3843    -----------------------------------
3844    -- 3.10  General Access Modifier --
3845    -----------------------------------
3846
3847    --  Parsed by P_Access_Type_Definition (3.10)
3848
3849    -------------------------------------------
3850    -- 3.10  Access To Subprogram Definition --
3851    -------------------------------------------
3852
3853    --  Parsed by P_Access_Type_Definition (3.10)
3854
3855    -----------------------------
3856    -- 3.10  Access Definition --
3857    -----------------------------
3858
3859    --  ACCESS_DEFINITION ::=
3860    --    [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
3861    --  | ACCESS_TO_SUBPROGRAM_DEFINITION
3862    --
3863    --  ACCESS_TO_SUBPROGRAM_DEFINITION
3864    --    [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3865    --  | [NULL_EXCLUSION] access [protected] function
3866    --    PARAMETER_AND_RESULT_PROFILE
3867
3868    --  The caller has parsed the null-exclusion part and it has also checked
3869    --  that the next token is ACCESS
3870
3871    --  Error recovery: cannot raise Error_Resync
3872
3873    function P_Access_Definition
3874      (Null_Exclusion_Present : Boolean) return Node_Id
3875    is
3876       Def_Node  : Node_Id;
3877       Subp_Node : Node_Id;
3878
3879    begin
3880       Def_Node := New_Node (N_Access_Definition, Token_Ptr);
3881       Scan; -- past ACCESS
3882
3883       --  Ada 2005 (AI-254): Access_To_Subprogram_Definition
3884
3885       if Token = Tok_Protected
3886         or else Token = Tok_Procedure
3887         or else Token = Tok_Function
3888       then
3889          if Ada_Version < Ada_05 then
3890             Error_Msg_SP ("access-to-subprogram is an Ada 2005 extension");
3891             Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
3892          end if;
3893
3894          Subp_Node := P_Access_Type_Definition (Header_Already_Parsed => True);
3895          Set_Null_Exclusion_Present (Subp_Node, Null_Exclusion_Present);
3896          Set_Access_To_Subprogram_Definition (Def_Node, Subp_Node);
3897
3898       --  Ada 2005 (AI-231)
3899       --  [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
3900
3901       else
3902          Set_Null_Exclusion_Present (Def_Node, Null_Exclusion_Present);
3903
3904          if Token = Tok_All then
3905             if Ada_Version < Ada_05 then
3906                Error_Msg_SP
3907                  ("access-all in this context is an Ada 2005 extension");
3908                Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
3909             end if;
3910
3911             Scan; -- past ALL
3912             Set_All_Present (Def_Node);
3913
3914          elsif Token = Tok_Constant then
3915             if Ada_Version < Ada_05 then
3916                Error_Msg_SP ("access-to-constant is an Ada 2005 extension");
3917                Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
3918             end if;
3919
3920             Scan; -- past CONSTANT
3921             Set_Constant_Present (Def_Node);
3922          end if;
3923
3924          Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
3925          No_Constraint;
3926       end if;
3927
3928       return Def_Node;
3929    end P_Access_Definition;
3930
3931    -----------------------------------------
3932    -- 3.10.1  Incomplete Type Declaration --
3933    -----------------------------------------
3934
3935    --  Parsed by P_Type_Declaration (3.2.1)
3936
3937    ----------------------------
3938    -- 3.11  Declarative Part --
3939    ----------------------------
3940
3941    --  DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
3942
3943    --  Error recovery: cannot raise Error_Resync (because P_Declarative_Items
3944    --  handles errors, and returns cleanly after an error has occurred)
3945
3946    function P_Declarative_Part return List_Id is
3947       Decls : List_Id;
3948       Done  : Boolean;
3949
3950    begin
3951       --  Indicate no bad declarations detected yet. This will be reset by
3952       --  P_Declarative_Items if a bad declaration is discovered.
3953
3954       Missing_Begin_Msg := No_Error_Msg;
3955
3956       --  Get rid of active SIS entry from outer scope. This means we will
3957       --  miss some nested cases, but it doesn't seem worth the effort. See
3958       --  discussion in Par for further details
3959
3960       SIS_Entry_Active := False;
3961       Decls := New_List;
3962
3963       --  Loop to scan out the declarations
3964
3965       loop
3966          P_Declarative_Items (Decls, Done, In_Spec => False);
3967          exit when Done;
3968       end loop;
3969
3970       --  Get rid of active SIS entry which is left set only if we scanned a
3971       --  procedure declaration and have not found the body. We could give
3972       --  an error message, but that really would be usurping the role of
3973       --  semantic analysis (this really is a missing body case).
3974
3975       SIS_Entry_Active := False;
3976       return Decls;
3977    end P_Declarative_Part;
3978
3979    ----------------------------
3980    -- 3.11  Declarative Item --
3981    ----------------------------
3982
3983    --  DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
3984
3985    --  Can return Error if a junk declaration is found, or Empty if no
3986    --  declaration is found (i.e. a token ending declarations, such as
3987    --  BEGIN or END is encountered).
3988
3989    --  Error recovery: cannot raise Error_Resync. If an error resync occurs,
3990    --  then the scan is set past the next semicolon and Error is returned.
3991
3992    procedure P_Declarative_Items
3993      (Decls   : List_Id;
3994       Done    : out Boolean;
3995       In_Spec : Boolean)
3996    is
3997       Scan_State : Saved_Scan_State;
3998
3999    begin
4000       if Style_Check then
4001          Style.Check_Indentation;
4002       end if;
4003
4004       case Token is
4005
4006          when Tok_Function =>
4007             Check_Bad_Layout;
4008             Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4009             Done := False;
4010
4011          when Tok_For =>
4012             Check_Bad_Layout;
4013
4014             --  Check for loop (premature statement)
4015
4016             Save_Scan_State (Scan_State);
4017             Scan; -- past FOR
4018
4019             if Token = Tok_Identifier then
4020                Scan; -- past identifier
4021
4022                if Token = Tok_In then
4023                   Restore_Scan_State (Scan_State);
4024                   Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4025                   return;
4026                end if;
4027             end if;
4028
4029             --  Not a loop, so must be rep clause
4030
4031             Restore_Scan_State (Scan_State);
4032             Append (P_Representation_Clause, Decls);
4033             Done := False;
4034
4035          when Tok_Generic =>
4036             Check_Bad_Layout;
4037             Append (P_Generic, Decls);
4038             Done := False;
4039
4040          when Tok_Identifier =>
4041             Check_Bad_Layout;
4042             P_Identifier_Declarations (Decls, Done, In_Spec);
4043
4044          --  Ada2005: A subprogram declaration can start with "not" or
4045          --  "overriding". In older versions, "overriding" is handled
4046          --  like an identifier, with the appropriate warning.
4047
4048          when Tok_Not =>
4049             Check_Bad_Layout;
4050             Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4051             Done := False;
4052
4053          when Tok_Overriding =>
4054             Check_Bad_Layout;
4055             Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4056             Done := False;
4057
4058          when Tok_Package =>
4059             Check_Bad_Layout;
4060             Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4061             Done := False;
4062
4063          when Tok_Pragma =>
4064             Append (P_Pragma, Decls);
4065             Done := False;
4066
4067          when Tok_Procedure =>
4068             Check_Bad_Layout;
4069             Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4070             Done := False;
4071
4072          when Tok_Protected =>
4073             Check_Bad_Layout;
4074             Scan; -- past PROTECTED
4075             Append (P_Protected, Decls);
4076             Done := False;
4077
4078          when Tok_Subtype =>
4079             Check_Bad_Layout;
4080             Append (P_Subtype_Declaration, Decls);
4081             Done := False;
4082
4083          when Tok_Task =>
4084             Check_Bad_Layout;
4085             Scan; -- past TASK
4086             Append (P_Task, Decls);
4087             Done := False;
4088
4089          when Tok_Type =>
4090             Check_Bad_Layout;
4091             Append (P_Type_Declaration, Decls);
4092             Done := False;
4093
4094          when Tok_Use =>
4095             Check_Bad_Layout;
4096             Append (P_Use_Clause, Decls);
4097             Done := False;
4098
4099          when Tok_With =>
4100             Check_Bad_Layout;
4101             Error_Msg_SC ("WITH can only appear in context clause");
4102             raise Error_Resync;
4103
4104          --  BEGIN terminates the scan of a sequence of declarations unless
4105          --  there is a missing subprogram body, see section on handling
4106          --  semicolon in place of IS. We only treat the begin as satisfying
4107          --  the subprogram declaration if it falls in the expected column
4108          --  or to its right.
4109
4110          when Tok_Begin =>
4111             if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
4112
4113                --  Here we have the case where a BEGIN is encountered during
4114                --  declarations in a declarative part, or at the outer level,
4115                --  and there is a subprogram declaration outstanding for which
4116                --  no body has been supplied. This is the case where we assume
4117                --  that the semicolon in the subprogram declaration should
4118                --  really have been is. The active SIS entry describes the
4119                --  subprogram declaration. On return the declaration has been
4120                --  modified to become a body.
4121
4122                declare
4123                   Specification_Node : Node_Id;
4124                   Decl_Node          : Node_Id;
4125                   Body_Node          : Node_Id;
4126
4127                begin
4128                   --  First issue the error message. If we had a missing
4129                   --  semicolon in the declaration, then change the message
4130                   --  to <missing "is">
4131
4132                   if SIS_Missing_Semicolon_Message /= No_Error_Msg then
4133                      Change_Error_Text     -- Replace: "missing "";"" "
4134                        (SIS_Missing_Semicolon_Message, "missing ""is""");
4135
4136                   --  Otherwise we saved the semicolon position, so complain
4137
4138                   else
4139                      Error_Msg (""";"" should be IS", SIS_Semicolon_Sloc);
4140                   end if;
4141
4142                   --  The next job is to fix up any declarations that occurred
4143                   --  between the procedure header and the BEGIN. These got
4144                   --  chained to the outer declarative region (immediately
4145                   --  after the procedure declaration) and they should be
4146                   --  chained to the subprogram itself, which is a body
4147                   --  rather than a spec.
4148
4149                   Specification_Node := Specification (SIS_Declaration_Node);
4150                   Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
4151                   Body_Node := SIS_Declaration_Node;
4152                   Set_Specification (Body_Node, Specification_Node);
4153                   Set_Declarations (Body_Node, New_List);
4154
4155                   loop
4156                      Decl_Node := Remove_Next (Body_Node);
4157                      exit when Decl_Node = Empty;
4158                      Append (Decl_Node, Declarations (Body_Node));
4159                   end loop;
4160
4161                   --  Now make the scope table entry for the Begin-End and
4162                   --  scan it out
4163
4164                   Push_Scope_Stack;
4165                   Scope.Table (Scope.Last).Sloc := SIS_Sloc;
4166                   Scope.Table (Scope.Last).Etyp := E_Name;
4167                   Scope.Table (Scope.Last).Ecol := SIS_Ecol;
4168                   Scope.Table (Scope.Last).Labl := SIS_Labl;
4169                   Scope.Table (Scope.Last).Lreq := False;
4170                   SIS_Entry_Active := False;
4171                   Scan; -- past BEGIN
4172                   Set_Handled_Statement_Sequence (Body_Node,
4173                     P_Handled_Sequence_Of_Statements);
4174                   End_Statements (Handled_Statement_Sequence (Body_Node));
4175                end;
4176
4177                Done := False;
4178
4179             else
4180                Done := True;
4181             end if;
4182
4183             --  Normally an END terminates the scan for basic declarative
4184             --  items. The one exception is END RECORD, which is probably
4185             --  left over from some other junk.
4186
4187             when Tok_End =>
4188                Save_Scan_State (Scan_State); -- at END
4189                Scan; -- past END
4190
4191                if Token = Tok_Record then
4192                   Error_Msg_SP ("no RECORD for this `end record`!");
4193                   Scan; -- past RECORD
4194                   TF_Semicolon;
4195
4196                else
4197                   Restore_Scan_State (Scan_State); -- to END
4198                   Done := True;
4199                end if;
4200
4201          --  The following tokens which can only be the start of a statement
4202          --  are considered to end a declarative part (i.e. we have a missing
4203          --  BEGIN situation). We are fairly conservative in making this
4204          --  judgment, because it is a real mess to go into statement mode
4205          --  prematurely in response to a junk declaration.
4206
4207          when Tok_Abort     |
4208               Tok_Accept    |
4209               Tok_Declare   |
4210               Tok_Delay     |
4211               Tok_Exit      |
4212               Tok_Goto      |
4213               Tok_If        |
4214               Tok_Loop      |
4215               Tok_Null      |
4216               Tok_Requeue   |
4217               Tok_Select    |
4218               Tok_While     =>
4219
4220             --  But before we decide that it's a statement, let's check for
4221             --  a reserved word misused as an identifier.
4222
4223             if Is_Reserved_Identifier then
4224                Save_Scan_State (Scan_State);
4225                Scan; -- past the token
4226
4227                --  If reserved identifier not followed by colon or comma, then
4228                --  this is most likely an assignment statement to the bad id.
4229
4230                if Token /= Tok_Colon and then Token /= Tok_Comma then
4231                   Restore_Scan_State (Scan_State);
4232                   Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4233                   return;
4234
4235                --  Otherwise we have a declaration of the bad id
4236
4237                else
4238                   Restore_Scan_State (Scan_State);
4239                   Scan_Reserved_Identifier (Force_Msg => True);
4240                   P_Identifier_Declarations (Decls, Done, In_Spec);
4241                end if;
4242
4243             --  If not reserved identifier, then it's definitely a statement
4244
4245             else
4246                Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4247                return;
4248             end if;
4249
4250          --  The token RETURN may well also signal a missing BEGIN situation,
4251          --  however, we never let it end the declarative part, because it may
4252          --  also be part of a half-baked function declaration.
4253
4254          when Tok_Return =>
4255             Error_Msg_SC ("misplaced RETURN statement");
4256             raise Error_Resync;
4257
4258          --  PRIVATE definitely terminates the declarations in a spec,
4259          --  and is an error in a body.
4260
4261          when Tok_Private =>
4262             if In_Spec then
4263                Done := True;
4264             else
4265                Error_Msg_SC ("PRIVATE not allowed in body");
4266                Scan; -- past PRIVATE
4267             end if;
4268
4269          --  An end of file definitely terminates the declarations!
4270
4271          when Tok_EOF =>
4272             Done := True;
4273
4274          --  The remaining tokens do not end the scan, but cannot start a
4275          --  valid declaration, so we signal an error and resynchronize.
4276          --  But first check for misuse of a reserved identifier.
4277
4278          when others =>
4279
4280             --  Here we check for a reserved identifier
4281
4282             if Is_Reserved_Identifier then
4283                Save_Scan_State (Scan_State);
4284                Scan; -- past the token
4285
4286                if Token /= Tok_Colon and then Token /= Tok_Comma then
4287                   Restore_Scan_State (Scan_State);
4288                   Set_Declaration_Expected;
4289                   raise Error_Resync;
4290                else
4291                   Restore_Scan_State (Scan_State);
4292                   Scan_Reserved_Identifier (Force_Msg => True);
4293                   Check_Bad_Layout;
4294                   P_Identifier_Declarations (Decls, Done, In_Spec);
4295                end if;
4296
4297             else
4298                Set_Declaration_Expected;
4299                raise Error_Resync;
4300             end if;
4301       end case;
4302
4303    --  To resynchronize after an error, we scan to the next semicolon and
4304    --  return with Done = False, indicating that there may still be more
4305    --  valid declarations to come.
4306
4307    exception
4308       when Error_Resync =>
4309          Resync_Past_Semicolon;
4310          Done := False;
4311    end P_Declarative_Items;
4312
4313    ----------------------------------
4314    -- 3.11  Basic Declarative Item --
4315    ----------------------------------
4316
4317    --  BASIC_DECLARATIVE_ITEM ::=
4318    --    BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
4319
4320    --  Scan zero or more basic declarative items
4321
4322    --  Error recovery: cannot raise Error_Resync. If an error is detected, then
4323    --  the scan pointer is repositioned past the next semicolon, and the scan
4324    --  for declarative items continues.
4325
4326    function P_Basic_Declarative_Items return List_Id is
4327       Decl  : Node_Id;
4328       Decls : List_Id;
4329       Kind  : Node_Kind;
4330       Done  : Boolean;
4331
4332    begin
4333       --  Indicate no bad declarations detected yet in the current context:
4334       --  visible or private declarations of a package spec.
4335
4336       Missing_Begin_Msg := No_Error_Msg;
4337
4338       --  Get rid of active SIS entry from outer scope. This means we will
4339       --  miss some nested cases, but it doesn't seem worth the effort. See
4340       --  discussion in Par for further details
4341
4342       SIS_Entry_Active := False;
4343
4344       --  Loop to scan out declarations
4345
4346       Decls := New_List;
4347
4348       loop
4349          P_Declarative_Items (Decls, Done, In_Spec => True);
4350          exit when Done;
4351       end loop;
4352
4353       --  Get rid of active SIS entry. This is set only if we have scanned a
4354       --  procedure declaration and have not found the body. We could give
4355       --  an error message, but that really would be usurping the role of
4356       --  semantic analysis (this really is a case of a missing body).
4357
4358       SIS_Entry_Active := False;
4359
4360       --  Test for assorted illegal declarations not diagnosed elsewhere
4361
4362       Decl := First (Decls);
4363
4364       while Present (Decl) loop
4365          Kind := Nkind (Decl);
4366
4367          --  Test for body scanned, not acceptable as basic decl item
4368
4369          if Kind = N_Subprogram_Body or else
4370             Kind = N_Package_Body or else
4371             Kind = N_Task_Body or else
4372             Kind = N_Protected_Body
4373          then
4374             Error_Msg
4375               ("proper body not allowed in package spec", Sloc (Decl));
4376
4377          --  Test for body stub scanned, not acceptable as basic decl item
4378
4379          elsif Kind in N_Body_Stub then
4380             Error_Msg
4381               ("body stub not allowed in package spec", Sloc (Decl));
4382
4383          elsif Kind = N_Assignment_Statement then
4384             Error_Msg
4385               ("assignment statement not allowed in package spec",
4386                  Sloc (Decl));
4387          end if;
4388
4389          Next (Decl);
4390       end loop;
4391
4392       return Decls;
4393    end P_Basic_Declarative_Items;
4394
4395    ----------------
4396    -- 3.11  Body --
4397    ----------------
4398
4399    --  For proper body, see below
4400    --  For body stub, see 10.1.3
4401
4402    -----------------------
4403    -- 3.11  Proper Body --
4404    -----------------------
4405
4406    --  Subprogram body is parsed by P_Subprogram (6.1)
4407    --  Package body is parsed by P_Package (7.1)
4408    --  Task body is parsed by P_Task (9.1)
4409    --  Protected body is parsed by P_Protected (9.4)
4410
4411    ------------------------------
4412    -- Set_Declaration_Expected --
4413    ------------------------------
4414
4415    procedure Set_Declaration_Expected is
4416    begin
4417       Error_Msg_SC ("declaration expected");
4418
4419       if Missing_Begin_Msg = No_Error_Msg then
4420          Missing_Begin_Msg := Get_Msg_Id;
4421       end if;
4422    end Set_Declaration_Expected;
4423
4424    ----------------------
4425    -- Skip_Declaration --
4426    ----------------------
4427
4428    procedure Skip_Declaration (S : List_Id) is
4429       Dummy_Done : Boolean;
4430       pragma Warnings (Off, Dummy_Done);
4431    begin
4432       P_Declarative_Items (S, Dummy_Done, False);
4433    end Skip_Declaration;
4434
4435    -----------------------------------------
4436    -- Statement_When_Declaration_Expected --
4437    -----------------------------------------
4438
4439    procedure Statement_When_Declaration_Expected
4440      (Decls   : List_Id;
4441       Done    : out Boolean;
4442       In_Spec : Boolean)
4443    is
4444    begin
4445       --  Case of second occurrence of statement in one declaration sequence
4446
4447       if Missing_Begin_Msg /= No_Error_Msg then
4448
4449          --  In the procedure spec case, just ignore it, we only give one
4450          --  message for the first occurrence, since otherwise we may get
4451          --  horrible cascading if BODY was missing in the header line.
4452
4453          if In_Spec then
4454             null;
4455
4456          --  In the declarative part case, take a second statement as a sure
4457          --  sign that we really have a missing BEGIN, and end the declarative
4458          --  part now. Note that the caller will fix up the first message to
4459          --  say "missing BEGIN" so that's how the error will be signalled.
4460
4461          else
4462             Done := True;
4463             return;
4464          end if;
4465
4466       --  Case of first occurrence of unexpected statement
4467
4468       else
4469          --  If we are in a package spec, then give message of statement
4470          --  not allowed in package spec. This message never gets changed.
4471
4472          if In_Spec then
4473             Error_Msg_SC ("statement not allowed in package spec");
4474
4475          --  If in declarative part, then we give the message complaining
4476          --  about finding a statement when a declaration is expected. This
4477          --  gets changed to a complaint about a missing BEGIN if we later
4478          --  find that no BEGIN is present.
4479
4480          else
4481             Error_Msg_SC ("statement not allowed in declarative part");
4482          end if;
4483
4484          --  Capture message Id. This is used for two purposes, first to
4485          --  stop multiple messages, see test above, and second, to allow
4486          --  the replacement of the message in the declarative part case.
4487
4488          Missing_Begin_Msg := Get_Msg_Id;
4489       end if;
4490
4491       --  In all cases except the case in which we decided to terminate the
4492       --  declaration sequence on a second error, we scan out the statement
4493       --  and append it to the list of declarations (note that the semantics
4494       --  can handle statements in a declaration list so if we proceed to
4495       --  call the semantic phase, all will be (reasonably) well!
4496
4497       Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
4498
4499       --  Done is set to False, since we want to continue the scan of
4500       --  declarations, hoping that this statement was a temporary glitch.
4501       --  If we indeed are now in the statement part (i.e. this was a missing
4502       --  BEGIN, then it's not terrible, we will simply keep calling this
4503       --  procedure to process the statements one by one, and then finally
4504       --  hit the missing BEGIN, which will clean up the error message.
4505
4506       Done := False;
4507    end Statement_When_Declaration_Expected;
4508
4509 end Ch3;