OSDN Git Service

Update copyright notice.
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch6.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 6                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, 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 with Sinfo.CN; use Sinfo.CN;
31
32 separate (Par)
33 package body Ch6 is
34
35    --  Local subprograms, used only in this chapter
36
37    function P_Defining_Designator        return Node_Id;
38    function P_Defining_Operator_Symbol   return Node_Id;
39    function P_Return_Object_Declaration  return Node_Id;
40
41    procedure P_Return_Subtype_Indication (Decl_Node : Node_Id);
42    --  Decl_Node is a N_Object_Declaration.
43    --  Set the Null_Exclusion_Present and Object_Definition fields of
44    --  Decl_Node.
45
46    procedure Check_Junk_Semicolon_Before_Return;
47
48    --  Check for common error of junk semicolon before RETURN keyword of
49    --  function specification. If present, skip over it with appropriate
50    --  error message, leaving Scan_Ptr pointing to the RETURN after. This
51    --  routine also deals with a possibly misspelled version of Return.
52
53    ----------------------------------------
54    -- Check_Junk_Semicolon_Before_Return --
55    ----------------------------------------
56
57    procedure Check_Junk_Semicolon_Before_Return is
58       Scan_State : Saved_Scan_State;
59
60    begin
61       if Token = Tok_Semicolon then
62          Save_Scan_State (Scan_State);
63          Scan; -- past the semicolon
64
65          if Token = Tok_Return then
66             Restore_Scan_State (Scan_State);
67             Error_Msg_SC -- CODEFIX
68               ("|extra "";"" ignored");
69             Scan; -- rescan past junk semicolon
70          else
71             Restore_Scan_State (Scan_State);
72          end if;
73
74       elsif Bad_Spelling_Of (Tok_Return) then
75          null;
76       end if;
77    end Check_Junk_Semicolon_Before_Return;
78
79    -----------------------------------------------------
80    -- 6.1  Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
81    -----------------------------------------------------
82
83    --  This routine scans out a subprogram declaration, subprogram body,
84    --  subprogram renaming declaration or subprogram generic instantiation.
85    --  It also handles the new Ada 2012 parameterized expression form
86
87    --  SUBPROGRAM_DECLARATION ::=
88    --    SUBPROGRAM_SPECIFICATION
89    --     [ASPECT_SPECIFICATIONS];
90
91    --  ABSTRACT_SUBPROGRAM_DECLARATION ::=
92    --    SUBPROGRAM_SPECIFICATION is abstract
93    --      [ASPECT_SPECIFICATIONS];
94
95    --  SUBPROGRAM_SPECIFICATION ::=
96    --      procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
97    --    | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
98
99    --  PARAMETER_PROFILE ::= [FORMAL_PART]
100
101    --  PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
102
103    --  SUBPROGRAM_BODY ::=
104    --    SUBPROGRAM_SPECIFICATION is
105    --      DECLARATIVE_PART
106    --    begin
107    --      HANDLED_SEQUENCE_OF_STATEMENTS
108    --    end [DESIGNATOR];
109
110    --  SUBPROGRAM_RENAMING_DECLARATION ::=
111    --    SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
112
113    --  SUBPROGRAM_BODY_STUB ::=
114    --    SUBPROGRAM_SPECIFICATION is separate;
115
116    --  GENERIC_INSTANTIATION ::=
117    --    procedure DEFINING_PROGRAM_UNIT_NAME is
118    --      new generic_procedure_NAME [GENERIC_ACTUAL_PART];
119    --  | function DEFINING_DESIGNATOR is
120    --      new generic_function_NAME [GENERIC_ACTUAL_PART];
121
122    --  NULL_PROCEDURE_DECLARATION ::=
123    --    SUBPROGRAM_SPECIFICATION is null;
124
125    --  Null procedures are an Ada 2005 feature. A null procedure declaration
126    --  is classified as a basic declarative item, but it is parsed here, with
127    --  other subprogram constructs.
128
129    --  PARAMETERIZED_EXPRESSION ::=
130    --    FUNCTION SPECIFICATION IS (EXPRESSION);
131
132    --  The value in Pf_Flags indicates which of these possible declarations
133    --  is acceptable to the caller:
134
135    --    Pf_Flags.Decl                 Set if declaration OK
136    --    Pf_Flags.Gins                 Set if generic instantiation OK
137    --    Pf_Flags.Pbod                 Set if proper body OK
138    --    Pf_Flags.Rnam                 Set if renaming declaration OK
139    --    Pf_Flags.Stub                 Set if body stub OK
140    --    Pf_Flags.Pexp                 Set if parameterized expression OK
141
142    --  If an inappropriate form is encountered, it is scanned out but an
143    --  error message indicating that it is appearing in an inappropriate
144    --  context is issued. The only possible values for Pf_Flags are those
145    --  defined as constants in the Par package.
146
147    --  The caller has checked that the initial token is FUNCTION, PROCEDURE,
148    --  NOT or OVERRIDING.
149
150    --  Error recovery: cannot raise Error_Resync
151
152    function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
153       Specification_Node : Node_Id;
154       Name_Node          : Node_Id;
155       Fpart_List         : List_Id;
156       Fpart_Sloc         : Source_Ptr;
157       Result_Not_Null    : Boolean := False;
158       Result_Node        : Node_Id;
159       Inst_Node          : Node_Id;
160       Body_Node          : Node_Id;
161       Decl_Node          : Node_Id;
162       Rename_Node        : Node_Id;
163       Absdec_Node        : Node_Id;
164       Stub_Node          : Node_Id;
165       Fproc_Sloc         : Source_Ptr;
166       Func               : Boolean;
167       Scan_State         : Saved_Scan_State;
168
169       --  Flags for optional overriding indication. Two flags are needed,
170       --  to distinguish positive and negative overriding indicators from
171       --  the absence of any indicator.
172
173       Is_Overriding  : Boolean := False;
174       Not_Overriding : Boolean := False;
175
176    begin
177       --  Set up scope stack entry. Note that the Labl field will be set later
178
179       SIS_Entry_Active := False;
180       SIS_Missing_Semicolon_Message := No_Error_Msg;
181       Push_Scope_Stack;
182       Scope.Table (Scope.Last).Sloc := Token_Ptr;
183       Scope.Table (Scope.Last).Etyp := E_Name;
184       Scope.Table (Scope.Last).Ecol := Start_Column;
185       Scope.Table (Scope.Last).Lreq := False;
186
187       --  Ada2005: scan leading NOT OVERRIDING indicator
188
189       if Token = Tok_Not then
190          Scan;  -- past NOT
191
192          if Token = Tok_Overriding then
193             Scan;  --  past OVERRIDING
194             Not_Overriding := True;
195
196          --  Overriding keyword used in non Ada 2005 mode
197
198          elsif Token = Tok_Identifier
199            and then Token_Name = Name_Overriding
200          then
201             Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
202             Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
203             Scan;  --  past Overriding
204             Not_Overriding := True;
205
206          else
207             Error_Msg_SC -- CODEFIX
208               ("OVERRIDING expected!");
209          end if;
210
211       --  Ada 2005: scan leading OVERRIDING indicator
212
213       --  Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
214       --  declaration circuit already gave an error message and changed the
215       --  token to Tok_Overriding.
216
217       elsif Token = Tok_Overriding then
218          Scan;  --  past OVERRIDING
219          Is_Overriding := True;
220       end if;
221
222       if Is_Overriding or else Not_Overriding then
223
224          --  Note that if we are not in Ada_2005 mode, error messages have
225          --  already been given, so no need to give another message here.
226
227          --  An overriding indicator is allowed for subprogram declarations,
228          --  bodies (including subunits), renamings, stubs, and instantiations.
229          --  The test against Pf_Decl_Pbod is added to account for the case of
230          --  subprograms declared in a protected type, where only subprogram
231          --  declarations and bodies can occur. The Pf_Pbod case is for
232          --  subunits.
233
234          if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
235               and then
236             Pf_Flags /= Pf_Decl_Pbod_Pexp
237               and then
238             Pf_Flags /= Pf_Pbod_Pexp
239          then
240             Error_Msg_SC ("overriding indicator not allowed here!");
241
242          elsif Token /= Tok_Function and then Token /= Tok_Procedure then
243             Error_Msg_SC -- CODEFIX
244               ("FUNCTION or PROCEDURE expected!");
245          end if;
246       end if;
247
248       Func := (Token = Tok_Function);
249       Fproc_Sloc := Token_Ptr;
250       Scan; -- past FUNCTION or PROCEDURE
251       Ignore (Tok_Type);
252       Ignore (Tok_Body);
253
254       if Func then
255          Name_Node := P_Defining_Designator;
256
257          if Nkind (Name_Node) = N_Defining_Operator_Symbol
258            and then Scope.Last = 1
259          then
260             Error_Msg_SP ("operator symbol not allowed at library level");
261             Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
262
263             --  Set name from file name, we need some junk name, and that's
264             --  as good as anything. This is only approximate, since we do
265             --  not do anything with non-standard name translations.
266
267             Get_Name_String (File_Name (Current_Source_File));
268
269             for J in 1 .. Name_Len loop
270                if Name_Buffer (J) = '.' then
271                   Name_Len := J - 1;
272                   exit;
273                end if;
274             end loop;
275
276             Set_Chars (Name_Node, Name_Find);
277             Set_Error_Posted (Name_Node);
278          end if;
279
280       else
281          Name_Node := P_Defining_Program_Unit_Name;
282       end if;
283
284       Scope.Table (Scope.Last).Labl := Name_Node;
285       Ignore (Tok_Colon);
286
287       --  Deal with generic instantiation, the one case in which we do not
288       --  have a subprogram specification as part of whatever we are parsing
289
290       if Token = Tok_Is then
291          Save_Scan_State (Scan_State); -- at the IS
292          T_Is; -- checks for redundant IS
293
294          if Token = Tok_New then
295             if not Pf_Flags.Gins then
296                Error_Msg_SC ("generic instantiation not allowed here!");
297             end if;
298
299             Scan; -- past NEW
300
301             if Func then
302                Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
303                Set_Name (Inst_Node, P_Function_Name);
304             else
305                Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
306                Set_Name (Inst_Node, P_Qualified_Simple_Name);
307             end if;
308
309             Set_Defining_Unit_Name (Inst_Node, Name_Node);
310             Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
311             P_Aspect_Specifications (Inst_Node);
312             Pop_Scope_Stack; -- Don't need scope stack entry in this case
313
314             if Is_Overriding then
315                Set_Must_Override (Inst_Node);
316
317             elsif Not_Overriding then
318                Set_Must_Not_Override (Inst_Node);
319             end if;
320
321             return Inst_Node;
322
323          else
324             Restore_Scan_State (Scan_State); -- to the IS
325          end if;
326       end if;
327
328       --  If not a generic instantiation, then we definitely have a subprogram
329       --  specification (all possibilities at this stage include one here)
330
331       Fpart_Sloc := Token_Ptr;
332
333       Check_Misspelling_Of (Tok_Return);
334
335       --  Scan formal part. First a special error check. If we have an
336       --  identifier here, then we have a definite error. If this identifier
337       --  is on the same line as the designator, then we assume it is the
338       --  first formal after a missing left parenthesis
339
340       if Token = Tok_Identifier
341         and then not Token_Is_At_Start_Of_Line
342       then
343             T_Left_Paren; -- to generate message
344             Fpart_List := P_Formal_Part;
345
346       --  Otherwise scan out an optional formal part in the usual manner
347
348       else
349          Fpart_List := P_Parameter_Profile;
350       end if;
351
352       --  We treat what we have as a function specification if FUNCTION was
353       --  used, or if a RETURN is present. This gives better error recovery
354       --  since later RETURN statements will be valid in either case.
355
356       Check_Junk_Semicolon_Before_Return;
357       Result_Node := Error;
358
359       if Token = Tok_Return then
360          if not Func then
361             Error_Msg -- CODEFIX
362               ("PROCEDURE should be FUNCTION", Fproc_Sloc);
363             Func := True;
364          end if;
365
366          Scan; -- past RETURN
367
368          Result_Not_Null := P_Null_Exclusion;     --  Ada 2005 (AI-231)
369
370          --  Ada 2005 (AI-318-02)
371
372          if Token = Tok_Access then
373             if Ada_Version < Ada_2005 then
374                Error_Msg_SC
375                  ("anonymous access result type is an Ada 2005 extension");
376                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
377             end if;
378
379             Result_Node := P_Access_Definition (Result_Not_Null);
380
381          else
382             Result_Node := P_Subtype_Mark;
383             No_Constraint;
384          end if;
385
386       else
387          --  Skip extra parenthesis at end of formal part
388
389          Ignore (Tok_Right_Paren);
390
391          --  For function, scan result subtype
392
393          if Func then
394             TF_Return;
395
396             if Prev_Token = Tok_Return then
397                Result_Node := P_Subtype_Mark;
398             end if;
399          end if;
400       end if;
401
402       if Func then
403          Specification_Node :=
404            New_Node (N_Function_Specification, Fproc_Sloc);
405
406          Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
407          Set_Result_Definition (Specification_Node, Result_Node);
408
409       else
410          Specification_Node :=
411            New_Node (N_Procedure_Specification, Fproc_Sloc);
412       end if;
413
414       Set_Defining_Unit_Name (Specification_Node, Name_Node);
415       Set_Parameter_Specifications (Specification_Node, Fpart_List);
416
417       if Is_Overriding then
418          Set_Must_Override (Specification_Node);
419
420       elsif Not_Overriding then
421          Set_Must_Not_Override (Specification_Node);
422       end if;
423
424       --  Error check: barriers not allowed on protected functions/procedures
425
426       if Token = Tok_When then
427          if Func then
428             Error_Msg_SC ("barrier not allowed on function, only on entry");
429          else
430             Error_Msg_SC ("barrier not allowed on procedure, only on entry");
431          end if;
432
433          Scan; -- past WHEN
434          Discard_Junk_Node (P_Expression);
435       end if;
436
437       --  Deal with semicolon followed by IS. We want to treat this as IS
438
439       if Token = Tok_Semicolon then
440          Save_Scan_State (Scan_State);
441          Scan; -- past semicolon
442
443          if Token = Tok_Is then
444             Error_Msg_SP -- CODEFIX
445               ("extra "";"" ignored");
446          else
447             Restore_Scan_State (Scan_State);
448          end if;
449       end if;
450
451       --  Subprogram declaration ended by aspect specifications
452
453       if Aspect_Specifications_Present then
454          goto Subprogram_Declaration;
455
456       --  Deal with case of semicolon ending a subprogram declaration
457
458       elsif Token = Tok_Semicolon then
459          if not Pf_Flags.Decl then
460             T_Is;
461          end if;
462
463          Save_Scan_State (Scan_State);
464          Scan; -- past semicolon
465
466          --  If semicolon is immediately followed by IS, then ignore the
467          --  semicolon, and go process the body.
468
469          if Token = Tok_Is then
470             Error_Msg_SP -- CODEFIX
471               ("|extra "";"" ignored");
472             T_Is; -- scan past IS
473             goto Subprogram_Body;
474
475          --  If BEGIN follows in an appropriate column, we immediately
476          --  commence the error action of assuming that the previous
477          --  subprogram declaration should have been a subprogram body,
478          --  i.e. that the terminating semicolon should have been IS.
479
480          elsif Token = Tok_Begin
481             and then Start_Column >= Scope.Table (Scope.Last).Ecol
482          then
483             Error_Msg_SP -- CODEFIX
484               ("|"";"" should be IS!");
485             goto Subprogram_Body;
486
487          else
488             Restore_Scan_State (Scan_State);
489             goto Subprogram_Declaration;
490          end if;
491
492       --  Case of not followed by semicolon
493
494       else
495          --  Subprogram renaming declaration case
496
497          Check_Misspelling_Of (Tok_Renames);
498
499          if Token = Tok_Renames then
500             if not Pf_Flags.Rnam then
501                Error_Msg_SC ("renaming declaration not allowed here!");
502             end if;
503
504             Rename_Node :=
505               New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
506             Scan; -- past RENAMES
507             Set_Name (Rename_Node, P_Name);
508             Set_Specification (Rename_Node, Specification_Node);
509             TF_Semicolon;
510             Pop_Scope_Stack;
511             return Rename_Node;
512
513          --  Case of IS following subprogram specification
514
515          elsif Token = Tok_Is then
516             T_Is; -- ignore redundant Is's
517
518             if Token_Name = Name_Abstract then
519                Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
520             end if;
521
522             --  Deal nicely with (now obsolete) use of <> in place of abstract
523
524             if Token = Tok_Box then
525                Error_Msg_SC -- CODEFIX
526                  ("ABSTRACT expected");
527                Token := Tok_Abstract;
528             end if;
529
530             --  Abstract subprogram declaration case
531
532             if Token = Tok_Abstract then
533                Absdec_Node :=
534                  New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
535                Set_Specification (Absdec_Node, Specification_Node);
536                Pop_Scope_Stack; -- discard unneeded entry
537                Scan; -- past ABSTRACT
538                P_Aspect_Specifications (Absdec_Node);
539                return Absdec_Node;
540
541             --  Ada 2005 (AI-248): Parse a null procedure declaration
542
543             elsif Token = Tok_Null then
544                if Ada_Version < Ada_2005 then
545                   Error_Msg_SP ("null procedures are an Ada 2005 extension");
546                   Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
547                end if;
548
549                Scan; -- past NULL
550
551                if Func then
552                   Error_Msg_SP ("only procedures can be null");
553                else
554                   Set_Null_Present (Specification_Node);
555                end if;
556
557                goto Subprogram_Declaration;
558
559             --  Check for IS NEW with Formal_Part present and handle nicely
560
561             elsif Token = Tok_New then
562                Error_Msg
563                  ("formal part not allowed in instantiation", Fpart_Sloc);
564                Scan; -- past NEW
565
566                if Func then
567                   Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
568                else
569                   Inst_Node :=
570                     New_Node (N_Procedure_Instantiation, Fproc_Sloc);
571                end if;
572
573                Set_Defining_Unit_Name (Inst_Node, Name_Node);
574                Set_Name (Inst_Node, P_Name);
575                Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
576                TF_Semicolon;
577                Pop_Scope_Stack; -- Don't need scope stack entry in this case
578                return Inst_Node;
579
580             else
581                goto Subprogram_Body;
582             end if;
583
584          --  Aspect specifications present
585
586          elsif Aspect_Specifications_Present then
587             goto Subprogram_Declaration;
588
589          --  Here we have a missing IS or missing semicolon, we always guess
590          --  a missing semicolon, since we are pretty good at fixing up a
591          --  semicolon which should really be an IS
592
593          else
594             Error_Msg_AP -- CODEFIX
595               ("|missing "";""");
596             SIS_Missing_Semicolon_Message := Get_Msg_Id;
597             goto Subprogram_Declaration;
598          end if;
599       end if;
600
601       --  Processing for stub or subprogram body or parameterized expression
602
603       <<Subprogram_Body>>
604
605          --  Subprogram body stub case
606
607          if Separate_Present then
608             if not Pf_Flags.Stub then
609                Error_Msg_SC ("body stub not allowed here!");
610             end if;
611
612             if Nkind (Name_Node) = N_Defining_Operator_Symbol then
613                Error_Msg
614                  ("operator symbol cannot be used as subunit name",
615                   Sloc (Name_Node));
616             end if;
617
618             Stub_Node :=
619               New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
620             Set_Specification (Stub_Node, Specification_Node);
621             Scan; -- past SEPARATE
622             Pop_Scope_Stack;
623             TF_Semicolon;
624             return Stub_Node;
625
626          --  Subprogram body or parameterized expression case
627
628          else
629             Scan_Body_Or_Parameterized_Expression : declare
630
631                function Likely_Parameterized_Expression return Boolean;
632                --  Returns True if we have a probably case of a parameterized
633                --  expression omitting the parentheses, if so, returns True
634                --  and emits an appropriate error message, else returns False.
635
636                -------------------------------------
637                -- Likely_Parameterized_Expression --
638                -------------------------------------
639
640                function Likely_Parameterized_Expression return Boolean is
641                begin
642                   --  If currently pointing to BEGIN or a declaration keyword
643                   --  or a pragma, then we definitely have a subprogram body.
644                   --  This is a common case, so worth testing first.
645
646                   if Token = Tok_Begin
647                     or else Token in Token_Class_Declk
648                     or else Token = Tok_Pragma
649                   then
650                      return False;
651
652                   --  Test for tokens which could only start an expression and
653                   --  thus signal the case of a parameterized expression.
654
655                   elsif Token in Token_Class_Literal
656                     or else Token in Token_Class_Unary_Addop
657                     or else Token = Tok_Left_Paren
658                     or else Token = Tok_Abs
659                     or else Token = Tok_Null
660                     or else Token = Tok_New
661                     or else Token = Tok_Not
662                   then
663                      null;
664
665                   --  Anything other than an identifier must be a body
666
667                   elsif Token /= Tok_Identifier then
668                      return False;
669
670                   --  Here for an identifier
671
672                   else
673                      --  If the identifier is the first token on its line, then
674                      --  let's assume that we have a missing begin and this is
675                      --  intended as a subprogram body.
676
677                      if Token_Is_At_Start_Of_Line then
678                         return False;
679
680                      --  Otherwise we have to scan ahead. If the identifier is
681                      --  followed by a colon or a comma, it is a declaration
682                      --  and hence we have a subprogram body. Otherwise assume
683                      --  a parameterized expression.
684
685                      else
686                         declare
687                            Scan_State : Saved_Scan_State;
688                            Tok        : Token_Type;
689                         begin
690                            Save_Scan_State (Scan_State);
691                            Scan; -- past identifier
692                            Tok := Token;
693                            Restore_Scan_State (Scan_State);
694
695                            if Tok = Tok_Colon or else Tok = Tok_Comma then
696                               return False;
697                            end if;
698                         end;
699                      end if;
700                   end if;
701
702                   --  Fall through if we have a likely parameterized expression
703
704                   Error_Msg_SC
705                     ("parameterized expression must be "
706                      & "enclosed in parentheses");
707                   return True;
708                end Likely_Parameterized_Expression;
709
710             --  Start of processing for Scan_Body_Or_Parameterized_Expression
711
712             begin
713                --  Parameterized_Expression case
714
715                if Token = Tok_Left_Paren
716                  or else Likely_Parameterized_Expression
717                then
718                   --  Check parameterized expression allowed here
719
720                   if not Pf_Flags.Pexp then
721                      Error_Msg_SC
722                        ("parameterized expression not allowed here!");
723                   end if;
724
725                   --  Check we are in Ada 2012 mode
726
727                   if Ada_Version < Ada_2012 then
728                      Error_Msg_SC
729                        ("parameterized expression is an Ada 2012 feature!");
730                      Error_Msg_SC
731                        ("\unit must be compiled with -gnat2012 switch!");
732                   end if;
733
734                   --  Parse out expression and build parameterized expression
735
736                   Body_Node :=
737                     New_Node
738                       (N_Parameterized_Expression, Sloc (Specification_Node));
739                   Set_Specification (Body_Node, Specification_Node);
740                   Set_Expression (Body_Node, P_Expression);
741                   T_Semicolon;
742                   Pop_Scope_Stack;
743
744                --  Subprogram body case
745
746                else
747                   --  Check body allowed here
748
749                   if not Pf_Flags.Pbod then
750                      Error_Msg_SP ("subprogram body not allowed here!");
751                   end if;
752
753                   --  Here is the test for a suspicious IS (i.e. one that
754                   --  looks like it might more properly be a semicolon).
755                   --  See separate section describing use of IS instead
756                   --  of semicolon in package Parse.
757
758                   if (Token in Token_Class_Declk
759                         or else
760                       Token = Tok_Identifier)
761                     and then Start_Column <= Scope.Table (Scope.Last).Ecol
762                     and then Scope.Last /= 1
763                   then
764                      Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
765                      Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
766                   end if;
767
768                   --  Build and return subprogram body, parsing declarations
769                   --  and statement sequence that belong to the body.
770
771                   Body_Node :=
772                     New_Node (N_Subprogram_Body, Sloc (Specification_Node));
773                   Set_Specification (Body_Node, Specification_Node);
774                   Parse_Decls_Begin_End (Body_Node);
775                end if;
776
777                return Body_Node;
778             end Scan_Body_Or_Parameterized_Expression;
779          end if;
780
781       --  Processing for subprogram declaration
782
783       <<Subprogram_Declaration>>
784          Decl_Node :=
785            New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
786          Set_Specification (Decl_Node, Specification_Node);
787          P_Aspect_Specifications (Decl_Node);
788
789          --  If this is a context in which a subprogram body is permitted,
790          --  set active SIS entry in case (see section titled "Handling
791          --  Semicolon Used in Place of IS" in body of Parser package)
792          --  Note that SIS_Missing_Semicolon_Message is already set properly.
793
794          if Pf_Flags.Pbod then
795             SIS_Labl := Scope.Table (Scope.Last).Labl;
796             SIS_Sloc := Scope.Table (Scope.Last).Sloc;
797             SIS_Ecol := Scope.Table (Scope.Last).Ecol;
798             SIS_Declaration_Node := Decl_Node;
799             SIS_Semicolon_Sloc := Prev_Token_Ptr;
800             SIS_Entry_Active := True;
801          end if;
802
803          Pop_Scope_Stack;
804          return Decl_Node;
805    end P_Subprogram;
806
807    ---------------------------------
808    -- 6.1  Subprogram Declaration --
809    ---------------------------------
810
811    --  Parsed by P_Subprogram (6.1)
812
813    ------------------------------------------
814    -- 6.1  Abstract Subprogram Declaration --
815    ------------------------------------------
816
817    --  Parsed by P_Subprogram (6.1)
818
819    -----------------------------------
820    -- 6.1  Subprogram Specification --
821    -----------------------------------
822
823    --  SUBPROGRAM_SPECIFICATION ::=
824    --      procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
825    --    | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
826
827    --  PARAMETER_PROFILE ::= [FORMAL_PART]
828
829    --  PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
830
831    --  Subprogram specifications that appear in subprogram declarations
832    --  are parsed by P_Subprogram (6.1). This routine is used in other
833    --  contexts where subprogram specifications occur.
834
835    --  Note: this routine does not affect the scope stack in any way
836
837    --  Error recovery: can raise Error_Resync
838
839    function P_Subprogram_Specification return Node_Id is
840       Specification_Node : Node_Id;
841       Result_Not_Null    : Boolean;
842       Result_Node        : Node_Id;
843
844    begin
845       if Token = Tok_Function then
846          Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
847          Scan; -- past FUNCTION
848          Ignore (Tok_Body);
849          Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
850          Set_Parameter_Specifications
851            (Specification_Node, P_Parameter_Profile);
852          Check_Junk_Semicolon_Before_Return;
853          TF_Return;
854
855          Result_Not_Null := P_Null_Exclusion;     --  Ada 2005 (AI-231)
856
857          --  Ada 2005 (AI-318-02)
858
859          if Token = Tok_Access then
860             if Ada_Version < Ada_2005 then
861                Error_Msg_SC
862                  ("anonymous access result type is an Ada 2005 extension");
863                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
864             end if;
865
866             Result_Node := P_Access_Definition (Result_Not_Null);
867
868          else
869             Result_Node := P_Subtype_Mark;
870             No_Constraint;
871          end if;
872
873          Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
874          Set_Result_Definition (Specification_Node, Result_Node);
875          return Specification_Node;
876
877       elsif Token = Tok_Procedure then
878          Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
879          Scan; -- past PROCEDURE
880          Ignore (Tok_Body);
881          Set_Defining_Unit_Name
882            (Specification_Node, P_Defining_Program_Unit_Name);
883          Set_Parameter_Specifications
884            (Specification_Node, P_Parameter_Profile);
885          return Specification_Node;
886
887       else
888          Error_Msg_SC ("subprogram specification expected");
889          raise Error_Resync;
890       end if;
891    end P_Subprogram_Specification;
892
893    ---------------------
894    -- 6.1  Designator --
895    ---------------------
896
897    --  DESIGNATOR ::=
898    --    [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
899
900    --  The caller has checked that the initial token is an identifier,
901    --  operator symbol, or string literal. Note that we don't bother to
902    --  do much error diagnosis in this routine, since it is only used for
903    --  the label on END lines, and the routines in package Par.Endh will
904    --  check that the label is appropriate.
905
906    --  Error recovery: cannot raise Error_Resync
907
908    function P_Designator return Node_Id is
909       Ident_Node  : Node_Id;
910       Name_Node   : Node_Id;
911       Prefix_Node : Node_Id;
912
913       function Real_Dot return Boolean;
914       --  Tests if a current token is an interesting period, i.e. is followed
915       --  by an identifier or operator symbol or string literal. If not, it is
916       --  probably just incorrect punctuation to be caught by our caller. Note
917       --  that the case of an operator symbol or string literal is also an
918       --  error, but that is an error that we catch here. If the result is
919       --  True, a real dot has been scanned and we are positioned past it,
920       --  if the result is False, the scan position is unchanged.
921
922       --------------
923       -- Real_Dot --
924       --------------
925
926       function Real_Dot return Boolean is
927          Scan_State  : Saved_Scan_State;
928
929       begin
930          if Token /= Tok_Dot then
931             return False;
932
933          else
934             Save_Scan_State (Scan_State);
935             Scan; -- past dot
936
937             if Token = Tok_Identifier
938               or else Token = Tok_Operator_Symbol
939               or else Token = Tok_String_Literal
940             then
941                return True;
942
943             else
944                Restore_Scan_State (Scan_State);
945                return False;
946             end if;
947          end if;
948       end Real_Dot;
949
950    --  Start of processing for P_Designator
951
952    begin
953       Ident_Node := Token_Node;
954       Scan; -- past initial token
955
956       if Prev_Token = Tok_Operator_Symbol
957         or else Prev_Token = Tok_String_Literal
958         or else not Real_Dot
959       then
960          return Ident_Node;
961
962       --  Child name case
963
964       else
965          Prefix_Node := Ident_Node;
966
967          --  Loop through child names, on entry to this loop, Prefix contains
968          --  the name scanned so far, and Ident_Node is the last identifier.
969
970          loop
971             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
972             Set_Prefix (Name_Node, Prefix_Node);
973             Ident_Node := P_Identifier;
974             Set_Selector_Name (Name_Node, Ident_Node);
975             Prefix_Node := Name_Node;
976             exit when not Real_Dot;
977          end loop;
978
979          --  On exit from the loop, Ident_Node is the last identifier scanned,
980          --  i.e. the defining identifier, and Prefix_Node is a node for the
981          --  entire name, structured (incorrectly!) as a selected component.
982
983          Name_Node := Prefix (Prefix_Node);
984          Change_Node (Prefix_Node, N_Designator);
985          Set_Name (Prefix_Node, Name_Node);
986          Set_Identifier (Prefix_Node, Ident_Node);
987          return Prefix_Node;
988       end if;
989
990    exception
991       when Error_Resync =>
992          while Token = Tok_Dot or else Token = Tok_Identifier loop
993             Scan;
994          end loop;
995
996          return Error;
997    end P_Designator;
998
999    ------------------------------
1000    -- 6.1  Defining Designator --
1001    ------------------------------
1002
1003    --  DEFINING_DESIGNATOR ::=
1004    --    DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1005
1006    --  Error recovery: cannot raise Error_Resync
1007
1008    function P_Defining_Designator return Node_Id is
1009    begin
1010       if Token = Tok_Operator_Symbol then
1011          return P_Defining_Operator_Symbol;
1012
1013       elsif Token = Tok_String_Literal then
1014          Error_Msg_SC ("invalid operator name");
1015          Scan; -- past junk string
1016          return Error;
1017
1018       else
1019          return P_Defining_Program_Unit_Name;
1020       end if;
1021    end P_Defining_Designator;
1022
1023    -------------------------------------
1024    -- 6.1  Defining Program Unit Name --
1025    -------------------------------------
1026
1027    --  DEFINING_PROGRAM_UNIT_NAME ::=
1028    --    [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1029
1030    --  Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1031
1032    --  Error recovery: cannot raise Error_Resync
1033
1034    function P_Defining_Program_Unit_Name return Node_Id is
1035       Ident_Node  : Node_Id;
1036       Name_Node   : Node_Id;
1037       Prefix_Node : Node_Id;
1038
1039    begin
1040       --  Set identifier casing if not already set and scan initial identifier
1041
1042       if Token = Tok_Identifier
1043         and then Identifier_Casing (Current_Source_File) = Unknown
1044       then
1045          Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1046       end if;
1047
1048       Ident_Node := P_Identifier (C_Dot);
1049       Merge_Identifier (Ident_Node, Tok_Return);
1050
1051       --  Normal case (not child library unit name)
1052
1053       if Token /= Tok_Dot then
1054          Change_Identifier_To_Defining_Identifier (Ident_Node);
1055          return Ident_Node;
1056
1057       --  Child library unit name case
1058
1059       else
1060          if Scope.Last > 1 then
1061             Error_Msg_SP ("child unit allowed only at library level");
1062             raise Error_Resync;
1063
1064          elsif Ada_Version = Ada_83 then
1065             Error_Msg_SP ("(Ada 83) child unit not allowed!");
1066
1067          end if;
1068
1069          Prefix_Node := Ident_Node;
1070
1071          --  Loop through child names, on entry to this loop, Prefix contains
1072          --  the name scanned so far, and Ident_Node is the last identifier.
1073
1074          loop
1075             exit when Token /= Tok_Dot;
1076             Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1077             Scan; -- past period
1078             Set_Prefix (Name_Node, Prefix_Node);
1079             Ident_Node := P_Identifier (C_Dot);
1080             Set_Selector_Name (Name_Node, Ident_Node);
1081             Prefix_Node := Name_Node;
1082          end loop;
1083
1084          --  On exit from the loop, Ident_Node is the last identifier scanned,
1085          --  i.e. the defining identifier, and Prefix_Node is a node for the
1086          --  entire name, structured (incorrectly!) as a selected component.
1087
1088          Name_Node := Prefix (Prefix_Node);
1089          Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1090          Set_Name (Prefix_Node, Name_Node);
1091          Change_Identifier_To_Defining_Identifier (Ident_Node);
1092          Set_Defining_Identifier (Prefix_Node, Ident_Node);
1093
1094          --  All set with unit name parsed
1095
1096          return Prefix_Node;
1097       end if;
1098
1099    exception
1100       when Error_Resync =>
1101          while Token = Tok_Dot or else Token = Tok_Identifier loop
1102             Scan;
1103          end loop;
1104
1105          return Error;
1106    end P_Defining_Program_Unit_Name;
1107
1108    --------------------------
1109    -- 6.1  Operator Symbol --
1110    --------------------------
1111
1112    --  OPERATOR_SYMBOL ::= STRING_LITERAL
1113
1114    --  Operator symbol is returned by the scanner as Tok_Operator_Symbol
1115
1116    -----------------------------------
1117    -- 6.1  Defining Operator Symbol --
1118    -----------------------------------
1119
1120    --  DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1121
1122    --  The caller has checked that the initial symbol is an operator symbol
1123
1124    function P_Defining_Operator_Symbol return Node_Id is
1125       Op_Node : Node_Id;
1126
1127    begin
1128       Op_Node := Token_Node;
1129       Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1130       Scan; -- past operator symbol
1131       return Op_Node;
1132    end P_Defining_Operator_Symbol;
1133
1134    ----------------------------
1135    -- 6.1  Parameter_Profile --
1136    ----------------------------
1137
1138    --  PARAMETER_PROFILE ::= [FORMAL_PART]
1139
1140    --  Empty is returned if no formal part is present
1141
1142    --  Error recovery: cannot raise Error_Resync
1143
1144    function P_Parameter_Profile return List_Id is
1145    begin
1146       if Token = Tok_Left_Paren then
1147          Scan; -- part left paren
1148          return P_Formal_Part;
1149       else
1150          return No_List;
1151       end if;
1152    end P_Parameter_Profile;
1153
1154    ---------------------------------------
1155    -- 6.1  Parameter And Result Profile --
1156    ---------------------------------------
1157
1158    --  Parsed by its parent construct, which uses P_Parameter_Profile to
1159    --  parse the parameters, and P_Subtype_Mark to parse the return type.
1160
1161    ----------------------
1162    -- 6.1  Formal part --
1163    ----------------------
1164
1165    --  FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1166
1167    --  PARAMETER_SPECIFICATION ::=
1168    --    DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
1169    --      [:= DEFAULT_EXPRESSION]
1170    --  | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1171    --      [:= DEFAULT_EXPRESSION]
1172
1173    --  This scans the construct Formal_Part. The caller has already checked
1174    --  that the initial token is a left parenthesis, and skipped past it, so
1175    --  that on entry Token is the first token following the left parenthesis.
1176
1177    --  Error recovery: cannot raise Error_Resync
1178
1179    function P_Formal_Part return List_Id is
1180       Specification_List : List_Id;
1181       Specification_Node : Node_Id;
1182       Scan_State         : Saved_Scan_State;
1183       Num_Idents         : Nat;
1184       Ident              : Nat;
1185       Ident_Sloc         : Source_Ptr;
1186       Not_Null_Present   : Boolean := False;
1187       Not_Null_Sloc      : Source_Ptr;
1188
1189       Idents : array (Int range 1 .. 4096) of Entity_Id;
1190       --  This array holds the list of defining identifiers. The upper bound
1191       --  of 4096 is intended to be essentially infinite, and we do not even
1192       --  bother to check for it being exceeded.
1193
1194    begin
1195       Specification_List := New_List;
1196       Specification_Loop : loop
1197          begin
1198             if Token = Tok_Pragma then
1199                Error_Msg_SC ("pragma not allowed in formal part");
1200                Discard_Junk_Node (P_Pragma (Skipping => True));
1201             end if;
1202
1203             Ignore (Tok_Left_Paren);
1204             Ident_Sloc := Token_Ptr;
1205             Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1206             Num_Idents := 1;
1207
1208             Ident_Loop : loop
1209                exit Ident_Loop when Token = Tok_Colon;
1210
1211                --  The only valid tokens are colon and comma, so if we have
1212                --  neither do a bit of investigation to see which is the
1213                --  better choice for insertion.
1214
1215                if Token /= Tok_Comma then
1216
1217                   --  Assume colon if IN or OUT keyword found
1218
1219                   exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1220
1221                   --  Otherwise scan ahead
1222
1223                   Save_Scan_State (Scan_State);
1224                   Look_Ahead : loop
1225
1226                      --  If we run into a semicolon, then assume that a
1227                      --  colon was missing, e.g.  Parms (X Y; ...). Also
1228                      --  assume missing colon on EOF (a real disaster!)
1229                      --  and on a right paren, e.g. Parms (X Y), and also
1230                      --  on an assignment symbol, e.g. Parms (X Y := ..)
1231
1232                      if Token = Tok_Semicolon
1233                        or else Token = Tok_Right_Paren
1234                        or else Token = Tok_EOF
1235                        or else Token = Tok_Colon_Equal
1236                      then
1237                         Restore_Scan_State (Scan_State);
1238                         exit Ident_Loop;
1239
1240                      --  If we run into a colon, assume that we had a missing
1241                      --  comma, e.g. Parms (A B : ...). Also assume a missing
1242                      --  comma if we hit another comma, e.g. Parms (A B, C ..)
1243
1244                      elsif Token = Tok_Colon
1245                        or else Token = Tok_Comma
1246                      then
1247                         Restore_Scan_State (Scan_State);
1248                         exit Look_Ahead;
1249                      end if;
1250
1251                      Scan;
1252                   end loop Look_Ahead;
1253                end if;
1254
1255                --  Here if a comma is present, or to be assumed
1256
1257                T_Comma;
1258                Num_Idents := Num_Idents + 1;
1259                Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1260             end loop Ident_Loop;
1261
1262             --  Fall through the loop on encountering a colon, or deciding
1263             --  that there is a missing colon.
1264
1265             T_Colon;
1266
1267             --  If there are multiple identifiers, we repeatedly scan the
1268             --  type and initialization expression information by resetting
1269             --  the scan pointer (so that we get completely separate trees
1270             --  for each occurrence).
1271
1272             if Num_Idents > 1 then
1273                Save_Scan_State (Scan_State);
1274             end if;
1275
1276             --  Loop through defining identifiers in list
1277
1278             Ident := 1;
1279
1280             Ident_List_Loop : loop
1281                Specification_Node :=
1282                  New_Node (N_Parameter_Specification, Ident_Sloc);
1283                Set_Defining_Identifier (Specification_Node, Idents (Ident));
1284
1285                --  Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1286
1287                Not_Null_Sloc := Token_Ptr;
1288                Not_Null_Present :=
1289                  P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1290
1291                --  Case of ACCESS keyword present
1292
1293                if Token = Tok_Access then
1294                   Set_Null_Exclusion_Present
1295                     (Specification_Node, Not_Null_Present);
1296
1297                   if Ada_Version = Ada_83 then
1298                      Error_Msg_SC ("(Ada 83) access parameters not allowed");
1299                   end if;
1300
1301                   Set_Parameter_Type
1302                     (Specification_Node,
1303                      P_Access_Definition (Not_Null_Present));
1304
1305                --  Case of IN or OUT present
1306
1307                else
1308                   if Token = Tok_In or else Token = Tok_Out then
1309                      if Not_Null_Present then
1310                         Error_Msg
1311                           ("`NOT NULL` can only be used with `ACCESS`",
1312                            Not_Null_Sloc);
1313
1314                         if Token = Tok_In then
1315                            Error_Msg
1316                              ("\`IN` not allowed together with `ACCESS`",
1317                               Not_Null_Sloc);
1318                         else
1319                            Error_Msg
1320                              ("\`OUT` not allowed together with `ACCESS`",
1321                               Not_Null_Sloc);
1322                         end if;
1323                      end if;
1324
1325                      P_Mode (Specification_Node);
1326                      Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1327                   end if;
1328
1329                   Set_Null_Exclusion_Present
1330                     (Specification_Node, Not_Null_Present);
1331
1332                   if Token = Tok_Procedure
1333                        or else
1334                      Token = Tok_Function
1335                   then
1336                      Error_Msg_SC ("formal subprogram parameter not allowed");
1337                      Scan;
1338
1339                      if Token = Tok_Left_Paren then
1340                         Discard_Junk_List (P_Formal_Part);
1341                      end if;
1342
1343                      if Token = Tok_Return then
1344                         Scan;
1345                         Discard_Junk_Node (P_Subtype_Mark);
1346                      end if;
1347
1348                      Set_Parameter_Type (Specification_Node, Error);
1349
1350                   else
1351                      Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1352                      No_Constraint;
1353                   end if;
1354                end if;
1355
1356                Set_Expression (Specification_Node, Init_Expr_Opt (True));
1357
1358                if Ident > 1 then
1359                   Set_Prev_Ids (Specification_Node, True);
1360                end if;
1361
1362                if Ident < Num_Idents then
1363                   Set_More_Ids (Specification_Node, True);
1364                end if;
1365
1366                Append (Specification_Node, Specification_List);
1367                exit Ident_List_Loop when Ident = Num_Idents;
1368                Ident := Ident + 1;
1369                Restore_Scan_State (Scan_State);
1370             end loop Ident_List_Loop;
1371
1372          exception
1373             when Error_Resync =>
1374                Resync_Semicolon_List;
1375          end;
1376
1377          if Token = Tok_Semicolon then
1378             Save_Scan_State (Scan_State);
1379             Scan; -- past semicolon
1380
1381             --  If we have RETURN or IS after the semicolon, then assume
1382             --  that semicolon should have been a right parenthesis and exit
1383
1384             if Token = Tok_Is or else Token = Tok_Return then
1385                Error_Msg_SP -- CODEFIX
1386                  ("|"";"" should be "")""");
1387                exit Specification_Loop;
1388             end if;
1389
1390             --  If we have a declaration keyword after the semicolon, then
1391             --  assume we had a missing right parenthesis and terminate list
1392
1393             if Token in Token_Class_Declk then
1394                Error_Msg_AP -- CODEFIX
1395                  ("missing "")""");
1396                Restore_Scan_State (Scan_State);
1397                exit Specification_Loop;
1398             end if;
1399
1400          elsif Token = Tok_Right_Paren then
1401             Scan; -- past right paren
1402             exit Specification_Loop;
1403
1404          --  Special check for common error of using comma instead of semicolon
1405
1406          elsif Token = Tok_Comma then
1407             T_Semicolon;
1408             Scan; -- past comma
1409
1410          --  Special check for omitted separator
1411
1412          elsif Token = Tok_Identifier then
1413             T_Semicolon;
1414
1415          --  If nothing sensible, skip to next semicolon or right paren
1416
1417          else
1418             T_Semicolon;
1419             Resync_Semicolon_List;
1420
1421             if Token = Tok_Semicolon then
1422                Scan; -- past semicolon
1423             else
1424                T_Right_Paren;
1425                exit Specification_Loop;
1426             end if;
1427          end if;
1428       end loop Specification_Loop;
1429
1430       return Specification_List;
1431    end P_Formal_Part;
1432
1433    ----------------------------------
1434    -- 6.1  Parameter Specification --
1435    ----------------------------------
1436
1437    --  Parsed by P_Formal_Part (6.1)
1438
1439    ---------------
1440    -- 6.1  Mode --
1441    ---------------
1442
1443    --  MODE ::= [in] | in out | out
1444
1445    --  There is no explicit node in the tree for the Mode. Instead the
1446    --  In_Present and Out_Present flags are set in the parent node to
1447    --  record the presence of keywords specifying the mode.
1448
1449    --  Error_Recovery: cannot raise Error_Resync
1450
1451    procedure P_Mode (Node : Node_Id) is
1452    begin
1453       if Token = Tok_In then
1454          Scan; -- past IN
1455          Set_In_Present (Node, True);
1456
1457          if Style.Mode_In_Check and then Token /= Tok_Out then
1458             Error_Msg_SP -- CODEFIX
1459               ("(style) IN should be omitted");
1460          end if;
1461
1462          if Token = Tok_Access then
1463             Error_Msg_SP ("IN not allowed together with ACCESS");
1464             Scan; -- past ACCESS
1465          end if;
1466       end if;
1467
1468       if Token = Tok_Out then
1469          Scan; -- past OUT
1470          Set_Out_Present (Node, True);
1471       end if;
1472
1473       if Token = Tok_In then
1474          Error_Msg_SC ("IN must precede OUT in parameter mode");
1475          Scan; -- past IN
1476          Set_In_Present (Node, True);
1477       end if;
1478    end P_Mode;
1479
1480    --------------------------
1481    -- 6.3  Subprogram Body --
1482    --------------------------
1483
1484    --  Parsed by P_Subprogram (6.1)
1485
1486    -----------------------------------
1487    -- 6.4  Procedure Call Statement --
1488    -----------------------------------
1489
1490    --  Parsed by P_Sequence_Of_Statements (5.1)
1491
1492    ------------------------
1493    -- 6.4  Function Call --
1494    ------------------------
1495
1496    --  Parsed by P_Call_Or_Name (4.1)
1497
1498    --------------------------------
1499    -- 6.4  Actual Parameter Part --
1500    --------------------------------
1501
1502    --  Parsed by P_Call_Or_Name (4.1)
1503
1504    --------------------------------
1505    -- 6.4  Parameter Association --
1506    --------------------------------
1507
1508    --  Parsed by P_Call_Or_Name (4.1)
1509
1510    ------------------------------------
1511    -- 6.4  Explicit Actual Parameter --
1512    ------------------------------------
1513
1514    --  Parsed by P_Call_Or_Name (4.1)
1515
1516    ---------------------------
1517    -- 6.5  Return Statement --
1518    ---------------------------
1519
1520    --  SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1521    --
1522    --  EXTENDED_RETURN_STATEMENT ::=
1523    --    return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1524    --                                           [:= EXPRESSION] [do
1525    --      HANDLED_SEQUENCE_OF_STATEMENTS
1526    --    end return];
1527    --
1528    --  RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1529
1530    --  RETURN_STATEMENT ::= return [EXPRESSION];
1531
1532    --  Error recovery: can raise Error_Resync
1533
1534    procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1535
1536       --  Note: We don't need to check Ada_Version here, because this is
1537       --  only called in >= Ada 2005 cases anyway.
1538
1539       Not_Null_Present : constant Boolean := P_Null_Exclusion;
1540
1541    begin
1542       Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1543
1544       if Token = Tok_Access then
1545          Set_Object_Definition
1546            (Decl_Node, P_Access_Definition (Not_Null_Present));
1547       else
1548          Set_Object_Definition
1549            (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1550       end if;
1551    end P_Return_Subtype_Indication;
1552
1553    --  Error recovery: can raise Error_Resync
1554
1555    function P_Return_Object_Declaration return Node_Id is
1556       Return_Obj : Node_Id;
1557       Decl_Node  : Node_Id;
1558
1559    begin
1560       Return_Obj := Token_Node;
1561       Change_Identifier_To_Defining_Identifier (Return_Obj);
1562       Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1563       Set_Defining_Identifier (Decl_Node, Return_Obj);
1564
1565       Scan; -- past identifier
1566       Scan; -- past :
1567
1568       --  First an error check, if we have two identifiers in a row, a likely
1569       --  possibility is that the first of the identifiers is an incorrectly
1570       --  spelled keyword. See similar check in P_Identifier_Declarations.
1571
1572       if Token = Tok_Identifier then
1573          declare
1574             SS : Saved_Scan_State;
1575             I2 : Boolean;
1576
1577          begin
1578             Save_Scan_State (SS);
1579             Scan; -- past initial identifier
1580             I2 := (Token = Tok_Identifier);
1581             Restore_Scan_State (SS);
1582
1583             if I2
1584               and then
1585                 (Bad_Spelling_Of (Tok_Access)   or else
1586                  Bad_Spelling_Of (Tok_Aliased)  or else
1587                  Bad_Spelling_Of (Tok_Constant))
1588             then
1589                null;
1590             end if;
1591          end;
1592       end if;
1593
1594       --  We allow "constant" here (as in "return Result : constant
1595       --  T..."). This is not in the latest RM, but the ARG is considering an
1596       --  AI on the subject (see AI05-0015-1), which we expect to be approved.
1597
1598       if Token = Tok_Constant then
1599          Scan; -- past CONSTANT
1600          Set_Constant_Present (Decl_Node);
1601
1602          if Token = Tok_Aliased then
1603             Error_Msg_SC -- CODEFIX
1604               ("ALIASED should be before CONSTANT");
1605             Scan; -- past ALIASED
1606             Set_Aliased_Present (Decl_Node);
1607          end if;
1608
1609       elsif Token = Tok_Aliased then
1610          Scan; -- past ALIASED
1611          Set_Aliased_Present (Decl_Node);
1612
1613          if Token = Tok_Constant then
1614             Scan; -- past CONSTANT
1615             Set_Constant_Present (Decl_Node);
1616          end if;
1617       end if;
1618
1619       P_Return_Subtype_Indication (Decl_Node);
1620
1621       if Token = Tok_Colon_Equal then
1622          Scan; -- past :=
1623          Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1624       end if;
1625
1626       return Decl_Node;
1627    end P_Return_Object_Declaration;
1628
1629    --  Error recovery: can raise Error_Resync
1630
1631    function P_Return_Statement return Node_Id is
1632       --  The caller has checked that the initial token is RETURN
1633
1634       function Is_Simple return Boolean;
1635       --  Scan state is just after RETURN (and is left that way).
1636       --  Determine whether this is a simple or extended return statement
1637       --  by looking ahead for "identifier :", which implies extended.
1638
1639       ---------------
1640       -- Is_Simple --
1641       ---------------
1642
1643       function Is_Simple return Boolean is
1644          Scan_State : Saved_Scan_State;
1645          Result     : Boolean := True;
1646
1647       begin
1648          if Token = Tok_Identifier then
1649             Save_Scan_State (Scan_State); -- at identifier
1650             Scan; -- past identifier
1651
1652             if Token = Tok_Colon then
1653                Result := False; -- It's an extended_return_statement.
1654             end if;
1655
1656             Restore_Scan_State (Scan_State); -- to identifier
1657          end if;
1658
1659          return Result;
1660       end Is_Simple;
1661
1662       Return_Sloc : constant Source_Ptr := Token_Ptr;
1663       Return_Node : Node_Id;
1664
1665    --  Start of processing for P_Return_Statement
1666
1667    begin
1668       Scan; -- past RETURN
1669
1670       --  Simple_return_statement, no expression, return an
1671       --  N_Simple_Return_Statement node with the expression field left Empty.
1672
1673       if Token = Tok_Semicolon then
1674          Scan; -- past ;
1675          Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1676
1677       --  Non-trivial case
1678
1679       else
1680          --  Simple_return_statement with expression
1681
1682          --  We avoid trying to scan an expression if we are at an
1683          --  expression terminator since in that case the best error
1684          --  message is probably that we have a missing semicolon.
1685
1686          if Is_Simple then
1687             Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1688
1689             if Token not in Token_Class_Eterm then
1690                Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1691             end if;
1692
1693          --  Extended_return_statement (Ada 2005 only -- AI-318):
1694
1695          else
1696             if Ada_Version < Ada_2005 then
1697                Error_Msg_SP
1698                  (" extended_return_statement is an Ada 2005 extension");
1699                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1700             end if;
1701
1702             Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1703             Set_Return_Object_Declarations
1704               (Return_Node, New_List (P_Return_Object_Declaration));
1705
1706             if Token = Tok_Do then
1707                Push_Scope_Stack;
1708                Scope.Table (Scope.Last).Etyp := E_Return;
1709                Scope.Table (Scope.Last).Ecol := Start_Column;
1710                Scope.Table (Scope.Last).Sloc := Return_Sloc;
1711
1712                Scan; -- past DO
1713                Set_Handled_Statement_Sequence
1714                  (Return_Node, P_Handled_Sequence_Of_Statements);
1715                End_Statements;
1716
1717                --  Do we need to handle Error_Resync here???
1718             end if;
1719          end if;
1720
1721          TF_Semicolon;
1722       end if;
1723
1724       return Return_Node;
1725    end P_Return_Statement;
1726
1727 end Ch6;