1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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.
43 -- Set the Null_Exclusion_Present and Object_Definition fields of
46 procedure Check_Junk_Semicolon_Before_Return
;
48 -- Check for common error of junk semicolon before RETURN keyword of
49 -- function specification. If present, skip over it with appropriate
50 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
51 -- routine also deals with a possibly misspelled version of Return.
53 ----------------------------------------
54 -- Check_Junk_Semicolon_Before_Return --
55 ----------------------------------------
57 procedure Check_Junk_Semicolon_Before_Return
is
58 Scan_State
: Saved_Scan_State
;
61 if Token
= Tok_Semicolon
then
62 Save_Scan_State
(Scan_State
);
63 Scan
; -- past the semicolon
65 if Token
= Tok_Return
then
66 Restore_Scan_State
(Scan_State
);
67 Error_Msg_SC
-- CODEFIX
68 ("|extra "";"" ignored");
69 Scan
; -- rescan past junk semicolon
71 Restore_Scan_State
(Scan_State
);
74 elsif Bad_Spelling_Of
(Tok_Return
) then
77 end Check_Junk_Semicolon_Before_Return
;
79 -----------------------------------------------------
80 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
81 -----------------------------------------------------
83 -- This routine scans out a subprogram declaration, subprogram body,
84 -- subprogram renaming declaration or subprogram generic instantiation.
85 -- It also handles the new Ada 2012 expression function form
87 -- SUBPROGRAM_DECLARATION ::=
88 -- SUBPROGRAM_SPECIFICATION
89 -- [ASPECT_SPECIFICATIONS];
91 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
92 -- SUBPROGRAM_SPECIFICATION is abstract
93 -- [ASPECT_SPECIFICATIONS];
95 -- SUBPROGRAM_SPECIFICATION ::=
96 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
97 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
99 -- PARAMETER_PROFILE ::= [FORMAL_PART]
101 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
103 -- SUBPROGRAM_BODY ::=
104 -- SUBPROGRAM_SPECIFICATION is
107 -- HANDLED_SEQUENCE_OF_STATEMENTS
110 -- SUBPROGRAM_RENAMING_DECLARATION ::=
111 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME
112 -- [ASPECT_SPECIFICATIONS];
114 -- SUBPROGRAM_BODY_STUB ::=
115 -- SUBPROGRAM_SPECIFICATION is separate;
117 -- GENERIC_INSTANTIATION ::=
118 -- procedure DEFINING_PROGRAM_UNIT_NAME is
119 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
120 -- | function DEFINING_DESIGNATOR is
121 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
123 -- NULL_PROCEDURE_DECLARATION ::=
124 -- SUBPROGRAM_SPECIFICATION is null;
126 -- Null procedures are an Ada 2005 feature. A null procedure declaration
127 -- is classified as a basic declarative item, but it is parsed here, with
128 -- other subprogram constructs.
130 -- EXPRESSION_FUNCTION ::=
131 -- FUNCTION SPECIFICATION IS (EXPRESSION)
132 -- [ASPECT_SPECIFICATIONS];
134 -- The value in Pf_Flags indicates which of these possible declarations
135 -- is acceptable to the caller:
137 -- Pf_Flags.Decl Set if declaration OK
138 -- Pf_Flags.Gins Set if generic instantiation OK
139 -- Pf_Flags.Pbod Set if proper body OK
140 -- Pf_Flags.Rnam Set if renaming declaration OK
141 -- Pf_Flags.Stub Set if body stub OK
142 -- Pf_Flags.Pexp Set if expression function OK
144 -- If an inappropriate form is encountered, it is scanned out but an
145 -- error message indicating that it is appearing in an inappropriate
146 -- context is issued. The only possible values for Pf_Flags are those
147 -- defined as constants in the Par package.
149 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
150 -- NOT or OVERRIDING.
152 -- Error recovery: cannot raise Error_Resync
154 function P_Subprogram
(Pf_Flags
: Pf_Rec
) return Node_Id
is
155 Specification_Node
: Node_Id
;
158 Fpart_List
: List_Id
;
159 Fpart_Sloc
: Source_Ptr
;
160 Result_Not_Null
: Boolean := False;
161 Result_Node
: Node_Id
;
165 Rename_Node
: Node_Id
;
166 Absdec_Node
: Node_Id
;
168 Fproc_Sloc
: Source_Ptr
;
170 Scan_State
: Saved_Scan_State
;
172 -- Flags for optional overriding indication. Two flags are needed,
173 -- to distinguish positive and negative overriding indicators from
174 -- the absence of any indicator.
176 Is_Overriding
: Boolean := False;
177 Not_Overriding
: Boolean := False;
180 -- Set up scope stack entry. Note that the Labl field will be set later
182 SIS_Entry_Active
:= False;
183 SIS_Missing_Semicolon_Message
:= No_Error_Msg
;
185 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
186 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
187 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
188 Scope
.Table
(Scope
.Last
).Lreq
:= False;
190 Aspects
:= Empty_List
;
192 -- Ada 2005: Scan leading NOT OVERRIDING indicator
194 if Token
= Tok_Not
then
197 if Token
= Tok_Overriding
then
198 Scan
; -- past OVERRIDING
199 Not_Overriding
:= True;
201 -- Overriding keyword used in non Ada 2005 mode
203 elsif Token
= Tok_Identifier
204 and then Token_Name
= Name_Overriding
206 Error_Msg_SC
("overriding indicator is an Ada 2005 extension");
207 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
208 Scan
; -- past Overriding
209 Not_Overriding
:= True;
212 Error_Msg_SC
-- CODEFIX
213 ("OVERRIDING expected!");
216 -- Ada 2005: scan leading OVERRIDING indicator
218 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
219 -- declaration circuit already gave an error message and changed the
220 -- token to Tok_Overriding.
222 elsif Token
= Tok_Overriding
then
223 Scan
; -- past OVERRIDING
224 Is_Overriding
:= True;
227 if Is_Overriding
or else Not_Overriding
then
229 -- Note that if we are not in Ada_2005 mode, error messages have
230 -- already been given, so no need to give another message here.
232 -- An overriding indicator is allowed for subprogram declarations,
233 -- bodies (including subunits), renamings, stubs, and instantiations.
234 -- The test against Pf_Decl_Pbod is added to account for the case of
235 -- subprograms declared in a protected type, where only subprogram
236 -- declarations and bodies can occur. The Pf_Pbod case is for
239 if Pf_Flags
/= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
241 Pf_Flags
/= Pf_Decl_Pbod_Pexp
243 Pf_Flags
/= Pf_Pbod_Pexp
245 Error_Msg_SC
("overriding indicator not allowed here!");
247 elsif Token
/= Tok_Function
and then Token
/= Tok_Procedure
then
248 Error_Msg_SC
-- CODEFIX
249 ("FUNCTION or PROCEDURE expected!");
253 Func
:= (Token
= Tok_Function
);
254 Fproc_Sloc
:= Token_Ptr
;
255 Scan
; -- past FUNCTION or PROCEDURE
260 Name_Node
:= P_Defining_Designator
;
262 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
263 and then Scope
.Last
= 1
265 Error_Msg_SP
("operator symbol not allowed at library level");
266 Name_Node
:= New_Entity
(N_Defining_Identifier
, Sloc
(Name_Node
));
268 -- Set name from file name, we need some junk name, and that's
269 -- as good as anything. This is only approximate, since we do
270 -- not do anything with non-standard name translations.
272 Get_Name_String
(File_Name
(Current_Source_File
));
274 for J
in 1 .. Name_Len
loop
275 if Name_Buffer
(J
) = '.' then
281 Set_Chars
(Name_Node
, Name_Find
);
282 Set_Error_Posted
(Name_Node
);
286 Name_Node
:= P_Defining_Program_Unit_Name
;
289 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
292 -- Deal with generic instantiation, the one case in which we do not
293 -- have a subprogram specification as part of whatever we are parsing
295 if Token
= Tok_Is
then
296 Save_Scan_State
(Scan_State
); -- at the IS
297 T_Is
; -- checks for redundant IS
299 if Token
= Tok_New
then
300 if not Pf_Flags
.Gins
then
301 Error_Msg_SC
("generic instantiation not allowed here!");
307 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
308 Set_Name
(Inst_Node
, P_Function_Name
);
310 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
311 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
314 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
315 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
316 P_Aspect_Specifications
(Inst_Node
);
317 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
319 if Is_Overriding
then
320 Set_Must_Override
(Inst_Node
);
322 elsif Not_Overriding
then
323 Set_Must_Not_Override
(Inst_Node
);
329 Restore_Scan_State
(Scan_State
); -- to the IS
333 -- If not a generic instantiation, then we definitely have a subprogram
334 -- specification (all possibilities at this stage include one here)
336 Fpart_Sloc
:= Token_Ptr
;
338 Check_Misspelling_Of
(Tok_Return
);
340 -- Scan formal part. First a special error check. If we have an
341 -- identifier here, then we have a definite error. If this identifier
342 -- is on the same line as the designator, then we assume it is the
343 -- first formal after a missing left parenthesis
345 if Token
= Tok_Identifier
346 and then not Token_Is_At_Start_Of_Line
348 T_Left_Paren
; -- to generate message
349 Fpart_List
:= P_Formal_Part
;
351 -- Otherwise scan out an optional formal part in the usual manner
354 Fpart_List
:= P_Parameter_Profile
;
357 -- We treat what we have as a function specification if FUNCTION was
358 -- used, or if a RETURN is present. This gives better error recovery
359 -- since later RETURN statements will be valid in either case.
361 Check_Junk_Semicolon_Before_Return
;
362 Result_Node
:= Error
;
364 if Token
= Tok_Return
then
367 ("PROCEDURE should be FUNCTION", Fproc_Sloc
);
373 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
375 -- Ada 2005 (AI-318-02)
377 if Token
= Tok_Access
then
378 if Ada_Version
< Ada_2005
then
380 ("anonymous access result type is an Ada 2005 extension");
381 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
384 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
387 Result_Node
:= P_Subtype_Mark
;
392 -- Skip extra parenthesis at end of formal part
394 Ignore
(Tok_Right_Paren
);
396 -- For function, scan result subtype
401 if Prev_Token
= Tok_Return
then
402 Result_Node
:= P_Subtype_Mark
;
408 Specification_Node
:=
409 New_Node
(N_Function_Specification
, Fproc_Sloc
);
411 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
412 Set_Result_Definition
(Specification_Node
, Result_Node
);
415 Specification_Node
:=
416 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
419 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
420 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
422 if Is_Overriding
then
423 Set_Must_Override
(Specification_Node
);
425 elsif Not_Overriding
then
426 Set_Must_Not_Override
(Specification_Node
);
429 -- Error check: barriers not allowed on protected functions/procedures
431 if Token
= Tok_When
then
433 Error_Msg_SC
("barrier not allowed on function, only on entry");
435 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
439 Discard_Junk_Node
(P_Expression
);
442 -- Deal with semicolon followed by IS. We want to treat this as IS
444 if Token
= Tok_Semicolon
then
445 Save_Scan_State
(Scan_State
);
446 Scan
; -- past semicolon
448 if Token
= Tok_Is
then
449 Error_Msg_SP
-- CODEFIX
450 ("extra "";"" ignored");
452 Restore_Scan_State
(Scan_State
);
456 -- Subprogram declaration ended by aspect specifications
458 if Aspect_Specifications_Present
then
459 goto Subprogram_Declaration
;
461 -- Deal with case of semicolon ending a subprogram declaration
463 elsif Token
= Tok_Semicolon
then
464 if not Pf_Flags
.Decl
then
468 Save_Scan_State
(Scan_State
);
469 Scan
; -- past semicolon
471 -- If semicolon is immediately followed by IS, then ignore the
472 -- semicolon, and go process the body.
474 if Token
= Tok_Is
then
475 Error_Msg_SP
-- CODEFIX
476 ("|extra "";"" ignored");
477 T_Is
; -- scan past IS
478 goto Subprogram_Body
;
480 -- If BEGIN follows in an appropriate column, we immediately
481 -- commence the error action of assuming that the previous
482 -- subprogram declaration should have been a subprogram body,
483 -- i.e. that the terminating semicolon should have been IS.
485 elsif Token
= Tok_Begin
486 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
488 Error_Msg_SP
-- CODEFIX
489 ("|"";"" should be IS!");
490 goto Subprogram_Body
;
493 Restore_Scan_State
(Scan_State
);
494 goto Subprogram_Declaration
;
497 -- Case of not followed by semicolon
500 -- Subprogram renaming declaration case
502 Check_Misspelling_Of
(Tok_Renames
);
504 if Token
= Tok_Renames
then
505 if not Pf_Flags
.Rnam
then
506 Error_Msg_SC
("renaming declaration not allowed here!");
510 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
511 Scan
; -- past RENAMES
512 Set_Name
(Rename_Node
, P_Name
);
513 Set_Specification
(Rename_Node
, Specification_Node
);
514 P_Aspect_Specifications
(Rename_Node
);
519 -- Case of IS following subprogram specification
521 elsif Token
= Tok_Is
then
522 T_Is
; -- ignore redundant Is's
524 if Token_Name
= Name_Abstract
then
525 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
528 -- Deal nicely with (now obsolete) use of <> in place of abstract
530 if Token
= Tok_Box
then
531 Error_Msg_SC
-- CODEFIX
532 ("ABSTRACT expected");
533 Token
:= Tok_Abstract
;
536 -- Abstract subprogram declaration case
538 if Token
= Tok_Abstract
then
540 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
541 Set_Specification
(Absdec_Node
, Specification_Node
);
542 Pop_Scope_Stack
; -- discard unneeded entry
543 Scan
; -- past ABSTRACT
544 P_Aspect_Specifications
(Absdec_Node
);
547 -- Ada 2005 (AI-248): Parse a null procedure declaration
549 elsif Token
= Tok_Null
then
550 if Ada_Version
< Ada_2005
then
551 Error_Msg_SP
("null procedures are an Ada 2005 extension");
552 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
558 Error_Msg_SP
("only procedures can be null");
560 Set_Null_Present
(Specification_Node
);
563 goto Subprogram_Declaration
;
565 -- Check for IS NEW with Formal_Part present and handle nicely
567 elsif Token
= Tok_New
then
569 ("formal part not allowed in instantiation", Fpart_Sloc
);
573 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
576 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
579 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
580 Set_Name
(Inst_Node
, P_Name
);
581 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
583 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
587 goto Subprogram_Body
;
590 -- Aspect specifications present
592 elsif Aspect_Specifications_Present
then
593 goto Subprogram_Declaration
;
595 -- Here we have a missing IS or missing semicolon, we always guess
596 -- a missing semicolon, since we are pretty good at fixing up a
597 -- semicolon which should really be an IS
600 Error_Msg_AP
-- CODEFIX
602 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
603 goto Subprogram_Declaration
;
607 -- Processing for stub or subprogram body or expression function
611 -- Subprogram body stub case
613 if Separate_Present
then
614 if not Pf_Flags
.Stub
then
615 Error_Msg_SC
("body stub not allowed here!");
618 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
620 ("operator symbol cannot be used as subunit name",
625 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
626 Set_Specification
(Stub_Node
, Specification_Node
);
627 Scan
; -- past SEPARATE
632 -- Subprogram body or expression function case
635 Scan_Body_Or_Expression_Function
: declare
637 Body_Is_Hidden_In_SPARK
: Boolean;
638 Hidden_Region_Start
: Source_Ptr
;
640 function Likely_Expression_Function
return Boolean;
641 -- Returns True if we have a probable case of an expression
642 -- function omitting the parentheses, if so, returns True
643 -- and emits an appropriate error message, else returns False.
645 --------------------------------
646 -- Likely_Expression_Function --
647 --------------------------------
649 function Likely_Expression_Function
return Boolean is
651 -- If currently pointing to BEGIN or a declaration keyword
652 -- or a pragma, then we definitely have a subprogram body.
653 -- This is a common case, so worth testing first.
656 or else Token
in Token_Class_Declk
657 or else Token
= Tok_Pragma
661 -- Test for tokens which could only start an expression and
662 -- thus signal the case of a expression function.
664 elsif Token
in Token_Class_Literal
665 or else Token
in Token_Class_Unary_Addop
666 or else Token
= Tok_Left_Paren
667 or else Token
= Tok_Abs
668 or else Token
= Tok_Null
669 or else Token
= Tok_New
670 or else Token
= Tok_Not
674 -- Anything other than an identifier must be a body
676 elsif Token
/= Tok_Identifier
then
679 -- Here for an identifier
682 -- If the identifier is the first token on its line, then
683 -- let's assume that we have a missing begin and this is
684 -- intended as a subprogram body. However, if the context
685 -- is a function and the unit is a package declaration, a
686 -- body would be illegal, so try for an unparenthesized
687 -- expression function.
689 if Token_Is_At_Start_Of_Line
then
691 -- The enclosing scope entry is a subprogram spec
693 Spec_Node
: constant Node_Id
:=
695 (Scope
.Table
(Scope
.Last
).Labl
);
696 Lib_Node
: Node_Id
:= Spec_Node
;
699 -- Check whether there is an enclosing scope that
700 -- is a package declaration.
702 if Scope
.Last
> 1 then
704 Parent
(Scope
.Table
(Scope
.Last
- 1).Labl
);
707 if Ada_Version
>= Ada_2012
709 Nkind
(Lib_Node
) = N_Package_Specification
711 Nkind
(Spec_Node
) = N_Function_Specification
719 -- Otherwise we have to scan ahead. If the identifier is
720 -- followed by a colon or a comma, it is a declaration
721 -- and hence we have a subprogram body. Otherwise assume
722 -- a expression function.
726 Scan_State
: Saved_Scan_State
;
730 Save_Scan_State
(Scan_State
);
731 Scan
; -- past identifier
733 Restore_Scan_State
(Scan_State
);
735 if Tok
= Tok_Colon
or else Tok
= Tok_Comma
then
742 -- Fall through if we have a likely expression function
745 ("expression function must be enclosed in parentheses");
747 end Likely_Expression_Function
;
749 -- Start of processing for Scan_Body_Or_Expression_Function
752 -- Expression_Function case
754 if Token
= Tok_Left_Paren
755 or else Likely_Expression_Function
757 -- Check expression function allowed here
759 if not Pf_Flags
.Pexp
then
760 Error_Msg_SC
("expression function not allowed here!");
763 -- Check we are in Ada 2012 mode
765 if Ada_Version
< Ada_2012
then
767 ("expression function is an Ada 2012 feature!");
769 ("\unit must be compiled with -gnat2012 switch!");
772 -- Parse out expression and build expression function
776 (N_Expression_Function
, Sloc
(Specification_Node
));
777 Set_Specification
(Body_Node
, Specification_Node
);
778 Set_Expression
(Body_Node
, P_Expression
);
780 -- Expression functions can carry pre/postconditions
782 P_Aspect_Specifications
(Body_Node
);
785 -- Subprogram body case
788 -- Check body allowed here
790 if not Pf_Flags
.Pbod
then
791 Error_Msg_SP
("subprogram body not allowed here!");
794 -- Here is the test for a suspicious IS (i.e. one that
795 -- looks like it might more properly be a semicolon).
796 -- See separate section describing use of IS instead
797 -- of semicolon in package Parse.
799 if (Token
in Token_Class_Declk
801 Token
= Tok_Identifier
)
802 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
803 and then Scope
.Last
/= 1
805 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
806 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
809 -- Build and return subprogram body, parsing declarations
810 -- and statement sequence that belong to the body.
813 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
814 Set_Specification
(Body_Node
, Specification_Node
);
816 -- If aspects are present, the specification is parsed as
817 -- a subprogram declaration, and we jump here after seeing
818 -- the keyword IS. Attach asspects previously collected to
821 if Is_Non_Empty_List
(Aspects
) then
822 Set_Parent
(Aspects
, Body_Node
);
823 Set_Aspect_Specifications
(Body_Node
, Aspects
);
826 -- In SPARK, a HIDE directive can be placed at the beginning
827 -- of a subprogram implementation, thus hiding the
828 -- subprogram body from SPARK tool-set. No violation of the
829 -- SPARK restriction should be issued on nodes in a hidden
830 -- part, which is obtained by marking such hidden parts.
832 if Token
= Tok_SPARK_Hide
then
833 Body_Is_Hidden_In_SPARK
:= True;
834 Hidden_Region_Start
:= Token_Ptr
;
835 Scan
; -- past HIDE directive
837 Body_Is_Hidden_In_SPARK
:= False;
840 Parse_Decls_Begin_End
(Body_Node
);
842 if Body_Is_Hidden_In_SPARK
then
843 Set_Hidden_Part_In_SPARK
(Hidden_Region_Start
, Token_Ptr
);
848 end Scan_Body_Or_Expression_Function
;
851 -- Processing for subprogram declaration
853 <<Subprogram_Declaration
>>
855 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
856 Set_Specification
(Decl_Node
, Specification_Node
);
857 Aspects
:= Get_Aspect_Specifications
(Semicolon
=> False);
859 -- Aspects may be present on a subprogram body. The source parsed
860 -- so far is that of its specification, go parse the body and attach
861 -- the collected aspects, if any, to the body.
863 if Token
= Tok_Is
then
865 goto Subprogram_Body
;
868 if Is_Non_Empty_List
(Aspects
) then
869 Set_Parent
(Aspects
, Decl_Node
);
870 Set_Aspect_Specifications
(Decl_Node
, Aspects
);
876 -- If this is a context in which a subprogram body is permitted,
877 -- set active SIS entry in case (see section titled "Handling
878 -- Semicolon Used in Place of IS" in body of Parser package)
879 -- Note that SIS_Missing_Semicolon_Message is already set properly.
881 if Pf_Flags
.Pbod
then
882 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
883 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
884 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
885 SIS_Declaration_Node
:= Decl_Node
;
886 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
887 SIS_Entry_Active
:= True;
894 ---------------------------------
895 -- 6.1 Subprogram Declaration --
896 ---------------------------------
898 -- Parsed by P_Subprogram (6.1)
900 ------------------------------------------
901 -- 6.1 Abstract Subprogram Declaration --
902 ------------------------------------------
904 -- Parsed by P_Subprogram (6.1)
906 -----------------------------------
907 -- 6.1 Subprogram Specification --
908 -----------------------------------
910 -- SUBPROGRAM_SPECIFICATION ::=
911 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
912 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
914 -- PARAMETER_PROFILE ::= [FORMAL_PART]
916 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
918 -- Subprogram specifications that appear in subprogram declarations
919 -- are parsed by P_Subprogram (6.1). This routine is used in other
920 -- contexts where subprogram specifications occur.
922 -- Note: this routine does not affect the scope stack in any way
924 -- Error recovery: can raise Error_Resync
926 function P_Subprogram_Specification
return Node_Id
is
927 Specification_Node
: Node_Id
;
928 Result_Not_Null
: Boolean;
929 Result_Node
: Node_Id
;
932 if Token
= Tok_Function
then
933 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
934 Scan
; -- past FUNCTION
936 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
937 Set_Parameter_Specifications
938 (Specification_Node
, P_Parameter_Profile
);
939 Check_Junk_Semicolon_Before_Return
;
942 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
944 -- Ada 2005 (AI-318-02)
946 if Token
= Tok_Access
then
947 if Ada_Version
< Ada_2005
then
949 ("anonymous access result type is an Ada 2005 extension");
950 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
953 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
956 Result_Node
:= P_Subtype_Mark
;
960 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
961 Set_Result_Definition
(Specification_Node
, Result_Node
);
962 return Specification_Node
;
964 elsif Token
= Tok_Procedure
then
965 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
966 Scan
; -- past PROCEDURE
968 Set_Defining_Unit_Name
969 (Specification_Node
, P_Defining_Program_Unit_Name
);
970 Set_Parameter_Specifications
971 (Specification_Node
, P_Parameter_Profile
);
972 return Specification_Node
;
975 Error_Msg_SC
("subprogram specification expected");
978 end P_Subprogram_Specification
;
980 ---------------------
982 ---------------------
985 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
987 -- The caller has checked that the initial token is an identifier,
988 -- operator symbol, or string literal. Note that we don't bother to
989 -- do much error diagnosis in this routine, since it is only used for
990 -- the label on END lines, and the routines in package Par.Endh will
991 -- check that the label is appropriate.
993 -- Error recovery: cannot raise Error_Resync
995 function P_Designator
return Node_Id
is
996 Ident_Node
: Node_Id
;
998 Prefix_Node
: Node_Id
;
1000 function Real_Dot
return Boolean;
1001 -- Tests if a current token is an interesting period, i.e. is followed
1002 -- by an identifier or operator symbol or string literal. If not, it is
1003 -- probably just incorrect punctuation to be caught by our caller. Note
1004 -- that the case of an operator symbol or string literal is also an
1005 -- error, but that is an error that we catch here. If the result is
1006 -- True, a real dot has been scanned and we are positioned past it,
1007 -- if the result is False, the scan position is unchanged.
1013 function Real_Dot
return Boolean is
1014 Scan_State
: Saved_Scan_State
;
1017 if Token
/= Tok_Dot
then
1021 Save_Scan_State
(Scan_State
);
1024 if Token
= Tok_Identifier
1025 or else Token
= Tok_Operator_Symbol
1026 or else Token
= Tok_String_Literal
1031 Restore_Scan_State
(Scan_State
);
1037 -- Start of processing for P_Designator
1040 Ident_Node
:= Token_Node
;
1041 Scan
; -- past initial token
1043 if Prev_Token
= Tok_Operator_Symbol
1044 or else Prev_Token
= Tok_String_Literal
1045 or else not Real_Dot
1052 Prefix_Node
:= Ident_Node
;
1054 -- Loop through child names, on entry to this loop, Prefix contains
1055 -- the name scanned so far, and Ident_Node is the last identifier.
1058 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
1059 Set_Prefix
(Name_Node
, Prefix_Node
);
1060 Ident_Node
:= P_Identifier
;
1061 Set_Selector_Name
(Name_Node
, Ident_Node
);
1062 Prefix_Node
:= Name_Node
;
1063 exit when not Real_Dot
;
1066 -- On exit from the loop, Ident_Node is the last identifier scanned,
1067 -- i.e. the defining identifier, and Prefix_Node is a node for the
1068 -- entire name, structured (incorrectly!) as a selected component.
1070 Name_Node
:= Prefix
(Prefix_Node
);
1071 Change_Node
(Prefix_Node
, N_Designator
);
1072 Set_Name
(Prefix_Node
, Name_Node
);
1073 Set_Identifier
(Prefix_Node
, Ident_Node
);
1078 when Error_Resync
=>
1079 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1086 ------------------------------
1087 -- 6.1 Defining Designator --
1088 ------------------------------
1090 -- DEFINING_DESIGNATOR ::=
1091 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1093 -- Error recovery: cannot raise Error_Resync
1095 function P_Defining_Designator
return Node_Id
is
1097 if Token
= Tok_Operator_Symbol
then
1098 return P_Defining_Operator_Symbol
;
1100 elsif Token
= Tok_String_Literal
then
1101 Error_Msg_SC
("invalid operator name");
1102 Scan
; -- past junk string
1106 return P_Defining_Program_Unit_Name
;
1108 end P_Defining_Designator
;
1110 -------------------------------------
1111 -- 6.1 Defining Program Unit Name --
1112 -------------------------------------
1114 -- DEFINING_PROGRAM_UNIT_NAME ::=
1115 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1117 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1119 -- Error recovery: cannot raise Error_Resync
1121 function P_Defining_Program_Unit_Name
return Node_Id
is
1122 Ident_Node
: Node_Id
;
1123 Name_Node
: Node_Id
;
1124 Prefix_Node
: Node_Id
;
1127 -- Set identifier casing if not already set and scan initial identifier
1129 if Token
= Tok_Identifier
1130 and then Identifier_Casing
(Current_Source_File
) = Unknown
1132 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
1135 Ident_Node
:= P_Identifier
(C_Dot
);
1136 Merge_Identifier
(Ident_Node
, Tok_Return
);
1138 -- Normal case (not child library unit name)
1140 if Token
/= Tok_Dot
then
1141 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1144 -- Child library unit name case
1147 if Scope
.Last
> 1 then
1148 Error_Msg_SP
("child unit allowed only at library level");
1151 elsif Ada_Version
= Ada_83
then
1152 Error_Msg_SP
("(Ada 83) child unit not allowed!");
1156 Prefix_Node
:= Ident_Node
;
1158 -- Loop through child names, on entry to this loop, Prefix contains
1159 -- the name scanned so far, and Ident_Node is the last identifier.
1162 exit when Token
/= Tok_Dot
;
1163 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
1164 Scan
; -- past period
1165 Set_Prefix
(Name_Node
, Prefix_Node
);
1166 Ident_Node
:= P_Identifier
(C_Dot
);
1167 Set_Selector_Name
(Name_Node
, Ident_Node
);
1168 Prefix_Node
:= Name_Node
;
1171 -- On exit from the loop, Ident_Node is the last identifier scanned,
1172 -- i.e. the defining identifier, and Prefix_Node is a node for the
1173 -- entire name, structured (incorrectly!) as a selected component.
1175 Name_Node
:= Prefix
(Prefix_Node
);
1176 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
1177 Set_Name
(Prefix_Node
, Name_Node
);
1178 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1179 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
1181 -- All set with unit name parsed
1187 when Error_Resync
=>
1188 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1193 end P_Defining_Program_Unit_Name
;
1195 --------------------------
1196 -- 6.1 Operator Symbol --
1197 --------------------------
1199 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1201 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1203 -----------------------------------
1204 -- 6.1 Defining Operator Symbol --
1205 -----------------------------------
1207 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1209 -- The caller has checked that the initial symbol is an operator symbol
1211 function P_Defining_Operator_Symbol
return Node_Id
is
1215 Op_Node
:= Token_Node
;
1216 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
1217 Scan
; -- past operator symbol
1219 end P_Defining_Operator_Symbol
;
1221 ----------------------------
1222 -- 6.1 Parameter_Profile --
1223 ----------------------------
1225 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1227 -- Empty is returned if no formal part is present
1229 -- Error recovery: cannot raise Error_Resync
1231 function P_Parameter_Profile
return List_Id
is
1233 if Token
= Tok_Left_Paren
then
1234 Scan
; -- part left paren
1235 return P_Formal_Part
;
1239 end P_Parameter_Profile
;
1241 ---------------------------------------
1242 -- 6.1 Parameter And Result Profile --
1243 ---------------------------------------
1245 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1246 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1248 ----------------------
1249 -- 6.1 Formal part --
1250 ----------------------
1252 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1254 -- PARAMETER_SPECIFICATION ::=
1255 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1256 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1257 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1258 -- [:= DEFAULT_EXPRESSION]
1260 -- This scans the construct Formal_Part. The caller has already checked
1261 -- that the initial token is a left parenthesis, and skipped past it, so
1262 -- that on entry Token is the first token following the left parenthesis.
1264 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1266 -- Error recovery: cannot raise Error_Resync
1268 function P_Formal_Part
return List_Id
is
1269 Specification_List
: List_Id
;
1270 Specification_Node
: Node_Id
;
1271 Scan_State
: Saved_Scan_State
;
1274 Ident_Sloc
: Source_Ptr
;
1275 Not_Null_Present
: Boolean := False;
1276 Not_Null_Sloc
: Source_Ptr
;
1278 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1279 -- This array holds the list of defining identifiers. The upper bound
1280 -- of 4096 is intended to be essentially infinite, and we do not even
1281 -- bother to check for it being exceeded.
1284 Specification_List
:= New_List
;
1285 Specification_Loop
: loop
1287 if Token
= Tok_Pragma
then
1288 Error_Msg_SC
("pragma not allowed in formal part");
1289 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
1292 Ignore
(Tok_Left_Paren
);
1293 Ident_Sloc
:= Token_Ptr
;
1294 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1298 exit Ident_Loop
when Token
= Tok_Colon
;
1300 -- The only valid tokens are colon and comma, so if we have
1301 -- neither do a bit of investigation to see which is the
1302 -- better choice for insertion.
1304 if Token
/= Tok_Comma
then
1306 -- Assume colon if ALIASED, IN or OUT keyword found
1308 exit Ident_Loop
when Token
= Tok_Aliased
or else
1309 Token
= Tok_In
or else
1312 -- Otherwise scan ahead
1314 Save_Scan_State
(Scan_State
);
1317 -- If we run into a semicolon, then assume that a
1318 -- colon was missing, e.g. Parms (X Y; ...). Also
1319 -- assume missing colon on EOF (a real disaster!)
1320 -- and on a right paren, e.g. Parms (X Y), and also
1321 -- on an assignment symbol, e.g. Parms (X Y := ..)
1323 if Token
= Tok_Semicolon
1324 or else Token
= Tok_Right_Paren
1325 or else Token
= Tok_EOF
1326 or else Token
= Tok_Colon_Equal
1328 Restore_Scan_State
(Scan_State
);
1331 -- If we run into a colon, assume that we had a missing
1332 -- comma, e.g. Parms (A B : ...). Also assume a missing
1333 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1335 elsif Token
= Tok_Colon
1336 or else Token
= Tok_Comma
1338 Restore_Scan_State
(Scan_State
);
1343 end loop Look_Ahead
;
1346 -- Here if a comma is present, or to be assumed
1349 Num_Idents
:= Num_Idents
+ 1;
1350 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1351 end loop Ident_Loop
;
1353 -- Fall through the loop on encountering a colon, or deciding
1354 -- that there is a missing colon.
1358 -- If there are multiple identifiers, we repeatedly scan the
1359 -- type and initialization expression information by resetting
1360 -- the scan pointer (so that we get completely separate trees
1361 -- for each occurrence).
1363 if Num_Idents
> 1 then
1364 Save_Scan_State
(Scan_State
);
1367 -- Loop through defining identifiers in list
1371 Ident_List_Loop
: loop
1372 Specification_Node
:=
1373 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1374 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1376 -- Scan possible ALIASED for Ada 2012 (AI-142)
1378 if Token
= Tok_Aliased
then
1379 if Ada_Version
< Ada_2012
then
1380 Error_Msg_SC
("ALIASED parameter is an Ada 2012 feature");
1382 Set_Aliased_Present
(Specification_Node
);
1385 Scan
; -- past ALIASED
1388 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1390 Not_Null_Sloc
:= Token_Ptr
;
1392 P_Null_Exclusion
(Allow_Anonymous_In_95
=> True);
1394 -- Case of ACCESS keyword present
1396 if Token
= Tok_Access
then
1397 Set_Null_Exclusion_Present
1398 (Specification_Node
, Not_Null_Present
);
1400 if Ada_Version
= Ada_83
then
1401 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1405 (Specification_Node
,
1406 P_Access_Definition
(Not_Null_Present
));
1408 -- Case of IN or OUT present
1411 if Token
= Tok_In
or else Token
= Tok_Out
then
1412 if Not_Null_Present
then
1414 ("`NOT NULL` can only be used with `ACCESS`",
1417 if Token
= Tok_In
then
1419 ("\`IN` not allowed together with `ACCESS`",
1423 ("\`OUT` not allowed together with `ACCESS`",
1428 P_Mode
(Specification_Node
);
1429 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1432 Set_Null_Exclusion_Present
1433 (Specification_Node
, Not_Null_Present
);
1435 if Token
= Tok_Procedure
1437 Token
= Tok_Function
1439 Error_Msg_SC
("formal subprogram parameter not allowed");
1442 if Token
= Tok_Left_Paren
then
1443 Discard_Junk_List
(P_Formal_Part
);
1446 if Token
= Tok_Return
then
1448 Discard_Junk_Node
(P_Subtype_Mark
);
1451 Set_Parameter_Type
(Specification_Node
, Error
);
1454 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1459 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1462 Set_Prev_Ids
(Specification_Node
, True);
1465 if Ident
< Num_Idents
then
1466 Set_More_Ids
(Specification_Node
, True);
1469 Append
(Specification_Node
, Specification_List
);
1470 exit Ident_List_Loop
when Ident
= Num_Idents
;
1472 Restore_Scan_State
(Scan_State
);
1473 end loop Ident_List_Loop
;
1476 when Error_Resync
=>
1477 Resync_Semicolon_List
;
1480 if Token
= Tok_Semicolon
then
1481 Save_Scan_State
(Scan_State
);
1482 Scan
; -- past semicolon
1484 -- If we have RETURN or IS after the semicolon, then assume
1485 -- that semicolon should have been a right parenthesis and exit
1487 if Token
= Tok_Is
or else Token
= Tok_Return
then
1488 Error_Msg_SP
-- CODEFIX
1489 ("|"";"" should be "")""");
1490 exit Specification_Loop
;
1493 -- If we have a declaration keyword after the semicolon, then
1494 -- assume we had a missing right parenthesis and terminate list
1496 if Token
in Token_Class_Declk
then
1497 Error_Msg_AP
-- CODEFIX
1499 Restore_Scan_State
(Scan_State
);
1500 exit Specification_Loop
;
1503 elsif Token
= Tok_Right_Paren
then
1504 Scan
; -- past right paren
1505 exit Specification_Loop
;
1507 -- Special check for common error of using comma instead of semicolon
1509 elsif Token
= Tok_Comma
then
1513 -- Special check for omitted separator
1515 elsif Token
= Tok_Identifier
then
1518 -- If nothing sensible, skip to next semicolon or right paren
1522 Resync_Semicolon_List
;
1524 if Token
= Tok_Semicolon
then
1525 Scan
; -- past semicolon
1528 exit Specification_Loop
;
1531 end loop Specification_Loop
;
1533 return Specification_List
;
1536 ----------------------------------
1537 -- 6.1 Parameter Specification --
1538 ----------------------------------
1540 -- Parsed by P_Formal_Part (6.1)
1546 -- MODE ::= [in] | in out | out
1548 -- There is no explicit node in the tree for the Mode. Instead the
1549 -- In_Present and Out_Present flags are set in the parent node to
1550 -- record the presence of keywords specifying the mode.
1552 -- Error_Recovery: cannot raise Error_Resync
1554 procedure P_Mode
(Node
: Node_Id
) is
1556 if Token
= Tok_In
then
1558 Set_In_Present
(Node
, True);
1560 if Style
.Mode_In_Check
and then Token
/= Tok_Out
then
1561 Error_Msg_SP
-- CODEFIX
1562 ("(style) IN should be omitted");
1565 -- Since Ada 2005, formal objects can have an anonymous access type,
1566 -- and of course carry a mode indicator.
1568 if Token
= Tok_Access
1569 and then Nkind
(Node
) /= N_Formal_Object_Declaration
1571 Error_Msg_SP
("IN not allowed together with ACCESS");
1572 Scan
; -- past ACCESS
1576 if Token
= Tok_Out
then
1578 Set_Out_Present
(Node
, True);
1581 if Token
= Tok_In
then
1582 Error_Msg_SC
("IN must precede OUT in parameter mode");
1584 Set_In_Present
(Node
, True);
1588 --------------------------
1589 -- 6.3 Subprogram Body --
1590 --------------------------
1592 -- Parsed by P_Subprogram (6.1)
1594 -----------------------------------
1595 -- 6.4 Procedure Call Statement --
1596 -----------------------------------
1598 -- Parsed by P_Sequence_Of_Statements (5.1)
1600 ------------------------
1601 -- 6.4 Function Call --
1602 ------------------------
1604 -- Parsed by P_Name (4.1)
1606 --------------------------------
1607 -- 6.4 Actual Parameter Part --
1608 --------------------------------
1610 -- Parsed by P_Name (4.1)
1612 --------------------------------
1613 -- 6.4 Parameter Association --
1614 --------------------------------
1616 -- Parsed by P_Name (4.1)
1618 ------------------------------------
1619 -- 6.4 Explicit Actual Parameter --
1620 ------------------------------------
1622 -- Parsed by P_Name (4.1)
1624 ---------------------------
1625 -- 6.5 Return Statement --
1626 ---------------------------
1628 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1630 -- EXTENDED_RETURN_STATEMENT ::=
1631 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1632 -- [:= EXPRESSION] [do
1633 -- HANDLED_SEQUENCE_OF_STATEMENTS
1636 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1638 -- RETURN_STATEMENT ::= return [EXPRESSION];
1640 -- Error recovery: can raise Error_Resync
1642 procedure P_Return_Subtype_Indication
(Decl_Node
: Node_Id
) is
1644 -- Note: We don't need to check Ada_Version here, because this is
1645 -- only called in >= Ada 2005 cases anyway.
1647 Not_Null_Present
: constant Boolean := P_Null_Exclusion
;
1650 Set_Null_Exclusion_Present
(Decl_Node
, Not_Null_Present
);
1652 if Token
= Tok_Access
then
1653 Set_Object_Definition
1654 (Decl_Node
, P_Access_Definition
(Not_Null_Present
));
1656 Set_Object_Definition
1657 (Decl_Node
, P_Subtype_Indication
(Not_Null_Present
));
1659 end P_Return_Subtype_Indication
;
1661 -- Error recovery: can raise Error_Resync
1663 function P_Return_Object_Declaration
return Node_Id
is
1664 Return_Obj
: Node_Id
;
1665 Decl_Node
: Node_Id
;
1668 Return_Obj
:= Token_Node
;
1669 Change_Identifier_To_Defining_Identifier
(Return_Obj
);
1670 Decl_Node
:= New_Node
(N_Object_Declaration
, Token_Ptr
);
1671 Set_Defining_Identifier
(Decl_Node
, Return_Obj
);
1673 Scan
; -- past identifier
1676 -- First an error check, if we have two identifiers in a row, a likely
1677 -- possibility is that the first of the identifiers is an incorrectly
1678 -- spelled keyword. See similar check in P_Identifier_Declarations.
1680 if Token
= Tok_Identifier
then
1682 SS
: Saved_Scan_State
;
1686 Save_Scan_State
(SS
);
1687 Scan
; -- past initial identifier
1688 I2
:= (Token
= Tok_Identifier
);
1689 Restore_Scan_State
(SS
);
1693 (Bad_Spelling_Of
(Tok_Access
) or else
1694 Bad_Spelling_Of
(Tok_Aliased
) or else
1695 Bad_Spelling_Of
(Tok_Constant
))
1702 -- We allow "constant" here (as in "return Result : constant
1703 -- T..."). This is not in the latest RM, but the ARG is considering an
1704 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1706 if Token
= Tok_Constant
then
1707 Scan
; -- past CONSTANT
1708 Set_Constant_Present
(Decl_Node
);
1710 if Token
= Tok_Aliased
then
1711 Error_Msg_SC
-- CODEFIX
1712 ("ALIASED should be before CONSTANT");
1713 Scan
; -- past ALIASED
1714 Set_Aliased_Present
(Decl_Node
);
1717 elsif Token
= Tok_Aliased
then
1718 Scan
; -- past ALIASED
1719 Set_Aliased_Present
(Decl_Node
);
1721 if Ada_Version
< Ada_2012
then
1722 Error_Msg_SC
-- CODEFIX
1723 ("ALIASED not allowed in extended return in Ada 2012?");
1725 Error_Msg_SC
-- CODEFIX
1726 ("ALIASED not allowed in extended return");
1729 if Token
= Tok_Constant
then
1730 Scan
; -- past CONSTANT
1731 Set_Constant_Present
(Decl_Node
);
1735 P_Return_Subtype_Indication
(Decl_Node
);
1737 if Token
= Tok_Colon_Equal
then
1739 Set_Expression
(Decl_Node
, P_Expression_No_Right_Paren
);
1743 end P_Return_Object_Declaration
;
1745 -- Error recovery: can raise Error_Resync
1747 function P_Return_Statement
return Node_Id
is
1748 -- The caller has checked that the initial token is RETURN
1750 function Is_Simple
return Boolean;
1751 -- Scan state is just after RETURN (and is left that way).
1752 -- Determine whether this is a simple or extended return statement
1753 -- by looking ahead for "identifier :", which implies extended.
1759 function Is_Simple
return Boolean is
1760 Scan_State
: Saved_Scan_State
;
1761 Result
: Boolean := True;
1764 if Token
= Tok_Identifier
then
1765 Save_Scan_State
(Scan_State
); -- at identifier
1766 Scan
; -- past identifier
1768 if Token
= Tok_Colon
then
1769 Result
:= False; -- It's an extended_return_statement.
1772 Restore_Scan_State
(Scan_State
); -- to identifier
1778 Return_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1779 Return_Node
: Node_Id
;
1781 -- Start of processing for P_Return_Statement
1784 Scan
; -- past RETURN
1786 -- Simple_return_statement, no expression, return an
1787 -- N_Simple_Return_Statement node with the expression field left Empty.
1789 if Token
= Tok_Semicolon
then
1791 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1796 -- Simple_return_statement with expression
1798 -- We avoid trying to scan an expression if we are at an
1799 -- expression terminator since in that case the best error
1800 -- message is probably that we have a missing semicolon.
1803 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1805 if Token
not in Token_Class_Eterm
then
1806 Set_Expression
(Return_Node
, P_Expression_No_Right_Paren
);
1809 -- Extended_return_statement (Ada 2005 only -- AI-318):
1812 if Ada_Version
< Ada_2005
then
1814 (" extended_return_statement is an Ada 2005 extension");
1815 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
1818 Return_Node
:= New_Node
(N_Extended_Return_Statement
, Return_Sloc
);
1819 Set_Return_Object_Declarations
1820 (Return_Node
, New_List
(P_Return_Object_Declaration
));
1822 if Token
= Tok_Do
then
1824 Scope
.Table
(Scope
.Last
).Etyp
:= E_Return
;
1825 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
1826 Scope
.Table
(Scope
.Last
).Sloc
:= Return_Sloc
;
1829 Set_Handled_Statement_Sequence
1830 (Return_Node
, P_Handled_Sequence_Of_Statements
);
1833 -- Do we need to handle Error_Resync here???
1841 end P_Return_Statement
;