OSDN Git Service

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