OSDN Git Service

2006-10-31 Hristian Kirtchev <kirtchev@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                                                 Is_Synchronized => 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 := P_Interface_Type_Definition
650                                 (Abstract_Present => False,
651                                  Is_Synchronized  => False);
652                Set_Limited_Present (Typedef_Node);
653                return Typedef_Node;
654
655             elsif Token = Tok_New then
656                Restore_Scan_State (Scan_State); -- to LIMITED
657                return P_Formal_Derived_Type_Definition;
658
659             else
660                if Token = Tok_Abstract then
661                   Error_Msg_SC ("ABSTRACT must come before LIMITED");
662                   Scan;  --  past improper ABSTRACT
663
664                   if Token = Tok_New then
665                      Restore_Scan_State (Scan_State); -- to LIMITED
666                      return P_Formal_Derived_Type_Definition;
667
668                   else
669                      Restore_Scan_State (Scan_State);
670                      return P_Formal_Private_Type_Definition;
671                   end if;
672                end if;
673
674                Restore_Scan_State (Scan_State);
675                return P_Formal_Private_Type_Definition;
676             end if;
677
678          when Tok_Mod =>
679             return P_Formal_Modular_Type_Definition;
680
681          when Tok_New =>
682             return P_Formal_Derived_Type_Definition;
683
684          when Tok_Private |
685               Tok_Tagged  =>
686             return P_Formal_Private_Type_Definition;
687
688          when Tok_Range =>
689             return P_Formal_Signed_Integer_Type_Definition;
690
691          when Tok_Record =>
692             Error_Msg_SC ("record not allowed in generic type definition!");
693             Discard_Junk_Node (P_Record_Definition);
694             return Error;
695
696          --  Ada 2005 (AI-345): Task, Protected or Synchronized interface or
697          --  (AI-443): Synchronized formal derived type declaration.
698
699          when Tok_Protected    |
700               Tok_Synchronized |
701               Tok_Task         =>
702
703             declare
704                Saved_Token : constant Token_Type := Token;
705
706             begin
707                Scan; -- past TASK, PROTECTED or SYNCHRONIZED
708
709                --  Synchronized derived type
710
711                if Token = Tok_New then
712                   Typedef_Node := P_Formal_Derived_Type_Definition;
713
714                   if Saved_Token = Tok_Synchronized then
715                      Set_Synchronized_Present (Typedef_Node);
716                   else
717                      Error_Msg_SC ("invalid kind of formal derived type");
718                   end if;
719
720                --  Interface
721
722                else
723                   Typedef_Node := P_Interface_Type_Definition
724                                     (Abstract_Present => False,
725                                      Is_Synchronized  => True);
726
727                   case Saved_Token is
728                      when Tok_Task =>
729                         Set_Task_Present         (Typedef_Node);
730
731                      when Tok_Protected =>
732                         Set_Protected_Present    (Typedef_Node);
733
734                      when Tok_Synchronized =>
735                         Set_Synchronized_Present (Typedef_Node);
736
737                      when others =>
738                         null;
739                   end case;
740                end if;
741
742                return Typedef_Node;
743             end;
744
745          when others =>
746             Error_Msg_BC ("expecting generic type definition here");
747             Resync_Past_Semicolon;
748             return Error;
749
750       end case;
751    end P_Formal_Type_Definition;
752
753    --------------------------------------------
754    -- 12.5.1  Formal Private Type Definition --
755    --------------------------------------------
756
757    --  FORMAL_PRIVATE_TYPE_DEFINITION ::=
758    --    [[abstract] tagged] [limited] private
759
760    --  The caller has checked the initial token is PRIVATE, ABSTRACT,
761    --   TAGGED or LIMITED
762
763    --  Error recovery: cannot raise Error_Resync
764
765    function P_Formal_Private_Type_Definition return Node_Id is
766       Def_Node : Node_Id;
767
768    begin
769       Def_Node := New_Node (N_Formal_Private_Type_Definition, Token_Ptr);
770
771       if Token = Tok_Abstract then
772          Scan; -- past ABSTRACT
773
774          if Token_Name = Name_Tagged then
775             Check_95_Keyword (Tok_Tagged, Tok_Private);
776             Check_95_Keyword (Tok_Tagged, Tok_Limited);
777          end if;
778
779          if Token /= Tok_Tagged then
780             Error_Msg_SP ("ABSTRACT must be followed by TAGGED");
781          else
782             Set_Abstract_Present (Def_Node, True);
783          end if;
784       end if;
785
786       if Token = Tok_Tagged then
787          Set_Tagged_Present (Def_Node, True);
788          Scan; -- past TAGGED
789       end if;
790
791       if Token = Tok_Limited then
792          Set_Limited_Present (Def_Node, True);
793          Scan; -- past LIMITED
794       end if;
795
796       if Token = Tok_Abstract then
797          if Prev_Token = Tok_Tagged then
798             Error_Msg_SC ("ABSTRACT must come before TAGGED");
799          elsif Prev_Token = Tok_Limited then
800             Error_Msg_SC ("ABSTRACT must come before LIMITED");
801          end if;
802
803          Resync_Past_Semicolon;
804
805       elsif Token = Tok_Tagged then
806          Error_Msg_SC ("TAGGED must come before LIMITED");
807          Resync_Past_Semicolon;
808       end if;
809
810       Set_Sloc (Def_Node, Token_Ptr);
811       T_Private;
812       return Def_Node;
813    end P_Formal_Private_Type_Definition;
814
815    --------------------------------------------
816    -- 12.5.1  Formal Derived Type Definition --
817    --------------------------------------------
818
819    --  FORMAL_DERIVED_TYPE_DEFINITION ::=
820    --    [abstract] [limited | synchronized]
821    --         new SUBTYPE_MARK [[and INTERFACE_LIST] with private]
822
823    --  The caller has checked the initial token(s) is/are NEW, ASTRACT NEW,
824    --  or LIMITED NEW, ABSTRACT LIMITED NEW, SYNCHRONIZED NEW or ABSTRACT
825    --  SYNCHRONIZED NEW.
826
827    --  Error recovery: cannot raise Error_Resync
828
829    function P_Formal_Derived_Type_Definition return Node_Id is
830       Def_Node : Node_Id;
831
832    begin
833       Def_Node := New_Node (N_Formal_Derived_Type_Definition, Token_Ptr);
834
835       if Token = Tok_Abstract then
836          Set_Abstract_Present (Def_Node);
837          Scan; -- past ABSTRACT
838       end if;
839
840       if Token = Tok_Limited then
841          Set_Limited_Present (Def_Node);
842          Scan;  --  past LIMITED
843
844          if Ada_Version < Ada_05 then
845             Error_Msg_SP
846               ("LIMITED in derived type is an Ada 2005 extension");
847             Error_Msg_SP
848               ("\unit must be compiled with -gnat05 switch");
849          end if;
850
851       elsif Token = Tok_Synchronized then
852          Set_Synchronized_Present (Def_Node);
853          Scan;  --  past SYNCHRONIZED
854
855          if Ada_Version < Ada_05 then
856             Error_Msg_SP
857               ("SYNCHRONIZED in derived type is an Ada 2005 extension");
858             Error_Msg_SP
859               ("\unit must be compiled with -gnat05 switch");
860          end if;
861       end if;
862
863       if Token = Tok_Abstract then
864          Scan;  --  past ABSTRACT, diagnosed already in caller.
865       end if;
866
867       Scan; -- past NEW;
868       Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
869       No_Constraint;
870
871       --  Ada 2005 (AI-251): Deal with interfaces
872
873       if Token = Tok_And then
874          Scan; -- past AND
875
876          if Ada_Version < Ada_05 then
877             Error_Msg_SP
878               ("abstract interface is an Ada 2005 extension");
879             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
880          end if;
881
882          Set_Interface_List (Def_Node, New_List);
883
884          loop
885             Append (P_Qualified_Simple_Name, Interface_List (Def_Node));
886             exit when Token /= Tok_And;
887             Scan; -- past AND
888          end loop;
889       end if;
890
891       if Token = Tok_With then
892          Scan; -- past WITH
893          Set_Private_Present (Def_Node, True);
894          T_Private;
895
896       elsif Token = Tok_Tagged then
897          Scan;
898
899          if Token = Tok_Private then
900             Error_Msg_SC ("TAGGED should be WITH");
901             Set_Private_Present (Def_Node, True);
902             T_Private;
903          else
904             Ignore (Tok_Tagged);
905          end if;
906       end if;
907
908       return Def_Node;
909    end P_Formal_Derived_Type_Definition;
910
911    ---------------------------------------------
912    -- 12.5.2  Formal Discrete Type Definition --
913    ---------------------------------------------
914
915    --  FORMAL_DISCRETE_TYPE_DEFINITION ::= (<>)
916
917    --  The caller has checked the initial token is left paren
918
919    --  Error recovery: cannot raise Error_Resync
920
921    function P_Formal_Discrete_Type_Definition return Node_Id is
922       Def_Node : Node_Id;
923
924    begin
925       Def_Node := New_Node (N_Formal_Discrete_Type_Definition, Token_Ptr);
926       Scan; -- past left paren
927       T_Box;
928       T_Right_Paren;
929       return Def_Node;
930    end P_Formal_Discrete_Type_Definition;
931
932    ---------------------------------------------------
933    -- 12.5.2  Formal Signed Integer Type Definition --
934    ---------------------------------------------------
935
936    --  FORMAL_SIGNED_INTEGER_TYPE_DEFINITION ::= range <>
937
938    --  The caller has checked the initial token is RANGE
939
940    --  Error recovery: cannot raise Error_Resync
941
942    function P_Formal_Signed_Integer_Type_Definition return Node_Id is
943       Def_Node : Node_Id;
944
945    begin
946       Def_Node :=
947         New_Node (N_Formal_Signed_Integer_Type_Definition, Token_Ptr);
948       Scan; -- past RANGE
949       T_Box;
950       return Def_Node;
951    end P_Formal_Signed_Integer_Type_Definition;
952
953    --------------------------------------------
954    -- 12.5.2  Formal Modular Type Definition --
955    --------------------------------------------
956
957    --  FORMAL_MODULAR_TYPE_DEFINITION ::= mod <>
958
959    --  The caller has checked the initial token is MOD
960
961    --  Error recovery: cannot raise Error_Resync
962
963    function P_Formal_Modular_Type_Definition return Node_Id is
964       Def_Node : Node_Id;
965
966    begin
967       Def_Node :=
968         New_Node (N_Formal_Modular_Type_Definition, Token_Ptr);
969       Scan; -- past MOD
970       T_Box;
971       return Def_Node;
972    end P_Formal_Modular_Type_Definition;
973
974    ----------------------------------------------
975    -- 12.5.2  Formal Floating Point Definition --
976    ----------------------------------------------
977
978    --  FORMAL_FLOATING_POINT_DEFINITION ::= digits <>
979
980    --  The caller has checked the initial token is DIGITS
981
982    --  Error recovery: cannot raise Error_Resync
983
984    function P_Formal_Floating_Point_Definition return Node_Id is
985       Def_Node : Node_Id;
986
987    begin
988       Def_Node :=
989         New_Node (N_Formal_Floating_Point_Definition, Token_Ptr);
990       Scan; -- past DIGITS
991       T_Box;
992       return Def_Node;
993    end P_Formal_Floating_Point_Definition;
994
995    -------------------------------------------
996    -- 12.5.2  Formal Fixed Point Definition --
997    -------------------------------------------
998
999    --  This routine parses either a formal ordinary fixed point definition
1000    --  or a formal decimal fixed point definition:
1001
1002    --  FORMAL_ORDINARY_FIXED_POINT_DEFINITION ::= delta <>
1003
1004    --  FORMAL_DECIMAL_FIXED_POINT_DEFINITION ::= delta <> digits <>
1005
1006    --  The caller has checked the initial token is DELTA
1007
1008    --  Error recovery: cannot raise Error_Resync
1009
1010    function P_Formal_Fixed_Point_Definition return Node_Id is
1011       Def_Node   : Node_Id;
1012       Delta_Sloc : Source_Ptr;
1013
1014    begin
1015       Delta_Sloc := Token_Ptr;
1016       Scan; -- past DELTA
1017       T_Box;
1018
1019       if Token = Tok_Digits then
1020          Def_Node :=
1021            New_Node (N_Formal_Decimal_Fixed_Point_Definition, Delta_Sloc);
1022          Scan; -- past DIGITS
1023          T_Box;
1024       else
1025          Def_Node :=
1026            New_Node (N_Formal_Ordinary_Fixed_Point_Definition, Delta_Sloc);
1027       end if;
1028
1029       return Def_Node;
1030    end P_Formal_Fixed_Point_Definition;
1031
1032    ----------------------------------------------------
1033    -- 12.5.2  Formal Ordinary Fixed Point Definition --
1034    ----------------------------------------------------
1035
1036    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
1037
1038    ---------------------------------------------------
1039    -- 12.5.2  Formal Decimal Fixed Point Definition --
1040    ---------------------------------------------------
1041
1042    --  Parsed by P_Formal_Fixed_Point_Definition (12.5.2)
1043
1044    ------------------------------------------
1045    -- 12.5.3  Formal Array Type Definition --
1046    ------------------------------------------
1047
1048    --  Parsed by P_Formal_Type_Definition (12.5)
1049
1050    -------------------------------------------
1051    -- 12.5.4  Formal Access Type Definition --
1052    -------------------------------------------
1053
1054    --  Parsed by P_Formal_Type_Definition (12.5)
1055
1056    -----------------------------------------
1057    -- 12.6  Formal Subprogram Declaration --
1058    -----------------------------------------
1059
1060    --  FORMAL_SUBPROGRAM_DECLARATION ::=
1061    --    FORMAL_CONCRETE_SUBPROGRAM_DECLARATION
1062    --  | FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION
1063
1064    --  FORMAL_CONCRETE_SUBPROGRAM_DECLARATION ::=
1065    --    with SUBPROGRAM_SPECIFICATION [is SUBPROGRAM_DEFAULT];
1066
1067    --  FORMAL_ABSTRACT_SUBPROGRAM_DECLARATION ::=
1068    --    with SUBPROGRAM_SPECIFICATION is abstract [SUBPROGRAM_DEFAULT];
1069
1070    --  SUBPROGRAM_DEFAULT ::= DEFAULT_NAME | <>
1071
1072    --  DEFAULT_NAME ::= NAME | null
1073
1074    --  The caller has checked that the initial tokens are WITH FUNCTION or
1075    --  WITH PROCEDURE, and the initial WITH has been scanned out.
1076
1077    --  A null default is an Ada 2005 feature
1078
1079    --  Error recovery: cannot raise Error_Resync
1080
1081    function P_Formal_Subprogram_Declaration return Node_Id is
1082       Prev_Sloc : constant Source_Ptr := Prev_Token_Ptr;
1083       Spec_Node : constant Node_Id    := P_Subprogram_Specification;
1084       Def_Node  : Node_Id;
1085
1086    begin
1087       if Token = Tok_Is then
1088          T_Is; -- past IS, skip extra IS or ";"
1089
1090          if Token = Tok_Abstract then
1091             Def_Node :=
1092               New_Node (N_Formal_Abstract_Subprogram_Declaration, Prev_Sloc);
1093             Scan; -- past ABSTRACT
1094
1095             if Ada_Version < Ada_05 then
1096                Error_Msg_SP
1097                  ("formal abstract subprograms are an Ada 2005 extension");
1098                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1099             end if;
1100
1101          else
1102             Def_Node :=
1103               New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
1104          end if;
1105
1106          Set_Specification (Def_Node, Spec_Node);
1107
1108          if Token = Tok_Semicolon then
1109             Scan; -- past ";"
1110
1111          elsif Token = Tok_Box then
1112             Set_Box_Present (Def_Node, True);
1113             Scan; -- past <>
1114             T_Semicolon;
1115
1116          elsif Token = Tok_Null then
1117             if Ada_Version < Ada_05 then
1118                Error_Msg_SP
1119                  ("null default subprograms are an Ada 2005 extension");
1120                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1121             end if;
1122
1123             if Nkind (Spec_Node) = N_Procedure_Specification then
1124                Set_Null_Present (Spec_Node);
1125             else
1126                Error_Msg_SP ("only procedures can be null");
1127             end if;
1128
1129             Scan;  --  past NULL
1130             T_Semicolon;
1131
1132          else
1133             Set_Default_Name (Def_Node, P_Name);
1134             T_Semicolon;
1135          end if;
1136
1137       else
1138          Def_Node :=
1139            New_Node (N_Formal_Concrete_Subprogram_Declaration, Prev_Sloc);
1140          Set_Specification (Def_Node, Spec_Node);
1141          T_Semicolon;
1142       end if;
1143
1144       return Def_Node;
1145    end P_Formal_Subprogram_Declaration;
1146
1147    ------------------------------
1148    -- 12.6  Subprogram Default --
1149    ------------------------------
1150
1151    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1152
1153    ------------------------
1154    -- 12.6  Default Name --
1155    ------------------------
1156
1157    --  Parsed by P_Formal_Procedure_Declaration (12.6)
1158
1159    --------------------------------------
1160    -- 12.7  Formal Package Declaration --
1161    --------------------------------------
1162
1163    --  FORMAL_PACKAGE_DECLARATION ::=
1164    --    with package DEFINING_IDENTIFIER
1165    --      is new generic_package_NAME FORMAL_PACKAGE_ACTUAL_PART;
1166
1167    --  FORMAL_PACKAGE_ACTUAL_PART ::=
1168    --    ([OTHERS =>] <>) |
1169    --    [GENERIC_ACTUAL_PART]
1170    --    (FORMAL_PACKAGE_ASSOCIATION {, FORMAL_PACKAGE_ASSOCIATION}
1171    --      [, OTHERS => <>)
1172
1173    --  FORMAL_PACKAGE_ASSOCIATION ::=
1174    --    GENERIC_ASSOCIATION
1175    --    | GENERIC_FORMAL_PARAMETER_SELECTOR_NAME => <>
1176
1177    --  The caller has checked that the initial tokens are WITH PACKAGE,
1178    --  and the initial WITH has been scanned out (so Token = Tok_Package).
1179
1180    --  Error recovery: cannot raise Error_Resync
1181
1182    function P_Formal_Package_Declaration return Node_Id is
1183       Def_Node : Node_Id;
1184       Scan_State : Saved_Scan_State;
1185
1186    begin
1187       Def_Node := New_Node (N_Formal_Package_Declaration, Prev_Token_Ptr);
1188       Scan; -- past PACKAGE
1189       Set_Defining_Identifier (Def_Node, P_Defining_Identifier (C_Is));
1190       T_Is;
1191       T_New;
1192       Set_Name (Def_Node, P_Qualified_Simple_Name);
1193
1194       if Token = Tok_Left_Paren then
1195          Save_Scan_State (Scan_State); -- at the left paren
1196          Scan; -- past the left paren
1197
1198          if Token = Tok_Box then
1199             Set_Box_Present (Def_Node, True);
1200             Scan; -- past box
1201             T_Right_Paren;
1202
1203          else
1204             Restore_Scan_State (Scan_State); -- to the left paren
1205             Set_Generic_Associations (Def_Node, P_Generic_Actual_Part_Opt);
1206          end if;
1207       end if;
1208
1209       T_Semicolon;
1210       return Def_Node;
1211    end P_Formal_Package_Declaration;
1212
1213    --------------------------------------
1214    -- 12.7  Formal Package Actual Part --
1215    --------------------------------------
1216
1217    --  Parsed by P_Formal_Package_Declaration (12.7)
1218
1219 end Ch12;