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, 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;
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 Return_Node : Node_Id;
148 Rename_Node : Node_Id;
149 Absdec_Node : Node_Id;
151 Fproc_Sloc : Source_Ptr;
153 Scan_State : Saved_Scan_State;
155 -- Flags for optional overriding indication. Two flags are needed,
156 -- to distinguish positive and negative overriding indicators from
157 -- the absence of any indicator.
159 Is_Overriding : Boolean := False;
160 Not_Overriding : Boolean := False;
163 -- Set up scope stack entry. Note that the Labl field will be set later
165 SIS_Entry_Active := False;
166 SIS_Missing_Semicolon_Message := No_Error_Msg;
168 Scope.Table (Scope.Last).Sloc := Token_Ptr;
169 Scope.Table (Scope.Last).Etyp := E_Name;
170 Scope.Table (Scope.Last).Ecol := Start_Column;
171 Scope.Table (Scope.Last).Lreq := False;
173 -- Ada2005: scan leading overriding indicator.
175 if Token = Tok_Not then
178 if Token = Tok_Overriding then
179 Scan; -- past OVERRIDING
180 Not_Overriding := True;
182 Error_Msg_SC ("OVERRIDING expected!");
185 elsif Token = Tok_Overriding then
186 Scan; -- past OVERRIDING
187 Is_Overriding := True;
190 if (Is_Overriding or else Not_Overriding) then
191 if Ada_Version < Ada_05 then
192 Error_Msg_SP (" overriding indicator is an Ada 2005 extension");
193 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
195 -- An overriding indicator is allowed for subprogram declarations,
196 -- bodies, renamings, stubs, and instantiations.
198 elsif Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub then
199 Error_Msg_SC ("overriding indicator not allowed here!");
201 elsif Token /= Tok_Function
202 and then Token /= Tok_Procedure
204 Error_Msg_SC ("FUNCTION or PROCEDURE expected!");
208 Func := (Token = Tok_Function);
209 Fproc_Sloc := Token_Ptr;
210 Scan; -- past FUNCTION or PROCEDURE
215 Name_Node := P_Defining_Designator;
217 if Nkind (Name_Node) = N_Defining_Operator_Symbol
218 and then Scope.Last = 1
220 Error_Msg_SP ("operator symbol not allowed at library level");
221 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
223 -- Set name from file name, we need some junk name, and that's
224 -- as good as anything. This is only approximate, since we do
225 -- not do anything with non-standard name translations.
227 Get_Name_String (File_Name (Current_Source_File));
229 for J in 1 .. Name_Len loop
230 if Name_Buffer (J) = '.' then
236 Set_Chars (Name_Node, Name_Find);
237 Set_Error_Posted (Name_Node);
241 Name_Node := P_Defining_Program_Unit_Name;
244 Scope.Table (Scope.Last).Labl := Name_Node;
246 if Token = Tok_Colon then
247 Error_Msg_SC ("redundant colon ignored");
251 -- Deal with generic instantiation, the one case in which we do not
252 -- have a subprogram specification as part of whatever we are parsing
254 if Token = Tok_Is then
255 Save_Scan_State (Scan_State); -- at the IS
256 T_Is; -- checks for redundant IS
258 if Token = Tok_New then
259 if not Pf_Flags.Gins then
260 Error_Msg_SC ("generic instantation not allowed here!");
266 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
267 Set_Name (Inst_Node, P_Function_Name);
269 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
270 Set_Name (Inst_Node, P_Qualified_Simple_Name);
273 Set_Defining_Unit_Name (Inst_Node, Name_Node);
274 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
276 Pop_Scope_Stack; -- Don't need scope stack entry in this case
278 if Is_Overriding then
279 Set_Must_Override (Inst_Node);
281 elsif Not_Overriding then
282 Set_Must_Not_Override (Inst_Node);
288 Restore_Scan_State (Scan_State); -- to the IS
292 -- If not a generic instantiation, then we definitely have a subprogram
293 -- specification (all possibilities at this stage include one here)
295 Fpart_Sloc := Token_Ptr;
297 Check_Misspelling_Of (Tok_Return);
299 -- Scan formal part. First a special error check. If we have an
300 -- identifier here, then we have a definite error. If this identifier
301 -- is on the same line as the designator, then we assume it is the
302 -- first formal after a missing left parenthesis
304 if Token = Tok_Identifier
305 and then not Token_Is_At_Start_Of_Line
307 T_Left_Paren; -- to generate message
308 Fpart_List := P_Formal_Part;
310 -- Otherwise scan out an optional formal part in the usual manner
313 Fpart_List := P_Parameter_Profile;
316 -- We treat what we have as a function specification if FUNCTION was
317 -- used, or if a RETURN is present. This gives better error recovery
318 -- since later RETURN statements will be valid in either case.
320 Check_Junk_Semicolon_Before_Return;
321 Return_Node := Error;
323 if Token = Tok_Return then
325 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
330 Return_Node := P_Subtype_Mark;
335 Ignore (Tok_Right_Paren);
341 Specification_Node :=
342 New_Node (N_Function_Specification, Fproc_Sloc);
343 Set_Subtype_Mark (Specification_Node, Return_Node);
346 Specification_Node :=
347 New_Node (N_Procedure_Specification, Fproc_Sloc);
350 Set_Defining_Unit_Name (Specification_Node, Name_Node);
351 Set_Parameter_Specifications (Specification_Node, Fpart_List);
353 if Is_Overriding then
354 Set_Must_Override (Specification_Node);
356 elsif Not_Overriding then
357 Set_Must_Not_Override (Specification_Node);
360 -- Error check: barriers not allowed on protected functions/procedures
362 if Token = Tok_When then
364 Error_Msg_SC ("barrier not allowed on function, only on entry");
366 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
370 Discard_Junk_Node (P_Expression);
373 -- Deal with case of semicolon ending a subprogram declaration
375 if Token = Tok_Semicolon then
376 if not Pf_Flags.Decl then
380 Scan; -- past semicolon
382 -- If semicolon is immediately followed by IS, then ignore the
383 -- semicolon, and go process the body.
385 if Token = Tok_Is then
386 Error_Msg_SP ("unexpected semicolon ignored");
387 T_Is; -- ignroe redundant IS's
388 goto Subprogram_Body;
390 -- If BEGIN follows in an appropriate column, we immediately
391 -- commence the error action of assuming that the previous
392 -- subprogram declaration should have been a subprogram body,
393 -- i.e. that the terminating semicolon should have been IS.
395 elsif Token = Tok_Begin
396 and then Start_Column >= Scope.Table (Scope.Last).Ecol
398 Error_Msg_SP (""";"" should be IS!");
399 goto Subprogram_Body;
402 goto Subprogram_Declaration;
405 -- Case of not followed by semicolon
408 -- Subprogram renaming declaration case
410 Check_Misspelling_Of (Tok_Renames);
412 if Token = Tok_Renames then
413 if not Pf_Flags.Rnam then
414 Error_Msg_SC ("renaming declaration not allowed here!");
418 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
419 Scan; -- past RENAMES
420 Set_Name (Rename_Node, P_Name);
421 Set_Specification (Rename_Node, Specification_Node);
426 -- Case of IS following subprogram specification
428 elsif Token = Tok_Is then
429 T_Is; -- ignore redundant Is's
431 if Token_Name = Name_Abstract then
432 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
435 -- Deal nicely with (now obsolete) use of <> in place of abstract
437 if Token = Tok_Box then
438 Error_Msg_SC ("ABSTRACT expected");
439 Token := Tok_Abstract;
442 -- Abstract subprogram declaration case
444 if Token = Tok_Abstract then
446 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
447 Set_Specification (Absdec_Node, Specification_Node);
448 Pop_Scope_Stack; -- discard unneeded entry
449 Scan; -- past ABSTRACT
453 -- Ada 2005 (AI-248): Parse a null procedure declaration
455 elsif Token = Tok_Null then
456 if Ada_Version < Ada_05 then
457 Error_Msg_SP ("null procedures are an Ada 2005 extension");
458 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
464 Error_Msg_SP ("only procedures can be null");
466 Set_Null_Present (Specification_Node);
470 goto Subprogram_Declaration;
472 -- Check for IS NEW with Formal_Part present and handle nicely
474 elsif Token = Tok_New then
476 ("formal part not allowed in instantiation", Fpart_Sloc);
480 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
483 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
486 Set_Defining_Unit_Name (Inst_Node, Name_Node);
487 Set_Name (Inst_Node, P_Name);
488 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
490 Pop_Scope_Stack; -- Don't need scope stack entry in this case
494 goto Subprogram_Body;
497 -- Here we have a missing IS or missing semicolon, we always guess
498 -- a missing semicolon, since we are pretty good at fixing up a
499 -- semicolon which should really be an IS
502 Error_Msg_AP ("missing "";""");
503 SIS_Missing_Semicolon_Message := Get_Msg_Id;
504 goto Subprogram_Declaration;
508 -- Processing for subprogram body
511 if not Pf_Flags.Pbod then
512 Error_Msg_SP ("subprogram body not allowed here!");
515 -- Subprogram body stub case
517 if Separate_Present then
518 if not Pf_Flags.Stub then
519 Error_Msg_SC ("body stub not allowed here!");
522 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
524 ("operator symbol cannot be used as subunit name",
529 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
530 Set_Specification (Stub_Node, Specification_Node);
531 Scan; -- past SEPARATE
536 -- Subprogram body case
539 -- Here is the test for a suspicious IS (i.e. one that looks
540 -- like it might more properly be a semicolon). See separate
541 -- section discussing use of IS instead of semicolon in
544 if (Token in Token_Class_Declk
546 Token = Tok_Identifier)
547 and then Start_Column <= Scope.Table (Scope.Last).Ecol
548 and then Scope.Last /= 1
550 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
551 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
555 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
556 Set_Specification (Body_Node, Specification_Node);
557 Parse_Decls_Begin_End (Body_Node);
561 -- Processing for subprogram declaration
563 <<Subprogram_Declaration>>
565 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
566 Set_Specification (Decl_Node, Specification_Node);
568 -- If this is a context in which a subprogram body is permitted,
569 -- set active SIS entry in case (see section titled "Handling
570 -- Semicolon Used in Place of IS" in body of Parser package)
571 -- Note that SIS_Missing_Semicolon_Message is already set properly.
573 if Pf_Flags.Pbod then
574 SIS_Labl := Scope.Table (Scope.Last).Labl;
575 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
576 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
577 SIS_Declaration_Node := Decl_Node;
578 SIS_Semicolon_Sloc := Prev_Token_Ptr;
579 SIS_Entry_Active := True;
587 ---------------------------------
588 -- 6.1 Subprogram Declaration --
589 ---------------------------------
591 -- Parsed by P_Subprogram (6.1)
593 ------------------------------------------
594 -- 6.1 Abstract Subprogram Declaration --
595 ------------------------------------------
597 -- Parsed by P_Subprogram (6.1)
599 -----------------------------------
600 -- 6.1 Subprogram Specification --
601 -----------------------------------
603 -- SUBPROGRAM_SPECIFICATION ::=
604 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
605 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
607 -- PARAMETER_PROFILE ::= [FORMAL_PART]
609 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
611 -- Subprogram specifications that appear in subprogram declarations
612 -- are parsed by P_Subprogram (6.1). This routine is used in other
613 -- contexts where subprogram specifications occur.
615 -- Note: this routine does not affect the scope stack in any way
617 -- Error recovery: can raise Error_Resync
619 function P_Subprogram_Specification return Node_Id is
620 Specification_Node : Node_Id;
623 if Token = Tok_Function then
624 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
625 Scan; -- past FUNCTION
627 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
628 Set_Parameter_Specifications
629 (Specification_Node, P_Parameter_Profile);
630 Check_Junk_Semicolon_Before_Return;
632 Set_Subtype_Mark (Specification_Node, P_Subtype_Mark);
634 return Specification_Node;
636 elsif Token = Tok_Procedure then
637 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
638 Scan; -- past PROCEDURE
640 Set_Defining_Unit_Name
641 (Specification_Node, P_Defining_Program_Unit_Name);
642 Set_Parameter_Specifications
643 (Specification_Node, P_Parameter_Profile);
644 return Specification_Node;
647 Error_Msg_SC ("subprogram specification expected");
650 end P_Subprogram_Specification;
652 ---------------------
654 ---------------------
657 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
659 -- The caller has checked that the initial token is an identifier,
660 -- operator symbol, or string literal. Note that we don't bother to
661 -- do much error diagnosis in this routine, since it is only used for
662 -- the label on END lines, and the routines in package Par.Endh will
663 -- check that the label is appropriate.
665 -- Error recovery: cannot raise Error_Resync
667 function P_Designator return Node_Id is
668 Ident_Node : Node_Id;
670 Prefix_Node : Node_Id;
672 function Real_Dot return Boolean;
673 -- Tests if a current token is an interesting period, i.e. is followed
674 -- by an identifier or operator symbol or string literal. If not, it is
675 -- probably just incorrect punctuation to be caught by our caller. Note
676 -- that the case of an operator symbol or string literal is also an
677 -- error, but that is an error that we catch here. If the result is
678 -- True, a real dot has been scanned and we are positioned past it,
679 -- if the result is False, the scan position is unchanged.
685 function Real_Dot return Boolean is
686 Scan_State : Saved_Scan_State;
689 if Token /= Tok_Dot then
693 Save_Scan_State (Scan_State);
696 if Token = Tok_Identifier
697 or else Token = Tok_Operator_Symbol
698 or else Token = Tok_String_Literal
703 Restore_Scan_State (Scan_State);
709 -- Start of processing for P_Designator
712 Ident_Node := Token_Node;
713 Scan; -- past initial token
715 if Prev_Token = Tok_Operator_Symbol
716 or else Prev_Token = Tok_String_Literal
724 Prefix_Node := Ident_Node;
726 -- Loop through child names, on entry to this loop, Prefix contains
727 -- the name scanned so far, and Ident_Node is the last identifier.
730 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
731 Set_Prefix (Name_Node, Prefix_Node);
732 Ident_Node := P_Identifier;
733 Set_Selector_Name (Name_Node, Ident_Node);
734 Prefix_Node := Name_Node;
735 exit when not Real_Dot;
738 -- On exit from the loop, Ident_Node is the last identifier scanned,
739 -- i.e. the defining identifier, and Prefix_Node is a node for the
740 -- entire name, structured (incorrectly!) as a selected component.
742 Name_Node := Prefix (Prefix_Node);
743 Change_Node (Prefix_Node, N_Designator);
744 Set_Name (Prefix_Node, Name_Node);
745 Set_Identifier (Prefix_Node, Ident_Node);
751 while Token = Tok_Dot or else Token = Tok_Identifier loop
758 ------------------------------
759 -- 6.1 Defining Designator --
760 ------------------------------
762 -- DEFINING_DESIGNATOR ::=
763 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
765 -- Error recovery: cannot raise Error_Resync
767 function P_Defining_Designator return Node_Id is
769 if Token = Tok_Operator_Symbol then
770 return P_Defining_Operator_Symbol;
772 elsif Token = Tok_String_Literal then
773 Error_Msg_SC ("invalid operator name");
774 Scan; -- past junk string
778 return P_Defining_Program_Unit_Name;
780 end P_Defining_Designator;
782 -------------------------------------
783 -- 6.1 Defining Program Unit Name --
784 -------------------------------------
786 -- DEFINING_PROGRAM_UNIT_NAME ::=
787 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
789 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
791 -- Error recovery: cannot raise Error_Resync
793 function P_Defining_Program_Unit_Name return Node_Id is
794 Ident_Node : Node_Id;
796 Prefix_Node : Node_Id;
799 -- Set identifier casing if not already set and scan initial identifier
801 if Token = Tok_Identifier
802 and then Identifier_Casing (Current_Source_File) = Unknown
804 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
807 Ident_Node := P_Identifier (C_Dot);
808 Merge_Identifier (Ident_Node, Tok_Return);
810 -- Normal case (not child library unit name)
812 if Token /= Tok_Dot then
813 Change_Identifier_To_Defining_Identifier (Ident_Node);
816 -- Child library unit name case
819 if Scope.Last > 1 then
820 Error_Msg_SP ("child unit allowed only at library level");
823 elsif Ada_Version = Ada_83 then
824 Error_Msg_SP ("(Ada 83) child unit not allowed!");
828 Prefix_Node := Ident_Node;
830 -- Loop through child names, on entry to this loop, Prefix contains
831 -- the name scanned so far, and Ident_Node is the last identifier.
834 exit when Token /= Tok_Dot;
835 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
837 Set_Prefix (Name_Node, Prefix_Node);
838 Ident_Node := P_Identifier (C_Dot);
839 Set_Selector_Name (Name_Node, Ident_Node);
840 Prefix_Node := Name_Node;
843 -- On exit from the loop, Ident_Node is the last identifier scanned,
844 -- i.e. the defining identifier, and Prefix_Node is a node for the
845 -- entire name, structured (incorrectly!) as a selected component.
847 Name_Node := Prefix (Prefix_Node);
848 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
849 Set_Name (Prefix_Node, Name_Node);
850 Change_Identifier_To_Defining_Identifier (Ident_Node);
851 Set_Defining_Identifier (Prefix_Node, Ident_Node);
853 -- All set with unit name parsed
860 while Token = Tok_Dot or else Token = Tok_Identifier loop
865 end P_Defining_Program_Unit_Name;
867 --------------------------
868 -- 6.1 Operator Symbol --
869 --------------------------
871 -- OPERATOR_SYMBOL ::= STRING_LITERAL
873 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
875 -----------------------------------
876 -- 6.1 Defining Operator Symbol --
877 -----------------------------------
879 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
881 -- The caller has checked that the initial symbol is an operator symbol
883 function P_Defining_Operator_Symbol return Node_Id is
887 Op_Node := Token_Node;
888 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
889 Scan; -- past operator symbol
891 end P_Defining_Operator_Symbol;
893 ----------------------------
894 -- 6.1 Parameter_Profile --
895 ----------------------------
897 -- PARAMETER_PROFILE ::= [FORMAL_PART]
899 -- Empty is returned if no formal part is present
901 -- Error recovery: cannot raise Error_Resync
903 function P_Parameter_Profile return List_Id is
905 if Token = Tok_Left_Paren then
906 Scan; -- part left paren
907 return P_Formal_Part;
911 end P_Parameter_Profile;
913 ---------------------------------------
914 -- 6.1 Parameter And Result Profile --
915 ---------------------------------------
917 -- Parsed by its parent construct, which uses P_Parameter_Profile to
918 -- parse the parameters, and P_Subtype_Mark to parse the return type.
920 ----------------------
921 -- 6.1 Formal part --
922 ----------------------
924 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
926 -- PARAMETER_SPECIFICATION ::=
927 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
928 -- [:= DEFAULT_EXPRESSION]
929 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
930 -- [:= DEFAULT_EXPRESSION]
932 -- This scans the construct Formal_Part. The caller has already checked
933 -- that the initial token is a left parenthesis, and skipped past it, so
934 -- that on entry Token is the first token following the left parenthesis.
936 -- Error recovery: cannot raise Error_Resync
938 function P_Formal_Part return List_Id is
939 Specification_List : List_Id;
940 Specification_Node : Node_Id;
941 Scan_State : Saved_Scan_State;
944 Ident_Sloc : Source_Ptr;
945 Not_Null_Present : Boolean := False;
947 Idents : array (Int range 1 .. 4096) of Entity_Id;
948 -- This array holds the list of defining identifiers. The upper bound
949 -- of 4096 is intended to be essentially infinite, and we do not even
950 -- bother to check for it being exceeded.
953 Specification_List := New_List;
954 Specification_Loop : loop
956 if Token = Tok_Pragma then
960 Ignore (Tok_Left_Paren);
961 Ident_Sloc := Token_Ptr;
962 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
966 exit Ident_Loop when Token = Tok_Colon;
968 -- The only valid tokens are colon and comma, so if we have
969 -- neither do a bit of investigation to see which is the
970 -- better choice for insertion.
972 if Token /= Tok_Comma then
974 -- Assume colon if IN or OUT keyword found
976 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
978 -- Otherwise scan ahead
980 Save_Scan_State (Scan_State);
983 -- If we run into a semicolon, then assume that a
984 -- colon was missing, e.g. Parms (X Y; ...). Also
985 -- assume missing colon on EOF (a real disaster!)
986 -- and on a right paren, e.g. Parms (X Y), and also
987 -- on an assignment symbol, e.g. Parms (X Y := ..)
989 if Token = Tok_Semicolon
990 or else Token = Tok_Right_Paren
991 or else Token = Tok_EOF
992 or else Token = Tok_Colon_Equal
994 Restore_Scan_State (Scan_State);
997 -- If we run into a colon, assume that we had a missing
998 -- comma, e.g. Parms (A B : ...). Also assume a missing
999 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1001 elsif Token = Tok_Colon
1002 or else Token = Tok_Comma
1004 Restore_Scan_State (Scan_State);
1009 end loop Look_Ahead;
1012 -- Here if a comma is present, or to be assumed
1015 Num_Idents := Num_Idents + 1;
1016 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1017 end loop Ident_Loop;
1019 -- Fall through the loop on encountering a colon, or deciding
1020 -- that there is a missing colon.
1024 -- If there are multiple identifiers, we repeatedly scan the
1025 -- type and initialization expression information by resetting
1026 -- the scan pointer (so that we get completely separate trees
1027 -- for each occurrence).
1029 if Num_Idents > 1 then
1030 Save_Scan_State (Scan_State);
1033 -- Loop through defining identifiers in list
1037 Ident_List_Loop : loop
1038 Specification_Node :=
1039 New_Node (N_Parameter_Specification, Ident_Sloc);
1040 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1041 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1043 if Token = Tok_Access then
1044 Set_Null_Exclusion_Present
1045 (Specification_Node, Not_Null_Present);
1047 if Ada_Version = Ada_83 then
1048 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1051 Set_Parameter_Type (Specification_Node,
1052 P_Access_Definition (Not_Null_Present));
1055 if Token = Tok_In or else Token = Tok_Out then
1056 if Not_Null_Present then
1058 ("ACCESS must be placed after the parameter mode");
1061 P_Mode (Specification_Node);
1062 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1065 Set_Null_Exclusion_Present
1066 (Specification_Node, Not_Null_Present);
1068 if Token = Tok_Procedure
1070 Token = Tok_Function
1072 Error_Msg_SC ("formal subprogram parameter not allowed");
1075 if Token = Tok_Left_Paren then
1076 Discard_Junk_List (P_Formal_Part);
1079 if Token = Tok_Return then
1081 Discard_Junk_Node (P_Subtype_Mark);
1084 Set_Parameter_Type (Specification_Node, Error);
1087 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1092 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1095 Set_Prev_Ids (Specification_Node, True);
1098 if Ident < Num_Idents then
1099 Set_More_Ids (Specification_Node, True);
1102 Append (Specification_Node, Specification_List);
1103 exit Ident_List_Loop when Ident = Num_Idents;
1105 Restore_Scan_State (Scan_State);
1106 end loop Ident_List_Loop;
1109 when Error_Resync =>
1110 Resync_Semicolon_List;
1113 if Token = Tok_Semicolon then
1114 Save_Scan_State (Scan_State);
1115 Scan; -- past semicolon
1117 -- If we have RETURN or IS after the semicolon, then assume
1118 -- that semicolon should have been a right parenthesis and exit
1120 if Token = Tok_Is or else Token = Tok_Return then
1121 Error_Msg_SP ("expected "")"" in place of "";""");
1122 exit Specification_Loop;
1125 -- If we have a declaration keyword after the semicolon, then
1126 -- assume we had a missing right parenthesis and terminate list
1128 if Token in Token_Class_Declk then
1129 Error_Msg_AP ("missing "")""");
1130 Restore_Scan_State (Scan_State);
1131 exit Specification_Loop;
1134 elsif Token = Tok_Right_Paren then
1135 Scan; -- past right paren
1136 exit Specification_Loop;
1138 -- Special check for common error of using comma instead of semicolon
1140 elsif Token = Tok_Comma then
1144 -- Special check for omitted separator
1146 elsif Token = Tok_Identifier then
1149 -- If nothing sensible, skip to next semicolon or right paren
1153 Resync_Semicolon_List;
1155 if Token = Tok_Semicolon then
1156 Scan; -- past semicolon
1159 exit Specification_Loop;
1162 end loop Specification_Loop;
1164 return Specification_List;
1167 ----------------------------------
1168 -- 6.1 Parameter Specification --
1169 ----------------------------------
1171 -- Parsed by P_Formal_Part (6.1)
1177 -- MODE ::= [in] | in out | out
1179 -- There is no explicit node in the tree for the Mode. Instead the
1180 -- In_Present and Out_Present flags are set in the parent node to
1181 -- record the presence of keywords specifying the mode.
1183 -- Error_Recovery: cannot raise Error_Resync
1185 procedure P_Mode (Node : Node_Id) is
1187 if Token = Tok_In then
1189 Set_In_Present (Node, True);
1192 if Token = Tok_Out then
1194 Set_Out_Present (Node, True);
1197 if Token = Tok_In then
1198 Error_Msg_SC ("IN must preceed OUT in parameter mode");
1200 Set_In_Present (Node, True);
1204 --------------------------
1205 -- 6.3 Subprogram Body --
1206 --------------------------
1208 -- Parsed by P_Subprogram (6.1)
1210 -----------------------------------
1211 -- 6.4 Procedure Call Statement --
1212 -----------------------------------
1214 -- Parsed by P_Sequence_Of_Statements (5.1)
1216 ------------------------
1217 -- 6.4 Function Call --
1218 ------------------------
1220 -- Parsed by P_Call_Or_Name (4.1)
1222 --------------------------------
1223 -- 6.4 Actual Parameter Part --
1224 --------------------------------
1226 -- Parsed by P_Call_Or_Name (4.1)
1228 --------------------------------
1229 -- 6.4 Parameter Association --
1230 --------------------------------
1232 -- Parsed by P_Call_Or_Name (4.1)
1234 ------------------------------------
1235 -- 6.4 Explicit Actual Parameter --
1236 ------------------------------------
1238 -- Parsed by P_Call_Or_Name (4.1)
1240 ---------------------------
1241 -- 6.5 Return Statement --
1242 ---------------------------
1244 -- RETURN_STATEMENT ::= return [EXPRESSION];
1246 -- The caller has checked that the initial token is RETURN
1248 -- Error recovery: can raise Error_Resync
1250 function P_Return_Statement return Node_Id is
1251 Return_Node : Node_Id;
1254 Return_Node := New_Node (N_Return_Statement, Token_Ptr);
1256 -- Sloc points to RETURN
1259 Scan; -- past RETURN
1261 if Token /= Tok_Semicolon then
1263 -- If no semicolon, then scan an expression, except that
1264 -- we avoid trying to scan an expression if we are at an
1265 -- expression terminator since in that case the best error
1266 -- message is probably that we have a missing semicolon.
1268 if Token not in Token_Class_Eterm then
1269 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1275 end P_Return_Statement;