OSDN Git Service

PR c++/27714
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch12.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . C H 1 2                              --
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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 pragma Style_Checks (All_Checks);
28 --  Turn off subprogram body ordering check. Subprograms are in order
29 --  by RM section rather than alphabetical
30
31 separate (Par)
32 package body Ch12 is
33
34    --  Local functions, used only in this chapter
35
36    function P_Formal_Derived_Type_Definition           return Node_Id;
37    function P_Formal_Discrete_Type_Definition          return Node_Id;
38    function P_Formal_Fixed_Point_Definition            return Node_Id;
39    function P_Formal_Floating_Point_Definition         return Node_Id;
40    function P_Formal_Modular_Type_Definition           return Node_Id;
41    function P_Formal_Package_Declaration               return Node_Id;
42    function P_Formal_Private_Type_Definition           return Node_Id;
43    function P_Formal_Signed_Integer_Type_Definition    return Node_Id;
44    function P_Formal_Subprogram_Declaration            return Node_Id;
45    function P_Formal_Type_Declaration                  return Node_Id;
46    function P_Formal_Type_Definition                   return Node_Id;
47    function P_Generic_Association                      return Node_Id;
48
49    procedure P_Formal_Object_Declarations (Decls : List_Id);
50    --  Scans one or more formal object declarations and appends them to
51    --  Decls. Scans more than one declaration only in the case where the
52    --  source has a declaration with multiple defining identifiers.
53
54    --------------------------------
55    -- 12.1  Generic (also 8.5.5) --
56    --------------------------------
57
58    --  This routine parses either one of the forms of a generic declaration
59    --  or a generic renaming declaration.
60
61    --  GENERIC_DECLARATION ::=
62    --    GENERIC_SUBPROGRAM_DECLARATION | GENERIC_PACKAGE_DECLARATION
63
64    --  GENERIC_SUBPROGRAM_DECLARATION ::=
65    --    GENERIC_FORMAL_PART SUBPROGRAM_SPECIFICATION;
66
67    --  GENERIC_PACKAGE_DECLARATION ::=
68    --    GENERIC_FORMAL_PART PACKAGE_SPECIFICATION;
69
70    --  GENERIC_FORMAL_PART ::=
71    --    generic {GENERIC_FORMAL_PARAMETER_DECLARATION | USE_CLAUSE}
72
73    --  GENERIC_RENAMING_DECLARATION ::=
74    --    generic package DEFINING_PROGRAM_UNIT_NAME
75    --      renames generic_package_NAME
76    --  | generic procedure DEFINING_PROGRAM_UNIT_NAME
77    --      renames generic_procedure_NAME
78    --  | generic function DEFINING_PROGRAM_UNIT_NAME
79    --      renames generic_function_NAME
80
81    --  GENERIC_FORMAL_PARAMETER_DECLARATION ::=
82    --    FORMAL_OBJECT_DECLARATION
83    --  | FORMAL_TYPE_DECLARATION
84    --  | FORMAL_SUBPROGRAM_DECLARATION
85    --  | FORMAL_PACKAGE_DECLARATION
86
87    --  The caller has checked that the initial token is GENERIC
88
89    --  Error recovery: can raise Error_Resync
90
91    function P_Generic return Node_Id is
92       Gen_Sloc   : constant Source_Ptr := Token_Ptr;
93       Gen_Decl   : Node_Id;
94       Decl_Node  : Node_Id;
95       Decls      : List_Id;
96       Def_Unit   : Node_Id;
97       Ren_Token  : Token_Type;
98       Scan_State : Saved_Scan_State;
99
100    begin
101       Scan; -- past GENERIC
102
103       if Token = Tok_Private then
104          Error_Msg_SC ("PRIVATE goes before GENERIC, not after");
105          Scan; -- past junk PRIVATE token
106       end if;
107
108       Save_Scan_State (Scan_State); -- at token past GENERIC
109
110       --  Check for generic renaming declaration case
111
112       if Token = Tok_Package
113         or else Token = Tok_Function
114         or else Token = Tok_Procedure
115       then
116          Ren_Token := Token;
117          Scan; -- scan past PACKAGE, FUNCTION or PROCEDURE
118
119          if Token = Tok_Identifier then
120             Def_Unit := P_Defining_Program_Unit_Name;
121
122             Check_Misspelling_Of (Tok_Renames);
123
124             if Token = Tok_Renames then
125                if Ren_Token = Tok_Package then
126                   Decl_Node := New_Node
127                     (N_Generic_Package_Renaming_Declaration, Gen_Sloc);
128
129                elsif Ren_Token = Tok_Procedure then
130                   Decl_Node := New_Node
131                     (N_Generic_Procedure_Renaming_Declaration, Gen_Sloc);
132
133                else -- Ren_Token = Tok_Function then
134                   Decl_Node := New_Node
135                     (N_Generic_Function_Renaming_Declaration, Gen_Sloc);
136                end if;
137
138                Scan; -- past RENAMES
139                Set_Defining_Unit_Name (Decl_Node, Def_Unit);
140                Set_Name (Decl_Node, P_Name);
141                TF_Semicolon;
142                return Decl_Node;
143             end if;
144          end if;
145       end if;
146
147       --  Fall through if this is *not* a generic renaming declaration
148
149       Restore_Scan_State (Scan_State);
150       Decls := New_List;
151
152       --  Loop through generic parameter declarations and use clauses
153
154       Decl_Loop : loop
155          P_Pragmas_Opt (Decls);
156
157          if Token = Tok_Private then
158             Error_Msg_S ("generic private child packages not permitted");
159             Scan; -- past PRIVATE
160          end if;
161
162          if Token = Tok_Use then
163             Append (P_Use_Clause, Decls);
164          else
165             --  Parse a generic parameter declaration
166
167             if Token = Tok_Identifier then
168                P_Formal_Object_Declarations (Decls);
169
170             elsif Token = Tok_Type then
171                Append (P_Formal_Type_Declaration, Decls);
172
173             elsif Token = Tok_With then
174                Scan; -- past WITH
175
176                if Token = Tok_Package then
177                   Append (P_Formal_Package_Declaration, Decls);
178
179                elsif Token = Tok_Procedure or Token = Tok_Function then
180                   Append (P_Formal_Subprogram_Declaration, Decls);
181
182                else
183                   Error_Msg_BC
184                     ("FUNCTION, PROCEDURE or PACKAGE expected here");
185                   Resync_Past_Semicolon;
186                end if;
187
188             elsif Token = Tok_Subtype then
189                Error_Msg_SC ("subtype declaration not allowed " &
190                                 "as generic parameter declaration!");
191                Resync_Past_Semicolon;
192
193             else
194                exit Decl_Loop;
195             end if;
196          end if;
197
198       end loop Decl_Loop;
199
200       --  Generic formal part is scanned, scan out subprogram or package spec
201
202       if Token = Tok_Package then
203          Gen_Decl := New_Node (N_Generic_Package_Declaration, Gen_Sloc);
204          Set_Specification (Gen_Decl, P_Package (Pf_Spcn));
205       else
206          Gen_Decl := New_Node (N_Generic_Subprogram_Declaration, Gen_Sloc);
207
208          Set_Specification (Gen_Decl, P_Subprogram_Specification);
209
210          if Nkind (Defining_Unit_Name (Specification (Gen_Decl))) =
211                                              N_Defining_Program_Unit_Name
212            and then Scope.Last > 0
213          then
214             Error_Msg_SP ("child unit allowed only at library level");
215          end if;
216          TF_Semicolon;
217       end if;
218
219       Set_Generic_Formal_Declarations (Gen_Decl, Decls);
220       return Gen_Decl;
221    end P_Generic;
222
223    -------------------------------
224    -- 12.1  Generic Declaration --
225    -------------------------------
226
227    --  Parsed by P_Generic (12.1)
228
229    ------------------------------------------
230    -- 12.1  Generic Subprogram Declaration --
231    ------------------------------------------
232
233    --  Parsed by P_Generic (12.1)
234
235    ---------------------------------------
236    -- 12.1  Generic Package Declaration --
237    ---------------------------------------
238
239    --  Parsed by P_Generic (12.1)
240
241    -------------------------------
242    -- 12.1  Generic Formal Part --
243    -------------------------------
244
245    --  Parsed by P_Generic (12.1)
246
247    -------------------------------------------------
248    -- 12.1   Generic Formal Parameter Declaration --
249    -------------------------------------------------
250
251    --  Parsed by P_Generic (12.1)
252
253    ---------------------------------
254    -- 12.3  Generic Instantiation --
255    ---------------------------------
256
257    --  Generic package instantiation parsed by P_Package (7.1)
258    --  Generic procedure instantiation parsed by P_Subprogram (6.1)
259    --  Generic function instantiation parsed by P_Subprogram (6.1)
260
261    -------------------------------
262    -- 12.3  Generic Actual Part --
263    -------------------------------
264
265    --  GENERIC_ACTUAL_PART ::=
266    --    (GENERIC_ASSOCIATION {, GENERIC_ASSOCIATION})
267
268    --  Returns a list of generic associations, or Empty if none are present
269
270    --  Error recovery: cannot raise Error_Resync
271
272    function P_Generic_Actual_Part_Opt return List_Id is
273       Association_List : List_Id;
274
275    begin
276       --  Figure out if a generic actual part operation is present. Clearly
277       --  there is no generic actual part if the current token is semicolon
278
279       if Token = Tok_Semicolon then
280          return No_List;
281
282       --  If we don't have a left paren, then we have an error, and the job
283       --  is to figure out whether a left paren or semicolon was intended.
284       --  We assume a missing left paren (and hence a generic actual part
285       --  present) if the current token is not on a new line, or if it is
286       --  indented from the subprogram token. Otherwise assume missing
287       --  semicolon (which will be diagnosed by caller) and no generic part
288
289       elsif Token /= Tok_Left_Paren
290         and then Token_Is_At_Start_Of_Line
291         and then Start_Column <= Scope.Table (Scope.Last).Ecol
292       then
293          return No_List;
294
295       --  Otherwise we have a generic actual part (either a left paren is
296       --  present, or we have decided that there must be a missing left paren)
297
298       else
299          Association_List := New_List;
300          T_Left_Paren;
301
302          loop
303             Append (P_Generic_Association, Association_List);
304             exit when not Comma_Present;
305          end loop;
306
307          T_Right_Paren;
308          return Association_List;
309       end if;
310
311    end P_Generic_Actual_Part_Opt;
312
313    -------------------------------
314    -- 12.3  Generic Association --
315    -------------------------------
316
317    --  GENERIC_ASSOCIATION ::=
318    --    [generic_formal_parameter_SELECTOR_NAME =>]
319    --      EXPLICIT_GENERIC_ACTUAL_PARAMETER
320
321    --  EXPLICIT_GENERIC_ACTUAL_PARAMETER ::=
322    --    EXPRESSION      | variable_NAME   | subprogram_NAME
323    --  | entry_NAME      | SUBTYPE_MARK    | package_instance_NAME
324
325    --  Error recovery: cannot raise Error_Resync
326
327    function P_Generic_Association return Node_Id is
328       Scan_State         : Saved_Scan_State;
329       Param_Name_Node    : Node_Id;
330       Generic_Assoc_Node : Node_Id;
331
332    begin
333       Generic_Assoc_Node := New_Node (N_Generic_Association, Token_Ptr);
334
335       if Token in Token_Class_Desig then
336          Param_Name_Node := Token_Node;
337          Save_Scan_State (Scan_State); -- at designator
338          Scan; -- past simple name or operator symbol
339
340          if Token = Tok_Arrow then
341             Scan; -- past arrow
342             Set_Selector_Name (Generic_Assoc_Node, Param_Name_Node);
343          else
344             Restore_Scan_State (Scan_State); -- to designator
345          end if;
346       end if;
347
348       Set_Explicit_Generic_Actual_Parameter (Generic_Assoc_Node, P_Expression);
349       return Generic_Assoc_Node;
350    end P_Generic_Association;
351
352    ---------------------------------------------
353    -- 12.3  Explicit Generic Actual Parameter --
354    ---------------------------------------------
355
356    --  Parsed by P_Generic_Association (12.3)
357
358    --------------------------------------
359    -- 12.4  Formal Object Declarations --
360    --------------------------------------
361
362    --  FORMAL_OBJECT_DECLARATION ::=
363    --    DEFINING_IDENTIFIER_LIST :
364    --      MODE SUBTYPE_MARK [:= DEFAULT_EXPRESSION];
365
366    --  The caller has checked that the initial token is an identifier
367
368    --  Error recovery: cannot raise Error_Resync
369
370    procedure P_Formal_Object_Declarations (Decls : List_Id) is
371       Decl_Node  : Node_Id;
372       Scan_State : Saved_Scan_State;
373       Num_Idents : Nat;
374       Ident      : Nat;
375
376       Idents : array (Int range 1 .. 4096) of Entity_Id;
377       --  This array holds the list of defining identifiers. The upper bound
378       --  of 4096 is intended to be essentially infinite, and we do not even
379       --  bother to check for it being exceeded.
380
381    begin
382       Idents (1) := P_Defining_Identifier (C_Comma_Colon);
383       Num_Idents := 1;
384
385       while Comma_Present loop
386          Num_Idents := Num_Idents + 1;
387          Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
388       end loop;
389
390       T_Colon;
391
392       --  If there are multiple identifiers, we repeatedly scan the
393       --  type and initialization expression information by resetting
394       --  the scan pointer (so that we get completely separate trees
395       --  for each occurrence).
396
397       if Num_Idents > 1 then
398          Save_Scan_State (Scan_State);
399       end if;
400
401       --  Loop through defining identifiers in list
402
403       Ident := 1;
404       Ident_Loop : loop
405          Decl_Node := New_Node (N_Formal_Object_Declaration, Token_Ptr);
406          Set_Defining_Identifier (Decl_Node, Idents (Ident));
407          P_Mode (Decl_Node);
408          Set_Subtype_Mark (Decl_Node, P_Subtype_Mark_Resync);
409          No_Constraint;
410          Set_Expression (Decl_Node, Init_Expr_Opt);
411
412          if Ident > 1 then
413             Set_Prev_Ids (Decl_Node, True);
414          end if;
415
416          if Ident < Num_Idents then
417             Set_More_Ids (Decl_Node, True);
418          end if;
419
420          Append (Decl_Node, Decls);
421
422          exit Ident_Loop when Ident = Num_Idents;
423          Ident := Ident + 1;
424          Restore_Scan_State (Scan_State);
425       end loop Ident_Loop;
426
427       TF_Semicolon;
428    end P_Formal_Object_Declarations;
429
430    -----------------------------------
431    -- 12.5  Formal Type Declaration --
432    -----------------------------------
433
434    --  FORMAL_TYPE_DECLARATION ::=
435    --    type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
436    --      is FORMAL_TYPE_DEFINITION;
437
438    --  The caller has checked that the initial token is TYPE
439
440    --  Error recovery: cannot raise Error_Resync
441
442    function P_Formal_Type_Declaration return Node_Id is
443       Decl_Node  : Node_Id;
444       Def_Node   : Node_Id;
445
446    begin
447       Decl_Node := New_Node (N_Formal_Type_Declaration, Token_Ptr);
448       Scan; -- past TYPE
449       Set_Defining_Identifier (Decl_Node, P_Defining_Identifier);
450
451       if P_Unknown_Discriminant_Part_Opt then
452          Set_Unknown_Discriminants_Present (Decl_Node, True);
453       else
454          Set_Discriminant_Specifications
455            (Decl_Node, P_Known_Discriminant_Part_Opt);
456       end if;
457
458       T_Is;
459
460       Def_Node := P_Formal_Type_Definition;
461
462       if Def_Node /= Error then
463          Set_Formal_Type_Definition (Decl_Node, Def_Node);
464          TF_Semicolon;
465
466       else
467          Decl_Node := Error;
468
469          --  If we have semicolon, skip it to avoid cascaded errors
470
471          if Token = Tok_Semicolon then
472             Scan;
473          end if;
474       end if;
475
476       return Decl_Node;
477    end P_Formal_Type_Declaration;
478
479    ----------------------------------
480    -- 12.5  Formal Type Definition --
481    ----------------------------------
482
483    --  FORMAL_TYPE_DEFINITION ::=
484    --    FORMAL_PRIVATE_TYPE_DEFINITION
485    --  | FORMAL_DERIVED_TYPE_DEFINITION
486    --  | FORMAL_DISCRETE_TYPE_DEFINITION
487    --  | FORMAL_SIGNED_INTEGER_TYPE_DEFINITION
488    --  | FORMAL_MODULAR_TYPE_DEFINITION
489    --  | FORMAL_FLOATING_POINT_DEFINITION
490    --  | FORMAL_ORDINARY_FIXED_POINT_DEFINITION
491    --  | FORMAL_DECIMAL_FIXED_POINT_DEFINITION
492    --  | FORMAL_ARRAY_TYPE_DEFINITION
493    --  | FORMAL_ACCESS_TYPE_DEFINITION
494    --  | FORMAL_INTERFACE_TYPE_DEFINITION
495
496    --  FORMAL_ARRAY_TYPE_DEFINITION ::= ARRAY_TYPE_DEFINITION
497
498    --  FORMAL_ACCESS_TYPE_DEFINITION ::= ACCESS_TYPE_DEFINITION
499
500    --  FORMAL_INTERFACE_TYPE_DEFINITION ::= INTERFACE_TYPE_DEFINITION
501
502    function P_Formal_Type_Definition return Node_Id is
503       Scan_State   : Saved_Scan_State;
504       Typedef_Node : Node_Id;
505
506    begin
507       if Token_Name = Name_Abstract then
508          Check_95_Keyword (Tok_Abstract, Tok_Tagged);
509       end if;
510
511       if Token_Name = Name_Tagged then
512          Check_95_Keyword (Tok_Tagged, Tok_Private);
513          Check_95_Keyword (Tok_Tagged, Tok_Limited);
514       end if;
515
516       case Token is
517
518          --  Mostly we can tell what we have from the initial token. The one
519          --  exception is ABSTRACT, where we have to scan ahead to see if we
520          --  have a formal derived type or a formal private type definition.
521
522          --  In addition, in Ada 2005 LIMITED may appear after abstract, so
523          --  that the lookahead must be extended by one more token.
524
525          when Tok_Abstract =>
526             Save_Scan_State (Scan_State);
527             Scan; -- past ABSTRACT
528
529             if Token = Tok_New then
530                Restore_Scan_State (Scan_State); -- to ABSTRACT
531                return P_Formal_Derived_Type_Definition;
532
533             elsif Token = Tok_Limited then
534                Scan;  --  past LIMITED
535
536                if Token = Tok_New then
537                   Restore_Scan_State (Scan_State); -- to ABSTRACT
538                   return P_Formal_Derived_Type_Definition;
539
540                else
541                   Restore_Scan_State (Scan_State); -- to ABSTRACT
542                   return P_Formal_Private_Type_Definition;
543                end if;
544
545             else
546                Restore_Scan_State (Scan_State); -- to ABSTRACT
547                return P_Formal_Private_Type_Definition;
548             end if;
549
550          when Tok_Access =>
551             return P_Access_Type_Definition;
552
553          when Tok_Array =>
554             return P_Array_Type_Definition;
555
556          when Tok_Delta =>
557             return P_Formal_Fixed_Point_Definition;
558
559          when Tok_Digits =>
560             return P_Formal_Floating_Point_Definition;
561
562          when Tok_Interface => --  Ada 2005 (AI-251)
563             return P_Interface_Type_Definition (Is_Synchronized => False);
564
565          when Tok_Left_Paren =>
566             return P_Formal_Discrete_Type_Definition;
567
568          when Tok_Limited =>
569             Save_Scan_State (Scan_State);
570             Scan; --  past LIMITED
571
572             if Token = Tok_Interface then
573                Typedef_Node := P_Interface_Type_Definition
574                                 (Is_Synchronized => False);
575                Set_Limited_Present (Typedef_Node);
576                return Typedef_Node;
577
578             elsif Token = Tok_New then
579                Restore_Scan_State (Scan_State); -- to LIMITED
580                return P_Formal_Derived_Type_Definition;
581
582             else
583                if Token = Tok_Abstract then
584                   Error_Msg_SC ("ABSTRACT must come before LIMITED");
585                   Scan;  --  past improper ABSTRACT
586
587                   if Token = Tok_New then
588                      Restore_Scan_State (Scan_State); -- to LIMITED
589                      return P_Formal_Derived_Type_Definition;
590
591                   else
592                      Restore_Scan_State (Scan_State);
593                      return P_Formal_Private_Type_Definition;
594                   end if;
595                end if;
596
597                Restore_Scan_State (Scan_State);
598                return P_Formal_Private_Type_Definition;
599             end if;
600
601          when Tok_Mod =>
602             return P_Formal_Modular_Type_Definition;
603
604          when Tok_New =>
605             return P_Formal_Derived_Type_Definition;
606
607          when Tok_Private |
608               Tok_Tagged  =>
609             return P_Formal_Private_Type_Definition;
610
611          when Tok_Range =>
612             return P_Formal_Signed_Integer_Type_Definition;
613
614          when Tok_Record =>
615             Error_Msg_SC ("record not allowed in generic type definition!");
616             Discard_Junk_Node (P_Record_Definition);
617             return Error;
618
619          --  Ada 2005 (AI-345)
620
621          when Tok_Protected    |
622               Tok_Synchronized |
623               Tok_Task         =>
624
625             Scan; -- past TASK, PROTECTED or SYNCHRONIZED
626
627             declare
628                Saved_Token  : constant Token_Type := Token;
629
630             begin
631                Typedef_Node := P_Interface_Type_Definition
632                                 (Is_Synchronized => True);
633
634                case Saved_Token is
635                   when Tok_Task =>
636                      Set_Task_Present         (Typedef_Node);
637
638                   when Tok_Protected =>
639                      Set_Protected_Present    (Typedef_Node);
640
641                   when Tok_Synchronized =>
642                      Set_Synchronized_Present (Typedef_Node);
643
644                   when others =>
645                      null;
646                end case;
647
648                return Typedef_Node;
649             end;
650
651          when others =>
652             Error_Msg_BC ("expecting generic type definition here");
653             Resync_Past_Semicolon;
654             return Error;
655
656       end case;
657    end P_Formal_Type_Definition;
658
659    --------------------------------------------
660    -- 12.5.1  Formal Private Type Definition --
661    --------------------------------------------
662
663    --  FORMAL_PRIVATE_TYPE_DEFINITION ::=
664    --    [[abstract] tagged] [limited] private
665
666    --  The caller has checked the initial token is PRIVATE, ABSTRACT,
667    --   TAGGED or LIMITED
668
669    --  Error recovery: cannot raise Error_Resync
670
671    function P_Formal_Private_Type_Definition return Node_Id is
672       Def_Node : Node_Id;
673
674    begin
675       Def_Node := New_Node (N_Formal_Private_Type_Definition, Token_Ptr);
676
677       if Token = Tok_Abstract then
678          Scan; -- past ABSTRACT
679
680          if Token_Name = Name_Tagged then
681             Check_95_Keyword (Tok_Tagged, Tok_Private);
682             Check_95_Keyword (Tok_Tagged, Tok_Limited);
683          end if;
684
685          if Token /= Tok_Tagged then
686             Error_Msg_SP ("ABSTRACT must be followed by TAGGED");
687          else
688             Set_Abstract_Present (Def_Node, True);
689          end if;
690       end if;
691
692       if Token = Tok_Tagged then
693          Set_Tagged_Present (Def_Node, True);
694          Scan; -- past TAGGED
695       end if;
696
697       if Token = Tok_Limited then
698          Set_Limited_Present (Def_Node, True);
699          Scan; -- past LIMITED
700       end if;
701
702       if Token = Tok_Abstract then
703          if Prev_Token = Tok_Tagged then
704             Error_Msg_SC ("ABSTRACT must come before TAGGED");
705          elsif Prev_Token = Tok_Limited then
706             Error_Msg_SC ("ABSTRACT must come before LIMITED");
707          end if;
708
709          Resync_Past_Semicolon;
710
711       elsif Token = Tok_Tagged then
712          Error_Msg_SC ("TAGGED must come before LIMITED");
713          Resync_Past_Semicolon;
714       end if;
715
716       Set_Sloc (Def_Node, Token_Ptr);
717       T_Private;
718       return Def_Node;
719    end P_Formal_Private_Type_Definition;
720
721    --------------------------------------------
722    -- 12.5.1  Formal Derived Type Definition --
723    --------------------------------------------
724
725    --  FORMAL_DERIVED_TYPE_DEFINITION ::=
726    --    [abstract] [limited]
727    --         new SUBTYPE_MARK [[AND interface_list] with private]
728
729    --  The caller has checked the initial token(s) is/are NEW, ASTRACT NEW
730    --  LIMITED NEW, or ABSTRACT LIMITED NEW
731
732    --  Error recovery: cannot raise Error_Resync
733
734    function P_Formal_Derived_Type_Definition return Node_Id is
735       Def_Node : Node_Id;
736
737    begin
738       Def_Node := New_Node (N_Formal_Derived_Type_Definition, Token_Ptr);
739
740       if Token = Tok_Abstract then
741          Set_Abstract_Present (Def_Node);
742          Scan; -- past ABSTRACT
743       end if;
744
745       if Token = Tok_Limited then
746          Set_Limited_Present (Def_Node);
747          Scan;  --  past Limited
748
749          if Ada_Version < Ada_05 then
750             Error_Msg_SP
751               ("LIMITED in derived type is an Ada 2005 extension");
752             Error_Msg_SP
753               ("\unit must be compiled with -gnat05 switch");
754          end if;
755
756          if Token = Tok_Abstract then
757             Scan;  --  past ABSTRACT. diagnosed already in caller.
758          end if;
759       end if;
760
761       Scan; -- past NEW;
762       Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
763       No_Constraint;
764
765       --  Ada 2005 (AI-251): Deal with interfaces
766
767       if Token = Tok_And then
768          Scan; -- past AND
769
770          if Ada_Version < Ada_05 then
771             Error_Msg_SP
772               ("abstract interface is an Ada 2005 extension");
773             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
774          end if;
775
776          Set_Interface_List (Def_Node, New_List);
777
778          loop
779             Append (P_Qualified_Simple_Name, Interface_List (Def_Node));
780             exit when Token /= Tok_And;
781             Scan; -- past AND
782          end loop;
783       end if;
784
785       if Token = Tok_With then
786          Scan; -- past WITH
787          Set_Private_Present (Def_Node, True);
788          T_Private;
789
790       elsif Token = Tok_Tagged then
791          Scan;
792
793          if Token = Tok_Private then
794             Error_Msg_SC ("TAGGED should be WITH");
795             Set_Private_Present (Def_Node, True);
796             T_Private;
797          else
798             Ignore (Tok_Tagged);
799          end if;
800       end if;
801
802       return Def_Node;
803    end P_Formal_Derived_Type_Definition;
804
805    ---------------------------------------------
806    -- 12.5.2  Formal Discrete Type Definition --
807    ---------------------------------------------
808
809    --  FORMAL_DISCRETE_TYPE_DEFINITION ::= (<>)
810
811    --  The caller has checked the initial token is left paren
812
813    --  Error recovery: cannot raise Error_Resync
814
815    function P_Formal_Discrete_Type_Definition return Node_Id is
816       Def_Node : Node_Id;
817
818    begin
819       Def_Node := New_Node (N_Formal_Discrete_Type_Definition, Token_Ptr);
820       Scan; -- past left paren
821       T_Box;
822       T_Right_Paren;
823       return Def_Node;
824    end P_Formal_Discrete_Type_Definition;
825
826    ---------------------------------------------------
827    -- 12.5.2  Formal Signed Integer Type Definition --
828    ---------------------------------------------------
829
830    --  FORMAL_SIGNED_INTEGER_TYPE_DEFINITION ::= range <>
831
832    --  The caller has checked the initial token is RANGE
833
834    --  Error recovery: cannot raise Error_Resync
835
836    function P_Formal_Signed_Integer_Type_Definition return Node_Id is
837       Def_Node : Node_Id;
838
839    begin
840       Def_Node :=
841         New_Node (N_Formal_Signed_Integer_Type_Definition, Token_Ptr);
842       Scan; -- past RANGE
843       T_Box;
844       return Def_Node;
845    end P_Formal_Signed_Integer_Type_Definition;
846
847    --------------------------------------------
848    -- 12.5.2  Formal Modular Type Definition --
849    --------------------------------------------
850
851    --  FORMAL_MODULAR_TYPE_DEFINITION ::= mod <>
852
853    --  The caller has checked the initial token is MOD
854
855    --  Error recovery: cannot raise Error_Resync
856
857    function P_Formal_Modular_Type_Definition return Node_Id is
858       Def_Node : Node_Id;
859
860    begin
861       Def_Node :=
862         New_Node (N_Formal_Modular_Type_Definition, Token_Ptr);
863       Scan; -- past MOD
864       T_Box;
865       return Def_Node;
866    end P_Formal_Modular_Type_Definition;
867
868    ----------------------------------------------
869    -- 12.5.2  Formal Floating Point Definition --
870    ----------------------------------------------
871
872    --  FORMAL_FLOATING_POINT_DEFINITION ::= digits <>
873
874    --  The caller has checked the initial token is DIGITS
875
876    --  Error recovery: cannot raise Error_Resync
877
878    function P_Formal_Floating_Point_Definition return Node_Id is
879       Def_Node : Node_Id;
880
881    begin
882       Def_Node :=
883         New_Node (N_Formal_Floating_Point_Definition, Token_Ptr);
884       Scan; -- past DIGITS
885       T_Box;
886       return Def_Node;
887    end P_Formal_Floating_Point_Definition;
888
889    -------------------------------------------
890    -- 12.5.2  Formal Fixed Point Definition --
891    -------------------------------------------
892
893    --  This routine parses either a formal ordinary fixed point definition
894    --  or a formal decimal fixed point definition:
895
896    --  FORMAL_ORDINARY_FIXED_POINT_DEFINITION ::= delta <>
897
898    --  FORMAL_DECIMAL_FIXED_POINT_DEFINITION ::= delta <> digits <>
899
900    --  The caller has checked the initial token is DELTA
901
902    --  Error recovery: cannot raise Error_Resync
903
904    function P_Formal_Fixed_Point_Definition return Node_Id is
905       Def_Node   : Node_Id;
906       Delta_Sloc : Source_Ptr;
907
908    begin
909       Delta_Sloc := Token_Ptr;
910       Scan; -- past DELTA
911       T_Box;
912
913       if Token = Tok_Digits then
914          Def_Node :=
915            New_Node (N_Formal_Decimal_Fixed_Point_Definition, Delta_Sloc);
916          Scan; -- past DIGITS
917          T_Box;
918       else
919          Def_Node :=
920            New_Node (N_Formal_Ordinary_Fixed_Point_Definition, Delta_Sloc);
921       end if;
922
923       return Def_Node;
924    end P_Formal_Fixed_Point_Definition;
925
926    ----------------------------------------------------
927    -- 12.5.2  Formal Ordinary Fixed Point Definition --
928    ----------------------------------------------------
929
930    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
931
932    ---------------------------------------------------
933    -- 12.5.2  Formal Decimal Fixed Point Definition --
934    ---------------------------------------------------
935
936    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
937
938    ------------------------------------------
939    -- 12.5.3  Formal Array Type Definition --
940    ------------------------------------------
941
942    --  Parsed by P_Formal_Type_Definition (12.5)
943
944    -------------------------------------------
945    -- 12.5.4  Formal Access Type Definition --
946    -------------------------------------------
947
948    --  Parsed by P_Formal_Type_Definition (12.5)
949
950    -----------------------------------------
951    -- 12.6  Formal Subprogram Declaration --
952    -----------------------------------------
953
954    --  FORMAL_SUBPROGRAM_DECLARATION ::=
955    --    FORMAL_CONCRETE_SUBPROGRAM_DECLARATION
956    --  | FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION
957
958    --  FORMAL_CONCRETE_SUBPROGRAM_DECLARATION ::=
959    --    with SUBPROGRAM_SPECIFICATION [is SUBPROGRAM_DEFAULT];
960
961    --  FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION ::=
962    --    with SUBPROGRAM_SPECIFICATION is abstract [SUBPROGRAM_DEFAULT];
963
964    --  SUBPROGRAM_DEFAULT ::= DEFAULT_NAME | <>
965
966    --  DEFAULT_NAME ::= NAME | null
967
968    --  The caller has checked that the initial tokens are WITH FUNCTION or
969    --  WITH PROCEDURE, and the initial WITH has been scanned out.
970
971    --  A null default is an Ada 2005 feature
972
973    --  Error recovery: cannot raise Error_Resync
974
975    function P_Formal_Subprogram_Declaration return Node_Id is
976       Prev_Sloc : constant Source_Ptr := Prev_Token_Ptr;
977       Spec_Node : constant Node_Id    := P_Subprogram_Specification;
978       Def_Node  : Node_Id;
979
980    begin
981       if Token = Tok_Is then
982          T_Is; -- past IS, skip extra IS or ";"
983
984          if Token = Tok_Abstract then
985             Def_Node :=
986               New_Node (N_Formal_Abstract_Subprogram_Declaration, Prev_Sloc);
987             Scan; -- past ABSTRACT
988
989             if Ada_Version < Ada_05 then
990                Error_Msg_SP
991                  ("formal abstract subprograms are an Ada 2005 extension");
992                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
993             end if;
994
995          else
996             Def_Node :=
997               New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
998          end if;
999
1000          Set_Specification (Def_Node, Spec_Node);
1001
1002          if Token = Tok_Semicolon then
1003             Scan; -- past ";"
1004
1005          elsif Token = Tok_Box then
1006             Set_Box_Present (Def_Node, True);
1007             Scan; -- past <>
1008             T_Semicolon;
1009
1010          elsif Token = Tok_Null then
1011             if Ada_Version < Ada_05 then
1012                Error_Msg_SP
1013                  ("null default subprograms are an Ada 2005 extension");
1014                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1015             end if;
1016
1017             if Nkind (Spec_Node) = N_Procedure_Specification then
1018                Set_Null_Present (Spec_Node);
1019             else
1020                Error_Msg_SP ("only procedures can be null");
1021             end if;
1022
1023             Scan;  --  past NULL
1024             T_Semicolon;
1025
1026          else
1027             Set_Default_Name (Def_Node, P_Name);
1028             T_Semicolon;
1029          end if;
1030
1031       else
1032          Def_Node :=
1033            New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
1034          Set_Specification (Def_Node, Spec_Node);
1035          T_Semicolon;
1036       end if;
1037
1038       return Def_Node;
1039    end P_Formal_Subprogram_Declaration;
1040
1041    ------------------------------
1042    -- 12.6  Subprogram Default --
1043    ------------------------------
1044
1045    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1046
1047    ------------------------
1048    -- 12.6  Default Name --
1049    ------------------------
1050
1051    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1052
1053    --------------------------------------
1054    -- 12.7  Formal Package Declaration --
1055    --------------------------------------
1056
1057    --  FORMAL_PACKAGE_DECLARATION ::=
1058    --    with package DEFINING_IDENTIFIER
1059    --      is new generic_package_NAME FORMAL_PACKAGE_ACTUAL_PART;
1060
1061    --  FORMAL_PACKAGE_ACTUAL_PART ::=
1062    --    (<>) | [GENERIC_ACTUAL_PART]
1063
1064    --  The caller has checked that the initial tokens are WITH PACKAGE,
1065    --  and the initial WITH has been scanned out (so Token = Tok_Package).
1066
1067    --  Error recovery: cannot raise Error_Resync
1068
1069    function P_Formal_Package_Declaration return Node_Id is
1070       Def_Node : Node_Id;
1071       Scan_State : Saved_Scan_State;
1072
1073    begin
1074       Def_Node := New_Node (N_Formal_Package_Declaration, Prev_Token_Ptr);
1075       Scan; -- past PACKAGE
1076       Set_Defining_Identifier (Def_Node, P_Defining_Identifier (C_Is));
1077       T_Is;
1078       T_New;
1079       Set_Name (Def_Node, P_Qualified_Simple_Name);
1080
1081       if Token = Tok_Left_Paren then
1082          Save_Scan_State (Scan_State); -- at the left paren
1083          Scan; -- past the left paren
1084
1085          if Token = Tok_Box then
1086             Set_Box_Present (Def_Node, True);
1087             Scan; -- past box
1088             T_Right_Paren;
1089
1090          else
1091             Restore_Scan_State (Scan_State); -- to the left paren
1092             Set_Generic_Associations (Def_Node, P_Generic_Actual_Part_Opt);
1093          end if;
1094       end if;
1095
1096       T_Semicolon;
1097       return Def_Node;
1098    end P_Formal_Package_Declaration;
1099
1100    --------------------------------------
1101    -- 12.7  Formal Package Actual Part --
1102    --------------------------------------
1103
1104    --  Parsed by P_Formal_Package_Declaration (12.7)
1105
1106 end Ch12;