OSDN Git Service

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