1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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
204 Specification_Node
: Node_Id
;
207 Fpart_List
: List_Id
;
208 Fpart_Sloc
: Source_Ptr
;
209 Result_Not_Null
: Boolean := False;
210 Result_Node
: Node_Id
;
214 Rename_Node
: Node_Id
;
215 Absdec_Node
: Node_Id
;
217 Fproc_Sloc
: Source_Ptr
;
219 Scan_State
: Saved_Scan_State
;
221 -- Flags for optional overriding indication. Two flags are needed,
222 -- to distinguish positive and negative overriding indicators from
223 -- the absence of any indicator.
225 Is_Overriding
: Boolean := False;
226 Not_Overriding
: Boolean := False;
229 -- Set up scope stack entry. Note that the Labl field will be set later
231 SIS_Entry_Active
:= False;
232 SIS_Missing_Semicolon_Message
:= No_Error_Msg
;
234 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
235 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
236 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
237 Scope
.Table
(Scope
.Last
).Lreq
:= False;
239 Aspects
:= Empty_List
;
241 -- Ada 2005: Scan leading NOT OVERRIDING indicator
243 if Token
= Tok_Not
then
246 if Token
= Tok_Overriding
then
247 Scan
; -- past OVERRIDING
248 Not_Overriding
:= True;
250 -- Overriding keyword used in non Ada 2005 mode
252 elsif Token
= Tok_Identifier
253 and then Token_Name
= Name_Overriding
255 Error_Msg_SC
("overriding indicator is an Ada 2005 extension");
256 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
257 Scan
; -- past Overriding
258 Not_Overriding
:= True;
261 Error_Msg_SC
-- CODEFIX
262 ("OVERRIDING expected!");
265 -- Ada 2005: scan leading OVERRIDING indicator
267 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
268 -- declaration circuit already gave an error message and changed the
269 -- token to Tok_Overriding.
271 elsif Token
= Tok_Overriding
then
272 Scan
; -- past OVERRIDING
273 Is_Overriding
:= True;
276 if Is_Overriding
or else Not_Overriding
then
278 -- Note that if we are not in Ada_2005 mode, error messages have
279 -- already been given, so no need to give another message here.
281 -- An overriding indicator is allowed for subprogram declarations,
282 -- bodies (including subunits), renamings, stubs, and instantiations.
283 -- The test against Pf_Decl_Pbod is added to account for the case of
284 -- subprograms declared in a protected type, where only subprogram
285 -- declarations and bodies can occur. The Pf_Pbod case is for
288 if Pf_Flags
/= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
290 Pf_Flags
/= Pf_Decl_Pbod_Pexp
292 Pf_Flags
/= Pf_Pbod_Pexp
294 Error_Msg_SC
("overriding indicator not allowed here!");
296 elsif Token
/= Tok_Function
and then Token
/= Tok_Procedure
then
297 Error_Msg_SC
-- CODEFIX
298 ("FUNCTION or PROCEDURE expected!");
302 Func
:= (Token
= Tok_Function
);
303 Fproc_Sloc
:= Token_Ptr
;
304 Scan
; -- past FUNCTION or PROCEDURE
309 Name_Node
:= P_Defining_Designator
;
311 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
312 and then Scope
.Last
= 1
314 Error_Msg_SP
("operator symbol not allowed at library level");
315 Name_Node
:= New_Entity
(N_Defining_Identifier
, Sloc
(Name_Node
));
317 -- Set name from file name, we need some junk name, and that's
318 -- as good as anything. This is only approximate, since we do
319 -- not do anything with non-standard name translations.
321 Get_Name_String
(File_Name
(Current_Source_File
));
323 for J
in 1 .. Name_Len
loop
324 if Name_Buffer
(J
) = '.' then
330 Set_Chars
(Name_Node
, Name_Find
);
331 Set_Error_Posted
(Name_Node
);
335 Name_Node
:= P_Defining_Program_Unit_Name
;
338 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
339 Current_Node
:= Name_Node
;
342 -- Deal with generic instantiation, the one case in which we do not
343 -- have a subprogram specification as part of whatever we are parsing
345 if Token
= Tok_Is
then
346 Save_Scan_State
(Scan_State
); -- at the IS
347 T_Is
; -- checks for redundant IS
349 if Token
= Tok_New
then
350 if not Pf_Flags
.Gins
then
351 Error_Msg_SC
("generic instantiation not allowed here!");
357 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
358 Set_Name
(Inst_Node
, P_Function_Name
);
360 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
361 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
364 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
365 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
366 P_Aspect_Specifications
(Inst_Node
);
367 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
369 if Is_Overriding
then
370 Set_Must_Override
(Inst_Node
);
372 elsif Not_Overriding
then
373 Set_Must_Not_Override
(Inst_Node
);
379 Restore_Scan_State
(Scan_State
); -- to the IS
383 -- If not a generic instantiation, then we definitely have a subprogram
384 -- specification (all possibilities at this stage include one here)
386 Fpart_Sloc
:= Token_Ptr
;
388 Check_Misspelling_Of
(Tok_Return
);
390 -- Scan formal part. First a special error check. If we have an
391 -- identifier here, then we have a definite error. If this identifier
392 -- is on the same line as the designator, then we assume it is the
393 -- first formal after a missing left parenthesis
395 if Token
= Tok_Identifier
396 and then not Token_Is_At_Start_Of_Line
398 T_Left_Paren
; -- to generate message
399 Fpart_List
:= P_Formal_Part
;
401 -- Otherwise scan out an optional formal part in the usual manner
404 Fpart_List
:= P_Parameter_Profile
;
407 -- We treat what we have as a function specification if FUNCTION was
408 -- used, or if a RETURN is present. This gives better error recovery
409 -- since later RETURN statements will be valid in either case.
411 Check_Junk_Semicolon_Before_Return
;
412 Result_Node
:= Error
;
414 if Token
= Tok_Return
then
417 ("PROCEDURE should be FUNCTION", Fproc_Sloc
);
423 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
425 -- Ada 2005 (AI-318-02)
427 if Token
= Tok_Access
then
428 if Ada_Version
< Ada_2005
then
430 ("anonymous access result type is an Ada 2005 extension");
431 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
434 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
437 Result_Node
:= P_Subtype_Mark
;
438 No_Constraint_Maybe_Expr_Func
;
442 -- Skip extra parenthesis at end of formal part
444 Ignore
(Tok_Right_Paren
);
446 -- For function, scan result subtype
451 if Prev_Token
= Tok_Return
then
452 Result_Node
:= P_Subtype_Mark
;
458 Specification_Node
:=
459 New_Node
(N_Function_Specification
, Fproc_Sloc
);
461 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
462 Set_Result_Definition
(Specification_Node
, Result_Node
);
465 Specification_Node
:=
466 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
469 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
470 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
472 if Is_Overriding
then
473 Set_Must_Override
(Specification_Node
);
475 elsif Not_Overriding
then
476 Set_Must_Not_Override
(Specification_Node
);
479 -- Error check: barriers not allowed on protected functions/procedures
481 if Token
= Tok_When
then
483 Error_Msg_SC
("barrier not allowed on function, only on entry");
485 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
489 Discard_Junk_Node
(P_Expression
);
492 -- Deal with semicolon followed by IS. We want to treat this as IS
494 if Token
= Tok_Semicolon
then
495 Save_Scan_State
(Scan_State
);
496 Scan
; -- past semicolon
498 if Token
= Tok_Is
then
499 Error_Msg_SP
-- CODEFIX
500 ("extra "";"" ignored");
502 Restore_Scan_State
(Scan_State
);
506 -- Subprogram declaration ended by aspect specifications
508 if Aspect_Specifications_Present
then
509 goto Subprogram_Declaration
;
511 -- Deal with case of semicolon ending a subprogram declaration
513 elsif Token
= Tok_Semicolon
then
514 if not Pf_Flags
.Decl
then
518 Save_Scan_State
(Scan_State
);
519 Scan
; -- past semicolon
521 -- If semicolon is immediately followed by IS, then ignore the
522 -- semicolon, and go process the body.
524 if Token
= Tok_Is
then
525 Error_Msg_SP
-- CODEFIX
526 ("|extra "";"" ignored");
527 T_Is
; -- scan past IS
528 goto Subprogram_Body
;
530 -- If BEGIN follows in an appropriate column, we immediately
531 -- commence the error action of assuming that the previous
532 -- subprogram declaration should have been a subprogram body,
533 -- i.e. that the terminating semicolon should have been IS.
535 elsif Token
= Tok_Begin
536 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
538 Error_Msg_SP
-- CODEFIX
539 ("|"";"" should be IS!");
540 goto Subprogram_Body
;
543 Restore_Scan_State
(Scan_State
);
544 goto Subprogram_Declaration
;
547 -- Case of not followed by semicolon
550 -- Subprogram renaming declaration case
552 Check_Misspelling_Of
(Tok_Renames
);
554 if Token
= Tok_Renames
then
555 if not Pf_Flags
.Rnam
then
556 Error_Msg_SC
("renaming declaration not allowed here!");
560 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
561 Scan
; -- past RENAMES
562 Set_Name
(Rename_Node
, P_Name
);
563 Set_Specification
(Rename_Node
, Specification_Node
);
564 P_Aspect_Specifications
(Rename_Node
);
569 -- Case of IS following subprogram specification
571 elsif Token
= Tok_Is
then
572 T_Is
; -- ignore redundant Is's
574 if Token_Name
= Name_Abstract
then
575 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
578 -- Deal nicely with (now obsolete) use of <> in place of abstract
580 if Token
= Tok_Box
then
581 Error_Msg_SC
-- CODEFIX
582 ("ABSTRACT expected");
583 Token
:= Tok_Abstract
;
586 -- Abstract subprogram declaration case
588 if Token
= Tok_Abstract
then
590 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
591 Set_Specification
(Absdec_Node
, Specification_Node
);
592 Pop_Scope_Stack
; -- discard unneeded entry
593 Scan
; -- past ABSTRACT
594 P_Aspect_Specifications
(Absdec_Node
);
597 -- Ada 2005 (AI-248): Parse a null procedure declaration
599 elsif Token
= Tok_Null
then
600 if Ada_Version
< Ada_2005
then
601 Error_Msg_SP
("null procedures are an Ada 2005 extension");
602 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
608 Error_Msg_SP
("only procedures can be null");
610 Set_Null_Present
(Specification_Node
);
611 Set_Null_Statement
(Specification_Node
,
612 New_Node
(N_Null_Statement
, Prev_Token_Ptr
));
615 goto Subprogram_Declaration
;
617 -- Check for IS NEW with Formal_Part present and handle nicely
619 elsif Token
= Tok_New
then
621 ("formal part not allowed in instantiation", Fpart_Sloc
);
625 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
628 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
631 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
632 Set_Name
(Inst_Node
, P_Name
);
633 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
635 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
639 goto Subprogram_Body
;
642 -- Aspect specifications present
644 elsif Aspect_Specifications_Present
then
645 goto Subprogram_Declaration
;
647 -- Here we have a missing IS or missing semicolon
650 -- If the next token is a left paren at the start of a line, then
651 -- this is almost certainly the start of the expression for an
652 -- expression function, so in this case guess a missing IS.
654 if Token
= Tok_Left_Paren
and then Token_Is_At_Start_Of_Line
then
655 Error_Msg_AP
-- CODEFIX
658 -- In all other cases, we guess a missing semicolon, since we are
659 -- good at fixing up a semicolon which should really be an IS.
662 Error_Msg_AP
-- CODEFIX
664 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
665 goto Subprogram_Declaration
;
670 -- Processing for stub or subprogram body or expression function
674 -- Subprogram body stub case
676 if Separate_Present
then
677 if not Pf_Flags
.Stub
then
678 Error_Msg_SC
("body stub not allowed here!");
681 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
683 ("operator symbol cannot be used as subunit name",
687 Scan
; -- past SEPARATE
690 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
691 Set_Specification
(Stub_Node
, Specification_Node
);
693 if Is_Non_Empty_List
(Aspects
) then
695 ("aspect specifications must come after SEPARATE",
696 Sloc
(First
(Aspects
)));
699 P_Aspect_Specifications
(Stub_Node
, Semicolon
=> False);
704 -- Subprogram body or expression function case
707 Scan_Body_Or_Expression_Function
: declare
709 Body_Is_Hidden_In_SPARK
: Boolean;
710 Hidden_Region_Start
: Source_Ptr
;
712 function Likely_Expression_Function
return Boolean;
713 -- Returns True if we have a probable case of an expression
714 -- function omitting the parentheses, if so, returns True
715 -- and emits an appropriate error message, else returns False.
717 --------------------------------
718 -- Likely_Expression_Function --
719 --------------------------------
721 function Likely_Expression_Function
return Boolean is
723 -- If currently pointing to BEGIN or a declaration keyword
724 -- or a pragma, then we definitely have a subprogram body.
725 -- This is a common case, so worth testing first.
728 or else Token
in Token_Class_Declk
729 or else Token
= Tok_Pragma
733 -- Test for tokens which could only start an expression and
734 -- thus signal the case of a expression function.
736 elsif Token
in Token_Class_Literal
737 or else Token
in Token_Class_Unary_Addop
738 or else Token
= Tok_Left_Paren
739 or else Token
= Tok_Abs
740 or else Token
= Tok_Null
741 or else Token
= Tok_New
742 or else Token
= Tok_Not
746 -- Anything other than an identifier must be a body
748 elsif Token
/= Tok_Identifier
then
751 -- Here for an identifier
754 -- If the identifier is the first token on its line, then
755 -- let's assume that we have a missing begin and this is
756 -- intended as a subprogram body. However, if the context
757 -- is a function and the unit is a package declaration, a
758 -- body would be illegal, so try for an unparenthesized
759 -- expression function.
761 if Token_Is_At_Start_Of_Line
then
763 -- The enclosing scope entry is a subprogram spec
765 Spec_Node
: constant Node_Id
:=
767 (Scope
.Table
(Scope
.Last
).Labl
);
768 Lib_Node
: Node_Id
:= Spec_Node
;
771 -- Check whether there is an enclosing scope that
772 -- is a package declaration.
774 if Scope
.Last
> 1 then
776 Parent
(Scope
.Table
(Scope
.Last
- 1).Labl
);
779 if Ada_Version
>= Ada_2012
781 Nkind
(Lib_Node
) = N_Package_Specification
783 Nkind
(Spec_Node
) = N_Function_Specification
791 -- Otherwise we have to scan ahead. If the identifier is
792 -- followed by a colon or a comma, it is a declaration
793 -- and hence we have a subprogram body. Otherwise assume
794 -- a expression function.
798 Scan_State
: Saved_Scan_State
;
802 Save_Scan_State
(Scan_State
);
803 Scan
; -- past identifier
805 Restore_Scan_State
(Scan_State
);
807 if Tok
= Tok_Colon
or else Tok
= Tok_Comma
then
814 -- Fall through if we have a likely expression function.
815 -- If the starting keyword is not "function" the error
816 -- will be reported elsewhere.
820 ("expression function must be enclosed in parentheses");
824 end Likely_Expression_Function
;
826 -- Start of processing for Scan_Body_Or_Expression_Function
829 -- Expression_Function case
831 if Token
= Tok_Left_Paren
832 or else Likely_Expression_Function
834 -- Check expression function allowed here
836 if not Pf_Flags
.Pexp
then
837 Error_Msg_SC
("expression function not allowed here!");
840 -- Check we are in Ada 2012 mode
842 Error_Msg_Ada_2012_Feature
843 ("!expression function", Token_Ptr
);
845 -- Catch an illegal placement of the aspect specification
848 -- function_specification
849 -- [aspect_specification] is (expression);
851 -- This case is correctly processed by the parser because
852 -- the expression function first appears as a subprogram
853 -- declaration to the parser. The starting keyword may
854 -- not have been "function" in which case the error is
855 -- on a malformed procedure.
857 if Is_Non_Empty_List
(Aspects
) then
860 ("aspect specifications must come after "
861 & "parenthesized expression",
862 Sloc
(First
(Aspects
)));
865 ("aspect specifications must come after subprogram "
866 & "specification", Sloc
(First
(Aspects
)));
870 -- Parse out expression and build expression function
874 (N_Expression_Function
, Sloc
(Specification_Node
));
875 Set_Specification
(Body_Node
, Specification_Node
);
878 Expr
: constant Node_Id
:= P_Expression
;
880 Set_Expression
(Body_Node
, Expr
);
882 -- Check that the full expression is properly
883 -- parenthesized since we may have a left-operand that is
884 -- parenthesized but that is not one of the allowed cases
885 -- with syntactic parentheses.
887 if not (Paren_Count
(Expr
) /= 0
888 or else Nkind_In
(Expr
, N_Aggregate
,
889 N_Extension_Aggregate
,
890 N_Quantified_Expression
))
893 ("expression function must be enclosed in "
894 & "parentheses", Sloc
(Expr
));
898 -- Expression functions can carry pre/postconditions
900 P_Aspect_Specifications
(Body_Node
);
903 -- Subprogram body case
906 -- Check body allowed here
908 if not Pf_Flags
.Pbod
then
909 Error_Msg_SP
("subprogram body not allowed here!");
912 -- Here is the test for a suspicious IS (i.e. one that
913 -- looks like it might more properly be a semicolon).
914 -- See separate section describing use of IS instead
915 -- of semicolon in package Parse.
917 if (Token
in Token_Class_Declk
919 Token
= Tok_Identifier
)
920 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
921 and then Scope
.Last
/= 1
923 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
924 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
927 -- Build and return subprogram body, parsing declarations
928 -- and statement sequence that belong to the body.
931 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
932 Set_Specification
(Body_Node
, Specification_Node
);
934 -- If aspects are present, the specification is parsed as
935 -- a subprogram declaration, and we jump here after seeing
936 -- the keyword IS. Attach asspects previously collected to
939 if Is_Non_Empty_List
(Aspects
) then
940 Set_Parent
(Aspects
, Body_Node
);
941 Set_Aspect_Specifications
(Body_Node
, Aspects
);
944 -- In SPARK, a HIDE directive can be placed at the beginning
945 -- of a subprogram implementation, thus hiding the
946 -- subprogram body from SPARK tool-set. No violation of the
947 -- SPARK restriction should be issued on nodes in a hidden
948 -- part, which is obtained by marking such hidden parts.
950 if Token
= Tok_SPARK_Hide
then
951 Body_Is_Hidden_In_SPARK
:= True;
952 Hidden_Region_Start
:= Token_Ptr
;
953 Scan
; -- past HIDE directive
955 Body_Is_Hidden_In_SPARK
:= False;
958 Parse_Decls_Begin_End
(Body_Node
);
960 if Body_Is_Hidden_In_SPARK
then
961 Set_Hidden_Part_In_SPARK
(Hidden_Region_Start
, Token_Ptr
);
966 end Scan_Body_Or_Expression_Function
;
969 -- Processing for subprogram declaration
971 <<Subprogram_Declaration
>>
973 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
974 Set_Specification
(Decl_Node
, Specification_Node
);
975 Aspects
:= Get_Aspect_Specifications
(Semicolon
=> False);
977 -- Aspects may be present on a subprogram body. The source parsed
978 -- so far is that of its specification. Go parse the body and attach
979 -- the collected aspects, if any, to the body.
981 if Token
= Tok_Is
then
983 goto Subprogram_Body
;
986 if Is_Non_Empty_List
(Aspects
) then
987 Set_Parent
(Aspects
, Decl_Node
);
988 Set_Aspect_Specifications
(Decl_Node
, Aspects
);
994 -- If this is a context in which a subprogram body is permitted,
995 -- set active SIS entry in case (see section titled "Handling
996 -- Semicolon Used in Place of IS" in body of Parser package)
997 -- Note that SIS_Missing_Semicolon_Message is already set properly.
1001 -- Disconnnect this processing if we have scanned a null procedure
1002 -- because in this case the spec is complete anyway with no body.
1004 and then (Nkind
(Specification_Node
) /= N_Procedure_Specification
1005 or else not Null_Present
(Specification_Node
))
1007 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
1008 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
1009 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
1010 SIS_Declaration_Node
:= Decl_Node
;
1011 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
1012 SIS_Entry_Active
:= True;
1019 ---------------------------------
1020 -- 6.1 Subprogram Declaration --
1021 ---------------------------------
1023 -- Parsed by P_Subprogram (6.1)
1025 ------------------------------------------
1026 -- 6.1 Abstract Subprogram Declaration --
1027 ------------------------------------------
1029 -- Parsed by P_Subprogram (6.1)
1031 -----------------------------------
1032 -- 6.1 Subprogram Specification --
1033 -----------------------------------
1035 -- SUBPROGRAM_SPECIFICATION ::=
1036 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1037 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1039 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1041 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1043 -- Subprogram specifications that appear in subprogram declarations
1044 -- are parsed by P_Subprogram (6.1). This routine is used in other
1045 -- contexts where subprogram specifications occur.
1047 -- Note: this routine does not affect the scope stack in any way
1049 -- Error recovery: can raise Error_Resync
1051 function P_Subprogram_Specification
return Node_Id
is
1052 Specification_Node
: Node_Id
;
1053 Result_Not_Null
: Boolean;
1054 Result_Node
: Node_Id
;
1057 if Token
= Tok_Function
then
1058 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
1059 Scan
; -- past FUNCTION
1061 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
1062 Set_Parameter_Specifications
1063 (Specification_Node
, P_Parameter_Profile
);
1064 Check_Junk_Semicolon_Before_Return
;
1067 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1069 -- Ada 2005 (AI-318-02)
1071 if Token
= Tok_Access
then
1072 if Ada_Version
< Ada_2005
then
1074 ("anonymous access result type is an Ada 2005 extension");
1075 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
1078 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
1081 Result_Node
:= P_Subtype_Mark
;
1082 No_Constraint_Maybe_Expr_Func
;
1085 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
1086 Set_Result_Definition
(Specification_Node
, Result_Node
);
1087 return Specification_Node
;
1089 elsif Token
= Tok_Procedure
then
1090 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
1091 Scan
; -- past PROCEDURE
1093 Set_Defining_Unit_Name
1094 (Specification_Node
, P_Defining_Program_Unit_Name
);
1095 Set_Parameter_Specifications
1096 (Specification_Node
, P_Parameter_Profile
);
1097 return Specification_Node
;
1100 Error_Msg_SC
("subprogram specification expected");
1103 end P_Subprogram_Specification
;
1105 ---------------------
1106 -- 6.1 Designator --
1107 ---------------------
1110 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1112 -- The caller has checked that the initial token is an identifier,
1113 -- operator symbol, or string literal. Note that we don't bother to
1114 -- do much error diagnosis in this routine, since it is only used for
1115 -- the label on END lines, and the routines in package Par.Endh will
1116 -- check that the label is appropriate.
1118 -- Error recovery: cannot raise Error_Resync
1120 function P_Designator
return Node_Id
is
1121 Ident_Node
: Node_Id
;
1122 Name_Node
: Node_Id
;
1123 Prefix_Node
: Node_Id
;
1125 function Real_Dot
return Boolean;
1126 -- Tests if a current token is an interesting period, i.e. is followed
1127 -- by an identifier or operator symbol or string literal. If not, it is
1128 -- probably just incorrect punctuation to be caught by our caller. Note
1129 -- that the case of an operator symbol or string literal is also an
1130 -- error, but that is an error that we catch here. If the result is
1131 -- True, a real dot has been scanned and we are positioned past it,
1132 -- if the result is False, the scan position is unchanged.
1138 function Real_Dot
return Boolean is
1139 Scan_State
: Saved_Scan_State
;
1142 if Token
/= Tok_Dot
then
1146 Save_Scan_State
(Scan_State
);
1149 if Token
= Tok_Identifier
1150 or else Token
= Tok_Operator_Symbol
1151 or else Token
= Tok_String_Literal
1156 Restore_Scan_State
(Scan_State
);
1162 -- Start of processing for P_Designator
1165 Ident_Node
:= Token_Node
;
1166 Scan
; -- past initial token
1168 if Prev_Token
= Tok_Operator_Symbol
1169 or else Prev_Token
= Tok_String_Literal
1170 or else not Real_Dot
1177 Prefix_Node
:= Ident_Node
;
1179 -- Loop through child names, on entry to this loop, Prefix contains
1180 -- the name scanned so far, and Ident_Node is the last identifier.
1183 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
1184 Set_Prefix
(Name_Node
, Prefix_Node
);
1185 Ident_Node
:= P_Identifier
;
1186 Set_Selector_Name
(Name_Node
, Ident_Node
);
1187 Prefix_Node
:= Name_Node
;
1188 exit when not Real_Dot
;
1191 -- On exit from the loop, Ident_Node is the last identifier scanned,
1192 -- i.e. the defining identifier, and Prefix_Node is a node for the
1193 -- entire name, structured (incorrectly) as a selected component.
1195 Name_Node
:= Prefix
(Prefix_Node
);
1196 Change_Node
(Prefix_Node
, N_Designator
);
1197 Set_Name
(Prefix_Node
, Name_Node
);
1198 Set_Identifier
(Prefix_Node
, Ident_Node
);
1203 when Error_Resync
=>
1204 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1211 ------------------------------
1212 -- 6.1 Defining Designator --
1213 ------------------------------
1215 -- DEFINING_DESIGNATOR ::=
1216 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1218 -- Error recovery: cannot raise Error_Resync
1220 function P_Defining_Designator
return Node_Id
is
1222 if Token
= Tok_Operator_Symbol
then
1223 return P_Defining_Operator_Symbol
;
1225 elsif Token
= Tok_String_Literal
then
1226 Error_Msg_SC
("invalid operator name");
1227 Scan
; -- past junk string
1231 return P_Defining_Program_Unit_Name
;
1233 end P_Defining_Designator
;
1235 -------------------------------------
1236 -- 6.1 Defining Program Unit Name --
1237 -------------------------------------
1239 -- DEFINING_PROGRAM_UNIT_NAME ::=
1240 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1242 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1244 -- Error recovery: cannot raise Error_Resync
1246 function P_Defining_Program_Unit_Name
return Node_Id
is
1247 Ident_Node
: Node_Id
;
1248 Name_Node
: Node_Id
;
1249 Prefix_Node
: Node_Id
;
1252 -- Set identifier casing if not already set and scan initial identifier
1254 if Token
= Tok_Identifier
1255 and then Identifier_Casing
(Current_Source_File
) = Unknown
1257 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
1260 Ident_Node
:= P_Identifier
(C_Dot
);
1261 Merge_Identifier
(Ident_Node
, Tok_Return
);
1263 -- Normal case (not child library unit name)
1265 if Token
/= Tok_Dot
then
1266 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1267 Warn_If_Standard_Redefinition
(Ident_Node
);
1270 -- Child library unit name case
1273 if Scope
.Last
> 1 then
1274 Error_Msg_SP
("child unit allowed only at library level");
1277 elsif Ada_Version
= Ada_83
then
1278 Error_Msg_SP
("(Ada 83) child unit not allowed!");
1282 Prefix_Node
:= Ident_Node
;
1284 -- Loop through child names, on entry to this loop, Prefix contains
1285 -- the name scanned so far, and Ident_Node is the last identifier.
1288 exit when Token
/= Tok_Dot
;
1289 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
1290 Scan
; -- past period
1291 Set_Prefix
(Name_Node
, Prefix_Node
);
1292 Ident_Node
:= P_Identifier
(C_Dot
);
1293 Set_Selector_Name
(Name_Node
, Ident_Node
);
1294 Prefix_Node
:= Name_Node
;
1297 -- On exit from the loop, Ident_Node is the last identifier scanned,
1298 -- i.e. the defining identifier, and Prefix_Node is a node for the
1299 -- entire name, structured (incorrectly) as a selected component.
1301 Name_Node
:= Prefix
(Prefix_Node
);
1302 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
1303 Set_Name
(Prefix_Node
, Name_Node
);
1304 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1305 Warn_If_Standard_Redefinition
(Ident_Node
);
1306 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
1308 -- All set with unit name parsed
1314 when Error_Resync
=>
1315 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1320 end P_Defining_Program_Unit_Name
;
1322 --------------------------
1323 -- 6.1 Operator Symbol --
1324 --------------------------
1326 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1328 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1330 -----------------------------------
1331 -- 6.1 Defining Operator Symbol --
1332 -----------------------------------
1334 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1336 -- The caller has checked that the initial symbol is an operator symbol
1338 function P_Defining_Operator_Symbol
return Node_Id
is
1342 Op_Node
:= Token_Node
;
1343 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
1344 Scan
; -- past operator symbol
1346 end P_Defining_Operator_Symbol
;
1348 ----------------------------
1349 -- 6.1 Parameter_Profile --
1350 ----------------------------
1352 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1354 -- Empty is returned if no formal part is present
1356 -- Error recovery: cannot raise Error_Resync
1358 function P_Parameter_Profile
return List_Id
is
1360 if Token
= Tok_Left_Paren
then
1361 Scan
; -- part left paren
1362 return P_Formal_Part
;
1366 end P_Parameter_Profile
;
1368 ---------------------------------------
1369 -- 6.1 Parameter And Result Profile --
1370 ---------------------------------------
1372 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1373 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1375 ----------------------
1376 -- 6.1 Formal part --
1377 ----------------------
1379 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1381 -- PARAMETER_SPECIFICATION ::=
1382 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1383 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1384 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1385 -- [:= DEFAULT_EXPRESSION]
1387 -- This scans the construct Formal_Part. The caller has already checked
1388 -- that the initial token is a left parenthesis, and skipped past it, so
1389 -- that on entry Token is the first token following the left parenthesis.
1391 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1393 -- Error recovery: cannot raise Error_Resync
1395 function P_Formal_Part
return List_Id
is
1396 Specification_List
: List_Id
;
1397 Specification_Node
: Node_Id
;
1398 Scan_State
: Saved_Scan_State
;
1401 Ident_Sloc
: Source_Ptr
;
1402 Not_Null_Present
: Boolean := False;
1403 Not_Null_Sloc
: Source_Ptr
;
1405 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1406 -- This array holds the list of defining identifiers. The upper bound
1407 -- of 4096 is intended to be essentially infinite, and we do not even
1408 -- bother to check for it being exceeded.
1411 Specification_List
:= New_List
;
1412 Specification_Loop
: loop
1414 if Token
= Tok_Pragma
then
1415 Error_Msg_SC
("pragma not allowed in formal part");
1416 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
1419 Ignore
(Tok_Left_Paren
);
1420 Ident_Sloc
:= Token_Ptr
;
1421 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1425 exit Ident_Loop
when Token
= Tok_Colon
;
1427 -- The only valid tokens are colon and comma, so if we have
1428 -- neither do a bit of investigation to see which is the
1429 -- better choice for insertion.
1431 if Token
/= Tok_Comma
then
1433 -- Assume colon if ALIASED, IN or OUT keyword found
1435 exit Ident_Loop
when Token
= Tok_Aliased
or else
1436 Token
= Tok_In
or else
1439 -- Otherwise scan ahead
1441 Save_Scan_State
(Scan_State
);
1444 -- If we run into a semicolon, then assume that a
1445 -- colon was missing, e.g. Parms (X Y; ...). Also
1446 -- assume missing colon on EOF (a real disaster)
1447 -- and on a right paren, e.g. Parms (X Y), and also
1448 -- on an assignment symbol, e.g. Parms (X Y := ..)
1450 if Token
= Tok_Semicolon
1451 or else Token
= Tok_Right_Paren
1452 or else Token
= Tok_EOF
1453 or else Token
= Tok_Colon_Equal
1455 Restore_Scan_State
(Scan_State
);
1458 -- If we run into a colon, assume that we had a missing
1459 -- comma, e.g. Parms (A B : ...). Also assume a missing
1460 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1462 elsif Token
= Tok_Colon
1463 or else Token
= Tok_Comma
1465 Restore_Scan_State
(Scan_State
);
1470 end loop Look_Ahead
;
1473 -- Here if a comma is present, or to be assumed
1476 Num_Idents
:= Num_Idents
+ 1;
1477 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1478 end loop Ident_Loop
;
1480 -- Fall through the loop on encountering a colon, or deciding
1481 -- that there is a missing colon.
1485 -- If there are multiple identifiers, we repeatedly scan the
1486 -- type and initialization expression information by resetting
1487 -- the scan pointer (so that we get completely separate trees
1488 -- for each occurrence).
1490 if Num_Idents
> 1 then
1491 Save_Scan_State
(Scan_State
);
1494 -- Loop through defining identifiers in list
1498 Ident_List_Loop
: loop
1499 Specification_Node
:=
1500 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1501 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1503 -- Scan possible ALIASED for Ada 2012 (AI-142)
1505 if Token
= Tok_Aliased
then
1506 if Ada_Version
< Ada_2012
then
1507 Error_Msg_Ada_2012_Feature
1508 ("ALIASED parameter", Token_Ptr
);
1510 Set_Aliased_Present
(Specification_Node
);
1513 Scan
; -- past ALIASED
1516 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1518 Not_Null_Sloc
:= Token_Ptr
;
1520 P_Null_Exclusion
(Allow_Anonymous_In_95
=> True);
1522 -- Case of ACCESS keyword present
1524 if Token
= Tok_Access
then
1525 Set_Null_Exclusion_Present
1526 (Specification_Node
, Not_Null_Present
);
1528 if Ada_Version
= Ada_83
then
1529 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1533 (Specification_Node
,
1534 P_Access_Definition
(Not_Null_Present
));
1536 -- Case of IN or OUT present
1539 if Token
= Tok_In
or else Token
= Tok_Out
then
1540 if Not_Null_Present
then
1542 ("`NOT NULL` can only be used with `ACCESS`",
1545 if Token
= Tok_In
then
1547 ("\`IN` not allowed together with `ACCESS`",
1551 ("\`OUT` not allowed together with `ACCESS`",
1556 P_Mode
(Specification_Node
);
1557 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1560 Set_Null_Exclusion_Present
1561 (Specification_Node
, Not_Null_Present
);
1563 if Token
= Tok_Procedure
1565 Token
= Tok_Function
1567 Error_Msg_SC
("formal subprogram parameter not allowed");
1570 if Token
= Tok_Left_Paren
then
1571 Discard_Junk_List
(P_Formal_Part
);
1574 if Token
= Tok_Return
then
1576 Discard_Junk_Node
(P_Subtype_Mark
);
1579 Set_Parameter_Type
(Specification_Node
, Error
);
1582 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1587 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1590 Set_Prev_Ids
(Specification_Node
, True);
1593 if Ident
< Num_Idents
then
1594 Set_More_Ids
(Specification_Node
, True);
1597 Append
(Specification_Node
, Specification_List
);
1598 exit Ident_List_Loop
when Ident
= Num_Idents
;
1600 Restore_Scan_State
(Scan_State
);
1601 end loop Ident_List_Loop
;
1604 when Error_Resync
=>
1605 Resync_Semicolon_List
;
1608 if Token
= Tok_Semicolon
then
1609 Save_Scan_State
(Scan_State
);
1610 Scan
; -- past semicolon
1612 -- If we have RETURN or IS after the semicolon, then assume
1613 -- that semicolon should have been a right parenthesis and exit
1615 if Token
= Tok_Is
or else Token
= Tok_Return
then
1616 Error_Msg_SP
-- CODEFIX
1617 ("|"";"" should be "")""");
1618 exit Specification_Loop
;
1621 -- If we have a declaration keyword after the semicolon, then
1622 -- assume we had a missing right parenthesis and terminate list
1624 if Token
in Token_Class_Declk
then
1625 Error_Msg_AP
-- CODEFIX
1627 Restore_Scan_State
(Scan_State
);
1628 exit Specification_Loop
;
1631 elsif Token
= Tok_Right_Paren
then
1632 Scan
; -- past right paren
1633 exit Specification_Loop
;
1635 -- Special check for common error of using comma instead of semicolon
1637 elsif Token
= Tok_Comma
then
1641 -- Special check for omitted separator
1643 elsif Token
= Tok_Identifier
then
1646 -- If nothing sensible, skip to next semicolon or right paren
1650 Resync_Semicolon_List
;
1652 if Token
= Tok_Semicolon
then
1653 Scan
; -- past semicolon
1656 exit Specification_Loop
;
1659 end loop Specification_Loop
;
1661 return Specification_List
;
1664 ----------------------------------
1665 -- 6.1 Parameter Specification --
1666 ----------------------------------
1668 -- Parsed by P_Formal_Part (6.1)
1674 -- MODE ::= [in] | in out | out
1676 -- There is no explicit node in the tree for the Mode. Instead the
1677 -- In_Present and Out_Present flags are set in the parent node to
1678 -- record the presence of keywords specifying the mode.
1680 -- Error_Recovery: cannot raise Error_Resync
1682 procedure P_Mode
(Node
: Node_Id
) is
1684 if Token
= Tok_In
then
1686 Set_In_Present
(Node
, True);
1688 if Style
.Mode_In_Check
and then Token
/= Tok_Out
then
1689 Error_Msg_SP
-- CODEFIX
1690 ("(style) IN should be omitted");
1693 -- Since Ada 2005, formal objects can have an anonymous access type,
1694 -- and of course carry a mode indicator.
1696 if Token
= Tok_Access
1697 and then Nkind
(Node
) /= N_Formal_Object_Declaration
1699 Error_Msg_SP
("IN not allowed together with ACCESS");
1700 Scan
; -- past ACCESS
1704 if Token
= Tok_Out
then
1706 Set_Out_Present
(Node
, True);
1709 if Token
= Tok_In
then
1710 Error_Msg_SC
("IN must precede OUT in parameter mode");
1712 Set_In_Present
(Node
, True);
1716 --------------------------
1717 -- 6.3 Subprogram Body --
1718 --------------------------
1720 -- Parsed by P_Subprogram (6.1)
1722 -----------------------------------
1723 -- 6.4 Procedure Call Statement --
1724 -----------------------------------
1726 -- Parsed by P_Sequence_Of_Statements (5.1)
1728 ------------------------
1729 -- 6.4 Function Call --
1730 ------------------------
1732 -- Parsed by P_Name (4.1)
1734 --------------------------------
1735 -- 6.4 Actual Parameter Part --
1736 --------------------------------
1738 -- Parsed by P_Name (4.1)
1740 --------------------------------
1741 -- 6.4 Parameter Association --
1742 --------------------------------
1744 -- Parsed by P_Name (4.1)
1746 ------------------------------------
1747 -- 6.4 Explicit Actual Parameter --
1748 ------------------------------------
1750 -- Parsed by P_Name (4.1)
1752 ---------------------------
1753 -- 6.5 Return Statement --
1754 ---------------------------
1756 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1758 -- EXTENDED_RETURN_STATEMENT ::=
1759 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1760 -- [:= EXPRESSION] [do
1761 -- HANDLED_SEQUENCE_OF_STATEMENTS
1764 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1766 -- RETURN_STATEMENT ::= return [EXPRESSION];
1768 -- Error recovery: can raise Error_Resync
1770 procedure P_Return_Subtype_Indication
(Decl_Node
: Node_Id
) is
1772 -- Note: We don't need to check Ada_Version here, because this is
1773 -- only called in >= Ada 2005 cases anyway.
1775 Not_Null_Present
: constant Boolean := P_Null_Exclusion
;
1778 Set_Null_Exclusion_Present
(Decl_Node
, Not_Null_Present
);
1780 if Token
= Tok_Access
then
1781 Set_Object_Definition
1782 (Decl_Node
, P_Access_Definition
(Not_Null_Present
));
1784 Set_Object_Definition
1785 (Decl_Node
, P_Subtype_Indication
(Not_Null_Present
));
1787 end P_Return_Subtype_Indication
;
1789 -- Error recovery: can raise Error_Resync
1791 function P_Return_Object_Declaration
return Node_Id
is
1792 Return_Obj
: Node_Id
;
1793 Decl_Node
: Node_Id
;
1796 Return_Obj
:= Token_Node
;
1797 Change_Identifier_To_Defining_Identifier
(Return_Obj
);
1798 Warn_If_Standard_Redefinition
(Return_Obj
);
1799 Decl_Node
:= New_Node
(N_Object_Declaration
, Token_Ptr
);
1800 Set_Defining_Identifier
(Decl_Node
, Return_Obj
);
1802 Scan
; -- past identifier
1805 -- First an error check, if we have two identifiers in a row, a likely
1806 -- possibility is that the first of the identifiers is an incorrectly
1807 -- spelled keyword. See similar check in P_Identifier_Declarations.
1809 if Token
= Tok_Identifier
then
1811 SS
: Saved_Scan_State
;
1815 Save_Scan_State
(SS
);
1816 Scan
; -- past initial identifier
1817 I2
:= (Token
= Tok_Identifier
);
1818 Restore_Scan_State
(SS
);
1822 (Bad_Spelling_Of
(Tok_Access
) or else
1823 Bad_Spelling_Of
(Tok_Aliased
) or else
1824 Bad_Spelling_Of
(Tok_Constant
))
1831 -- We allow "constant" here (as in "return Result : constant
1832 -- T..."). This is not in the latest RM, but the ARG is considering an
1833 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1835 if Token
= Tok_Constant
then
1836 Scan
; -- past CONSTANT
1837 Set_Constant_Present
(Decl_Node
);
1839 if Token
= Tok_Aliased
then
1840 Error_Msg_SC
-- CODEFIX
1841 ("ALIASED should be before CONSTANT");
1842 Scan
; -- past ALIASED
1843 Set_Aliased_Present
(Decl_Node
);
1846 elsif Token
= Tok_Aliased
then
1847 Scan
; -- past ALIASED
1848 Set_Aliased_Present
(Decl_Node
);
1850 -- The restrictions on the use of aliased in an extended return
1851 -- are semantic, not syntactic.
1853 if Token
= Tok_Constant
then
1854 Scan
; -- past CONSTANT
1855 Set_Constant_Present
(Decl_Node
);
1859 P_Return_Subtype_Indication
(Decl_Node
);
1861 if Token
= Tok_Colon_Equal
then
1863 Set_Expression
(Decl_Node
, P_Expression_No_Right_Paren
);
1867 end P_Return_Object_Declaration
;
1869 -- Error recovery: can raise Error_Resync
1871 function P_Return_Statement
return Node_Id
is
1872 -- The caller has checked that the initial token is RETURN
1874 function Is_Simple
return Boolean;
1875 -- Scan state is just after RETURN (and is left that way). Determine
1876 -- whether this is a simple or extended return statement by looking
1877 -- ahead for "identifier :", which implies extended.
1883 function Is_Simple
return Boolean is
1884 Scan_State
: Saved_Scan_State
;
1885 Result
: Boolean := True;
1888 if Token
= Tok_Identifier
then
1889 Save_Scan_State
(Scan_State
); -- at identifier
1890 Scan
; -- past identifier
1892 if Token
= Tok_Colon
then
1893 Result
:= False; -- It's an extended_return_statement.
1896 Restore_Scan_State
(Scan_State
); -- to identifier
1902 Ret_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1903 Ret_Strt
: constant Column_Number
:= Start_Column
;
1906 -- Start of processing for P_Return_Statement
1909 Scan
; -- past RETURN
1911 -- Simple_return_statement, no expression, return an
1912 -- N_Simple_Return_Statement node with the expression field left Empty.
1914 if Token
= Tok_Semicolon
then
1916 Ret_Node
:= New_Node
(N_Simple_Return_Statement
, Ret_Sloc
);
1921 -- Simple_return_statement with expression
1923 -- We avoid trying to scan an expression if we are at an
1924 -- expression terminator since in that case the best error
1925 -- message is probably that we have a missing semicolon.
1928 Ret_Node
:= New_Node
(N_Simple_Return_Statement
, Ret_Sloc
);
1930 if Token
not in Token_Class_Eterm
then
1931 Set_Expression
(Ret_Node
, P_Expression_No_Right_Paren
);
1934 -- Extended_return_statement (Ada 2005 only -- AI-318):
1937 if Ada_Version
< Ada_2005
then
1939 (" extended_return_statement is an Ada 2005 extension");
1940 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
1943 Ret_Node
:= New_Node
(N_Extended_Return_Statement
, Ret_Sloc
);
1944 Set_Return_Object_Declarations
1945 (Ret_Node
, New_List
(P_Return_Object_Declaration
));
1947 if Token
= Tok_Do
then
1949 Scope
.Table
(Scope
.Last
).Ecol
:= Ret_Strt
;
1950 Scope
.Table
(Scope
.Last
).Etyp
:= E_Return
;
1951 Scope
.Table
(Scope
.Last
).Labl
:= Error
;
1952 Scope
.Table
(Scope
.Last
).Sloc
:= Ret_Sloc
;
1955 Set_Handled_Statement_Sequence
1956 (Ret_Node
, P_Handled_Sequence_Of_Statements
);
1959 -- Do we need to handle Error_Resync here???
1967 end P_Return_Statement
;