1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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.
86 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
88 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
89 -- SUBPROGRAM_SPECIFICATION is abstract;
91 -- SUBPROGRAM_SPECIFICATION ::=
92 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
93 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
95 -- PARAMETER_PROFILE ::= [FORMAL_PART]
97 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
99 -- SUBPROGRAM_BODY ::=
100 -- SUBPROGRAM_SPECIFICATION is
103 -- HANDLED_SEQUENCE_OF_STATEMENTS
106 -- SUBPROGRAM_RENAMING_DECLARATION ::=
107 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
109 -- SUBPROGRAM_BODY_STUB ::=
110 -- SUBPROGRAM_SPECIFICATION is separate;
112 -- GENERIC_INSTANTIATION ::=
113 -- procedure DEFINING_PROGRAM_UNIT_NAME is
114 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
115 -- | function DEFINING_DESIGNATOR is
116 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
118 -- NULL_PROCEDURE_DECLARATION ::=
119 -- SUBPROGRAM_SPECIFICATION is null;
121 -- Null procedures are an Ada 2005 feature. A null procedure declaration
122 -- is classified as a basic declarative item, but it is parsed here, with
123 -- other subprogram constructs.
125 -- The value in Pf_Flags indicates which of these possible declarations
126 -- is acceptable to the caller:
128 -- Pf_Flags.Decl Set if declaration OK
129 -- Pf_Flags.Gins Set if generic instantiation OK
130 -- Pf_Flags.Pbod Set if proper body OK
131 -- Pf_Flags.Rnam Set if renaming declaration OK
132 -- Pf_Flags.Stub Set if body stub OK
134 -- If an inappropriate form is encountered, it is scanned out but an
135 -- error message indicating that it is appearing in an inappropriate
136 -- context is issued. The only possible values for Pf_Flags are those
137 -- defined as constants in the Par package.
139 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
140 -- NOT or OVERRIDING.
142 -- Error recovery: cannot raise Error_Resync
144 function P_Subprogram
(Pf_Flags
: Pf_Rec
) return Node_Id
is
145 Specification_Node
: Node_Id
;
147 Fpart_List
: List_Id
;
148 Fpart_Sloc
: Source_Ptr
;
149 Result_Not_Null
: Boolean := False;
150 Result_Node
: Node_Id
;
154 Rename_Node
: Node_Id
;
155 Absdec_Node
: Node_Id
;
157 Fproc_Sloc
: Source_Ptr
;
159 Scan_State
: Saved_Scan_State
;
161 -- Flags for optional overriding indication. Two flags are needed,
162 -- to distinguish positive and negative overriding indicators from
163 -- the absence of any indicator.
165 Is_Overriding
: Boolean := False;
166 Not_Overriding
: Boolean := False;
169 -- Set up scope stack entry. Note that the Labl field will be set later
171 SIS_Entry_Active
:= False;
172 SIS_Missing_Semicolon_Message
:= No_Error_Msg
;
174 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
175 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
176 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
177 Scope
.Table
(Scope
.Last
).Lreq
:= False;
179 -- Ada2005: scan leading NOT OVERRIDING indicator
181 if Token
= Tok_Not
then
184 if Token
= Tok_Overriding
then
185 Scan
; -- past OVERRIDING
186 Not_Overriding
:= True;
188 -- Overriding keyword used in non Ada 2005 mode
190 elsif Token
= Tok_Identifier
191 and then Token_Name
= Name_Overriding
193 Error_Msg_SC
("overriding indicator is an Ada 2005 extension");
194 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
195 Scan
; -- past Overriding
196 Not_Overriding
:= True;
199 Error_Msg_SC
-- CODEFIX
200 ("OVERRIDING expected!");
203 -- Ada 2005: scan leading OVERRIDING indicator
205 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
206 -- declaration circuit already gave an error message and changed the
207 -- token to Tok_Overriding.
209 elsif Token
= Tok_Overriding
then
210 Scan
; -- past OVERRIDING
211 Is_Overriding
:= True;
214 if Is_Overriding
or else Not_Overriding
then
216 -- Note that if we are not in Ada_05 mode, error messages have
217 -- already been given, so no need to give another message here.
219 -- An overriding indicator is allowed for subprogram declarations,
220 -- bodies (including subunits), renamings, stubs, and
221 -- instantiations. The test against Pf_Decl_Pbod is added to account
222 -- for the case of subprograms declared in a protected type, where
223 -- only subprogram declarations and bodies can occur. The Pf_Pbod
224 -- case is for subunits.
226 if Pf_Flags
/= Pf_Decl_Gins_Pbod_Rnam_Stub
228 Pf_Flags
/= Pf_Decl_Pbod
232 Error_Msg_SC
("overriding indicator not allowed here!");
234 elsif Token
/= Tok_Function
and then Token
/= Tok_Procedure
then
235 Error_Msg_SC
-- CODEFIX
236 ("FUNCTION or PROCEDURE expected!");
240 Func
:= (Token
= Tok_Function
);
241 Fproc_Sloc
:= Token_Ptr
;
242 Scan
; -- past FUNCTION or PROCEDURE
247 Name_Node
:= P_Defining_Designator
;
249 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
250 and then Scope
.Last
= 1
252 Error_Msg_SP
("operator symbol not allowed at library level");
253 Name_Node
:= New_Entity
(N_Defining_Identifier
, Sloc
(Name_Node
));
255 -- Set name from file name, we need some junk name, and that's
256 -- as good as anything. This is only approximate, since we do
257 -- not do anything with non-standard name translations.
259 Get_Name_String
(File_Name
(Current_Source_File
));
261 for J
in 1 .. Name_Len
loop
262 if Name_Buffer
(J
) = '.' then
268 Set_Chars
(Name_Node
, Name_Find
);
269 Set_Error_Posted
(Name_Node
);
273 Name_Node
:= P_Defining_Program_Unit_Name
;
276 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
279 -- Deal with generic instantiation, the one case in which we do not
280 -- have a subprogram specification as part of whatever we are parsing
282 if Token
= Tok_Is
then
283 Save_Scan_State
(Scan_State
); -- at the IS
284 T_Is
; -- checks for redundant IS
286 if Token
= Tok_New
then
287 if not Pf_Flags
.Gins
then
288 Error_Msg_SC
("generic instantiation not allowed here!");
294 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
295 Set_Name
(Inst_Node
, P_Function_Name
);
297 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
298 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
301 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
302 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
304 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
306 if Is_Overriding
then
307 Set_Must_Override
(Inst_Node
);
309 elsif Not_Overriding
then
310 Set_Must_Not_Override
(Inst_Node
);
316 Restore_Scan_State
(Scan_State
); -- to the IS
320 -- If not a generic instantiation, then we definitely have a subprogram
321 -- specification (all possibilities at this stage include one here)
323 Fpart_Sloc
:= Token_Ptr
;
325 Check_Misspelling_Of
(Tok_Return
);
327 -- Scan formal part. First a special error check. If we have an
328 -- identifier here, then we have a definite error. If this identifier
329 -- is on the same line as the designator, then we assume it is the
330 -- first formal after a missing left parenthesis
332 if Token
= Tok_Identifier
333 and then not Token_Is_At_Start_Of_Line
335 T_Left_Paren
; -- to generate message
336 Fpart_List
:= P_Formal_Part
;
338 -- Otherwise scan out an optional formal part in the usual manner
341 Fpart_List
:= P_Parameter_Profile
;
344 -- We treat what we have as a function specification if FUNCTION was
345 -- used, or if a RETURN is present. This gives better error recovery
346 -- since later RETURN statements will be valid in either case.
348 Check_Junk_Semicolon_Before_Return
;
349 Result_Node
:= Error
;
351 if Token
= Tok_Return
then
354 ("PROCEDURE should be FUNCTION", Fproc_Sloc
);
360 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
362 -- Ada 2005 (AI-318-02)
364 if Token
= Tok_Access
then
365 if Ada_Version
< Ada_05
then
367 ("anonymous access result type is an Ada 2005 extension");
368 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
371 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
374 Result_Node
:= P_Subtype_Mark
;
380 Ignore
(Tok_Right_Paren
);
386 Specification_Node
:=
387 New_Node
(N_Function_Specification
, Fproc_Sloc
);
389 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
390 Set_Result_Definition
(Specification_Node
, Result_Node
);
393 Specification_Node
:=
394 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
397 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
398 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
400 if Is_Overriding
then
401 Set_Must_Override
(Specification_Node
);
403 elsif Not_Overriding
then
404 Set_Must_Not_Override
(Specification_Node
);
407 -- Error check: barriers not allowed on protected functions/procedures
409 if Token
= Tok_When
then
411 Error_Msg_SC
("barrier not allowed on function, only on entry");
413 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
417 Discard_Junk_Node
(P_Expression
);
420 -- Deal with semicolon followed by IS. We want to treat this as IS
422 if Token
= Tok_Semicolon
then
423 Save_Scan_State
(Scan_State
);
424 Scan
; -- past semicolon
426 if Token
= Tok_Is
then
427 Error_Msg_SP
-- CODEFIX
428 ("extra "";"" ignored");
430 Restore_Scan_State
(Scan_State
);
434 -- Deal with case of semicolon ending a subprogram declaration
436 if Token
= Tok_Semicolon
then
437 if not Pf_Flags
.Decl
then
441 Scan
; -- past semicolon
443 -- If semicolon is immediately followed by IS, then ignore the
444 -- semicolon, and go process the body.
446 if Token
= Tok_Is
then
447 Error_Msg_SP
-- CODEFIX
448 ("|extra "";"" ignored");
449 T_Is
; -- scan past IS
450 goto Subprogram_Body
;
452 -- If BEGIN follows in an appropriate column, we immediately
453 -- commence the error action of assuming that the previous
454 -- subprogram declaration should have been a subprogram body,
455 -- i.e. that the terminating semicolon should have been IS.
457 elsif Token
= Tok_Begin
458 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
460 Error_Msg_SP
-- CODEFIX
461 ("|"";"" should be IS!");
462 goto Subprogram_Body
;
465 goto Subprogram_Declaration
;
468 -- Case of not followed by semicolon
471 -- Subprogram renaming declaration case
473 Check_Misspelling_Of
(Tok_Renames
);
475 if Token
= Tok_Renames
then
476 if not Pf_Flags
.Rnam
then
477 Error_Msg_SC
("renaming declaration not allowed here!");
481 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
482 Scan
; -- past RENAMES
483 Set_Name
(Rename_Node
, P_Name
);
484 Set_Specification
(Rename_Node
, Specification_Node
);
489 -- Case of IS following subprogram specification
491 elsif Token
= Tok_Is
then
492 T_Is
; -- ignore redundant Is's
494 if Token_Name
= Name_Abstract
then
495 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
498 -- Deal nicely with (now obsolete) use of <> in place of abstract
500 if Token
= Tok_Box
then
501 Error_Msg_SC
-- CODEFIX
502 ("ABSTRACT expected");
503 Token
:= Tok_Abstract
;
506 -- Abstract subprogram declaration case
508 if Token
= Tok_Abstract
then
510 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
511 Set_Specification
(Absdec_Node
, Specification_Node
);
512 Pop_Scope_Stack
; -- discard unneeded entry
513 Scan
; -- past ABSTRACT
517 -- Ada 2005 (AI-248): Parse a null procedure declaration
519 elsif Token
= Tok_Null
then
520 if Ada_Version
< Ada_05
then
521 Error_Msg_SP
("null procedures are an Ada 2005 extension");
522 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
528 Error_Msg_SP
("only procedures can be null");
530 Set_Null_Present
(Specification_Node
);
534 goto Subprogram_Declaration
;
536 -- Check for IS NEW with Formal_Part present and handle nicely
538 elsif Token
= Tok_New
then
540 ("formal part not allowed in instantiation", Fpart_Sloc
);
544 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
547 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
550 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
551 Set_Name
(Inst_Node
, P_Name
);
552 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
554 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
558 goto Subprogram_Body
;
561 -- Here we have a missing IS or missing semicolon, we always guess
562 -- a missing semicolon, since we are pretty good at fixing up a
563 -- semicolon which should really be an IS
566 Error_Msg_AP
-- CODEFIX
568 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
569 goto Subprogram_Declaration
;
573 -- Processing for subprogram body
576 if not Pf_Flags
.Pbod
then
577 Error_Msg_SP
("subprogram body not allowed here!");
580 -- Subprogram body stub case
582 if Separate_Present
then
583 if not Pf_Flags
.Stub
then
584 Error_Msg_SC
("body stub not allowed here!");
587 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
589 ("operator symbol cannot be used as subunit name",
594 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
595 Set_Specification
(Stub_Node
, Specification_Node
);
596 Scan
; -- past SEPARATE
601 -- Subprogram body case
604 -- Here is the test for a suspicious IS (i.e. one that looks
605 -- like it might more properly be a semicolon). See separate
606 -- section discussing use of IS instead of semicolon in
609 if (Token
in Token_Class_Declk
611 Token
= Tok_Identifier
)
612 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
613 and then Scope
.Last
/= 1
615 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
616 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
620 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
621 Set_Specification
(Body_Node
, Specification_Node
);
622 Parse_Decls_Begin_End
(Body_Node
);
626 -- Processing for subprogram declaration
628 <<Subprogram_Declaration
>>
630 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
631 Set_Specification
(Decl_Node
, Specification_Node
);
633 -- If this is a context in which a subprogram body is permitted,
634 -- set active SIS entry in case (see section titled "Handling
635 -- Semicolon Used in Place of IS" in body of Parser package)
636 -- Note that SIS_Missing_Semicolon_Message is already set properly.
638 if Pf_Flags
.Pbod
then
639 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
640 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
641 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
642 SIS_Declaration_Node
:= Decl_Node
;
643 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
644 SIS_Entry_Active
:= True;
652 ---------------------------------
653 -- 6.1 Subprogram Declaration --
654 ---------------------------------
656 -- Parsed by P_Subprogram (6.1)
658 ------------------------------------------
659 -- 6.1 Abstract Subprogram Declaration --
660 ------------------------------------------
662 -- Parsed by P_Subprogram (6.1)
664 -----------------------------------
665 -- 6.1 Subprogram Specification --
666 -----------------------------------
668 -- SUBPROGRAM_SPECIFICATION ::=
669 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
670 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
672 -- PARAMETER_PROFILE ::= [FORMAL_PART]
674 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
676 -- Subprogram specifications that appear in subprogram declarations
677 -- are parsed by P_Subprogram (6.1). This routine is used in other
678 -- contexts where subprogram specifications occur.
680 -- Note: this routine does not affect the scope stack in any way
682 -- Error recovery: can raise Error_Resync
684 function P_Subprogram_Specification
return Node_Id
is
685 Specification_Node
: Node_Id
;
686 Result_Not_Null
: Boolean;
687 Result_Node
: Node_Id
;
690 if Token
= Tok_Function
then
691 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
692 Scan
; -- past FUNCTION
694 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
695 Set_Parameter_Specifications
696 (Specification_Node
, P_Parameter_Profile
);
697 Check_Junk_Semicolon_Before_Return
;
700 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
702 -- Ada 2005 (AI-318-02)
704 if Token
= Tok_Access
then
705 if Ada_Version
< Ada_05
then
707 ("anonymous access result type is an Ada 2005 extension");
708 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
711 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
714 Result_Node
:= P_Subtype_Mark
;
718 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
719 Set_Result_Definition
(Specification_Node
, Result_Node
);
720 return Specification_Node
;
722 elsif Token
= Tok_Procedure
then
723 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
724 Scan
; -- past PROCEDURE
726 Set_Defining_Unit_Name
727 (Specification_Node
, P_Defining_Program_Unit_Name
);
728 Set_Parameter_Specifications
729 (Specification_Node
, P_Parameter_Profile
);
730 return Specification_Node
;
733 Error_Msg_SC
("subprogram specification expected");
736 end P_Subprogram_Specification
;
738 ---------------------
740 ---------------------
743 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
745 -- The caller has checked that the initial token is an identifier,
746 -- operator symbol, or string literal. Note that we don't bother to
747 -- do much error diagnosis in this routine, since it is only used for
748 -- the label on END lines, and the routines in package Par.Endh will
749 -- check that the label is appropriate.
751 -- Error recovery: cannot raise Error_Resync
753 function P_Designator
return Node_Id
is
754 Ident_Node
: Node_Id
;
756 Prefix_Node
: Node_Id
;
758 function Real_Dot
return Boolean;
759 -- Tests if a current token is an interesting period, i.e. is followed
760 -- by an identifier or operator symbol or string literal. If not, it is
761 -- probably just incorrect punctuation to be caught by our caller. Note
762 -- that the case of an operator symbol or string literal is also an
763 -- error, but that is an error that we catch here. If the result is
764 -- True, a real dot has been scanned and we are positioned past it,
765 -- if the result is False, the scan position is unchanged.
771 function Real_Dot
return Boolean is
772 Scan_State
: Saved_Scan_State
;
775 if Token
/= Tok_Dot
then
779 Save_Scan_State
(Scan_State
);
782 if Token
= Tok_Identifier
783 or else Token
= Tok_Operator_Symbol
784 or else Token
= Tok_String_Literal
789 Restore_Scan_State
(Scan_State
);
795 -- Start of processing for P_Designator
798 Ident_Node
:= Token_Node
;
799 Scan
; -- past initial token
801 if Prev_Token
= Tok_Operator_Symbol
802 or else Prev_Token
= Tok_String_Literal
810 Prefix_Node
:= Ident_Node
;
812 -- Loop through child names, on entry to this loop, Prefix contains
813 -- the name scanned so far, and Ident_Node is the last identifier.
816 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
817 Set_Prefix
(Name_Node
, Prefix_Node
);
818 Ident_Node
:= P_Identifier
;
819 Set_Selector_Name
(Name_Node
, Ident_Node
);
820 Prefix_Node
:= Name_Node
;
821 exit when not Real_Dot
;
824 -- On exit from the loop, Ident_Node is the last identifier scanned,
825 -- i.e. the defining identifier, and Prefix_Node is a node for the
826 -- entire name, structured (incorrectly!) as a selected component.
828 Name_Node
:= Prefix
(Prefix_Node
);
829 Change_Node
(Prefix_Node
, N_Designator
);
830 Set_Name
(Prefix_Node
, Name_Node
);
831 Set_Identifier
(Prefix_Node
, Ident_Node
);
837 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
844 ------------------------------
845 -- 6.1 Defining Designator --
846 ------------------------------
848 -- DEFINING_DESIGNATOR ::=
849 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
851 -- Error recovery: cannot raise Error_Resync
853 function P_Defining_Designator
return Node_Id
is
855 if Token
= Tok_Operator_Symbol
then
856 return P_Defining_Operator_Symbol
;
858 elsif Token
= Tok_String_Literal
then
859 Error_Msg_SC
("invalid operator name");
860 Scan
; -- past junk string
864 return P_Defining_Program_Unit_Name
;
866 end P_Defining_Designator
;
868 -------------------------------------
869 -- 6.1 Defining Program Unit Name --
870 -------------------------------------
872 -- DEFINING_PROGRAM_UNIT_NAME ::=
873 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
875 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
877 -- Error recovery: cannot raise Error_Resync
879 function P_Defining_Program_Unit_Name
return Node_Id
is
880 Ident_Node
: Node_Id
;
882 Prefix_Node
: Node_Id
;
885 -- Set identifier casing if not already set and scan initial identifier
887 if Token
= Tok_Identifier
888 and then Identifier_Casing
(Current_Source_File
) = Unknown
890 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
893 Ident_Node
:= P_Identifier
(C_Dot
);
894 Merge_Identifier
(Ident_Node
, Tok_Return
);
896 -- Normal case (not child library unit name)
898 if Token
/= Tok_Dot
then
899 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
902 -- Child library unit name case
905 if Scope
.Last
> 1 then
906 Error_Msg_SP
("child unit allowed only at library level");
909 elsif Ada_Version
= Ada_83
then
910 Error_Msg_SP
("(Ada 83) child unit not allowed!");
914 Prefix_Node
:= Ident_Node
;
916 -- Loop through child names, on entry to this loop, Prefix contains
917 -- the name scanned so far, and Ident_Node is the last identifier.
920 exit when Token
/= Tok_Dot
;
921 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
923 Set_Prefix
(Name_Node
, Prefix_Node
);
924 Ident_Node
:= P_Identifier
(C_Dot
);
925 Set_Selector_Name
(Name_Node
, Ident_Node
);
926 Prefix_Node
:= Name_Node
;
929 -- On exit from the loop, Ident_Node is the last identifier scanned,
930 -- i.e. the defining identifier, and Prefix_Node is a node for the
931 -- entire name, structured (incorrectly!) as a selected component.
933 Name_Node
:= Prefix
(Prefix_Node
);
934 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
935 Set_Name
(Prefix_Node
, Name_Node
);
936 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
937 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
939 -- All set with unit name parsed
946 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
951 end P_Defining_Program_Unit_Name
;
953 --------------------------
954 -- 6.1 Operator Symbol --
955 --------------------------
957 -- OPERATOR_SYMBOL ::= STRING_LITERAL
959 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
961 -----------------------------------
962 -- 6.1 Defining Operator Symbol --
963 -----------------------------------
965 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
967 -- The caller has checked that the initial symbol is an operator symbol
969 function P_Defining_Operator_Symbol
return Node_Id
is
973 Op_Node
:= Token_Node
;
974 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
975 Scan
; -- past operator symbol
977 end P_Defining_Operator_Symbol
;
979 ----------------------------
980 -- 6.1 Parameter_Profile --
981 ----------------------------
983 -- PARAMETER_PROFILE ::= [FORMAL_PART]
985 -- Empty is returned if no formal part is present
987 -- Error recovery: cannot raise Error_Resync
989 function P_Parameter_Profile
return List_Id
is
991 if Token
= Tok_Left_Paren
then
992 Scan
; -- part left paren
993 return P_Formal_Part
;
997 end P_Parameter_Profile
;
999 ---------------------------------------
1000 -- 6.1 Parameter And Result Profile --
1001 ---------------------------------------
1003 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1004 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1006 ----------------------
1007 -- 6.1 Formal part --
1008 ----------------------
1010 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1012 -- PARAMETER_SPECIFICATION ::=
1013 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
1014 -- [:= DEFAULT_EXPRESSION]
1015 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1016 -- [:= DEFAULT_EXPRESSION]
1018 -- This scans the construct Formal_Part. The caller has already checked
1019 -- that the initial token is a left parenthesis, and skipped past it, so
1020 -- that on entry Token is the first token following the left parenthesis.
1022 -- Error recovery: cannot raise Error_Resync
1024 function P_Formal_Part
return List_Id
is
1025 Specification_List
: List_Id
;
1026 Specification_Node
: Node_Id
;
1027 Scan_State
: Saved_Scan_State
;
1030 Ident_Sloc
: Source_Ptr
;
1031 Not_Null_Present
: Boolean := False;
1032 Not_Null_Sloc
: Source_Ptr
;
1034 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1035 -- This array holds the list of defining identifiers. The upper bound
1036 -- of 4096 is intended to be essentially infinite, and we do not even
1037 -- bother to check for it being exceeded.
1040 Specification_List
:= New_List
;
1041 Specification_Loop
: loop
1043 if Token
= Tok_Pragma
then
1044 Error_Msg_SC
("pragma not allowed in formal part");
1045 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
1048 Ignore
(Tok_Left_Paren
);
1049 Ident_Sloc
:= Token_Ptr
;
1050 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1054 exit Ident_Loop
when Token
= Tok_Colon
;
1056 -- The only valid tokens are colon and comma, so if we have
1057 -- neither do a bit of investigation to see which is the
1058 -- better choice for insertion.
1060 if Token
/= Tok_Comma
then
1062 -- Assume colon if IN or OUT keyword found
1064 exit Ident_Loop
when Token
= Tok_In
or else Token
= Tok_Out
;
1066 -- Otherwise scan ahead
1068 Save_Scan_State
(Scan_State
);
1071 -- If we run into a semicolon, then assume that a
1072 -- colon was missing, e.g. Parms (X Y; ...). Also
1073 -- assume missing colon on EOF (a real disaster!)
1074 -- and on a right paren, e.g. Parms (X Y), and also
1075 -- on an assignment symbol, e.g. Parms (X Y := ..)
1077 if Token
= Tok_Semicolon
1078 or else Token
= Tok_Right_Paren
1079 or else Token
= Tok_EOF
1080 or else Token
= Tok_Colon_Equal
1082 Restore_Scan_State
(Scan_State
);
1085 -- If we run into a colon, assume that we had a missing
1086 -- comma, e.g. Parms (A B : ...). Also assume a missing
1087 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1089 elsif Token
= Tok_Colon
1090 or else Token
= Tok_Comma
1092 Restore_Scan_State
(Scan_State
);
1097 end loop Look_Ahead
;
1100 -- Here if a comma is present, or to be assumed
1103 Num_Idents
:= Num_Idents
+ 1;
1104 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1105 end loop Ident_Loop
;
1107 -- Fall through the loop on encountering a colon, or deciding
1108 -- that there is a missing colon.
1112 -- If there are multiple identifiers, we repeatedly scan the
1113 -- type and initialization expression information by resetting
1114 -- the scan pointer (so that we get completely separate trees
1115 -- for each occurrence).
1117 if Num_Idents
> 1 then
1118 Save_Scan_State
(Scan_State
);
1121 -- Loop through defining identifiers in list
1125 Ident_List_Loop
: loop
1126 Specification_Node
:=
1127 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1128 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1130 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1132 Not_Null_Sloc
:= Token_Ptr
;
1134 P_Null_Exclusion
(Allow_Anonymous_In_95
=> True);
1136 -- Case of ACCESS keyword present
1138 if Token
= Tok_Access
then
1139 Set_Null_Exclusion_Present
1140 (Specification_Node
, Not_Null_Present
);
1142 if Ada_Version
= Ada_83
then
1143 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1147 (Specification_Node
,
1148 P_Access_Definition
(Not_Null_Present
));
1150 -- Case of IN or OUT present
1153 if Token
= Tok_In
or else Token
= Tok_Out
then
1154 if Not_Null_Present
then
1156 ("`NOT NULL` can only be used with `ACCESS`",
1159 if Token
= Tok_In
then
1161 ("\`IN` not allowed together with `ACCESS`",
1165 ("\`OUT` not allowed together with `ACCESS`",
1170 P_Mode
(Specification_Node
);
1171 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1174 Set_Null_Exclusion_Present
1175 (Specification_Node
, Not_Null_Present
);
1177 if Token
= Tok_Procedure
1179 Token
= Tok_Function
1181 Error_Msg_SC
("formal subprogram parameter not allowed");
1184 if Token
= Tok_Left_Paren
then
1185 Discard_Junk_List
(P_Formal_Part
);
1188 if Token
= Tok_Return
then
1190 Discard_Junk_Node
(P_Subtype_Mark
);
1193 Set_Parameter_Type
(Specification_Node
, Error
);
1196 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1201 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1204 Set_Prev_Ids
(Specification_Node
, True);
1207 if Ident
< Num_Idents
then
1208 Set_More_Ids
(Specification_Node
, True);
1211 Append
(Specification_Node
, Specification_List
);
1212 exit Ident_List_Loop
when Ident
= Num_Idents
;
1214 Restore_Scan_State
(Scan_State
);
1215 end loop Ident_List_Loop
;
1218 when Error_Resync
=>
1219 Resync_Semicolon_List
;
1222 if Token
= Tok_Semicolon
then
1223 Save_Scan_State
(Scan_State
);
1224 Scan
; -- past semicolon
1226 -- If we have RETURN or IS after the semicolon, then assume
1227 -- that semicolon should have been a right parenthesis and exit
1229 if Token
= Tok_Is
or else Token
= Tok_Return
then
1230 Error_Msg_SP
-- CODEFIX
1231 ("|"";"" should be "")""");
1232 exit Specification_Loop
;
1235 -- If we have a declaration keyword after the semicolon, then
1236 -- assume we had a missing right parenthesis and terminate list
1238 if Token
in Token_Class_Declk
then
1239 Error_Msg_AP
-- CODEFIX
1241 Restore_Scan_State
(Scan_State
);
1242 exit Specification_Loop
;
1245 elsif Token
= Tok_Right_Paren
then
1246 Scan
; -- past right paren
1247 exit Specification_Loop
;
1249 -- Special check for common error of using comma instead of semicolon
1251 elsif Token
= Tok_Comma
then
1255 -- Special check for omitted separator
1257 elsif Token
= Tok_Identifier
then
1260 -- If nothing sensible, skip to next semicolon or right paren
1264 Resync_Semicolon_List
;
1266 if Token
= Tok_Semicolon
then
1267 Scan
; -- past semicolon
1270 exit Specification_Loop
;
1273 end loop Specification_Loop
;
1275 return Specification_List
;
1278 ----------------------------------
1279 -- 6.1 Parameter Specification --
1280 ----------------------------------
1282 -- Parsed by P_Formal_Part (6.1)
1288 -- MODE ::= [in] | in out | out
1290 -- There is no explicit node in the tree for the Mode. Instead the
1291 -- In_Present and Out_Present flags are set in the parent node to
1292 -- record the presence of keywords specifying the mode.
1294 -- Error_Recovery: cannot raise Error_Resync
1296 procedure P_Mode
(Node
: Node_Id
) is
1298 if Token
= Tok_In
then
1300 Set_In_Present
(Node
, True);
1302 if Style
.Mode_In_Check
and then Token
/= Tok_Out
then
1303 Error_Msg_SP
-- CODEFIX
1304 ("(style) IN should be omitted");
1307 if Token
= Tok_Access
then
1308 Error_Msg_SP
("IN not allowed together with ACCESS");
1309 Scan
; -- past ACCESS
1313 if Token
= Tok_Out
then
1315 Set_Out_Present
(Node
, True);
1318 if Token
= Tok_In
then
1319 Error_Msg_SC
("IN must precede OUT in parameter mode");
1321 Set_In_Present
(Node
, True);
1325 --------------------------
1326 -- 6.3 Subprogram Body --
1327 --------------------------
1329 -- Parsed by P_Subprogram (6.1)
1331 -----------------------------------
1332 -- 6.4 Procedure Call Statement --
1333 -----------------------------------
1335 -- Parsed by P_Sequence_Of_Statements (5.1)
1337 ------------------------
1338 -- 6.4 Function Call --
1339 ------------------------
1341 -- Parsed by P_Call_Or_Name (4.1)
1343 --------------------------------
1344 -- 6.4 Actual Parameter Part --
1345 --------------------------------
1347 -- Parsed by P_Call_Or_Name (4.1)
1349 --------------------------------
1350 -- 6.4 Parameter Association --
1351 --------------------------------
1353 -- Parsed by P_Call_Or_Name (4.1)
1355 ------------------------------------
1356 -- 6.4 Explicit Actual Parameter --
1357 ------------------------------------
1359 -- Parsed by P_Call_Or_Name (4.1)
1361 ---------------------------
1362 -- 6.5 Return Statement --
1363 ---------------------------
1365 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1367 -- EXTENDED_RETURN_STATEMENT ::=
1368 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1369 -- [:= EXPRESSION] [do
1370 -- HANDLED_SEQUENCE_OF_STATEMENTS
1373 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1375 -- RETURN_STATEMENT ::= return [EXPRESSION];
1377 -- Error recovery: can raise Error_Resync
1379 procedure P_Return_Subtype_Indication
(Decl_Node
: Node_Id
) is
1381 -- Note: We don't need to check Ada_Version here, because this is
1382 -- only called in >= Ada 2005 cases anyway.
1384 Not_Null_Present
: constant Boolean := P_Null_Exclusion
;
1387 Set_Null_Exclusion_Present
(Decl_Node
, Not_Null_Present
);
1389 if Token
= Tok_Access
then
1390 Set_Object_Definition
1391 (Decl_Node
, P_Access_Definition
(Not_Null_Present
));
1393 Set_Object_Definition
1394 (Decl_Node
, P_Subtype_Indication
(Not_Null_Present
));
1396 end P_Return_Subtype_Indication
;
1398 -- Error recovery: can raise Error_Resync
1400 function P_Return_Object_Declaration
return Node_Id
is
1401 Return_Obj
: Node_Id
;
1402 Decl_Node
: Node_Id
;
1405 Return_Obj
:= Token_Node
;
1406 Change_Identifier_To_Defining_Identifier
(Return_Obj
);
1407 Decl_Node
:= New_Node
(N_Object_Declaration
, Token_Ptr
);
1408 Set_Defining_Identifier
(Decl_Node
, Return_Obj
);
1410 Scan
; -- past identifier
1413 -- First an error check, if we have two identifiers in a row, a likely
1414 -- possibility is that the first of the identifiers is an incorrectly
1415 -- spelled keyword. See similar check in P_Identifier_Declarations.
1417 if Token
= Tok_Identifier
then
1419 SS
: Saved_Scan_State
;
1423 Save_Scan_State
(SS
);
1424 Scan
; -- past initial identifier
1425 I2
:= (Token
= Tok_Identifier
);
1426 Restore_Scan_State
(SS
);
1430 (Bad_Spelling_Of
(Tok_Access
) or else
1431 Bad_Spelling_Of
(Tok_Aliased
) or else
1432 Bad_Spelling_Of
(Tok_Constant
))
1439 -- We allow "constant" here (as in "return Result : constant
1440 -- T..."). This is not in the latest RM, but the ARG is considering an
1441 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1443 if Token
= Tok_Constant
then
1444 Scan
; -- past CONSTANT
1445 Set_Constant_Present
(Decl_Node
);
1447 if Token
= Tok_Aliased
then
1448 Error_Msg_SC
-- CODEFIX
1449 ("ALIASED should be before CONSTANT");
1450 Scan
; -- past ALIASED
1451 Set_Aliased_Present
(Decl_Node
);
1454 elsif Token
= Tok_Aliased
then
1455 Scan
; -- past ALIASED
1456 Set_Aliased_Present
(Decl_Node
);
1458 if Token
= Tok_Constant
then
1459 Scan
; -- past CONSTANT
1460 Set_Constant_Present
(Decl_Node
);
1464 P_Return_Subtype_Indication
(Decl_Node
);
1466 if Token
= Tok_Colon_Equal
then
1468 Set_Expression
(Decl_Node
, P_Expression_No_Right_Paren
);
1472 end P_Return_Object_Declaration
;
1474 -- Error recovery: can raise Error_Resync
1476 function P_Return_Statement
return Node_Id
is
1477 -- The caller has checked that the initial token is RETURN
1479 function Is_Simple
return Boolean;
1480 -- Scan state is just after RETURN (and is left that way).
1481 -- Determine whether this is a simple or extended return statement
1482 -- by looking ahead for "identifier :", which implies extended.
1488 function Is_Simple
return Boolean is
1489 Scan_State
: Saved_Scan_State
;
1490 Result
: Boolean := True;
1493 if Token
= Tok_Identifier
then
1494 Save_Scan_State
(Scan_State
); -- at identifier
1495 Scan
; -- past identifier
1497 if Token
= Tok_Colon
then
1498 Result
:= False; -- It's an extended_return_statement.
1501 Restore_Scan_State
(Scan_State
); -- to identifier
1507 Return_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1508 Return_Node
: Node_Id
;
1510 -- Start of processing for P_Return_Statement
1513 Scan
; -- past RETURN
1515 -- Simple_return_statement, no expression, return an
1516 -- N_Simple_Return_Statement node with the expression field left Empty.
1518 if Token
= Tok_Semicolon
then
1520 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1525 -- Simple_return_statement with expression
1527 -- We avoid trying to scan an expression if we are at an
1528 -- expression terminator since in that case the best error
1529 -- message is probably that we have a missing semicolon.
1532 Return_Node
:= New_Node
(N_Simple_Return_Statement
, Return_Sloc
);
1534 if Token
not in Token_Class_Eterm
then
1535 Set_Expression
(Return_Node
, P_Expression_No_Right_Paren
);
1538 -- Extended_return_statement (Ada 2005 only -- AI-318):
1541 if Ada_Version
< Ada_05
then
1543 (" extended_return_statement is an Ada 2005 extension");
1544 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
1547 Return_Node
:= New_Node
(N_Extended_Return_Statement
, Return_Sloc
);
1548 Set_Return_Object_Declarations
1549 (Return_Node
, New_List
(P_Return_Object_Declaration
));
1551 if Token
= Tok_Do
then
1553 Scope
.Table
(Scope
.Last
).Etyp
:= E_Return
;
1554 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
1555 Scope
.Table
(Scope
.Last
).Sloc
:= Return_Sloc
;
1558 Set_Handled_Statement_Sequence
1559 (Return_Node
, P_Handled_Sequence_Of_Statements
);
1562 -- Do we need to handle Error_Resync here???
1570 end P_Return_Statement
;