1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 pragma Style_Checks
(All_Checks
);
28 -- Turn off subprogram body ordering check. Subprograms are in order
29 -- by RM section rather than alphabetical
31 with Sinfo
.CN
; use Sinfo
.CN
;
36 -- Local subprograms, used only in this chapter
38 function P_Defining_Designator
return Node_Id
;
39 function P_Defining_Operator_Symbol
return Node_Id
;
41 procedure Check_Junk_Semicolon_Before_Return
;
43 -- Check for common error of junk semicolon before RETURN keyword of
44 -- function specification. If present, skip over it with appropriate
45 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
46 -- routine also deals with a possibly misspelled version of Return.
48 ----------------------------------------
49 -- Check_Junk_Semicolon_Before_Return --
50 ----------------------------------------
52 procedure Check_Junk_Semicolon_Before_Return
is
53 Scan_State
: Saved_Scan_State
;
56 if Token
= Tok_Semicolon
then
57 Save_Scan_State
(Scan_State
);
58 Scan
; -- past the semicolon
60 if Token
= Tok_Return
then
61 Restore_Scan_State
(Scan_State
);
62 Error_Msg_SC
("unexpected semicolon ignored");
63 Scan
; -- rescan past junk semicolon
66 Restore_Scan_State
(Scan_State
);
69 elsif Bad_Spelling_Of
(Tok_Return
) then
72 end Check_Junk_Semicolon_Before_Return
;
74 -----------------------------------------------------
75 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
76 -----------------------------------------------------
78 -- This routine scans out a subprogram declaration, subprogram body,
79 -- subprogram renaming declaration or subprogram generic instantiation.
81 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
83 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
84 -- SUBPROGRAM_SPECIFICATION is abstract;
86 -- SUBPROGRAM_SPECIFICATION ::=
87 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
88 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
90 -- PARAMETER_PROFILE ::= [FORMAL_PART]
92 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
94 -- SUBPROGRAM_BODY ::=
95 -- SUBPROGRAM_SPECIFICATION is
98 -- HANDLED_SEQUENCE_OF_STATEMENTS
101 -- SUBPROGRAM_RENAMING_DECLARATION ::=
102 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
104 -- SUBPROGRAM_BODY_STUB ::=
105 -- SUBPROGRAM_SPECIFICATION is separate;
107 -- GENERIC_INSTANTIATION ::=
108 -- procedure DEFINING_PROGRAM_UNIT_NAME is
109 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
110 -- | function DEFINING_DESIGNATOR is
111 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
113 -- NULL_PROCEDURE_DECLARATION ::=
114 -- SUBPROGRAM_SPECIFICATION is null;
116 -- Null procedures are an Ada 2005 feature. A null procedure declaration
117 -- is classified as a basic declarative item, but it is parsed here, with
118 -- other subprogram constructs.
120 -- The value in Pf_Flags indicates which of these possible declarations
121 -- is acceptable to the caller:
123 -- Pf_Flags.Decl Set if declaration OK
124 -- Pf_Flags.Gins Set if generic instantiation OK
125 -- Pf_Flags.Pbod Set if proper body OK
126 -- Pf_Flags.Rnam Set if renaming declaration OK
127 -- Pf_Flags.Stub Set if body stub OK
129 -- If an inappropriate form is encountered, it is scanned out but an
130 -- error message indicating that it is appearing in an inappropriate
131 -- context is issued. The only possible values for Pf_Flags are those
132 -- defined as constants in the Par package.
134 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
135 -- NOT or OVERRIDING.
137 -- Error recovery: cannot raise Error_Resync
139 function P_Subprogram
(Pf_Flags
: Pf_Rec
) return Node_Id
is
140 Specification_Node
: Node_Id
;
142 Fpart_List
: List_Id
;
143 Fpart_Sloc
: Source_Ptr
;
144 Result_Not_Null
: Boolean := False;
145 Result_Node
: Node_Id
;
149 Rename_Node
: Node_Id
;
150 Absdec_Node
: Node_Id
;
152 Fproc_Sloc
: Source_Ptr
;
154 Scan_State
: Saved_Scan_State
;
156 -- Flags for optional overriding indication. Two flags are needed,
157 -- to distinguish positive and negative overriding indicators from
158 -- the absence of any indicator.
160 Is_Overriding
: Boolean := False;
161 Not_Overriding
: Boolean := False;
164 -- Set up scope stack entry. Note that the Labl field will be set later
166 SIS_Entry_Active
:= False;
167 SIS_Missing_Semicolon_Message
:= No_Error_Msg
;
169 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
170 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
171 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
172 Scope
.Table
(Scope
.Last
).Lreq
:= False;
174 -- Ada2005: scan leading overriding indicator.
176 if Token
= Tok_Not
then
179 if Token
= Tok_Overriding
then
180 Scan
; -- past OVERRIDING
181 Not_Overriding
:= True;
183 Error_Msg_SC
("OVERRIDING expected!");
186 elsif Token
= Tok_Overriding
then
187 Scan
; -- past OVERRIDING
188 Is_Overriding
:= True;
191 if (Is_Overriding
or else Not_Overriding
) then
192 if Ada_Version
< Ada_05
then
193 Error_Msg_SP
(" overriding indicator is an Ada 2005 extension");
194 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
196 -- An overriding indicator is allowed for subprogram declarations,
197 -- bodies, renamings, stubs, and instantiations.
199 elsif Pf_Flags
/= Pf_Decl_Gins_Pbod_Rnam_Stub
then
200 Error_Msg_SC
("overriding indicator not allowed here!");
202 elsif Token
/= Tok_Function
203 and then Token
/= Tok_Procedure
205 Error_Msg_SC
("FUNCTION or PROCEDURE expected!");
209 Func
:= (Token
= Tok_Function
);
210 Fproc_Sloc
:= Token_Ptr
;
211 Scan
; -- past FUNCTION or PROCEDURE
216 Name_Node
:= P_Defining_Designator
;
218 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
219 and then Scope
.Last
= 1
221 Error_Msg_SP
("operator symbol not allowed at library level");
222 Name_Node
:= New_Entity
(N_Defining_Identifier
, Sloc
(Name_Node
));
224 -- Set name from file name, we need some junk name, and that's
225 -- as good as anything. This is only approximate, since we do
226 -- not do anything with non-standard name translations.
228 Get_Name_String
(File_Name
(Current_Source_File
));
230 for J
in 1 .. Name_Len
loop
231 if Name_Buffer
(J
) = '.' then
237 Set_Chars
(Name_Node
, Name_Find
);
238 Set_Error_Posted
(Name_Node
);
242 Name_Node
:= P_Defining_Program_Unit_Name
;
245 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
247 if Token
= Tok_Colon
then
248 Error_Msg_SC
("redundant colon ignored");
252 -- Deal with generic instantiation, the one case in which we do not
253 -- have a subprogram specification as part of whatever we are parsing
255 if Token
= Tok_Is
then
256 Save_Scan_State
(Scan_State
); -- at the IS
257 T_Is
; -- checks for redundant IS
259 if Token
= Tok_New
then
260 if not Pf_Flags
.Gins
then
261 Error_Msg_SC
("generic instantation not allowed here!");
267 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
268 Set_Name
(Inst_Node
, P_Function_Name
);
270 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
271 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
274 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
275 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
277 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
279 if Is_Overriding
then
280 Set_Must_Override
(Inst_Node
);
282 elsif Not_Overriding
then
283 Set_Must_Not_Override
(Inst_Node
);
289 Restore_Scan_State
(Scan_State
); -- to the IS
293 -- If not a generic instantiation, then we definitely have a subprogram
294 -- specification (all possibilities at this stage include one here)
296 Fpart_Sloc
:= Token_Ptr
;
298 Check_Misspelling_Of
(Tok_Return
);
300 -- Scan formal part. First a special error check. If we have an
301 -- identifier here, then we have a definite error. If this identifier
302 -- is on the same line as the designator, then we assume it is the
303 -- first formal after a missing left parenthesis
305 if Token
= Tok_Identifier
306 and then not Token_Is_At_Start_Of_Line
308 T_Left_Paren
; -- to generate message
309 Fpart_List
:= P_Formal_Part
;
311 -- Otherwise scan out an optional formal part in the usual manner
314 Fpart_List
:= P_Parameter_Profile
;
317 -- We treat what we have as a function specification if FUNCTION was
318 -- used, or if a RETURN is present. This gives better error recovery
319 -- since later RETURN statements will be valid in either case.
321 Check_Junk_Semicolon_Before_Return
;
322 Result_Node
:= Error
;
324 if Token
= Tok_Return
then
326 Error_Msg
("PROCEDURE should be FUNCTION", Fproc_Sloc
);
332 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
334 -- Ada 2005 (AI-318-02)
336 if Token
= Tok_Access
then
337 if Ada_Version
< Ada_05
then
339 ("anonymous access result type is an Ada 2005 extension");
340 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
343 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
346 Result_Node
:= P_Subtype_Mark
;
352 Ignore
(Tok_Right_Paren
);
358 Specification_Node
:=
359 New_Node
(N_Function_Specification
, Fproc_Sloc
);
361 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
362 Set_Result_Definition
(Specification_Node
, Result_Node
);
365 Specification_Node
:=
366 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
369 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
370 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
372 if Is_Overriding
then
373 Set_Must_Override
(Specification_Node
);
375 elsif Not_Overriding
then
376 Set_Must_Not_Override
(Specification_Node
);
379 -- Error check: barriers not allowed on protected functions/procedures
381 if Token
= Tok_When
then
383 Error_Msg_SC
("barrier not allowed on function, only on entry");
385 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
389 Discard_Junk_Node
(P_Expression
);
392 -- Deal with case of semicolon ending a subprogram declaration
394 if Token
= Tok_Semicolon
then
395 if not Pf_Flags
.Decl
then
399 Scan
; -- past semicolon
401 -- If semicolon is immediately followed by IS, then ignore the
402 -- semicolon, and go process the body.
404 if Token
= Tok_Is
then
405 Error_Msg_SP
("unexpected semicolon ignored");
406 T_Is
; -- ignroe redundant IS's
407 goto Subprogram_Body
;
409 -- If BEGIN follows in an appropriate column, we immediately
410 -- commence the error action of assuming that the previous
411 -- subprogram declaration should have been a subprogram body,
412 -- i.e. that the terminating semicolon should have been IS.
414 elsif Token
= Tok_Begin
415 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
417 Error_Msg_SP
(""";"" should be IS!");
418 goto Subprogram_Body
;
421 goto Subprogram_Declaration
;
424 -- Case of not followed by semicolon
427 -- Subprogram renaming declaration case
429 Check_Misspelling_Of
(Tok_Renames
);
431 if Token
= Tok_Renames
then
432 if not Pf_Flags
.Rnam
then
433 Error_Msg_SC
("renaming declaration not allowed here!");
437 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
438 Scan
; -- past RENAMES
439 Set_Name
(Rename_Node
, P_Name
);
440 Set_Specification
(Rename_Node
, Specification_Node
);
445 -- Case of IS following subprogram specification
447 elsif Token
= Tok_Is
then
448 T_Is
; -- ignore redundant Is's
450 if Token_Name
= Name_Abstract
then
451 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
454 -- Deal nicely with (now obsolete) use of <> in place of abstract
456 if Token
= Tok_Box
then
457 Error_Msg_SC
("ABSTRACT expected");
458 Token
:= Tok_Abstract
;
461 -- Abstract subprogram declaration case
463 if Token
= Tok_Abstract
then
465 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
466 Set_Specification
(Absdec_Node
, Specification_Node
);
467 Pop_Scope_Stack
; -- discard unneeded entry
468 Scan
; -- past ABSTRACT
472 -- Ada 2005 (AI-248): Parse a null procedure declaration
474 elsif Token
= Tok_Null
then
475 if Ada_Version
< Ada_05
then
476 Error_Msg_SP
("null procedures are an Ada 2005 extension");
477 Error_Msg_SP
("\unit must be compiled with -gnat05 switch");
483 Error_Msg_SP
("only procedures can be null");
485 Set_Null_Present
(Specification_Node
);
489 goto Subprogram_Declaration
;
491 -- Check for IS NEW with Formal_Part present and handle nicely
493 elsif Token
= Tok_New
then
495 ("formal part not allowed in instantiation", Fpart_Sloc
);
499 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
502 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
505 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
506 Set_Name
(Inst_Node
, P_Name
);
507 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
509 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
513 goto Subprogram_Body
;
516 -- Here we have a missing IS or missing semicolon, we always guess
517 -- a missing semicolon, since we are pretty good at fixing up a
518 -- semicolon which should really be an IS
521 Error_Msg_AP
("missing "";""");
522 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
523 goto Subprogram_Declaration
;
527 -- Processing for subprogram body
530 if not Pf_Flags
.Pbod
then
531 Error_Msg_SP
("subprogram body not allowed here!");
534 -- Subprogram body stub case
536 if Separate_Present
then
537 if not Pf_Flags
.Stub
then
538 Error_Msg_SC
("body stub not allowed here!");
541 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
543 ("operator symbol cannot be used as subunit name",
548 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
549 Set_Specification
(Stub_Node
, Specification_Node
);
550 Scan
; -- past SEPARATE
555 -- Subprogram body case
558 -- Here is the test for a suspicious IS (i.e. one that looks
559 -- like it might more properly be a semicolon). See separate
560 -- section discussing use of IS instead of semicolon in
563 if (Token
in Token_Class_Declk
565 Token
= Tok_Identifier
)
566 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
567 and then Scope
.Last
/= 1
569 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
570 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
574 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
575 Set_Specification
(Body_Node
, Specification_Node
);
576 Parse_Decls_Begin_End
(Body_Node
);
580 -- Processing for subprogram declaration
582 <<Subprogram_Declaration
>>
584 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
585 Set_Specification
(Decl_Node
, Specification_Node
);
587 -- If this is a context in which a subprogram body is permitted,
588 -- set active SIS entry in case (see section titled "Handling
589 -- Semicolon Used in Place of IS" in body of Parser package)
590 -- Note that SIS_Missing_Semicolon_Message is already set properly.
592 if Pf_Flags
.Pbod
then
593 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
594 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
595 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
596 SIS_Declaration_Node
:= Decl_Node
;
597 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
598 SIS_Entry_Active
:= True;
606 ---------------------------------
607 -- 6.1 Subprogram Declaration --
608 ---------------------------------
610 -- Parsed by P_Subprogram (6.1)
612 ------------------------------------------
613 -- 6.1 Abstract Subprogram Declaration --
614 ------------------------------------------
616 -- Parsed by P_Subprogram (6.1)
618 -----------------------------------
619 -- 6.1 Subprogram Specification --
620 -----------------------------------
622 -- SUBPROGRAM_SPECIFICATION ::=
623 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
624 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
626 -- PARAMETER_PROFILE ::= [FORMAL_PART]
628 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
630 -- Subprogram specifications that appear in subprogram declarations
631 -- are parsed by P_Subprogram (6.1). This routine is used in other
632 -- contexts where subprogram specifications occur.
634 -- Note: this routine does not affect the scope stack in any way
636 -- Error recovery: can raise Error_Resync
638 function P_Subprogram_Specification
return Node_Id
is
639 Specification_Node
: Node_Id
;
640 Result_Not_Null
: Boolean;
641 Result_Node
: Node_Id
;
644 if Token
= Tok_Function
then
645 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
646 Scan
; -- past FUNCTION
648 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
649 Set_Parameter_Specifications
650 (Specification_Node
, P_Parameter_Profile
);
651 Check_Junk_Semicolon_Before_Return
;
654 Result_Not_Null
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
656 -- Ada 2005 (AI-318-02)
658 if Token
= Tok_Access
then
659 if Ada_Version
< Ada_05
then
661 ("anonymous access result type is an Ada 2005 extension");
662 Error_Msg_SC
("\unit must be compiled with -gnat05 switch");
665 Result_Node
:= P_Access_Definition
(Result_Not_Null
);
668 Result_Node
:= P_Subtype_Mark
;
672 Set_Null_Exclusion_Present
(Specification_Node
, Result_Not_Null
);
673 Set_Result_Definition
(Specification_Node
, Result_Node
);
674 return Specification_Node
;
676 elsif Token
= Tok_Procedure
then
677 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
678 Scan
; -- past PROCEDURE
680 Set_Defining_Unit_Name
681 (Specification_Node
, P_Defining_Program_Unit_Name
);
682 Set_Parameter_Specifications
683 (Specification_Node
, P_Parameter_Profile
);
684 return Specification_Node
;
687 Error_Msg_SC
("subprogram specification expected");
690 end P_Subprogram_Specification
;
692 ---------------------
694 ---------------------
697 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
699 -- The caller has checked that the initial token is an identifier,
700 -- operator symbol, or string literal. Note that we don't bother to
701 -- do much error diagnosis in this routine, since it is only used for
702 -- the label on END lines, and the routines in package Par.Endh will
703 -- check that the label is appropriate.
705 -- Error recovery: cannot raise Error_Resync
707 function P_Designator
return Node_Id
is
708 Ident_Node
: Node_Id
;
710 Prefix_Node
: Node_Id
;
712 function Real_Dot
return Boolean;
713 -- Tests if a current token is an interesting period, i.e. is followed
714 -- by an identifier or operator symbol or string literal. If not, it is
715 -- probably just incorrect punctuation to be caught by our caller. Note
716 -- that the case of an operator symbol or string literal is also an
717 -- error, but that is an error that we catch here. If the result is
718 -- True, a real dot has been scanned and we are positioned past it,
719 -- if the result is False, the scan position is unchanged.
725 function Real_Dot
return Boolean is
726 Scan_State
: Saved_Scan_State
;
729 if Token
/= Tok_Dot
then
733 Save_Scan_State
(Scan_State
);
736 if Token
= Tok_Identifier
737 or else Token
= Tok_Operator_Symbol
738 or else Token
= Tok_String_Literal
743 Restore_Scan_State
(Scan_State
);
749 -- Start of processing for P_Designator
752 Ident_Node
:= Token_Node
;
753 Scan
; -- past initial token
755 if Prev_Token
= Tok_Operator_Symbol
756 or else Prev_Token
= Tok_String_Literal
764 Prefix_Node
:= Ident_Node
;
766 -- Loop through child names, on entry to this loop, Prefix contains
767 -- the name scanned so far, and Ident_Node is the last identifier.
770 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
771 Set_Prefix
(Name_Node
, Prefix_Node
);
772 Ident_Node
:= P_Identifier
;
773 Set_Selector_Name
(Name_Node
, Ident_Node
);
774 Prefix_Node
:= Name_Node
;
775 exit when not Real_Dot
;
778 -- On exit from the loop, Ident_Node is the last identifier scanned,
779 -- i.e. the defining identifier, and Prefix_Node is a node for the
780 -- entire name, structured (incorrectly!) as a selected component.
782 Name_Node
:= Prefix
(Prefix_Node
);
783 Change_Node
(Prefix_Node
, N_Designator
);
784 Set_Name
(Prefix_Node
, Name_Node
);
785 Set_Identifier
(Prefix_Node
, Ident_Node
);
791 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
798 ------------------------------
799 -- 6.1 Defining Designator --
800 ------------------------------
802 -- DEFINING_DESIGNATOR ::=
803 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
805 -- Error recovery: cannot raise Error_Resync
807 function P_Defining_Designator
return Node_Id
is
809 if Token
= Tok_Operator_Symbol
then
810 return P_Defining_Operator_Symbol
;
812 elsif Token
= Tok_String_Literal
then
813 Error_Msg_SC
("invalid operator name");
814 Scan
; -- past junk string
818 return P_Defining_Program_Unit_Name
;
820 end P_Defining_Designator
;
822 -------------------------------------
823 -- 6.1 Defining Program Unit Name --
824 -------------------------------------
826 -- DEFINING_PROGRAM_UNIT_NAME ::=
827 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
829 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
831 -- Error recovery: cannot raise Error_Resync
833 function P_Defining_Program_Unit_Name
return Node_Id
is
834 Ident_Node
: Node_Id
;
836 Prefix_Node
: Node_Id
;
839 -- Set identifier casing if not already set and scan initial identifier
841 if Token
= Tok_Identifier
842 and then Identifier_Casing
(Current_Source_File
) = Unknown
844 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
847 Ident_Node
:= P_Identifier
(C_Dot
);
848 Merge_Identifier
(Ident_Node
, Tok_Return
);
850 -- Normal case (not child library unit name)
852 if Token
/= Tok_Dot
then
853 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
856 -- Child library unit name case
859 if Scope
.Last
> 1 then
860 Error_Msg_SP
("child unit allowed only at library level");
863 elsif Ada_Version
= Ada_83
then
864 Error_Msg_SP
("(Ada 83) child unit not allowed!");
868 Prefix_Node
:= Ident_Node
;
870 -- Loop through child names, on entry to this loop, Prefix contains
871 -- the name scanned so far, and Ident_Node is the last identifier.
874 exit when Token
/= Tok_Dot
;
875 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
877 Set_Prefix
(Name_Node
, Prefix_Node
);
878 Ident_Node
:= P_Identifier
(C_Dot
);
879 Set_Selector_Name
(Name_Node
, Ident_Node
);
880 Prefix_Node
:= Name_Node
;
883 -- On exit from the loop, Ident_Node is the last identifier scanned,
884 -- i.e. the defining identifier, and Prefix_Node is a node for the
885 -- entire name, structured (incorrectly!) as a selected component.
887 Name_Node
:= Prefix
(Prefix_Node
);
888 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
889 Set_Name
(Prefix_Node
, Name_Node
);
890 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
891 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
893 -- All set with unit name parsed
900 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
905 end P_Defining_Program_Unit_Name
;
907 --------------------------
908 -- 6.1 Operator Symbol --
909 --------------------------
911 -- OPERATOR_SYMBOL ::= STRING_LITERAL
913 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
915 -----------------------------------
916 -- 6.1 Defining Operator Symbol --
917 -----------------------------------
919 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
921 -- The caller has checked that the initial symbol is an operator symbol
923 function P_Defining_Operator_Symbol
return Node_Id
is
927 Op_Node
:= Token_Node
;
928 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
929 Scan
; -- past operator symbol
931 end P_Defining_Operator_Symbol
;
933 ----------------------------
934 -- 6.1 Parameter_Profile --
935 ----------------------------
937 -- PARAMETER_PROFILE ::= [FORMAL_PART]
939 -- Empty is returned if no formal part is present
941 -- Error recovery: cannot raise Error_Resync
943 function P_Parameter_Profile
return List_Id
is
945 if Token
= Tok_Left_Paren
then
946 Scan
; -- part left paren
947 return P_Formal_Part
;
951 end P_Parameter_Profile
;
953 ---------------------------------------
954 -- 6.1 Parameter And Result Profile --
955 ---------------------------------------
957 -- Parsed by its parent construct, which uses P_Parameter_Profile to
958 -- parse the parameters, and P_Subtype_Mark to parse the return type.
960 ----------------------
961 -- 6.1 Formal part --
962 ----------------------
964 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
966 -- PARAMETER_SPECIFICATION ::=
967 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
968 -- [:= DEFAULT_EXPRESSION]
969 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
970 -- [:= DEFAULT_EXPRESSION]
972 -- This scans the construct Formal_Part. The caller has already checked
973 -- that the initial token is a left parenthesis, and skipped past it, so
974 -- that on entry Token is the first token following the left parenthesis.
976 -- Error recovery: cannot raise Error_Resync
978 function P_Formal_Part
return List_Id
is
979 Specification_List
: List_Id
;
980 Specification_Node
: Node_Id
;
981 Scan_State
: Saved_Scan_State
;
984 Ident_Sloc
: Source_Ptr
;
985 Not_Null_Present
: Boolean := False;
987 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
988 -- This array holds the list of defining identifiers. The upper bound
989 -- of 4096 is intended to be essentially infinite, and we do not even
990 -- bother to check for it being exceeded.
993 Specification_List
:= New_List
;
994 Specification_Loop
: loop
996 if Token
= Tok_Pragma
then
1000 Ignore
(Tok_Left_Paren
);
1001 Ident_Sloc
:= Token_Ptr
;
1002 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1006 exit Ident_Loop
when Token
= Tok_Colon
;
1008 -- The only valid tokens are colon and comma, so if we have
1009 -- neither do a bit of investigation to see which is the
1010 -- better choice for insertion.
1012 if Token
/= Tok_Comma
then
1014 -- Assume colon if IN or OUT keyword found
1016 exit Ident_Loop
when Token
= Tok_In
or else Token
= Tok_Out
;
1018 -- Otherwise scan ahead
1020 Save_Scan_State
(Scan_State
);
1023 -- If we run into a semicolon, then assume that a
1024 -- colon was missing, e.g. Parms (X Y; ...). Also
1025 -- assume missing colon on EOF (a real disaster!)
1026 -- and on a right paren, e.g. Parms (X Y), and also
1027 -- on an assignment symbol, e.g. Parms (X Y := ..)
1029 if Token
= Tok_Semicolon
1030 or else Token
= Tok_Right_Paren
1031 or else Token
= Tok_EOF
1032 or else Token
= Tok_Colon_Equal
1034 Restore_Scan_State
(Scan_State
);
1037 -- If we run into a colon, assume that we had a missing
1038 -- comma, e.g. Parms (A B : ...). Also assume a missing
1039 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1041 elsif Token
= Tok_Colon
1042 or else Token
= Tok_Comma
1044 Restore_Scan_State
(Scan_State
);
1049 end loop Look_Ahead
;
1052 -- Here if a comma is present, or to be assumed
1055 Num_Idents
:= Num_Idents
+ 1;
1056 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1057 end loop Ident_Loop
;
1059 -- Fall through the loop on encountering a colon, or deciding
1060 -- that there is a missing colon.
1064 -- If there are multiple identifiers, we repeatedly scan the
1065 -- type and initialization expression information by resetting
1066 -- the scan pointer (so that we get completely separate trees
1067 -- for each occurrence).
1069 if Num_Idents
> 1 then
1070 Save_Scan_State
(Scan_State
);
1073 -- Loop through defining identifiers in list
1077 Ident_List_Loop
: loop
1078 Specification_Node
:=
1079 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
1080 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
1081 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1083 if Token
= Tok_Access
then
1084 Set_Null_Exclusion_Present
1085 (Specification_Node
, Not_Null_Present
);
1087 if Ada_Version
= Ada_83
then
1088 Error_Msg_SC
("(Ada 83) access parameters not allowed");
1091 Set_Parameter_Type
(Specification_Node
,
1092 P_Access_Definition
(Not_Null_Present
));
1095 if Token
= Tok_In
or else Token
= Tok_Out
then
1096 if Not_Null_Present
then
1098 ("ACCESS must be placed after the parameter mode");
1101 P_Mode
(Specification_Node
);
1102 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
1105 Set_Null_Exclusion_Present
1106 (Specification_Node
, Not_Null_Present
);
1108 if Token
= Tok_Procedure
1110 Token
= Tok_Function
1112 Error_Msg_SC
("formal subprogram parameter not allowed");
1115 if Token
= Tok_Left_Paren
then
1116 Discard_Junk_List
(P_Formal_Part
);
1119 if Token
= Tok_Return
then
1121 Discard_Junk_Node
(P_Subtype_Mark
);
1124 Set_Parameter_Type
(Specification_Node
, Error
);
1127 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1132 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1135 Set_Prev_Ids
(Specification_Node
, True);
1138 if Ident
< Num_Idents
then
1139 Set_More_Ids
(Specification_Node
, True);
1142 Append
(Specification_Node
, Specification_List
);
1143 exit Ident_List_Loop
when Ident
= Num_Idents
;
1145 Restore_Scan_State
(Scan_State
);
1146 end loop Ident_List_Loop
;
1149 when Error_Resync
=>
1150 Resync_Semicolon_List
;
1153 if Token
= Tok_Semicolon
then
1154 Save_Scan_State
(Scan_State
);
1155 Scan
; -- past semicolon
1157 -- If we have RETURN or IS after the semicolon, then assume
1158 -- that semicolon should have been a right parenthesis and exit
1160 if Token
= Tok_Is
or else Token
= Tok_Return
then
1161 Error_Msg_SP
("expected "")"" in place of "";""");
1162 exit Specification_Loop
;
1165 -- If we have a declaration keyword after the semicolon, then
1166 -- assume we had a missing right parenthesis and terminate list
1168 if Token
in Token_Class_Declk
then
1169 Error_Msg_AP
("missing "")""");
1170 Restore_Scan_State
(Scan_State
);
1171 exit Specification_Loop
;
1174 elsif Token
= Tok_Right_Paren
then
1175 Scan
; -- past right paren
1176 exit Specification_Loop
;
1178 -- Special check for common error of using comma instead of semicolon
1180 elsif Token
= Tok_Comma
then
1184 -- Special check for omitted separator
1186 elsif Token
= Tok_Identifier
then
1189 -- If nothing sensible, skip to next semicolon or right paren
1193 Resync_Semicolon_List
;
1195 if Token
= Tok_Semicolon
then
1196 Scan
; -- past semicolon
1199 exit Specification_Loop
;
1202 end loop Specification_Loop
;
1204 return Specification_List
;
1207 ----------------------------------
1208 -- 6.1 Parameter Specification --
1209 ----------------------------------
1211 -- Parsed by P_Formal_Part (6.1)
1217 -- MODE ::= [in] | in out | out
1219 -- There is no explicit node in the tree for the Mode. Instead the
1220 -- In_Present and Out_Present flags are set in the parent node to
1221 -- record the presence of keywords specifying the mode.
1223 -- Error_Recovery: cannot raise Error_Resync
1225 procedure P_Mode
(Node
: Node_Id
) is
1227 if Token
= Tok_In
then
1229 Set_In_Present
(Node
, True);
1232 if Token
= Tok_Out
then
1234 Set_Out_Present
(Node
, True);
1237 if Token
= Tok_In
then
1238 Error_Msg_SC
("IN must preceed OUT in parameter mode");
1240 Set_In_Present
(Node
, True);
1244 --------------------------
1245 -- 6.3 Subprogram Body --
1246 --------------------------
1248 -- Parsed by P_Subprogram (6.1)
1250 -----------------------------------
1251 -- 6.4 Procedure Call Statement --
1252 -----------------------------------
1254 -- Parsed by P_Sequence_Of_Statements (5.1)
1256 ------------------------
1257 -- 6.4 Function Call --
1258 ------------------------
1260 -- Parsed by P_Call_Or_Name (4.1)
1262 --------------------------------
1263 -- 6.4 Actual Parameter Part --
1264 --------------------------------
1266 -- Parsed by P_Call_Or_Name (4.1)
1268 --------------------------------
1269 -- 6.4 Parameter Association --
1270 --------------------------------
1272 -- Parsed by P_Call_Or_Name (4.1)
1274 ------------------------------------
1275 -- 6.4 Explicit Actual Parameter --
1276 ------------------------------------
1278 -- Parsed by P_Call_Or_Name (4.1)
1280 ---------------------------
1281 -- 6.5 Return Statement --
1282 ---------------------------
1284 -- RETURN_STATEMENT ::= return [EXPRESSION];
1286 -- The caller has checked that the initial token is RETURN
1288 -- Error recovery: can raise Error_Resync
1290 function P_Return_Statement
return Node_Id
is
1291 Return_Node
: Node_Id
;
1294 Return_Node
:= New_Node
(N_Return_Statement
, Token_Ptr
);
1296 -- Sloc points to RETURN
1299 Scan
; -- past RETURN
1301 if Token
/= Tok_Semicolon
then
1303 -- If no semicolon, then scan an expression, except that
1304 -- we avoid trying to scan an expression if we are at an
1305 -- expression terminator since in that case the best error
1306 -- message is probably that we have a missing semicolon.
1308 if Token
not in Token_Class_Eterm
then
1309 Set_Expression
(Return_Node
, P_Expression_No_Right_Paren
);
1315 end P_Return_Statement
;