OSDN Git Service

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