1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
;
42 -- Check for common error of junk semicolon before RETURN keyword of
43 -- function specification. If present, skip over it with appropriate
44 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
45 -- routine also deals with a possibly misspelled version of Return.
47 ----------------------------------------
48 -- Check_Junk_Semicolon_Before_Return --
49 ----------------------------------------
51 procedure Check_Junk_Semicolon_Before_Return
is
52 Scan_State
: Saved_Scan_State
;
55 if Token
= Tok_Semicolon
then
56 Save_Scan_State
(Scan_State
);
57 Scan
; -- past the semicolon
59 if Token
= Tok_Return
then
60 Restore_Scan_State
(Scan_State
);
61 Error_Msg_SC
("Unexpected semicolon ignored");
62 Scan
; -- rescan past junk semicolon
65 Restore_Scan_State
(Scan_State
);
68 elsif Bad_Spelling_Of
(Tok_Return
) then
71 end Check_Junk_Semicolon_Before_Return
;
73 -----------------------------------------------------
74 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
75 -----------------------------------------------------
77 -- This routine scans out a subprogram declaration, subprogram body,
78 -- subprogram renaming declaration or subprogram generic instantiation.
80 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
82 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
83 -- SUBPROGRAM_SPECIFICATION is abstract;
85 -- SUBPROGRAM_SPECIFICATION ::=
86 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
87 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
89 -- PARAMETER_PROFILE ::= [FORMAL_PART]
91 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
93 -- SUBPROGRAM_BODY ::=
94 -- SUBPROGRAM_SPECIFICATION is
97 -- HANDLED_SEQUENCE_OF_STATEMENTS
100 -- SUBPROGRAM_RENAMING_DECLARATION ::=
101 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
103 -- SUBPROGRAM_BODY_STUB ::=
104 -- SUBPROGRAM_SPECIFICATION is separate;
106 -- GENERIC_INSTANTIATION ::=
107 -- procedure DEFINING_PROGRAM_UNIT_NAME is
108 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
109 -- | function DEFINING_DESIGNATOR is
110 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
112 -- The value in Pf_Flags indicates which of these possible declarations
113 -- is acceptable to the caller:
115 -- Pf_Flags.Decl Set if declaration OK
116 -- Pf_Flags.Gins Set if generic instantiation OK
117 -- Pf_Flags.Pbod Set if proper body OK
118 -- Pf_Flags.Rnam Set if renaming declaration OK
119 -- Pf_Flags.Stub Set if body stub OK
121 -- If an inappropriate form is encountered, it is scanned out but an
122 -- error message indicating that it is appearing in an inappropriate
123 -- context is issued. The only possible values for Pf_Flags are those
124 -- defined as constants in the Par package.
126 -- The caller has checked that the initial token is FUNCTION or PROCEDURE
128 -- Error recovery: cannot raise Error_Resync
130 function P_Subprogram
(Pf_Flags
: Pf_Rec
) return Node_Id
is
131 Specification_Node
: Node_Id
;
133 Fpart_List
: List_Id
;
134 Fpart_Sloc
: Source_Ptr
;
135 Return_Node
: Node_Id
;
139 Rename_Node
: Node_Id
;
140 Absdec_Node
: Node_Id
;
142 Fproc_Sloc
: Source_Ptr
;
144 Scan_State
: Saved_Scan_State
;
147 -- Set up scope stack entry. Note that the Labl field will be set later
149 SIS_Entry_Active
:= False;
150 SIS_Missing_Semicolon_Message
:= No_Error_Msg
;
152 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
153 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
154 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
155 Scope
.Table
(Scope
.Last
).Lreq
:= False;
157 Func
:= (Token
= Tok_Function
);
158 Fproc_Sloc
:= Token_Ptr
;
159 Scan
; -- past FUNCTION or PROCEDURE
164 Name_Node
:= P_Defining_Designator
;
166 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
167 and then Scope
.Last
= 1
169 Error_Msg_SP
("operator symbol not allowed at library level");
170 Name_Node
:= New_Entity
(N_Defining_Identifier
, Sloc
(Name_Node
));
172 -- Set name from file name, we need some junk name, and that's
173 -- as good as anything. This is only approximate, since we do
174 -- not do anything with non-standard name translations.
176 Get_Name_String
(File_Name
(Current_Source_File
));
178 for J
in 1 .. Name_Len
loop
179 if Name_Buffer
(J
) = '.' then
185 Set_Chars
(Name_Node
, Name_Find
);
186 Set_Error_Posted
(Name_Node
);
190 Name_Node
:= P_Defining_Program_Unit_Name
;
193 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
195 if Token
= Tok_Colon
then
196 Error_Msg_SC
("redundant colon ignored");
200 -- Deal with generic instantiation, the one case in which we do not
201 -- have a subprogram specification as part of whatever we are parsing
203 if Token
= Tok_Is
then
204 Save_Scan_State
(Scan_State
); -- at the IS
205 T_Is
; -- checks for redundant IS's
207 if Token
= Tok_New
then
208 if not Pf_Flags
.Gins
then
209 Error_Msg_SC
("generic instantation not allowed here!");
215 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
216 Set_Name
(Inst_Node
, P_Function_Name
);
218 Inst_Node
:= New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
219 Set_Name
(Inst_Node
, P_Qualified_Simple_Name
);
222 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
223 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
225 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
229 Restore_Scan_State
(Scan_State
); -- to the IS
233 -- If not a generic instantiation, then we definitely have a subprogram
234 -- specification (all possibilities at this stage include one here)
236 Fpart_Sloc
:= Token_Ptr
;
238 Check_Misspelling_Of
(Tok_Return
);
240 -- Scan formal part. First a special error check. If we have an
241 -- identifier here, then we have a definite error. If this identifier
242 -- is on the same line as the designator, then we assume it is the
243 -- first formal after a missing left parenthesis
245 if Token
= Tok_Identifier
246 and then not Token_Is_At_Start_Of_Line
248 T_Left_Paren
; -- to generate message
249 Fpart_List
:= P_Formal_Part
;
251 -- Otherwise scan out an optional formal part in the usual manner
254 Fpart_List
:= P_Parameter_Profile
;
257 -- We treat what we have as a function specification if FUNCTION was
258 -- used, or if a RETURN is present. This gives better error recovery
259 -- since later RETURN statements will be valid in either case.
261 Check_Junk_Semicolon_Before_Return
;
262 Return_Node
:= Error
;
264 if Token
= Tok_Return
then
266 Error_Msg
("PROCEDURE should be FUNCTION", Fproc_Sloc
);
271 Return_Node
:= P_Subtype_Mark
;
276 Ignore
(Tok_Right_Paren
);
282 Specification_Node
:=
283 New_Node
(N_Function_Specification
, Fproc_Sloc
);
284 Set_Subtype_Mark
(Specification_Node
, Return_Node
);
287 Specification_Node
:=
288 New_Node
(N_Procedure_Specification
, Fproc_Sloc
);
291 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
292 Set_Parameter_Specifications
(Specification_Node
, Fpart_List
);
294 -- Error check: barriers not allowed on protected functions/procedures
296 if Token
= Tok_When
then
298 Error_Msg_SC
("barrier not allowed on function, only on entry");
300 Error_Msg_SC
("barrier not allowed on procedure, only on entry");
304 Discard_Junk_Node
(P_Expression
);
307 -- Deal with case of semicolon ending a subprogram declaration
309 if Token
= Tok_Semicolon
then
310 if not Pf_Flags
.Decl
then
314 Scan
; -- past semicolon
316 -- If semicolon is immediately followed by IS, then ignore the
317 -- semicolon, and go process the body.
319 if Token
= Tok_Is
then
320 Error_Msg_SP
("unexpected semicolon ignored");
321 T_Is
; -- ignroe redundant IS's
322 goto Subprogram_Body
;
324 -- If BEGIN follows in an appropriate column, we immediately
325 -- commence the error action of assuming that the previous
326 -- subprogram declaration should have been a subprogram body,
327 -- i.e. that the terminating semicolon should have been IS.
329 elsif Token
= Tok_Begin
330 and then Start_Column
>= Scope
.Table
(Scope
.Last
).Ecol
332 Error_Msg_SP
(""";"" should be IS!");
333 goto Subprogram_Body
;
336 goto Subprogram_Declaration
;
339 -- Case of not followed by semicolon
342 -- Subprogram renaming declaration case
344 Check_Misspelling_Of
(Tok_Renames
);
346 if Token
= Tok_Renames
then
347 if not Pf_Flags
.Rnam
then
348 Error_Msg_SC
("renaming declaration not allowed here!");
352 New_Node
(N_Subprogram_Renaming_Declaration
, Token_Ptr
);
353 Scan
; -- past RENAMES
354 Set_Name
(Rename_Node
, P_Name
);
355 Set_Specification
(Rename_Node
, Specification_Node
);
360 -- Case of IS following subprogram specification
362 elsif Token
= Tok_Is
then
363 T_Is
; -- ignore redundant Is's
365 if Token_Name
= Name_Abstract
then
366 Check_95_Keyword
(Tok_Abstract
, Tok_Semicolon
);
369 -- Deal nicely with (now obsolete) use of <> in place of abstract
371 if Token
= Tok_Box
then
372 Error_Msg_SC
("ABSTRACT expected");
373 Token
:= Tok_Abstract
;
376 -- Abstract subprogram declaration case
378 if Token
= Tok_Abstract
then
380 New_Node
(N_Abstract_Subprogram_Declaration
, Token_Ptr
);
381 Set_Specification
(Absdec_Node
, Specification_Node
);
382 Pop_Scope_Stack
; -- discard unneeded entry
383 Scan
; -- past ABSTRACT
387 -- Check for IS NEW with Formal_Part present and handle nicely
389 elsif Token
= Tok_New
then
391 ("formal part not allowed in instantiation", Fpart_Sloc
);
395 Inst_Node
:= New_Node
(N_Function_Instantiation
, Fproc_Sloc
);
398 New_Node
(N_Procedure_Instantiation
, Fproc_Sloc
);
401 Set_Defining_Unit_Name
(Inst_Node
, Name_Node
);
402 Set_Name
(Inst_Node
, P_Name
);
403 Set_Generic_Associations
(Inst_Node
, P_Generic_Actual_Part_Opt
);
405 Pop_Scope_Stack
; -- Don't need scope stack entry in this case
409 goto Subprogram_Body
;
412 -- Here we have a missing IS or missing semicolon, we always guess
413 -- a missing semicolon, since we are pretty good at fixing up a
414 -- semicolon which should really be an IS
417 Error_Msg_AP
("missing "";""");
418 SIS_Missing_Semicolon_Message
:= Get_Msg_Id
;
419 goto Subprogram_Declaration
;
423 -- Processing for subprogram body
426 if not Pf_Flags
.Pbod
then
427 Error_Msg_SP
("subprogram body not allowed here!");
430 -- Subprogram body stub case
432 if Separate_Present
then
433 if not Pf_Flags
.Stub
then
434 Error_Msg_SC
("body stub not allowed here!");
437 if Nkind
(Name_Node
) = N_Defining_Operator_Symbol
then
439 ("operator symbol cannot be used as subunit name",
444 New_Node
(N_Subprogram_Body_Stub
, Sloc
(Specification_Node
));
445 Set_Specification
(Stub_Node
, Specification_Node
);
446 Scan
; -- past SEPARATE
451 -- Subprogram body case
454 -- Here is the test for a suspicious IS (i.e. one that looks
455 -- like it might more properly be a semicolon). See separate
456 -- section discussing use of IS instead of semicolon in
459 if (Token
in Token_Class_Declk
461 Token
= Tok_Identifier
)
462 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
463 and then Scope
.Last
/= 1
465 Scope
.Table
(Scope
.Last
).Etyp
:= E_Suspicious_Is
;
466 Scope
.Table
(Scope
.Last
).S_Is
:= Prev_Token_Ptr
;
470 New_Node
(N_Subprogram_Body
, Sloc
(Specification_Node
));
471 Set_Specification
(Body_Node
, Specification_Node
);
472 Parse_Decls_Begin_End
(Body_Node
);
476 -- Processing for subprogram declaration
478 <<Subprogram_Declaration
>>
480 New_Node
(N_Subprogram_Declaration
, Sloc
(Specification_Node
));
481 Set_Specification
(Decl_Node
, Specification_Node
);
483 -- If this is a context in which a subprogram body is permitted,
484 -- set active SIS entry in case (see section titled "Handling
485 -- Semicolon Used in Place of IS" in body of Parser package)
486 -- Note that SIS_Missing_Semicolon_Message is already set properly.
488 if Pf_Flags
.Pbod
then
489 SIS_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
490 SIS_Sloc
:= Scope
.Table
(Scope
.Last
).Sloc
;
491 SIS_Ecol
:= Scope
.Table
(Scope
.Last
).Ecol
;
492 SIS_Declaration_Node
:= Decl_Node
;
493 SIS_Semicolon_Sloc
:= Prev_Token_Ptr
;
494 SIS_Entry_Active
:= True;
502 ---------------------------------
503 -- 6.1 Subprogram Declaration --
504 ---------------------------------
506 -- Parsed by P_Subprogram (6.1)
508 ------------------------------------------
509 -- 6.1 Abstract Subprogram Declaration --
510 ------------------------------------------
512 -- Parsed by P_Subprogram (6.1)
514 -----------------------------------
515 -- 6.1 Subprogram Specification --
516 -----------------------------------
518 -- SUBPROGRAM_SPECIFICATION ::=
519 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
520 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
522 -- PARAMETER_PROFILE ::= [FORMAL_PART]
524 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
526 -- Subprogram specifications that appear in subprogram declarations
527 -- are parsed by P_Subprogram (6.1). This routine is used in other
528 -- contexts where subprogram specifications occur.
530 -- Note: this routine does not affect the scope stack in any way
532 -- Error recovery: can raise Error_Resync
534 function P_Subprogram_Specification
return Node_Id
is
535 Specification_Node
: Node_Id
;
538 if Token
= Tok_Function
then
539 Specification_Node
:= New_Node
(N_Function_Specification
, Token_Ptr
);
540 Scan
; -- past FUNCTION
542 Set_Defining_Unit_Name
(Specification_Node
, P_Defining_Designator
);
543 Set_Parameter_Specifications
544 (Specification_Node
, P_Parameter_Profile
);
545 Check_Junk_Semicolon_Before_Return
;
547 Set_Subtype_Mark
(Specification_Node
, P_Subtype_Mark
);
549 return Specification_Node
;
551 elsif Token
= Tok_Procedure
then
552 Specification_Node
:= New_Node
(N_Procedure_Specification
, Token_Ptr
);
553 Scan
; -- past PROCEDURE
555 Set_Defining_Unit_Name
556 (Specification_Node
, P_Defining_Program_Unit_Name
);
557 Set_Parameter_Specifications
558 (Specification_Node
, P_Parameter_Profile
);
559 return Specification_Node
;
562 Error_Msg_SC
("subprogram specification expected");
565 end P_Subprogram_Specification
;
567 ---------------------
569 ---------------------
572 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
574 -- The caller has checked that the initial token is an identifier,
575 -- operator symbol, or string literal. Note that we don't bother to
576 -- do much error diagnosis in this routine, since it is only used for
577 -- the label on END lines, and the routines in package Par.Endh will
578 -- check that the label is appropriate.
580 -- Error recovery: cannot raise Error_Resync
582 function P_Designator
return Node_Id
is
583 Ident_Node
: Node_Id
;
585 Prefix_Node
: Node_Id
;
587 function Real_Dot
return Boolean;
588 -- Tests if a current token is an interesting period, i.e. is followed
589 -- by an identifier or operator symbol or string literal. If not, it is
590 -- probably just incorrect punctuation to be caught by our caller. Note
591 -- that the case of an operator symbol or string literal is also an
592 -- error, but that is an error that we catch here. If the result is
593 -- True, a real dot has been scanned and we are positioned past it,
594 -- if the result is False, the scan position is unchanged.
600 function Real_Dot
return Boolean is
601 Scan_State
: Saved_Scan_State
;
604 if Token
/= Tok_Dot
then
608 Save_Scan_State
(Scan_State
);
611 if Token
= Tok_Identifier
612 or else Token
= Tok_Operator_Symbol
613 or else Token
= Tok_String_Literal
618 Restore_Scan_State
(Scan_State
);
624 -- Start of processing for P_Designator
627 Ident_Node
:= Token_Node
;
628 Scan
; -- past initial token
630 if Prev_Token
= Tok_Operator_Symbol
631 or else Prev_Token
= Tok_String_Literal
639 Prefix_Node
:= Ident_Node
;
641 -- Loop through child names, on entry to this loop, Prefix contains
642 -- the name scanned so far, and Ident_Node is the last identifier.
645 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
646 Set_Prefix
(Name_Node
, Prefix_Node
);
647 Ident_Node
:= P_Identifier
;
648 Set_Selector_Name
(Name_Node
, Ident_Node
);
649 Prefix_Node
:= Name_Node
;
650 exit when not Real_Dot
;
653 -- On exit from the loop, Ident_Node is the last identifier scanned,
654 -- i.e. the defining identifier, and Prefix_Node is a node for the
655 -- entire name, structured (incorrectly!) as a selected component.
657 Name_Node
:= Prefix
(Prefix_Node
);
658 Change_Node
(Prefix_Node
, N_Designator
);
659 Set_Name
(Prefix_Node
, Name_Node
);
660 Set_Identifier
(Prefix_Node
, Ident_Node
);
666 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
673 ------------------------------
674 -- 6.1 Defining Designator --
675 ------------------------------
677 -- DEFINING_DESIGNATOR ::=
678 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
680 -- Error recovery: cannot raise Error_Resync
682 function P_Defining_Designator
return Node_Id
is
684 if Token
= Tok_Operator_Symbol
then
685 return P_Defining_Operator_Symbol
;
687 elsif Token
= Tok_String_Literal
then
688 Error_Msg_SC
("invalid operator name");
689 Scan
; -- past junk string
693 return P_Defining_Program_Unit_Name
;
695 end P_Defining_Designator
;
697 -------------------------------------
698 -- 6.1 Defining Program Unit Name --
699 -------------------------------------
701 -- DEFINING_PROGRAM_UNIT_NAME ::=
702 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
704 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
706 -- Error recovery: cannot raise Error_Resync
708 function P_Defining_Program_Unit_Name
return Node_Id
is
709 Ident_Node
: Node_Id
;
711 Prefix_Node
: Node_Id
;
714 -- Set identifier casing if not already set and scan initial identifier
716 if Token
= Tok_Identifier
717 and then Identifier_Casing
(Current_Source_File
) = Unknown
719 Set_Identifier_Casing
(Current_Source_File
, Determine_Token_Casing
);
722 Ident_Node
:= P_Identifier
(C_Dot
);
723 Merge_Identifier
(Ident_Node
, Tok_Return
);
725 -- Normal case (not child library unit name)
727 if Token
/= Tok_Dot
then
728 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
731 -- Child library unit name case
734 if Scope
.Last
> 1 then
735 Error_Msg_SP
("child unit allowed only at library level");
738 elsif Ada_Version
= Ada_83
then
739 Error_Msg_SP
("(Ada 83) child unit not allowed!");
743 Prefix_Node
:= Ident_Node
;
745 -- Loop through child names, on entry to this loop, Prefix contains
746 -- the name scanned so far, and Ident_Node is the last identifier.
749 exit when Token
/= Tok_Dot
;
750 Name_Node
:= New_Node
(N_Selected_Component
, Token_Ptr
);
752 Set_Prefix
(Name_Node
, Prefix_Node
);
753 Ident_Node
:= P_Identifier
(C_Dot
);
754 Set_Selector_Name
(Name_Node
, Ident_Node
);
755 Prefix_Node
:= Name_Node
;
758 -- On exit from the loop, Ident_Node is the last identifier scanned,
759 -- i.e. the defining identifier, and Prefix_Node is a node for the
760 -- entire name, structured (incorrectly!) as a selected component.
762 Name_Node
:= Prefix
(Prefix_Node
);
763 Change_Node
(Prefix_Node
, N_Defining_Program_Unit_Name
);
764 Set_Name
(Prefix_Node
, Name_Node
);
765 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
766 Set_Defining_Identifier
(Prefix_Node
, Ident_Node
);
768 -- All set with unit name parsed
775 while Token
= Tok_Dot
or else Token
= Tok_Identifier
loop
780 end P_Defining_Program_Unit_Name
;
782 --------------------------
783 -- 6.1 Operator Symbol --
784 --------------------------
786 -- OPERATOR_SYMBOL ::= STRING_LITERAL
788 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
790 -----------------------------------
791 -- 6.1 Defining Operator Symbol --
792 -----------------------------------
794 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
796 -- The caller has checked that the initial symbol is an operator symbol
798 function P_Defining_Operator_Symbol
return Node_Id
is
802 Op_Node
:= Token_Node
;
803 Change_Operator_Symbol_To_Defining_Operator_Symbol
(Op_Node
);
804 Scan
; -- past operator symbol
806 end P_Defining_Operator_Symbol
;
808 ----------------------------
809 -- 6.1 Parameter_Profile --
810 ----------------------------
812 -- PARAMETER_PROFILE ::= [FORMAL_PART]
814 -- Empty is returned if no formal part is present
816 -- Error recovery: cannot raise Error_Resync
818 function P_Parameter_Profile
return List_Id
is
820 if Token
= Tok_Left_Paren
then
821 Scan
; -- part left paren
822 return P_Formal_Part
;
826 end P_Parameter_Profile
;
828 ---------------------------------------
829 -- 6.1 Parameter And Result Profile --
830 ---------------------------------------
832 -- Parsed by its parent construct, which uses P_Parameter_Profile to
833 -- parse the parameters, and P_Subtype_Mark to parse the return type.
835 ----------------------
836 -- 6.1 Formal part --
837 ----------------------
839 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
841 -- PARAMETER_SPECIFICATION ::=
842 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
843 -- [:= DEFAULT_EXPRESSION]
844 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
845 -- [:= DEFAULT_EXPRESSION]
847 -- This scans the construct Formal_Part. The caller has already checked
848 -- that the initial token is a left parenthesis, and skipped past it, so
849 -- that on entry Token is the first token following the left parenthesis.
851 -- Error recovery: cannot raise Error_Resync
853 function P_Formal_Part
return List_Id
is
854 Specification_List
: List_Id
;
855 Specification_Node
: Node_Id
;
856 Scan_State
: Saved_Scan_State
;
859 Ident_Sloc
: Source_Ptr
;
860 Not_Null_Present
: Boolean := False;
862 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
863 -- This array holds the list of defining identifiers. The upper bound
864 -- of 4096 is intended to be essentially infinite, and we do not even
865 -- bother to check for it being exceeded.
868 Specification_List
:= New_List
;
869 Specification_Loop
: loop
871 if Token
= Tok_Pragma
then
875 Ignore
(Tok_Left_Paren
);
876 Ident_Sloc
:= Token_Ptr
;
877 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
881 exit Ident_Loop
when Token
= Tok_Colon
;
883 -- The only valid tokens are colon and comma, so if we have
884 -- neither do a bit of investigation to see which is the
885 -- better choice for insertion.
887 if Token
/= Tok_Comma
then
889 -- Assume colon if IN or OUT keyword found
891 exit Ident_Loop
when Token
= Tok_In
or else Token
= Tok_Out
;
893 -- Otherwise scan ahead
895 Save_Scan_State
(Scan_State
);
898 -- If we run into a semicolon, then assume that a
899 -- colon was missing, e.g. Parms (X Y; ...). Also
900 -- assume missing colon on EOF (a real disaster!)
901 -- and on a right paren, e.g. Parms (X Y), and also
902 -- on an assignment symbol, e.g. Parms (X Y := ..)
904 if Token
= Tok_Semicolon
905 or else Token
= Tok_Right_Paren
906 or else Token
= Tok_EOF
907 or else Token
= Tok_Colon_Equal
909 Restore_Scan_State
(Scan_State
);
912 -- If we run into a colon, assume that we had a missing
913 -- comma, e.g. Parms (A B : ...). Also assume a missing
914 -- comma if we hit another comma, e.g. Parms (A B, C ..)
916 elsif Token
= Tok_Colon
917 or else Token
= Tok_Comma
919 Restore_Scan_State
(Scan_State
);
927 -- Here if a comma is present, or to be assumed
930 Num_Idents
:= Num_Idents
+ 1;
931 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
934 -- Fall through the loop on encountering a colon, or deciding
935 -- that there is a missing colon.
939 -- If there are multiple identifiers, we repeatedly scan the
940 -- type and initialization expression information by resetting
941 -- the scan pointer (so that we get completely separate trees
942 -- for each occurrence).
944 if Num_Idents
> 1 then
945 Save_Scan_State
(Scan_State
);
948 -- Loop through defining identifiers in list
952 Ident_List_Loop
: loop
953 Specification_Node
:=
954 New_Node
(N_Parameter_Specification
, Ident_Sloc
);
955 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
956 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
958 if Token
= Tok_Access
then
959 Set_Null_Exclusion_Present
960 (Specification_Node
, Not_Null_Present
);
962 if Ada_Version
= Ada_83
then
963 Error_Msg_SC
("(Ada 83) access parameters not allowed");
966 Set_Parameter_Type
(Specification_Node
,
967 P_Access_Definition
(Not_Null_Present
));
970 if Token
= Tok_In
or else Token
= Tok_Out
then
971 if Not_Null_Present
then
973 ("ACCESS must be placed after the parameter mode");
976 P_Mode
(Specification_Node
);
977 Not_Null_Present
:= P_Null_Exclusion
; -- Ada 2005 (AI-231)
980 Set_Null_Exclusion_Present
981 (Specification_Node
, Not_Null_Present
);
983 if Token
= Tok_Procedure
987 Error_Msg_SC
("formal subprogram parameter not allowed");
990 if Token
= Tok_Left_Paren
then
991 Discard_Junk_List
(P_Formal_Part
);
994 if Token
= Tok_Return
then
996 Discard_Junk_Node
(P_Subtype_Mark
);
999 Set_Parameter_Type
(Specification_Node
, Error
);
1002 Set_Parameter_Type
(Specification_Node
, P_Subtype_Mark
);
1007 Set_Expression
(Specification_Node
, Init_Expr_Opt
(True));
1010 Set_Prev_Ids
(Specification_Node
, True);
1013 if Ident
< Num_Idents
then
1014 Set_More_Ids
(Specification_Node
, True);
1017 Append
(Specification_Node
, Specification_List
);
1018 exit Ident_List_Loop
when Ident
= Num_Idents
;
1020 Restore_Scan_State
(Scan_State
);
1021 end loop Ident_List_Loop
;
1024 when Error_Resync
=>
1025 Resync_Semicolon_List
;
1028 if Token
= Tok_Semicolon
then
1029 Save_Scan_State
(Scan_State
);
1030 Scan
; -- past semicolon
1032 -- If we have RETURN or IS after the semicolon, then assume
1033 -- that semicolon should have been a right parenthesis and exit
1035 if Token
= Tok_Is
or else Token
= Tok_Return
then
1036 Error_Msg_SP
("expected "")"" in place of "";""");
1037 exit Specification_Loop
;
1040 -- If we have a declaration keyword after the semicolon, then
1041 -- assume we had a missing right parenthesis and terminate list
1043 if Token
in Token_Class_Declk
then
1044 Error_Msg_AP
("missing "")""");
1045 Restore_Scan_State
(Scan_State
);
1046 exit Specification_Loop
;
1049 elsif Token
= Tok_Right_Paren
then
1050 Scan
; -- past right paren
1051 exit Specification_Loop
;
1053 -- Special check for common error of using comma instead of semicolon
1055 elsif Token
= Tok_Comma
then
1059 -- Special check for omitted separator
1061 elsif Token
= Tok_Identifier
then
1064 -- If nothing sensible, skip to next semicolon or right paren
1068 Resync_Semicolon_List
;
1070 if Token
= Tok_Semicolon
then
1071 Scan
; -- past semicolon
1074 exit Specification_Loop
;
1077 end loop Specification_Loop
;
1079 return Specification_List
;
1082 ----------------------------------
1083 -- 6.1 Parameter Specification --
1084 ----------------------------------
1086 -- Parsed by P_Formal_Part (6.1)
1092 -- MODE ::= [in] | in out | out
1094 -- There is no explicit node in the tree for the Mode. Instead the
1095 -- In_Present and Out_Present flags are set in the parent node to
1096 -- record the presence of keywords specifying the mode.
1098 -- Error_Recovery: cannot raise Error_Resync
1100 procedure P_Mode
(Node
: Node_Id
) is
1102 if Token
= Tok_In
then
1104 Set_In_Present
(Node
, True);
1107 if Token
= Tok_Out
then
1109 Set_Out_Present
(Node
, True);
1112 if Token
= Tok_In
then
1113 Error_Msg_SC
("IN must preceed OUT in parameter mode");
1115 Set_In_Present
(Node
, True);
1119 --------------------------
1120 -- 6.3 Subprogram Body --
1121 --------------------------
1123 -- Parsed by P_Subprogram (6.1)
1125 -----------------------------------
1126 -- 6.4 Procedure Call Statement --
1127 -----------------------------------
1129 -- Parsed by P_Sequence_Of_Statements (5.1)
1131 ------------------------
1132 -- 6.4 Function Call --
1133 ------------------------
1135 -- Parsed by P_Call_Or_Name (4.1)
1137 --------------------------------
1138 -- 6.4 Actual Parameter Part --
1139 --------------------------------
1141 -- Parsed by P_Call_Or_Name (4.1)
1143 --------------------------------
1144 -- 6.4 Parameter Association --
1145 --------------------------------
1147 -- Parsed by P_Call_Or_Name (4.1)
1149 ------------------------------------
1150 -- 6.4 Explicit Actual Parameter --
1151 ------------------------------------
1153 -- Parsed by P_Call_Or_Name (4.1)
1155 ---------------------------
1156 -- 6.5 Return Statement --
1157 ---------------------------
1159 -- RETURN_STATEMENT ::= return [EXPRESSION];
1161 -- The caller has checked that the initial token is RETURN
1163 -- Error recovery: can raise Error_Resync
1165 function P_Return_Statement
return Node_Id
is
1166 Return_Node
: Node_Id
;
1169 Return_Node
:= New_Node
(N_Return_Statement
, Token_Ptr
);
1171 -- Sloc points to RETURN
1174 Scan
; -- past RETURN
1176 if Token
/= Tok_Semicolon
then
1178 -- If no semicolon, then scan an expression, except that
1179 -- we avoid trying to scan an expression if we are at an
1180 -- expression terminator since in that case the best error
1181 -- message is probably that we have a missing semicolon.
1183 if Token
not in Token_Class_Eterm
then
1184 Set_Expression
(Return_Node
, P_Expression_No_Right_Paren
);
1190 end P_Return_Statement
;