[gcc/testsuite]
[official-gcc.git] / gcc / ada / par-ch6.adb
blob83bb25118a406eafd883f912da013a6d23b51d15
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
30 with Sinfo.CN; use Sinfo.CN;
32 separate (Par)
33 package body Ch6 is
35 -- Local subprograms, used only in this chapter
37 function P_Defining_Designator return Node_Id;
38 function P_Defining_Operator_Symbol return Node_Id;
39 function P_Return_Object_Declaration return Node_Id;
41 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id);
42 -- Decl_Node is a N_Object_Declaration. Set the Null_Exclusion_Present and
43 -- Object_Definition fields of Decl_Node.
45 procedure Check_Junk_Semicolon_Before_Return;
46 -- Check for common error of junk semicolon before RETURN keyword of
47 -- function specification. If present, skip over it with appropriate error
48 -- message, leaving Scan_Ptr pointing to the RETURN after. This routine
49 -- also deals with a possibly misspelled version of Return.
51 procedure No_Constraint_Maybe_Expr_Func;
52 -- Called after scanning return subtype to check for missing constraint,
53 -- taking into account the possibility of an occurrence of an expression
54 -- function where the IS has been forgotten.
56 ----------------------------------------
57 -- Check_Junk_Semicolon_Before_Return --
58 ----------------------------------------
60 procedure Check_Junk_Semicolon_Before_Return is
61 Scan_State : Saved_Scan_State;
63 begin
64 if Token = Tok_Semicolon then
65 Save_Scan_State (Scan_State);
66 Scan; -- past the semicolon
68 if Token = Tok_Return then
69 Restore_Scan_State (Scan_State);
70 Error_Msg_SC -- CODEFIX
71 ("|extra "";"" ignored");
72 Scan; -- rescan past junk semicolon
73 else
74 Restore_Scan_State (Scan_State);
75 end if;
76 end if;
77 end Check_Junk_Semicolon_Before_Return;
79 -----------------------------------
80 -- No_Constraint_Maybe_Expr_Func --
81 -----------------------------------
83 procedure No_Constraint_Maybe_Expr_Func is
84 begin
85 -- If we have a left paren at the start of the line, then assume this is
86 -- the case of an expression function with missing IS. We do not have to
87 -- diagnose the missing IS, that is done elsewhere. We do this game in
88 -- Ada 2012 mode where expression functions are legal.
90 if Token = Tok_Left_Paren
91 and Ada_Version >= Ada_2012
92 and Token_Is_At_Start_Of_Line
93 then
94 -- One exception if we have "(token .." then this is a constraint
96 declare
97 Scan_State : Saved_Scan_State;
99 begin
100 Save_Scan_State (Scan_State);
101 Scan; -- past left paren
102 Scan; -- past following token
104 -- If we have "(token .." then restore scan state and treat as
105 -- unexpected constraint.
107 if Token = Tok_Dot_Dot then
108 Restore_Scan_State (Scan_State);
109 No_Constraint;
111 -- Otherwise we treat this as an expression function
113 else
114 Restore_Scan_State (Scan_State);
115 end if;
116 end;
118 -- Otherwise use standard routine to check for no constraint present
120 else
121 No_Constraint;
122 end if;
123 end No_Constraint_Maybe_Expr_Func;
125 -----------------------------------------------------
126 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
127 -----------------------------------------------------
129 -- This routine scans out a subprogram declaration, subprogram body,
130 -- subprogram renaming declaration or subprogram generic instantiation.
131 -- It also handles the new Ada 2012 expression function form
133 -- SUBPROGRAM_DECLARATION ::=
134 -- SUBPROGRAM_SPECIFICATION
135 -- [ASPECT_SPECIFICATIONS];
137 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
138 -- SUBPROGRAM_SPECIFICATION is abstract
139 -- [ASPECT_SPECIFICATIONS];
141 -- SUBPROGRAM_SPECIFICATION ::=
142 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
143 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
145 -- PARAMETER_PROFILE ::= [FORMAL_PART]
147 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
149 -- SUBPROGRAM_BODY ::=
150 -- SUBPROGRAM_SPECIFICATION [ASPECT_SPECIFICATIONS] is
151 -- DECLARATIVE_PART
152 -- begin
153 -- HANDLED_SEQUENCE_OF_STATEMENTS
154 -- end [DESIGNATOR];
156 -- SUBPROGRAM_RENAMING_DECLARATION ::=
157 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME
158 -- [ASPECT_SPECIFICATIONS];
160 -- SUBPROGRAM_BODY_STUB ::=
161 -- SUBPROGRAM_SPECIFICATION is separate
162 -- [ASPECT_SPECIFICATIONS];
164 -- GENERIC_INSTANTIATION ::=
165 -- procedure DEFINING_PROGRAM_UNIT_NAME is
166 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART]
167 -- [ASPECT_SPECIFICATIONS];
168 -- | function DEFINING_DESIGNATOR is
169 -- new generic_function_NAME [GENERIC_ACTUAL_PART]
170 -- [ASPECT_SPECIFICATIONS];
172 -- NULL_PROCEDURE_DECLARATION ::=
173 -- SUBPROGRAM_SPECIFICATION is null;
175 -- Null procedures are an Ada 2005 feature. A null procedure declaration
176 -- is classified as a basic declarative item, but it is parsed here, with
177 -- other subprogram constructs.
179 -- EXPRESSION_FUNCTION ::=
180 -- FUNCTION SPECIFICATION IS (EXPRESSION)
181 -- [ASPECT_SPECIFICATIONS];
183 -- The value in Pf_Flags indicates which of these possible declarations
184 -- is acceptable to the caller:
186 -- Pf_Flags.Decl Set if declaration OK
187 -- Pf_Flags.Gins Set if generic instantiation OK
188 -- Pf_Flags.Pbod Set if proper body OK
189 -- Pf_Flags.Rnam Set if renaming declaration OK
190 -- Pf_Flags.Stub Set if body stub OK
191 -- Pf_Flags.Pexp Set if expression function OK
193 -- If an inappropriate form is encountered, it is scanned out but an
194 -- error message indicating that it is appearing in an inappropriate
195 -- context is issued. The only possible values for Pf_Flags are those
196 -- defined as constants in the Par package.
198 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
199 -- NOT or OVERRIDING.
201 -- Error recovery: cannot raise Error_Resync
203 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
204 Specification_Node : Node_Id;
205 Name_Node : Node_Id;
206 Aspects : List_Id;
207 Fpart_List : List_Id;
208 Fpart_Sloc : Source_Ptr;
209 Result_Not_Null : Boolean := False;
210 Result_Node : Node_Id;
211 Inst_Node : Node_Id;
212 Body_Node : Node_Id;
213 Decl_Node : Node_Id;
214 Rename_Node : Node_Id;
215 Absdec_Node : Node_Id;
216 Stub_Node : Node_Id;
217 Fproc_Sloc : Source_Ptr;
218 Func : Boolean;
219 Scan_State : Saved_Scan_State;
221 -- Flags for optional overriding indication. Two flags are needed,
222 -- to distinguish positive and negative overriding indicators from
223 -- the absence of any indicator.
225 Is_Overriding : Boolean := False;
226 Not_Overriding : Boolean := False;
228 begin
229 -- Set up scope stack entry. Note that the Labl field will be set later
231 SIS_Entry_Active := False;
232 SIS_Missing_Semicolon_Message := No_Error_Msg;
233 Push_Scope_Stack;
234 Scope.Table (Scope.Last).Sloc := Token_Ptr;
235 Scope.Table (Scope.Last).Etyp := E_Name;
236 Scope.Table (Scope.Last).Ecol := Start_Column;
237 Scope.Table (Scope.Last).Lreq := False;
239 Aspects := Empty_List;
241 -- Ada 2005: Scan leading NOT OVERRIDING indicator
243 if Token = Tok_Not then
244 Scan; -- past NOT
246 if Token = Tok_Overriding then
247 Scan; -- past OVERRIDING
248 Not_Overriding := True;
250 -- Overriding keyword used in non Ada 2005 mode
252 elsif Token = Tok_Identifier
253 and then Token_Name = Name_Overriding
254 then
255 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
256 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
257 Scan; -- past Overriding
258 Not_Overriding := True;
260 else
261 Error_Msg_SC -- CODEFIX
262 ("OVERRIDING expected!");
263 end if;
265 -- Ada 2005: scan leading OVERRIDING indicator
267 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
268 -- declaration circuit already gave an error message and changed the
269 -- token to Tok_Overriding.
271 elsif Token = Tok_Overriding then
272 Scan; -- past OVERRIDING
273 Is_Overriding := True;
274 end if;
276 if Is_Overriding or else Not_Overriding then
278 -- Note that if we are not in Ada_2005 mode, error messages have
279 -- already been given, so no need to give another message here.
281 -- An overriding indicator is allowed for subprogram declarations,
282 -- bodies (including subunits), renamings, stubs, and instantiations.
283 -- The test against Pf_Decl_Pbod is added to account for the case of
284 -- subprograms declared in a protected type, where only subprogram
285 -- declarations and bodies can occur. The Pf_Pbod case is for
286 -- subunits.
288 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
289 and then
290 Pf_Flags /= Pf_Decl_Pbod_Pexp
291 and then
292 Pf_Flags /= Pf_Pbod_Pexp
293 then
294 Error_Msg_SC ("overriding indicator not allowed here!");
296 elsif Token /= Tok_Function and then Token /= Tok_Procedure then
297 Error_Msg_SC -- CODEFIX
298 ("FUNCTION or PROCEDURE expected!");
299 end if;
300 end if;
302 Func := (Token = Tok_Function);
303 Fproc_Sloc := Token_Ptr;
304 Scan; -- past FUNCTION or PROCEDURE
305 Ignore (Tok_Type);
306 Ignore (Tok_Body);
308 if Func then
309 Name_Node := P_Defining_Designator;
311 if Nkind (Name_Node) = N_Defining_Operator_Symbol
312 and then Scope.Last = 1
313 then
314 Error_Msg_SP ("operator symbol not allowed at library level");
315 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
317 -- Set name from file name, we need some junk name, and that's
318 -- as good as anything. This is only approximate, since we do
319 -- not do anything with non-standard name translations.
321 Get_Name_String (File_Name (Current_Source_File));
323 for J in 1 .. Name_Len loop
324 if Name_Buffer (J) = '.' then
325 Name_Len := J - 1;
326 exit;
327 end if;
328 end loop;
330 Set_Chars (Name_Node, Name_Find);
331 Set_Error_Posted (Name_Node);
332 end if;
334 else
335 Name_Node := P_Defining_Program_Unit_Name;
336 end if;
338 Scope.Table (Scope.Last).Labl := Name_Node;
339 Ignore (Tok_Colon);
341 -- Deal with generic instantiation, the one case in which we do not
342 -- have a subprogram specification as part of whatever we are parsing
344 if Token = Tok_Is then
345 Save_Scan_State (Scan_State); -- at the IS
346 T_Is; -- checks for redundant IS
348 if Token = Tok_New then
349 if not Pf_Flags.Gins then
350 Error_Msg_SC ("generic instantiation not allowed here!");
351 end if;
353 Scan; -- past NEW
355 if Func then
356 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
357 Set_Name (Inst_Node, P_Function_Name);
358 else
359 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
360 Set_Name (Inst_Node, P_Qualified_Simple_Name);
361 end if;
363 Set_Defining_Unit_Name (Inst_Node, Name_Node);
364 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
365 P_Aspect_Specifications (Inst_Node);
366 Pop_Scope_Stack; -- Don't need scope stack entry in this case
368 if Is_Overriding then
369 Set_Must_Override (Inst_Node);
371 elsif Not_Overriding then
372 Set_Must_Not_Override (Inst_Node);
373 end if;
375 return Inst_Node;
377 else
378 Restore_Scan_State (Scan_State); -- to the IS
379 end if;
380 end if;
382 -- If not a generic instantiation, then we definitely have a subprogram
383 -- specification (all possibilities at this stage include one here)
385 Fpart_Sloc := Token_Ptr;
387 Check_Misspelling_Of (Tok_Return);
389 -- Scan formal part. First a special error check. If we have an
390 -- identifier here, then we have a definite error. If this identifier
391 -- is on the same line as the designator, then we assume it is the
392 -- first formal after a missing left parenthesis
394 if Token = Tok_Identifier
395 and then not Token_Is_At_Start_Of_Line
396 then
397 T_Left_Paren; -- to generate message
398 Fpart_List := P_Formal_Part;
400 -- Otherwise scan out an optional formal part in the usual manner
402 else
403 Fpart_List := P_Parameter_Profile;
404 end if;
406 -- We treat what we have as a function specification if FUNCTION was
407 -- used, or if a RETURN is present. This gives better error recovery
408 -- since later RETURN statements will be valid in either case.
410 Check_Junk_Semicolon_Before_Return;
411 Result_Node := Error;
413 if Token = Tok_Return then
414 if not Func then
415 Error_Msg -- CODEFIX
416 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
417 Func := True;
418 end if;
420 Scan; -- past RETURN
422 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
424 -- Ada 2005 (AI-318-02)
426 if Token = Tok_Access then
427 if Ada_Version < Ada_2005 then
428 Error_Msg_SC
429 ("anonymous access result type is an Ada 2005 extension");
430 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
431 end if;
433 Result_Node := P_Access_Definition (Result_Not_Null);
435 else
436 Result_Node := P_Subtype_Mark;
437 No_Constraint_Maybe_Expr_Func;
438 end if;
440 else
441 -- Skip extra parenthesis at end of formal part
443 Ignore (Tok_Right_Paren);
445 -- For function, scan result subtype
447 if Func then
448 TF_Return;
450 if Prev_Token = Tok_Return then
451 Result_Node := P_Subtype_Mark;
452 end if;
453 end if;
454 end if;
456 if Func then
457 Specification_Node :=
458 New_Node (N_Function_Specification, Fproc_Sloc);
460 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
461 Set_Result_Definition (Specification_Node, Result_Node);
463 else
464 Specification_Node :=
465 New_Node (N_Procedure_Specification, Fproc_Sloc);
466 end if;
468 Set_Defining_Unit_Name (Specification_Node, Name_Node);
469 Set_Parameter_Specifications (Specification_Node, Fpart_List);
471 if Is_Overriding then
472 Set_Must_Override (Specification_Node);
474 elsif Not_Overriding then
475 Set_Must_Not_Override (Specification_Node);
476 end if;
478 -- Error check: barriers not allowed on protected functions/procedures
480 if Token = Tok_When then
481 if Func then
482 Error_Msg_SC ("barrier not allowed on function, only on entry");
483 else
484 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
485 end if;
487 Scan; -- past WHEN
488 Discard_Junk_Node (P_Expression);
489 end if;
491 -- Deal with semicolon followed by IS. We want to treat this as IS
493 if Token = Tok_Semicolon then
494 Save_Scan_State (Scan_State);
495 Scan; -- past semicolon
497 if Token = Tok_Is then
498 Error_Msg_SP -- CODEFIX
499 ("extra "";"" ignored");
500 else
501 Restore_Scan_State (Scan_State);
502 end if;
503 end if;
505 -- Subprogram declaration ended by aspect specifications
507 if Aspect_Specifications_Present then
508 goto Subprogram_Declaration;
510 -- Deal with case of semicolon ending a subprogram declaration
512 elsif Token = Tok_Semicolon then
513 if not Pf_Flags.Decl then
514 T_Is;
515 end if;
517 Save_Scan_State (Scan_State);
518 Scan; -- past semicolon
520 -- If semicolon is immediately followed by IS, then ignore the
521 -- semicolon, and go process the body.
523 if Token = Tok_Is then
524 Error_Msg_SP -- CODEFIX
525 ("|extra "";"" ignored");
526 T_Is; -- scan past IS
527 goto Subprogram_Body;
529 -- If BEGIN follows in an appropriate column, we immediately
530 -- commence the error action of assuming that the previous
531 -- subprogram declaration should have been a subprogram body,
532 -- i.e. that the terminating semicolon should have been IS.
534 elsif Token = Tok_Begin
535 and then Start_Column >= Scope.Table (Scope.Last).Ecol
536 then
537 Error_Msg_SP -- CODEFIX
538 ("|"";"" should be IS!");
539 goto Subprogram_Body;
541 else
542 Restore_Scan_State (Scan_State);
543 goto Subprogram_Declaration;
544 end if;
546 -- Case of not followed by semicolon
548 else
549 -- Subprogram renaming declaration case
551 Check_Misspelling_Of (Tok_Renames);
553 if Token = Tok_Renames then
554 if not Pf_Flags.Rnam then
555 Error_Msg_SC ("renaming declaration not allowed here!");
556 end if;
558 Rename_Node :=
559 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
560 Scan; -- past RENAMES
561 Set_Name (Rename_Node, P_Name);
562 Set_Specification (Rename_Node, Specification_Node);
563 P_Aspect_Specifications (Rename_Node);
564 TF_Semicolon;
565 Pop_Scope_Stack;
566 return Rename_Node;
568 -- Case of IS following subprogram specification
570 elsif Token = Tok_Is then
571 T_Is; -- ignore redundant Is's
573 if Token_Name = Name_Abstract then
574 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
575 end if;
577 -- Deal nicely with (now obsolete) use of <> in place of abstract
579 if Token = Tok_Box then
580 Error_Msg_SC -- CODEFIX
581 ("ABSTRACT expected");
582 Token := Tok_Abstract;
583 end if;
585 -- Abstract subprogram declaration case
587 if Token = Tok_Abstract then
588 Absdec_Node :=
589 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
590 Set_Specification (Absdec_Node, Specification_Node);
591 Pop_Scope_Stack; -- discard unneeded entry
592 Scan; -- past ABSTRACT
593 P_Aspect_Specifications (Absdec_Node);
594 return Absdec_Node;
596 -- Ada 2005 (AI-248): Parse a null procedure declaration
598 elsif Token = Tok_Null then
599 if Ada_Version < Ada_2005 then
600 Error_Msg_SP ("null procedures are an Ada 2005 extension");
601 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
602 end if;
604 Scan; -- past NULL
606 if Func then
607 Error_Msg_SP ("only procedures can be null");
608 else
609 Set_Null_Present (Specification_Node);
610 Set_Null_Statement (Specification_Node,
611 New_Node (N_Null_Statement, Prev_Token_Ptr));
612 end if;
614 goto Subprogram_Declaration;
616 -- Check for IS NEW with Formal_Part present and handle nicely
618 elsif Token = Tok_New then
619 Error_Msg
620 ("formal part not allowed in instantiation", Fpart_Sloc);
621 Scan; -- past NEW
623 if Func then
624 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
625 else
626 Inst_Node :=
627 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
628 end if;
630 Set_Defining_Unit_Name (Inst_Node, Name_Node);
631 Set_Name (Inst_Node, P_Name);
632 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
633 TF_Semicolon;
634 Pop_Scope_Stack; -- Don't need scope stack entry in this case
635 return Inst_Node;
637 else
638 goto Subprogram_Body;
639 end if;
641 -- Aspect specifications present
643 elsif Aspect_Specifications_Present then
644 goto Subprogram_Declaration;
646 -- Here we have a missing IS or missing semicolon
648 else
649 -- If the next token is a left paren at the start of a line, then
650 -- this is almost certainly the start of the expression for an
651 -- expression function, so in this case guess a missing IS.
653 if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then
654 Error_Msg_AP -- CODEFIX
655 ("missing IS");
657 -- In all other cases, we guess a missing semicolon, since we are
658 -- good at fixing up a semicolon which should really be an IS.
660 else
661 Error_Msg_AP -- CODEFIX
662 ("|missing "";""");
663 SIS_Missing_Semicolon_Message := Get_Msg_Id;
664 goto Subprogram_Declaration;
665 end if;
666 end if;
667 end if;
669 -- Processing for stub or subprogram body or expression function
671 <<Subprogram_Body>>
673 -- Subprogram body stub case
675 if Separate_Present then
676 if not Pf_Flags.Stub then
677 Error_Msg_SC ("body stub not allowed here!");
678 end if;
680 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
681 Error_Msg
682 ("operator symbol cannot be used as subunit name",
683 Sloc (Name_Node));
684 end if;
686 Scan; -- past SEPARATE
688 Stub_Node :=
689 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
690 Set_Specification (Stub_Node, Specification_Node);
692 if Is_Non_Empty_List (Aspects) then
693 Error_Msg
694 ("aspect specifications must come after SEPARATE",
695 Sloc (First (Aspects)));
696 end if;
698 P_Aspect_Specifications (Stub_Node, Semicolon => False);
699 TF_Semicolon;
700 Pop_Scope_Stack;
701 return Stub_Node;
703 -- Subprogram body or expression function case
705 else
706 Scan_Body_Or_Expression_Function : declare
708 Body_Is_Hidden_In_SPARK : Boolean;
709 Hidden_Region_Start : Source_Ptr;
711 function Likely_Expression_Function return Boolean;
712 -- Returns True if we have a probable case of an expression
713 -- function omitting the parentheses, if so, returns True
714 -- and emits an appropriate error message, else returns False.
716 --------------------------------
717 -- Likely_Expression_Function --
718 --------------------------------
720 function Likely_Expression_Function return Boolean is
721 begin
722 -- If currently pointing to BEGIN or a declaration keyword
723 -- or a pragma, then we definitely have a subprogram body.
724 -- This is a common case, so worth testing first.
726 if Token = Tok_Begin
727 or else Token in Token_Class_Declk
728 or else Token = Tok_Pragma
729 then
730 return False;
732 -- Test for tokens which could only start an expression and
733 -- thus signal the case of a expression function.
735 elsif Token in Token_Class_Literal
736 or else Token in Token_Class_Unary_Addop
737 or else Token = Tok_Left_Paren
738 or else Token = Tok_Abs
739 or else Token = Tok_Null
740 or else Token = Tok_New
741 or else Token = Tok_Not
742 then
743 null;
745 -- Anything other than an identifier must be a body
747 elsif Token /= Tok_Identifier then
748 return False;
750 -- Here for an identifier
752 else
753 -- If the identifier is the first token on its line, then
754 -- let's assume that we have a missing begin and this is
755 -- intended as a subprogram body. However, if the context
756 -- is a function and the unit is a package declaration, a
757 -- body would be illegal, so try for an unparenthesized
758 -- expression function.
760 if Token_Is_At_Start_Of_Line then
761 declare
762 -- The enclosing scope entry is a subprogram spec
764 Spec_Node : constant Node_Id :=
765 Parent
766 (Scope.Table (Scope.Last).Labl);
767 Lib_Node : Node_Id := Spec_Node;
769 begin
770 -- Check whether there is an enclosing scope that
771 -- is a package declaration.
773 if Scope.Last > 1 then
774 Lib_Node :=
775 Parent (Scope.Table (Scope.Last - 1).Labl);
776 end if;
778 if Ada_Version >= Ada_2012
779 and then
780 Nkind (Lib_Node) = N_Package_Specification
781 and then
782 Nkind (Spec_Node) = N_Function_Specification
783 then
784 null;
785 else
786 return False;
787 end if;
788 end;
790 -- Otherwise we have to scan ahead. If the identifier is
791 -- followed by a colon or a comma, it is a declaration
792 -- and hence we have a subprogram body. Otherwise assume
793 -- a expression function.
795 else
796 declare
797 Scan_State : Saved_Scan_State;
798 Tok : Token_Type;
800 begin
801 Save_Scan_State (Scan_State);
802 Scan; -- past identifier
803 Tok := Token;
804 Restore_Scan_State (Scan_State);
806 if Tok = Tok_Colon or else Tok = Tok_Comma then
807 return False;
808 end if;
809 end;
810 end if;
811 end if;
813 -- Fall through if we have a likely expression function.
814 -- If the starting keyword is not "function" the error
815 -- will be reported elsewhere.
817 if Func then
818 Error_Msg_SC
819 ("expression function must be enclosed in parentheses");
820 end if;
822 return True;
823 end Likely_Expression_Function;
825 -- Start of processing for Scan_Body_Or_Expression_Function
827 begin
828 -- Expression_Function case
830 if Token = Tok_Left_Paren
831 or else Likely_Expression_Function
832 then
833 -- Check expression function allowed here
835 if not Pf_Flags.Pexp then
836 Error_Msg_SC ("expression function not allowed here!");
837 end if;
839 -- Check we are in Ada 2012 mode
841 Error_Msg_Ada_2012_Feature
842 ("!expression function", Token_Ptr);
844 -- Catch an illegal placement of the aspect specification
845 -- list:
847 -- function_specification
848 -- [aspect_specification] is (expression);
850 -- This case is correctly processed by the parser because
851 -- the expression function first appears as a subprogram
852 -- declaration to the parser. The starting keyword may
853 -- not have been "function" in which case the error is
854 -- on a malformed procedure.
856 if Is_Non_Empty_List (Aspects) then
857 if Func then
858 Error_Msg
859 ("aspect specifications must come after "
860 & "parenthesized expression",
861 Sloc (First (Aspects)));
862 else
863 Error_Msg
864 ("aspect specifications must come after subprogram "
865 & "specification", Sloc (First (Aspects)));
866 end if;
867 end if;
869 -- Parse out expression and build expression function
871 Body_Node :=
872 New_Node
873 (N_Expression_Function, Sloc (Specification_Node));
874 Set_Specification (Body_Node, Specification_Node);
875 Set_Expression (Body_Node, P_Expression);
877 -- Expression functions can carry pre/postconditions
879 P_Aspect_Specifications (Body_Node);
880 Pop_Scope_Stack;
882 -- Subprogram body case
884 else
885 -- Check body allowed here
887 if not Pf_Flags.Pbod then
888 Error_Msg_SP ("subprogram body not allowed here!");
889 end if;
891 -- Here is the test for a suspicious IS (i.e. one that
892 -- looks like it might more properly be a semicolon).
893 -- See separate section describing use of IS instead
894 -- of semicolon in package Parse.
896 if (Token in Token_Class_Declk
897 or else
898 Token = Tok_Identifier)
899 and then Start_Column <= Scope.Table (Scope.Last).Ecol
900 and then Scope.Last /= 1
901 then
902 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
903 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
904 end if;
906 -- Build and return subprogram body, parsing declarations
907 -- and statement sequence that belong to the body.
909 Body_Node :=
910 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
911 Set_Specification (Body_Node, Specification_Node);
913 -- If aspects are present, the specification is parsed as
914 -- a subprogram declaration, and we jump here after seeing
915 -- the keyword IS. Attach asspects previously collected to
916 -- the body.
918 if Is_Non_Empty_List (Aspects) then
919 Set_Parent (Aspects, Body_Node);
920 Set_Aspect_Specifications (Body_Node, Aspects);
921 end if;
923 -- In SPARK, a HIDE directive can be placed at the beginning
924 -- of a subprogram implementation, thus hiding the
925 -- subprogram body from SPARK tool-set. No violation of the
926 -- SPARK restriction should be issued on nodes in a hidden
927 -- part, which is obtained by marking such hidden parts.
929 if Token = Tok_SPARK_Hide then
930 Body_Is_Hidden_In_SPARK := True;
931 Hidden_Region_Start := Token_Ptr;
932 Scan; -- past HIDE directive
933 else
934 Body_Is_Hidden_In_SPARK := False;
935 end if;
937 Parse_Decls_Begin_End (Body_Node);
939 if Body_Is_Hidden_In_SPARK then
940 Set_Hidden_Part_In_SPARK (Hidden_Region_Start, Token_Ptr);
941 end if;
942 end if;
944 return Body_Node;
945 end Scan_Body_Or_Expression_Function;
946 end if;
948 -- Processing for subprogram declaration
950 <<Subprogram_Declaration>>
951 Decl_Node :=
952 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
953 Set_Specification (Decl_Node, Specification_Node);
954 Aspects := Get_Aspect_Specifications (Semicolon => False);
956 -- Aspects may be present on a subprogram body. The source parsed
957 -- so far is that of its specification. Go parse the body and attach
958 -- the collected aspects, if any, to the body.
960 if Token = Tok_Is then
961 Scan;
962 goto Subprogram_Body;
964 else
965 if Is_Non_Empty_List (Aspects) then
966 Set_Parent (Aspects, Decl_Node);
967 Set_Aspect_Specifications (Decl_Node, Aspects);
968 end if;
970 TF_Semicolon;
971 end if;
973 -- If this is a context in which a subprogram body is permitted,
974 -- set active SIS entry in case (see section titled "Handling
975 -- Semicolon Used in Place of IS" in body of Parser package)
976 -- Note that SIS_Missing_Semicolon_Message is already set properly.
978 if Pf_Flags.Pbod
980 -- Disconnnect this processing if we have scanned a null procedure
981 -- because in this case the spec is complete anyway with no body.
983 and then (Nkind (Specification_Node) /= N_Procedure_Specification
984 or else not Null_Present (Specification_Node))
985 then
986 SIS_Labl := Scope.Table (Scope.Last).Labl;
987 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
988 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
989 SIS_Declaration_Node := Decl_Node;
990 SIS_Semicolon_Sloc := Prev_Token_Ptr;
991 SIS_Entry_Active := True;
992 end if;
994 Pop_Scope_Stack;
995 return Decl_Node;
996 end P_Subprogram;
998 ---------------------------------
999 -- 6.1 Subprogram Declaration --
1000 ---------------------------------
1002 -- Parsed by P_Subprogram (6.1)
1004 ------------------------------------------
1005 -- 6.1 Abstract Subprogram Declaration --
1006 ------------------------------------------
1008 -- Parsed by P_Subprogram (6.1)
1010 -----------------------------------
1011 -- 6.1 Subprogram Specification --
1012 -----------------------------------
1014 -- SUBPROGRAM_SPECIFICATION ::=
1015 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1016 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1018 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1020 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1022 -- Subprogram specifications that appear in subprogram declarations
1023 -- are parsed by P_Subprogram (6.1). This routine is used in other
1024 -- contexts where subprogram specifications occur.
1026 -- Note: this routine does not affect the scope stack in any way
1028 -- Error recovery: can raise Error_Resync
1030 function P_Subprogram_Specification return Node_Id is
1031 Specification_Node : Node_Id;
1032 Result_Not_Null : Boolean;
1033 Result_Node : Node_Id;
1035 begin
1036 if Token = Tok_Function then
1037 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1038 Scan; -- past FUNCTION
1039 Ignore (Tok_Body);
1040 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1041 Set_Parameter_Specifications
1042 (Specification_Node, P_Parameter_Profile);
1043 Check_Junk_Semicolon_Before_Return;
1044 TF_Return;
1046 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1048 -- Ada 2005 (AI-318-02)
1050 if Token = Tok_Access then
1051 if Ada_Version < Ada_2005 then
1052 Error_Msg_SC
1053 ("anonymous access result type is an Ada 2005 extension");
1054 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
1055 end if;
1057 Result_Node := P_Access_Definition (Result_Not_Null);
1059 else
1060 Result_Node := P_Subtype_Mark;
1061 No_Constraint_Maybe_Expr_Func;
1062 end if;
1064 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1065 Set_Result_Definition (Specification_Node, Result_Node);
1066 return Specification_Node;
1068 elsif Token = Tok_Procedure then
1069 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1070 Scan; -- past PROCEDURE
1071 Ignore (Tok_Body);
1072 Set_Defining_Unit_Name
1073 (Specification_Node, P_Defining_Program_Unit_Name);
1074 Set_Parameter_Specifications
1075 (Specification_Node, P_Parameter_Profile);
1076 return Specification_Node;
1078 else
1079 Error_Msg_SC ("subprogram specification expected");
1080 raise Error_Resync;
1081 end if;
1082 end P_Subprogram_Specification;
1084 ---------------------
1085 -- 6.1 Designator --
1086 ---------------------
1088 -- DESIGNATOR ::=
1089 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1091 -- The caller has checked that the initial token is an identifier,
1092 -- operator symbol, or string literal. Note that we don't bother to
1093 -- do much error diagnosis in this routine, since it is only used for
1094 -- the label on END lines, and the routines in package Par.Endh will
1095 -- check that the label is appropriate.
1097 -- Error recovery: cannot raise Error_Resync
1099 function P_Designator return Node_Id is
1100 Ident_Node : Node_Id;
1101 Name_Node : Node_Id;
1102 Prefix_Node : Node_Id;
1104 function Real_Dot return Boolean;
1105 -- Tests if a current token is an interesting period, i.e. is followed
1106 -- by an identifier or operator symbol or string literal. If not, it is
1107 -- probably just incorrect punctuation to be caught by our caller. Note
1108 -- that the case of an operator symbol or string literal is also an
1109 -- error, but that is an error that we catch here. If the result is
1110 -- True, a real dot has been scanned and we are positioned past it,
1111 -- if the result is False, the scan position is unchanged.
1113 --------------
1114 -- Real_Dot --
1115 --------------
1117 function Real_Dot return Boolean is
1118 Scan_State : Saved_Scan_State;
1120 begin
1121 if Token /= Tok_Dot then
1122 return False;
1124 else
1125 Save_Scan_State (Scan_State);
1126 Scan; -- past dot
1128 if Token = Tok_Identifier
1129 or else Token = Tok_Operator_Symbol
1130 or else Token = Tok_String_Literal
1131 then
1132 return True;
1134 else
1135 Restore_Scan_State (Scan_State);
1136 return False;
1137 end if;
1138 end if;
1139 end Real_Dot;
1141 -- Start of processing for P_Designator
1143 begin
1144 Ident_Node := Token_Node;
1145 Scan; -- past initial token
1147 if Prev_Token = Tok_Operator_Symbol
1148 or else Prev_Token = Tok_String_Literal
1149 or else not Real_Dot
1150 then
1151 return Ident_Node;
1153 -- Child name case
1155 else
1156 Prefix_Node := Ident_Node;
1158 -- Loop through child names, on entry to this loop, Prefix contains
1159 -- the name scanned so far, and Ident_Node is the last identifier.
1161 loop
1162 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1163 Set_Prefix (Name_Node, Prefix_Node);
1164 Ident_Node := P_Identifier;
1165 Set_Selector_Name (Name_Node, Ident_Node);
1166 Prefix_Node := Name_Node;
1167 exit when not Real_Dot;
1168 end loop;
1170 -- On exit from the loop, Ident_Node is the last identifier scanned,
1171 -- i.e. the defining identifier, and Prefix_Node is a node for the
1172 -- entire name, structured (incorrectly) as a selected component.
1174 Name_Node := Prefix (Prefix_Node);
1175 Change_Node (Prefix_Node, N_Designator);
1176 Set_Name (Prefix_Node, Name_Node);
1177 Set_Identifier (Prefix_Node, Ident_Node);
1178 return Prefix_Node;
1179 end if;
1181 exception
1182 when Error_Resync =>
1183 while Token = Tok_Dot or else Token = Tok_Identifier loop
1184 Scan;
1185 end loop;
1187 return Error;
1188 end P_Designator;
1190 ------------------------------
1191 -- 6.1 Defining Designator --
1192 ------------------------------
1194 -- DEFINING_DESIGNATOR ::=
1195 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1197 -- Error recovery: cannot raise Error_Resync
1199 function P_Defining_Designator return Node_Id is
1200 begin
1201 if Token = Tok_Operator_Symbol then
1202 return P_Defining_Operator_Symbol;
1204 elsif Token = Tok_String_Literal then
1205 Error_Msg_SC ("invalid operator name");
1206 Scan; -- past junk string
1207 return Error;
1209 else
1210 return P_Defining_Program_Unit_Name;
1211 end if;
1212 end P_Defining_Designator;
1214 -------------------------------------
1215 -- 6.1 Defining Program Unit Name --
1216 -------------------------------------
1218 -- DEFINING_PROGRAM_UNIT_NAME ::=
1219 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1221 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1223 -- Error recovery: cannot raise Error_Resync
1225 function P_Defining_Program_Unit_Name return Node_Id is
1226 Ident_Node : Node_Id;
1227 Name_Node : Node_Id;
1228 Prefix_Node : Node_Id;
1230 begin
1231 -- Set identifier casing if not already set and scan initial identifier
1233 if Token = Tok_Identifier
1234 and then Identifier_Casing (Current_Source_File) = Unknown
1235 then
1236 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1237 end if;
1239 Ident_Node := P_Identifier (C_Dot);
1240 Merge_Identifier (Ident_Node, Tok_Return);
1242 -- Normal case (not child library unit name)
1244 if Token /= Tok_Dot then
1245 Change_Identifier_To_Defining_Identifier (Ident_Node);
1246 Warn_If_Standard_Redefinition (Ident_Node);
1247 return Ident_Node;
1249 -- Child library unit name case
1251 else
1252 if Scope.Last > 1 then
1253 Error_Msg_SP ("child unit allowed only at library level");
1254 raise Error_Resync;
1256 elsif Ada_Version = Ada_83 then
1257 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1259 end if;
1261 Prefix_Node := Ident_Node;
1263 -- Loop through child names, on entry to this loop, Prefix contains
1264 -- the name scanned so far, and Ident_Node is the last identifier.
1266 loop
1267 exit when Token /= Tok_Dot;
1268 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1269 Scan; -- past period
1270 Set_Prefix (Name_Node, Prefix_Node);
1271 Ident_Node := P_Identifier (C_Dot);
1272 Set_Selector_Name (Name_Node, Ident_Node);
1273 Prefix_Node := Name_Node;
1274 end loop;
1276 -- On exit from the loop, Ident_Node is the last identifier scanned,
1277 -- i.e. the defining identifier, and Prefix_Node is a node for the
1278 -- entire name, structured (incorrectly) as a selected component.
1280 Name_Node := Prefix (Prefix_Node);
1281 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1282 Set_Name (Prefix_Node, Name_Node);
1283 Change_Identifier_To_Defining_Identifier (Ident_Node);
1284 Warn_If_Standard_Redefinition (Ident_Node);
1285 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1287 -- All set with unit name parsed
1289 return Prefix_Node;
1290 end if;
1292 exception
1293 when Error_Resync =>
1294 while Token = Tok_Dot or else Token = Tok_Identifier loop
1295 Scan;
1296 end loop;
1298 return Error;
1299 end P_Defining_Program_Unit_Name;
1301 --------------------------
1302 -- 6.1 Operator Symbol --
1303 --------------------------
1305 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1307 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1309 -----------------------------------
1310 -- 6.1 Defining Operator Symbol --
1311 -----------------------------------
1313 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1315 -- The caller has checked that the initial symbol is an operator symbol
1317 function P_Defining_Operator_Symbol return Node_Id is
1318 Op_Node : Node_Id;
1320 begin
1321 Op_Node := Token_Node;
1322 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1323 Scan; -- past operator symbol
1324 return Op_Node;
1325 end P_Defining_Operator_Symbol;
1327 ----------------------------
1328 -- 6.1 Parameter_Profile --
1329 ----------------------------
1331 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1333 -- Empty is returned if no formal part is present
1335 -- Error recovery: cannot raise Error_Resync
1337 function P_Parameter_Profile return List_Id is
1338 begin
1339 if Token = Tok_Left_Paren then
1340 Scan; -- part left paren
1341 return P_Formal_Part;
1342 else
1343 return No_List;
1344 end if;
1345 end P_Parameter_Profile;
1347 ---------------------------------------
1348 -- 6.1 Parameter And Result Profile --
1349 ---------------------------------------
1351 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1352 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1354 ----------------------
1355 -- 6.1 Formal part --
1356 ----------------------
1358 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1360 -- PARAMETER_SPECIFICATION ::=
1361 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1362 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1363 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1364 -- [:= DEFAULT_EXPRESSION]
1366 -- This scans the construct Formal_Part. The caller has already checked
1367 -- that the initial token is a left parenthesis, and skipped past it, so
1368 -- that on entry Token is the first token following the left parenthesis.
1370 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1372 -- Error recovery: cannot raise Error_Resync
1374 function P_Formal_Part return List_Id is
1375 Specification_List : List_Id;
1376 Specification_Node : Node_Id;
1377 Scan_State : Saved_Scan_State;
1378 Num_Idents : Nat;
1379 Ident : Nat;
1380 Ident_Sloc : Source_Ptr;
1381 Not_Null_Present : Boolean := False;
1382 Not_Null_Sloc : Source_Ptr;
1384 Idents : array (Int range 1 .. 4096) of Entity_Id;
1385 -- This array holds the list of defining identifiers. The upper bound
1386 -- of 4096 is intended to be essentially infinite, and we do not even
1387 -- bother to check for it being exceeded.
1389 begin
1390 Specification_List := New_List;
1391 Specification_Loop : loop
1392 begin
1393 if Token = Tok_Pragma then
1394 Error_Msg_SC ("pragma not allowed in formal part");
1395 Discard_Junk_Node (P_Pragma (Skipping => True));
1396 end if;
1398 Ignore (Tok_Left_Paren);
1399 Ident_Sloc := Token_Ptr;
1400 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1401 Num_Idents := 1;
1403 Ident_Loop : loop
1404 exit Ident_Loop when Token = Tok_Colon;
1406 -- The only valid tokens are colon and comma, so if we have
1407 -- neither do a bit of investigation to see which is the
1408 -- better choice for insertion.
1410 if Token /= Tok_Comma then
1412 -- Assume colon if ALIASED, IN or OUT keyword found
1414 exit Ident_Loop when Token = Tok_Aliased or else
1415 Token = Tok_In or else
1416 Token = Tok_Out;
1418 -- Otherwise scan ahead
1420 Save_Scan_State (Scan_State);
1421 Look_Ahead : loop
1423 -- If we run into a semicolon, then assume that a
1424 -- colon was missing, e.g. Parms (X Y; ...). Also
1425 -- assume missing colon on EOF (a real disaster)
1426 -- and on a right paren, e.g. Parms (X Y), and also
1427 -- on an assignment symbol, e.g. Parms (X Y := ..)
1429 if Token = Tok_Semicolon
1430 or else Token = Tok_Right_Paren
1431 or else Token = Tok_EOF
1432 or else Token = Tok_Colon_Equal
1433 then
1434 Restore_Scan_State (Scan_State);
1435 exit Ident_Loop;
1437 -- If we run into a colon, assume that we had a missing
1438 -- comma, e.g. Parms (A B : ...). Also assume a missing
1439 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1441 elsif Token = Tok_Colon
1442 or else Token = Tok_Comma
1443 then
1444 Restore_Scan_State (Scan_State);
1445 exit Look_Ahead;
1446 end if;
1448 Scan;
1449 end loop Look_Ahead;
1450 end if;
1452 -- Here if a comma is present, or to be assumed
1454 T_Comma;
1455 Num_Idents := Num_Idents + 1;
1456 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1457 end loop Ident_Loop;
1459 -- Fall through the loop on encountering a colon, or deciding
1460 -- that there is a missing colon.
1462 T_Colon;
1464 -- If there are multiple identifiers, we repeatedly scan the
1465 -- type and initialization expression information by resetting
1466 -- the scan pointer (so that we get completely separate trees
1467 -- for each occurrence).
1469 if Num_Idents > 1 then
1470 Save_Scan_State (Scan_State);
1471 end if;
1473 -- Loop through defining identifiers in list
1475 Ident := 1;
1477 Ident_List_Loop : loop
1478 Specification_Node :=
1479 New_Node (N_Parameter_Specification, Ident_Sloc);
1480 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1482 -- Scan possible ALIASED for Ada 2012 (AI-142)
1484 if Token = Tok_Aliased then
1485 if Ada_Version < Ada_2012 then
1486 Error_Msg_Ada_2012_Feature
1487 ("ALIASED parameter", Token_Ptr);
1488 else
1489 Set_Aliased_Present (Specification_Node);
1490 end if;
1492 Scan; -- past ALIASED
1493 end if;
1495 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1497 Not_Null_Sloc := Token_Ptr;
1498 Not_Null_Present :=
1499 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1501 -- Case of ACCESS keyword present
1503 if Token = Tok_Access then
1504 Set_Null_Exclusion_Present
1505 (Specification_Node, Not_Null_Present);
1507 if Ada_Version = Ada_83 then
1508 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1509 end if;
1511 Set_Parameter_Type
1512 (Specification_Node,
1513 P_Access_Definition (Not_Null_Present));
1515 -- Case of IN or OUT present
1517 else
1518 if Token = Tok_In or else Token = Tok_Out then
1519 if Not_Null_Present then
1520 Error_Msg
1521 ("`NOT NULL` can only be used with `ACCESS`",
1522 Not_Null_Sloc);
1524 if Token = Tok_In then
1525 Error_Msg
1526 ("\`IN` not allowed together with `ACCESS`",
1527 Not_Null_Sloc);
1528 else
1529 Error_Msg
1530 ("\`OUT` not allowed together with `ACCESS`",
1531 Not_Null_Sloc);
1532 end if;
1533 end if;
1535 P_Mode (Specification_Node);
1536 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1537 end if;
1539 Set_Null_Exclusion_Present
1540 (Specification_Node, Not_Null_Present);
1542 if Token = Tok_Procedure
1543 or else
1544 Token = Tok_Function
1545 then
1546 Error_Msg_SC ("formal subprogram parameter not allowed");
1547 Scan;
1549 if Token = Tok_Left_Paren then
1550 Discard_Junk_List (P_Formal_Part);
1551 end if;
1553 if Token = Tok_Return then
1554 Scan;
1555 Discard_Junk_Node (P_Subtype_Mark);
1556 end if;
1558 Set_Parameter_Type (Specification_Node, Error);
1560 else
1561 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1562 No_Constraint;
1563 end if;
1564 end if;
1566 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1568 if Ident > 1 then
1569 Set_Prev_Ids (Specification_Node, True);
1570 end if;
1572 if Ident < Num_Idents then
1573 Set_More_Ids (Specification_Node, True);
1574 end if;
1576 Append (Specification_Node, Specification_List);
1577 exit Ident_List_Loop when Ident = Num_Idents;
1578 Ident := Ident + 1;
1579 Restore_Scan_State (Scan_State);
1580 end loop Ident_List_Loop;
1582 exception
1583 when Error_Resync =>
1584 Resync_Semicolon_List;
1585 end;
1587 if Token = Tok_Semicolon then
1588 Save_Scan_State (Scan_State);
1589 Scan; -- past semicolon
1591 -- If we have RETURN or IS after the semicolon, then assume
1592 -- that semicolon should have been a right parenthesis and exit
1594 if Token = Tok_Is or else Token = Tok_Return then
1595 Error_Msg_SP -- CODEFIX
1596 ("|"";"" should be "")""");
1597 exit Specification_Loop;
1598 end if;
1600 -- If we have a declaration keyword after the semicolon, then
1601 -- assume we had a missing right parenthesis and terminate list
1603 if Token in Token_Class_Declk then
1604 Error_Msg_AP -- CODEFIX
1605 ("missing "")""");
1606 Restore_Scan_State (Scan_State);
1607 exit Specification_Loop;
1608 end if;
1610 elsif Token = Tok_Right_Paren then
1611 Scan; -- past right paren
1612 exit Specification_Loop;
1614 -- Special check for common error of using comma instead of semicolon
1616 elsif Token = Tok_Comma then
1617 T_Semicolon;
1618 Scan; -- past comma
1620 -- Special check for omitted separator
1622 elsif Token = Tok_Identifier then
1623 T_Semicolon;
1625 -- If nothing sensible, skip to next semicolon or right paren
1627 else
1628 T_Semicolon;
1629 Resync_Semicolon_List;
1631 if Token = Tok_Semicolon then
1632 Scan; -- past semicolon
1633 else
1634 T_Right_Paren;
1635 exit Specification_Loop;
1636 end if;
1637 end if;
1638 end loop Specification_Loop;
1640 return Specification_List;
1641 end P_Formal_Part;
1643 ----------------------------------
1644 -- 6.1 Parameter Specification --
1645 ----------------------------------
1647 -- Parsed by P_Formal_Part (6.1)
1649 ---------------
1650 -- 6.1 Mode --
1651 ---------------
1653 -- MODE ::= [in] | in out | out
1655 -- There is no explicit node in the tree for the Mode. Instead the
1656 -- In_Present and Out_Present flags are set in the parent node to
1657 -- record the presence of keywords specifying the mode.
1659 -- Error_Recovery: cannot raise Error_Resync
1661 procedure P_Mode (Node : Node_Id) is
1662 begin
1663 if Token = Tok_In then
1664 Scan; -- past IN
1665 Set_In_Present (Node, True);
1667 if Style.Mode_In_Check and then Token /= Tok_Out then
1668 Error_Msg_SP -- CODEFIX
1669 ("(style) IN should be omitted");
1670 end if;
1672 -- Since Ada 2005, formal objects can have an anonymous access type,
1673 -- and of course carry a mode indicator.
1675 if Token = Tok_Access
1676 and then Nkind (Node) /= N_Formal_Object_Declaration
1677 then
1678 Error_Msg_SP ("IN not allowed together with ACCESS");
1679 Scan; -- past ACCESS
1680 end if;
1681 end if;
1683 if Token = Tok_Out then
1684 Scan; -- past OUT
1685 Set_Out_Present (Node, True);
1686 end if;
1688 if Token = Tok_In then
1689 Error_Msg_SC ("IN must precede OUT in parameter mode");
1690 Scan; -- past IN
1691 Set_In_Present (Node, True);
1692 end if;
1693 end P_Mode;
1695 --------------------------
1696 -- 6.3 Subprogram Body --
1697 --------------------------
1699 -- Parsed by P_Subprogram (6.1)
1701 -----------------------------------
1702 -- 6.4 Procedure Call Statement --
1703 -----------------------------------
1705 -- Parsed by P_Sequence_Of_Statements (5.1)
1707 ------------------------
1708 -- 6.4 Function Call --
1709 ------------------------
1711 -- Parsed by P_Name (4.1)
1713 --------------------------------
1714 -- 6.4 Actual Parameter Part --
1715 --------------------------------
1717 -- Parsed by P_Name (4.1)
1719 --------------------------------
1720 -- 6.4 Parameter Association --
1721 --------------------------------
1723 -- Parsed by P_Name (4.1)
1725 ------------------------------------
1726 -- 6.4 Explicit Actual Parameter --
1727 ------------------------------------
1729 -- Parsed by P_Name (4.1)
1731 ---------------------------
1732 -- 6.5 Return Statement --
1733 ---------------------------
1735 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1737 -- EXTENDED_RETURN_STATEMENT ::=
1738 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1739 -- [:= EXPRESSION] [do
1740 -- HANDLED_SEQUENCE_OF_STATEMENTS
1741 -- end return];
1743 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1745 -- RETURN_STATEMENT ::= return [EXPRESSION];
1747 -- Error recovery: can raise Error_Resync
1749 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1751 -- Note: We don't need to check Ada_Version here, because this is
1752 -- only called in >= Ada 2005 cases anyway.
1754 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1756 begin
1757 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1759 if Token = Tok_Access then
1760 Set_Object_Definition
1761 (Decl_Node, P_Access_Definition (Not_Null_Present));
1762 else
1763 Set_Object_Definition
1764 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1765 end if;
1766 end P_Return_Subtype_Indication;
1768 -- Error recovery: can raise Error_Resync
1770 function P_Return_Object_Declaration return Node_Id is
1771 Return_Obj : Node_Id;
1772 Decl_Node : Node_Id;
1774 begin
1775 Return_Obj := Token_Node;
1776 Change_Identifier_To_Defining_Identifier (Return_Obj);
1777 Warn_If_Standard_Redefinition (Return_Obj);
1778 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1779 Set_Defining_Identifier (Decl_Node, Return_Obj);
1781 Scan; -- past identifier
1782 Scan; -- past :
1784 -- First an error check, if we have two identifiers in a row, a likely
1785 -- possibility is that the first of the identifiers is an incorrectly
1786 -- spelled keyword. See similar check in P_Identifier_Declarations.
1788 if Token = Tok_Identifier then
1789 declare
1790 SS : Saved_Scan_State;
1791 I2 : Boolean;
1793 begin
1794 Save_Scan_State (SS);
1795 Scan; -- past initial identifier
1796 I2 := (Token = Tok_Identifier);
1797 Restore_Scan_State (SS);
1799 if I2
1800 and then
1801 (Bad_Spelling_Of (Tok_Access) or else
1802 Bad_Spelling_Of (Tok_Aliased) or else
1803 Bad_Spelling_Of (Tok_Constant))
1804 then
1805 null;
1806 end if;
1807 end;
1808 end if;
1810 -- We allow "constant" here (as in "return Result : constant
1811 -- T..."). This is not in the latest RM, but the ARG is considering an
1812 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1814 if Token = Tok_Constant then
1815 Scan; -- past CONSTANT
1816 Set_Constant_Present (Decl_Node);
1818 if Token = Tok_Aliased then
1819 Error_Msg_SC -- CODEFIX
1820 ("ALIASED should be before CONSTANT");
1821 Scan; -- past ALIASED
1822 Set_Aliased_Present (Decl_Node);
1823 end if;
1825 elsif Token = Tok_Aliased then
1826 Scan; -- past ALIASED
1827 Set_Aliased_Present (Decl_Node);
1829 -- The restrictions on the use of aliased in an extended return
1830 -- are semantic, not syntactic.
1832 if Token = Tok_Constant then
1833 Scan; -- past CONSTANT
1834 Set_Constant_Present (Decl_Node);
1835 end if;
1836 end if;
1838 P_Return_Subtype_Indication (Decl_Node);
1840 if Token = Tok_Colon_Equal then
1841 Scan; -- past :=
1842 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1843 end if;
1845 return Decl_Node;
1846 end P_Return_Object_Declaration;
1848 -- Error recovery: can raise Error_Resync
1850 function P_Return_Statement return Node_Id is
1851 -- The caller has checked that the initial token is RETURN
1853 function Is_Simple return Boolean;
1854 -- Scan state is just after RETURN (and is left that way). Determine
1855 -- whether this is a simple or extended return statement by looking
1856 -- ahead for "identifier :", which implies extended.
1858 ---------------
1859 -- Is_Simple --
1860 ---------------
1862 function Is_Simple return Boolean is
1863 Scan_State : Saved_Scan_State;
1864 Result : Boolean := True;
1866 begin
1867 if Token = Tok_Identifier then
1868 Save_Scan_State (Scan_State); -- at identifier
1869 Scan; -- past identifier
1871 if Token = Tok_Colon then
1872 Result := False; -- It's an extended_return_statement.
1873 end if;
1875 Restore_Scan_State (Scan_State); -- to identifier
1876 end if;
1878 return Result;
1879 end Is_Simple;
1881 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1882 Ret_Strt : constant Column_Number := Start_Column;
1883 Ret_Node : Node_Id;
1885 -- Start of processing for P_Return_Statement
1887 begin
1888 Scan; -- past RETURN
1890 -- Simple_return_statement, no expression, return an
1891 -- N_Simple_Return_Statement node with the expression field left Empty.
1893 if Token = Tok_Semicolon then
1894 Scan; -- past ;
1895 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1897 -- Nontrivial case
1899 else
1900 -- Simple_return_statement with expression
1902 -- We avoid trying to scan an expression if we are at an
1903 -- expression terminator since in that case the best error
1904 -- message is probably that we have a missing semicolon.
1906 if Is_Simple then
1907 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1909 if Token not in Token_Class_Eterm then
1910 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1911 end if;
1913 -- Extended_return_statement (Ada 2005 only -- AI-318):
1915 else
1916 if Ada_Version < Ada_2005 then
1917 Error_Msg_SP
1918 (" extended_return_statement is an Ada 2005 extension");
1919 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1920 end if;
1922 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1923 Set_Return_Object_Declarations
1924 (Ret_Node, New_List (P_Return_Object_Declaration));
1926 if Token = Tok_Do then
1927 Push_Scope_Stack;
1928 Scope.Table (Scope.Last).Ecol := Ret_Strt;
1929 Scope.Table (Scope.Last).Etyp := E_Return;
1930 Scope.Table (Scope.Last).Labl := Error;
1931 Scope.Table (Scope.Last).Sloc := Ret_Sloc;
1933 Scan; -- past DO
1934 Set_Handled_Statement_Sequence
1935 (Ret_Node, P_Handled_Sequence_Of_Statements);
1936 End_Statements;
1938 -- Do we need to handle Error_Resync here???
1939 end if;
1940 end if;
1942 TF_Semicolon;
1943 end if;
1945 return Ret_Node;
1946 end P_Return_Statement;
1948 end Ch6;