OSDN Git Service

2007-04-06 Javier Miranda <miranda@adacore.com>
[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-2006, 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       --  Ada2005: an association can be given by: others => <>.
336
337       if Token = Tok_Others then
338          if Ada_Version < Ada_05 then
339             Error_Msg_SP
340               ("partial parametrization of formal packages" &
341                 "  is an Ada 2005 extension");
342             Error_Msg_SP
343               ("\unit must be compiled with -gnat05 switch");
344          end if;
345
346          Scan;  --  past OTHERS
347
348          if Token /= Tok_Arrow then
349             Error_Msg_BC ("expect arrow after others");
350          else
351             Scan;  --  past arrow
352          end if;
353
354          if Token /= Tok_Box then
355             Error_Msg_BC ("expect Box after arrow");
356          else
357             Scan;  --  past box
358          end if;
359
360          return New_Node (N_Others_Choice, Token_Ptr);
361       end if;
362
363       if Token in Token_Class_Desig then
364          Param_Name_Node := Token_Node;
365          Save_Scan_State (Scan_State); -- at designator
366          Scan; -- past simple name or operator symbol
367
368          if Token = Tok_Arrow then
369             Scan; -- past arrow
370             Set_Selector_Name (Generic_Assoc_Node, Param_Name_Node);
371          else
372             Restore_Scan_State (Scan_State); -- to designator
373          end if;
374       end if;
375
376       --  In Ada 2005 the actual can be a box.
377
378       if Token = Tok_Box then
379          Scan;
380          Set_Box_Present (Generic_Assoc_Node);
381          Set_Explicit_Generic_Actual_Parameter (Generic_Assoc_Node, Empty);
382
383       else
384          Set_Explicit_Generic_Actual_Parameter
385            (Generic_Assoc_Node, P_Expression);
386       end if;
387
388       return Generic_Assoc_Node;
389    end P_Generic_Association;
390
391    ---------------------------------------------
392    -- 12.3  Explicit Generic Actual Parameter --
393    ---------------------------------------------
394
395    --  Parsed by P_Generic_Association (12.3)
396
397    --------------------------------------
398    -- 12.4  Formal Object Declarations --
399    --------------------------------------
400
401    --  FORMAL_OBJECT_DECLARATION ::=
402    --    DEFINING_IDENTIFIER_LIST :
403    --      MODE [NULL_EXCLUSION] SUBTYPE_MARK [:= DEFAULT_EXPRESSION];
404    --  | DEFINING_IDENTIFIER_LIST :
405    --      MODE ACCESS_DEFINITION [:= DEFAULT_EXPRESSION];
406
407    --  The caller has checked that the initial token is an identifier
408
409    --  Error recovery: cannot raise Error_Resync
410
411    procedure P_Formal_Object_Declarations (Decls : List_Id) is
412       Decl_Node        : Node_Id;
413       Ident            : Nat;
414       Not_Null_Present : Boolean := False;
415       Num_Idents       : Nat;
416       Scan_State       : Saved_Scan_State;
417
418       Idents : array (Int range 1 .. 4096) of Entity_Id;
419       --  This array holds the list of defining identifiers. The upper bound
420       --  of 4096 is intended to be essentially infinite, and we do not even
421       --  bother to check for it being exceeded.
422
423    begin
424       Idents (1) := P_Defining_Identifier (C_Comma_Colon);
425       Num_Idents := 1;
426
427       while Comma_Present loop
428          Num_Idents := Num_Idents + 1;
429          Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
430       end loop;
431
432       T_Colon;
433
434       --  If there are multiple identifiers, we repeatedly scan the
435       --  type and initialization expression information by resetting
436       --  the scan pointer (so that we get completely separate trees
437       --  for each occurrence).
438
439       if Num_Idents > 1 then
440          Save_Scan_State (Scan_State);
441       end if;
442
443       --  Loop through defining identifiers in list
444
445       Ident := 1;
446       Ident_Loop : loop
447          Decl_Node := New_Node (N_Formal_Object_Declaration, Token_Ptr);
448          Set_Defining_Identifier (Decl_Node, Idents (Ident));
449          P_Mode (Decl_Node);
450
451          Not_Null_Present := P_Null_Exclusion;  --  Ada 2005 (AI-423)
452
453          --  Ada 2005 (AI-423): Formal object with an access definition
454
455          if Token = Tok_Access then
456
457             --  The access definition is still parsed and set even though
458             --  the compilation may not use the proper switch. This action
459             --  ensures the required local error recovery.
460
461             Set_Access_Definition (Decl_Node,
462               P_Access_Definition (Not_Null_Present));
463
464             if Ada_Version < Ada_05 then
465                Error_Msg_SP
466                  ("access definition not allowed in formal object " &
467                   "declaration");
468                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
469             end if;
470
471          --  Formal object with a subtype mark
472
473          else
474             Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
475             Set_Subtype_Mark (Decl_Node, P_Subtype_Mark_Resync);
476          end if;
477
478          No_Constraint;
479          Set_Default_Expression (Decl_Node, Init_Expr_Opt);
480
481          if Ident > 1 then
482             Set_Prev_Ids (Decl_Node, True);
483          end if;
484
485          if Ident < Num_Idents then
486             Set_More_Ids (Decl_Node, True);
487          end if;
488
489          Append (Decl_Node, Decls);
490
491          exit Ident_Loop when Ident = Num_Idents;
492          Ident := Ident + 1;
493          Restore_Scan_State (Scan_State);
494       end loop Ident_Loop;
495
496       TF_Semicolon;
497    end P_Formal_Object_Declarations;
498
499    -----------------------------------
500    -- 12.5  Formal Type Declaration --
501    -----------------------------------
502
503    --  FORMAL_TYPE_DECLARATION ::=
504    --    type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
505    --      is FORMAL_TYPE_DEFINITION;
506
507    --  The caller has checked that the initial token is TYPE
508
509    --  Error recovery: cannot raise Error_Resync
510
511    function P_Formal_Type_Declaration return Node_Id is
512       Decl_Node  : Node_Id;
513       Def_Node   : Node_Id;
514
515    begin
516       Decl_Node := New_Node (N_Formal_Type_Declaration, Token_Ptr);
517       Scan; -- past TYPE
518       Set_Defining_Identifier (Decl_Node, P_Defining_Identifier);
519
520       if P_Unknown_Discriminant_Part_Opt then
521          Set_Unknown_Discriminants_Present (Decl_Node, True);
522       else
523          Set_Discriminant_Specifications
524            (Decl_Node, P_Known_Discriminant_Part_Opt);
525       end if;
526
527       T_Is;
528
529       Def_Node := P_Formal_Type_Definition;
530
531       if Def_Node /= Error then
532          Set_Formal_Type_Definition (Decl_Node, Def_Node);
533          TF_Semicolon;
534
535       else
536          Decl_Node := Error;
537
538          --  If we have semicolon, skip it to avoid cascaded errors
539
540          if Token = Tok_Semicolon then
541             Scan;
542          end if;
543       end if;
544
545       return Decl_Node;
546    end P_Formal_Type_Declaration;
547
548    ----------------------------------
549    -- 12.5  Formal Type Definition --
550    ----------------------------------
551
552    --  FORMAL_TYPE_DEFINITION ::=
553    --    FORMAL_PRIVATE_TYPE_DEFINITION
554    --  | FORMAL_DERIVED_TYPE_DEFINITION
555    --  | FORMAL_DISCRETE_TYPE_DEFINITION
556    --  | FORMAL_SIGNED_INTEGER_TYPE_DEFINITION
557    --  | FORMAL_MODULAR_TYPE_DEFINITION
558    --  | FORMAL_FLOATING_POINT_DEFINITION
559    --  | FORMAL_ORDINARY_FIXED_POINT_DEFINITION
560    --  | FORMAL_DECIMAL_FIXED_POINT_DEFINITION
561    --  | FORMAL_ARRAY_TYPE_DEFINITION
562    --  | FORMAL_ACCESS_TYPE_DEFINITION
563    --  | FORMAL_INTERFACE_TYPE_DEFINITION
564
565    --  FORMAL_ARRAY_TYPE_DEFINITION ::= ARRAY_TYPE_DEFINITION
566
567    --  FORMAL_ACCESS_TYPE_DEFINITION ::= ACCESS_TYPE_DEFINITION
568
569    --  FORMAL_INTERFACE_TYPE_DEFINITION ::= INTERFACE_TYPE_DEFINITION
570
571    function P_Formal_Type_Definition return Node_Id is
572       Scan_State   : Saved_Scan_State;
573       Typedef_Node : Node_Id;
574
575    begin
576       if Token_Name = Name_Abstract then
577          Check_95_Keyword (Tok_Abstract, Tok_Tagged);
578       end if;
579
580       if Token_Name = Name_Tagged then
581          Check_95_Keyword (Tok_Tagged, Tok_Private);
582          Check_95_Keyword (Tok_Tagged, Tok_Limited);
583       end if;
584
585       case Token is
586
587          --  Mostly we can tell what we have from the initial token. The one
588          --  exception is ABSTRACT, where we have to scan ahead to see if we
589          --  have a formal derived type or a formal private type definition.
590
591          --  In addition, in Ada 2005 LIMITED may appear after abstract, so
592          --  that the lookahead must be extended by one more token.
593
594          when Tok_Abstract =>
595             Save_Scan_State (Scan_State);
596             Scan; -- past ABSTRACT
597
598             if Token = Tok_New then
599                Restore_Scan_State (Scan_State); -- to ABSTRACT
600                return P_Formal_Derived_Type_Definition;
601
602             elsif Token = Tok_Limited then
603                Scan;  --  past LIMITED
604
605                if Token = Tok_New then
606                   Restore_Scan_State (Scan_State); -- to ABSTRACT
607                   return P_Formal_Derived_Type_Definition;
608
609                else
610                   Restore_Scan_State (Scan_State); -- to ABSTRACT
611                   return P_Formal_Private_Type_Definition;
612                end if;
613
614             --  Ada 2005 (AI-443): Abstract synchronized formal derived type
615
616             elsif Token = Tok_Synchronized then
617                Restore_Scan_State (Scan_State); -- to ABSTRACT
618                return P_Formal_Derived_Type_Definition;
619
620             else
621                Restore_Scan_State (Scan_State); -- to ABSTRACT
622                return P_Formal_Private_Type_Definition;
623             end if;
624
625          when Tok_Access =>
626             return P_Access_Type_Definition;
627
628          when Tok_Array =>
629             return P_Array_Type_Definition;
630
631          when Tok_Delta =>
632             return P_Formal_Fixed_Point_Definition;
633
634          when Tok_Digits =>
635             return P_Formal_Floating_Point_Definition;
636
637          when Tok_Interface => --  Ada 2005 (AI-251)
638             return P_Interface_Type_Definition (Abstract_Present => False);
639
640          when Tok_Left_Paren =>
641             return P_Formal_Discrete_Type_Definition;
642
643          when Tok_Limited =>
644             Save_Scan_State (Scan_State);
645             Scan; --  past LIMITED
646
647             if Token = Tok_Interface then
648                Typedef_Node :=
649                  P_Interface_Type_Definition (Abstract_Present => False);
650                Set_Limited_Present (Typedef_Node);
651                return Typedef_Node;
652
653             elsif Token = Tok_New then
654                Restore_Scan_State (Scan_State); -- to LIMITED
655                return P_Formal_Derived_Type_Definition;
656
657             else
658                if Token = Tok_Abstract then
659                   Error_Msg_SC ("ABSTRACT must come before LIMITED");
660                   Scan;  --  past improper ABSTRACT
661
662                   if Token = Tok_New then
663                      Restore_Scan_State (Scan_State); -- to LIMITED
664                      return P_Formal_Derived_Type_Definition;
665
666                   else
667                      Restore_Scan_State (Scan_State);
668                      return P_Formal_Private_Type_Definition;
669                   end if;
670                end if;
671
672                Restore_Scan_State (Scan_State);
673                return P_Formal_Private_Type_Definition;
674             end if;
675
676          when Tok_Mod =>
677             return P_Formal_Modular_Type_Definition;
678
679          when Tok_New =>
680             return P_Formal_Derived_Type_Definition;
681
682          when Tok_Private |
683               Tok_Tagged  =>
684             return P_Formal_Private_Type_Definition;
685
686          when Tok_Range =>
687             return P_Formal_Signed_Integer_Type_Definition;
688
689          when Tok_Record =>
690             Error_Msg_SC ("record not allowed in generic type definition!");
691             Discard_Junk_Node (P_Record_Definition);
692             return Error;
693
694          --  Ada 2005 (AI-345): Task, Protected or Synchronized interface or
695          --  (AI-443): Synchronized formal derived type declaration.
696
697          when Tok_Protected    |
698               Tok_Synchronized |
699               Tok_Task         =>
700
701             declare
702                Saved_Token : constant Token_Type := Token;
703
704             begin
705                Scan; -- past TASK, PROTECTED or SYNCHRONIZED
706
707                --  Synchronized derived type
708
709                if Token = Tok_New then
710                   Typedef_Node := P_Formal_Derived_Type_Definition;
711
712                   if Saved_Token = Tok_Synchronized then
713                      Set_Synchronized_Present (Typedef_Node);
714                   else
715                      Error_Msg_SC ("invalid kind of formal derived type");
716                   end if;
717
718                --  Interface
719
720                else
721                   Typedef_Node :=
722                     P_Interface_Type_Definition (Abstract_Present => False);
723
724                   case Saved_Token is
725                      when Tok_Task =>
726                         Set_Task_Present         (Typedef_Node);
727
728                      when Tok_Protected =>
729                         Set_Protected_Present    (Typedef_Node);
730
731                      when Tok_Synchronized =>
732                         Set_Synchronized_Present (Typedef_Node);
733
734                      when others =>
735                         null;
736                   end case;
737                end if;
738
739                return Typedef_Node;
740             end;
741
742          when others =>
743             Error_Msg_BC ("expecting generic type definition here");
744             Resync_Past_Semicolon;
745             return Error;
746
747       end case;
748    end P_Formal_Type_Definition;
749
750    --------------------------------------------
751    -- 12.5.1  Formal Private Type Definition --
752    --------------------------------------------
753
754    --  FORMAL_PRIVATE_TYPE_DEFINITION ::=
755    --    [[abstract] tagged] [limited] private
756
757    --  The caller has checked the initial token is PRIVATE, ABSTRACT,
758    --   TAGGED or LIMITED
759
760    --  Error recovery: cannot raise Error_Resync
761
762    function P_Formal_Private_Type_Definition return Node_Id is
763       Def_Node : Node_Id;
764
765    begin
766       Def_Node := New_Node (N_Formal_Private_Type_Definition, Token_Ptr);
767
768       if Token = Tok_Abstract then
769          Scan; -- past ABSTRACT
770
771          if Token_Name = Name_Tagged then
772             Check_95_Keyword (Tok_Tagged, Tok_Private);
773             Check_95_Keyword (Tok_Tagged, Tok_Limited);
774          end if;
775
776          if Token /= Tok_Tagged then
777             Error_Msg_SP ("ABSTRACT must be followed by TAGGED");
778          else
779             Set_Abstract_Present (Def_Node, True);
780          end if;
781       end if;
782
783       if Token = Tok_Tagged then
784          Set_Tagged_Present (Def_Node, True);
785          Scan; -- past TAGGED
786       end if;
787
788       if Token = Tok_Limited then
789          Set_Limited_Present (Def_Node, True);
790          Scan; -- past LIMITED
791       end if;
792
793       if Token = Tok_Abstract then
794          if Prev_Token = Tok_Tagged then
795             Error_Msg_SC ("ABSTRACT must come before TAGGED");
796          elsif Prev_Token = Tok_Limited then
797             Error_Msg_SC ("ABSTRACT must come before LIMITED");
798          end if;
799
800          Resync_Past_Semicolon;
801
802       elsif Token = Tok_Tagged then
803          Error_Msg_SC ("TAGGED must come before LIMITED");
804          Resync_Past_Semicolon;
805       end if;
806
807       Set_Sloc (Def_Node, Token_Ptr);
808       T_Private;
809       return Def_Node;
810    end P_Formal_Private_Type_Definition;
811
812    --------------------------------------------
813    -- 12.5.1  Formal Derived Type Definition --
814    --------------------------------------------
815
816    --  FORMAL_DERIVED_TYPE_DEFINITION ::=
817    --    [abstract] [limited | synchronized]
818    --         new SUBTYPE_MARK [[and INTERFACE_LIST] with private]
819
820    --  The caller has checked the initial token(s) is/are NEW, ASTRACT NEW,
821    --  or LIMITED NEW, ABSTRACT LIMITED NEW, SYNCHRONIZED NEW or ABSTRACT
822    --  SYNCHRONIZED NEW.
823
824    --  Error recovery: cannot raise Error_Resync
825
826    function P_Formal_Derived_Type_Definition return Node_Id is
827       Def_Node : Node_Id;
828
829    begin
830       Def_Node := New_Node (N_Formal_Derived_Type_Definition, Token_Ptr);
831
832       if Token = Tok_Abstract then
833          Set_Abstract_Present (Def_Node);
834          Scan; -- past ABSTRACT
835       end if;
836
837       if Token = Tok_Limited then
838          Set_Limited_Present (Def_Node);
839          Scan;  --  past LIMITED
840
841          if Ada_Version < Ada_05 then
842             Error_Msg_SP
843               ("LIMITED in derived type is an Ada 2005 extension");
844             Error_Msg_SP
845               ("\unit must be compiled with -gnat05 switch");
846          end if;
847
848       elsif Token = Tok_Synchronized then
849          Set_Synchronized_Present (Def_Node);
850          Scan;  --  past SYNCHRONIZED
851
852          if Ada_Version < Ada_05 then
853             Error_Msg_SP
854               ("SYNCHRONIZED in derived type is an Ada 2005 extension");
855             Error_Msg_SP
856               ("\unit must be compiled with -gnat05 switch");
857          end if;
858       end if;
859
860       if Token = Tok_Abstract then
861          Scan;  --  past ABSTRACT, diagnosed already in caller.
862       end if;
863
864       Scan; -- past NEW;
865       Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
866       No_Constraint;
867
868       --  Ada 2005 (AI-251): Deal with interfaces
869
870       if Token = Tok_And then
871          Scan; -- past AND
872
873          if Ada_Version < Ada_05 then
874             Error_Msg_SP
875               ("abstract interface is an Ada 2005 extension");
876             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
877          end if;
878
879          Set_Interface_List (Def_Node, New_List);
880
881          loop
882             Append (P_Qualified_Simple_Name, Interface_List (Def_Node));
883             exit when Token /= Tok_And;
884             Scan; -- past AND
885          end loop;
886       end if;
887
888       if Token = Tok_With then
889          Scan; -- past WITH
890          Set_Private_Present (Def_Node, True);
891          T_Private;
892
893       elsif Token = Tok_Tagged then
894          Scan;
895
896          if Token = Tok_Private then
897             Error_Msg_SC ("TAGGED should be WITH");
898             Set_Private_Present (Def_Node, True);
899             T_Private;
900          else
901             Ignore (Tok_Tagged);
902          end if;
903       end if;
904
905       return Def_Node;
906    end P_Formal_Derived_Type_Definition;
907
908    ---------------------------------------------
909    -- 12.5.2  Formal Discrete Type Definition --
910    ---------------------------------------------
911
912    --  FORMAL_DISCRETE_TYPE_DEFINITION ::= (<>)
913
914    --  The caller has checked the initial token is left paren
915
916    --  Error recovery: cannot raise Error_Resync
917
918    function P_Formal_Discrete_Type_Definition return Node_Id is
919       Def_Node : Node_Id;
920
921    begin
922       Def_Node := New_Node (N_Formal_Discrete_Type_Definition, Token_Ptr);
923       Scan; -- past left paren
924       T_Box;
925       T_Right_Paren;
926       return Def_Node;
927    end P_Formal_Discrete_Type_Definition;
928
929    ---------------------------------------------------
930    -- 12.5.2  Formal Signed Integer Type Definition --
931    ---------------------------------------------------
932
933    --  FORMAL_SIGNED_INTEGER_TYPE_DEFINITION ::= range <>
934
935    --  The caller has checked the initial token is RANGE
936
937    --  Error recovery: cannot raise Error_Resync
938
939    function P_Formal_Signed_Integer_Type_Definition return Node_Id is
940       Def_Node : Node_Id;
941
942    begin
943       Def_Node :=
944         New_Node (N_Formal_Signed_Integer_Type_Definition, Token_Ptr);
945       Scan; -- past RANGE
946       T_Box;
947       return Def_Node;
948    end P_Formal_Signed_Integer_Type_Definition;
949
950    --------------------------------------------
951    -- 12.5.2  Formal Modular Type Definition --
952    --------------------------------------------
953
954    --  FORMAL_MODULAR_TYPE_DEFINITION ::= mod <>
955
956    --  The caller has checked the initial token is MOD
957
958    --  Error recovery: cannot raise Error_Resync
959
960    function P_Formal_Modular_Type_Definition return Node_Id is
961       Def_Node : Node_Id;
962
963    begin
964       Def_Node :=
965         New_Node (N_Formal_Modular_Type_Definition, Token_Ptr);
966       Scan; -- past MOD
967       T_Box;
968       return Def_Node;
969    end P_Formal_Modular_Type_Definition;
970
971    ----------------------------------------------
972    -- 12.5.2  Formal Floating Point Definition --
973    ----------------------------------------------
974
975    --  FORMAL_FLOATING_POINT_DEFINITION ::= digits <>
976
977    --  The caller has checked the initial token is DIGITS
978
979    --  Error recovery: cannot raise Error_Resync
980
981    function P_Formal_Floating_Point_Definition return Node_Id is
982       Def_Node : Node_Id;
983
984    begin
985       Def_Node :=
986         New_Node (N_Formal_Floating_Point_Definition, Token_Ptr);
987       Scan; -- past DIGITS
988       T_Box;
989       return Def_Node;
990    end P_Formal_Floating_Point_Definition;
991
992    -------------------------------------------
993    -- 12.5.2  Formal Fixed Point Definition --
994    -------------------------------------------
995
996    --  This routine parses either a formal ordinary fixed point definition
997    --  or a formal decimal fixed point definition:
998
999    --  FORMAL_ORDINARY_FIXED_POINT_DEFINITION ::= delta <>
1000
1001    --  FORMAL_DECIMAL_FIXED_POINT_DEFINITION ::= delta <> digits <>
1002
1003    --  The caller has checked the initial token is DELTA
1004
1005    --  Error recovery: cannot raise Error_Resync
1006
1007    function P_Formal_Fixed_Point_Definition return Node_Id is
1008       Def_Node   : Node_Id;
1009       Delta_Sloc : Source_Ptr;
1010
1011    begin
1012       Delta_Sloc := Token_Ptr;
1013       Scan; -- past DELTA
1014       T_Box;
1015
1016       if Token = Tok_Digits then
1017          Def_Node :=
1018            New_Node (N_Formal_Decimal_Fixed_Point_Definition, Delta_Sloc);
1019          Scan; -- past DIGITS
1020          T_Box;
1021       else
1022          Def_Node :=
1023            New_Node (N_Formal_Ordinary_Fixed_Point_Definition, Delta_Sloc);
1024       end if;
1025
1026       return Def_Node;
1027    end P_Formal_Fixed_Point_Definition;
1028
1029    ----------------------------------------------------
1030    -- 12.5.2  Formal Ordinary Fixed Point Definition --
1031    ----------------------------------------------------
1032
1033    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
1034
1035    ---------------------------------------------------
1036    -- 12.5.2  Formal Decimal Fixed Point Definition --
1037    ---------------------------------------------------
1038
1039    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
1040
1041    ------------------------------------------
1042    -- 12.5.3  Formal Array Type Definition --
1043    ------------------------------------------
1044
1045    --  Parsed by P_Formal_Type_Definition (12.5)
1046
1047    -------------------------------------------
1048    -- 12.5.4  Formal Access Type Definition --
1049    -------------------------------------------
1050
1051    --  Parsed by P_Formal_Type_Definition (12.5)
1052
1053    -----------------------------------------
1054    -- 12.6  Formal Subprogram Declaration --
1055    -----------------------------------------
1056
1057    --  FORMAL_SUBPROGRAM_DECLARATION ::=
1058    --    FORMAL_CONCRETE_SUBPROGRAM_DECLARATION
1059    --  | FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION
1060
1061    --  FORMAL_CONCRETE_SUBPROGRAM_DECLARATION ::=
1062    --    with SUBPROGRAM_SPECIFICATION [is SUBPROGRAM_DEFAULT];
1063
1064    --  FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION ::=
1065    --    with SUBPROGRAM_SPECIFICATION is abstract [SUBPROGRAM_DEFAULT];
1066
1067    --  SUBPROGRAM_DEFAULT ::= DEFAULT_NAME | <>
1068
1069    --  DEFAULT_NAME ::= NAME | null
1070
1071    --  The caller has checked that the initial tokens are WITH FUNCTION or
1072    --  WITH PROCEDURE, and the initial WITH has been scanned out.
1073
1074    --  A null default is an Ada 2005 feature
1075
1076    --  Error recovery: cannot raise Error_Resync
1077
1078    function P_Formal_Subprogram_Declaration return Node_Id is
1079       Prev_Sloc : constant Source_Ptr := Prev_Token_Ptr;
1080       Spec_Node : constant Node_Id    := P_Subprogram_Specification;
1081       Def_Node  : Node_Id;
1082
1083    begin
1084       if Token = Tok_Is then
1085          T_Is; -- past IS, skip extra IS or ";"
1086
1087          if Token = Tok_Abstract then
1088             Def_Node :=
1089               New_Node (N_Formal_Abstract_Subprogram_Declaration, Prev_Sloc);
1090             Scan; -- past ABSTRACT
1091
1092             if Ada_Version < Ada_05 then
1093                Error_Msg_SP
1094                  ("formal abstract subprograms are an Ada 2005 extension");
1095                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1096             end if;
1097
1098          else
1099             Def_Node :=
1100               New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
1101          end if;
1102
1103          Set_Specification (Def_Node, Spec_Node);
1104
1105          if Token = Tok_Semicolon then
1106             Scan; -- past ";"
1107
1108          elsif Token = Tok_Box then
1109             Set_Box_Present (Def_Node, True);
1110             Scan; -- past <>
1111             T_Semicolon;
1112
1113          elsif Token = Tok_Null then
1114             if Ada_Version < Ada_05 then
1115                Error_Msg_SP
1116                  ("null default subprograms are an Ada 2005 extension");
1117                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1118             end if;
1119
1120             if Nkind (Spec_Node) = N_Procedure_Specification then
1121                Set_Null_Present (Spec_Node);
1122             else
1123                Error_Msg_SP ("only procedures can be null");
1124             end if;
1125
1126             Scan;  --  past NULL
1127             T_Semicolon;
1128
1129          else
1130             Set_Default_Name (Def_Node, P_Name);
1131             T_Semicolon;
1132          end if;
1133
1134       else
1135          Def_Node :=
1136            New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
1137          Set_Specification (Def_Node, Spec_Node);
1138          T_Semicolon;
1139       end if;
1140
1141       return Def_Node;
1142    end P_Formal_Subprogram_Declaration;
1143
1144    ------------------------------
1145    -- 12.6  Subprogram Default --
1146    ------------------------------
1147
1148    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1149
1150    ------------------------
1151    -- 12.6  Default Name --
1152    ------------------------
1153
1154    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1155
1156    --------------------------------------
1157    -- 12.7  Formal Package Declaration --
1158    --------------------------------------
1159
1160    --  FORMAL_PACKAGE_DECLARATION ::=
1161    --    with package DEFINING_IDENTIFIER
1162    --      is new generic_package_NAME FORMAL_PACKAGE_ACTUAL_PART;
1163
1164    --  FORMAL_PACKAGE_ACTUAL_PART ::=
1165    --    ([OTHERS =>] <>) |
1166    --    [GENERIC_ACTUAL_PART]
1167    --    (FORMAL_PACKAGE_ASSOCIATION {, FORMAL_PACKAGE_ASSOCIATION}
1168    --      [, OTHERS => <>)
1169
1170    --  FORMAL_PACKAGE_ASSOCIATION ::=
1171    --    GENERIC_ASSOCIATION
1172    --    | GENERIC_FORMAL_PARAMETER_SELECTOR_NAME => <>
1173
1174    --  The caller has checked that the initial tokens are WITH PACKAGE,
1175    --  and the initial WITH has been scanned out (so Token = Tok_Package).
1176
1177    --  Error recovery: cannot raise Error_Resync
1178
1179    function P_Formal_Package_Declaration return Node_Id is
1180       Def_Node : Node_Id;
1181       Scan_State : Saved_Scan_State;
1182
1183    begin
1184       Def_Node := New_Node (N_Formal_Package_Declaration, Prev_Token_Ptr);
1185       Scan; -- past PACKAGE
1186       Set_Defining_Identifier (Def_Node, P_Defining_Identifier (C_Is));
1187       T_Is;
1188       T_New;
1189       Set_Name (Def_Node, P_Qualified_Simple_Name);
1190
1191       if Token = Tok_Left_Paren then
1192          Save_Scan_State (Scan_State); -- at the left paren
1193          Scan; -- past the left paren
1194
1195          if Token = Tok_Box then
1196             Set_Box_Present (Def_Node, True);
1197             Scan; -- past box
1198             T_Right_Paren;
1199
1200          else
1201             Restore_Scan_State (Scan_State); -- to the left paren
1202             Set_Generic_Associations (Def_Node, P_Generic_Actual_Part_Opt);
1203          end if;
1204       end if;
1205
1206       T_Semicolon;
1207       return Def_Node;
1208    end P_Formal_Package_Declaration;
1209
1210    --------------------------------------
1211    -- 12.7  Formal Package Actual Part --
1212    --------------------------------------
1213
1214    --  Parsed by P_Formal_Package_Declaration (12.7)
1215
1216 end Ch12;