OSDN Git Service

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