1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 elsif Bad_Spelling_Of
(Tok_Return
) then
80 end Check_Junk_Semicolon_Before_Return
;
82 -----------------------------------
83 -- No_Constraint_Maybe_Expr_Func --
84 -----------------------------------
86 procedure No_Constraint_Maybe_Expr_Func
is
88 -- If we have a left paren at the start of the line, then assume this is
89 -- the case of an expression function with missing IS. We do not have to
90 -- diagnose the missing IS, that is done elsewhere. We do this game in
91 -- Ada 2012 mode where expression functions are legal.
93 if Token
= Tok_Left_Paren
94 and Ada_Version
>= Ada_2012
95 and Token_Is_At_Start_Of_Line
97 -- One exception if we have "(token .." then this is a constraint
100 Scan_State
: Saved_Scan_State
;
103 Save_Scan_State
(Scan_State
);
104 Scan
; -- past left paren
105 Scan
; -- past following token
107 -- If we have "(token .." then restore scan state and treat as
108 -- unexpected constraint.
110 if Token
= Tok_Dot_Dot
then
111 Restore_Scan_State
(Scan_State
);
114 -- Otherwise we treat this as an expression function
117 Restore_Scan_State
(Scan_State
);
121 -- Otherwise use standard routine to check for no constraint present
126 end No_Constraint_Maybe_Expr_Func
;
128 -----------------------------------------------------
129 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
130 -----------------------------------------------------
132 -- This routine scans out a subprogram declaration, subprogram body,
133 -- subprogram renaming declaration or subprogram generic instantiation.
134 -- It also handles the new Ada 2012 expression function form
136 -- SUBPROGRAM_DECLARATION ::=
137 -- SUBPROGRAM_SPECIFICATION
138 -- [ASPECT_SPECIFICATIONS];
140 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
141 -- SUBPROGRAM_SPECIFICATION is abstract
142 -- [ASPECT_SPECIFICATIONS];
144 -- SUBPROGRAM_SPECIFICATION ::=
145 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
146 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
148 -- PARAMETER_PROFILE ::= [FORMAL_PART]
150 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
152 -- SUBPROGRAM_BODY ::=
153 -- SUBPROGRAM_SPECIFICATION [ASPECT_SPECIFICATIONS] is
156 -- HANDLED_SEQUENCE_OF_STATEMENTS
159 -- SUBPROGRAM_RENAMING_DECLARATION ::=
160 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME
161 -- [ASPECT_SPECIFICATIONS];
163 -- SUBPROGRAM_BODY_STUB ::=
164 -- SUBPROGRAM_SPECIFICATION is separate;
166 -- GENERIC_INSTANTIATION ::=
167 -- procedure DEFINING_PROGRAM_UNIT_NAME is
168 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
169 -- | function DEFINING_DESIGNATOR is
170 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
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
;
341 -- Deal with generic instantiation, the one case in which we do not
342 -- have a subprogram specification as part of whatever we are parsing
344 if Token
= Tok_Is
then
345 Save_Scan_State
(Scan_State
); -- at the IS
346 T_Is
; -- checks for redundant IS
348 if Token
= Tok_New
then
349 if not Pf_Flags
.Gins
then
350 Error_Msg_SC
("generic instantiation not allowed here!");
356 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
357 Set_Name
(Inst_Node
, P_Function_Name
);
359 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
360 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
363 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
364 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
365 P_Aspect_Specifications
(Inst_Node
);
366 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
368 if Is_Overriding
then
369 Set_Must_Override
(Inst_Node
);
371 elsif Not_Overriding
then
372 Set_Must_Not_Override
(Inst_Node
);
378 Restore_Scan_State
(Scan_State
); -- to the IS
382 -- If not a generic instantiation, then we definitely have a subprogram
383 -- specification (all possibilities at this stage include one here)
385 Fpart_Sloc
:= Token_Ptr
;
387 Check_Misspelling_Of
(Tok_Return
);
389 -- Scan formal part. First a special error check. If we have an
390 -- identifier here, then we have a definite error. If this identifier
391 -- is on the same line as the designator, then we assume it is the
392 -- first formal after a missing left parenthesis
394 if Token
= Tok_Identifier
395 and then not Token_Is_At_Start_Of_Line
397 T_Left_Paren
; -- to generate message
398 Fpart_List
:= P_Formal_Part
;
400 -- Otherwise scan out an optional formal part in the usual manner
403 Fpart_List
:= P_Parameter_Profile
;
406 -- We treat what we have as a function specification if FUNCTION was
407 -- used, or if a RETURN is present. This gives better error recovery
408 -- since later RETURN statements will be valid in either case.
410 Check_Junk_Semicolon_Before_Return
;
411 Result_Node
:= Error
;
413 if Token
= Tok_Return
then
416 ("PROCEDURE should be FUNCTION", Fproc_Sloc
);
422 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
424 -- Ada 2005 (AI-318-02)
426 if Token
= Tok_Access
then
427 if Ada_Version
< Ada_2005
then
429 ("anonymous access result type is an Ada 2005 extension");
430 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
433 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
436 Result_Node
:= P_Subtype_Mark
;
437 No_Constraint_Maybe_Expr_Func
;
441 -- Skip extra parenthesis at end of formal part
443 Ignore
(Tok_Right_Paren
);
445 -- For function, scan result subtype
450 if Prev_Token
= Tok_Return
then
451 Result_Node
:= P_Subtype_Mark
;
457 Specification_Node
:=
458 New_Node
(N_Function_Specification
, Fproc_Sloc
);
460 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
461 Set_Result_Definition
(Specification_Node
, Result_Node
);
464 Specification_Node
:=
465 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
468 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
469 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
471 if Is_Overriding
then
472 Set_Must_Override
(Specification_Node
);
474 elsif Not_Overriding
then
475 Set_Must_Not_Override
(Specification_Node
);
478 -- Error check: barriers not allowed on protected functions/procedures
480 if Token
= Tok_When
then
482 Error_Msg_SC
("barrier not allowed on function, only on entry");
484 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
488 Discard_Junk_Node
(P_Expression
);
491 -- Deal with semicolon followed by IS. We want to treat this as IS
493 if Token
= Tok_Semicolon
then
494 Save_Scan_State
(Scan_State
);
495 Scan
; -- past semicolon
497 if Token
= Tok_Is
then
498 Error_Msg_SP
-- CODEFIX
499 ("extra "";"" ignored");
501 Restore_Scan_State
(Scan_State
);
505 -- Subprogram declaration ended by aspect specifications
507 if Aspect_Specifications_Present
then
508 goto Subprogram_Declaration
;
510 -- Deal with case of semicolon ending a subprogram declaration
512 elsif Token
= Tok_Semicolon
then
513 if not Pf_Flags
.Decl
then
517 Save_Scan_State
(Scan_State
);
518 Scan
; -- past semicolon
520 -- If semicolon is immediately followed by IS, then ignore the
521 -- semicolon, and go process the body.
523 if Token
= Tok_Is
then
524 Error_Msg_SP
-- CODEFIX
525 ("|extra "";"" ignored");
526 T_Is
; -- scan past IS
527 goto Subprogram_Body
;
529 -- If BEGIN follows in an appropriate column, we immediately
530 -- commence the error action of assuming that the previous
531 -- subprogram declaration should have been a subprogram body,
532 -- i.e. that the terminating semicolon should have been IS.
534 elsif Token
= Tok_Begin
535 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
537 Error_Msg_SP
-- CODEFIX
538 ("|"";"" should be IS!");
539 goto Subprogram_Body
;
542 Restore_Scan_State
(Scan_State
);
543 goto Subprogram_Declaration
;
546 -- Case of not followed by semicolon
549 -- Subprogram renaming declaration case
551 Check_Misspelling_Of
(Tok_Renames
);
553 if Token
= Tok_Renames
then
554 if not Pf_Flags
.Rnam
then
555 Error_Msg_SC
("renaming declaration not allowed here!");
559 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
560 Scan
; -- past RENAMES
561 Set_Name
(Rename_Node
, P_Name
);
562 Set_Specification
(Rename_Node
, Specification_Node
);
563 P_Aspect_Specifications
(Rename_Node
);
568 -- Case of IS following subprogram specification
570 elsif Token
= Tok_Is
then
571 T_Is
; -- ignore redundant Is's
573 if Token_Name
= Name_Abstract
then
574 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
577 -- Deal nicely with (now obsolete) use of <> in place of abstract
579 if Token
= Tok_Box
then
580 Error_Msg_SC
-- CODEFIX
581 ("ABSTRACT expected");
582 Token
:= Tok_Abstract
;
585 -- Abstract subprogram declaration case
587 if Token
= Tok_Abstract
then
589 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
590 Set_Specification
(Absdec_Node
, Specification_Node
);
591 Pop_Scope_Stack
; -- discard unneeded entry
592 Scan
; -- past ABSTRACT
593 P_Aspect_Specifications
(Absdec_Node
);
596 -- Ada 2005 (AI-248): Parse a null procedure declaration
598 elsif Token
= Tok_Null
then
599 if Ada_Version
< Ada_2005
then
600 Error_Msg_SP
("null procedures are an Ada 2005 extension");
601 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
607 Error_Msg_SP
("only procedures can be null");
609 Set_Null_Present
(Specification_Node
);
612 goto Subprogram_Declaration
;
614 -- Check for IS NEW with Formal_Part present and handle nicely
616 elsif Token
= Tok_New
then
618 ("formal part not allowed in instantiation", Fpart_Sloc
);
622 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
625 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
628 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
629 Set_Name
(Inst_Node
, P_Name
);
630 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
632 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
636 goto Subprogram_Body
;
639 -- Aspect specifications present
641 elsif Aspect_Specifications_Present
then
642 goto Subprogram_Declaration
;
644 -- Here we have a missing IS or missing semicolon
647 -- If the next token is a left paren at the start of a line, then
648 -- this is almost certainly the start of the expression for an
649 -- expression function, so in this case guess a missing IS.
651 if Token
= Tok_Left_Paren
and then Token_Is_At_Start_Of_Line
then
652 Error_Msg_AP
-- CODEFIX
655 -- In all other cases, we guess a missing semicolon, since we are
656 -- good at fixing up a semicolon which should really be an IS.
659 Error_Msg_AP
-- CODEFIX
661 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
662 goto Subprogram_Declaration
;
667 -- Processing for stub or subprogram body or expression function
671 -- Subprogram body stub case
673 if Separate_Present
then
674 if not Pf_Flags
.Stub
then
675 Error_Msg_SC
("body stub not allowed here!");
678 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
680 ("operator symbol cannot be used as subunit name",
685 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
686 Set_Specification
(Stub_Node
, Specification_Node
);
688 -- The specification has been parsed as part of a subprogram
689 -- declaration, and aspects have already been collected.
691 if Is_Non_Empty_List
(Aspects
) then
692 Set_Parent
(Aspects
, Stub_Node
);
693 Set_Aspect_Specifications
(Stub_Node
, Aspects
);
696 Scan
; -- past SEPARATE
701 -- Subprogram body or expression function case
704 Scan_Body_Or_Expression_Function
: declare
706 Body_Is_Hidden_In_SPARK
: Boolean;
707 Hidden_Region_Start
: Source_Ptr
;
709 function Likely_Expression_Function
return Boolean;
710 -- Returns True if we have a probable case of an expression
711 -- function omitting the parentheses, if so, returns True
712 -- and emits an appropriate error message, else returns False.
714 --------------------------------
715 -- Likely_Expression_Function --
716 --------------------------------
718 function Likely_Expression_Function
return Boolean is
720 -- If currently pointing to BEGIN or a declaration keyword
721 -- or a pragma, then we definitely have a subprogram body.
722 -- This is a common case, so worth testing first.
725 or else Token
in Token_Class_Declk
726 or else Token
= Tok_Pragma
730 -- Test for tokens which could only start an expression and
731 -- thus signal the case of a expression function.
733 elsif Token
in Token_Class_Literal
734 or else Token
in Token_Class_Unary_Addop
735 or else Token
= Tok_Left_Paren
736 or else Token
= Tok_Abs
737 or else Token
= Tok_Null
738 or else Token
= Tok_New
739 or else Token
= Tok_Not
743 -- Anything other than an identifier must be a body
745 elsif Token
/= Tok_Identifier
then
748 -- Here for an identifier
751 -- If the identifier is the first token on its line, then
752 -- let's assume that we have a missing begin and this is
753 -- intended as a subprogram body. However, if the context
754 -- is a function and the unit is a package declaration, a
755 -- body would be illegal, so try for an unparenthesized
756 -- expression function.
758 if Token_Is_At_Start_Of_Line
then
760 -- The enclosing scope entry is a subprogram spec
762 Spec_Node
: constant Node_Id
:=
764 (Scope
.Table
(Scope
.Last
).Labl
);
765 Lib_Node
: Node_Id
:= Spec_Node
;
768 -- Check whether there is an enclosing scope that
769 -- is a package declaration.
771 if Scope
.Last
> 1 then
773 Parent
(Scope
.Table
(Scope
.Last
- 1).Labl
);
776 if Ada_Version
>= Ada_2012
778 Nkind
(Lib_Node
) = N_Package_Specification
780 Nkind
(Spec_Node
) = N_Function_Specification
788 -- Otherwise we have to scan ahead. If the identifier is
789 -- followed by a colon or a comma, it is a declaration
790 -- and hence we have a subprogram body. Otherwise assume
791 -- a expression function.
795 Scan_State
: Saved_Scan_State
;
799 Save_Scan_State
(Scan_State
);
800 Scan
; -- past identifier
802 Restore_Scan_State
(Scan_State
);
804 if Tok
= Tok_Colon
or else Tok
= Tok_Comma
then
811 -- Fall through if we have a likely expression function
814 ("expression function must be enclosed in parentheses");
816 end Likely_Expression_Function
;
818 -- Start of processing for Scan_Body_Or_Expression_Function
821 -- Expression_Function case
823 if Token
= Tok_Left_Paren
824 or else Likely_Expression_Function
826 -- Check expression function allowed here
828 if not Pf_Flags
.Pexp
then
829 Error_Msg_SC
("expression function not allowed here!");
832 -- Check we are in Ada 2012 mode
834 if Ada_Version
< Ada_2012
then
836 ("expression function is an Ada 2012 feature!");
838 ("\unit must be compiled with -gnat2012 switch!");
841 -- Catch an illegal placement of the aspect specification
844 -- function_specification
845 -- [aspect_specification] is (expression);
847 -- This case is correctly processed by the parser because
848 -- the expression function first appears as a subprogram
849 -- declaration to the parser.
851 if Is_Non_Empty_List
(Aspects
) then
853 ("aspect specifications must come after parenthesized "
854 & "expression", Sloc
(First
(Aspects
)));
857 -- Parse out expression and build expression function
861 (N_Expression_Function
, Sloc
(Specification_Node
));
862 Set_Specification
(Body_Node
, Specification_Node
);
863 Set_Expression
(Body_Node
, P_Expression
);
865 -- Expression functions can carry pre/postconditions
867 P_Aspect_Specifications
(Body_Node
);
870 -- Subprogram body case
873 -- Check body allowed here
875 if not Pf_Flags
.Pbod
then
876 Error_Msg_SP
("subprogram body not allowed here!");
879 -- Here is the test for a suspicious IS (i.e. one that
880 -- looks like it might more properly be a semicolon).
881 -- See separate section describing use of IS instead
882 -- of semicolon in package Parse.
884 if (Token
in Token_Class_Declk
886 Token
= Tok_Identifier
)
887 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
888 and then Scope
.Last
/= 1
890 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
891 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
894 -- Build and return subprogram body, parsing declarations
895 -- and statement sequence that belong to the body.
898 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
899 Set_Specification
(Body_Node
, Specification_Node
);
901 -- If aspects are present, the specification is parsed as
902 -- a subprogram declaration, and we jump here after seeing
903 -- the keyword IS. Attach asspects previously collected to
906 if Is_Non_Empty_List
(Aspects
) then
907 Set_Parent
(Aspects
, Body_Node
);
908 Set_Aspect_Specifications
(Body_Node
, Aspects
);
911 -- In SPARK, a HIDE directive can be placed at the beginning
912 -- of a subprogram implementation, thus hiding the
913 -- subprogram body from SPARK tool-set. No violation of the
914 -- SPARK restriction should be issued on nodes in a hidden
915 -- part, which is obtained by marking such hidden parts.
917 if Token
= Tok_SPARK_Hide
then
918 Body_Is_Hidden_In_SPARK
:= True;
919 Hidden_Region_Start
:= Token_Ptr
;
920 Scan
; -- past HIDE directive
922 Body_Is_Hidden_In_SPARK
:= False;
925 Parse_Decls_Begin_End
(Body_Node
);
927 if Body_Is_Hidden_In_SPARK
then
928 Set_Hidden_Part_In_SPARK
(Hidden_Region_Start
, Token_Ptr
);
933 end Scan_Body_Or_Expression_Function
;
936 -- Processing for subprogram declaration
938 <<Subprogram_Declaration
>>
940 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
941 Set_Specification
(Decl_Node
, Specification_Node
);
942 Aspects
:= Get_Aspect_Specifications
(Semicolon
=> False);
944 -- Aspects may be present on a subprogram body. The source parsed
945 -- so far is that of its specification, go parse the body and attach
946 -- the collected aspects, if any, to the body.
948 if Token
= Tok_Is
then
950 goto Subprogram_Body
;
953 if Is_Non_Empty_List
(Aspects
) then
954 Set_Parent
(Aspects
, Decl_Node
);
955 Set_Aspect_Specifications
(Decl_Node
, Aspects
);
961 -- If this is a context in which a subprogram body is permitted,
962 -- set active SIS entry in case (see section titled "Handling
963 -- Semicolon Used in Place of IS" in body of Parser package)
964 -- Note that SIS_Missing_Semicolon_Message is already set properly.
966 if Pf_Flags
.Pbod
then
967 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
968 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
969 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
970 SIS_Declaration_Node
:= Decl_Node
;
971 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
972 SIS_Entry_Active
:= True;
979 ---------------------------------
980 -- 6.1 Subprogram Declaration --
981 ---------------------------------
983 -- Parsed by P_Subprogram (6.1)
985 ------------------------------------------
986 -- 6.1 Abstract Subprogram Declaration --
987 ------------------------------------------
989 -- Parsed by P_Subprogram (6.1)
991 -----------------------------------
992 -- 6.1 Subprogram Specification --
993 -----------------------------------
995 -- SUBPROGRAM_SPECIFICATION ::=
996 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
997 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
999 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1001 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1003 -- Subprogram specifications that appear in subprogram declarations
1004 -- are parsed by P_Subprogram (6.1). This routine is used in other
1005 -- contexts where subprogram specifications occur.
1007 -- Note: this routine does not affect the scope stack in any way
1009 -- Error recovery: can raise Error_Resync
1011 function P_Subprogram_Specification
return Node_Id
is
1012 Specification_Node
: Node_Id
;
1013 Result_Not_Null
: Boolean;
1014 Result_Node
: Node_Id
;
1017 if Token
= Tok_Function
then
1018 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
1019 Scan
; -- past FUNCTION
1021 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
1022 Set_Parameter_Specifications
1023 (Specification_Node
, P_Parameter_Profile
);
1024 Check_Junk_Semicolon_Before_Return
;
1027 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1029 -- Ada 2005 (AI-318-02)
1031 if Token
= Tok_Access
then
1032 if Ada_Version
< Ada_2005
then
1034 ("anonymous access result type is an Ada 2005 extension");
1035 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
1038 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
1041 Result_Node
:= P_Subtype_Mark
;
1042 No_Constraint_Maybe_Expr_Func
;
1045 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
1046 Set_Result_Definition
(Specification_Node
, Result_Node
);
1047 return Specification_Node
;
1049 elsif Token
= Tok_Procedure
then
1050 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
1051 Scan
; -- past PROCEDURE
1053 Set_Defining_Unit_Name
1054 (Specification_Node
, P_Defining_Program_Unit_Name
);
1055 Set_Parameter_Specifications
1056 (Specification_Node
, P_Parameter_Profile
);
1057 return Specification_Node
;
1060 Error_Msg_SC
("subprogram specification expected");
1063 end P_Subprogram_Specification
;
1065 ---------------------
1066 -- 6.1 Designator --
1067 ---------------------
1070 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1072 -- The caller has checked that the initial token is an identifier,
1073 -- operator symbol, or string literal. Note that we don't bother to
1074 -- do much error diagnosis in this routine, since it is only used for
1075 -- the label on END lines, and the routines in package Par.Endh will
1076 -- check that the label is appropriate.
1078 -- Error recovery: cannot raise Error_Resync
1080 function P_Designator
return Node_Id
is
1081 Ident_Node
: Node_Id
;
1082 Name_Node
: Node_Id
;
1083 Prefix_Node
: Node_Id
;
1085 function Real_Dot
return Boolean;
1086 -- Tests if a current token is an interesting period, i.e. is followed
1087 -- by an identifier or operator symbol or string literal. If not, it is
1088 -- probably just incorrect punctuation to be caught by our caller. Note
1089 -- that the case of an operator symbol or string literal is also an
1090 -- error, but that is an error that we catch here. If the result is
1091 -- True, a real dot has been scanned and we are positioned past it,
1092 -- if the result is False, the scan position is unchanged.
1098 function Real_Dot
return Boolean is
1099 Scan_State
: Saved_Scan_State
;
1102 if Token
/= Tok_Dot
then
1106 Save_Scan_State
(Scan_State
);
1109 if Token
= Tok_Identifier
1110 or else Token
= Tok_Operator_Symbol
1111 or else Token
= Tok_String_Literal
1116 Restore_Scan_State
(Scan_State
);
1122 -- Start of processing for P_Designator
1125 Ident_Node
:= Token_Node
;
1126 Scan
; -- past initial token
1128 if Prev_Token
= Tok_Operator_Symbol
1129 or else Prev_Token
= Tok_String_Literal
1130 or else not Real_Dot
1137 Prefix_Node
:= Ident_Node
;
1139 -- Loop through child names, on entry to this loop, Prefix contains
1140 -- the name scanned so far, and Ident_Node is the last identifier.
1143 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
1144 Set_Prefix
(Name_Node
, Prefix_Node
);
1145 Ident_Node
:= P_Identifier
;
1146 Set_Selector_Name
(Name_Node
, Ident_Node
);
1147 Prefix_Node
:= Name_Node
;
1148 exit when not Real_Dot
;
1151 -- On exit from the loop, Ident_Node is the last identifier scanned,
1152 -- i.e. the defining identifier, and Prefix_Node is a node for the
1153 -- entire name, structured (incorrectly!) as a selected component.
1155 Name_Node
:= Prefix
(Prefix_Node
);
1156 Change_Node
(Prefix_Node
, N_Designator
);
1157 Set_Name
(Prefix_Node
, Name_Node
);
1158 Set_Identifier
(Prefix_Node
, Ident_Node
);
1163 when Error_Resync
=>
1164 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1171 ------------------------------
1172 -- 6.1 Defining Designator --
1173 ------------------------------
1175 -- DEFINING_DESIGNATOR ::=
1176 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1178 -- Error recovery: cannot raise Error_Resync
1180 function P_Defining_Designator
return Node_Id
is
1182 if Token
= Tok_Operator_Symbol
then
1183 return P_Defining_Operator_Symbol
;
1185 elsif Token
= Tok_String_Literal
then
1186 Error_Msg_SC
("invalid operator name");
1187 Scan
; -- past junk string
1191 return P_Defining_Program_Unit_Name
;
1193 end P_Defining_Designator
;
1195 -------------------------------------
1196 -- 6.1 Defining Program Unit Name --
1197 -------------------------------------
1199 -- DEFINING_PROGRAM_UNIT_NAME ::=
1200 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1202 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1204 -- Error recovery: cannot raise Error_Resync
1206 function P_Defining_Program_Unit_Name
return Node_Id
is
1207 Ident_Node
: Node_Id
;
1208 Name_Node
: Node_Id
;
1209 Prefix_Node
: Node_Id
;
1212 -- Set identifier casing if not already set and scan initial identifier
1214 if Token
= Tok_Identifier
1215 and then Identifier_Casing
(Current_Source_File
) = Unknown
1217 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
1220 Ident_Node
:= P_Identifier
(C_Dot
);
1221 Merge_Identifier
(Ident_Node
, Tok_Return
);
1223 -- Normal case (not child library unit name)
1225 if Token
/= Tok_Dot
then
1226 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1227 Warn_If_Standard_Redefinition
(Ident_Node
);
1230 -- Child library unit name case
1233 if Scope
.Last
> 1 then
1234 Error_Msg_SP
("child unit allowed only at library level");
1237 elsif Ada_Version
= Ada_83
then
1238 Error_Msg_SP
("(Ada 83) child unit not allowed!");
1242 Prefix_Node
:= Ident_Node
;
1244 -- Loop through child names, on entry to this loop, Prefix contains
1245 -- the name scanned so far, and Ident_Node is the last identifier.
1248 exit when Token
/= Tok_Dot
;
1249 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
1250 Scan
; -- past period
1251 Set_Prefix
(Name_Node
, Prefix_Node
);
1252 Ident_Node
:= P_Identifier
(C_Dot
);
1253 Set_Selector_Name
(Name_Node
, Ident_Node
);
1254 Prefix_Node
:= Name_Node
;
1257 -- On exit from the loop, Ident_Node is the last identifier scanned,
1258 -- i.e. the defining identifier, and Prefix_Node is a node for the
1259 -- entire name, structured (incorrectly!) as a selected component.
1261 Name_Node
:= Prefix
(Prefix_Node
);
1262 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
1263 Set_Name
(Prefix_Node
, Name_Node
);
1264 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
1265 Warn_If_Standard_Redefinition
(Ident_Node
);
1266 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
1268 -- All set with unit name parsed
1274 when Error_Resync
=>
1275 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
1280 end P_Defining_Program_Unit_Name
;
1282 --------------------------
1283 -- 6.1 Operator Symbol --
1284 --------------------------
1286 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1288 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1290 -----------------------------------
1291 -- 6.1 Defining Operator Symbol --
1292 -----------------------------------
1294 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1296 -- The caller has checked that the initial symbol is an operator symbol
1298 function P_Defining_Operator_Symbol
return Node_Id
is
1302 Op_Node
:= Token_Node
;
1303 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
1304 Scan
; -- past operator symbol
1306 end P_Defining_Operator_Symbol
;
1308 ----------------------------
1309 -- 6.1 Parameter_Profile --
1310 ----------------------------
1312 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1314 -- Empty is returned if no formal part is present
1316 -- Error recovery: cannot raise Error_Resync
1318 function P_Parameter_Profile
return List_Id
is
1320 if Token
= Tok_Left_Paren
then
1321 Scan
; -- part left paren
1322 return P_Formal_Part
;
1326 end P_Parameter_Profile
;
1328 ---------------------------------------
1329 -- 6.1 Parameter And Result Profile --
1330 ---------------------------------------
1332 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1333 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1335 ----------------------
1336 -- 6.1 Formal part --
1337 ----------------------
1339 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1341 -- PARAMETER_SPECIFICATION ::=
1342 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1343 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1344 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1345 -- [:= DEFAULT_EXPRESSION]
1347 -- This scans the construct Formal_Part. The caller has already checked
1348 -- that the initial token is a left parenthesis, and skipped past it, so
1349 -- that on entry Token is the first token following the left parenthesis.
1351 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1353 -- Error recovery: cannot raise Error_Resync
1355 function P_Formal_Part
return List_Id
is
1356 Specification_List
: List_Id
;
1357 Specification_Node
: Node_Id
;
1358 Scan_State
: Saved_Scan_State
;
1361 Ident_Sloc
: Source_Ptr
;
1362 Not_Null_Present
: Boolean := False;
1363 Not_Null_Sloc
: Source_Ptr
;
1365 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1366 -- This array holds the list of defining identifiers. The upper bound
1367 -- of 4096 is intended to be essentially infinite, and we do not even
1368 -- bother to check for it being exceeded.
1371 Specification_List
:= New_List
;
1372 Specification_Loop
: loop
1374 if Token
= Tok_Pragma
then
1375 Error_Msg_SC
("pragma not allowed in formal part");
1376 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
1379 Ignore
(Tok_Left_Paren
);
1380 Ident_Sloc
:= Token_Ptr
;
1381 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1385 exit Ident_Loop
when Token
= Tok_Colon
;
1387 -- The only valid tokens are colon and comma, so if we have
1388 -- neither do a bit of investigation to see which is the
1389 -- better choice for insertion.
1391 if Token
/= Tok_Comma
then
1393 -- Assume colon if ALIASED, IN or OUT keyword found
1395 exit Ident_Loop
when Token
= Tok_Aliased
or else
1396 Token
= Tok_In
or else
1399 -- Otherwise scan ahead
1401 Save_Scan_State
(Scan_State
);
1404 -- If we run into a semicolon, then assume that a
1405 -- colon was missing, e.g. Parms (X Y; ...). Also
1406 -- assume missing colon on EOF (a real disaster!)
1407 -- and on a right paren, e.g. Parms (X Y), and also
1408 -- on an assignment symbol, e.g. Parms (X Y := ..)
1410 if Token
= Tok_Semicolon
1411 or else Token
= Tok_Right_Paren
1412 or else Token
= Tok_EOF
1413 or else Token
= Tok_Colon_Equal
1415 Restore_Scan_State
(Scan_State
);
1418 -- If we run into a colon, assume that we had a missing
1419 -- comma, e.g. Parms (A B : ...). Also assume a missing
1420 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1422 elsif Token
= Tok_Colon
1423 or else Token
= Tok_Comma
1425 Restore_Scan_State
(Scan_State
);
1430 end loop Look_Ahead
;
1433 -- Here if a comma is present, or to be assumed
1436 Num_Idents
:= Num_Idents
+ 1;
1437 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1438 end loop Ident_Loop
;
1440 -- Fall through the loop on encountering a colon, or deciding
1441 -- that there is a missing colon.
1445 -- If there are multiple identifiers, we repeatedly scan the
1446 -- type and initialization expression information by resetting
1447 -- the scan pointer (so that we get completely separate trees
1448 -- for each occurrence).
1450 if Num_Idents
> 1 then
1451 Save_Scan_State
(Scan_State
);
1454 -- Loop through defining identifiers in list
1458 Ident_List_Loop
: loop
1459 Specification_Node
:=
1460 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1461 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1463 -- Scan possible ALIASED for Ada 2012 (AI-142)
1465 if Token
= Tok_Aliased
then
1466 if Ada_Version
< Ada_2012
then
1467 Error_Msg_SC
("ALIASED parameter is an Ada 2012 feature");
1469 Set_Aliased_Present
(Specification_Node
);
1472 Scan
; -- past ALIASED
1475 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1477 Not_Null_Sloc
:= Token_Ptr
;
1479 P_Null_Exclusion
(Allow_Anonymous_In_95
=> True);
1481 -- Case of ACCESS keyword present
1483 if Token
= Tok_Access
then
1484 Set_Null_Exclusion_Present
1485 (Specification_Node
, Not_Null_Present
);
1487 if Ada_Version
= Ada_83
then
1488 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1492 (Specification_Node
,
1493 P_Access_Definition
(Not_Null_Present
));
1495 -- Case of IN or OUT present
1498 if Token
= Tok_In
or else Token
= Tok_Out
then
1499 if Not_Null_Present
then
1501 ("`NOT NULL` can only be used with `ACCESS`",
1504 if Token
= Tok_In
then
1506 ("\`IN` not allowed together with `ACCESS`",
1510 ("\`OUT` not allowed together with `ACCESS`",
1515 P_Mode
(Specification_Node
);
1516 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1519 Set_Null_Exclusion_Present
1520 (Specification_Node
, Not_Null_Present
);
1522 if Token
= Tok_Procedure
1524 Token
= Tok_Function
1526 Error_Msg_SC
("formal subprogram parameter not allowed");
1529 if Token
= Tok_Left_Paren
then
1530 Discard_Junk_List
(P_Formal_Part
);
1533 if Token
= Tok_Return
then
1535 Discard_Junk_Node
(P_Subtype_Mark
);
1538 Set_Parameter_Type
(Specification_Node
, Error
);
1541 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1546 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1549 Set_Prev_Ids
(Specification_Node
, True);
1552 if Ident
< Num_Idents
then
1553 Set_More_Ids
(Specification_Node
, True);
1556 Append
(Specification_Node
, Specification_List
);
1557 exit Ident_List_Loop
when Ident
= Num_Idents
;
1559 Restore_Scan_State
(Scan_State
);
1560 end loop Ident_List_Loop
;
1563 when Error_Resync
=>
1564 Resync_Semicolon_List
;
1567 if Token
= Tok_Semicolon
then
1568 Save_Scan_State
(Scan_State
);
1569 Scan
; -- past semicolon
1571 -- If we have RETURN or IS after the semicolon, then assume
1572 -- that semicolon should have been a right parenthesis and exit
1574 if Token
= Tok_Is
or else Token
= Tok_Return
then
1575 Error_Msg_SP
-- CODEFIX
1576 ("|"";"" should be "")""");
1577 exit Specification_Loop
;
1580 -- If we have a declaration keyword after the semicolon, then
1581 -- assume we had a missing right parenthesis and terminate list
1583 if Token
in Token_Class_Declk
then
1584 Error_Msg_AP
-- CODEFIX
1586 Restore_Scan_State
(Scan_State
);
1587 exit Specification_Loop
;
1590 elsif Token
= Tok_Right_Paren
then
1591 Scan
; -- past right paren
1592 exit Specification_Loop
;
1594 -- Special check for common error of using comma instead of semicolon
1596 elsif Token
= Tok_Comma
then
1600 -- Special check for omitted separator
1602 elsif Token
= Tok_Identifier
then
1605 -- If nothing sensible, skip to next semicolon or right paren
1609 Resync_Semicolon_List
;
1611 if Token
= Tok_Semicolon
then
1612 Scan
; -- past semicolon
1615 exit Specification_Loop
;
1618 end loop Specification_Loop
;
1620 return Specification_List
;
1623 ----------------------------------
1624 -- 6.1 Parameter Specification --
1625 ----------------------------------
1627 -- Parsed by P_Formal_Part (6.1)
1633 -- MODE ::= [in] | in out | out
1635 -- There is no explicit node in the tree for the Mode. Instead the
1636 -- In_Present and Out_Present flags are set in the parent node to
1637 -- record the presence of keywords specifying the mode.
1639 -- Error_Recovery: cannot raise Error_Resync
1641 procedure P_Mode
(Node
: Node_Id
) is
1643 if Token
= Tok_In
then
1645 Set_In_Present
(Node
, True);
1647 if Style
.Mode_In_Check
and then Token
/= Tok_Out
then
1648 Error_Msg_SP
-- CODEFIX
1649 ("(style) IN should be omitted");
1652 -- Since Ada 2005, formal objects can have an anonymous access type,
1653 -- and of course carry a mode indicator.
1655 if Token
= Tok_Access
1656 and then Nkind
(Node
) /= N_Formal_Object_Declaration
1658 Error_Msg_SP
("IN not allowed together with ACCESS");
1659 Scan
; -- past ACCESS
1663 if Token
= Tok_Out
then
1665 Set_Out_Present
(Node
, True);
1668 if Token
= Tok_In
then
1669 Error_Msg_SC
("IN must precede OUT in parameter mode");
1671 Set_In_Present
(Node
, True);
1675 --------------------------
1676 -- 6.3 Subprogram Body --
1677 --------------------------
1679 -- Parsed by P_Subprogram (6.1)
1681 -----------------------------------
1682 -- 6.4 Procedure Call Statement --
1683 -----------------------------------
1685 -- Parsed by P_Sequence_Of_Statements (5.1)
1687 ------------------------
1688 -- 6.4 Function Call --
1689 ------------------------
1691 -- Parsed by P_Name (4.1)
1693 --------------------------------
1694 -- 6.4 Actual Parameter Part --
1695 --------------------------------
1697 -- Parsed by P_Name (4.1)
1699 --------------------------------
1700 -- 6.4 Parameter Association --
1701 --------------------------------
1703 -- Parsed by P_Name (4.1)
1705 ------------------------------------
1706 -- 6.4 Explicit Actual Parameter --
1707 ------------------------------------
1709 -- Parsed by P_Name (4.1)
1711 ---------------------------
1712 -- 6.5 Return Statement --
1713 ---------------------------
1715 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1717 -- EXTENDED_RETURN_STATEMENT ::=
1718 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1719 -- [:= EXPRESSION] [do
1720 -- HANDLED_SEQUENCE_OF_STATEMENTS
1723 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1725 -- RETURN_STATEMENT ::= return [EXPRESSION];
1727 -- Error recovery: can raise Error_Resync
1729 procedure P_Return_Subtype_Indication
(Decl_Node
: Node_Id
) is
1731 -- Note: We don't need to check Ada_Version here, because this is
1732 -- only called in >= Ada 2005 cases anyway.
1734 Not_Null_Present
: constant Boolean := P_Null_Exclusion
;
1737 Set_Null_Exclusion_Present
(Decl_Node
, Not_Null_Present
);
1739 if Token
= Tok_Access
then
1740 Set_Object_Definition
1741 (Decl_Node
, P_Access_Definition
(Not_Null_Present
));
1743 Set_Object_Definition
1744 (Decl_Node
, P_Subtype_Indication
(Not_Null_Present
));
1746 end P_Return_Subtype_Indication
;
1748 -- Error recovery: can raise Error_Resync
1750 function P_Return_Object_Declaration
return Node_Id
is
1751 Return_Obj
: Node_Id
;
1752 Decl_Node
: Node_Id
;
1755 Return_Obj
:= Token_Node
;
1756 Change_Identifier_To_Defining_Identifier
(Return_Obj
);
1757 Warn_If_Standard_Redefinition
(Return_Obj
);
1758 Decl_Node
:= New_Node
(N_Object_Declaration
, Token_Ptr
);
1759 Set_Defining_Identifier
(Decl_Node
, Return_Obj
);
1761 Scan
; -- past identifier
1764 -- First an error check, if we have two identifiers in a row, a likely
1765 -- possibility is that the first of the identifiers is an incorrectly
1766 -- spelled keyword. See similar check in P_Identifier_Declarations.
1768 if Token
= Tok_Identifier
then
1770 SS
: Saved_Scan_State
;
1774 Save_Scan_State
(SS
);
1775 Scan
; -- past initial identifier
1776 I2
:= (Token
= Tok_Identifier
);
1777 Restore_Scan_State
(SS
);
1781 (Bad_Spelling_Of
(Tok_Access
) or else
1782 Bad_Spelling_Of
(Tok_Aliased
) or else
1783 Bad_Spelling_Of
(Tok_Constant
))
1790 -- We allow "constant" here (as in "return Result : constant
1791 -- T..."). This is not in the latest RM, but the ARG is considering an
1792 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1794 if Token
= Tok_Constant
then
1795 Scan
; -- past CONSTANT
1796 Set_Constant_Present
(Decl_Node
);
1798 if Token
= Tok_Aliased
then
1799 Error_Msg_SC
-- CODEFIX
1800 ("ALIASED should be before CONSTANT");
1801 Scan
; -- past ALIASED
1802 Set_Aliased_Present
(Decl_Node
);
1805 elsif Token
= Tok_Aliased
then
1806 Scan
; -- past ALIASED
1807 Set_Aliased_Present
(Decl_Node
);
1809 -- The restrictions on the use of aliased in an extended return
1810 -- are semantic, not syntactic.
1812 if Token
= Tok_Constant
then
1813 Scan
; -- past CONSTANT
1814 Set_Constant_Present
(Decl_Node
);
1818 P_Return_Subtype_Indication
(Decl_Node
);
1820 if Token
= Tok_Colon_Equal
then
1822 Set_Expression
(Decl_Node
, P_Expression_No_Right_Paren
);
1826 end P_Return_Object_Declaration
;
1828 -- Error recovery: can raise Error_Resync
1830 function P_Return_Statement
return Node_Id
is
1831 -- The caller has checked that the initial token is RETURN
1833 function Is_Simple
return Boolean;
1834 -- Scan state is just after RETURN (and is left that way).
1835 -- Determine whether this is a simple or extended return statement
1836 -- by looking ahead for "identifier :", which implies extended.
1842 function Is_Simple
return Boolean is
1843 Scan_State
: Saved_Scan_State
;
1844 Result
: Boolean := True;
1847 if Token
= Tok_Identifier
then
1848 Save_Scan_State
(Scan_State
); -- at identifier
1849 Scan
; -- past identifier
1851 if Token
= Tok_Colon
then
1852 Result
:= False; -- It's an extended_return_statement.
1855 Restore_Scan_State
(Scan_State
); -- to identifier
1861 Return_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1862 Return_Node
: Node_Id
;
1864 -- Start of processing for P_Return_Statement
1867 Scan
; -- past RETURN
1869 -- Simple_return_statement, no expression, return an
1870 -- N_Simple_Return_Statement node with the expression field left Empty.
1872 if Token
= Tok_Semicolon
then
1874 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1879 -- Simple_return_statement with expression
1881 -- We avoid trying to scan an expression if we are at an
1882 -- expression terminator since in that case the best error
1883 -- message is probably that we have a missing semicolon.
1886 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1888 if Token
not in Token_Class_Eterm
then
1889 Set_Expression
(Return_Node
, P_Expression_No_Right_Paren
);
1892 -- Extended_return_statement (Ada 2005 only -- AI-318):
1895 if Ada_Version
< Ada_2005
then
1897 (" extended_return_statement is an Ada 2005 extension");
1898 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
1901 Return_Node
:= New_Node
(N_Extended_Return_Statement
, Return_Sloc
);
1902 Set_Return_Object_Declarations
1903 (Return_Node
, New_List
(P_Return_Object_Declaration
));
1905 if Token
= Tok_Do
then
1907 Scope
.Table
(Scope
.Last
).Etyp
:= E_Return
;
1908 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
1909 Scope
.Table
(Scope
.Last
).Sloc
:= Return_Sloc
;
1912 Set_Handled_Statement_Sequence
1913 (Return_Node
, P_Handled_Sequence_Of_Statements
);
1916 -- Do we need to handle Error_Resync here???
1924 end P_Return_Statement
;