Skip various cmp-mem-const tests on lp64 hppa*-*-*
[official-gcc.git] / gcc / ada / par-ch6.adb
blobf041bbb2e49b151e6b2a5d4e50e01a5192678b0a
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-2023, 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 caller has checked that the initial token is FUNCTION, PROCEDURE,
184 -- NOT or OVERRIDING.
186 -- Error recovery: cannot raise Error_Resync
188 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
190 function Contains_Import_Aspect (Aspects : List_Id) return Boolean;
191 -- Return True if Aspects contains an Import aspect.
193 ----------------------------
194 -- Contains_Import_Aspect --
195 ----------------------------
197 function Contains_Import_Aspect (Aspects : List_Id) return Boolean is
198 Aspect : Node_Id := First (Aspects);
199 begin
200 while Present (Aspect) loop
201 if Chars (Identifier (Aspect)) = Name_Import then
202 return True;
203 end if;
205 Next (Aspect);
206 end loop;
208 return False;
209 end Contains_Import_Aspect;
211 Specification_Node : Node_Id;
212 Name_Node : Node_Id;
213 Aspects : List_Id;
214 Fpart_List : List_Id;
215 Fpart_Sloc : Source_Ptr;
216 Result_Not_Null : Boolean := False;
217 Result_Node : Node_Id;
218 Inst_Node : Node_Id;
219 Body_Node : Node_Id;
220 Decl_Node : Node_Id;
221 Rename_Node : Node_Id;
222 Absdec_Node : Node_Id;
223 Stub_Node : Node_Id;
224 Fproc_Sloc : Source_Ptr;
225 Func : Boolean;
226 Scan_State : Saved_Scan_State;
228 -- Flags for optional overriding indication. Two flags are needed,
229 -- to distinguish positive and negative overriding indicators from
230 -- the absence of any indicator.
232 Is_Overriding : Boolean := False;
233 Not_Overriding : Boolean := False;
235 begin
236 -- Set up scope stack entry. Note that the Labl field will be set later
238 SIS_Entry_Active := False;
239 SIS_Aspect_Import_Seen := False;
240 SIS_Missing_Semicolon_Message := No_Error_Msg;
241 Push_Scope_Stack;
242 Scopes (Scope.Last).Sloc := Token_Ptr;
243 Scopes (Scope.Last).Etyp := E_Name;
244 Scopes (Scope.Last).Ecol := Start_Column;
245 Scopes (Scope.Last).Lreq := False;
247 Aspects := Empty_List;
249 -- Ada 2005: Scan leading NOT OVERRIDING indicator
251 if Token = Tok_Not then
252 Scan; -- past NOT
254 if Token = Tok_Overriding then
255 Scan; -- past OVERRIDING
256 Not_Overriding := True;
258 -- Overriding keyword used in non Ada 2005 mode
260 elsif Token = Tok_Identifier
261 and then Token_Name = Name_Overriding
262 then
263 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
264 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
265 Scan; -- past Overriding
266 Not_Overriding := True;
268 else
269 Error_Msg_SC -- CODEFIX
270 ("OVERRIDING expected!");
271 end if;
273 -- Ada 2005: scan leading OVERRIDING indicator
275 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
276 -- declaration circuit already gave an error message and changed the
277 -- token to Tok_Overriding.
279 elsif Token = Tok_Overriding then
280 Scan; -- past OVERRIDING
281 Is_Overriding := True;
282 end if;
284 if Is_Overriding or else Not_Overriding then
286 -- Note that if we are not in Ada_2005 mode, error messages have
287 -- already been given, so no need to give another message here.
289 -- An overriding indicator is allowed for subprogram declarations,
290 -- bodies (including subunits), renamings, stubs, and instantiations.
291 -- The test against Pf_Decl_Pbod is added to account for the case of
292 -- subprograms declared in a protected type, where only subprogram
293 -- declarations and bodies can occur. The Pf_Pbod case is for
294 -- subunits.
296 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
297 and then
298 Pf_Flags /= Pf_Decl_Pbod_Pexp
299 and then
300 Pf_Flags /= Pf_Pbod_Pexp
301 then
302 Error_Msg_SC ("overriding indicator not allowed here!");
304 elsif Token not in Tok_Function | Tok_Procedure then
305 Error_Msg_SC -- CODEFIX
306 ("FUNCTION or PROCEDURE expected!");
307 end if;
308 end if;
310 Func := (Token = Tok_Function);
311 Fproc_Sloc := Token_Ptr;
312 Scan; -- past FUNCTION or PROCEDURE
313 Ignore (Tok_Type);
314 Ignore (Tok_Body);
316 if Func then
317 Name_Node := P_Defining_Designator;
319 if Nkind (Name_Node) = N_Defining_Operator_Symbol
320 and then Scope.Last = 1
321 then
322 Error_Msg_SP ("operator symbol not allowed at library level");
323 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
325 -- Set name from file name, we need some junk name, and that's
326 -- as good as anything. This is only approximate, since we do
327 -- not do anything with non-standard name translations.
329 Get_Name_String (File_Name (Current_Source_File));
331 for J in 1 .. Name_Len loop
332 if Name_Buffer (J) = '.' then
333 Name_Len := J - 1;
334 exit;
335 end if;
336 end loop;
338 Set_Chars (Name_Node, Name_Find);
339 Set_Error_Posted (Name_Node);
340 end if;
342 else
343 Name_Node := P_Defining_Program_Unit_Name;
344 end if;
346 Scopes (Scope.Last).Labl := Name_Node;
347 Current_Node := Name_Node;
348 Ignore (Tok_Colon);
350 -- Deal with generic instantiation, the one case in which we do not
351 -- have a subprogram specification as part of whatever we are parsing
353 if Token = Tok_Is then
354 Save_Scan_State (Scan_State); -- at the IS
355 T_Is; -- checks for redundant IS
357 if Token = Tok_New then
358 if not Pf_Flags.Gins then
359 Error_Msg_SC ("generic instantiation not allowed here!");
360 end if;
362 Scan; -- past NEW
364 if Func then
365 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
366 Set_Name (Inst_Node, P_Function_Name);
367 else
368 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
369 Set_Name (Inst_Node, P_Qualified_Simple_Name);
370 end if;
372 Set_Defining_Unit_Name (Inst_Node, Name_Node);
373 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
374 P_Aspect_Specifications (Inst_Node);
375 Pop_Scope_Stack; -- Don't need scope stack entry in this case
377 if Is_Overriding then
378 Set_Must_Override (Inst_Node);
380 elsif Not_Overriding then
381 Set_Must_Not_Override (Inst_Node);
382 end if;
384 return Inst_Node;
386 else
387 Restore_Scan_State (Scan_State); -- to the IS
388 end if;
389 end if;
391 -- If not a generic instantiation, then we definitely have a subprogram
392 -- specification (all possibilities at this stage include one here)
394 Fpart_Sloc := Token_Ptr;
396 Check_Misspelling_Of (Tok_Return);
398 -- Scan formal part. First a special error check. If we have an
399 -- identifier here, then we have a definite error. If this identifier
400 -- is on the same line as the designator, then we assume it is the
401 -- first formal after a missing left parenthesis
403 if Token = Tok_Identifier
404 and then not Token_Is_At_Start_Of_Line
405 then
406 T_Left_Paren; -- to generate message
407 Fpart_List := P_Formal_Part;
409 -- Otherwise scan out an optional formal part in the usual manner
411 else
412 Fpart_List := P_Parameter_Profile;
413 end if;
415 -- We treat what we have as a function specification if FUNCTION was
416 -- used, or if a RETURN is present. This gives better error recovery
417 -- since later RETURN statements will be valid in either case.
419 Check_Junk_Semicolon_Before_Return;
420 Result_Node := Error;
422 if Token = Tok_Return then
423 if not Func then
424 Error_Msg -- CODEFIX
425 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
426 Func := True;
427 end if;
429 Scan; -- past RETURN
431 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
433 -- Ada 2005 (AI-318-02)
435 if Token = Tok_Access then
436 Error_Msg_Ada_2005_Extension ("anonymous access result type");
438 Result_Node := P_Access_Definition (Result_Not_Null);
440 else
441 Result_Node := P_Subtype_Mark;
442 No_Constraint_Maybe_Expr_Func;
443 end if;
445 else
446 -- Skip extra parenthesis at end of formal part
448 Ignore (Tok_Right_Paren);
450 -- For function, scan result subtype
452 if Func then
453 TF_Return;
455 if Prev_Token = Tok_Return then
456 Result_Node := P_Subtype_Mark;
457 end if;
458 end if;
459 end if;
461 if Func then
462 Specification_Node :=
463 New_Node (N_Function_Specification, Fproc_Sloc);
465 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
466 Set_Result_Definition (Specification_Node, Result_Node);
468 else
469 Specification_Node :=
470 New_Node (N_Procedure_Specification, Fproc_Sloc);
471 end if;
473 Set_Defining_Unit_Name (Specification_Node, Name_Node);
474 Set_Parameter_Specifications (Specification_Node, Fpart_List);
476 if Is_Overriding then
477 Set_Must_Override (Specification_Node);
479 elsif Not_Overriding then
480 Set_Must_Not_Override (Specification_Node);
481 end if;
483 -- Error check: barriers not allowed on protected functions/procedures
485 if Token = Tok_When then
486 if Func then
487 Error_Msg_SC ("barrier not allowed on function, only on entry");
488 else
489 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
490 end if;
492 Scan; -- past WHEN
493 Discard_Junk_Node (P_Expression);
494 end if;
496 -- Deal with semicolon followed by IS. We want to treat this as IS
498 if Token = Tok_Semicolon then
499 Save_Scan_State (Scan_State);
500 Scan; -- past semicolon
502 if Token = Tok_Is then
503 Error_Msg_SP -- CODEFIX
504 ("extra "";"" ignored");
505 else
506 Restore_Scan_State (Scan_State);
507 end if;
508 end if;
510 -- Subprogram declaration ended by aspect specifications
512 if Aspect_Specifications_Present then
513 goto Subprogram_Declaration;
515 -- Deal with case of semicolon ending a subprogram declaration
517 elsif Token = Tok_Semicolon then
518 if not Pf_Flags.Decl then
519 T_Is;
520 end if;
522 Save_Scan_State (Scan_State);
523 Scan; -- past semicolon
525 -- If semicolon is immediately followed by IS, then ignore the
526 -- semicolon, and go process the body.
528 if Token = Tok_Is then
529 Error_Msg_SP -- CODEFIX
530 ("|extra "";"" ignored");
531 T_Is; -- scan past IS
532 goto Subprogram_Body;
534 -- If BEGIN follows in an appropriate column, we immediately
535 -- commence the error action of assuming that the previous
536 -- subprogram declaration should have been a subprogram body,
537 -- i.e. that the terminating semicolon should have been IS.
539 elsif Token = Tok_Begin
540 and then Start_Column >= Scopes (Scope.Last).Ecol
541 then
542 Error_Msg_SP -- CODEFIX
543 ("|"";"" should be IS!");
544 goto Subprogram_Body;
546 else
547 Restore_Scan_State (Scan_State);
548 goto Subprogram_Declaration;
549 end if;
551 -- Case of not followed by semicolon
553 else
554 -- Subprogram renaming declaration case
556 Check_Misspelling_Of (Tok_Renames);
558 if Token = Tok_Renames then
559 if not Pf_Flags.Rnam then
560 Error_Msg_SC ("renaming declaration not allowed here!");
561 end if;
563 Rename_Node :=
564 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
565 Scan; -- past RENAMES
566 Set_Name (Rename_Node, P_Name);
567 Set_Specification (Rename_Node, Specification_Node);
568 P_Aspect_Specifications (Rename_Node);
569 TF_Semicolon;
570 Pop_Scope_Stack;
571 return Rename_Node;
573 -- Case of IS following subprogram specification
575 elsif Token = Tok_Is then
576 T_Is; -- ignore redundant Is's
578 if Token_Name = Name_Abstract then
579 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
580 end if;
582 -- Deal nicely with (now obsolete) use of <> in place of abstract
584 if Token = Tok_Box then
585 Error_Msg_SC -- CODEFIX
586 ("ABSTRACT expected");
587 Token := Tok_Abstract;
588 end if;
590 -- Abstract subprogram declaration case
592 if Token = Tok_Abstract then
593 Absdec_Node :=
594 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
595 Set_Specification (Absdec_Node, Specification_Node);
596 Pop_Scope_Stack; -- discard unneeded entry
597 Scan; -- past ABSTRACT
598 P_Aspect_Specifications (Absdec_Node);
599 return Absdec_Node;
601 -- Ada 2005 (AI-248): Parse a null procedure declaration
603 elsif Token = Tok_Null then
604 Error_Msg_Ada_2005_Extension ("null procedure");
606 Scan; -- past NULL
608 if Func then
609 Error_Msg_SP ("only procedures can be null");
610 else
611 Set_Null_Present (Specification_Node);
612 Set_Null_Statement (Specification_Node,
613 New_Node (N_Null_Statement, Prev_Token_Ptr));
614 end if;
616 goto Subprogram_Declaration;
618 -- Check for IS NEW with Formal_Part present and handle nicely
620 elsif Token = Tok_New then
621 Error_Msg
622 ("formal part not allowed in instantiation", Fpart_Sloc);
623 Scan; -- past NEW
625 if Func then
626 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
627 else
628 Inst_Node :=
629 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
630 end if;
632 Set_Defining_Unit_Name (Inst_Node, Name_Node);
633 Set_Name (Inst_Node, P_Name);
634 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
635 TF_Semicolon;
636 Pop_Scope_Stack; -- Don't need scope stack entry in this case
637 return Inst_Node;
639 else
640 goto Subprogram_Body;
641 end if;
643 -- Aspect specifications present
645 elsif Aspect_Specifications_Present then
646 goto Subprogram_Declaration;
648 -- Here we have a missing IS or missing semicolon
650 else
651 -- If the next token is a left paren at the start of a line, then
652 -- this is almost certainly the start of the expression for an
653 -- expression function, so in this case guess a missing IS.
655 if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then
656 Error_Msg_AP -- CODEFIX
657 ("missing IS");
659 -- In all other cases, we guess a missing semicolon, since we are
660 -- good at fixing up a semicolon which should really be an IS.
662 else
663 Error_Msg_AP -- CODEFIX
664 ("|missing "";""");
665 SIS_Missing_Semicolon_Message := Get_Msg_Id;
666 goto Subprogram_Declaration;
667 end if;
668 end if;
669 end if;
671 -- Processing for stub or subprogram body or expression function
673 <<Subprogram_Body>>
675 -- Subprogram body stub case
677 if Separate_Present then
678 if not Pf_Flags.Stub then
679 Error_Msg_SC ("body stub not allowed here!");
680 end if;
682 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
683 Error_Msg
684 ("operator symbol cannot be used as subunit name",
685 Sloc (Name_Node));
686 end if;
688 Scan; -- past SEPARATE
690 Stub_Node :=
691 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
692 Set_Specification (Stub_Node, Specification_Node);
694 if Is_Non_Empty_List (Aspects) then
695 Error_Msg
696 ("aspect specifications must come after SEPARATE",
697 Sloc (First (Aspects)));
698 end if;
700 P_Aspect_Specifications (Stub_Node, Semicolon => False);
701 TF_Semicolon;
702 Pop_Scope_Stack;
703 return Stub_Node;
705 -- Subprogram body or expression function case
707 else
708 Scan_Body_Or_Expression_Function : declare
710 function Likely_Expression_Function return Boolean;
711 -- Returns True if we have a probable case of an expression
712 -- function omitting the parentheses, if so, returns True
713 -- and emits an appropriate error message, else returns False.
715 --------------------------------
716 -- Likely_Expression_Function --
717 --------------------------------
719 function Likely_Expression_Function return Boolean is
720 begin
721 -- If currently pointing to BEGIN or a declaration keyword
722 -- or a pragma, then we definitely have a subprogram body.
723 -- This is a common case, so worth testing first.
725 if Token in Tok_Begin | Token_Class_Declk | Tok_Pragma then
726 return False;
728 -- Test for tokens which could only start an expression and
729 -- thus signal the case of a expression function.
731 elsif Token in
732 Token_Class_Literal | Token_Class_Unary_Addop |
733 Tok_Left_Paren | Tok_Abs | Tok_Null | Tok_New | Tok_Not
734 then
735 null;
737 -- Anything other than an identifier must be a body
739 elsif Token /= Tok_Identifier then
740 return False;
742 -- Here for an identifier
744 else
745 -- If the identifier is the first token on its line, then
746 -- let's assume that we have a missing begin and this is
747 -- intended as a subprogram body. However, if the context
748 -- is a function and the unit is a package declaration, a
749 -- body would be illegal, so try for an unparenthesized
750 -- expression function.
752 if Token_Is_At_Start_Of_Line then
753 declare
754 -- The enclosing scope entry is a subprogram spec
756 Spec_Node : constant Node_Id :=
757 Parent
758 (Scopes (Scope.Last).Labl);
759 Lib_Node : Node_Id := Spec_Node;
761 begin
762 -- Check whether there is an enclosing scope that
763 -- is a package declaration.
765 if Scope.Last > 1 then
766 Lib_Node :=
767 Parent (Scopes (Scope.Last - 1).Labl);
768 end if;
770 if Ada_Version >= Ada_2012
771 and then
772 Nkind (Lib_Node) = N_Package_Specification
773 and then
774 Nkind (Spec_Node) = N_Function_Specification
775 then
776 null;
777 else
778 return False;
779 end if;
780 end;
782 -- Otherwise we have to scan ahead. If the identifier is
783 -- followed by a colon or a comma, it is a declaration
784 -- and hence we have a subprogram body. Otherwise assume
785 -- a expression function.
787 else
788 declare
789 Scan_State : Saved_Scan_State;
790 Tok : Token_Type;
792 begin
793 Save_Scan_State (Scan_State);
794 Scan; -- past identifier
795 Tok := Token;
796 Restore_Scan_State (Scan_State);
798 if Tok = Tok_Colon or else Tok = Tok_Comma then
799 return False;
800 end if;
801 end;
802 end if;
803 end if;
805 -- Fall through if we have a likely expression function.
806 -- If the starting keyword is not "function" the error
807 -- will be reported elsewhere.
809 if Func then
810 Error_Msg_SC
811 ("expression function must be enclosed in parentheses");
812 end if;
814 return True;
815 end Likely_Expression_Function;
817 -- Start of processing for Scan_Body_Or_Expression_Function
819 begin
820 -- Expression_Function case
822 -- If likely an aggregate, check we are in Ada 2022 mode
824 if Token = Tok_Left_Bracket then
825 Error_Msg_Ada_2022_Feature
826 ("!aggregates as expression function", Token_Ptr);
827 end if;
829 if Token in Tok_Left_Paren | Tok_Left_Bracket
830 or else Likely_Expression_Function
831 then
832 -- Check expression function allowed here
834 if not Pf_Flags.Pexp then
835 Error_Msg_SC ("expression function not allowed here!");
836 end if;
838 -- Check we are in Ada 2012 mode
840 Error_Msg_Ada_2012_Feature
841 ("!expression function", Token_Ptr);
843 -- Catch an illegal placement of the aspect specification
844 -- list:
846 -- function_specification
847 -- [aspect_specification] is (expression);
849 -- This case is correctly processed by the parser because
850 -- the expression function first appears as a subprogram
851 -- declaration to the parser. The starting keyword may
852 -- not have been "function" in which case the error is
853 -- on a malformed procedure.
855 if Is_Non_Empty_List (Aspects) then
856 if Func then
857 Error_Msg
858 ("aspect specifications must come after "
859 & "parenthesized expression",
860 Sloc (First (Aspects)));
861 else
862 Error_Msg
863 ("aspect specifications must come after subprogram "
864 & "specification", Sloc (First (Aspects)));
865 end if;
866 end if;
868 -- Parse out expression and build expression function
870 Body_Node :=
871 New_Node
872 (N_Expression_Function, Sloc (Specification_Node));
873 Set_Specification (Body_Node, Specification_Node);
875 declare
876 Expr : constant Node_Id := P_Expression;
877 begin
878 Set_Expression (Body_Node, Expr);
880 -- Check that the full expression is properly
881 -- parenthesized since we may have a left-operand that is
882 -- parenthesized but that is not one of the allowed cases
883 -- with syntactic parentheses.
885 if not (Paren_Count (Expr) /= 0
886 or else Nkind (Expr) in N_Aggregate
887 | N_Extension_Aggregate
888 | N_Quantified_Expression)
889 then
890 Error_Msg
891 ("expression function must be enclosed in "
892 & "parentheses", Sloc (Expr));
893 end if;
894 end;
896 -- Expression functions can carry pre/postconditions
898 P_Aspect_Specifications (Body_Node);
899 Pop_Scope_Stack;
901 -- Subprogram body case
903 else
904 -- Check body allowed here
906 if not Pf_Flags.Pbod then
907 Error_Msg_SP ("subprogram body not allowed here!");
908 end if;
910 -- Here is the test for a suspicious IS (i.e. one that
911 -- looks like it might more properly be a semicolon).
912 -- See separate section describing use of IS instead
913 -- of semicolon in package Parse.
915 if (Token in Token_Class_Declk
916 or else
917 Token = Tok_Identifier)
918 and then Start_Column <= Scopes (Scope.Last).Ecol
919 and then Scope.Last /= 1
920 then
921 Scopes (Scope.Last).Etyp := E_Suspicious_Is;
922 Scopes (Scope.Last).S_Is := Prev_Token_Ptr;
923 end if;
925 -- Build and return subprogram body, parsing declarations
926 -- and statement sequence that belong to the body.
928 Body_Node :=
929 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
930 Set_Specification (Body_Node, Specification_Node);
932 -- If aspects are present, the specification is parsed as
933 -- a subprogram declaration, and we jump here after seeing
934 -- the keyword IS. Attach asspects previously collected to
935 -- the body.
937 if Is_Non_Empty_List (Aspects) then
938 Set_Aspect_Specifications (Body_Node, Aspects);
939 end if;
941 Parse_Decls_Begin_End (Body_Node);
942 end if;
944 return Body_Node;
945 end Scan_Body_Or_Expression_Function;
946 end if;
948 -- Processing for subprogram declaration
950 <<Subprogram_Declaration>>
951 Decl_Node :=
952 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
953 Set_Specification (Decl_Node, Specification_Node);
954 Aspects := Get_Aspect_Specifications (Semicolon => False);
956 -- Aspects may be present on a subprogram body. The source parsed
957 -- so far is that of its specification. Go parse the body and attach
958 -- the collected aspects, if any, to the body.
960 if Token = Tok_Is then
962 -- If the subprogram is a procedure and already has a
963 -- specification, we can't define another.
965 if Nkind (Specification (Decl_Node)) = N_Procedure_Specification
966 and then Null_Present (Specification (Decl_Node))
967 then
968 Error_Msg_AP ("null procedure cannot have a body");
969 end if;
971 Scan;
972 goto Subprogram_Body;
974 else
975 if Is_Non_Empty_List (Aspects) then
976 Set_Aspect_Specifications (Decl_Node, Aspects);
977 end if;
979 TF_Semicolon;
980 end if;
982 -- If this is a context in which a subprogram body is permitted,
983 -- set active SIS entry in case (see section titled "Handling
984 -- Semicolon Used in Place of IS" in body of Parser package)
985 -- Note that SIS_Missing_Semicolon_Message is already set properly.
987 if Pf_Flags.Pbod
989 -- Disconnect this processing if we have scanned a null procedure
990 -- or an Import aspect because in this case the spec is complete
991 -- anyway with no body.
993 and then (Nkind (Specification_Node) /= N_Procedure_Specification
994 or else not Null_Present (Specification_Node))
995 and then not Contains_Import_Aspect (Aspects)
996 then
997 SIS_Labl := Scopes (Scope.Last).Labl;
998 SIS_Sloc := Scopes (Scope.Last).Sloc;
999 SIS_Ecol := Scopes (Scope.Last).Ecol;
1000 SIS_Declaration_Node := Decl_Node;
1001 SIS_Semicolon_Sloc := Prev_Token_Ptr;
1003 -- Do not activate the entry if we have "with Import"
1005 if not SIS_Aspect_Import_Seen then
1006 SIS_Entry_Active := True;
1007 end if;
1008 end if;
1010 Pop_Scope_Stack;
1011 return Decl_Node;
1012 end P_Subprogram;
1014 ---------------------------------
1015 -- 6.1 Subprogram Declaration --
1016 ---------------------------------
1018 -- Parsed by P_Subprogram (6.1)
1020 ------------------------------------------
1021 -- 6.1 Abstract Subprogram Declaration --
1022 ------------------------------------------
1024 -- Parsed by P_Subprogram (6.1)
1026 -----------------------------------
1027 -- 6.1 Subprogram Specification --
1028 -----------------------------------
1030 -- SUBPROGRAM_SPECIFICATION ::=
1031 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1032 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1034 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1036 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1038 -- Subprogram specifications that appear in subprogram declarations
1039 -- are parsed by P_Subprogram (6.1). This routine is used in other
1040 -- contexts where subprogram specifications occur.
1042 -- Note: this routine does not affect the scope stack in any way
1044 -- Error recovery: can raise Error_Resync
1046 function P_Subprogram_Specification return Node_Id is
1047 Specification_Node : Node_Id;
1048 Result_Not_Null : Boolean;
1049 Result_Node : Node_Id;
1051 begin
1052 if Token = Tok_Function then
1053 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1054 Scan; -- past FUNCTION
1055 Ignore (Tok_Body);
1056 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1057 Set_Parameter_Specifications
1058 (Specification_Node, P_Parameter_Profile);
1059 Check_Junk_Semicolon_Before_Return;
1060 TF_Return;
1062 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1064 -- Ada 2005 (AI-318-02)
1066 if Token = Tok_Access then
1067 Error_Msg_Ada_2005_Extension ("anonymous access result type");
1069 Result_Node := P_Access_Definition (Result_Not_Null);
1071 else
1072 Result_Node := P_Subtype_Mark;
1073 No_Constraint_Maybe_Expr_Func;
1074 end if;
1076 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1077 Set_Result_Definition (Specification_Node, Result_Node);
1078 return Specification_Node;
1080 elsif Token = Tok_Procedure then
1081 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1082 Scan; -- past PROCEDURE
1083 Ignore (Tok_Body);
1084 Set_Defining_Unit_Name
1085 (Specification_Node, P_Defining_Program_Unit_Name);
1086 Set_Parameter_Specifications
1087 (Specification_Node, P_Parameter_Profile);
1088 return Specification_Node;
1090 else
1091 Error_Msg_SC ("subprogram specification expected");
1092 raise Error_Resync;
1093 end if;
1094 end P_Subprogram_Specification;
1096 ---------------------
1097 -- 6.1 Designator --
1098 ---------------------
1100 -- DESIGNATOR ::=
1101 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1103 -- The caller has checked that the initial token is an identifier,
1104 -- operator symbol, or string literal. Note that we don't bother to
1105 -- do much error diagnosis in this routine, since it is only used for
1106 -- the label on END lines, and the routines in package Par.Endh will
1107 -- check that the label is appropriate.
1109 -- Error recovery: cannot raise Error_Resync
1111 function P_Designator return Node_Id is
1112 Ident_Node : Node_Id;
1113 Name_Node : Node_Id;
1114 Prefix_Node : Node_Id;
1116 function Real_Dot return Boolean;
1117 -- Tests if a current token is an interesting period, i.e. is followed
1118 -- by an identifier or operator symbol or string literal. If not, it is
1119 -- probably just incorrect punctuation to be caught by our caller. Note
1120 -- that the case of an operator symbol or string literal is also an
1121 -- error, but that is an error that we catch here. If the result is
1122 -- True, a real dot has been scanned and we are positioned past it,
1123 -- if the result is False, the scan position is unchanged.
1125 --------------
1126 -- Real_Dot --
1127 --------------
1129 function Real_Dot return Boolean is
1130 Scan_State : Saved_Scan_State;
1132 begin
1133 if Token /= Tok_Dot then
1134 return False;
1136 else
1137 Save_Scan_State (Scan_State);
1138 Scan; -- past dot
1140 if Token in
1141 Tok_Identifier | Tok_Operator_Symbol | Tok_String_Literal
1142 then
1143 return True;
1145 else
1146 Restore_Scan_State (Scan_State);
1147 return False;
1148 end if;
1149 end if;
1150 end Real_Dot;
1152 -- Start of processing for P_Designator
1154 begin
1155 Ident_Node := Token_Node;
1156 Scan; -- past initial token
1158 if Prev_Token in Tok_Operator_Symbol | Tok_String_Literal
1159 or else not Real_Dot
1160 then
1161 return Ident_Node;
1163 -- Child name case
1165 else
1166 Prefix_Node := Ident_Node;
1168 -- Loop through child names, on entry to this loop, Prefix contains
1169 -- the name scanned so far, and Ident_Node is the last identifier.
1171 loop
1172 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1173 Set_Prefix (Name_Node, Prefix_Node);
1174 Ident_Node := P_Identifier;
1175 Set_Selector_Name (Name_Node, Ident_Node);
1176 Prefix_Node := Name_Node;
1177 exit when not Real_Dot;
1178 end loop;
1180 -- On exit from the loop, Ident_Node is the last identifier scanned,
1181 -- i.e. the defining identifier, and Prefix_Node is a node for the
1182 -- entire name, structured (incorrectly) as a selected component.
1184 Name_Node := Prefix (Prefix_Node);
1185 Change_Node (Prefix_Node, N_Designator);
1186 Set_Name (Prefix_Node, Name_Node);
1187 Set_Identifier (Prefix_Node, Ident_Node);
1188 return Prefix_Node;
1189 end if;
1191 exception
1192 when Error_Resync =>
1193 while Token in Tok_Dot | Tok_Identifier loop
1194 Scan;
1195 end loop;
1197 return Error;
1198 end P_Designator;
1200 ------------------------------
1201 -- 6.1 Defining Designator --
1202 ------------------------------
1204 -- DEFINING_DESIGNATOR ::=
1205 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1207 -- Error recovery: cannot raise Error_Resync
1209 function P_Defining_Designator return Node_Id is
1210 begin
1211 if Token = Tok_Operator_Symbol then
1212 return P_Defining_Operator_Symbol;
1214 elsif Token = Tok_String_Literal then
1215 Error_Msg_SC ("invalid operator name");
1216 Scan; -- past junk string
1217 return Error;
1219 else
1220 return P_Defining_Program_Unit_Name;
1221 end if;
1222 end P_Defining_Designator;
1224 -------------------------------------
1225 -- 6.1 Defining Program Unit Name --
1226 -------------------------------------
1228 -- DEFINING_PROGRAM_UNIT_NAME ::=
1229 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1231 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1233 -- Error recovery: cannot raise Error_Resync
1235 function P_Defining_Program_Unit_Name return Node_Id is
1236 Ident_Node : Node_Id;
1237 Name_Node : Node_Id;
1238 Prefix_Node : Node_Id;
1240 begin
1241 -- Set identifier casing if not already set and scan initial identifier
1243 if Token = Tok_Identifier
1244 and then Identifier_Casing (Current_Source_File) = Unknown
1245 then
1246 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1247 end if;
1249 Ident_Node := P_Identifier (C_Dot);
1250 Merge_Identifier (Ident_Node, Tok_Return);
1252 -- Normal case (not child library unit name)
1254 if Token /= Tok_Dot then
1255 Change_Identifier_To_Defining_Identifier (Ident_Node);
1256 Warn_If_Standard_Redefinition (Ident_Node);
1257 return Ident_Node;
1259 -- Child library unit name case
1261 else
1262 if Scope.Last > 1 then
1263 Error_Msg_SP ("child unit allowed only at library level");
1264 raise Error_Resync;
1266 elsif Ada_Version = Ada_83 then
1267 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1269 end if;
1271 Prefix_Node := Ident_Node;
1273 -- Loop through child names, on entry to this loop, Prefix contains
1274 -- the name scanned so far, and Ident_Node is the last identifier.
1276 loop
1277 exit when Token /= Tok_Dot;
1278 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1279 Scan; -- past period
1280 Set_Prefix (Name_Node, Prefix_Node);
1281 Ident_Node := P_Identifier (C_Dot);
1282 Set_Selector_Name (Name_Node, Ident_Node);
1283 Prefix_Node := Name_Node;
1284 end loop;
1286 -- On exit from the loop, Ident_Node is the last identifier scanned,
1287 -- i.e. the defining identifier, and Prefix_Node is a node for the
1288 -- entire name, structured (incorrectly) as a selected component.
1290 Name_Node := Prefix (Prefix_Node);
1291 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1292 Set_Name (Prefix_Node, Name_Node);
1293 Change_Identifier_To_Defining_Identifier (Ident_Node);
1294 Warn_If_Standard_Redefinition (Ident_Node);
1295 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1297 -- All set with unit name parsed
1299 return Prefix_Node;
1300 end if;
1302 exception
1303 when Error_Resync =>
1304 while Token in Tok_Dot | Tok_Identifier loop
1305 Scan;
1306 end loop;
1308 return Error;
1309 end P_Defining_Program_Unit_Name;
1311 --------------------------
1312 -- 6.1 Operator Symbol --
1313 --------------------------
1315 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1317 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1319 -----------------------------------
1320 -- 6.1 Defining Operator Symbol --
1321 -----------------------------------
1323 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1325 -- The caller has checked that the initial symbol is an operator symbol
1327 function P_Defining_Operator_Symbol return Node_Id is
1328 Op_Node : Node_Id;
1330 begin
1331 Op_Node := Token_Node;
1332 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1333 Scan; -- past operator symbol
1334 return Op_Node;
1335 end P_Defining_Operator_Symbol;
1337 ----------------------------
1338 -- 6.1 Parameter_Profile --
1339 ----------------------------
1341 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1343 -- Empty is returned if no formal part is present
1345 -- Error recovery: cannot raise Error_Resync
1347 function P_Parameter_Profile return List_Id is
1348 begin
1349 if Token = Tok_Left_Paren then
1350 Scan; -- part left paren
1351 return P_Formal_Part;
1352 else
1353 return No_List;
1354 end if;
1355 end P_Parameter_Profile;
1357 ---------------------------------------
1358 -- 6.1 Parameter And Result Profile --
1359 ---------------------------------------
1361 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1362 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1364 ----------------------
1365 -- 6.1 Formal part --
1366 ----------------------
1368 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1370 -- PARAMETER_SPECIFICATION ::=
1371 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1372 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1373 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1374 -- [:= DEFAULT_EXPRESSION]
1376 -- This scans the construct Formal_Part. The caller has already checked
1377 -- that the initial token is a left parenthesis, and skipped past it, so
1378 -- that on entry Token is the first token following the left parenthesis.
1380 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1382 -- Error recovery: cannot raise Error_Resync
1384 function P_Formal_Part return List_Id is
1385 Specification_List : List_Id;
1386 Specification_Node : Node_Id;
1387 Scan_State : Saved_Scan_State;
1388 Num_Idents : Nat;
1389 Ident : Nat;
1390 Ident_Sloc : Source_Ptr;
1391 Not_Null_Present : Boolean := False;
1392 Not_Null_Sloc : Source_Ptr;
1394 Idents : array (Int range 1 .. 4096) of Entity_Id;
1395 -- This array holds the list of defining identifiers. The upper bound
1396 -- of 4096 is intended to be essentially infinite, and we do not even
1397 -- bother to check for it being exceeded.
1399 begin
1400 Specification_List := New_List;
1401 Specification_Loop : loop
1402 begin
1403 if Token = Tok_Pragma then
1404 Error_Msg_SC ("pragma not allowed in formal part");
1405 Discard_Junk_Node (P_Pragma (Skipping => True));
1406 end if;
1408 Ignore (Tok_Left_Paren);
1409 Ident_Sloc := Token_Ptr;
1410 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1411 Num_Idents := 1;
1413 Ident_Loop : loop
1414 exit Ident_Loop when Token = Tok_Colon;
1416 -- The only valid tokens are colon and comma, so if we have
1417 -- neither do a bit of investigation to see which is the
1418 -- better choice for insertion.
1420 if Token /= Tok_Comma then
1422 -- Assume colon if ALIASED, IN or OUT keyword found
1424 exit Ident_Loop when Token = Tok_Aliased or else
1425 Token = Tok_In or else
1426 Token = Tok_Out;
1428 -- Otherwise scan ahead
1430 Save_Scan_State (Scan_State);
1431 Look_Ahead : loop
1433 -- If we run into a semicolon, then assume that a
1434 -- colon was missing, e.g. Parms (X Y; ...). Also
1435 -- assume missing colon on EOF (a real disaster)
1436 -- and on a right paren, e.g. Parms (X Y), and also
1437 -- on an assignment symbol, e.g. Parms (X Y := ..)
1439 if Token in Tok_Semicolon | Tok_Right_Paren |
1440 Tok_EOF | Tok_Colon_Equal
1441 then
1442 Restore_Scan_State (Scan_State);
1443 exit Ident_Loop;
1445 -- If we run into a colon, assume that we had a missing
1446 -- comma, e.g. Parms (A B : ...). Also assume a missing
1447 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1449 elsif Token in Tok_Colon | Tok_Comma then
1450 Restore_Scan_State (Scan_State);
1451 exit Look_Ahead;
1452 end if;
1454 Scan;
1455 end loop Look_Ahead;
1456 end if;
1458 -- Here if a comma is present, or to be assumed
1460 T_Comma;
1461 Num_Idents := Num_Idents + 1;
1462 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1463 end loop Ident_Loop;
1465 -- Fall through the loop on encountering a colon, or deciding
1466 -- that there is a missing colon.
1468 T_Colon;
1470 -- If there are multiple identifiers, we repeatedly scan the
1471 -- type and initialization expression information by resetting
1472 -- the scan pointer (so that we get completely separate trees
1473 -- for each occurrence).
1475 if Num_Idents > 1 then
1476 Save_Scan_State (Scan_State);
1477 end if;
1479 -- Loop through defining identifiers in list
1481 Ident := 1;
1483 Ident_List_Loop : loop
1484 Specification_Node :=
1485 New_Node (N_Parameter_Specification, Ident_Sloc);
1486 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1488 -- Scan possible ALIASED for Ada 2012 (AI-142)
1490 if Token = Tok_Aliased then
1491 if Ada_Version < Ada_2012 then
1492 Error_Msg_Ada_2012_Feature
1493 ("ALIASED parameter", Token_Ptr);
1494 else
1495 Set_Aliased_Present (Specification_Node);
1496 end if;
1498 Scan; -- past ALIASED
1499 end if;
1501 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1503 Not_Null_Sloc := Token_Ptr;
1504 Not_Null_Present :=
1505 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1507 -- Case of ACCESS keyword present
1509 if Token = Tok_Access then
1510 Set_Null_Exclusion_Present
1511 (Specification_Node, Not_Null_Present);
1513 if Ada_Version = Ada_83 then
1514 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1515 end if;
1517 Set_Parameter_Type
1518 (Specification_Node,
1519 P_Access_Definition (Not_Null_Present));
1521 -- Case of IN or OUT present
1523 else
1524 if Token in Tok_In | Tok_Out then
1525 if Not_Null_Present then
1526 Error_Msg
1527 ("`NOT NULL` can only be used with `ACCESS`",
1528 Not_Null_Sloc);
1530 if Token = Tok_In then
1531 Error_Msg
1532 ("\`IN` not allowed together with `ACCESS`",
1533 Not_Null_Sloc);
1534 else
1535 Error_Msg
1536 ("\`OUT` not allowed together with `ACCESS`",
1537 Not_Null_Sloc);
1538 end if;
1539 end if;
1541 P_Mode (Specification_Node);
1542 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1543 end if;
1545 Set_Null_Exclusion_Present
1546 (Specification_Node, Not_Null_Present);
1548 if Token = Tok_Procedure
1549 or else
1550 Token = Tok_Function
1551 then
1552 Error_Msg_SC ("formal subprogram parameter not allowed");
1553 Scan;
1555 if Token = Tok_Left_Paren then
1556 Discard_Junk_List (P_Formal_Part);
1557 end if;
1559 if Token = Tok_Return then
1560 Scan;
1561 Discard_Junk_Node (P_Subtype_Mark);
1562 end if;
1564 Set_Parameter_Type (Specification_Node, Error);
1566 else
1567 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1568 No_Constraint;
1569 end if;
1570 end if;
1572 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1574 if Ident > 1 then
1575 Set_Prev_Ids (Specification_Node, True);
1576 end if;
1578 if Ident < Num_Idents then
1579 Set_More_Ids (Specification_Node, True);
1580 end if;
1582 Append (Specification_Node, Specification_List);
1583 exit Ident_List_Loop when Ident = Num_Idents;
1584 Ident := Ident + 1;
1585 Restore_Scan_State (Scan_State);
1586 end loop Ident_List_Loop;
1588 exception
1589 when Error_Resync =>
1590 Resync_Semicolon_List;
1591 end;
1593 if Token = Tok_Semicolon then
1594 Save_Scan_State (Scan_State);
1595 Scan; -- past semicolon
1597 -- If we have RETURN or IS after the semicolon, then assume
1598 -- that semicolon should have been a right parenthesis and exit
1600 if Token in Tok_Is | Tok_Return then
1601 Error_Msg_SP -- CODEFIX
1602 ("|"";"" should be "")""");
1603 exit Specification_Loop;
1604 end if;
1606 -- If we have a declaration keyword after the semicolon, then
1607 -- assume we had a missing right parenthesis and terminate list
1609 if Token in Token_Class_Declk then
1610 Error_Msg_AP -- CODEFIX
1611 ("missing "")""");
1612 Restore_Scan_State (Scan_State);
1613 exit Specification_Loop;
1614 end if;
1616 elsif Token = Tok_Right_Paren then
1617 Scan; -- past right paren
1618 exit Specification_Loop;
1620 -- Support for aspects on formal parameters is a GNAT extension for
1621 -- the time being.
1623 elsif Token = Tok_With then
1624 Error_Msg_Ada_2022_Feature
1625 ("aspect on formal parameter", Token_Ptr);
1627 P_Aspect_Specifications (Specification_Node, False);
1629 -- Set the aspect specifications for previous Ids
1631 if Has_Aspects (Specification_Node)
1632 and then Prev_Ids (Specification_Node)
1633 then
1634 -- Loop through each previous id
1636 declare
1637 Prev_Id : Node_Id := Prev (Specification_Node);
1638 begin
1639 loop
1640 Set_Aspect_Specifications
1641 (Prev_Id, Aspect_Specifications (Specification_Node));
1643 -- Exit when we reach the first parameter in the list
1645 exit when not Prev_Ids (Prev_Id);
1646 Prev_Id := Prev (Prev_Id);
1647 end loop;
1648 end;
1649 end if;
1651 if Token = Tok_Right_Paren then
1652 Scan; -- past right paren
1653 exit Specification_Loop;
1655 elsif Token = Tok_Semicolon then
1656 Save_Scan_State (Scan_State);
1657 Scan; -- past semicolon
1658 end if;
1660 -- Special check for common error of using comma instead of semicolon
1662 elsif Token = Tok_Comma then
1663 T_Semicolon;
1665 -- Special check for omitted separator
1667 elsif Token = Tok_Identifier then
1668 T_Semicolon;
1670 -- If nothing sensible, skip to next semicolon or right paren
1672 else
1673 T_Semicolon;
1674 Resync_Semicolon_List;
1676 if Token = Tok_Semicolon then
1677 Scan; -- past semicolon
1678 else
1679 T_Right_Paren;
1680 exit Specification_Loop;
1681 end if;
1682 end if;
1683 end loop Specification_Loop;
1685 return Specification_List;
1686 end P_Formal_Part;
1688 ----------------------------------
1689 -- 6.1 Parameter Specification --
1690 ----------------------------------
1692 -- Parsed by P_Formal_Part (6.1)
1694 ---------------
1695 -- 6.1 Mode --
1696 ---------------
1698 -- MODE ::= [in] | in out | out
1700 -- There is no explicit node in the tree for the Mode. Instead the
1701 -- In_Present and Out_Present flags are set in the parent node to
1702 -- record the presence of keywords specifying the mode.
1704 -- Error_Recovery: cannot raise Error_Resync
1706 procedure P_Mode (Node : Node_Id) is
1707 begin
1708 if Token = Tok_In then
1709 Scan; -- past IN
1710 Set_In_Present (Node, True);
1712 if Style.Mode_In_Check and then Token /= Tok_Out then
1713 Error_Msg_SP -- CODEFIX
1714 ("(style) IN should be omitted?I?");
1715 end if;
1717 -- Since Ada 2005, formal objects can have an anonymous access type,
1718 -- and of course carry a mode indicator.
1720 if Token = Tok_Access
1721 and then Nkind (Node) /= N_Formal_Object_Declaration
1722 then
1723 Error_Msg_SP ("IN not allowed together with ACCESS");
1724 Scan; -- past ACCESS
1725 end if;
1726 end if;
1728 if Token = Tok_Out then
1729 Scan; -- past OUT
1730 Set_Out_Present (Node, True);
1731 end if;
1733 if Token = Tok_In then
1734 Error_Msg_SC ("IN must precede OUT in parameter mode");
1735 Scan; -- past IN
1736 Set_In_Present (Node, True);
1737 end if;
1738 end P_Mode;
1740 --------------------------
1741 -- 6.3 Subprogram Body --
1742 --------------------------
1744 -- Parsed by P_Subprogram (6.1)
1746 -----------------------------------
1747 -- 6.4 Procedure Call Statement --
1748 -----------------------------------
1750 -- Parsed by P_Sequence_Of_Statements (5.1)
1752 ------------------------
1753 -- 6.4 Function Call --
1754 ------------------------
1756 -- Parsed by P_Name (4.1)
1758 --------------------------------
1759 -- 6.4 Actual Parameter Part --
1760 --------------------------------
1762 -- Parsed by P_Name (4.1)
1764 --------------------------------
1765 -- 6.4 Parameter Association --
1766 --------------------------------
1768 -- Parsed by P_Name (4.1)
1770 ------------------------------------
1771 -- 6.4 Explicit Actual Parameter --
1772 ------------------------------------
1774 -- Parsed by P_Name (4.1)
1776 ---------------------------
1777 -- 6.5 Return Statement --
1778 ---------------------------
1780 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1782 -- EXTENDED_RETURN_STATEMENT ::=
1783 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1784 -- [:= EXPRESSION]
1785 -- [ASPECT_SPECIFICATION] [do
1786 -- HANDLED_SEQUENCE_OF_STATEMENTS
1787 -- end return];
1789 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1791 -- RETURN_STATEMENT ::= return [EXPRESSION];
1793 -- Error recovery: can raise Error_Resync
1795 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1797 -- Note: We don't need to check Ada_Version here, because this is
1798 -- only called in >= Ada 2005 cases anyway.
1800 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1802 begin
1803 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1805 if Token = Tok_Access then
1806 Set_Object_Definition
1807 (Decl_Node, P_Access_Definition (Not_Null_Present));
1808 else
1809 Set_Object_Definition
1810 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1811 end if;
1812 end P_Return_Subtype_Indication;
1814 -- Error recovery: can raise Error_Resync
1816 function P_Return_Object_Declaration return Node_Id is
1817 Return_Obj : Node_Id;
1818 Decl_Node : Node_Id;
1820 begin
1821 Return_Obj := Token_Node;
1822 Change_Identifier_To_Defining_Identifier (Return_Obj);
1823 Warn_If_Standard_Redefinition (Return_Obj);
1824 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1825 Set_Defining_Identifier (Decl_Node, Return_Obj);
1827 Scan; -- past identifier
1828 Scan; -- past :
1830 -- First an error check, if we have two identifiers in a row, a likely
1831 -- possibility is that the first of the identifiers is an incorrectly
1832 -- spelled keyword. See similar check in P_Identifier_Declarations.
1834 if Token = Tok_Identifier then
1835 declare
1836 SS : Saved_Scan_State;
1837 I2 : Boolean;
1839 begin
1840 Save_Scan_State (SS);
1841 Scan; -- past initial identifier
1842 I2 := (Token = Tok_Identifier);
1843 Restore_Scan_State (SS);
1845 if I2
1846 and then
1847 (Bad_Spelling_Of (Tok_Access) or else
1848 Bad_Spelling_Of (Tok_Aliased) or else
1849 Bad_Spelling_Of (Tok_Constant))
1850 then
1851 null;
1852 end if;
1853 end;
1854 end if;
1856 -- We allow "constant" here (as in "return Result : constant
1857 -- T..."). This is not in the latest RM, but the ARG is considering an
1858 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1860 if Token = Tok_Constant then
1861 Scan; -- past CONSTANT
1862 Set_Constant_Present (Decl_Node);
1864 if Token = Tok_Aliased then
1865 Error_Msg_SC -- CODEFIX
1866 ("ALIASED should be before CONSTANT");
1867 Scan; -- past ALIASED
1868 Set_Aliased_Present (Decl_Node);
1869 end if;
1871 elsif Token = Tok_Aliased then
1872 Scan; -- past ALIASED
1873 Set_Aliased_Present (Decl_Node);
1875 -- The restrictions on the use of aliased in an extended return
1876 -- are semantic, not syntactic.
1878 if Token = Tok_Constant then
1879 Scan; -- past CONSTANT
1880 Set_Constant_Present (Decl_Node);
1881 end if;
1882 end if;
1884 P_Return_Subtype_Indication (Decl_Node);
1886 if Token = Tok_Colon_Equal then
1887 Scan; -- past :=
1888 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1889 Set_Has_Init_Expression (Decl_Node);
1890 end if;
1892 return Decl_Node;
1893 end P_Return_Object_Declaration;
1895 -- Error recovery: can raise Error_Resync
1897 function P_Return_Statement return Node_Id is
1898 -- The caller has checked that the initial token is RETURN
1900 function Is_Extended return Boolean;
1901 -- Scan state is just after RETURN (and is left that way). Determine
1902 -- whether this is a simple or extended return statement by looking
1903 -- ahead for "identifier :", which implies extended.
1905 -----------------
1906 -- Is_Extended --
1907 -----------------
1909 function Is_Extended return Boolean is
1910 Scan_State : Saved_Scan_State;
1911 Is_Extended : Boolean := False;
1913 begin
1915 if Token = Tok_Identifier then
1916 Save_Scan_State (Scan_State); -- at identifier
1917 Scan; -- past identifier
1919 if Token = Tok_Colon then
1920 Is_Extended := True;
1921 end if;
1923 Restore_Scan_State (Scan_State); -- to identifier
1924 end if;
1926 return Is_Extended;
1927 end Is_Extended;
1929 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1930 Ret_Strt : constant Column_Number := Start_Column;
1931 Ret_Node : Node_Id;
1932 Decl : Node_Id;
1934 -- Start of processing for P_Return_Statement
1936 begin
1937 Scan; -- past RETURN
1939 -- Simple_return_statement, no expression, return an
1940 -- N_Simple_Return_Statement node with the expression field left Empty.
1942 if Token = Tok_Semicolon then
1943 Scan; -- past ;
1944 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1946 -- Nontrivial case
1948 else
1949 -- Extended_return_statement (Ada 2005 only -- AI-318):
1951 if Is_Extended then
1952 Error_Msg_Ada_2005_Extension ("extended return statement");
1954 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1955 Decl := P_Return_Object_Declaration;
1956 Set_Return_Object_Declarations (Ret_Node, New_List (Decl));
1958 if Token = Tok_With then
1959 P_Aspect_Specifications (Decl, False);
1960 end if;
1962 if Token = Tok_Do then
1963 Push_Scope_Stack;
1964 Scopes (Scope.Last).Ecol := Ret_Strt;
1965 Scopes (Scope.Last).Etyp := E_Return;
1966 Scopes (Scope.Last).Labl := Error;
1967 Scopes (Scope.Last).Sloc := Ret_Sloc;
1968 Scan; -- past DO
1969 Set_Handled_Statement_Sequence
1970 (Ret_Node, P_Handled_Sequence_Of_Statements);
1971 End_Statements;
1973 -- Do we need to handle Error_Resync here???
1974 end if;
1976 -- Simple_return_statement or Return_when_Statement
1977 -- with expression.
1979 -- We avoid trying to scan an expression if we are at an
1980 -- expression terminator since in that case the best error
1981 -- message is probably that we have a missing semicolon.
1983 else
1984 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1986 if Token not in Token_Class_Eterm then
1987 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1988 end if;
1990 -- When the next token is WHEN or IF we know that we are looking
1991 -- at a Return_when_statement
1993 if Token = Tok_When and then not Missing_Semicolon_On_When then
1994 Error_Msg_GNAT_Extension ("return when statement", Token_Ptr);
1995 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
1997 Scan; -- past WHEN
1998 Set_Condition (Ret_Node, P_Condition);
2000 -- Allow IF instead of WHEN, giving error message
2002 elsif Token = Tok_If then
2003 Error_Msg_GNAT_Extension ("return when statement", Token_Ptr);
2004 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
2006 T_When;
2007 Scan; -- past IF used in place of WHEN
2008 Set_Condition (Ret_Node, P_Condition);
2009 end if;
2010 end if;
2012 TF_Semicolon;
2013 end if;
2015 return Ret_Node;
2016 end P_Return_Statement;
2018 end Ch6;