OSDN Git Service

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