PR rtl-optimization/82913
[official-gcc.git] / gcc / ada / par-ch6.adb
blobddcedcae1304c281a1a7b10bf1374654d9b6f341
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 Current_Node := Name_Node;
340 Ignore (Tok_Colon);
342 -- Deal with generic instantiation, the one case in which we do not
343 -- have a subprogram specification as part of whatever we are parsing
345 if Token = Tok_Is then
346 Save_Scan_State (Scan_State); -- at the IS
347 T_Is; -- checks for redundant IS
349 if Token = Tok_New then
350 if not Pf_Flags.Gins then
351 Error_Msg_SC ("generic instantiation not allowed here!");
352 end if;
354 Scan; -- past NEW
356 if Func then
357 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
358 Set_Name (Inst_Node, P_Function_Name);
359 else
360 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
361 Set_Name (Inst_Node, P_Qualified_Simple_Name);
362 end if;
364 Set_Defining_Unit_Name (Inst_Node, Name_Node);
365 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
366 P_Aspect_Specifications (Inst_Node);
367 Pop_Scope_Stack; -- Don't need scope stack entry in this case
369 if Is_Overriding then
370 Set_Must_Override (Inst_Node);
372 elsif Not_Overriding then
373 Set_Must_Not_Override (Inst_Node);
374 end if;
376 return Inst_Node;
378 else
379 Restore_Scan_State (Scan_State); -- to the IS
380 end if;
381 end if;
383 -- If not a generic instantiation, then we definitely have a subprogram
384 -- specification (all possibilities at this stage include one here)
386 Fpart_Sloc := Token_Ptr;
388 Check_Misspelling_Of (Tok_Return);
390 -- Scan formal part. First a special error check. If we have an
391 -- identifier here, then we have a definite error. If this identifier
392 -- is on the same line as the designator, then we assume it is the
393 -- first formal after a missing left parenthesis
395 if Token = Tok_Identifier
396 and then not Token_Is_At_Start_Of_Line
397 then
398 T_Left_Paren; -- to generate message
399 Fpart_List := P_Formal_Part;
401 -- Otherwise scan out an optional formal part in the usual manner
403 else
404 Fpart_List := P_Parameter_Profile;
405 end if;
407 -- We treat what we have as a function specification if FUNCTION was
408 -- used, or if a RETURN is present. This gives better error recovery
409 -- since later RETURN statements will be valid in either case.
411 Check_Junk_Semicolon_Before_Return;
412 Result_Node := Error;
414 if Token = Tok_Return then
415 if not Func then
416 Error_Msg -- CODEFIX
417 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
418 Func := True;
419 end if;
421 Scan; -- past RETURN
423 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
425 -- Ada 2005 (AI-318-02)
427 if Token = Tok_Access then
428 if Ada_Version < Ada_2005 then
429 Error_Msg_SC
430 ("anonymous access result type is an Ada 2005 extension");
431 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
432 end if;
434 Result_Node := P_Access_Definition (Result_Not_Null);
436 else
437 Result_Node := P_Subtype_Mark;
438 No_Constraint_Maybe_Expr_Func;
439 end if;
441 else
442 -- Skip extra parenthesis at end of formal part
444 Ignore (Tok_Right_Paren);
446 -- For function, scan result subtype
448 if Func then
449 TF_Return;
451 if Prev_Token = Tok_Return then
452 Result_Node := P_Subtype_Mark;
453 end if;
454 end if;
455 end if;
457 if Func then
458 Specification_Node :=
459 New_Node (N_Function_Specification, Fproc_Sloc);
461 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
462 Set_Result_Definition (Specification_Node, Result_Node);
464 else
465 Specification_Node :=
466 New_Node (N_Procedure_Specification, Fproc_Sloc);
467 end if;
469 Set_Defining_Unit_Name (Specification_Node, Name_Node);
470 Set_Parameter_Specifications (Specification_Node, Fpart_List);
472 if Is_Overriding then
473 Set_Must_Override (Specification_Node);
475 elsif Not_Overriding then
476 Set_Must_Not_Override (Specification_Node);
477 end if;
479 -- Error check: barriers not allowed on protected functions/procedures
481 if Token = Tok_When then
482 if Func then
483 Error_Msg_SC ("barrier not allowed on function, only on entry");
484 else
485 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
486 end if;
488 Scan; -- past WHEN
489 Discard_Junk_Node (P_Expression);
490 end if;
492 -- Deal with semicolon followed by IS. We want to treat this as IS
494 if Token = Tok_Semicolon then
495 Save_Scan_State (Scan_State);
496 Scan; -- past semicolon
498 if Token = Tok_Is then
499 Error_Msg_SP -- CODEFIX
500 ("extra "";"" ignored");
501 else
502 Restore_Scan_State (Scan_State);
503 end if;
504 end if;
506 -- Subprogram declaration ended by aspect specifications
508 if Aspect_Specifications_Present then
509 goto Subprogram_Declaration;
511 -- Deal with case of semicolon ending a subprogram declaration
513 elsif Token = Tok_Semicolon then
514 if not Pf_Flags.Decl then
515 T_Is;
516 end if;
518 Save_Scan_State (Scan_State);
519 Scan; -- past semicolon
521 -- If semicolon is immediately followed by IS, then ignore the
522 -- semicolon, and go process the body.
524 if Token = Tok_Is then
525 Error_Msg_SP -- CODEFIX
526 ("|extra "";"" ignored");
527 T_Is; -- scan past IS
528 goto Subprogram_Body;
530 -- If BEGIN follows in an appropriate column, we immediately
531 -- commence the error action of assuming that the previous
532 -- subprogram declaration should have been a subprogram body,
533 -- i.e. that the terminating semicolon should have been IS.
535 elsif Token = Tok_Begin
536 and then Start_Column >= Scope.Table (Scope.Last).Ecol
537 then
538 Error_Msg_SP -- CODEFIX
539 ("|"";"" should be IS!");
540 goto Subprogram_Body;
542 else
543 Restore_Scan_State (Scan_State);
544 goto Subprogram_Declaration;
545 end if;
547 -- Case of not followed by semicolon
549 else
550 -- Subprogram renaming declaration case
552 Check_Misspelling_Of (Tok_Renames);
554 if Token = Tok_Renames then
555 if not Pf_Flags.Rnam then
556 Error_Msg_SC ("renaming declaration not allowed here!");
557 end if;
559 Rename_Node :=
560 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
561 Scan; -- past RENAMES
562 Set_Name (Rename_Node, P_Name);
563 Set_Specification (Rename_Node, Specification_Node);
564 P_Aspect_Specifications (Rename_Node);
565 TF_Semicolon;
566 Pop_Scope_Stack;
567 return Rename_Node;
569 -- Case of IS following subprogram specification
571 elsif Token = Tok_Is then
572 T_Is; -- ignore redundant Is's
574 if Token_Name = Name_Abstract then
575 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
576 end if;
578 -- Deal nicely with (now obsolete) use of <> in place of abstract
580 if Token = Tok_Box then
581 Error_Msg_SC -- CODEFIX
582 ("ABSTRACT expected");
583 Token := Tok_Abstract;
584 end if;
586 -- Abstract subprogram declaration case
588 if Token = Tok_Abstract then
589 Absdec_Node :=
590 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
591 Set_Specification (Absdec_Node, Specification_Node);
592 Pop_Scope_Stack; -- discard unneeded entry
593 Scan; -- past ABSTRACT
594 P_Aspect_Specifications (Absdec_Node);
595 return Absdec_Node;
597 -- Ada 2005 (AI-248): Parse a null procedure declaration
599 elsif Token = Tok_Null then
600 if Ada_Version < Ada_2005 then
601 Error_Msg_SP ("null procedures are an Ada 2005 extension");
602 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
603 end if;
605 Scan; -- past NULL
607 if Func then
608 Error_Msg_SP ("only procedures can be null");
609 else
610 Set_Null_Present (Specification_Node);
611 Set_Null_Statement (Specification_Node,
612 New_Node (N_Null_Statement, Prev_Token_Ptr));
613 end if;
615 goto Subprogram_Declaration;
617 -- Check for IS NEW with Formal_Part present and handle nicely
619 elsif Token = Tok_New then
620 Error_Msg
621 ("formal part not allowed in instantiation", Fpart_Sloc);
622 Scan; -- past NEW
624 if Func then
625 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
626 else
627 Inst_Node :=
628 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
629 end if;
631 Set_Defining_Unit_Name (Inst_Node, Name_Node);
632 Set_Name (Inst_Node, P_Name);
633 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
634 TF_Semicolon;
635 Pop_Scope_Stack; -- Don't need scope stack entry in this case
636 return Inst_Node;
638 else
639 goto Subprogram_Body;
640 end if;
642 -- Aspect specifications present
644 elsif Aspect_Specifications_Present then
645 goto Subprogram_Declaration;
647 -- Here we have a missing IS or missing semicolon
649 else
650 -- If the next token is a left paren at the start of a line, then
651 -- this is almost certainly the start of the expression for an
652 -- expression function, so in this case guess a missing IS.
654 if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then
655 Error_Msg_AP -- CODEFIX
656 ("missing IS");
658 -- In all other cases, we guess a missing semicolon, since we are
659 -- good at fixing up a semicolon which should really be an IS.
661 else
662 Error_Msg_AP -- CODEFIX
663 ("|missing "";""");
664 SIS_Missing_Semicolon_Message := Get_Msg_Id;
665 goto Subprogram_Declaration;
666 end if;
667 end if;
668 end if;
670 -- Processing for stub or subprogram body or expression function
672 <<Subprogram_Body>>
674 -- Subprogram body stub case
676 if Separate_Present then
677 if not Pf_Flags.Stub then
678 Error_Msg_SC ("body stub not allowed here!");
679 end if;
681 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
682 Error_Msg
683 ("operator symbol cannot be used as subunit name",
684 Sloc (Name_Node));
685 end if;
687 Scan; -- past SEPARATE
689 Stub_Node :=
690 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
691 Set_Specification (Stub_Node, Specification_Node);
693 if Is_Non_Empty_List (Aspects) then
694 Error_Msg
695 ("aspect specifications must come after SEPARATE",
696 Sloc (First (Aspects)));
697 end if;
699 P_Aspect_Specifications (Stub_Node, Semicolon => False);
700 TF_Semicolon;
701 Pop_Scope_Stack;
702 return Stub_Node;
704 -- Subprogram body or expression function case
706 else
707 Scan_Body_Or_Expression_Function : declare
709 Body_Is_Hidden_In_SPARK : Boolean;
710 Hidden_Region_Start : Source_Ptr;
712 function Likely_Expression_Function return Boolean;
713 -- Returns True if we have a probable case of an expression
714 -- function omitting the parentheses, if so, returns True
715 -- and emits an appropriate error message, else returns False.
717 --------------------------------
718 -- Likely_Expression_Function --
719 --------------------------------
721 function Likely_Expression_Function return Boolean is
722 begin
723 -- If currently pointing to BEGIN or a declaration keyword
724 -- or a pragma, then we definitely have a subprogram body.
725 -- This is a common case, so worth testing first.
727 if Token = Tok_Begin
728 or else Token in Token_Class_Declk
729 or else Token = Tok_Pragma
730 then
731 return False;
733 -- Test for tokens which could only start an expression and
734 -- thus signal the case of a expression function.
736 elsif Token in Token_Class_Literal
737 or else Token in Token_Class_Unary_Addop
738 or else Token = Tok_Left_Paren
739 or else Token = Tok_Abs
740 or else Token = Tok_Null
741 or else Token = Tok_New
742 or else Token = Tok_Not
743 then
744 null;
746 -- Anything other than an identifier must be a body
748 elsif Token /= Tok_Identifier then
749 return False;
751 -- Here for an identifier
753 else
754 -- If the identifier is the first token on its line, then
755 -- let's assume that we have a missing begin and this is
756 -- intended as a subprogram body. However, if the context
757 -- is a function and the unit is a package declaration, a
758 -- body would be illegal, so try for an unparenthesized
759 -- expression function.
761 if Token_Is_At_Start_Of_Line then
762 declare
763 -- The enclosing scope entry is a subprogram spec
765 Spec_Node : constant Node_Id :=
766 Parent
767 (Scope.Table (Scope.Last).Labl);
768 Lib_Node : Node_Id := Spec_Node;
770 begin
771 -- Check whether there is an enclosing scope that
772 -- is a package declaration.
774 if Scope.Last > 1 then
775 Lib_Node :=
776 Parent (Scope.Table (Scope.Last - 1).Labl);
777 end if;
779 if Ada_Version >= Ada_2012
780 and then
781 Nkind (Lib_Node) = N_Package_Specification
782 and then
783 Nkind (Spec_Node) = N_Function_Specification
784 then
785 null;
786 else
787 return False;
788 end if;
789 end;
791 -- Otherwise we have to scan ahead. If the identifier is
792 -- followed by a colon or a comma, it is a declaration
793 -- and hence we have a subprogram body. Otherwise assume
794 -- a expression function.
796 else
797 declare
798 Scan_State : Saved_Scan_State;
799 Tok : Token_Type;
801 begin
802 Save_Scan_State (Scan_State);
803 Scan; -- past identifier
804 Tok := Token;
805 Restore_Scan_State (Scan_State);
807 if Tok = Tok_Colon or else Tok = Tok_Comma then
808 return False;
809 end if;
810 end;
811 end if;
812 end if;
814 -- Fall through if we have a likely expression function.
815 -- If the starting keyword is not "function" the error
816 -- will be reported elsewhere.
818 if Func then
819 Error_Msg_SC
820 ("expression function must be enclosed in parentheses");
821 end if;
823 return True;
824 end Likely_Expression_Function;
826 -- Start of processing for Scan_Body_Or_Expression_Function
828 begin
829 -- Expression_Function case
831 if Token = Tok_Left_Paren
832 or else Likely_Expression_Function
833 then
834 -- Check expression function allowed here
836 if not Pf_Flags.Pexp then
837 Error_Msg_SC ("expression function not allowed here!");
838 end if;
840 -- Check we are in Ada 2012 mode
842 Error_Msg_Ada_2012_Feature
843 ("!expression function", Token_Ptr);
845 -- Catch an illegal placement of the aspect specification
846 -- list:
848 -- function_specification
849 -- [aspect_specification] is (expression);
851 -- This case is correctly processed by the parser because
852 -- the expression function first appears as a subprogram
853 -- declaration to the parser. The starting keyword may
854 -- not have been "function" in which case the error is
855 -- on a malformed procedure.
857 if Is_Non_Empty_List (Aspects) then
858 if Func then
859 Error_Msg
860 ("aspect specifications must come after "
861 & "parenthesized expression",
862 Sloc (First (Aspects)));
863 else
864 Error_Msg
865 ("aspect specifications must come after subprogram "
866 & "specification", Sloc (First (Aspects)));
867 end if;
868 end if;
870 -- Parse out expression and build expression function
872 Body_Node :=
873 New_Node
874 (N_Expression_Function, Sloc (Specification_Node));
875 Set_Specification (Body_Node, Specification_Node);
876 Set_Expression (Body_Node, P_Expression);
878 -- Expression functions can carry pre/postconditions
880 P_Aspect_Specifications (Body_Node);
881 Pop_Scope_Stack;
883 -- Subprogram body case
885 else
886 -- Check body allowed here
888 if not Pf_Flags.Pbod then
889 Error_Msg_SP ("subprogram body not allowed here!");
890 end if;
892 -- Here is the test for a suspicious IS (i.e. one that
893 -- looks like it might more properly be a semicolon).
894 -- See separate section describing use of IS instead
895 -- of semicolon in package Parse.
897 if (Token in Token_Class_Declk
898 or else
899 Token = Tok_Identifier)
900 and then Start_Column <= Scope.Table (Scope.Last).Ecol
901 and then Scope.Last /= 1
902 then
903 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
904 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
905 end if;
907 -- Build and return subprogram body, parsing declarations
908 -- and statement sequence that belong to the body.
910 Body_Node :=
911 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
912 Set_Specification (Body_Node, Specification_Node);
914 -- If aspects are present, the specification is parsed as
915 -- a subprogram declaration, and we jump here after seeing
916 -- the keyword IS. Attach asspects previously collected to
917 -- the body.
919 if Is_Non_Empty_List (Aspects) then
920 Set_Parent (Aspects, Body_Node);
921 Set_Aspect_Specifications (Body_Node, Aspects);
922 end if;
924 -- In SPARK, a HIDE directive can be placed at the beginning
925 -- of a subprogram implementation, thus hiding the
926 -- subprogram body from SPARK tool-set. No violation of the
927 -- SPARK restriction should be issued on nodes in a hidden
928 -- part, which is obtained by marking such hidden parts.
930 if Token = Tok_SPARK_Hide then
931 Body_Is_Hidden_In_SPARK := True;
932 Hidden_Region_Start := Token_Ptr;
933 Scan; -- past HIDE directive
934 else
935 Body_Is_Hidden_In_SPARK := False;
936 end if;
938 Parse_Decls_Begin_End (Body_Node);
940 if Body_Is_Hidden_In_SPARK then
941 Set_Hidden_Part_In_SPARK (Hidden_Region_Start, Token_Ptr);
942 end if;
943 end if;
945 return Body_Node;
946 end Scan_Body_Or_Expression_Function;
947 end if;
949 -- Processing for subprogram declaration
951 <<Subprogram_Declaration>>
952 Decl_Node :=
953 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
954 Set_Specification (Decl_Node, Specification_Node);
955 Aspects := Get_Aspect_Specifications (Semicolon => False);
957 -- Aspects may be present on a subprogram body. The source parsed
958 -- so far is that of its specification. Go parse the body and attach
959 -- the collected aspects, if any, to the body.
961 if Token = Tok_Is then
962 Scan;
963 goto Subprogram_Body;
965 else
966 if Is_Non_Empty_List (Aspects) then
967 Set_Parent (Aspects, Decl_Node);
968 Set_Aspect_Specifications (Decl_Node, Aspects);
969 end if;
971 TF_Semicolon;
972 end if;
974 -- If this is a context in which a subprogram body is permitted,
975 -- set active SIS entry in case (see section titled "Handling
976 -- Semicolon Used in Place of IS" in body of Parser package)
977 -- Note that SIS_Missing_Semicolon_Message is already set properly.
979 if Pf_Flags.Pbod
981 -- Disconnnect this processing if we have scanned a null procedure
982 -- because in this case the spec is complete anyway with no body.
984 and then (Nkind (Specification_Node) /= N_Procedure_Specification
985 or else not Null_Present (Specification_Node))
986 then
987 SIS_Labl := Scope.Table (Scope.Last).Labl;
988 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
989 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
990 SIS_Declaration_Node := Decl_Node;
991 SIS_Semicolon_Sloc := Prev_Token_Ptr;
992 SIS_Entry_Active := True;
993 end if;
995 Pop_Scope_Stack;
996 return Decl_Node;
997 end P_Subprogram;
999 ---------------------------------
1000 -- 6.1 Subprogram Declaration --
1001 ---------------------------------
1003 -- Parsed by P_Subprogram (6.1)
1005 ------------------------------------------
1006 -- 6.1 Abstract Subprogram Declaration --
1007 ------------------------------------------
1009 -- Parsed by P_Subprogram (6.1)
1011 -----------------------------------
1012 -- 6.1 Subprogram Specification --
1013 -----------------------------------
1015 -- SUBPROGRAM_SPECIFICATION ::=
1016 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1017 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1019 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1021 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1023 -- Subprogram specifications that appear in subprogram declarations
1024 -- are parsed by P_Subprogram (6.1). This routine is used in other
1025 -- contexts where subprogram specifications occur.
1027 -- Note: this routine does not affect the scope stack in any way
1029 -- Error recovery: can raise Error_Resync
1031 function P_Subprogram_Specification return Node_Id is
1032 Specification_Node : Node_Id;
1033 Result_Not_Null : Boolean;
1034 Result_Node : Node_Id;
1036 begin
1037 if Token = Tok_Function then
1038 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1039 Scan; -- past FUNCTION
1040 Ignore (Tok_Body);
1041 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1042 Set_Parameter_Specifications
1043 (Specification_Node, P_Parameter_Profile);
1044 Check_Junk_Semicolon_Before_Return;
1045 TF_Return;
1047 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1049 -- Ada 2005 (AI-318-02)
1051 if Token = Tok_Access then
1052 if Ada_Version < Ada_2005 then
1053 Error_Msg_SC
1054 ("anonymous access result type is an Ada 2005 extension");
1055 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
1056 end if;
1058 Result_Node := P_Access_Definition (Result_Not_Null);
1060 else
1061 Result_Node := P_Subtype_Mark;
1062 No_Constraint_Maybe_Expr_Func;
1063 end if;
1065 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1066 Set_Result_Definition (Specification_Node, Result_Node);
1067 return Specification_Node;
1069 elsif Token = Tok_Procedure then
1070 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1071 Scan; -- past PROCEDURE
1072 Ignore (Tok_Body);
1073 Set_Defining_Unit_Name
1074 (Specification_Node, P_Defining_Program_Unit_Name);
1075 Set_Parameter_Specifications
1076 (Specification_Node, P_Parameter_Profile);
1077 return Specification_Node;
1079 else
1080 Error_Msg_SC ("subprogram specification expected");
1081 raise Error_Resync;
1082 end if;
1083 end P_Subprogram_Specification;
1085 ---------------------
1086 -- 6.1 Designator --
1087 ---------------------
1089 -- DESIGNATOR ::=
1090 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1092 -- The caller has checked that the initial token is an identifier,
1093 -- operator symbol, or string literal. Note that we don't bother to
1094 -- do much error diagnosis in this routine, since it is only used for
1095 -- the label on END lines, and the routines in package Par.Endh will
1096 -- check that the label is appropriate.
1098 -- Error recovery: cannot raise Error_Resync
1100 function P_Designator return Node_Id is
1101 Ident_Node : Node_Id;
1102 Name_Node : Node_Id;
1103 Prefix_Node : Node_Id;
1105 function Real_Dot return Boolean;
1106 -- Tests if a current token is an interesting period, i.e. is followed
1107 -- by an identifier or operator symbol or string literal. If not, it is
1108 -- probably just incorrect punctuation to be caught by our caller. Note
1109 -- that the case of an operator symbol or string literal is also an
1110 -- error, but that is an error that we catch here. If the result is
1111 -- True, a real dot has been scanned and we are positioned past it,
1112 -- if the result is False, the scan position is unchanged.
1114 --------------
1115 -- Real_Dot --
1116 --------------
1118 function Real_Dot return Boolean is
1119 Scan_State : Saved_Scan_State;
1121 begin
1122 if Token /= Tok_Dot then
1123 return False;
1125 else
1126 Save_Scan_State (Scan_State);
1127 Scan; -- past dot
1129 if Token = Tok_Identifier
1130 or else Token = Tok_Operator_Symbol
1131 or else Token = Tok_String_Literal
1132 then
1133 return True;
1135 else
1136 Restore_Scan_State (Scan_State);
1137 return False;
1138 end if;
1139 end if;
1140 end Real_Dot;
1142 -- Start of processing for P_Designator
1144 begin
1145 Ident_Node := Token_Node;
1146 Scan; -- past initial token
1148 if Prev_Token = Tok_Operator_Symbol
1149 or else Prev_Token = Tok_String_Literal
1150 or else not Real_Dot
1151 then
1152 return Ident_Node;
1154 -- Child name case
1156 else
1157 Prefix_Node := Ident_Node;
1159 -- Loop through child names, on entry to this loop, Prefix contains
1160 -- the name scanned so far, and Ident_Node is the last identifier.
1162 loop
1163 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1164 Set_Prefix (Name_Node, Prefix_Node);
1165 Ident_Node := P_Identifier;
1166 Set_Selector_Name (Name_Node, Ident_Node);
1167 Prefix_Node := Name_Node;
1168 exit when not Real_Dot;
1169 end loop;
1171 -- On exit from the loop, Ident_Node is the last identifier scanned,
1172 -- i.e. the defining identifier, and Prefix_Node is a node for the
1173 -- entire name, structured (incorrectly) as a selected component.
1175 Name_Node := Prefix (Prefix_Node);
1176 Change_Node (Prefix_Node, N_Designator);
1177 Set_Name (Prefix_Node, Name_Node);
1178 Set_Identifier (Prefix_Node, Ident_Node);
1179 return Prefix_Node;
1180 end if;
1182 exception
1183 when Error_Resync =>
1184 while Token = Tok_Dot or else Token = Tok_Identifier loop
1185 Scan;
1186 end loop;
1188 return Error;
1189 end P_Designator;
1191 ------------------------------
1192 -- 6.1 Defining Designator --
1193 ------------------------------
1195 -- DEFINING_DESIGNATOR ::=
1196 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1198 -- Error recovery: cannot raise Error_Resync
1200 function P_Defining_Designator return Node_Id is
1201 begin
1202 if Token = Tok_Operator_Symbol then
1203 return P_Defining_Operator_Symbol;
1205 elsif Token = Tok_String_Literal then
1206 Error_Msg_SC ("invalid operator name");
1207 Scan; -- past junk string
1208 return Error;
1210 else
1211 return P_Defining_Program_Unit_Name;
1212 end if;
1213 end P_Defining_Designator;
1215 -------------------------------------
1216 -- 6.1 Defining Program Unit Name --
1217 -------------------------------------
1219 -- DEFINING_PROGRAM_UNIT_NAME ::=
1220 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1222 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1224 -- Error recovery: cannot raise Error_Resync
1226 function P_Defining_Program_Unit_Name return Node_Id is
1227 Ident_Node : Node_Id;
1228 Name_Node : Node_Id;
1229 Prefix_Node : Node_Id;
1231 begin
1232 -- Set identifier casing if not already set and scan initial identifier
1234 if Token = Tok_Identifier
1235 and then Identifier_Casing (Current_Source_File) = Unknown
1236 then
1237 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1238 end if;
1240 Ident_Node := P_Identifier (C_Dot);
1241 Merge_Identifier (Ident_Node, Tok_Return);
1243 -- Normal case (not child library unit name)
1245 if Token /= Tok_Dot then
1246 Change_Identifier_To_Defining_Identifier (Ident_Node);
1247 Warn_If_Standard_Redefinition (Ident_Node);
1248 return Ident_Node;
1250 -- Child library unit name case
1252 else
1253 if Scope.Last > 1 then
1254 Error_Msg_SP ("child unit allowed only at library level");
1255 raise Error_Resync;
1257 elsif Ada_Version = Ada_83 then
1258 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1260 end if;
1262 Prefix_Node := Ident_Node;
1264 -- Loop through child names, on entry to this loop, Prefix contains
1265 -- the name scanned so far, and Ident_Node is the last identifier.
1267 loop
1268 exit when Token /= Tok_Dot;
1269 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1270 Scan; -- past period
1271 Set_Prefix (Name_Node, Prefix_Node);
1272 Ident_Node := P_Identifier (C_Dot);
1273 Set_Selector_Name (Name_Node, Ident_Node);
1274 Prefix_Node := Name_Node;
1275 end loop;
1277 -- On exit from the loop, Ident_Node is the last identifier scanned,
1278 -- i.e. the defining identifier, and Prefix_Node is a node for the
1279 -- entire name, structured (incorrectly) as a selected component.
1281 Name_Node := Prefix (Prefix_Node);
1282 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1283 Set_Name (Prefix_Node, Name_Node);
1284 Change_Identifier_To_Defining_Identifier (Ident_Node);
1285 Warn_If_Standard_Redefinition (Ident_Node);
1286 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1288 -- All set with unit name parsed
1290 return Prefix_Node;
1291 end if;
1293 exception
1294 when Error_Resync =>
1295 while Token = Tok_Dot or else Token = Tok_Identifier loop
1296 Scan;
1297 end loop;
1299 return Error;
1300 end P_Defining_Program_Unit_Name;
1302 --------------------------
1303 -- 6.1 Operator Symbol --
1304 --------------------------
1306 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1308 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1310 -----------------------------------
1311 -- 6.1 Defining Operator Symbol --
1312 -----------------------------------
1314 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1316 -- The caller has checked that the initial symbol is an operator symbol
1318 function P_Defining_Operator_Symbol return Node_Id is
1319 Op_Node : Node_Id;
1321 begin
1322 Op_Node := Token_Node;
1323 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1324 Scan; -- past operator symbol
1325 return Op_Node;
1326 end P_Defining_Operator_Symbol;
1328 ----------------------------
1329 -- 6.1 Parameter_Profile --
1330 ----------------------------
1332 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1334 -- Empty is returned if no formal part is present
1336 -- Error recovery: cannot raise Error_Resync
1338 function P_Parameter_Profile return List_Id is
1339 begin
1340 if Token = Tok_Left_Paren then
1341 Scan; -- part left paren
1342 return P_Formal_Part;
1343 else
1344 return No_List;
1345 end if;
1346 end P_Parameter_Profile;
1348 ---------------------------------------
1349 -- 6.1 Parameter And Result Profile --
1350 ---------------------------------------
1352 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1353 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1355 ----------------------
1356 -- 6.1 Formal part --
1357 ----------------------
1359 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1361 -- PARAMETER_SPECIFICATION ::=
1362 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1363 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1364 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1365 -- [:= DEFAULT_EXPRESSION]
1367 -- This scans the construct Formal_Part. The caller has already checked
1368 -- that the initial token is a left parenthesis, and skipped past it, so
1369 -- that on entry Token is the first token following the left parenthesis.
1371 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1373 -- Error recovery: cannot raise Error_Resync
1375 function P_Formal_Part return List_Id is
1376 Specification_List : List_Id;
1377 Specification_Node : Node_Id;
1378 Scan_State : Saved_Scan_State;
1379 Num_Idents : Nat;
1380 Ident : Nat;
1381 Ident_Sloc : Source_Ptr;
1382 Not_Null_Present : Boolean := False;
1383 Not_Null_Sloc : Source_Ptr;
1385 Idents : array (Int range 1 .. 4096) of Entity_Id;
1386 -- This array holds the list of defining identifiers. The upper bound
1387 -- of 4096 is intended to be essentially infinite, and we do not even
1388 -- bother to check for it being exceeded.
1390 begin
1391 Specification_List := New_List;
1392 Specification_Loop : loop
1393 begin
1394 if Token = Tok_Pragma then
1395 Error_Msg_SC ("pragma not allowed in formal part");
1396 Discard_Junk_Node (P_Pragma (Skipping => True));
1397 end if;
1399 Ignore (Tok_Left_Paren);
1400 Ident_Sloc := Token_Ptr;
1401 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1402 Num_Idents := 1;
1404 Ident_Loop : loop
1405 exit Ident_Loop when Token = Tok_Colon;
1407 -- The only valid tokens are colon and comma, so if we have
1408 -- neither do a bit of investigation to see which is the
1409 -- better choice for insertion.
1411 if Token /= Tok_Comma then
1413 -- Assume colon if ALIASED, IN or OUT keyword found
1415 exit Ident_Loop when Token = Tok_Aliased or else
1416 Token = Tok_In or else
1417 Token = Tok_Out;
1419 -- Otherwise scan ahead
1421 Save_Scan_State (Scan_State);
1422 Look_Ahead : loop
1424 -- If we run into a semicolon, then assume that a
1425 -- colon was missing, e.g. Parms (X Y; ...). Also
1426 -- assume missing colon on EOF (a real disaster)
1427 -- and on a right paren, e.g. Parms (X Y), and also
1428 -- on an assignment symbol, e.g. Parms (X Y := ..)
1430 if Token = Tok_Semicolon
1431 or else Token = Tok_Right_Paren
1432 or else Token = Tok_EOF
1433 or else Token = Tok_Colon_Equal
1434 then
1435 Restore_Scan_State (Scan_State);
1436 exit Ident_Loop;
1438 -- If we run into a colon, assume that we had a missing
1439 -- comma, e.g. Parms (A B : ...). Also assume a missing
1440 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1442 elsif Token = Tok_Colon
1443 or else Token = Tok_Comma
1444 then
1445 Restore_Scan_State (Scan_State);
1446 exit Look_Ahead;
1447 end if;
1449 Scan;
1450 end loop Look_Ahead;
1451 end if;
1453 -- Here if a comma is present, or to be assumed
1455 T_Comma;
1456 Num_Idents := Num_Idents + 1;
1457 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1458 end loop Ident_Loop;
1460 -- Fall through the loop on encountering a colon, or deciding
1461 -- that there is a missing colon.
1463 T_Colon;
1465 -- If there are multiple identifiers, we repeatedly scan the
1466 -- type and initialization expression information by resetting
1467 -- the scan pointer (so that we get completely separate trees
1468 -- for each occurrence).
1470 if Num_Idents > 1 then
1471 Save_Scan_State (Scan_State);
1472 end if;
1474 -- Loop through defining identifiers in list
1476 Ident := 1;
1478 Ident_List_Loop : loop
1479 Specification_Node :=
1480 New_Node (N_Parameter_Specification, Ident_Sloc);
1481 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1483 -- Scan possible ALIASED for Ada 2012 (AI-142)
1485 if Token = Tok_Aliased then
1486 if Ada_Version < Ada_2012 then
1487 Error_Msg_Ada_2012_Feature
1488 ("ALIASED parameter", Token_Ptr);
1489 else
1490 Set_Aliased_Present (Specification_Node);
1491 end if;
1493 Scan; -- past ALIASED
1494 end if;
1496 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1498 Not_Null_Sloc := Token_Ptr;
1499 Not_Null_Present :=
1500 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1502 -- Case of ACCESS keyword present
1504 if Token = Tok_Access then
1505 Set_Null_Exclusion_Present
1506 (Specification_Node, Not_Null_Present);
1508 if Ada_Version = Ada_83 then
1509 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1510 end if;
1512 Set_Parameter_Type
1513 (Specification_Node,
1514 P_Access_Definition (Not_Null_Present));
1516 -- Case of IN or OUT present
1518 else
1519 if Token = Tok_In or else Token = Tok_Out then
1520 if Not_Null_Present then
1521 Error_Msg
1522 ("`NOT NULL` can only be used with `ACCESS`",
1523 Not_Null_Sloc);
1525 if Token = Tok_In then
1526 Error_Msg
1527 ("\`IN` not allowed together with `ACCESS`",
1528 Not_Null_Sloc);
1529 else
1530 Error_Msg
1531 ("\`OUT` not allowed together with `ACCESS`",
1532 Not_Null_Sloc);
1533 end if;
1534 end if;
1536 P_Mode (Specification_Node);
1537 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1538 end if;
1540 Set_Null_Exclusion_Present
1541 (Specification_Node, Not_Null_Present);
1543 if Token = Tok_Procedure
1544 or else
1545 Token = Tok_Function
1546 then
1547 Error_Msg_SC ("formal subprogram parameter not allowed");
1548 Scan;
1550 if Token = Tok_Left_Paren then
1551 Discard_Junk_List (P_Formal_Part);
1552 end if;
1554 if Token = Tok_Return then
1555 Scan;
1556 Discard_Junk_Node (P_Subtype_Mark);
1557 end if;
1559 Set_Parameter_Type (Specification_Node, Error);
1561 else
1562 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1563 No_Constraint;
1564 end if;
1565 end if;
1567 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1569 if Ident > 1 then
1570 Set_Prev_Ids (Specification_Node, True);
1571 end if;
1573 if Ident < Num_Idents then
1574 Set_More_Ids (Specification_Node, True);
1575 end if;
1577 Append (Specification_Node, Specification_List);
1578 exit Ident_List_Loop when Ident = Num_Idents;
1579 Ident := Ident + 1;
1580 Restore_Scan_State (Scan_State);
1581 end loop Ident_List_Loop;
1583 exception
1584 when Error_Resync =>
1585 Resync_Semicolon_List;
1586 end;
1588 if Token = Tok_Semicolon then
1589 Save_Scan_State (Scan_State);
1590 Scan; -- past semicolon
1592 -- If we have RETURN or IS after the semicolon, then assume
1593 -- that semicolon should have been a right parenthesis and exit
1595 if Token = Tok_Is or else Token = Tok_Return then
1596 Error_Msg_SP -- CODEFIX
1597 ("|"";"" should be "")""");
1598 exit Specification_Loop;
1599 end if;
1601 -- If we have a declaration keyword after the semicolon, then
1602 -- assume we had a missing right parenthesis and terminate list
1604 if Token in Token_Class_Declk then
1605 Error_Msg_AP -- CODEFIX
1606 ("missing "")""");
1607 Restore_Scan_State (Scan_State);
1608 exit Specification_Loop;
1609 end if;
1611 elsif Token = Tok_Right_Paren then
1612 Scan; -- past right paren
1613 exit Specification_Loop;
1615 -- Special check for common error of using comma instead of semicolon
1617 elsif Token = Tok_Comma then
1618 T_Semicolon;
1619 Scan; -- past comma
1621 -- Special check for omitted separator
1623 elsif Token = Tok_Identifier then
1624 T_Semicolon;
1626 -- If nothing sensible, skip to next semicolon or right paren
1628 else
1629 T_Semicolon;
1630 Resync_Semicolon_List;
1632 if Token = Tok_Semicolon then
1633 Scan; -- past semicolon
1634 else
1635 T_Right_Paren;
1636 exit Specification_Loop;
1637 end if;
1638 end if;
1639 end loop Specification_Loop;
1641 return Specification_List;
1642 end P_Formal_Part;
1644 ----------------------------------
1645 -- 6.1 Parameter Specification --
1646 ----------------------------------
1648 -- Parsed by P_Formal_Part (6.1)
1650 ---------------
1651 -- 6.1 Mode --
1652 ---------------
1654 -- MODE ::= [in] | in out | out
1656 -- There is no explicit node in the tree for the Mode. Instead the
1657 -- In_Present and Out_Present flags are set in the parent node to
1658 -- record the presence of keywords specifying the mode.
1660 -- Error_Recovery: cannot raise Error_Resync
1662 procedure P_Mode (Node : Node_Id) is
1663 begin
1664 if Token = Tok_In then
1665 Scan; -- past IN
1666 Set_In_Present (Node, True);
1668 if Style.Mode_In_Check and then Token /= Tok_Out then
1669 Error_Msg_SP -- CODEFIX
1670 ("(style) IN should be omitted");
1671 end if;
1673 -- Since Ada 2005, formal objects can have an anonymous access type,
1674 -- and of course carry a mode indicator.
1676 if Token = Tok_Access
1677 and then Nkind (Node) /= N_Formal_Object_Declaration
1678 then
1679 Error_Msg_SP ("IN not allowed together with ACCESS");
1680 Scan; -- past ACCESS
1681 end if;
1682 end if;
1684 if Token = Tok_Out then
1685 Scan; -- past OUT
1686 Set_Out_Present (Node, True);
1687 end if;
1689 if Token = Tok_In then
1690 Error_Msg_SC ("IN must precede OUT in parameter mode");
1691 Scan; -- past IN
1692 Set_In_Present (Node, True);
1693 end if;
1694 end P_Mode;
1696 --------------------------
1697 -- 6.3 Subprogram Body --
1698 --------------------------
1700 -- Parsed by P_Subprogram (6.1)
1702 -----------------------------------
1703 -- 6.4 Procedure Call Statement --
1704 -----------------------------------
1706 -- Parsed by P_Sequence_Of_Statements (5.1)
1708 ------------------------
1709 -- 6.4 Function Call --
1710 ------------------------
1712 -- Parsed by P_Name (4.1)
1714 --------------------------------
1715 -- 6.4 Actual Parameter Part --
1716 --------------------------------
1718 -- Parsed by P_Name (4.1)
1720 --------------------------------
1721 -- 6.4 Parameter Association --
1722 --------------------------------
1724 -- Parsed by P_Name (4.1)
1726 ------------------------------------
1727 -- 6.4 Explicit Actual Parameter --
1728 ------------------------------------
1730 -- Parsed by P_Name (4.1)
1732 ---------------------------
1733 -- 6.5 Return Statement --
1734 ---------------------------
1736 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1738 -- EXTENDED_RETURN_STATEMENT ::=
1739 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1740 -- [:= EXPRESSION] [do
1741 -- HANDLED_SEQUENCE_OF_STATEMENTS
1742 -- end return];
1744 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1746 -- RETURN_STATEMENT ::= return [EXPRESSION];
1748 -- Error recovery: can raise Error_Resync
1750 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1752 -- Note: We don't need to check Ada_Version here, because this is
1753 -- only called in >= Ada 2005 cases anyway.
1755 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1757 begin
1758 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1760 if Token = Tok_Access then
1761 Set_Object_Definition
1762 (Decl_Node, P_Access_Definition (Not_Null_Present));
1763 else
1764 Set_Object_Definition
1765 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1766 end if;
1767 end P_Return_Subtype_Indication;
1769 -- Error recovery: can raise Error_Resync
1771 function P_Return_Object_Declaration return Node_Id is
1772 Return_Obj : Node_Id;
1773 Decl_Node : Node_Id;
1775 begin
1776 Return_Obj := Token_Node;
1777 Change_Identifier_To_Defining_Identifier (Return_Obj);
1778 Warn_If_Standard_Redefinition (Return_Obj);
1779 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1780 Set_Defining_Identifier (Decl_Node, Return_Obj);
1782 Scan; -- past identifier
1783 Scan; -- past :
1785 -- First an error check, if we have two identifiers in a row, a likely
1786 -- possibility is that the first of the identifiers is an incorrectly
1787 -- spelled keyword. See similar check in P_Identifier_Declarations.
1789 if Token = Tok_Identifier then
1790 declare
1791 SS : Saved_Scan_State;
1792 I2 : Boolean;
1794 begin
1795 Save_Scan_State (SS);
1796 Scan; -- past initial identifier
1797 I2 := (Token = Tok_Identifier);
1798 Restore_Scan_State (SS);
1800 if I2
1801 and then
1802 (Bad_Spelling_Of (Tok_Access) or else
1803 Bad_Spelling_Of (Tok_Aliased) or else
1804 Bad_Spelling_Of (Tok_Constant))
1805 then
1806 null;
1807 end if;
1808 end;
1809 end if;
1811 -- We allow "constant" here (as in "return Result : constant
1812 -- T..."). This is not in the latest RM, but the ARG is considering an
1813 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1815 if Token = Tok_Constant then
1816 Scan; -- past CONSTANT
1817 Set_Constant_Present (Decl_Node);
1819 if Token = Tok_Aliased then
1820 Error_Msg_SC -- CODEFIX
1821 ("ALIASED should be before CONSTANT");
1822 Scan; -- past ALIASED
1823 Set_Aliased_Present (Decl_Node);
1824 end if;
1826 elsif Token = Tok_Aliased then
1827 Scan; -- past ALIASED
1828 Set_Aliased_Present (Decl_Node);
1830 -- The restrictions on the use of aliased in an extended return
1831 -- are semantic, not syntactic.
1833 if Token = Tok_Constant then
1834 Scan; -- past CONSTANT
1835 Set_Constant_Present (Decl_Node);
1836 end if;
1837 end if;
1839 P_Return_Subtype_Indication (Decl_Node);
1841 if Token = Tok_Colon_Equal then
1842 Scan; -- past :=
1843 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1844 end if;
1846 return Decl_Node;
1847 end P_Return_Object_Declaration;
1849 -- Error recovery: can raise Error_Resync
1851 function P_Return_Statement return Node_Id is
1852 -- The caller has checked that the initial token is RETURN
1854 function Is_Simple return Boolean;
1855 -- Scan state is just after RETURN (and is left that way). Determine
1856 -- whether this is a simple or extended return statement by looking
1857 -- ahead for "identifier :", which implies extended.
1859 ---------------
1860 -- Is_Simple --
1861 ---------------
1863 function Is_Simple return Boolean is
1864 Scan_State : Saved_Scan_State;
1865 Result : Boolean := True;
1867 begin
1868 if Token = Tok_Identifier then
1869 Save_Scan_State (Scan_State); -- at identifier
1870 Scan; -- past identifier
1872 if Token = Tok_Colon then
1873 Result := False; -- It's an extended_return_statement.
1874 end if;
1876 Restore_Scan_State (Scan_State); -- to identifier
1877 end if;
1879 return Result;
1880 end Is_Simple;
1882 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1883 Ret_Strt : constant Column_Number := Start_Column;
1884 Ret_Node : Node_Id;
1886 -- Start of processing for P_Return_Statement
1888 begin
1889 Scan; -- past RETURN
1891 -- Simple_return_statement, no expression, return an
1892 -- N_Simple_Return_Statement node with the expression field left Empty.
1894 if Token = Tok_Semicolon then
1895 Scan; -- past ;
1896 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1898 -- Nontrivial case
1900 else
1901 -- Simple_return_statement with expression
1903 -- We avoid trying to scan an expression if we are at an
1904 -- expression terminator since in that case the best error
1905 -- message is probably that we have a missing semicolon.
1907 if Is_Simple then
1908 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1910 if Token not in Token_Class_Eterm then
1911 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1912 end if;
1914 -- Extended_return_statement (Ada 2005 only -- AI-318):
1916 else
1917 if Ada_Version < Ada_2005 then
1918 Error_Msg_SP
1919 (" extended_return_statement is an Ada 2005 extension");
1920 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1921 end if;
1923 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1924 Set_Return_Object_Declarations
1925 (Ret_Node, New_List (P_Return_Object_Declaration));
1927 if Token = Tok_Do then
1928 Push_Scope_Stack;
1929 Scope.Table (Scope.Last).Ecol := Ret_Strt;
1930 Scope.Table (Scope.Last).Etyp := E_Return;
1931 Scope.Table (Scope.Last).Labl := Error;
1932 Scope.Table (Scope.Last).Sloc := Ret_Sloc;
1934 Scan; -- past DO
1935 Set_Handled_Statement_Sequence
1936 (Ret_Node, P_Handled_Sequence_Of_Statements);
1937 End_Statements;
1939 -- Do we need to handle Error_Resync here???
1940 end if;
1941 end if;
1943 TF_Semicolon;
1944 end if;
1946 return Ret_Node;
1947 end P_Return_Statement;
1949 end Ch6;