1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
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
;
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
74 Restore_Scan_State
(Scan_State
);
77 end Check_Junk_Semicolon_Before_Return
;
79 -----------------------------------
80 -- No_Constraint_Maybe_Expr_Func --
81 -----------------------------------
83 procedure No_Constraint_Maybe_Expr_Func
is
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
94 -- One exception if we have "(token .." then this is a constraint
97 Scan_State
: Saved_Scan_State
;
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
);
111 -- Otherwise we treat this as an expression function
114 Restore_Scan_State
(Scan_State
);
118 -- Otherwise use standard routine to check for no constraint present
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
153 -- HANDLED_SEQUENCE_OF_STATEMENTS
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
);
215 while Present
(Aspect
) loop
216 if Chars
(Identifier
(Aspect
)) = Name_Import
then
224 end Contains_Import_Aspect
;
226 Specification_Node
: Node_Id
;
229 Fpart_List
: List_Id
;
230 Fpart_Sloc
: Source_Ptr
;
231 Result_Not_Null
: Boolean := False;
232 Result_Node
: Node_Id
;
236 Rename_Node
: Node_Id
;
237 Absdec_Node
: Node_Id
;
239 Fproc_Sloc
: Source_Ptr
;
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;
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
;
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
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
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;
284 Error_Msg_SC
-- CODEFIX
285 ("OVERRIDING expected!");
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;
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
311 if Pf_Flags
/= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
313 Pf_Flags
/= Pf_Decl_Pbod_Pexp
315 Pf_Flags
/= Pf_Pbod_Pexp
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!");
325 Func
:= (Token
= Tok_Function
);
326 Fproc_Sloc
:= Token_Ptr
;
327 Scan
; -- past FUNCTION or PROCEDURE
332 Name_Node
:= P_Defining_Designator
;
334 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
335 and then Scope
.Last
= 1
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
353 Set_Chars
(Name_Node
, Name_Find
);
354 Set_Error_Posted
(Name_Node
);
358 Name_Node
:= P_Defining_Program_Unit_Name
;
361 Scopes
(Scope
.Last
).Labl
:= Name_Node
;
362 Current_Node
:= Name_Node
;
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!");
380 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
381 Set_Name
(Inst_Node
, P_Function_Name
);
383 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
384 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
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
);
402 Restore_Scan_State
(Scan_State
); -- to the IS
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
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
427 Fpart_List
:= P_Parameter_Profile
;
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
440 ("PROCEDURE should be FUNCTION", Fproc_Sloc
);
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
);
456 Result_Node
:= P_Subtype_Mark
;
457 No_Constraint_Maybe_Expr_Func
;
461 -- Skip extra parenthesis at end of formal part
463 Ignore
(Tok_Right_Paren
);
465 -- For function, scan result subtype
470 if Prev_Token
= Tok_Return
then
471 Result_Node
:= P_Subtype_Mark
;
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
);
484 Specification_Node
:=
485 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
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
);
498 -- Error check: barriers not allowed on protected functions/procedures
500 if Token
= Tok_When
then
502 Error_Msg_SC
("barrier not allowed on function, only on entry");
504 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
508 Discard_Junk_Node
(P_Expression
);
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");
521 Restore_Scan_State
(Scan_State
);
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
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
557 Error_Msg_SP
-- CODEFIX
558 ("|"";"" should be IS!");
559 goto Subprogram_Body
;
562 Restore_Scan_State
(Scan_State
);
563 goto Subprogram_Declaration
;
566 -- Case of not followed by semicolon
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!");
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
);
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
);
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
;
605 -- Abstract subprogram declaration case
607 if Token
= Tok_Abstract
then
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
);
616 -- Ada 2005 (AI-248): Parse a null procedure declaration
618 elsif Token
= Tok_Null
then
619 Error_Msg_Ada_2005_Extension
("null procedure");
624 Error_Msg_SP
("only procedures can be null");
626 Set_Null_Present
(Specification_Node
);
627 Set_Null_Statement
(Specification_Node
,
628 New_Node
(N_Null_Statement
, Prev_Token_Ptr
));
631 goto Subprogram_Declaration
;
633 -- Check for IS NEW with Formal_Part present and handle nicely
635 elsif Token
= Tok_New
then
637 ("formal part not allowed in instantiation", Fpart_Sloc
);
641 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
644 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
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
);
651 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
655 goto Subprogram_Body
;
658 -- Aspect specifications present
660 elsif Aspect_Specifications_Present
then
661 goto Subprogram_Declaration
;
663 -- Here we have a missing IS or missing semicolon
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
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.
678 Error_Msg_AP
-- CODEFIX
680 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
681 goto Subprogram_Declaration
;
686 -- Processing for stub or subprogram body or expression function
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!");
697 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
699 ("operator symbol cannot be used as subunit name",
703 Scan
; -- past SEPARATE
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
711 ("aspect specifications must come after SEPARATE",
712 Sloc
(First
(Aspects
)));
715 P_Aspect_Specifications
(Stub_Node
, Semicolon
=> False);
720 -- Subprogram body or expression function case
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
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.
741 or else Token
in Token_Class_Declk
742 or else Token
= Tok_Pragma
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
759 -- Anything other than an identifier must be a body
761 elsif Token
/= Tok_Identifier
then
764 -- Here for an identifier
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
776 -- The enclosing scope entry is a subprogram spec
778 Spec_Node
: constant Node_Id
:=
780 (Scopes
(Scope
.Last
).Labl
);
781 Lib_Node
: Node_Id
:= Spec_Node
;
784 -- Check whether there is an enclosing scope that
785 -- is a package declaration.
787 if Scope
.Last
> 1 then
789 Parent
(Scopes
(Scope
.Last
- 1).Labl
);
792 if Ada_Version
>= Ada_2012
794 Nkind
(Lib_Node
) = N_Package_Specification
796 Nkind
(Spec_Node
) = N_Function_Specification
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.
811 Scan_State
: Saved_Scan_State
;
815 Save_Scan_State
(Scan_State
);
816 Scan
; -- past identifier
818 Restore_Scan_State
(Scan_State
);
820 if Tok
= Tok_Colon
or else Tok
= Tok_Comma
then
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.
833 ("expression function must be enclosed in parentheses");
837 end Likely_Expression_Function
;
839 -- Start of processing for Scan_Body_Or_Expression_Function
842 -- Expression_Function case
844 if Token
= Tok_Left_Paren
845 or else Likely_Expression_Function
847 -- Check expression function allowed here
849 if not Pf_Flags
.Pexp
then
850 Error_Msg_SC
("expression function not allowed here!");
853 -- Check we are in Ada 2012 mode
855 Error_Msg_Ada_2012_Feature
856 ("!expression function", Token_Ptr
);
858 -- Catch an illegal placement of the aspect specification
861 -- function_specification
862 -- [aspect_specification] is (expression);
864 -- This case is correctly processed by the parser because
865 -- the expression function first appears as a subprogram
866 -- declaration to the parser. The starting keyword may
867 -- not have been "function" in which case the error is
868 -- on a malformed procedure.
870 if Is_Non_Empty_List
(Aspects
) then
873 ("aspect specifications must come after "
874 & "parenthesized expression",
875 Sloc
(First
(Aspects
)));
878 ("aspect specifications must come after subprogram "
879 & "specification", Sloc
(First
(Aspects
)));
883 -- Parse out expression and build expression function
887 (N_Expression_Function
, Sloc
(Specification_Node
));
888 Set_Specification
(Body_Node
, Specification_Node
);
891 Expr
: constant Node_Id
:= P_Expression
;
893 Set_Expression
(Body_Node
, Expr
);
895 -- Check that the full expression is properly
896 -- parenthesized since we may have a left-operand that is
897 -- parenthesized but that is not one of the allowed cases
898 -- with syntactic parentheses.
900 if not (Paren_Count
(Expr
) /= 0
901 or else Nkind
(Expr
) in N_Aggregate
902 | N_Extension_Aggregate
903 | N_Quantified_Expression
)
906 ("expression function must be enclosed in "
907 & "parentheses", Sloc
(Expr
));
911 -- Expression functions can carry pre/postconditions
913 P_Aspect_Specifications
(Body_Node
);
916 -- Subprogram body case
919 -- Check body allowed here
921 if not Pf_Flags
.Pbod
then
922 Error_Msg_SP
("subprogram body not allowed here!");
925 -- Here is the test for a suspicious IS (i.e. one that
926 -- looks like it might more properly be a semicolon).
927 -- See separate section describing use of IS instead
928 -- of semicolon in package Parse.
930 if (Token
in Token_Class_Declk
932 Token
= Tok_Identifier
)
933 and then Start_Column
<= Scopes
(Scope
.Last
).Ecol
934 and then Scope
.Last
/= 1
936 Scopes
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
937 Scopes
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
940 -- Build and return subprogram body, parsing declarations
941 -- and statement sequence that belong to the body.
944 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
945 Set_Specification
(Body_Node
, Specification_Node
);
947 -- If aspects are present, the specification is parsed as
948 -- a subprogram declaration, and we jump here after seeing
949 -- the keyword IS. Attach asspects previously collected to
952 if Is_Non_Empty_List
(Aspects
) then
953 Set_Parent
(Aspects
, Body_Node
);
954 Set_Aspect_Specifications
(Body_Node
, Aspects
);
957 Parse_Decls_Begin_End
(Body_Node
);
961 end Scan_Body_Or_Expression_Function
;
964 -- Processing for subprogram declaration
966 <<Subprogram_Declaration
>>
968 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
969 Set_Specification
(Decl_Node
, Specification_Node
);
970 Aspects
:= Get_Aspect_Specifications
(Semicolon
=> False);
972 -- Aspects may be present on a subprogram body. The source parsed
973 -- so far is that of its specification. Go parse the body and attach
974 -- the collected aspects, if any, to the body.
976 if Token
= Tok_Is
then
978 -- If the subprogram is a procedure and already has a
979 -- specification, we can't define another.
981 if Nkind
(Specification
(Decl_Node
)) = N_Procedure_Specification
982 and then Null_Present
(Specification
(Decl_Node
))
984 Error_Msg_AP
("null procedure cannot have a body");
988 goto Subprogram_Body
;
991 if Is_Non_Empty_List
(Aspects
) then
992 Set_Parent
(Aspects
, Decl_Node
);
993 Set_Aspect_Specifications
(Decl_Node
, Aspects
);
999 -- If this is a context in which a subprogram body is permitted,
1000 -- set active SIS entry in case (see section titled "Handling
1001 -- Semicolon Used in Place of IS" in body of Parser package)
1002 -- Note that SIS_Missing_Semicolon_Message is already set properly.
1006 -- Disconnect this processing if we have scanned a null procedure
1007 -- or an Import aspect because in this case the spec is complete
1008 -- anyway with no body.
1010 and then (Nkind
(Specification_Node
) /= N_Procedure_Specification
1011 or else not Null_Present
(Specification_Node
))
1012 and then not Contains_Import_Aspect
(Aspects
)
1014 SIS_Labl
:= Scopes
(Scope
.Last
).Labl
;
1015 SIS_Sloc
:= Scopes
(Scope
.Last
).Sloc
;
1016 SIS_Ecol
:= Scopes
(Scope
.Last
).Ecol
;
1017 SIS_Declaration_Node
:= Decl_Node
;
1018 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
1020 -- Do not activate the entry if we have "with Import"
1022 if not SIS_Aspect_Import_Seen
then
1023 SIS_Entry_Active
:= True;
1031 ---------------------------------
1032 -- 6.1 Subprogram Declaration --
1033 ---------------------------------
1035 -- Parsed by P_Subprogram (6.1)
1037 ------------------------------------------
1038 -- 6.1 Abstract Subprogram Declaration --
1039 ------------------------------------------
1041 -- Parsed by P_Subprogram (6.1)
1043 -----------------------------------
1044 -- 6.1 Subprogram Specification --
1045 -----------------------------------
1047 -- SUBPROGRAM_SPECIFICATION ::=
1048 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1049 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1051 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1053 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1055 -- Subprogram specifications that appear in subprogram declarations
1056 -- are parsed by P_Subprogram (6.1). This routine is used in other
1057 -- contexts where subprogram specifications occur.
1059 -- Note: this routine does not affect the scope stack in any way
1061 -- Error recovery: can raise Error_Resync
1063 function P_Subprogram_Specification
return Node_Id
is
1064 Specification_Node
: Node_Id
;
1065 Result_Not_Null
: Boolean;
1066 Result_Node
: Node_Id
;
1069 if Token
= Tok_Function
then
1070 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
1071 Scan
; -- past FUNCTION
1073 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
1074 Set_Parameter_Specifications
1075 (Specification_Node
, P_Parameter_Profile
);
1076 Check_Junk_Semicolon_Before_Return
;
1079 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1081 -- Ada 2005 (AI-318-02)
1083 if Token
= Tok_Access
then
1084 Error_Msg_Ada_2005_Extension
("anonymous access result type");
1086 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
1089 Result_Node
:= P_Subtype_Mark
;
1090 No_Constraint_Maybe_Expr_Func
;
1093 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
1094 Set_Result_Definition
(Specification_Node
, Result_Node
);
1095 return Specification_Node
;
1097 elsif Token
= Tok_Procedure
then
1098 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
1099 Scan
; -- past PROCEDURE
1101 Set_Defining_Unit_Name
1102 (Specification_Node
, P_Defining_Program_Unit_Name
);
1103 Set_Parameter_Specifications
1104 (Specification_Node
, P_Parameter_Profile
);
1105 return Specification_Node
;
1108 Error_Msg_SC
("subprogram specification expected");
1111 end P_Subprogram_Specification
;
1113 ---------------------
1114 -- 6.1 Designator --
1115 ---------------------
1118 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1120 -- The caller has checked that the initial token is an identifier,
1121 -- operator symbol, or string literal. Note that we don't bother to
1122 -- do much error diagnosis in this routine, since it is only used for
1123 -- the label on END lines, and the routines in package Par.Endh will
1124 -- check that the label is appropriate.
1126 -- Error recovery: cannot raise Error_Resync
1128 function P_Designator
return Node_Id
is
1129 Ident_Node
: Node_Id
;
1130 Name_Node
: Node_Id
;
1131 Prefix_Node
: Node_Id
;
1133 function Real_Dot
return Boolean;
1134 -- Tests if a current token is an interesting period, i.e. is followed
1135 -- by an identifier or operator symbol or string literal. If not, it is
1136 -- probably just incorrect punctuation to be caught by our caller. Note
1137 -- that the case of an operator symbol or string literal is also an
1138 -- error, but that is an error that we catch here. If the result is
1139 -- True, a real dot has been scanned and we are positioned past it,
1140 -- if the result is False, the scan position is unchanged.
1146 function Real_Dot
return Boolean is
1147 Scan_State
: Saved_Scan_State
;
1150 if Token
/= Tok_Dot
then
1154 Save_Scan_State
(Scan_State
);
1157 if Token
= Tok_Identifier
1158 or else Token
= Tok_Operator_Symbol
1159 or else Token
= Tok_String_Literal
1164 Restore_Scan_State
(Scan_State
);
1170 -- Start of processing for P_Designator
1173 Ident_Node
:= Token_Node
;
1174 Scan
; -- past initial token
1176 if Prev_Token
= Tok_Operator_Symbol
1177 or else Prev_Token
= Tok_String_Literal
1178 or else not Real_Dot
1185 Prefix_Node
:= Ident_Node
;
1187 -- Loop through child names, on entry to this loop, Prefix contains
1188 -- the name scanned so far, and Ident_Node is the last identifier.
1191 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
1192 Set_Prefix
(Name_Node
, Prefix_Node
);
1193 Ident_Node
:= P_Identifier
;
1194 Set_Selector_Name
(Name_Node
, Ident_Node
);
1195 Prefix_Node
:= Name_Node
;
1196 exit when not Real_Dot
;
1199 -- On exit from the loop, Ident_Node is the last identifier scanned,
1200 -- i.e. the defining identifier, and Prefix_Node is a node for the
1201 -- entire name, structured (incorrectly) as a selected component.
1203 Name_Node
:= Prefix
(Prefix_Node
);
1204 Change_Node
(Prefix_Node
, N_Designator
);
1205 Set_Name
(Prefix_Node
, Name_Node
);
1206 Set_Identifier
(Prefix_Node
, Ident_Node
);
1211 when Error_Resync
=>
1212 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1219 ------------------------------
1220 -- 6.1 Defining Designator --
1221 ------------------------------
1223 -- DEFINING_DESIGNATOR ::=
1224 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1226 -- Error recovery: cannot raise Error_Resync
1228 function P_Defining_Designator
return Node_Id
is
1230 if Token
= Tok_Operator_Symbol
then
1231 return P_Defining_Operator_Symbol
;
1233 elsif Token
= Tok_String_Literal
then
1234 Error_Msg_SC
("invalid operator name");
1235 Scan
; -- past junk string
1239 return P_Defining_Program_Unit_Name
;
1241 end P_Defining_Designator
;
1243 -------------------------------------
1244 -- 6.1 Defining Program Unit Name --
1245 -------------------------------------
1247 -- DEFINING_PROGRAM_UNIT_NAME ::=
1248 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1250 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1252 -- Error recovery: cannot raise Error_Resync
1254 function P_Defining_Program_Unit_Name
return Node_Id
is
1255 Ident_Node
: Node_Id
;
1256 Name_Node
: Node_Id
;
1257 Prefix_Node
: Node_Id
;
1260 -- Set identifier casing if not already set and scan initial identifier
1262 if Token
= Tok_Identifier
1263 and then Identifier_Casing
(Current_Source_File
) = Unknown
1265 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
1268 Ident_Node
:= P_Identifier
(C_Dot
);
1269 Merge_Identifier
(Ident_Node
, Tok_Return
);
1271 -- Normal case (not child library unit name)
1273 if Token
/= Tok_Dot
then
1274 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1275 Warn_If_Standard_Redefinition
(Ident_Node
);
1278 -- Child library unit name case
1281 if Scope
.Last
> 1 then
1282 Error_Msg_SP
("child unit allowed only at library level");
1285 elsif Ada_Version
= Ada_83
then
1286 Error_Msg_SP
("(Ada 83) child unit not allowed!");
1290 Prefix_Node
:= Ident_Node
;
1292 -- Loop through child names, on entry to this loop, Prefix contains
1293 -- the name scanned so far, and Ident_Node is the last identifier.
1296 exit when Token
/= Tok_Dot
;
1297 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
1298 Scan
; -- past period
1299 Set_Prefix
(Name_Node
, Prefix_Node
);
1300 Ident_Node
:= P_Identifier
(C_Dot
);
1301 Set_Selector_Name
(Name_Node
, Ident_Node
);
1302 Prefix_Node
:= Name_Node
;
1305 -- On exit from the loop, Ident_Node is the last identifier scanned,
1306 -- i.e. the defining identifier, and Prefix_Node is a node for the
1307 -- entire name, structured (incorrectly) as a selected component.
1309 Name_Node
:= Prefix
(Prefix_Node
);
1310 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
1311 Set_Name
(Prefix_Node
, Name_Node
);
1312 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1313 Warn_If_Standard_Redefinition
(Ident_Node
);
1314 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
1316 -- All set with unit name parsed
1322 when Error_Resync
=>
1323 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1328 end P_Defining_Program_Unit_Name
;
1330 --------------------------
1331 -- 6.1 Operator Symbol --
1332 --------------------------
1334 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1336 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1338 -----------------------------------
1339 -- 6.1 Defining Operator Symbol --
1340 -----------------------------------
1342 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1344 -- The caller has checked that the initial symbol is an operator symbol
1346 function P_Defining_Operator_Symbol
return Node_Id
is
1350 Op_Node
:= Token_Node
;
1351 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
1352 Scan
; -- past operator symbol
1354 end P_Defining_Operator_Symbol
;
1356 ----------------------------
1357 -- 6.1 Parameter_Profile --
1358 ----------------------------
1360 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1362 -- Empty is returned if no formal part is present
1364 -- Error recovery: cannot raise Error_Resync
1366 function P_Parameter_Profile
return List_Id
is
1368 if Token
= Tok_Left_Paren
then
1369 Scan
; -- part left paren
1370 return P_Formal_Part
;
1374 end P_Parameter_Profile
;
1376 ---------------------------------------
1377 -- 6.1 Parameter And Result Profile --
1378 ---------------------------------------
1380 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1381 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1383 ----------------------
1384 -- 6.1 Formal part --
1385 ----------------------
1387 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1389 -- PARAMETER_SPECIFICATION ::=
1390 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1391 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1392 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1393 -- [:= DEFAULT_EXPRESSION]
1395 -- This scans the construct Formal_Part. The caller has already checked
1396 -- that the initial token is a left parenthesis, and skipped past it, so
1397 -- that on entry Token is the first token following the left parenthesis.
1399 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1401 -- Error recovery: cannot raise Error_Resync
1403 function P_Formal_Part
return List_Id
is
1404 Specification_List
: List_Id
;
1405 Specification_Node
: Node_Id
;
1406 Scan_State
: Saved_Scan_State
;
1409 Ident_Sloc
: Source_Ptr
;
1410 Not_Null_Present
: Boolean := False;
1411 Not_Null_Sloc
: Source_Ptr
;
1413 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1414 -- This array holds the list of defining identifiers. The upper bound
1415 -- of 4096 is intended to be essentially infinite, and we do not even
1416 -- bother to check for it being exceeded.
1419 Specification_List
:= New_List
;
1420 Specification_Loop
: loop
1422 if Token
= Tok_Pragma
then
1423 Error_Msg_SC
("pragma not allowed in formal part");
1424 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
1427 Ignore
(Tok_Left_Paren
);
1428 Ident_Sloc
:= Token_Ptr
;
1429 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1433 exit Ident_Loop
when Token
= Tok_Colon
;
1435 -- The only valid tokens are colon and comma, so if we have
1436 -- neither do a bit of investigation to see which is the
1437 -- better choice for insertion.
1439 if Token
/= Tok_Comma
then
1441 -- Assume colon if ALIASED, IN or OUT keyword found
1443 exit Ident_Loop
when Token
= Tok_Aliased
or else
1444 Token
= Tok_In
or else
1447 -- Otherwise scan ahead
1449 Save_Scan_State
(Scan_State
);
1452 -- If we run into a semicolon, then assume that a
1453 -- colon was missing, e.g. Parms (X Y; ...). Also
1454 -- assume missing colon on EOF (a real disaster)
1455 -- and on a right paren, e.g. Parms (X Y), and also
1456 -- on an assignment symbol, e.g. Parms (X Y := ..)
1458 if Token
= Tok_Semicolon
1459 or else Token
= Tok_Right_Paren
1460 or else Token
= Tok_EOF
1461 or else Token
= Tok_Colon_Equal
1463 Restore_Scan_State
(Scan_State
);
1466 -- If we run into a colon, assume that we had a missing
1467 -- comma, e.g. Parms (A B : ...). Also assume a missing
1468 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1470 elsif Token
= Tok_Colon
1471 or else Token
= Tok_Comma
1473 Restore_Scan_State
(Scan_State
);
1478 end loop Look_Ahead
;
1481 -- Here if a comma is present, or to be assumed
1484 Num_Idents
:= Num_Idents
+ 1;
1485 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1486 end loop Ident_Loop
;
1488 -- Fall through the loop on encountering a colon, or deciding
1489 -- that there is a missing colon.
1493 -- If there are multiple identifiers, we repeatedly scan the
1494 -- type and initialization expression information by resetting
1495 -- the scan pointer (so that we get completely separate trees
1496 -- for each occurrence).
1498 if Num_Idents
> 1 then
1499 Save_Scan_State
(Scan_State
);
1502 -- Loop through defining identifiers in list
1506 Ident_List_Loop
: loop
1507 Specification_Node
:=
1508 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1509 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1511 -- Scan possible ALIASED for Ada 2012 (AI-142)
1513 if Token
= Tok_Aliased
then
1514 if Ada_Version
< Ada_2012
then
1515 Error_Msg_Ada_2012_Feature
1516 ("ALIASED parameter", Token_Ptr
);
1518 Set_Aliased_Present
(Specification_Node
);
1521 Scan
; -- past ALIASED
1524 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1526 Not_Null_Sloc
:= Token_Ptr
;
1528 P_Null_Exclusion
(Allow_Anonymous_In_95
=> True);
1530 -- Case of ACCESS keyword present
1532 if Token
= Tok_Access
then
1533 Set_Null_Exclusion_Present
1534 (Specification_Node
, Not_Null_Present
);
1536 if Ada_Version
= Ada_83
then
1537 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1541 (Specification_Node
,
1542 P_Access_Definition
(Not_Null_Present
));
1544 -- Case of IN or OUT present
1547 if Token
= Tok_In
or else Token
= Tok_Out
then
1548 if Not_Null_Present
then
1550 ("`NOT NULL` can only be used with `ACCESS`",
1553 if Token
= Tok_In
then
1555 ("\`IN` not allowed together with `ACCESS`",
1559 ("\`OUT` not allowed together with `ACCESS`",
1564 P_Mode
(Specification_Node
);
1565 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1568 Set_Null_Exclusion_Present
1569 (Specification_Node
, Not_Null_Present
);
1571 if Token
= Tok_Procedure
1573 Token
= Tok_Function
1575 Error_Msg_SC
("formal subprogram parameter not allowed");
1578 if Token
= Tok_Left_Paren
then
1579 Discard_Junk_List
(P_Formal_Part
);
1582 if Token
= Tok_Return
then
1584 Discard_Junk_Node
(P_Subtype_Mark
);
1587 Set_Parameter_Type
(Specification_Node
, Error
);
1590 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1595 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1598 Set_Prev_Ids
(Specification_Node
, True);
1601 if Ident
< Num_Idents
then
1602 Set_More_Ids
(Specification_Node
, True);
1605 Append
(Specification_Node
, Specification_List
);
1606 exit Ident_List_Loop
when Ident
= Num_Idents
;
1608 Restore_Scan_State
(Scan_State
);
1609 end loop Ident_List_Loop
;
1612 when Error_Resync
=>
1613 Resync_Semicolon_List
;
1616 if Token
= Tok_Semicolon
then
1617 Save_Scan_State
(Scan_State
);
1618 Scan
; -- past semicolon
1620 -- If we have RETURN or IS after the semicolon, then assume
1621 -- that semicolon should have been a right parenthesis and exit
1623 if Token
= Tok_Is
or else Token
= Tok_Return
then
1624 Error_Msg_SP
-- CODEFIX
1625 ("|"";"" should be "")""");
1626 exit Specification_Loop
;
1629 -- If we have a declaration keyword after the semicolon, then
1630 -- assume we had a missing right parenthesis and terminate list
1632 if Token
in Token_Class_Declk
then
1633 Error_Msg_AP
-- CODEFIX
1635 Restore_Scan_State
(Scan_State
);
1636 exit Specification_Loop
;
1639 elsif Token
= Tok_Right_Paren
then
1640 Scan
; -- past right paren
1641 exit Specification_Loop
;
1643 -- Support for aspects on formal parameters is a GNAT extension for
1646 elsif Token
= Tok_With
then
1647 Error_Msg_Ada_2022_Feature
1648 ("aspect on formal parameter", Token_Ptr
);
1650 P_Aspect_Specifications
(Specification_Node
, False);
1652 if Token
= Tok_Right_Paren
then
1653 Scan
; -- past right paren
1654 exit Specification_Loop
;
1656 elsif Token
= Tok_Semicolon
then
1657 Save_Scan_State
(Scan_State
);
1658 Scan
; -- past semicolon
1661 -- Special check for common error of using comma instead of semicolon
1663 elsif Token
= Tok_Comma
then
1666 -- Special check for omitted separator
1668 elsif Token
= Tok_Identifier
then
1671 -- If nothing sensible, skip to next semicolon or right paren
1675 Resync_Semicolon_List
;
1677 if Token
= Tok_Semicolon
then
1678 Scan
; -- past semicolon
1681 exit Specification_Loop
;
1684 end loop Specification_Loop
;
1686 return Specification_List
;
1689 ----------------------------------
1690 -- 6.1 Parameter Specification --
1691 ----------------------------------
1693 -- Parsed by P_Formal_Part (6.1)
1699 -- MODE ::= [in] | in out | out
1701 -- There is no explicit node in the tree for the Mode. Instead the
1702 -- In_Present and Out_Present flags are set in the parent node to
1703 -- record the presence of keywords specifying the mode.
1705 -- Error_Recovery: cannot raise Error_Resync
1707 procedure P_Mode
(Node
: Node_Id
) is
1709 if Token
= Tok_In
then
1711 Set_In_Present
(Node
, True);
1713 if Style
.Mode_In_Check
and then Token
/= Tok_Out
then
1714 Error_Msg_SP
-- CODEFIX
1715 ("(style) IN should be omitted");
1718 -- Since Ada 2005, formal objects can have an anonymous access type,
1719 -- and of course carry a mode indicator.
1721 if Token
= Tok_Access
1722 and then Nkind
(Node
) /= N_Formal_Object_Declaration
1724 Error_Msg_SP
("IN not allowed together with ACCESS");
1725 Scan
; -- past ACCESS
1729 if Token
= Tok_Out
then
1731 Set_Out_Present
(Node
, True);
1734 if Token
= Tok_In
then
1735 Error_Msg_SC
("IN must precede OUT in parameter mode");
1737 Set_In_Present
(Node
, True);
1741 --------------------------
1742 -- 6.3 Subprogram Body --
1743 --------------------------
1745 -- Parsed by P_Subprogram (6.1)
1747 -----------------------------------
1748 -- 6.4 Procedure Call Statement --
1749 -----------------------------------
1751 -- Parsed by P_Sequence_Of_Statements (5.1)
1753 ------------------------
1754 -- 6.4 Function Call --
1755 ------------------------
1757 -- Parsed by P_Name (4.1)
1759 --------------------------------
1760 -- 6.4 Actual Parameter Part --
1761 --------------------------------
1763 -- Parsed by P_Name (4.1)
1765 --------------------------------
1766 -- 6.4 Parameter Association --
1767 --------------------------------
1769 -- Parsed by P_Name (4.1)
1771 ------------------------------------
1772 -- 6.4 Explicit Actual Parameter --
1773 ------------------------------------
1775 -- Parsed by P_Name (4.1)
1777 ---------------------------
1778 -- 6.5 Return Statement --
1779 ---------------------------
1781 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1783 -- EXTENDED_RETURN_STATEMENT ::=
1784 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1786 -- [ASPECT_SPECIFICATION] [do
1787 -- HANDLED_SEQUENCE_OF_STATEMENTS
1790 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1792 -- RETURN_STATEMENT ::= return [EXPRESSION];
1794 -- Error recovery: can raise Error_Resync
1796 procedure P_Return_Subtype_Indication
(Decl_Node
: Node_Id
) is
1798 -- Note: We don't need to check Ada_Version here, because this is
1799 -- only called in >= Ada 2005 cases anyway.
1801 Not_Null_Present
: constant Boolean := P_Null_Exclusion
;
1804 Set_Null_Exclusion_Present
(Decl_Node
, Not_Null_Present
);
1806 if Token
= Tok_Access
then
1807 Set_Object_Definition
1808 (Decl_Node
, P_Access_Definition
(Not_Null_Present
));
1810 Set_Object_Definition
1811 (Decl_Node
, P_Subtype_Indication
(Not_Null_Present
));
1813 end P_Return_Subtype_Indication
;
1815 -- Error recovery: can raise Error_Resync
1817 function P_Return_Object_Declaration
return Node_Id
is
1818 Return_Obj
: Node_Id
;
1819 Decl_Node
: Node_Id
;
1822 Return_Obj
:= Token_Node
;
1823 Change_Identifier_To_Defining_Identifier
(Return_Obj
);
1824 Warn_If_Standard_Redefinition
(Return_Obj
);
1825 Decl_Node
:= New_Node
(N_Object_Declaration
, Token_Ptr
);
1826 Set_Defining_Identifier
(Decl_Node
, Return_Obj
);
1828 Scan
; -- past identifier
1831 -- First an error check, if we have two identifiers in a row, a likely
1832 -- possibility is that the first of the identifiers is an incorrectly
1833 -- spelled keyword. See similar check in P_Identifier_Declarations.
1835 if Token
= Tok_Identifier
then
1837 SS
: Saved_Scan_State
;
1841 Save_Scan_State
(SS
);
1842 Scan
; -- past initial identifier
1843 I2
:= (Token
= Tok_Identifier
);
1844 Restore_Scan_State
(SS
);
1848 (Bad_Spelling_Of
(Tok_Access
) or else
1849 Bad_Spelling_Of
(Tok_Aliased
) or else
1850 Bad_Spelling_Of
(Tok_Constant
))
1857 -- We allow "constant" here (as in "return Result : constant
1858 -- T..."). This is not in the latest RM, but the ARG is considering an
1859 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1861 if Token
= Tok_Constant
then
1862 Scan
; -- past CONSTANT
1863 Set_Constant_Present
(Decl_Node
);
1865 if Token
= Tok_Aliased
then
1866 Error_Msg_SC
-- CODEFIX
1867 ("ALIASED should be before CONSTANT");
1868 Scan
; -- past ALIASED
1869 Set_Aliased_Present
(Decl_Node
);
1872 elsif Token
= Tok_Aliased
then
1873 Scan
; -- past ALIASED
1874 Set_Aliased_Present
(Decl_Node
);
1876 -- The restrictions on the use of aliased in an extended return
1877 -- are semantic, not syntactic.
1879 if Token
= Tok_Constant
then
1880 Scan
; -- past CONSTANT
1881 Set_Constant_Present
(Decl_Node
);
1885 P_Return_Subtype_Indication
(Decl_Node
);
1887 if Token
= Tok_Colon_Equal
then
1889 Set_Expression
(Decl_Node
, P_Expression_No_Right_Paren
);
1890 Set_Has_Init_Expression
(Decl_Node
);
1894 end P_Return_Object_Declaration
;
1896 -- Error recovery: can raise Error_Resync
1898 function P_Return_Statement
return Node_Id
is
1899 -- The caller has checked that the initial token is RETURN
1901 function Is_Extended
return Boolean;
1902 -- Scan state is just after RETURN (and is left that way). Determine
1903 -- whether this is a simple or extended return statement by looking
1904 -- ahead for "identifier :", which implies extended.
1910 function Is_Extended
return Boolean is
1911 Scan_State
: Saved_Scan_State
;
1912 Is_Extended
: Boolean := False;
1916 if Token
= Tok_Identifier
then
1917 Save_Scan_State
(Scan_State
); -- at identifier
1918 Scan
; -- past identifier
1920 if Token
= Tok_Colon
then
1921 Is_Extended
:= True;
1924 Restore_Scan_State
(Scan_State
); -- to identifier
1930 Ret_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1931 Ret_Strt
: constant Column_Number
:= Start_Column
;
1935 -- Start of processing for P_Return_Statement
1938 Scan
; -- past RETURN
1940 -- Simple_return_statement, no expression, return an
1941 -- N_Simple_Return_Statement node with the expression field left Empty.
1943 if Token
= Tok_Semicolon
then
1945 Ret_Node
:= New_Node
(N_Simple_Return_Statement
, Ret_Sloc
);
1950 -- Extended_return_statement (Ada 2005 only -- AI-318):
1953 Error_Msg_Ada_2005_Extension
("extended return statement");
1955 Ret_Node
:= New_Node
(N_Extended_Return_Statement
, Ret_Sloc
);
1956 Decl
:= P_Return_Object_Declaration
;
1957 Set_Return_Object_Declarations
(Ret_Node
, New_List
(Decl
));
1959 if Token
= Tok_With
then
1960 P_Aspect_Specifications
(Decl
, False);
1963 if Token
= Tok_Do
then
1965 Scopes
(Scope
.Last
).Ecol
:= Ret_Strt
;
1966 Scopes
(Scope
.Last
).Etyp
:= E_Return
;
1967 Scopes
(Scope
.Last
).Labl
:= Error
;
1968 Scopes
(Scope
.Last
).Sloc
:= Ret_Sloc
;
1970 Set_Handled_Statement_Sequence
1971 (Ret_Node
, P_Handled_Sequence_Of_Statements
);
1974 -- Do we need to handle Error_Resync here???
1977 -- Simple_return_statement or Return_when_Statement
1980 -- We avoid trying to scan an expression if we are at an
1981 -- expression terminator since in that case the best error
1982 -- message is probably that we have a missing semicolon.
1985 Ret_Node
:= New_Node
(N_Simple_Return_Statement
, Ret_Sloc
);
1987 if Token
not in Token_Class_Eterm
then
1988 Set_Expression
(Ret_Node
, P_Expression_No_Right_Paren
);
1991 -- When the next token is WHEN or IF we know that we are looking
1992 -- at a Return_when_statement
1994 if Token
= Tok_When
and then not Missing_Semicolon_On_When
then
1995 Error_Msg_GNAT_Extension
("return when statement");
1996 Mutate_Nkind
(Ret_Node
, N_Return_When_Statement
);
1999 Set_Condition
(Ret_Node
, P_Condition
);
2001 -- Allow IF instead of WHEN, giving error message
2003 elsif Token
= Tok_If
then
2004 Error_Msg_GNAT_Extension
("return when statement");
2005 Mutate_Nkind
(Ret_Node
, N_Return_When_Statement
);
2008 Scan
; -- past IF used in place of WHEN
2009 Set_Condition
(Ret_Node
, P_Condition
);
2017 end P_Return_Statement
;