[Ada] Improve error messages for occurrence of GNAT extensions without -gnatX
[official-gcc.git] / gcc / ada / par-ch6.adb
blob2832fd4a82e633ea822d7ffc31fd7ce4921752a2
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-2022, 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 likely an aggregate, check we are in Ada 2022 mode
846 if Token = Tok_Left_Bracket then
847 Error_Msg_Ada_2022_Feature
848 ("!aggregates as expression function", Token_Ptr);
849 end if;
851 if Token in Tok_Left_Paren | Tok_Left_Bracket
852 or else Likely_Expression_Function
853 then
854 -- Check expression function allowed here
856 if not Pf_Flags.Pexp then
857 Error_Msg_SC ("expression function not allowed here!");
858 end if;
860 -- Check we are in Ada 2012 mode
862 Error_Msg_Ada_2012_Feature
863 ("!expression function", Token_Ptr);
865 -- Catch an illegal placement of the aspect specification
866 -- list:
868 -- function_specification
869 -- [aspect_specification] is (expression);
871 -- This case is correctly processed by the parser because
872 -- the expression function first appears as a subprogram
873 -- declaration to the parser. The starting keyword may
874 -- not have been "function" in which case the error is
875 -- on a malformed procedure.
877 if Is_Non_Empty_List (Aspects) then
878 if Func then
879 Error_Msg
880 ("aspect specifications must come after "
881 & "parenthesized expression",
882 Sloc (First (Aspects)));
883 else
884 Error_Msg
885 ("aspect specifications must come after subprogram "
886 & "specification", Sloc (First (Aspects)));
887 end if;
888 end if;
890 -- Parse out expression and build expression function
892 Body_Node :=
893 New_Node
894 (N_Expression_Function, Sloc (Specification_Node));
895 Set_Specification (Body_Node, Specification_Node);
897 declare
898 Expr : constant Node_Id := P_Expression;
899 begin
900 Set_Expression (Body_Node, Expr);
902 -- Check that the full expression is properly
903 -- parenthesized since we may have a left-operand that is
904 -- parenthesized but that is not one of the allowed cases
905 -- with syntactic parentheses.
907 if not (Paren_Count (Expr) /= 0
908 or else Nkind (Expr) in N_Aggregate
909 | N_Extension_Aggregate
910 | N_Quantified_Expression)
911 then
912 Error_Msg
913 ("expression function must be enclosed in "
914 & "parentheses", Sloc (Expr));
915 end if;
916 end;
918 -- Expression functions can carry pre/postconditions
920 P_Aspect_Specifications (Body_Node);
921 Pop_Scope_Stack;
923 -- Subprogram body case
925 else
926 -- Check body allowed here
928 if not Pf_Flags.Pbod then
929 Error_Msg_SP ("subprogram body not allowed here!");
930 end if;
932 -- Here is the test for a suspicious IS (i.e. one that
933 -- looks like it might more properly be a semicolon).
934 -- See separate section describing use of IS instead
935 -- of semicolon in package Parse.
937 if (Token in Token_Class_Declk
938 or else
939 Token = Tok_Identifier)
940 and then Start_Column <= Scopes (Scope.Last).Ecol
941 and then Scope.Last /= 1
942 then
943 Scopes (Scope.Last).Etyp := E_Suspicious_Is;
944 Scopes (Scope.Last).S_Is := Prev_Token_Ptr;
945 end if;
947 -- Build and return subprogram body, parsing declarations
948 -- and statement sequence that belong to the body.
950 Body_Node :=
951 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
952 Set_Specification (Body_Node, Specification_Node);
954 -- If aspects are present, the specification is parsed as
955 -- a subprogram declaration, and we jump here after seeing
956 -- the keyword IS. Attach asspects previously collected to
957 -- the body.
959 if Is_Non_Empty_List (Aspects) then
960 Set_Parent (Aspects, Body_Node);
961 Set_Aspect_Specifications (Body_Node, Aspects);
962 end if;
964 Parse_Decls_Begin_End (Body_Node);
965 end if;
967 return Body_Node;
968 end Scan_Body_Or_Expression_Function;
969 end if;
971 -- Processing for subprogram declaration
973 <<Subprogram_Declaration>>
974 Decl_Node :=
975 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
976 Set_Specification (Decl_Node, Specification_Node);
977 Aspects := Get_Aspect_Specifications (Semicolon => False);
979 -- Aspects may be present on a subprogram body. The source parsed
980 -- so far is that of its specification. Go parse the body and attach
981 -- the collected aspects, if any, to the body.
983 if Token = Tok_Is then
985 -- If the subprogram is a procedure and already has a
986 -- specification, we can't define another.
988 if Nkind (Specification (Decl_Node)) = N_Procedure_Specification
989 and then Null_Present (Specification (Decl_Node))
990 then
991 Error_Msg_AP ("null procedure cannot have a body");
992 end if;
994 Scan;
995 goto Subprogram_Body;
997 else
998 if Is_Non_Empty_List (Aspects) then
999 Set_Parent (Aspects, Decl_Node);
1000 Set_Aspect_Specifications (Decl_Node, Aspects);
1001 end if;
1003 TF_Semicolon;
1004 end if;
1006 -- If this is a context in which a subprogram body is permitted,
1007 -- set active SIS entry in case (see section titled "Handling
1008 -- Semicolon Used in Place of IS" in body of Parser package)
1009 -- Note that SIS_Missing_Semicolon_Message is already set properly.
1011 if Pf_Flags.Pbod
1013 -- Disconnect this processing if we have scanned a null procedure
1014 -- or an Import aspect because in this case the spec is complete
1015 -- anyway with no body.
1017 and then (Nkind (Specification_Node) /= N_Procedure_Specification
1018 or else not Null_Present (Specification_Node))
1019 and then not Contains_Import_Aspect (Aspects)
1020 then
1021 SIS_Labl := Scopes (Scope.Last).Labl;
1022 SIS_Sloc := Scopes (Scope.Last).Sloc;
1023 SIS_Ecol := Scopes (Scope.Last).Ecol;
1024 SIS_Declaration_Node := Decl_Node;
1025 SIS_Semicolon_Sloc := Prev_Token_Ptr;
1027 -- Do not activate the entry if we have "with Import"
1029 if not SIS_Aspect_Import_Seen then
1030 SIS_Entry_Active := True;
1031 end if;
1032 end if;
1034 Pop_Scope_Stack;
1035 return Decl_Node;
1036 end P_Subprogram;
1038 ---------------------------------
1039 -- 6.1 Subprogram Declaration --
1040 ---------------------------------
1042 -- Parsed by P_Subprogram (6.1)
1044 ------------------------------------------
1045 -- 6.1 Abstract Subprogram Declaration --
1046 ------------------------------------------
1048 -- Parsed by P_Subprogram (6.1)
1050 -----------------------------------
1051 -- 6.1 Subprogram Specification --
1052 -----------------------------------
1054 -- SUBPROGRAM_SPECIFICATION ::=
1055 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1056 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1058 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1060 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1062 -- Subprogram specifications that appear in subprogram declarations
1063 -- are parsed by P_Subprogram (6.1). This routine is used in other
1064 -- contexts where subprogram specifications occur.
1066 -- Note: this routine does not affect the scope stack in any way
1068 -- Error recovery: can raise Error_Resync
1070 function P_Subprogram_Specification return Node_Id is
1071 Specification_Node : Node_Id;
1072 Result_Not_Null : Boolean;
1073 Result_Node : Node_Id;
1075 begin
1076 if Token = Tok_Function then
1077 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1078 Scan; -- past FUNCTION
1079 Ignore (Tok_Body);
1080 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1081 Set_Parameter_Specifications
1082 (Specification_Node, P_Parameter_Profile);
1083 Check_Junk_Semicolon_Before_Return;
1084 TF_Return;
1086 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1088 -- Ada 2005 (AI-318-02)
1090 if Token = Tok_Access then
1091 Error_Msg_Ada_2005_Extension ("anonymous access result type");
1093 Result_Node := P_Access_Definition (Result_Not_Null);
1095 else
1096 Result_Node := P_Subtype_Mark;
1097 No_Constraint_Maybe_Expr_Func;
1098 end if;
1100 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1101 Set_Result_Definition (Specification_Node, Result_Node);
1102 return Specification_Node;
1104 elsif Token = Tok_Procedure then
1105 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1106 Scan; -- past PROCEDURE
1107 Ignore (Tok_Body);
1108 Set_Defining_Unit_Name
1109 (Specification_Node, P_Defining_Program_Unit_Name);
1110 Set_Parameter_Specifications
1111 (Specification_Node, P_Parameter_Profile);
1112 return Specification_Node;
1114 else
1115 Error_Msg_SC ("subprogram specification expected");
1116 raise Error_Resync;
1117 end if;
1118 end P_Subprogram_Specification;
1120 ---------------------
1121 -- 6.1 Designator --
1122 ---------------------
1124 -- DESIGNATOR ::=
1125 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1127 -- The caller has checked that the initial token is an identifier,
1128 -- operator symbol, or string literal. Note that we don't bother to
1129 -- do much error diagnosis in this routine, since it is only used for
1130 -- the label on END lines, and the routines in package Par.Endh will
1131 -- check that the label is appropriate.
1133 -- Error recovery: cannot raise Error_Resync
1135 function P_Designator return Node_Id is
1136 Ident_Node : Node_Id;
1137 Name_Node : Node_Id;
1138 Prefix_Node : Node_Id;
1140 function Real_Dot return Boolean;
1141 -- Tests if a current token is an interesting period, i.e. is followed
1142 -- by an identifier or operator symbol or string literal. If not, it is
1143 -- probably just incorrect punctuation to be caught by our caller. Note
1144 -- that the case of an operator symbol or string literal is also an
1145 -- error, but that is an error that we catch here. If the result is
1146 -- True, a real dot has been scanned and we are positioned past it,
1147 -- if the result is False, the scan position is unchanged.
1149 --------------
1150 -- Real_Dot --
1151 --------------
1153 function Real_Dot return Boolean is
1154 Scan_State : Saved_Scan_State;
1156 begin
1157 if Token /= Tok_Dot then
1158 return False;
1160 else
1161 Save_Scan_State (Scan_State);
1162 Scan; -- past dot
1164 if Token = Tok_Identifier
1165 or else Token = Tok_Operator_Symbol
1166 or else Token = Tok_String_Literal
1167 then
1168 return True;
1170 else
1171 Restore_Scan_State (Scan_State);
1172 return False;
1173 end if;
1174 end if;
1175 end Real_Dot;
1177 -- Start of processing for P_Designator
1179 begin
1180 Ident_Node := Token_Node;
1181 Scan; -- past initial token
1183 if Prev_Token = Tok_Operator_Symbol
1184 or else Prev_Token = Tok_String_Literal
1185 or else not Real_Dot
1186 then
1187 return Ident_Node;
1189 -- Child name case
1191 else
1192 Prefix_Node := Ident_Node;
1194 -- Loop through child names, on entry to this loop, Prefix contains
1195 -- the name scanned so far, and Ident_Node is the last identifier.
1197 loop
1198 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1199 Set_Prefix (Name_Node, Prefix_Node);
1200 Ident_Node := P_Identifier;
1201 Set_Selector_Name (Name_Node, Ident_Node);
1202 Prefix_Node := Name_Node;
1203 exit when not Real_Dot;
1204 end loop;
1206 -- On exit from the loop, Ident_Node is the last identifier scanned,
1207 -- i.e. the defining identifier, and Prefix_Node is a node for the
1208 -- entire name, structured (incorrectly) as a selected component.
1210 Name_Node := Prefix (Prefix_Node);
1211 Change_Node (Prefix_Node, N_Designator);
1212 Set_Name (Prefix_Node, Name_Node);
1213 Set_Identifier (Prefix_Node, Ident_Node);
1214 return Prefix_Node;
1215 end if;
1217 exception
1218 when Error_Resync =>
1219 while Token = Tok_Dot or else Token = Tok_Identifier loop
1220 Scan;
1221 end loop;
1223 return Error;
1224 end P_Designator;
1226 ------------------------------
1227 -- 6.1 Defining Designator --
1228 ------------------------------
1230 -- DEFINING_DESIGNATOR ::=
1231 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1233 -- Error recovery: cannot raise Error_Resync
1235 function P_Defining_Designator return Node_Id is
1236 begin
1237 if Token = Tok_Operator_Symbol then
1238 return P_Defining_Operator_Symbol;
1240 elsif Token = Tok_String_Literal then
1241 Error_Msg_SC ("invalid operator name");
1242 Scan; -- past junk string
1243 return Error;
1245 else
1246 return P_Defining_Program_Unit_Name;
1247 end if;
1248 end P_Defining_Designator;
1250 -------------------------------------
1251 -- 6.1 Defining Program Unit Name --
1252 -------------------------------------
1254 -- DEFINING_PROGRAM_UNIT_NAME ::=
1255 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1257 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1259 -- Error recovery: cannot raise Error_Resync
1261 function P_Defining_Program_Unit_Name return Node_Id is
1262 Ident_Node : Node_Id;
1263 Name_Node : Node_Id;
1264 Prefix_Node : Node_Id;
1266 begin
1267 -- Set identifier casing if not already set and scan initial identifier
1269 if Token = Tok_Identifier
1270 and then Identifier_Casing (Current_Source_File) = Unknown
1271 then
1272 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1273 end if;
1275 Ident_Node := P_Identifier (C_Dot);
1276 Merge_Identifier (Ident_Node, Tok_Return);
1278 -- Normal case (not child library unit name)
1280 if Token /= Tok_Dot then
1281 Change_Identifier_To_Defining_Identifier (Ident_Node);
1282 Warn_If_Standard_Redefinition (Ident_Node);
1283 return Ident_Node;
1285 -- Child library unit name case
1287 else
1288 if Scope.Last > 1 then
1289 Error_Msg_SP ("child unit allowed only at library level");
1290 raise Error_Resync;
1292 elsif Ada_Version = Ada_83 then
1293 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1295 end if;
1297 Prefix_Node := Ident_Node;
1299 -- Loop through child names, on entry to this loop, Prefix contains
1300 -- the name scanned so far, and Ident_Node is the last identifier.
1302 loop
1303 exit when Token /= Tok_Dot;
1304 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1305 Scan; -- past period
1306 Set_Prefix (Name_Node, Prefix_Node);
1307 Ident_Node := P_Identifier (C_Dot);
1308 Set_Selector_Name (Name_Node, Ident_Node);
1309 Prefix_Node := Name_Node;
1310 end loop;
1312 -- On exit from the loop, Ident_Node is the last identifier scanned,
1313 -- i.e. the defining identifier, and Prefix_Node is a node for the
1314 -- entire name, structured (incorrectly) as a selected component.
1316 Name_Node := Prefix (Prefix_Node);
1317 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1318 Set_Name (Prefix_Node, Name_Node);
1319 Change_Identifier_To_Defining_Identifier (Ident_Node);
1320 Warn_If_Standard_Redefinition (Ident_Node);
1321 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1323 -- All set with unit name parsed
1325 return Prefix_Node;
1326 end if;
1328 exception
1329 when Error_Resync =>
1330 while Token = Tok_Dot or else Token = Tok_Identifier loop
1331 Scan;
1332 end loop;
1334 return Error;
1335 end P_Defining_Program_Unit_Name;
1337 --------------------------
1338 -- 6.1 Operator Symbol --
1339 --------------------------
1341 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1343 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1345 -----------------------------------
1346 -- 6.1 Defining Operator Symbol --
1347 -----------------------------------
1349 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1351 -- The caller has checked that the initial symbol is an operator symbol
1353 function P_Defining_Operator_Symbol return Node_Id is
1354 Op_Node : Node_Id;
1356 begin
1357 Op_Node := Token_Node;
1358 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1359 Scan; -- past operator symbol
1360 return Op_Node;
1361 end P_Defining_Operator_Symbol;
1363 ----------------------------
1364 -- 6.1 Parameter_Profile --
1365 ----------------------------
1367 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1369 -- Empty is returned if no formal part is present
1371 -- Error recovery: cannot raise Error_Resync
1373 function P_Parameter_Profile return List_Id is
1374 begin
1375 if Token = Tok_Left_Paren then
1376 Scan; -- part left paren
1377 return P_Formal_Part;
1378 else
1379 return No_List;
1380 end if;
1381 end P_Parameter_Profile;
1383 ---------------------------------------
1384 -- 6.1 Parameter And Result Profile --
1385 ---------------------------------------
1387 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1388 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1390 ----------------------
1391 -- 6.1 Formal part --
1392 ----------------------
1394 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1396 -- PARAMETER_SPECIFICATION ::=
1397 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1398 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1399 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1400 -- [:= DEFAULT_EXPRESSION]
1402 -- This scans the construct Formal_Part. The caller has already checked
1403 -- that the initial token is a left parenthesis, and skipped past it, so
1404 -- that on entry Token is the first token following the left parenthesis.
1406 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1408 -- Error recovery: cannot raise Error_Resync
1410 function P_Formal_Part return List_Id is
1411 Specification_List : List_Id;
1412 Specification_Node : Node_Id;
1413 Scan_State : Saved_Scan_State;
1414 Num_Idents : Nat;
1415 Ident : Nat;
1416 Ident_Sloc : Source_Ptr;
1417 Not_Null_Present : Boolean := False;
1418 Not_Null_Sloc : Source_Ptr;
1420 Idents : array (Int range 1 .. 4096) of Entity_Id;
1421 -- This array holds the list of defining identifiers. The upper bound
1422 -- of 4096 is intended to be essentially infinite, and we do not even
1423 -- bother to check for it being exceeded.
1425 begin
1426 Specification_List := New_List;
1427 Specification_Loop : loop
1428 begin
1429 if Token = Tok_Pragma then
1430 Error_Msg_SC ("pragma not allowed in formal part");
1431 Discard_Junk_Node (P_Pragma (Skipping => True));
1432 end if;
1434 Ignore (Tok_Left_Paren);
1435 Ident_Sloc := Token_Ptr;
1436 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1437 Num_Idents := 1;
1439 Ident_Loop : loop
1440 exit Ident_Loop when Token = Tok_Colon;
1442 -- The only valid tokens are colon and comma, so if we have
1443 -- neither do a bit of investigation to see which is the
1444 -- better choice for insertion.
1446 if Token /= Tok_Comma then
1448 -- Assume colon if ALIASED, IN or OUT keyword found
1450 exit Ident_Loop when Token = Tok_Aliased or else
1451 Token = Tok_In or else
1452 Token = Tok_Out;
1454 -- Otherwise scan ahead
1456 Save_Scan_State (Scan_State);
1457 Look_Ahead : loop
1459 -- If we run into a semicolon, then assume that a
1460 -- colon was missing, e.g. Parms (X Y; ...). Also
1461 -- assume missing colon on EOF (a real disaster)
1462 -- and on a right paren, e.g. Parms (X Y), and also
1463 -- on an assignment symbol, e.g. Parms (X Y := ..)
1465 if Token = Tok_Semicolon
1466 or else Token = Tok_Right_Paren
1467 or else Token = Tok_EOF
1468 or else Token = Tok_Colon_Equal
1469 then
1470 Restore_Scan_State (Scan_State);
1471 exit Ident_Loop;
1473 -- If we run into a colon, assume that we had a missing
1474 -- comma, e.g. Parms (A B : ...). Also assume a missing
1475 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1477 elsif Token = Tok_Colon
1478 or else Token = Tok_Comma
1479 then
1480 Restore_Scan_State (Scan_State);
1481 exit Look_Ahead;
1482 end if;
1484 Scan;
1485 end loop Look_Ahead;
1486 end if;
1488 -- Here if a comma is present, or to be assumed
1490 T_Comma;
1491 Num_Idents := Num_Idents + 1;
1492 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1493 end loop Ident_Loop;
1495 -- Fall through the loop on encountering a colon, or deciding
1496 -- that there is a missing colon.
1498 T_Colon;
1500 -- If there are multiple identifiers, we repeatedly scan the
1501 -- type and initialization expression information by resetting
1502 -- the scan pointer (so that we get completely separate trees
1503 -- for each occurrence).
1505 if Num_Idents > 1 then
1506 Save_Scan_State (Scan_State);
1507 end if;
1509 -- Loop through defining identifiers in list
1511 Ident := 1;
1513 Ident_List_Loop : loop
1514 Specification_Node :=
1515 New_Node (N_Parameter_Specification, Ident_Sloc);
1516 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1518 -- Scan possible ALIASED for Ada 2012 (AI-142)
1520 if Token = Tok_Aliased then
1521 if Ada_Version < Ada_2012 then
1522 Error_Msg_Ada_2012_Feature
1523 ("ALIASED parameter", Token_Ptr);
1524 else
1525 Set_Aliased_Present (Specification_Node);
1526 end if;
1528 Scan; -- past ALIASED
1529 end if;
1531 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1533 Not_Null_Sloc := Token_Ptr;
1534 Not_Null_Present :=
1535 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1537 -- Case of ACCESS keyword present
1539 if Token = Tok_Access then
1540 Set_Null_Exclusion_Present
1541 (Specification_Node, Not_Null_Present);
1543 if Ada_Version = Ada_83 then
1544 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1545 end if;
1547 Set_Parameter_Type
1548 (Specification_Node,
1549 P_Access_Definition (Not_Null_Present));
1551 -- Case of IN or OUT present
1553 else
1554 if Token = Tok_In or else Token = Tok_Out then
1555 if Not_Null_Present then
1556 Error_Msg
1557 ("`NOT NULL` can only be used with `ACCESS`",
1558 Not_Null_Sloc);
1560 if Token = Tok_In then
1561 Error_Msg
1562 ("\`IN` not allowed together with `ACCESS`",
1563 Not_Null_Sloc);
1564 else
1565 Error_Msg
1566 ("\`OUT` not allowed together with `ACCESS`",
1567 Not_Null_Sloc);
1568 end if;
1569 end if;
1571 P_Mode (Specification_Node);
1572 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1573 end if;
1575 Set_Null_Exclusion_Present
1576 (Specification_Node, Not_Null_Present);
1578 if Token = Tok_Procedure
1579 or else
1580 Token = Tok_Function
1581 then
1582 Error_Msg_SC ("formal subprogram parameter not allowed");
1583 Scan;
1585 if Token = Tok_Left_Paren then
1586 Discard_Junk_List (P_Formal_Part);
1587 end if;
1589 if Token = Tok_Return then
1590 Scan;
1591 Discard_Junk_Node (P_Subtype_Mark);
1592 end if;
1594 Set_Parameter_Type (Specification_Node, Error);
1596 else
1597 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1598 No_Constraint;
1599 end if;
1600 end if;
1602 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1604 if Ident > 1 then
1605 Set_Prev_Ids (Specification_Node, True);
1606 end if;
1608 if Ident < Num_Idents then
1609 Set_More_Ids (Specification_Node, True);
1610 end if;
1612 Append (Specification_Node, Specification_List);
1613 exit Ident_List_Loop when Ident = Num_Idents;
1614 Ident := Ident + 1;
1615 Restore_Scan_State (Scan_State);
1616 end loop Ident_List_Loop;
1618 exception
1619 when Error_Resync =>
1620 Resync_Semicolon_List;
1621 end;
1623 if Token = Tok_Semicolon then
1624 Save_Scan_State (Scan_State);
1625 Scan; -- past semicolon
1627 -- If we have RETURN or IS after the semicolon, then assume
1628 -- that semicolon should have been a right parenthesis and exit
1630 if Token = Tok_Is or else Token = Tok_Return then
1631 Error_Msg_SP -- CODEFIX
1632 ("|"";"" should be "")""");
1633 exit Specification_Loop;
1634 end if;
1636 -- If we have a declaration keyword after the semicolon, then
1637 -- assume we had a missing right parenthesis and terminate list
1639 if Token in Token_Class_Declk then
1640 Error_Msg_AP -- CODEFIX
1641 ("missing "")""");
1642 Restore_Scan_State (Scan_State);
1643 exit Specification_Loop;
1644 end if;
1646 elsif Token = Tok_Right_Paren then
1647 Scan; -- past right paren
1648 exit Specification_Loop;
1650 -- Support for aspects on formal parameters is a GNAT extension for
1651 -- the time being.
1653 elsif Token = Tok_With then
1654 Error_Msg_Ada_2022_Feature
1655 ("aspect on formal parameter", Token_Ptr);
1657 P_Aspect_Specifications (Specification_Node, False);
1659 if Token = Tok_Right_Paren then
1660 Scan; -- past right paren
1661 exit Specification_Loop;
1663 elsif Token = Tok_Semicolon then
1664 Save_Scan_State (Scan_State);
1665 Scan; -- past semicolon
1666 end if;
1668 -- Special check for common error of using comma instead of semicolon
1670 elsif Token = Tok_Comma then
1671 T_Semicolon;
1673 -- Special check for omitted separator
1675 elsif Token = Tok_Identifier then
1676 T_Semicolon;
1678 -- If nothing sensible, skip to next semicolon or right paren
1680 else
1681 T_Semicolon;
1682 Resync_Semicolon_List;
1684 if Token = Tok_Semicolon then
1685 Scan; -- past semicolon
1686 else
1687 T_Right_Paren;
1688 exit Specification_Loop;
1689 end if;
1690 end if;
1691 end loop Specification_Loop;
1693 return Specification_List;
1694 end P_Formal_Part;
1696 ----------------------------------
1697 -- 6.1 Parameter Specification --
1698 ----------------------------------
1700 -- Parsed by P_Formal_Part (6.1)
1702 ---------------
1703 -- 6.1 Mode --
1704 ---------------
1706 -- MODE ::= [in] | in out | out
1708 -- There is no explicit node in the tree for the Mode. Instead the
1709 -- In_Present and Out_Present flags are set in the parent node to
1710 -- record the presence of keywords specifying the mode.
1712 -- Error_Recovery: cannot raise Error_Resync
1714 procedure P_Mode (Node : Node_Id) is
1715 begin
1716 if Token = Tok_In then
1717 Scan; -- past IN
1718 Set_In_Present (Node, True);
1720 if Style.Mode_In_Check and then Token /= Tok_Out then
1721 Error_Msg_SP -- CODEFIX
1722 ("(style) IN should be omitted");
1723 end if;
1725 -- Since Ada 2005, formal objects can have an anonymous access type,
1726 -- and of course carry a mode indicator.
1728 if Token = Tok_Access
1729 and then Nkind (Node) /= N_Formal_Object_Declaration
1730 then
1731 Error_Msg_SP ("IN not allowed together with ACCESS");
1732 Scan; -- past ACCESS
1733 end if;
1734 end if;
1736 if Token = Tok_Out then
1737 Scan; -- past OUT
1738 Set_Out_Present (Node, True);
1739 end if;
1741 if Token = Tok_In then
1742 Error_Msg_SC ("IN must precede OUT in parameter mode");
1743 Scan; -- past IN
1744 Set_In_Present (Node, True);
1745 end if;
1746 end P_Mode;
1748 --------------------------
1749 -- 6.3 Subprogram Body --
1750 --------------------------
1752 -- Parsed by P_Subprogram (6.1)
1754 -----------------------------------
1755 -- 6.4 Procedure Call Statement --
1756 -----------------------------------
1758 -- Parsed by P_Sequence_Of_Statements (5.1)
1760 ------------------------
1761 -- 6.4 Function Call --
1762 ------------------------
1764 -- Parsed by P_Name (4.1)
1766 --------------------------------
1767 -- 6.4 Actual Parameter Part --
1768 --------------------------------
1770 -- Parsed by P_Name (4.1)
1772 --------------------------------
1773 -- 6.4 Parameter Association --
1774 --------------------------------
1776 -- Parsed by P_Name (4.1)
1778 ------------------------------------
1779 -- 6.4 Explicit Actual Parameter --
1780 ------------------------------------
1782 -- Parsed by P_Name (4.1)
1784 ---------------------------
1785 -- 6.5 Return Statement --
1786 ---------------------------
1788 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1790 -- EXTENDED_RETURN_STATEMENT ::=
1791 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1792 -- [:= EXPRESSION]
1793 -- [ASPECT_SPECIFICATION] [do
1794 -- HANDLED_SEQUENCE_OF_STATEMENTS
1795 -- end return];
1797 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1799 -- RETURN_STATEMENT ::= return [EXPRESSION];
1801 -- Error recovery: can raise Error_Resync
1803 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1805 -- Note: We don't need to check Ada_Version here, because this is
1806 -- only called in >= Ada 2005 cases anyway.
1808 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1810 begin
1811 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1813 if Token = Tok_Access then
1814 Set_Object_Definition
1815 (Decl_Node, P_Access_Definition (Not_Null_Present));
1816 else
1817 Set_Object_Definition
1818 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1819 end if;
1820 end P_Return_Subtype_Indication;
1822 -- Error recovery: can raise Error_Resync
1824 function P_Return_Object_Declaration return Node_Id is
1825 Return_Obj : Node_Id;
1826 Decl_Node : Node_Id;
1828 begin
1829 Return_Obj := Token_Node;
1830 Change_Identifier_To_Defining_Identifier (Return_Obj);
1831 Warn_If_Standard_Redefinition (Return_Obj);
1832 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1833 Set_Defining_Identifier (Decl_Node, Return_Obj);
1835 Scan; -- past identifier
1836 Scan; -- past :
1838 -- First an error check, if we have two identifiers in a row, a likely
1839 -- possibility is that the first of the identifiers is an incorrectly
1840 -- spelled keyword. See similar check in P_Identifier_Declarations.
1842 if Token = Tok_Identifier then
1843 declare
1844 SS : Saved_Scan_State;
1845 I2 : Boolean;
1847 begin
1848 Save_Scan_State (SS);
1849 Scan; -- past initial identifier
1850 I2 := (Token = Tok_Identifier);
1851 Restore_Scan_State (SS);
1853 if I2
1854 and then
1855 (Bad_Spelling_Of (Tok_Access) or else
1856 Bad_Spelling_Of (Tok_Aliased) or else
1857 Bad_Spelling_Of (Tok_Constant))
1858 then
1859 null;
1860 end if;
1861 end;
1862 end if;
1864 -- We allow "constant" here (as in "return Result : constant
1865 -- T..."). This is not in the latest RM, but the ARG is considering an
1866 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1868 if Token = Tok_Constant then
1869 Scan; -- past CONSTANT
1870 Set_Constant_Present (Decl_Node);
1872 if Token = Tok_Aliased then
1873 Error_Msg_SC -- CODEFIX
1874 ("ALIASED should be before CONSTANT");
1875 Scan; -- past ALIASED
1876 Set_Aliased_Present (Decl_Node);
1877 end if;
1879 elsif Token = Tok_Aliased then
1880 Scan; -- past ALIASED
1881 Set_Aliased_Present (Decl_Node);
1883 -- The restrictions on the use of aliased in an extended return
1884 -- are semantic, not syntactic.
1886 if Token = Tok_Constant then
1887 Scan; -- past CONSTANT
1888 Set_Constant_Present (Decl_Node);
1889 end if;
1890 end if;
1892 P_Return_Subtype_Indication (Decl_Node);
1894 if Token = Tok_Colon_Equal then
1895 Scan; -- past :=
1896 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1897 Set_Has_Init_Expression (Decl_Node);
1898 end if;
1900 return Decl_Node;
1901 end P_Return_Object_Declaration;
1903 -- Error recovery: can raise Error_Resync
1905 function P_Return_Statement return Node_Id is
1906 -- The caller has checked that the initial token is RETURN
1908 function Is_Extended return Boolean;
1909 -- Scan state is just after RETURN (and is left that way). Determine
1910 -- whether this is a simple or extended return statement by looking
1911 -- ahead for "identifier :", which implies extended.
1913 -----------------
1914 -- Is_Extended --
1915 -----------------
1917 function Is_Extended return Boolean is
1918 Scan_State : Saved_Scan_State;
1919 Is_Extended : Boolean := False;
1921 begin
1923 if Token = Tok_Identifier then
1924 Save_Scan_State (Scan_State); -- at identifier
1925 Scan; -- past identifier
1927 if Token = Tok_Colon then
1928 Is_Extended := True;
1929 end if;
1931 Restore_Scan_State (Scan_State); -- to identifier
1932 end if;
1934 return Is_Extended;
1935 end Is_Extended;
1937 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1938 Ret_Strt : constant Column_Number := Start_Column;
1939 Ret_Node : Node_Id;
1940 Decl : Node_Id;
1942 -- Start of processing for P_Return_Statement
1944 begin
1945 Scan; -- past RETURN
1947 -- Simple_return_statement, no expression, return an
1948 -- N_Simple_Return_Statement node with the expression field left Empty.
1950 if Token = Tok_Semicolon then
1951 Scan; -- past ;
1952 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1954 -- Nontrivial case
1956 else
1957 -- Extended_return_statement (Ada 2005 only -- AI-318):
1959 if Is_Extended then
1960 Error_Msg_Ada_2005_Extension ("extended return statement");
1962 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1963 Decl := P_Return_Object_Declaration;
1964 Set_Return_Object_Declarations (Ret_Node, New_List (Decl));
1966 if Token = Tok_With then
1967 P_Aspect_Specifications (Decl, False);
1968 end if;
1970 if Token = Tok_Do then
1971 Push_Scope_Stack;
1972 Scopes (Scope.Last).Ecol := Ret_Strt;
1973 Scopes (Scope.Last).Etyp := E_Return;
1974 Scopes (Scope.Last).Labl := Error;
1975 Scopes (Scope.Last).Sloc := Ret_Sloc;
1976 Scan; -- past DO
1977 Set_Handled_Statement_Sequence
1978 (Ret_Node, P_Handled_Sequence_Of_Statements);
1979 End_Statements;
1981 -- Do we need to handle Error_Resync here???
1982 end if;
1984 -- Simple_return_statement or Return_when_Statement
1985 -- with expression.
1987 -- We avoid trying to scan an expression if we are at an
1988 -- expression terminator since in that case the best error
1989 -- message is probably that we have a missing semicolon.
1991 else
1992 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1994 if Token not in Token_Class_Eterm then
1995 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1996 end if;
1998 -- When the next token is WHEN or IF we know that we are looking
1999 -- at a Return_when_statement
2001 if Token = Tok_When and then not Missing_Semicolon_On_When then
2002 Error_Msg_GNAT_Extension ("return when statement", Token_Ptr);
2003 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
2005 Scan; -- past WHEN
2006 Set_Condition (Ret_Node, P_Condition);
2008 -- Allow IF instead of WHEN, giving error message
2010 elsif Token = Tok_If then
2011 Error_Msg_GNAT_Extension ("return when statement", Token_Ptr);
2012 Mutate_Nkind (Ret_Node, N_Return_When_Statement);
2014 T_When;
2015 Scan; -- past IF used in place of WHEN
2016 Set_Condition (Ret_Node, P_Condition);
2017 end if;
2018 end if;
2020 TF_Semicolon;
2021 end if;
2023 return Ret_Node;
2024 end P_Return_Statement;
2026 end Ch6;