OSDN Git Service

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