1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
30 with Sinfo.CN; use Sinfo.CN;
35 -- Local subprograms, used only in this chapter
37 function P_Defining_Designator return Node_Id;
38 function P_Defining_Operator_Symbol return Node_Id;
39 function P_Return_Object_Declaration return Node_Id;
41 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id);
42 -- Decl_Node is a N_Object_Declaration.
43 -- Set the Null_Exclusion_Present and Object_Definition fields of
46 procedure Check_Junk_Semicolon_Before_Return;
48 -- Check for common error of junk semicolon before RETURN keyword of
49 -- function specification. If present, skip over it with appropriate
50 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
51 -- routine also deals with a possibly misspelled version of Return.
53 ----------------------------------------
54 -- Check_Junk_Semicolon_Before_Return --
55 ----------------------------------------
57 procedure Check_Junk_Semicolon_Before_Return is
58 Scan_State : Saved_Scan_State;
61 if Token = Tok_Semicolon then
62 Save_Scan_State (Scan_State);
63 Scan; -- past the semicolon
65 if Token = Tok_Return then
66 Restore_Scan_State (Scan_State);
67 Error_Msg_SC ("unexpected semicolon ignored");
68 Scan; -- rescan past junk semicolon
71 Restore_Scan_State (Scan_State);
74 elsif Bad_Spelling_Of (Tok_Return) then
77 end Check_Junk_Semicolon_Before_Return;
79 -----------------------------------------------------
80 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
81 -----------------------------------------------------
83 -- This routine scans out a subprogram declaration, subprogram body,
84 -- subprogram renaming declaration or subprogram generic instantiation.
86 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
88 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
89 -- SUBPROGRAM_SPECIFICATION is abstract;
91 -- SUBPROGRAM_SPECIFICATION ::=
92 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
93 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
95 -- PARAMETER_PROFILE ::= [FORMAL_PART]
97 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
99 -- SUBPROGRAM_BODY ::=
100 -- SUBPROGRAM_SPECIFICATION is
103 -- HANDLED_SEQUENCE_OF_STATEMENTS
106 -- SUBPROGRAM_RENAMING_DECLARATION ::=
107 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
109 -- SUBPROGRAM_BODY_STUB ::=
110 -- SUBPROGRAM_SPECIFICATION is separate;
112 -- GENERIC_INSTANTIATION ::=
113 -- procedure DEFINING_PROGRAM_UNIT_NAME is
114 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
115 -- | function DEFINING_DESIGNATOR is
116 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
118 -- NULL_PROCEDURE_DECLARATION ::=
119 -- SUBPROGRAM_SPECIFICATION is null;
121 -- Null procedures are an Ada 2005 feature. A null procedure declaration
122 -- is classified as a basic declarative item, but it is parsed here, with
123 -- other subprogram constructs.
125 -- The value in Pf_Flags indicates which of these possible declarations
126 -- is acceptable to the caller:
128 -- Pf_Flags.Decl Set if declaration OK
129 -- Pf_Flags.Gins Set if generic instantiation OK
130 -- Pf_Flags.Pbod Set if proper body OK
131 -- Pf_Flags.Rnam Set if renaming declaration OK
132 -- Pf_Flags.Stub Set if body stub OK
134 -- If an inappropriate form is encountered, it is scanned out but an
135 -- error message indicating that it is appearing in an inappropriate
136 -- context is issued. The only possible values for Pf_Flags are those
137 -- defined as constants in the Par package.
139 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
140 -- NOT or OVERRIDING.
142 -- Error recovery: cannot raise Error_Resync
144 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
145 Specification_Node : Node_Id;
147 Fpart_List : List_Id;
148 Fpart_Sloc : Source_Ptr;
149 Result_Not_Null : Boolean := False;
150 Result_Node : Node_Id;
154 Rename_Node : Node_Id;
155 Absdec_Node : Node_Id;
157 Fproc_Sloc : Source_Ptr;
159 Scan_State : Saved_Scan_State;
161 -- Flags for optional overriding indication. Two flags are needed,
162 -- to distinguish positive and negative overriding indicators from
163 -- the absence of any indicator.
165 Is_Overriding : Boolean := False;
166 Not_Overriding : Boolean := False;
169 -- Set up scope stack entry. Note that the Labl field will be set later
171 SIS_Entry_Active := False;
172 SIS_Missing_Semicolon_Message := No_Error_Msg;
174 Scope.Table (Scope.Last).Sloc := Token_Ptr;
175 Scope.Table (Scope.Last).Etyp := E_Name;
176 Scope.Table (Scope.Last).Ecol := Start_Column;
177 Scope.Table (Scope.Last).Lreq := False;
179 -- Ada2005: scan leading overriding indicator
181 if Token = Tok_Not then
184 if Token = Tok_Overriding then
185 Scan; -- past OVERRIDING
186 Not_Overriding := True;
188 Error_Msg_SC ("OVERRIDING expected!");
191 elsif Token = Tok_Overriding then
192 Scan; -- past OVERRIDING
193 Is_Overriding := True;
196 if (Is_Overriding or else Not_Overriding) then
197 if Ada_Version < Ada_05 then
198 Error_Msg_SP (" overriding indicator is an Ada 2005 extension");
199 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
201 -- An overriding indicator is allowed for subprogram declarations,
202 -- bodies, renamings, stubs, and instantiations.
204 elsif Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub then
205 Error_Msg_SC ("overriding indicator not allowed here!");
207 elsif Token /= Tok_Function
208 and then Token /= Tok_Procedure
210 Error_Msg_SC ("FUNCTION or PROCEDURE expected!");
214 Func := (Token = Tok_Function);
215 Fproc_Sloc := Token_Ptr;
216 Scan; -- past FUNCTION or PROCEDURE
221 Name_Node := P_Defining_Designator;
223 if Nkind (Name_Node) = N_Defining_Operator_Symbol
224 and then Scope.Last = 1
226 Error_Msg_SP ("operator symbol not allowed at library level");
227 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
229 -- Set name from file name, we need some junk name, and that's
230 -- as good as anything. This is only approximate, since we do
231 -- not do anything with non-standard name translations.
233 Get_Name_String (File_Name (Current_Source_File));
235 for J in 1 .. Name_Len loop
236 if Name_Buffer (J) = '.' then
242 Set_Chars (Name_Node, Name_Find);
243 Set_Error_Posted (Name_Node);
247 Name_Node := P_Defining_Program_Unit_Name;
250 Scope.Table (Scope.Last).Labl := Name_Node;
252 if Token = Tok_Colon then
253 Error_Msg_SC ("redundant colon ignored");
257 -- Deal with generic instantiation, the one case in which we do not
258 -- have a subprogram specification as part of whatever we are parsing
260 if Token = Tok_Is then
261 Save_Scan_State (Scan_State); -- at the IS
262 T_Is; -- checks for redundant IS
264 if Token = Tok_New then
265 if not Pf_Flags.Gins then
266 Error_Msg_SC ("generic instantation not allowed here!");
272 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
273 Set_Name (Inst_Node, P_Function_Name);
275 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
276 Set_Name (Inst_Node, P_Qualified_Simple_Name);
279 Set_Defining_Unit_Name (Inst_Node, Name_Node);
280 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
282 Pop_Scope_Stack; -- Don't need scope stack entry in this case
284 if Is_Overriding then
285 Set_Must_Override (Inst_Node);
287 elsif Not_Overriding then
288 Set_Must_Not_Override (Inst_Node);
294 Restore_Scan_State (Scan_State); -- to the IS
298 -- If not a generic instantiation, then we definitely have a subprogram
299 -- specification (all possibilities at this stage include one here)
301 Fpart_Sloc := Token_Ptr;
303 Check_Misspelling_Of (Tok_Return);
305 -- Scan formal part. First a special error check. If we have an
306 -- identifier here, then we have a definite error. If this identifier
307 -- is on the same line as the designator, then we assume it is the
308 -- first formal after a missing left parenthesis
310 if Token = Tok_Identifier
311 and then not Token_Is_At_Start_Of_Line
313 T_Left_Paren; -- to generate message
314 Fpart_List := P_Formal_Part;
316 -- Otherwise scan out an optional formal part in the usual manner
319 Fpart_List := P_Parameter_Profile;
322 -- We treat what we have as a function specification if FUNCTION was
323 -- used, or if a RETURN is present. This gives better error recovery
324 -- since later RETURN statements will be valid in either case.
326 Check_Junk_Semicolon_Before_Return;
327 Result_Node := Error;
329 if Token = Tok_Return then
331 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
337 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
339 -- Ada 2005 (AI-318-02)
341 if Token = Tok_Access then
342 if Ada_Version < Ada_05 then
344 ("anonymous access result type is an Ada 2005 extension");
345 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
348 Result_Node := P_Access_Definition (Result_Not_Null);
351 Result_Node := P_Subtype_Mark;
357 Ignore (Tok_Right_Paren);
363 Specification_Node :=
364 New_Node (N_Function_Specification, Fproc_Sloc);
366 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
367 Set_Result_Definition (Specification_Node, Result_Node);
370 Specification_Node :=
371 New_Node (N_Procedure_Specification, Fproc_Sloc);
374 Set_Defining_Unit_Name (Specification_Node, Name_Node);
375 Set_Parameter_Specifications (Specification_Node, Fpart_List);
377 if Is_Overriding then
378 Set_Must_Override (Specification_Node);
380 elsif Not_Overriding then
381 Set_Must_Not_Override (Specification_Node);
384 -- Error check: barriers not allowed on protected functions/procedures
386 if Token = Tok_When then
388 Error_Msg_SC ("barrier not allowed on function, only on entry");
390 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
394 Discard_Junk_Node (P_Expression);
397 -- Deal with case of semicolon ending a subprogram declaration
399 if Token = Tok_Semicolon then
400 if not Pf_Flags.Decl then
404 Scan; -- past semicolon
406 -- If semicolon is immediately followed by IS, then ignore the
407 -- semicolon, and go process the body.
409 if Token = Tok_Is then
410 Error_Msg_SP ("unexpected semicolon ignored");
411 T_Is; -- ignroe redundant IS's
412 goto Subprogram_Body;
414 -- If BEGIN follows in an appropriate column, we immediately
415 -- commence the error action of assuming that the previous
416 -- subprogram declaration should have been a subprogram body,
417 -- i.e. that the terminating semicolon should have been IS.
419 elsif Token = Tok_Begin
420 and then Start_Column >= Scope.Table (Scope.Last).Ecol
422 Error_Msg_SP (""";"" should be IS!");
423 goto Subprogram_Body;
426 goto Subprogram_Declaration;
429 -- Case of not followed by semicolon
432 -- Subprogram renaming declaration case
434 Check_Misspelling_Of (Tok_Renames);
436 if Token = Tok_Renames then
437 if not Pf_Flags.Rnam then
438 Error_Msg_SC ("renaming declaration not allowed here!");
442 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
443 Scan; -- past RENAMES
444 Set_Name (Rename_Node, P_Name);
445 Set_Specification (Rename_Node, Specification_Node);
450 -- Case of IS following subprogram specification
452 elsif Token = Tok_Is then
453 T_Is; -- ignore redundant Is's
455 if Token_Name = Name_Abstract then
456 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
459 -- Deal nicely with (now obsolete) use of <> in place of abstract
461 if Token = Tok_Box then
462 Error_Msg_SC ("ABSTRACT expected");
463 Token := Tok_Abstract;
466 -- Abstract subprogram declaration case
468 if Token = Tok_Abstract then
470 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
471 Set_Specification (Absdec_Node, Specification_Node);
472 Pop_Scope_Stack; -- discard unneeded entry
473 Scan; -- past ABSTRACT
477 -- Ada 2005 (AI-248): Parse a null procedure declaration
479 elsif Token = Tok_Null then
480 if Ada_Version < Ada_05 then
481 Error_Msg_SP ("null procedures are an Ada 2005 extension");
482 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
488 Error_Msg_SP ("only procedures can be null");
490 Set_Null_Present (Specification_Node);
494 goto Subprogram_Declaration;
496 -- Check for IS NEW with Formal_Part present and handle nicely
498 elsif Token = Tok_New then
500 ("formal part not allowed in instantiation", Fpart_Sloc);
504 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
507 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
510 Set_Defining_Unit_Name (Inst_Node, Name_Node);
511 Set_Name (Inst_Node, P_Name);
512 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
514 Pop_Scope_Stack; -- Don't need scope stack entry in this case
518 goto Subprogram_Body;
521 -- Here we have a missing IS or missing semicolon, we always guess
522 -- a missing semicolon, since we are pretty good at fixing up a
523 -- semicolon which should really be an IS
526 Error_Msg_AP ("missing "";""");
527 SIS_Missing_Semicolon_Message := Get_Msg_Id;
528 goto Subprogram_Declaration;
532 -- Processing for subprogram body
535 if not Pf_Flags.Pbod then
536 Error_Msg_SP ("subprogram body not allowed here!");
539 -- Subprogram body stub case
541 if Separate_Present then
542 if not Pf_Flags.Stub then
543 Error_Msg_SC ("body stub not allowed here!");
546 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
548 ("operator symbol cannot be used as subunit name",
553 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
554 Set_Specification (Stub_Node, Specification_Node);
555 Scan; -- past SEPARATE
560 -- Subprogram body case
563 -- Here is the test for a suspicious IS (i.e. one that looks
564 -- like it might more properly be a semicolon). See separate
565 -- section discussing use of IS instead of semicolon in
568 if (Token in Token_Class_Declk
570 Token = Tok_Identifier)
571 and then Start_Column <= Scope.Table (Scope.Last).Ecol
572 and then Scope.Last /= 1
574 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
575 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
579 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
580 Set_Specification (Body_Node, Specification_Node);
581 Parse_Decls_Begin_End (Body_Node);
585 -- Processing for subprogram declaration
587 <<Subprogram_Declaration>>
589 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
590 Set_Specification (Decl_Node, Specification_Node);
592 -- If this is a context in which a subprogram body is permitted,
593 -- set active SIS entry in case (see section titled "Handling
594 -- Semicolon Used in Place of IS" in body of Parser package)
595 -- Note that SIS_Missing_Semicolon_Message is already set properly.
597 if Pf_Flags.Pbod then
598 SIS_Labl := Scope.Table (Scope.Last).Labl;
599 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
600 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
601 SIS_Declaration_Node := Decl_Node;
602 SIS_Semicolon_Sloc := Prev_Token_Ptr;
603 SIS_Entry_Active := True;
611 ---------------------------------
612 -- 6.1 Subprogram Declaration --
613 ---------------------------------
615 -- Parsed by P_Subprogram (6.1)
617 ------------------------------------------
618 -- 6.1 Abstract Subprogram Declaration --
619 ------------------------------------------
621 -- Parsed by P_Subprogram (6.1)
623 -----------------------------------
624 -- 6.1 Subprogram Specification --
625 -----------------------------------
627 -- SUBPROGRAM_SPECIFICATION ::=
628 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
629 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
631 -- PARAMETER_PROFILE ::= [FORMAL_PART]
633 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
635 -- Subprogram specifications that appear in subprogram declarations
636 -- are parsed by P_Subprogram (6.1). This routine is used in other
637 -- contexts where subprogram specifications occur.
639 -- Note: this routine does not affect the scope stack in any way
641 -- Error recovery: can raise Error_Resync
643 function P_Subprogram_Specification return Node_Id is
644 Specification_Node : Node_Id;
645 Result_Not_Null : Boolean;
646 Result_Node : Node_Id;
649 if Token = Tok_Function then
650 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
651 Scan; -- past FUNCTION
653 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
654 Set_Parameter_Specifications
655 (Specification_Node, P_Parameter_Profile);
656 Check_Junk_Semicolon_Before_Return;
659 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
661 -- Ada 2005 (AI-318-02)
663 if Token = Tok_Access then
664 if Ada_Version < Ada_05 then
666 ("anonymous access result type is an Ada 2005 extension");
667 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
670 Result_Node := P_Access_Definition (Result_Not_Null);
673 Result_Node := P_Subtype_Mark;
677 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
678 Set_Result_Definition (Specification_Node, Result_Node);
679 return Specification_Node;
681 elsif Token = Tok_Procedure then
682 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
683 Scan; -- past PROCEDURE
685 Set_Defining_Unit_Name
686 (Specification_Node, P_Defining_Program_Unit_Name);
687 Set_Parameter_Specifications
688 (Specification_Node, P_Parameter_Profile);
689 return Specification_Node;
692 Error_Msg_SC ("subprogram specification expected");
695 end P_Subprogram_Specification;
697 ---------------------
699 ---------------------
702 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
704 -- The caller has checked that the initial token is an identifier,
705 -- operator symbol, or string literal. Note that we don't bother to
706 -- do much error diagnosis in this routine, since it is only used for
707 -- the label on END lines, and the routines in package Par.Endh will
708 -- check that the label is appropriate.
710 -- Error recovery: cannot raise Error_Resync
712 function P_Designator return Node_Id is
713 Ident_Node : Node_Id;
715 Prefix_Node : Node_Id;
717 function Real_Dot return Boolean;
718 -- Tests if a current token is an interesting period, i.e. is followed
719 -- by an identifier or operator symbol or string literal. If not, it is
720 -- probably just incorrect punctuation to be caught by our caller. Note
721 -- that the case of an operator symbol or string literal is also an
722 -- error, but that is an error that we catch here. If the result is
723 -- True, a real dot has been scanned and we are positioned past it,
724 -- if the result is False, the scan position is unchanged.
730 function Real_Dot return Boolean is
731 Scan_State : Saved_Scan_State;
734 if Token /= Tok_Dot then
738 Save_Scan_State (Scan_State);
741 if Token = Tok_Identifier
742 or else Token = Tok_Operator_Symbol
743 or else Token = Tok_String_Literal
748 Restore_Scan_State (Scan_State);
754 -- Start of processing for P_Designator
757 Ident_Node := Token_Node;
758 Scan; -- past initial token
760 if Prev_Token = Tok_Operator_Symbol
761 or else Prev_Token = Tok_String_Literal
769 Prefix_Node := Ident_Node;
771 -- Loop through child names, on entry to this loop, Prefix contains
772 -- the name scanned so far, and Ident_Node is the last identifier.
775 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
776 Set_Prefix (Name_Node, Prefix_Node);
777 Ident_Node := P_Identifier;
778 Set_Selector_Name (Name_Node, Ident_Node);
779 Prefix_Node := Name_Node;
780 exit when not Real_Dot;
783 -- On exit from the loop, Ident_Node is the last identifier scanned,
784 -- i.e. the defining identifier, and Prefix_Node is a node for the
785 -- entire name, structured (incorrectly!) as a selected component.
787 Name_Node := Prefix (Prefix_Node);
788 Change_Node (Prefix_Node, N_Designator);
789 Set_Name (Prefix_Node, Name_Node);
790 Set_Identifier (Prefix_Node, Ident_Node);
796 while Token = Tok_Dot or else Token = Tok_Identifier loop
803 ------------------------------
804 -- 6.1 Defining Designator --
805 ------------------------------
807 -- DEFINING_DESIGNATOR ::=
808 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
810 -- Error recovery: cannot raise Error_Resync
812 function P_Defining_Designator return Node_Id is
814 if Token = Tok_Operator_Symbol then
815 return P_Defining_Operator_Symbol;
817 elsif Token = Tok_String_Literal then
818 Error_Msg_SC ("invalid operator name");
819 Scan; -- past junk string
823 return P_Defining_Program_Unit_Name;
825 end P_Defining_Designator;
827 -------------------------------------
828 -- 6.1 Defining Program Unit Name --
829 -------------------------------------
831 -- DEFINING_PROGRAM_UNIT_NAME ::=
832 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
834 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
836 -- Error recovery: cannot raise Error_Resync
838 function P_Defining_Program_Unit_Name return Node_Id is
839 Ident_Node : Node_Id;
841 Prefix_Node : Node_Id;
844 -- Set identifier casing if not already set and scan initial identifier
846 if Token = Tok_Identifier
847 and then Identifier_Casing (Current_Source_File) = Unknown
849 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
852 Ident_Node := P_Identifier (C_Dot);
853 Merge_Identifier (Ident_Node, Tok_Return);
855 -- Normal case (not child library unit name)
857 if Token /= Tok_Dot then
858 Change_Identifier_To_Defining_Identifier (Ident_Node);
861 -- Child library unit name case
864 if Scope.Last > 1 then
865 Error_Msg_SP ("child unit allowed only at library level");
868 elsif Ada_Version = Ada_83 then
869 Error_Msg_SP ("(Ada 83) child unit not allowed!");
873 Prefix_Node := Ident_Node;
875 -- Loop through child names, on entry to this loop, Prefix contains
876 -- the name scanned so far, and Ident_Node is the last identifier.
879 exit when Token /= Tok_Dot;
880 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
882 Set_Prefix (Name_Node, Prefix_Node);
883 Ident_Node := P_Identifier (C_Dot);
884 Set_Selector_Name (Name_Node, Ident_Node);
885 Prefix_Node := Name_Node;
888 -- On exit from the loop, Ident_Node is the last identifier scanned,
889 -- i.e. the defining identifier, and Prefix_Node is a node for the
890 -- entire name, structured (incorrectly!) as a selected component.
892 Name_Node := Prefix (Prefix_Node);
893 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
894 Set_Name (Prefix_Node, Name_Node);
895 Change_Identifier_To_Defining_Identifier (Ident_Node);
896 Set_Defining_Identifier (Prefix_Node, Ident_Node);
898 -- All set with unit name parsed
905 while Token = Tok_Dot or else Token = Tok_Identifier loop
910 end P_Defining_Program_Unit_Name;
912 --------------------------
913 -- 6.1 Operator Symbol --
914 --------------------------
916 -- OPERATOR_SYMBOL ::= STRING_LITERAL
918 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
920 -----------------------------------
921 -- 6.1 Defining Operator Symbol --
922 -----------------------------------
924 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
926 -- The caller has checked that the initial symbol is an operator symbol
928 function P_Defining_Operator_Symbol return Node_Id is
932 Op_Node := Token_Node;
933 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
934 Scan; -- past operator symbol
936 end P_Defining_Operator_Symbol;
938 ----------------------------
939 -- 6.1 Parameter_Profile --
940 ----------------------------
942 -- PARAMETER_PROFILE ::= [FORMAL_PART]
944 -- Empty is returned if no formal part is present
946 -- Error recovery: cannot raise Error_Resync
948 function P_Parameter_Profile return List_Id is
950 if Token = Tok_Left_Paren then
951 Scan; -- part left paren
952 return P_Formal_Part;
956 end P_Parameter_Profile;
958 ---------------------------------------
959 -- 6.1 Parameter And Result Profile --
960 ---------------------------------------
962 -- Parsed by its parent construct, which uses P_Parameter_Profile to
963 -- parse the parameters, and P_Subtype_Mark to parse the return type.
965 ----------------------
966 -- 6.1 Formal part --
967 ----------------------
969 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
971 -- PARAMETER_SPECIFICATION ::=
972 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
973 -- [:= DEFAULT_EXPRESSION]
974 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
975 -- [:= DEFAULT_EXPRESSION]
977 -- This scans the construct Formal_Part. The caller has already checked
978 -- that the initial token is a left parenthesis, and skipped past it, so
979 -- that on entry Token is the first token following the left parenthesis.
981 -- Error recovery: cannot raise Error_Resync
983 function P_Formal_Part return List_Id is
984 Specification_List : List_Id;
985 Specification_Node : Node_Id;
986 Scan_State : Saved_Scan_State;
989 Ident_Sloc : Source_Ptr;
990 Not_Null_Present : Boolean := False;
991 Not_Null_Sloc : Source_Ptr;
993 Idents : array (Int range 1 .. 4096) of Entity_Id;
994 -- This array holds the list of defining identifiers. The upper bound
995 -- of 4096 is intended to be essentially infinite, and we do not even
996 -- bother to check for it being exceeded.
999 Specification_List := New_List;
1000 Specification_Loop : loop
1002 if Token = Tok_Pragma then
1003 P_Pragmas_Misplaced;
1006 Ignore (Tok_Left_Paren);
1007 Ident_Sloc := Token_Ptr;
1008 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1012 exit Ident_Loop when Token = Tok_Colon;
1014 -- The only valid tokens are colon and comma, so if we have
1015 -- neither do a bit of investigation to see which is the
1016 -- better choice for insertion.
1018 if Token /= Tok_Comma then
1020 -- Assume colon if IN or OUT keyword found
1022 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1024 -- Otherwise scan ahead
1026 Save_Scan_State (Scan_State);
1029 -- If we run into a semicolon, then assume that a
1030 -- colon was missing, e.g. Parms (X Y; ...). Also
1031 -- assume missing colon on EOF (a real disaster!)
1032 -- and on a right paren, e.g. Parms (X Y), and also
1033 -- on an assignment symbol, e.g. Parms (X Y := ..)
1035 if Token = Tok_Semicolon
1036 or else Token = Tok_Right_Paren
1037 or else Token = Tok_EOF
1038 or else Token = Tok_Colon_Equal
1040 Restore_Scan_State (Scan_State);
1043 -- If we run into a colon, assume that we had a missing
1044 -- comma, e.g. Parms (A B : ...). Also assume a missing
1045 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1047 elsif Token = Tok_Colon
1048 or else Token = Tok_Comma
1050 Restore_Scan_State (Scan_State);
1055 end loop Look_Ahead;
1058 -- Here if a comma is present, or to be assumed
1061 Num_Idents := Num_Idents + 1;
1062 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1063 end loop Ident_Loop;
1065 -- Fall through the loop on encountering a colon, or deciding
1066 -- that there is a missing colon.
1070 -- If there are multiple identifiers, we repeatedly scan the
1071 -- type and initialization expression information by resetting
1072 -- the scan pointer (so that we get completely separate trees
1073 -- for each occurrence).
1075 if Num_Idents > 1 then
1076 Save_Scan_State (Scan_State);
1079 -- Loop through defining identifiers in list
1083 Ident_List_Loop : loop
1084 Specification_Node :=
1085 New_Node (N_Parameter_Specification, Ident_Sloc);
1086 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1088 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1090 Not_Null_Sloc := Token_Ptr;
1092 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1094 -- Case of ACCESS keyword present
1096 if Token = Tok_Access then
1097 Set_Null_Exclusion_Present
1098 (Specification_Node, Not_Null_Present);
1100 if Ada_Version = Ada_83 then
1101 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1105 (Specification_Node,
1106 P_Access_Definition (Not_Null_Present));
1108 -- Case of IN or OUT present
1111 if Token = Tok_In or else Token = Tok_Out then
1112 if Not_Null_Present then
1114 ("`NOT NULL` can only be used with `ACCESS`",
1117 if Token = Tok_In then
1119 ("\`IN` not allowed together with `ACCESS`",
1123 ("\`OUT` not allowed together with `ACCESS`",
1128 P_Mode (Specification_Node);
1129 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1132 Set_Null_Exclusion_Present
1133 (Specification_Node, Not_Null_Present);
1135 if Token = Tok_Procedure
1137 Token = Tok_Function
1139 Error_Msg_SC ("formal subprogram parameter not allowed");
1142 if Token = Tok_Left_Paren then
1143 Discard_Junk_List (P_Formal_Part);
1146 if Token = Tok_Return then
1148 Discard_Junk_Node (P_Subtype_Mark);
1151 Set_Parameter_Type (Specification_Node, Error);
1154 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1159 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1162 Set_Prev_Ids (Specification_Node, True);
1165 if Ident < Num_Idents then
1166 Set_More_Ids (Specification_Node, True);
1169 Append (Specification_Node, Specification_List);
1170 exit Ident_List_Loop when Ident = Num_Idents;
1172 Restore_Scan_State (Scan_State);
1173 end loop Ident_List_Loop;
1176 when Error_Resync =>
1177 Resync_Semicolon_List;
1180 if Token = Tok_Semicolon then
1181 Save_Scan_State (Scan_State);
1182 Scan; -- past semicolon
1184 -- If we have RETURN or IS after the semicolon, then assume
1185 -- that semicolon should have been a right parenthesis and exit
1187 if Token = Tok_Is or else Token = Tok_Return then
1188 Error_Msg_SP ("expected "")"" in place of "";""");
1189 exit Specification_Loop;
1192 -- If we have a declaration keyword after the semicolon, then
1193 -- assume we had a missing right parenthesis and terminate list
1195 if Token in Token_Class_Declk then
1196 Error_Msg_AP ("missing "")""");
1197 Restore_Scan_State (Scan_State);
1198 exit Specification_Loop;
1201 elsif Token = Tok_Right_Paren then
1202 Scan; -- past right paren
1203 exit Specification_Loop;
1205 -- Special check for common error of using comma instead of semicolon
1207 elsif Token = Tok_Comma then
1211 -- Special check for omitted separator
1213 elsif Token = Tok_Identifier then
1216 -- If nothing sensible, skip to next semicolon or right paren
1220 Resync_Semicolon_List;
1222 if Token = Tok_Semicolon then
1223 Scan; -- past semicolon
1226 exit Specification_Loop;
1229 end loop Specification_Loop;
1231 return Specification_List;
1234 ----------------------------------
1235 -- 6.1 Parameter Specification --
1236 ----------------------------------
1238 -- Parsed by P_Formal_Part (6.1)
1244 -- MODE ::= [in] | in out | out
1246 -- There is no explicit node in the tree for the Mode. Instead the
1247 -- In_Present and Out_Present flags are set in the parent node to
1248 -- record the presence of keywords specifying the mode.
1250 -- Error_Recovery: cannot raise Error_Resync
1252 procedure P_Mode (Node : Node_Id) is
1254 if Token = Tok_In then
1256 Set_In_Present (Node, True);
1258 if Style.Mode_In_Check and then Token /= Tok_Out then
1259 Error_Msg_SP ("(style) IN should be omitted");
1262 if Token = Tok_Access then
1263 Error_Msg_SP ("IN not allowed together with ACCESS");
1264 Scan; -- past ACCESS
1268 if Token = Tok_Out then
1270 Set_Out_Present (Node, True);
1273 if Token = Tok_In then
1274 Error_Msg_SC ("IN must preceed OUT in parameter mode");
1276 Set_In_Present (Node, True);
1280 --------------------------
1281 -- 6.3 Subprogram Body --
1282 --------------------------
1284 -- Parsed by P_Subprogram (6.1)
1286 -----------------------------------
1287 -- 6.4 Procedure Call Statement --
1288 -----------------------------------
1290 -- Parsed by P_Sequence_Of_Statements (5.1)
1292 ------------------------
1293 -- 6.4 Function Call --
1294 ------------------------
1296 -- Parsed by P_Call_Or_Name (4.1)
1298 --------------------------------
1299 -- 6.4 Actual Parameter Part --
1300 --------------------------------
1302 -- Parsed by P_Call_Or_Name (4.1)
1304 --------------------------------
1305 -- 6.4 Parameter Association --
1306 --------------------------------
1308 -- Parsed by P_Call_Or_Name (4.1)
1310 ------------------------------------
1311 -- 6.4 Explicit Actual Parameter --
1312 ------------------------------------
1314 -- Parsed by P_Call_Or_Name (4.1)
1316 ---------------------------
1317 -- 6.5 Return Statement --
1318 ---------------------------
1320 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1322 -- EXTENDED_RETURN_STATEMENT ::=
1323 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1324 -- [:= EXPRESSION] [do
1325 -- HANDLED_SEQUENCE_OF_STATEMENTS
1328 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1330 -- RETURN_STATEMENT ::= return [EXPRESSION];
1332 -- Error recovery: can raise Error_Resync
1334 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1336 -- Note: We don't need to check Ada_Version here, because this is
1337 -- only called in >= Ada 2005 cases anyway.
1339 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1342 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1344 if Token = Tok_Access then
1345 Set_Object_Definition
1346 (Decl_Node, P_Access_Definition (Not_Null_Present));
1348 Set_Object_Definition
1349 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1351 end P_Return_Subtype_Indication;
1353 -- Error recovery: can raise Error_Resync
1355 function P_Return_Object_Declaration return Node_Id is
1356 Return_Obj : Node_Id;
1357 Decl_Node : Node_Id;
1360 Return_Obj := Token_Node;
1361 Change_Identifier_To_Defining_Identifier (Return_Obj);
1362 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1363 Set_Defining_Identifier (Decl_Node, Return_Obj);
1365 Scan; -- past identifier
1368 -- First an error check, if we have two identifiers in a row, a likely
1369 -- possibility is that the first of the identifiers is an incorrectly
1370 -- spelled keyword. See similar check in P_Identifier_Declarations.
1372 if Token = Tok_Identifier then
1374 SS : Saved_Scan_State;
1378 Save_Scan_State (SS);
1379 Scan; -- past initial identifier
1380 I2 := (Token = Tok_Identifier);
1381 Restore_Scan_State (SS);
1385 (Bad_Spelling_Of (Tok_Access) or else
1386 Bad_Spelling_Of (Tok_Aliased) or else
1387 Bad_Spelling_Of (Tok_Constant))
1394 -- We allow "constant" here (as in "return Result : constant
1395 -- T..."). This is not in the latest RM, but the ARG is considering an
1396 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1398 if Token = Tok_Constant then
1399 Scan; -- past CONSTANT
1400 Set_Constant_Present (Decl_Node);
1402 if Token = Tok_Aliased then
1403 Error_Msg_SC ("ALIASED should be before CONSTANT");
1404 Scan; -- past ALIASED
1405 Set_Aliased_Present (Decl_Node);
1408 elsif Token = Tok_Aliased then
1409 Scan; -- past ALIASED
1410 Set_Aliased_Present (Decl_Node);
1412 if Token = Tok_Constant then
1413 Scan; -- past CONSTANT
1414 Set_Constant_Present (Decl_Node);
1418 P_Return_Subtype_Indication (Decl_Node);
1420 if Token = Tok_Colon_Equal then
1422 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1426 end P_Return_Object_Declaration;
1428 -- Error recovery: can raise Error_Resync
1430 function P_Return_Statement return Node_Id is
1431 -- The caller has checked that the initial token is RETURN
1433 function Is_Simple return Boolean;
1434 -- Scan state is just after RETURN (and is left that way).
1435 -- Determine whether this is a simple or extended return statement
1436 -- by looking ahead for "identifier :", which implies extended.
1442 function Is_Simple return Boolean is
1443 Scan_State : Saved_Scan_State;
1444 Result : Boolean := True;
1447 if Token = Tok_Identifier then
1448 Save_Scan_State (Scan_State); -- at identifier
1449 Scan; -- past identifier
1451 if Token = Tok_Colon then
1452 Result := False; -- It's an extended_return_statement.
1455 Restore_Scan_State (Scan_State); -- to identifier
1461 Return_Sloc : constant Source_Ptr := Token_Ptr;
1462 Return_Node : Node_Id;
1464 -- Start of processing for P_Return_Statement
1467 Scan; -- past RETURN
1469 -- Simple_return_statement, no expression, return an
1470 -- N_Simple_Return_Statement node with the expression field left Empty.
1472 if Token = Tok_Semicolon then
1474 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1479 -- Simple_return_statement with expression
1481 -- We avoid trying to scan an expression if we are at an
1482 -- expression terminator since in that case the best error
1483 -- message is probably that we have a missing semicolon.
1486 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1488 if Token not in Token_Class_Eterm then
1489 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1492 -- Extended_return_statement (Ada 2005 only -- AI-318):
1495 if Ada_Version < Ada_05 then
1497 (" extended_return_statement is an Ada 2005 extension");
1498 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1501 Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1502 Set_Return_Object_Declarations
1503 (Return_Node, New_List (P_Return_Object_Declaration));
1505 if Token = Tok_Do then
1507 Scope.Table (Scope.Last).Etyp := E_Return;
1508 Scope.Table (Scope.Last).Ecol := Start_Column;
1509 Scope.Table (Scope.Last).Sloc := Return_Sloc;
1512 Set_Handled_Statement_Sequence
1513 (Return_Node, P_Handled_Sequence_Of_Statements);
1516 -- Do we need to handle Error_Resync here???
1524 end P_Return_Statement;