Daily bump.
[official-gcc.git] / gcc / ada / par-ch6.adb
blobbe85d093d1fd72003ea1e92250fa4614e1cb8813
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-2021, 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
205 function Contains_Import_Aspect (Aspects : List_Id) return Boolean;
206 -- Return True if Aspects contains an Import aspect.
208 ----------------------------
209 -- Contains_Import_Aspect --
210 ----------------------------
212 function Contains_Import_Aspect (Aspects : List_Id) return Boolean is
213 Aspect : Node_Id := First (Aspects);
214 begin
215 while Present (Aspect) loop
216 if Chars (Identifier (Aspect)) = Name_Import then
217 return True;
218 end if;
220 Next (Aspect);
221 end loop;
223 return False;
224 end Contains_Import_Aspect;
226 Specification_Node : Node_Id;
227 Name_Node : Node_Id;
228 Aspects : List_Id;
229 Fpart_List : List_Id;
230 Fpart_Sloc : Source_Ptr;
231 Result_Not_Null : Boolean := False;
232 Result_Node : Node_Id;
233 Inst_Node : Node_Id;
234 Body_Node : Node_Id;
235 Decl_Node : Node_Id;
236 Rename_Node : Node_Id;
237 Absdec_Node : Node_Id;
238 Stub_Node : Node_Id;
239 Fproc_Sloc : Source_Ptr;
240 Func : Boolean;
241 Scan_State : Saved_Scan_State;
243 -- Flags for optional overriding indication. Two flags are needed,
244 -- to distinguish positive and negative overriding indicators from
245 -- the absence of any indicator.
247 Is_Overriding : Boolean := False;
248 Not_Overriding : Boolean := False;
250 begin
251 -- Set up scope stack entry. Note that the Labl field will be set later
253 SIS_Entry_Active := False;
254 SIS_Aspect_Import_Seen := False;
255 SIS_Missing_Semicolon_Message := No_Error_Msg;
256 Push_Scope_Stack;
257 Scopes (Scope.Last).Sloc := Token_Ptr;
258 Scopes (Scope.Last).Etyp := E_Name;
259 Scopes (Scope.Last).Ecol := Start_Column;
260 Scopes (Scope.Last).Lreq := False;
262 Aspects := Empty_List;
264 -- Ada 2005: Scan leading NOT OVERRIDING indicator
266 if Token = Tok_Not then
267 Scan; -- past NOT
269 if Token = Tok_Overriding then
270 Scan; -- past OVERRIDING
271 Not_Overriding := True;
273 -- Overriding keyword used in non Ada 2005 mode
275 elsif Token = Tok_Identifier
276 and then Token_Name = Name_Overriding
277 then
278 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
279 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
280 Scan; -- past Overriding
281 Not_Overriding := True;
283 else
284 Error_Msg_SC -- CODEFIX
285 ("OVERRIDING expected!");
286 end if;
288 -- Ada 2005: scan leading OVERRIDING indicator
290 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
291 -- declaration circuit already gave an error message and changed the
292 -- token to Tok_Overriding.
294 elsif Token = Tok_Overriding then
295 Scan; -- past OVERRIDING
296 Is_Overriding := True;
297 end if;
299 if Is_Overriding or else Not_Overriding then
301 -- Note that if we are not in Ada_2005 mode, error messages have
302 -- already been given, so no need to give another message here.
304 -- An overriding indicator is allowed for subprogram declarations,
305 -- bodies (including subunits), renamings, stubs, and instantiations.
306 -- The test against Pf_Decl_Pbod is added to account for the case of
307 -- subprograms declared in a protected type, where only subprogram
308 -- declarations and bodies can occur. The Pf_Pbod case is for
309 -- subunits.
311 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
312 and then
313 Pf_Flags /= Pf_Decl_Pbod_Pexp
314 and then
315 Pf_Flags /= Pf_Pbod_Pexp
316 then
317 Error_Msg_SC ("overriding indicator not allowed here!");
319 elsif Token /= Tok_Function and then Token /= Tok_Procedure then
320 Error_Msg_SC -- CODEFIX
321 ("FUNCTION or PROCEDURE expected!");
322 end if;
323 end if;
325 Func := (Token = Tok_Function);
326 Fproc_Sloc := Token_Ptr;
327 Scan; -- past FUNCTION or PROCEDURE
328 Ignore (Tok_Type);
329 Ignore (Tok_Body);
331 if Func then
332 Name_Node := P_Defining_Designator;
334 if Nkind (Name_Node) = N_Defining_Operator_Symbol
335 and then Scope.Last = 1
336 then
337 Error_Msg_SP ("operator symbol not allowed at library level");
338 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
340 -- Set name from file name, we need some junk name, and that's
341 -- as good as anything. This is only approximate, since we do
342 -- not do anything with non-standard name translations.
344 Get_Name_String (File_Name (Current_Source_File));
346 for J in 1 .. Name_Len loop
347 if Name_Buffer (J) = '.' then
348 Name_Len := J - 1;
349 exit;
350 end if;
351 end loop;
353 Set_Chars (Name_Node, Name_Find);
354 Set_Error_Posted (Name_Node);
355 end if;
357 else
358 Name_Node := P_Defining_Program_Unit_Name;
359 end if;
361 Scopes (Scope.Last).Labl := Name_Node;
362 Current_Node := Name_Node;
363 Ignore (Tok_Colon);
365 -- Deal with generic instantiation, the one case in which we do not
366 -- have a subprogram specification as part of whatever we are parsing
368 if Token = Tok_Is then
369 Save_Scan_State (Scan_State); -- at the IS
370 T_Is; -- checks for redundant IS
372 if Token = Tok_New then
373 if not Pf_Flags.Gins then
374 Error_Msg_SC ("generic instantiation not allowed here!");
375 end if;
377 Scan; -- past NEW
379 if Func then
380 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
381 Set_Name (Inst_Node, P_Function_Name);
382 else
383 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
384 Set_Name (Inst_Node, P_Qualified_Simple_Name);
385 end if;
387 Set_Defining_Unit_Name (Inst_Node, Name_Node);
388 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
389 P_Aspect_Specifications (Inst_Node);
390 Pop_Scope_Stack; -- Don't need scope stack entry in this case
392 if Is_Overriding then
393 Set_Must_Override (Inst_Node);
395 elsif Not_Overriding then
396 Set_Must_Not_Override (Inst_Node);
397 end if;
399 return Inst_Node;
401 else
402 Restore_Scan_State (Scan_State); -- to the IS
403 end if;
404 end if;
406 -- If not a generic instantiation, then we definitely have a subprogram
407 -- specification (all possibilities at this stage include one here)
409 Fpart_Sloc := Token_Ptr;
411 Check_Misspelling_Of (Tok_Return);
413 -- Scan formal part. First a special error check. If we have an
414 -- identifier here, then we have a definite error. If this identifier
415 -- is on the same line as the designator, then we assume it is the
416 -- first formal after a missing left parenthesis
418 if Token = Tok_Identifier
419 and then not Token_Is_At_Start_Of_Line
420 then
421 T_Left_Paren; -- to generate message
422 Fpart_List := P_Formal_Part;
424 -- Otherwise scan out an optional formal part in the usual manner
426 else
427 Fpart_List := P_Parameter_Profile;
428 end if;
430 -- We treat what we have as a function specification if FUNCTION was
431 -- used, or if a RETURN is present. This gives better error recovery
432 -- since later RETURN statements will be valid in either case.
434 Check_Junk_Semicolon_Before_Return;
435 Result_Node := Error;
437 if Token = Tok_Return then
438 if not Func then
439 Error_Msg -- CODEFIX
440 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
441 Func := True;
442 end if;
444 Scan; -- past RETURN
446 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
448 -- Ada 2005 (AI-318-02)
450 if Token = Tok_Access then
451 Error_Msg_Ada_2005_Extension ("anonymous access result type");
453 Result_Node := P_Access_Definition (Result_Not_Null);
455 else
456 Result_Node := P_Subtype_Mark;
457 No_Constraint_Maybe_Expr_Func;
458 end if;
460 else
461 -- Skip extra parenthesis at end of formal part
463 Ignore (Tok_Right_Paren);
465 -- For function, scan result subtype
467 if Func then
468 TF_Return;
470 if Prev_Token = Tok_Return then
471 Result_Node := P_Subtype_Mark;
472 end if;
473 end if;
474 end if;
476 if Func then
477 Specification_Node :=
478 New_Node (N_Function_Specification, Fproc_Sloc);
480 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
481 Set_Result_Definition (Specification_Node, Result_Node);
483 else
484 Specification_Node :=
485 New_Node (N_Procedure_Specification, Fproc_Sloc);
486 end if;
488 Set_Defining_Unit_Name (Specification_Node, Name_Node);
489 Set_Parameter_Specifications (Specification_Node, Fpart_List);
491 if Is_Overriding then
492 Set_Must_Override (Specification_Node);
494 elsif Not_Overriding then
495 Set_Must_Not_Override (Specification_Node);
496 end if;
498 -- Error check: barriers not allowed on protected functions/procedures
500 if Token = Tok_When then
501 if Func then
502 Error_Msg_SC ("barrier not allowed on function, only on entry");
503 else
504 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
505 end if;
507 Scan; -- past WHEN
508 Discard_Junk_Node (P_Expression);
509 end if;
511 -- Deal with semicolon followed by IS. We want to treat this as IS
513 if Token = Tok_Semicolon then
514 Save_Scan_State (Scan_State);
515 Scan; -- past semicolon
517 if Token = Tok_Is then
518 Error_Msg_SP -- CODEFIX
519 ("extra "";"" ignored");
520 else
521 Restore_Scan_State (Scan_State);
522 end if;
523 end if;
525 -- Subprogram declaration ended by aspect specifications
527 if Aspect_Specifications_Present then
528 goto Subprogram_Declaration;
530 -- Deal with case of semicolon ending a subprogram declaration
532 elsif Token = Tok_Semicolon then
533 if not Pf_Flags.Decl then
534 T_Is;
535 end if;
537 Save_Scan_State (Scan_State);
538 Scan; -- past semicolon
540 -- If semicolon is immediately followed by IS, then ignore the
541 -- semicolon, and go process the body.
543 if Token = Tok_Is then
544 Error_Msg_SP -- CODEFIX
545 ("|extra "";"" ignored");
546 T_Is; -- scan past IS
547 goto Subprogram_Body;
549 -- If BEGIN follows in an appropriate column, we immediately
550 -- commence the error action of assuming that the previous
551 -- subprogram declaration should have been a subprogram body,
552 -- i.e. that the terminating semicolon should have been IS.
554 elsif Token = Tok_Begin
555 and then Start_Column >= Scopes (Scope.Last).Ecol
556 then
557 Error_Msg_SP -- CODEFIX
558 ("|"";"" should be IS!");
559 goto Subprogram_Body;
561 else
562 Restore_Scan_State (Scan_State);
563 goto Subprogram_Declaration;
564 end if;
566 -- Case of not followed by semicolon
568 else
569 -- Subprogram renaming declaration case
571 Check_Misspelling_Of (Tok_Renames);
573 if Token = Tok_Renames then
574 if not Pf_Flags.Rnam then
575 Error_Msg_SC ("renaming declaration not allowed here!");
576 end if;
578 Rename_Node :=
579 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
580 Scan; -- past RENAMES
581 Set_Name (Rename_Node, P_Name);
582 Set_Specification (Rename_Node, Specification_Node);
583 P_Aspect_Specifications (Rename_Node);
584 TF_Semicolon;
585 Pop_Scope_Stack;
586 return Rename_Node;
588 -- Case of IS following subprogram specification
590 elsif Token = Tok_Is then
591 T_Is; -- ignore redundant Is's
593 if Token_Name = Name_Abstract then
594 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
595 end if;
597 -- Deal nicely with (now obsolete) use of <> in place of abstract
599 if Token = Tok_Box then
600 Error_Msg_SC -- CODEFIX
601 ("ABSTRACT expected");
602 Token := Tok_Abstract;
603 end if;
605 -- Abstract subprogram declaration case
607 if Token = Tok_Abstract then
608 Absdec_Node :=
609 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
610 Set_Specification (Absdec_Node, Specification_Node);
611 Pop_Scope_Stack; -- discard unneeded entry
612 Scan; -- past ABSTRACT
613 P_Aspect_Specifications (Absdec_Node);
614 return Absdec_Node;
616 -- Ada 2005 (AI-248): Parse a null procedure declaration
618 elsif Token = Tok_Null then
619 Error_Msg_Ada_2005_Extension ("null procedure");
621 Scan; -- past NULL
623 if Func then
624 Error_Msg_SP ("only procedures can be null");
625 else
626 Set_Null_Present (Specification_Node);
627 Set_Null_Statement (Specification_Node,
628 New_Node (N_Null_Statement, Prev_Token_Ptr));
629 end if;
631 goto Subprogram_Declaration;
633 -- Check for IS NEW with Formal_Part present and handle nicely
635 elsif Token = Tok_New then
636 Error_Msg
637 ("formal part not allowed in instantiation", Fpart_Sloc);
638 Scan; -- past NEW
640 if Func then
641 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
642 else
643 Inst_Node :=
644 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
645 end if;
647 Set_Defining_Unit_Name (Inst_Node, Name_Node);
648 Set_Name (Inst_Node, P_Name);
649 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
650 TF_Semicolon;
651 Pop_Scope_Stack; -- Don't need scope stack entry in this case
652 return Inst_Node;
654 else
655 goto Subprogram_Body;
656 end if;
658 -- Aspect specifications present
660 elsif Aspect_Specifications_Present then
661 goto Subprogram_Declaration;
663 -- Here we have a missing IS or missing semicolon
665 else
666 -- If the next token is a left paren at the start of a line, then
667 -- this is almost certainly the start of the expression for an
668 -- expression function, so in this case guess a missing IS.
670 if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then
671 Error_Msg_AP -- CODEFIX
672 ("missing IS");
674 -- In all other cases, we guess a missing semicolon, since we are
675 -- good at fixing up a semicolon which should really be an IS.
677 else
678 Error_Msg_AP -- CODEFIX
679 ("|missing "";""");
680 SIS_Missing_Semicolon_Message := Get_Msg_Id;
681 goto Subprogram_Declaration;
682 end if;
683 end if;
684 end if;
686 -- Processing for stub or subprogram body or expression function
688 <<Subprogram_Body>>
690 -- Subprogram body stub case
692 if Separate_Present then
693 if not Pf_Flags.Stub then
694 Error_Msg_SC ("body stub not allowed here!");
695 end if;
697 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
698 Error_Msg
699 ("operator symbol cannot be used as subunit name",
700 Sloc (Name_Node));
701 end if;
703 Scan; -- past SEPARATE
705 Stub_Node :=
706 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
707 Set_Specification (Stub_Node, Specification_Node);
709 if Is_Non_Empty_List (Aspects) then
710 Error_Msg
711 ("aspect specifications must come after SEPARATE",
712 Sloc (First (Aspects)));
713 end if;
715 P_Aspect_Specifications (Stub_Node, Semicolon => False);
716 TF_Semicolon;
717 Pop_Scope_Stack;
718 return Stub_Node;
720 -- Subprogram body or expression function case
722 else
723 Scan_Body_Or_Expression_Function : declare
725 function Likely_Expression_Function return Boolean;
726 -- Returns True if we have a probable case of an expression
727 -- function omitting the parentheses, if so, returns True
728 -- and emits an appropriate error message, else returns False.
730 --------------------------------
731 -- Likely_Expression_Function --
732 --------------------------------
734 function Likely_Expression_Function return Boolean is
735 begin
736 -- If currently pointing to BEGIN or a declaration keyword
737 -- or a pragma, then we definitely have a subprogram body.
738 -- This is a common case, so worth testing first.
740 if Token = Tok_Begin
741 or else Token in Token_Class_Declk
742 or else Token = Tok_Pragma
743 then
744 return False;
746 -- Test for tokens which could only start an expression and
747 -- thus signal the case of a expression function.
749 elsif Token in Token_Class_Literal
750 or else Token in Token_Class_Unary_Addop
751 or else Token = Tok_Left_Paren
752 or else Token = Tok_Abs
753 or else Token = Tok_Null
754 or else Token = Tok_New
755 or else Token = Tok_Not
756 then
757 null;
759 -- Anything other than an identifier must be a body
761 elsif Token /= Tok_Identifier then
762 return False;
764 -- Here for an identifier
766 else
767 -- If the identifier is the first token on its line, then
768 -- let's assume that we have a missing begin and this is
769 -- intended as a subprogram body. However, if the context
770 -- is a function and the unit is a package declaration, a
771 -- body would be illegal, so try for an unparenthesized
772 -- expression function.
774 if Token_Is_At_Start_Of_Line then
775 declare
776 -- The enclosing scope entry is a subprogram spec
778 Spec_Node : constant Node_Id :=
779 Parent
780 (Scopes (Scope.Last).Labl);
781 Lib_Node : Node_Id := Spec_Node;
783 begin
784 -- Check whether there is an enclosing scope that
785 -- is a package declaration.
787 if Scope.Last > 1 then
788 Lib_Node :=
789 Parent (Scopes (Scope.Last - 1).Labl);
790 end if;
792 if Ada_Version >= Ada_2012
793 and then
794 Nkind (Lib_Node) = N_Package_Specification
795 and then
796 Nkind (Spec_Node) = N_Function_Specification
797 then
798 null;
799 else
800 return False;
801 end if;
802 end;
804 -- Otherwise we have to scan ahead. If the identifier is
805 -- followed by a colon or a comma, it is a declaration
806 -- and hence we have a subprogram body. Otherwise assume
807 -- a expression function.
809 else
810 declare
811 Scan_State : Saved_Scan_State;
812 Tok : Token_Type;
814 begin
815 Save_Scan_State (Scan_State);
816 Scan; -- past identifier
817 Tok := Token;
818 Restore_Scan_State (Scan_State);
820 if Tok = Tok_Colon or else Tok = Tok_Comma then
821 return False;
822 end if;
823 end;
824 end if;
825 end if;
827 -- Fall through if we have a likely expression function.
828 -- If the starting keyword is not "function" the error
829 -- will be reported elsewhere.
831 if Func then
832 Error_Msg_SC
833 ("expression function must be enclosed in parentheses");
834 end if;
836 return True;
837 end Likely_Expression_Function;
839 -- Start of processing for Scan_Body_Or_Expression_Function
841 begin
842 -- Expression_Function case
844 if Token = Tok_Left_Paren
845 or else Likely_Expression_Function
846 then
847 -- Check expression function allowed here
849 if not Pf_Flags.Pexp then
850 Error_Msg_SC ("expression function not allowed here!");
851 end if;
853 -- Check we are in Ada 2012 mode
855 Error_Msg_Ada_2012_Feature
856 ("!expression function", Token_Ptr);
858 -- Catch an illegal placement of the aspect specification
859 -- list:
861 -- function_specification
862 -- [aspect_specification] is (expression);
864 -- This case is correctly processed by the parser because
865 -- the expression function first appears as a subprogram
866 -- declaration to the parser. The starting keyword may
867 -- not have been "function" in which case the error is
868 -- on a malformed procedure.
870 if Is_Non_Empty_List (Aspects) then
871 if Func then
872 Error_Msg
873 ("aspect specifications must come after "
874 & "parenthesized expression",
875 Sloc (First (Aspects)));
876 else
877 Error_Msg
878 ("aspect specifications must come after subprogram "
879 & "specification", Sloc (First (Aspects)));
880 end if;
881 end if;
883 -- Parse out expression and build expression function
885 Body_Node :=
886 New_Node
887 (N_Expression_Function, Sloc (Specification_Node));
888 Set_Specification (Body_Node, Specification_Node);
890 declare
891 Expr : constant Node_Id := P_Expression;
892 begin
893 Set_Expression (Body_Node, Expr);
895 -- Check that the full expression is properly
896 -- parenthesized since we may have a left-operand that is
897 -- parenthesized but that is not one of the allowed cases
898 -- with syntactic parentheses.
900 if not (Paren_Count (Expr) /= 0
901 or else Nkind (Expr) in N_Aggregate
902 | N_Extension_Aggregate
903 | N_Quantified_Expression)
904 then
905 Error_Msg
906 ("expression function must be enclosed in "
907 & "parentheses", Sloc (Expr));
908 end if;
909 end;
911 -- Expression functions can carry pre/postconditions
913 P_Aspect_Specifications (Body_Node);
914 Pop_Scope_Stack;
916 -- Subprogram body case
918 else
919 -- Check body allowed here
921 if not Pf_Flags.Pbod then
922 Error_Msg_SP ("subprogram body not allowed here!");
923 end if;
925 -- Here is the test for a suspicious IS (i.e. one that
926 -- looks like it might more properly be a semicolon).
927 -- See separate section describing use of IS instead
928 -- of semicolon in package Parse.
930 if (Token in Token_Class_Declk
931 or else
932 Token = Tok_Identifier)
933 and then Start_Column <= Scopes (Scope.Last).Ecol
934 and then Scope.Last /= 1
935 then
936 Scopes (Scope.Last).Etyp := E_Suspicious_Is;
937 Scopes (Scope.Last).S_Is := Prev_Token_Ptr;
938 end if;
940 -- Build and return subprogram body, parsing declarations
941 -- and statement sequence that belong to the body.
943 Body_Node :=
944 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
945 Set_Specification (Body_Node, Specification_Node);
947 -- If aspects are present, the specification is parsed as
948 -- a subprogram declaration, and we jump here after seeing
949 -- the keyword IS. Attach asspects previously collected to
950 -- the body.
952 if Is_Non_Empty_List (Aspects) then
953 Set_Parent (Aspects, Body_Node);
954 Set_Aspect_Specifications (Body_Node, Aspects);
955 end if;
957 Parse_Decls_Begin_End (Body_Node);
958 end if;
960 return Body_Node;
961 end Scan_Body_Or_Expression_Function;
962 end if;
964 -- Processing for subprogram declaration
966 <<Subprogram_Declaration>>
967 Decl_Node :=
968 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
969 Set_Specification (Decl_Node, Specification_Node);
970 Aspects := Get_Aspect_Specifications (Semicolon => False);
972 -- Aspects may be present on a subprogram body. The source parsed
973 -- so far is that of its specification. Go parse the body and attach
974 -- the collected aspects, if any, to the body.
976 if Token = Tok_Is then
978 -- If the subprogram is a procedure and already has a
979 -- specification, we can't define another.
981 if Nkind (Specification (Decl_Node)) = N_Procedure_Specification
982 and then Null_Present (Specification (Decl_Node))
983 then
984 Error_Msg_AP ("null procedure cannot have a body");
985 end if;
987 Scan;
988 goto Subprogram_Body;
990 else
991 if Is_Non_Empty_List (Aspects) then
992 Set_Parent (Aspects, Decl_Node);
993 Set_Aspect_Specifications (Decl_Node, Aspects);
994 end if;
996 TF_Semicolon;
997 end if;
999 -- If this is a context in which a subprogram body is permitted,
1000 -- set active SIS entry in case (see section titled "Handling
1001 -- Semicolon Used in Place of IS" in body of Parser package)
1002 -- Note that SIS_Missing_Semicolon_Message is already set properly.
1004 if Pf_Flags.Pbod
1006 -- Disconnect this processing if we have scanned a null procedure
1007 -- or an Import aspect because in this case the spec is complete
1008 -- anyway with no body.
1010 and then (Nkind (Specification_Node) /= N_Procedure_Specification
1011 or else not Null_Present (Specification_Node))
1012 and then not Contains_Import_Aspect (Aspects)
1013 then
1014 SIS_Labl := Scopes (Scope.Last).Labl;
1015 SIS_Sloc := Scopes (Scope.Last).Sloc;
1016 SIS_Ecol := Scopes (Scope.Last).Ecol;
1017 SIS_Declaration_Node := Decl_Node;
1018 SIS_Semicolon_Sloc := Prev_Token_Ptr;
1020 -- Do not activate the entry if we have "with Import"
1022 if not SIS_Aspect_Import_Seen then
1023 SIS_Entry_Active := True;
1024 end if;
1025 end if;
1027 Pop_Scope_Stack;
1028 return Decl_Node;
1029 end P_Subprogram;
1031 ---------------------------------
1032 -- 6.1 Subprogram Declaration --
1033 ---------------------------------
1035 -- Parsed by P_Subprogram (6.1)
1037 ------------------------------------------
1038 -- 6.1 Abstract Subprogram Declaration --
1039 ------------------------------------------
1041 -- Parsed by P_Subprogram (6.1)
1043 -----------------------------------
1044 -- 6.1 Subprogram Specification --
1045 -----------------------------------
1047 -- SUBPROGRAM_SPECIFICATION ::=
1048 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1049 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1051 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1053 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1055 -- Subprogram specifications that appear in subprogram declarations
1056 -- are parsed by P_Subprogram (6.1). This routine is used in other
1057 -- contexts where subprogram specifications occur.
1059 -- Note: this routine does not affect the scope stack in any way
1061 -- Error recovery: can raise Error_Resync
1063 function P_Subprogram_Specification return Node_Id is
1064 Specification_Node : Node_Id;
1065 Result_Not_Null : Boolean;
1066 Result_Node : Node_Id;
1068 begin
1069 if Token = Tok_Function then
1070 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1071 Scan; -- past FUNCTION
1072 Ignore (Tok_Body);
1073 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1074 Set_Parameter_Specifications
1075 (Specification_Node, P_Parameter_Profile);
1076 Check_Junk_Semicolon_Before_Return;
1077 TF_Return;
1079 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1081 -- Ada 2005 (AI-318-02)
1083 if Token = Tok_Access then
1084 Error_Msg_Ada_2005_Extension ("anonymous access result type");
1086 Result_Node := P_Access_Definition (Result_Not_Null);
1088 else
1089 Result_Node := P_Subtype_Mark;
1090 No_Constraint_Maybe_Expr_Func;
1091 end if;
1093 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1094 Set_Result_Definition (Specification_Node, Result_Node);
1095 return Specification_Node;
1097 elsif Token = Tok_Procedure then
1098 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1099 Scan; -- past PROCEDURE
1100 Ignore (Tok_Body);
1101 Set_Defining_Unit_Name
1102 (Specification_Node, P_Defining_Program_Unit_Name);
1103 Set_Parameter_Specifications
1104 (Specification_Node, P_Parameter_Profile);
1105 return Specification_Node;
1107 else
1108 Error_Msg_SC ("subprogram specification expected");
1109 raise Error_Resync;
1110 end if;
1111 end P_Subprogram_Specification;
1113 ---------------------
1114 -- 6.1 Designator --
1115 ---------------------
1117 -- DESIGNATOR ::=
1118 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1120 -- The caller has checked that the initial token is an identifier,
1121 -- operator symbol, or string literal. Note that we don't bother to
1122 -- do much error diagnosis in this routine, since it is only used for
1123 -- the label on END lines, and the routines in package Par.Endh will
1124 -- check that the label is appropriate.
1126 -- Error recovery: cannot raise Error_Resync
1128 function P_Designator return Node_Id is
1129 Ident_Node : Node_Id;
1130 Name_Node : Node_Id;
1131 Prefix_Node : Node_Id;
1133 function Real_Dot return Boolean;
1134 -- Tests if a current token is an interesting period, i.e. is followed
1135 -- by an identifier or operator symbol or string literal. If not, it is
1136 -- probably just incorrect punctuation to be caught by our caller. Note
1137 -- that the case of an operator symbol or string literal is also an
1138 -- error, but that is an error that we catch here. If the result is
1139 -- True, a real dot has been scanned and we are positioned past it,
1140 -- if the result is False, the scan position is unchanged.
1142 --------------
1143 -- Real_Dot --
1144 --------------
1146 function Real_Dot return Boolean is
1147 Scan_State : Saved_Scan_State;
1149 begin
1150 if Token /= Tok_Dot then
1151 return False;
1153 else
1154 Save_Scan_State (Scan_State);
1155 Scan; -- past dot
1157 if Token = Tok_Identifier
1158 or else Token = Tok_Operator_Symbol
1159 or else Token = Tok_String_Literal
1160 then
1161 return True;
1163 else
1164 Restore_Scan_State (Scan_State);
1165 return False;
1166 end if;
1167 end if;
1168 end Real_Dot;
1170 -- Start of processing for P_Designator
1172 begin
1173 Ident_Node := Token_Node;
1174 Scan; -- past initial token
1176 if Prev_Token = Tok_Operator_Symbol
1177 or else Prev_Token = Tok_String_Literal
1178 or else not Real_Dot
1179 then
1180 return Ident_Node;
1182 -- Child name case
1184 else
1185 Prefix_Node := Ident_Node;
1187 -- Loop through child names, on entry to this loop, Prefix contains
1188 -- the name scanned so far, and Ident_Node is the last identifier.
1190 loop
1191 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1192 Set_Prefix (Name_Node, Prefix_Node);
1193 Ident_Node := P_Identifier;
1194 Set_Selector_Name (Name_Node, Ident_Node);
1195 Prefix_Node := Name_Node;
1196 exit when not Real_Dot;
1197 end loop;
1199 -- On exit from the loop, Ident_Node is the last identifier scanned,
1200 -- i.e. the defining identifier, and Prefix_Node is a node for the
1201 -- entire name, structured (incorrectly) as a selected component.
1203 Name_Node := Prefix (Prefix_Node);
1204 Change_Node (Prefix_Node, N_Designator);
1205 Set_Name (Prefix_Node, Name_Node);
1206 Set_Identifier (Prefix_Node, Ident_Node);
1207 return Prefix_Node;
1208 end if;
1210 exception
1211 when Error_Resync =>
1212 while Token = Tok_Dot or else Token = Tok_Identifier loop
1213 Scan;
1214 end loop;
1216 return Error;
1217 end P_Designator;
1219 ------------------------------
1220 -- 6.1 Defining Designator --
1221 ------------------------------
1223 -- DEFINING_DESIGNATOR ::=
1224 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1226 -- Error recovery: cannot raise Error_Resync
1228 function P_Defining_Designator return Node_Id is
1229 begin
1230 if Token = Tok_Operator_Symbol then
1231 return P_Defining_Operator_Symbol;
1233 elsif Token = Tok_String_Literal then
1234 Error_Msg_SC ("invalid operator name");
1235 Scan; -- past junk string
1236 return Error;
1238 else
1239 return P_Defining_Program_Unit_Name;
1240 end if;
1241 end P_Defining_Designator;
1243 -------------------------------------
1244 -- 6.1 Defining Program Unit Name --
1245 -------------------------------------
1247 -- DEFINING_PROGRAM_UNIT_NAME ::=
1248 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1250 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1252 -- Error recovery: cannot raise Error_Resync
1254 function P_Defining_Program_Unit_Name return Node_Id is
1255 Ident_Node : Node_Id;
1256 Name_Node : Node_Id;
1257 Prefix_Node : Node_Id;
1259 begin
1260 -- Set identifier casing if not already set and scan initial identifier
1262 if Token = Tok_Identifier
1263 and then Identifier_Casing (Current_Source_File) = Unknown
1264 then
1265 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1266 end if;
1268 Ident_Node := P_Identifier (C_Dot);
1269 Merge_Identifier (Ident_Node, Tok_Return);
1271 -- Normal case (not child library unit name)
1273 if Token /= Tok_Dot then
1274 Change_Identifier_To_Defining_Identifier (Ident_Node);
1275 Warn_If_Standard_Redefinition (Ident_Node);
1276 return Ident_Node;
1278 -- Child library unit name case
1280 else
1281 if Scope.Last > 1 then
1282 Error_Msg_SP ("child unit allowed only at library level");
1283 raise Error_Resync;
1285 elsif Ada_Version = Ada_83 then
1286 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1288 end if;
1290 Prefix_Node := Ident_Node;
1292 -- Loop through child names, on entry to this loop, Prefix contains
1293 -- the name scanned so far, and Ident_Node is the last identifier.
1295 loop
1296 exit when Token /= Tok_Dot;
1297 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1298 Scan; -- past period
1299 Set_Prefix (Name_Node, Prefix_Node);
1300 Ident_Node := P_Identifier (C_Dot);
1301 Set_Selector_Name (Name_Node, Ident_Node);
1302 Prefix_Node := Name_Node;
1303 end loop;
1305 -- On exit from the loop, Ident_Node is the last identifier scanned,
1306 -- i.e. the defining identifier, and Prefix_Node is a node for the
1307 -- entire name, structured (incorrectly) as a selected component.
1309 Name_Node := Prefix (Prefix_Node);
1310 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1311 Set_Name (Prefix_Node, Name_Node);
1312 Change_Identifier_To_Defining_Identifier (Ident_Node);
1313 Warn_If_Standard_Redefinition (Ident_Node);
1314 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1316 -- All set with unit name parsed
1318 return Prefix_Node;
1319 end if;
1321 exception
1322 when Error_Resync =>
1323 while Token = Tok_Dot or else Token = Tok_Identifier loop
1324 Scan;
1325 end loop;
1327 return Error;
1328 end P_Defining_Program_Unit_Name;
1330 --------------------------
1331 -- 6.1 Operator Symbol --
1332 --------------------------
1334 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1336 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1338 -----------------------------------
1339 -- 6.1 Defining Operator Symbol --
1340 -----------------------------------
1342 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1344 -- The caller has checked that the initial symbol is an operator symbol
1346 function P_Defining_Operator_Symbol return Node_Id is
1347 Op_Node : Node_Id;
1349 begin
1350 Op_Node := Token_Node;
1351 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1352 Scan; -- past operator symbol
1353 return Op_Node;
1354 end P_Defining_Operator_Symbol;
1356 ----------------------------
1357 -- 6.1 Parameter_Profile --
1358 ----------------------------
1360 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1362 -- Empty is returned if no formal part is present
1364 -- Error recovery: cannot raise Error_Resync
1366 function P_Parameter_Profile return List_Id is
1367 begin
1368 if Token = Tok_Left_Paren then
1369 Scan; -- part left paren
1370 return P_Formal_Part;
1371 else
1372 return No_List;
1373 end if;
1374 end P_Parameter_Profile;
1376 ---------------------------------------
1377 -- 6.1 Parameter And Result Profile --
1378 ---------------------------------------
1380 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1381 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1383 ----------------------
1384 -- 6.1 Formal part --
1385 ----------------------
1387 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1389 -- PARAMETER_SPECIFICATION ::=
1390 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1391 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1392 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1393 -- [:= DEFAULT_EXPRESSION]
1395 -- This scans the construct Formal_Part. The caller has already checked
1396 -- that the initial token is a left parenthesis, and skipped past it, so
1397 -- that on entry Token is the first token following the left parenthesis.
1399 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1401 -- Error recovery: cannot raise Error_Resync
1403 function P_Formal_Part return List_Id is
1404 Specification_List : List_Id;
1405 Specification_Node : Node_Id;
1406 Scan_State : Saved_Scan_State;
1407 Num_Idents : Nat;
1408 Ident : Nat;
1409 Ident_Sloc : Source_Ptr;
1410 Not_Null_Present : Boolean := False;
1411 Not_Null_Sloc : Source_Ptr;
1413 Idents : array (Int range 1 .. 4096) of Entity_Id;
1414 -- This array holds the list of defining identifiers. The upper bound
1415 -- of 4096 is intended to be essentially infinite, and we do not even
1416 -- bother to check for it being exceeded.
1418 begin
1419 Specification_List := New_List;
1420 Specification_Loop : loop
1421 begin
1422 if Token = Tok_Pragma then
1423 Error_Msg_SC ("pragma not allowed in formal part");
1424 Discard_Junk_Node (P_Pragma (Skipping => True));
1425 end if;
1427 Ignore (Tok_Left_Paren);
1428 Ident_Sloc := Token_Ptr;
1429 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1430 Num_Idents := 1;
1432 Ident_Loop : loop
1433 exit Ident_Loop when Token = Tok_Colon;
1435 -- The only valid tokens are colon and comma, so if we have
1436 -- neither do a bit of investigation to see which is the
1437 -- better choice for insertion.
1439 if Token /= Tok_Comma then
1441 -- Assume colon if ALIASED, IN or OUT keyword found
1443 exit Ident_Loop when Token = Tok_Aliased or else
1444 Token = Tok_In or else
1445 Token = Tok_Out;
1447 -- Otherwise scan ahead
1449 Save_Scan_State (Scan_State);
1450 Look_Ahead : loop
1452 -- If we run into a semicolon, then assume that a
1453 -- colon was missing, e.g. Parms (X Y; ...). Also
1454 -- assume missing colon on EOF (a real disaster)
1455 -- and on a right paren, e.g. Parms (X Y), and also
1456 -- on an assignment symbol, e.g. Parms (X Y := ..)
1458 if Token = Tok_Semicolon
1459 or else Token = Tok_Right_Paren
1460 or else Token = Tok_EOF
1461 or else Token = Tok_Colon_Equal
1462 then
1463 Restore_Scan_State (Scan_State);
1464 exit Ident_Loop;
1466 -- If we run into a colon, assume that we had a missing
1467 -- comma, e.g. Parms (A B : ...). Also assume a missing
1468 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1470 elsif Token = Tok_Colon
1471 or else Token = Tok_Comma
1472 then
1473 Restore_Scan_State (Scan_State);
1474 exit Look_Ahead;
1475 end if;
1477 Scan;
1478 end loop Look_Ahead;
1479 end if;
1481 -- Here if a comma is present, or to be assumed
1483 T_Comma;
1484 Num_Idents := Num_Idents + 1;
1485 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1486 end loop Ident_Loop;
1488 -- Fall through the loop on encountering a colon, or deciding
1489 -- that there is a missing colon.
1491 T_Colon;
1493 -- If there are multiple identifiers, we repeatedly scan the
1494 -- type and initialization expression information by resetting
1495 -- the scan pointer (so that we get completely separate trees
1496 -- for each occurrence).
1498 if Num_Idents > 1 then
1499 Save_Scan_State (Scan_State);
1500 end if;
1502 -- Loop through defining identifiers in list
1504 Ident := 1;
1506 Ident_List_Loop : loop
1507 Specification_Node :=
1508 New_Node (N_Parameter_Specification, Ident_Sloc);
1509 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1511 -- Scan possible ALIASED for Ada 2012 (AI-142)
1513 if Token = Tok_Aliased then
1514 if Ada_Version < Ada_2012 then
1515 Error_Msg_Ada_2012_Feature
1516 ("ALIASED parameter", Token_Ptr);
1517 else
1518 Set_Aliased_Present (Specification_Node);
1519 end if;
1521 Scan; -- past ALIASED
1522 end if;
1524 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1526 Not_Null_Sloc := Token_Ptr;
1527 Not_Null_Present :=
1528 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1530 -- Case of ACCESS keyword present
1532 if Token = Tok_Access then
1533 Set_Null_Exclusion_Present
1534 (Specification_Node, Not_Null_Present);
1536 if Ada_Version = Ada_83 then
1537 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1538 end if;
1540 Set_Parameter_Type
1541 (Specification_Node,
1542 P_Access_Definition (Not_Null_Present));
1544 -- Case of IN or OUT present
1546 else
1547 if Token = Tok_In or else Token = Tok_Out then
1548 if Not_Null_Present then
1549 Error_Msg
1550 ("`NOT NULL` can only be used with `ACCESS`",
1551 Not_Null_Sloc);
1553 if Token = Tok_In then
1554 Error_Msg
1555 ("\`IN` not allowed together with `ACCESS`",
1556 Not_Null_Sloc);
1557 else
1558 Error_Msg
1559 ("\`OUT` not allowed together with `ACCESS`",
1560 Not_Null_Sloc);
1561 end if;
1562 end if;
1564 P_Mode (Specification_Node);
1565 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1566 end if;
1568 Set_Null_Exclusion_Present
1569 (Specification_Node, Not_Null_Present);
1571 if Token = Tok_Procedure
1572 or else
1573 Token = Tok_Function
1574 then
1575 Error_Msg_SC ("formal subprogram parameter not allowed");
1576 Scan;
1578 if Token = Tok_Left_Paren then
1579 Discard_Junk_List (P_Formal_Part);
1580 end if;
1582 if Token = Tok_Return then
1583 Scan;
1584 Discard_Junk_Node (P_Subtype_Mark);
1585 end if;
1587 Set_Parameter_Type (Specification_Node, Error);
1589 else
1590 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1591 No_Constraint;
1592 end if;
1593 end if;
1595 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1597 if Ident > 1 then
1598 Set_Prev_Ids (Specification_Node, True);
1599 end if;
1601 if Ident < Num_Idents then
1602 Set_More_Ids (Specification_Node, True);
1603 end if;
1605 Append (Specification_Node, Specification_List);
1606 exit Ident_List_Loop when Ident = Num_Idents;
1607 Ident := Ident + 1;
1608 Restore_Scan_State (Scan_State);
1609 end loop Ident_List_Loop;
1611 exception
1612 when Error_Resync =>
1613 Resync_Semicolon_List;
1614 end;
1616 if Token = Tok_Semicolon then
1617 Save_Scan_State (Scan_State);
1618 Scan; -- past semicolon
1620 -- If we have RETURN or IS after the semicolon, then assume
1621 -- that semicolon should have been a right parenthesis and exit
1623 if Token = Tok_Is or else Token = Tok_Return then
1624 Error_Msg_SP -- CODEFIX
1625 ("|"";"" should be "")""");
1626 exit Specification_Loop;
1627 end if;
1629 -- If we have a declaration keyword after the semicolon, then
1630 -- assume we had a missing right parenthesis and terminate list
1632 if Token in Token_Class_Declk then
1633 Error_Msg_AP -- CODEFIX
1634 ("missing "")""");
1635 Restore_Scan_State (Scan_State);
1636 exit Specification_Loop;
1637 end if;
1639 elsif Token = Tok_Right_Paren then
1640 Scan; -- past right paren
1641 exit Specification_Loop;
1643 -- Support for aspects on formal parameters is a GNAT extension for
1644 -- the time being.
1646 elsif Token = Tok_With then
1647 Error_Msg_Ada_2022_Feature
1648 ("aspect on formal parameter", Token_Ptr);
1650 P_Aspect_Specifications (Specification_Node, False);
1652 if Token = Tok_Right_Paren then
1653 Scan; -- past right paren
1654 exit Specification_Loop;
1656 elsif Token = Tok_Semicolon then
1657 Save_Scan_State (Scan_State);
1658 Scan; -- past semicolon
1659 end if;
1661 -- Special check for common error of using comma instead of semicolon
1663 elsif Token = Tok_Comma then
1664 T_Semicolon;
1666 -- Special check for omitted separator
1668 elsif Token = Tok_Identifier then
1669 T_Semicolon;
1671 -- If nothing sensible, skip to next semicolon or right paren
1673 else
1674 T_Semicolon;
1675 Resync_Semicolon_List;
1677 if Token = Tok_Semicolon then
1678 Scan; -- past semicolon
1679 else
1680 T_Right_Paren;
1681 exit Specification_Loop;
1682 end if;
1683 end if;
1684 end loop Specification_Loop;
1686 return Specification_List;
1687 end P_Formal_Part;
1689 ----------------------------------
1690 -- 6.1 Parameter Specification --
1691 ----------------------------------
1693 -- Parsed by P_Formal_Part (6.1)
1695 ---------------
1696 -- 6.1 Mode --
1697 ---------------
1699 -- MODE ::= [in] | in out | out
1701 -- There is no explicit node in the tree for the Mode. Instead the
1702 -- In_Present and Out_Present flags are set in the parent node to
1703 -- record the presence of keywords specifying the mode.
1705 -- Error_Recovery: cannot raise Error_Resync
1707 procedure P_Mode (Node : Node_Id) is
1708 begin
1709 if Token = Tok_In then
1710 Scan; -- past IN
1711 Set_In_Present (Node, True);
1713 if Style.Mode_In_Check and then Token /= Tok_Out then
1714 Error_Msg_SP -- CODEFIX
1715 ("(style) IN should be omitted");
1716 end if;
1718 -- Since Ada 2005, formal objects can have an anonymous access type,
1719 -- and of course carry a mode indicator.
1721 if Token = Tok_Access
1722 and then Nkind (Node) /= N_Formal_Object_Declaration
1723 then
1724 Error_Msg_SP ("IN not allowed together with ACCESS");
1725 Scan; -- past ACCESS
1726 end if;
1727 end if;
1729 if Token = Tok_Out then
1730 Scan; -- past OUT
1731 Set_Out_Present (Node, True);
1732 end if;
1734 if Token = Tok_In then
1735 Error_Msg_SC ("IN must precede OUT in parameter mode");
1736 Scan; -- past IN
1737 Set_In_Present (Node, True);
1738 end if;
1739 end P_Mode;
1741 --------------------------
1742 -- 6.3 Subprogram Body --
1743 --------------------------
1745 -- Parsed by P_Subprogram (6.1)
1747 -----------------------------------
1748 -- 6.4 Procedure Call Statement --
1749 -----------------------------------
1751 -- Parsed by P_Sequence_Of_Statements (5.1)
1753 ------------------------
1754 -- 6.4 Function Call --
1755 ------------------------
1757 -- Parsed by P_Name (4.1)
1759 --------------------------------
1760 -- 6.4 Actual Parameter Part --
1761 --------------------------------
1763 -- Parsed by P_Name (4.1)
1765 --------------------------------
1766 -- 6.4 Parameter Association --
1767 --------------------------------
1769 -- Parsed by P_Name (4.1)
1771 ------------------------------------
1772 -- 6.4 Explicit Actual Parameter --
1773 ------------------------------------
1775 -- Parsed by P_Name (4.1)
1777 ---------------------------
1778 -- 6.5 Return Statement --
1779 ---------------------------
1781 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1783 -- EXTENDED_RETURN_STATEMENT ::=
1784 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1785 -- [:= EXPRESSION]
1786 -- [ASPECT_SPECIFICATION] [do
1787 -- HANDLED_SEQUENCE_OF_STATEMENTS
1788 -- end return];
1790 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1792 -- RETURN_STATEMENT ::= return [EXPRESSION];
1794 -- Error recovery: can raise Error_Resync
1796 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1798 -- Note: We don't need to check Ada_Version here, because this is
1799 -- only called in >= Ada 2005 cases anyway.
1801 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1803 begin
1804 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1806 if Token = Tok_Access then
1807 Set_Object_Definition
1808 (Decl_Node, P_Access_Definition (Not_Null_Present));
1809 else
1810 Set_Object_Definition
1811 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1812 end if;
1813 end P_Return_Subtype_Indication;
1815 -- Error recovery: can raise Error_Resync
1817 function P_Return_Object_Declaration return Node_Id is
1818 Return_Obj : Node_Id;
1819 Decl_Node : Node_Id;
1821 begin
1822 Return_Obj := Token_Node;
1823 Change_Identifier_To_Defining_Identifier (Return_Obj);
1824 Warn_If_Standard_Redefinition (Return_Obj);
1825 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1826 Set_Defining_Identifier (Decl_Node, Return_Obj);
1828 Scan; -- past identifier
1829 Scan; -- past :
1831 -- First an error check, if we have two identifiers in a row, a likely
1832 -- possibility is that the first of the identifiers is an incorrectly
1833 -- spelled keyword. See similar check in P_Identifier_Declarations.
1835 if Token = Tok_Identifier then
1836 declare
1837 SS : Saved_Scan_State;
1838 I2 : Boolean;
1840 begin
1841 Save_Scan_State (SS);
1842 Scan; -- past initial identifier
1843 I2 := (Token = Tok_Identifier);
1844 Restore_Scan_State (SS);
1846 if I2
1847 and then
1848 (Bad_Spelling_Of (Tok_Access) or else
1849 Bad_Spelling_Of (Tok_Aliased) or else
1850 Bad_Spelling_Of (Tok_Constant))
1851 then
1852 null;
1853 end if;
1854 end;
1855 end if;
1857 -- We allow "constant" here (as in "return Result : constant
1858 -- T..."). This is not in the latest RM, but the ARG is considering an
1859 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1861 if Token = Tok_Constant then
1862 Scan; -- past CONSTANT
1863 Set_Constant_Present (Decl_Node);
1865 if Token = Tok_Aliased then
1866 Error_Msg_SC -- CODEFIX
1867 ("ALIASED should be before CONSTANT");
1868 Scan; -- past ALIASED
1869 Set_Aliased_Present (Decl_Node);
1870 end if;
1872 elsif Token = Tok_Aliased then
1873 Scan; -- past ALIASED
1874 Set_Aliased_Present (Decl_Node);
1876 -- The restrictions on the use of aliased in an extended return
1877 -- are semantic, not syntactic.
1879 if Token = Tok_Constant then
1880 Scan; -- past CONSTANT
1881 Set_Constant_Present (Decl_Node);
1882 end if;
1883 end if;
1885 P_Return_Subtype_Indication (Decl_Node);
1887 if Token = Tok_Colon_Equal then
1888 Scan; -- past :=
1889 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1890 Set_Has_Init_Expression (Decl_Node);
1891 end if;
1893 return Decl_Node;
1894 end P_Return_Object_Declaration;
1896 -- Error recovery: can raise Error_Resync
1898 function P_Return_Statement return Node_Id is
1899 -- The caller has checked that the initial token is RETURN
1901 function Is_Extended return Boolean;
1902 -- Scan state is just after RETURN (and is left that way). Determine
1903 -- whether this is a simple or extended return statement by looking
1904 -- ahead for "identifier :", which implies extended.
1906 -----------------
1907 -- Is_Extended --
1908 -----------------
1910 function Is_Extended return Boolean is
1911 Scan_State : Saved_Scan_State;
1912 Is_Extended : Boolean := False;
1914 begin
1916 if Token = Tok_Identifier then
1917 Save_Scan_State (Scan_State); -- at identifier
1918 Scan; -- past identifier
1920 if Token = Tok_Colon then
1921 Is_Extended := True;
1922 end if;
1924 Restore_Scan_State (Scan_State); -- to identifier
1925 end if;
1927 return Is_Extended;
1928 end Is_Extended;
1930 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1931 Ret_Strt : constant Column_Number := Start_Column;
1932 Ret_Node : Node_Id;
1933 Decl : Node_Id;
1935 -- Start of processing for P_Return_Statement
1937 begin
1938 Scan; -- past RETURN
1940 -- Simple_return_statement, no expression, return an
1941 -- N_Simple_Return_Statement node with the expression field left Empty.
1943 if Token = Tok_Semicolon then
1944 Scan; -- past ;
1945 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1947 -- Nontrivial case
1949 else
1950 -- Extended_return_statement (Ada 2005 only -- AI-318):
1952 if Is_Extended then
1953 Error_Msg_Ada_2005_Extension ("extended return statement");
1955 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1956 Decl := P_Return_Object_Declaration;
1957 Set_Return_Object_Declarations (Ret_Node, New_List (Decl));
1959 if Token = Tok_With then
1960 P_Aspect_Specifications (Decl, False);
1961 end if;
1963 if Token = Tok_Do then
1964 Push_Scope_Stack;
1965 Scopes (Scope.Last).Ecol := Ret_Strt;
1966 Scopes (Scope.Last).Etyp := E_Return;
1967 Scopes (Scope.Last).Labl := Error;
1968 Scopes (Scope.Last).Sloc := Ret_Sloc;
1969 Scan; -- past DO
1970 Set_Handled_Statement_Sequence
1971 (Ret_Node, P_Handled_Sequence_Of_Statements);
1972 End_Statements;
1974 -- Do we need to handle Error_Resync here???
1975 end if;
1977 -- Simple_return_statement or Return_when_Statement
1978 -- with expression.
1980 -- We avoid trying to scan an expression if we are at an
1981 -- expression terminator since in that case the best error
1982 -- message is probably that we have a missing semicolon.
1984 else
1985 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1987 if Token not in Token_Class_Eterm then
1988 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1989 end if;
1991 -- When the next token is WHEN or IF we know that we are looking
1992 -- at a Return_when_statement
1994 if Token = Tok_When and then not Missing_Semicolon_On_When then
1995 Error_Msg_GNAT_Extension ("return when statement");
1996 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
1998 Scan; -- past WHEN
1999 Set_Condition (Ret_Node, P_Condition);
2001 -- Allow IF instead of WHEN, giving error message
2003 elsif Token = Tok_If then
2004 Error_Msg_GNAT_Extension ("return when statement");
2005 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
2007 T_When;
2008 Scan; -- past IF used in place of WHEN
2009 Set_Condition (Ret_Node, P_Condition);
2010 end if;
2011 end if;
2013 TF_Semicolon;
2014 end if;
2016 return Ret_Node;
2017 end P_Return_Statement;
2019 end Ch6;