OSDN Git Service

2007-08-31 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch6.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 6                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 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       Not_Null_Sloc      : Source_Ptr;
993
994       Idents : array (Int range 1 .. 4096) of Entity_Id;
995       --  This array holds the list of defining identifiers. The upper bound
996       --  of 4096 is intended to be essentially infinite, and we do not even
997       --  bother to check for it being exceeded.
998
999    begin
1000       Specification_List := New_List;
1001       Specification_Loop : loop
1002          begin
1003             if Token = Tok_Pragma then
1004                P_Pragmas_Misplaced;
1005             end if;
1006
1007             Ignore (Tok_Left_Paren);
1008             Ident_Sloc := Token_Ptr;
1009             Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1010             Num_Idents := 1;
1011
1012             Ident_Loop : loop
1013                exit Ident_Loop when Token = Tok_Colon;
1014
1015                --  The only valid tokens are colon and comma, so if we have
1016                --  neither do a bit of investigation to see which is the
1017                --  better choice for insertion.
1018
1019                if Token /= Tok_Comma then
1020
1021                   --  Assume colon if IN or OUT keyword found
1022
1023                   exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1024
1025                   --  Otherwise scan ahead
1026
1027                   Save_Scan_State (Scan_State);
1028                   Look_Ahead : loop
1029
1030                      --  If we run into a semicolon, then assume that a
1031                      --  colon was missing, e.g.  Parms (X Y; ...). Also
1032                      --  assume missing colon on EOF (a real disaster!)
1033                      --  and on a right paren, e.g. Parms (X Y), and also
1034                      --  on an assignment symbol, e.g. Parms (X Y := ..)
1035
1036                      if Token = Tok_Semicolon
1037                        or else Token = Tok_Right_Paren
1038                        or else Token = Tok_EOF
1039                        or else Token = Tok_Colon_Equal
1040                      then
1041                         Restore_Scan_State (Scan_State);
1042                         exit Ident_Loop;
1043
1044                      --  If we run into a colon, assume that we had a missing
1045                      --  comma, e.g. Parms (A B : ...). Also assume a missing
1046                      --  comma if we hit another comma, e.g. Parms (A B, C ..)
1047
1048                      elsif Token = Tok_Colon
1049                        or else Token = Tok_Comma
1050                      then
1051                         Restore_Scan_State (Scan_State);
1052                         exit Look_Ahead;
1053                      end if;
1054
1055                      Scan;
1056                   end loop Look_Ahead;
1057                end if;
1058
1059                --  Here if a comma is present, or to be assumed
1060
1061                T_Comma;
1062                Num_Idents := Num_Idents + 1;
1063                Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1064             end loop Ident_Loop;
1065
1066             --  Fall through the loop on encountering a colon, or deciding
1067             --  that there is a missing colon.
1068
1069             T_Colon;
1070
1071             --  If there are multiple identifiers, we repeatedly scan the
1072             --  type and initialization expression information by resetting
1073             --  the scan pointer (so that we get completely separate trees
1074             --  for each occurrence).
1075
1076             if Num_Idents > 1 then
1077                Save_Scan_State (Scan_State);
1078             end if;
1079
1080             --  Loop through defining identifiers in list
1081
1082             Ident := 1;
1083
1084             Ident_List_Loop : loop
1085                Specification_Node :=
1086                  New_Node (N_Parameter_Specification, Ident_Sloc);
1087                Set_Defining_Identifier (Specification_Node, Idents (Ident));
1088
1089                --  Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1090
1091                Not_Null_Sloc := Token_Ptr;
1092                Not_Null_Present :=
1093                  P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1094
1095                --  Case of ACCESS keyword present
1096
1097                if Token = Tok_Access then
1098                   Set_Null_Exclusion_Present
1099                     (Specification_Node, Not_Null_Present);
1100
1101                   if Ada_Version = Ada_83 then
1102                      Error_Msg_SC ("(Ada 83) access parameters not allowed");
1103                   end if;
1104
1105                   Set_Parameter_Type
1106                     (Specification_Node,
1107                      P_Access_Definition (Not_Null_Present));
1108
1109                --  Case of IN or OUT present
1110
1111                else
1112                   if Token = Tok_In or else Token = Tok_Out then
1113                      if Not_Null_Present then
1114                         Error_Msg
1115                           ("`NOT NULL` can only be used with `ACCESS`",
1116                            Not_Null_Sloc);
1117
1118                         if Token = Tok_In then
1119                            Error_Msg
1120                              ("\`IN` not allowed together with `ACCESS`",
1121                               Not_Null_Sloc);
1122                         else
1123                            Error_Msg
1124                              ("\`OUT` not allowed together with `ACCESS`",
1125                               Not_Null_Sloc);
1126                         end if;
1127                      end if;
1128
1129                      P_Mode (Specification_Node);
1130                      Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1131                   end if;
1132
1133                   Set_Null_Exclusion_Present
1134                     (Specification_Node, Not_Null_Present);
1135
1136                   if Token = Tok_Procedure
1137                        or else
1138                      Token = Tok_Function
1139                   then
1140                      Error_Msg_SC ("formal subprogram parameter not allowed");
1141                      Scan;
1142
1143                      if Token = Tok_Left_Paren then
1144                         Discard_Junk_List (P_Formal_Part);
1145                      end if;
1146
1147                      if Token = Tok_Return then
1148                         Scan;
1149                         Discard_Junk_Node (P_Subtype_Mark);
1150                      end if;
1151
1152                      Set_Parameter_Type (Specification_Node, Error);
1153
1154                   else
1155                      Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1156                      No_Constraint;
1157                   end if;
1158                end if;
1159
1160                Set_Expression (Specification_Node, Init_Expr_Opt (True));
1161
1162                if Ident > 1 then
1163                   Set_Prev_Ids (Specification_Node, True);
1164                end if;
1165
1166                if Ident < Num_Idents then
1167                   Set_More_Ids (Specification_Node, True);
1168                end if;
1169
1170                Append (Specification_Node, Specification_List);
1171                exit Ident_List_Loop when Ident = Num_Idents;
1172                Ident := Ident + 1;
1173                Restore_Scan_State (Scan_State);
1174             end loop Ident_List_Loop;
1175
1176          exception
1177             when Error_Resync =>
1178                Resync_Semicolon_List;
1179          end;
1180
1181          if Token = Tok_Semicolon then
1182             Save_Scan_State (Scan_State);
1183             Scan; -- past semicolon
1184
1185             --  If we have RETURN or IS after the semicolon, then assume
1186             --  that semicolon should have been a right parenthesis and exit
1187
1188             if Token = Tok_Is or else Token = Tok_Return then
1189                Error_Msg_SP ("expected "")"" in place of "";""");
1190                exit Specification_Loop;
1191             end if;
1192
1193             --  If we have a declaration keyword after the semicolon, then
1194             --  assume we had a missing right parenthesis and terminate list
1195
1196             if Token in Token_Class_Declk then
1197                Error_Msg_AP ("missing "")""");
1198                Restore_Scan_State (Scan_State);
1199                exit Specification_Loop;
1200             end if;
1201
1202          elsif Token = Tok_Right_Paren then
1203             Scan; -- past right paren
1204             exit Specification_Loop;
1205
1206          --  Special check for common error of using comma instead of semicolon
1207
1208          elsif Token = Tok_Comma then
1209             T_Semicolon;
1210             Scan; -- past comma
1211
1212          --  Special check for omitted separator
1213
1214          elsif Token = Tok_Identifier then
1215             T_Semicolon;
1216
1217          --  If nothing sensible, skip to next semicolon or right paren
1218
1219          else
1220             T_Semicolon;
1221             Resync_Semicolon_List;
1222
1223             if Token = Tok_Semicolon then
1224                Scan; -- past semicolon
1225             else
1226                T_Right_Paren;
1227                exit Specification_Loop;
1228             end if;
1229          end if;
1230       end loop Specification_Loop;
1231
1232       return Specification_List;
1233    end P_Formal_Part;
1234
1235    ----------------------------------
1236    -- 6.1  Parameter Specification --
1237    ----------------------------------
1238
1239    --  Parsed by P_Formal_Part (6.1)
1240
1241    ---------------
1242    -- 6.1  Mode --
1243    ---------------
1244
1245    --  MODE ::= [in] | in out | out
1246
1247    --  There is no explicit node in the tree for the Mode. Instead the
1248    --  In_Present and Out_Present flags are set in the parent node to
1249    --  record the presence of keywords specifying the mode.
1250
1251    --  Error_Recovery: cannot raise Error_Resync
1252
1253    procedure P_Mode (Node : Node_Id) is
1254    begin
1255       if Token = Tok_In then
1256          Scan; -- past IN
1257          Set_In_Present (Node, True);
1258
1259          if Style.Mode_In_Check and then Token /= Tok_Out then
1260             Error_Msg_SP ("(style) IN should be omitted");
1261          end if;
1262
1263          if Token = Tok_Access then
1264             Error_Msg_SP ("IN not allowed together with ACCESS");
1265             Scan; -- past ACCESS
1266          end if;
1267       end if;
1268
1269       if Token = Tok_Out then
1270          Scan; -- past OUT
1271          Set_Out_Present (Node, True);
1272       end if;
1273
1274       if Token = Tok_In then
1275          Error_Msg_SC ("IN must preceed OUT in parameter mode");
1276          Scan; -- past IN
1277          Set_In_Present (Node, True);
1278       end if;
1279    end P_Mode;
1280
1281    --------------------------
1282    -- 6.3  Subprogram Body --
1283    --------------------------
1284
1285    --  Parsed by P_Subprogram (6.1)
1286
1287    -----------------------------------
1288    -- 6.4  Procedure Call Statement --
1289    -----------------------------------
1290
1291    --  Parsed by P_Sequence_Of_Statements (5.1)
1292
1293    ------------------------
1294    -- 6.4  Function Call --
1295    ------------------------
1296
1297    --  Parsed by P_Call_Or_Name (4.1)
1298
1299    --------------------------------
1300    -- 6.4  Actual Parameter Part --
1301    --------------------------------
1302
1303    --  Parsed by P_Call_Or_Name (4.1)
1304
1305    --------------------------------
1306    -- 6.4  Parameter Association --
1307    --------------------------------
1308
1309    --  Parsed by P_Call_Or_Name (4.1)
1310
1311    ------------------------------------
1312    -- 6.4  Explicit Actual Parameter --
1313    ------------------------------------
1314
1315    --  Parsed by P_Call_Or_Name (4.1)
1316
1317    ---------------------------
1318    -- 6.5  Return Statement --
1319    ---------------------------
1320
1321    --  SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1322    --
1323    --  EXTENDED_RETURN_STATEMENT ::=
1324    --    return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1325    --                                           [:= EXPRESSION] [do
1326    --      HANDLED_SEQUENCE_OF_STATEMENTS
1327    --    end return];
1328    --
1329    --  RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1330
1331    --  RETURN_STATEMENT ::= return [EXPRESSION];
1332
1333    --  Error recovery: can raise Error_Resync
1334
1335    procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1336
1337       --  Note: We don't need to check Ada_Version here, because this is
1338       --  only called in >= Ada 2005 cases anyway.
1339
1340       Not_Null_Present : constant Boolean := P_Null_Exclusion;
1341
1342    begin
1343       Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1344
1345       if Token = Tok_Access then
1346          Set_Object_Definition
1347            (Decl_Node, P_Access_Definition (Not_Null_Present));
1348       else
1349          Set_Object_Definition
1350            (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1351       end if;
1352    end P_Return_Subtype_Indication;
1353
1354    --  Error recovery: can raise Error_Resync
1355
1356    function P_Return_Object_Declaration return Node_Id is
1357       Return_Obj : Node_Id;
1358       Decl_Node  : Node_Id;
1359
1360    begin
1361       Return_Obj := Token_Node;
1362       Change_Identifier_To_Defining_Identifier (Return_Obj);
1363       Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1364       Set_Defining_Identifier (Decl_Node, Return_Obj);
1365
1366       Scan; -- past identifier
1367       Scan; -- past :
1368
1369       --  First an error check, if we have two identifiers in a row, a likely
1370       --  possibility is that the first of the identifiers is an incorrectly
1371       --  spelled keyword. See similar check in P_Identifier_Declarations.
1372
1373       if Token = Tok_Identifier then
1374          declare
1375             SS : Saved_Scan_State;
1376             I2 : Boolean;
1377
1378          begin
1379             Save_Scan_State (SS);
1380             Scan; -- past initial identifier
1381             I2 := (Token = Tok_Identifier);
1382             Restore_Scan_State (SS);
1383
1384             if I2
1385               and then
1386                 (Bad_Spelling_Of (Tok_Access)   or else
1387                  Bad_Spelling_Of (Tok_Aliased)  or else
1388                  Bad_Spelling_Of (Tok_Constant))
1389             then
1390                null;
1391             end if;
1392          end;
1393       end if;
1394
1395       --  We allow "constant" here (as in "return Result : constant
1396       --  T..."). This is not in the latest RM, but the ARG is considering an
1397       --  AI on the subject (see AI05-0015-1), which we expect to be approved.
1398
1399       if Token = Tok_Constant then
1400          Scan; -- past CONSTANT
1401          Set_Constant_Present (Decl_Node);
1402
1403          if Token = Tok_Aliased then
1404             Error_Msg_SC ("ALIASED should be before CONSTANT");
1405             Scan; -- past ALIASED
1406             Set_Aliased_Present (Decl_Node);
1407          end if;
1408
1409       elsif Token = Tok_Aliased then
1410          Scan; -- past ALIASED
1411          Set_Aliased_Present (Decl_Node);
1412
1413          if Token = Tok_Constant then
1414             Scan; -- past CONSTANT
1415             Set_Constant_Present (Decl_Node);
1416          end if;
1417       end if;
1418
1419       P_Return_Subtype_Indication (Decl_Node);
1420
1421       if Token = Tok_Colon_Equal then
1422          Scan; -- past :=
1423          Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1424       end if;
1425
1426       return Decl_Node;
1427    end P_Return_Object_Declaration;
1428
1429    --  Error recovery: can raise Error_Resync
1430
1431    function P_Return_Statement return Node_Id is
1432       --  The caller has checked that the initial token is RETURN
1433
1434       function Is_Simple return Boolean;
1435       --  Scan state is just after RETURN (and is left that way).
1436       --  Determine whether this is a simple or extended return statement
1437       --  by looking ahead for "identifier :", which implies extended.
1438
1439       ---------------
1440       -- Is_Simple --
1441       ---------------
1442
1443       function Is_Simple return Boolean is
1444          Scan_State : Saved_Scan_State;
1445          Result     : Boolean := True;
1446
1447       begin
1448          if Token = Tok_Identifier then
1449             Save_Scan_State (Scan_State); -- at identifier
1450             Scan; -- past identifier
1451
1452             if Token = Tok_Colon then
1453                Result := False; -- It's an extended_return_statement.
1454             end if;
1455
1456             Restore_Scan_State (Scan_State); -- to identifier
1457          end if;
1458
1459          return Result;
1460       end Is_Simple;
1461
1462       Return_Sloc : constant Source_Ptr := Token_Ptr;
1463       Return_Node : Node_Id;
1464
1465    --  Start of processing for P_Return_Statement
1466
1467    begin
1468       Scan; -- past RETURN
1469
1470       --  Simple_return_statement, no expression, return an
1471       --  N_Simple_Return_Statement node with the expression field left Empty.
1472
1473       if Token = Tok_Semicolon then
1474          Scan; -- past ;
1475          Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1476
1477       --  Non-trivial case
1478
1479       else
1480          --  Simple_return_statement with expression
1481
1482          --  We avoid trying to scan an expression if we are at an
1483          --  expression terminator since in that case the best error
1484          --  message is probably that we have a missing semicolon.
1485
1486          if Is_Simple then
1487             Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1488
1489             if Token not in Token_Class_Eterm then
1490                Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1491             end if;
1492
1493          --  Extended_return_statement (Ada 2005 only -- AI-318):
1494
1495          else
1496             if Ada_Version < Ada_05 then
1497                Error_Msg_SP
1498                  (" extended_return_statement is an Ada 2005 extension");
1499                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1500             end if;
1501
1502             Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1503             Set_Return_Object_Declarations
1504               (Return_Node, New_List (P_Return_Object_Declaration));
1505
1506             if Token = Tok_Do then
1507                Push_Scope_Stack;
1508                Scope.Table (Scope.Last).Etyp := E_Return;
1509                Scope.Table (Scope.Last).Ecol := Start_Column;
1510                Scope.Table (Scope.Last).Sloc := Return_Sloc;
1511
1512                Scan; -- past DO
1513                Set_Handled_Statement_Sequence
1514                  (Return_Node, P_Handled_Sequence_Of_Statements);
1515                End_Statements;
1516
1517                --  Do we need to handle Error_Resync here???
1518             end if;
1519          end if;
1520
1521          TF_Semicolon;
1522       end if;
1523
1524       return Return_Node;
1525    end P_Return_Statement;
1526
1527 end Ch6;