OSDN Git Service

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