OSDN Git Service

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