1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 ("|extra "";"" ignored");
68 Scan; -- rescan past junk semicolon
70 Restore_Scan_State (Scan_State);
73 elsif Bad_Spelling_Of (Tok_Return) then
76 end Check_Junk_Semicolon_Before_Return;
78 -----------------------------------------------------
79 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
80 -----------------------------------------------------
82 -- This routine scans out a subprogram declaration, subprogram body,
83 -- subprogram renaming declaration or subprogram generic instantiation.
85 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
87 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
88 -- SUBPROGRAM_SPECIFICATION is abstract;
90 -- SUBPROGRAM_SPECIFICATION ::=
91 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
92 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
94 -- PARAMETER_PROFILE ::= [FORMAL_PART]
96 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
98 -- SUBPROGRAM_BODY ::=
99 -- SUBPROGRAM_SPECIFICATION is
102 -- HANDLED_SEQUENCE_OF_STATEMENTS
105 -- SUBPROGRAM_RENAMING_DECLARATION ::=
106 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
108 -- SUBPROGRAM_BODY_STUB ::=
109 -- SUBPROGRAM_SPECIFICATION is separate;
111 -- GENERIC_INSTANTIATION ::=
112 -- procedure DEFINING_PROGRAM_UNIT_NAME is
113 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
114 -- | function DEFINING_DESIGNATOR is
115 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
117 -- NULL_PROCEDURE_DECLARATION ::=
118 -- SUBPROGRAM_SPECIFICATION is null;
120 -- Null procedures are an Ada 2005 feature. A null procedure declaration
121 -- is classified as a basic declarative item, but it is parsed here, with
122 -- other subprogram constructs.
124 -- The value in Pf_Flags indicates which of these possible declarations
125 -- is acceptable to the caller:
127 -- Pf_Flags.Decl Set if declaration OK
128 -- Pf_Flags.Gins Set if generic instantiation OK
129 -- Pf_Flags.Pbod Set if proper body OK
130 -- Pf_Flags.Rnam Set if renaming declaration OK
131 -- Pf_Flags.Stub Set if body stub OK
133 -- If an inappropriate form is encountered, it is scanned out but an
134 -- error message indicating that it is appearing in an inappropriate
135 -- context is issued. The only possible values for Pf_Flags are those
136 -- defined as constants in the Par package.
138 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
139 -- NOT or OVERRIDING.
141 -- Error recovery: cannot raise Error_Resync
143 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
144 Specification_Node : Node_Id;
146 Fpart_List : List_Id;
147 Fpart_Sloc : Source_Ptr;
148 Result_Not_Null : Boolean := False;
149 Result_Node : Node_Id;
153 Rename_Node : Node_Id;
154 Absdec_Node : Node_Id;
156 Fproc_Sloc : Source_Ptr;
158 Scan_State : Saved_Scan_State;
160 -- Flags for optional overriding indication. Two flags are needed,
161 -- to distinguish positive and negative overriding indicators from
162 -- the absence of any indicator.
164 Is_Overriding : Boolean := False;
165 Not_Overriding : Boolean := False;
168 -- Set up scope stack entry. Note that the Labl field will be set later
170 SIS_Entry_Active := False;
171 SIS_Missing_Semicolon_Message := No_Error_Msg;
173 Scope.Table (Scope.Last).Sloc := Token_Ptr;
174 Scope.Table (Scope.Last).Etyp := E_Name;
175 Scope.Table (Scope.Last).Ecol := Start_Column;
176 Scope.Table (Scope.Last).Lreq := False;
178 -- Ada2005: scan leading NOT OVERRIDING indicator
180 if Token = Tok_Not then
183 if Token = Tok_Overriding then
184 Scan; -- past OVERRIDING
185 Not_Overriding := True;
187 -- Overriding keyword used in non Ada 2005 mode
189 elsif Token = Tok_Identifier
190 and then Token_Name = Name_Overriding
192 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
193 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
194 Scan; -- past Overriding
195 Not_Overriding := True;
198 Error_Msg_SC ("OVERRIDING expected!");
201 -- Ada 2005: scan leading OVERRIDING indicator
203 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
204 -- declaration circuit already gave an error message and changed the
205 -- token to Tok_Overriding.
207 elsif Token = Tok_Overriding then
208 Scan; -- past OVERRIDING
209 Is_Overriding := True;
212 if (Is_Overriding or else Not_Overriding) then
214 -- Note that if we are not in Ada_05 mode, error messages have
215 -- already been given, so no need to give another message here.
217 -- An overriding indicator is allowed for subprogram declarations,
218 -- bodies (including subunits), renamings, stubs, and
219 -- instantiations. The test against Pf_Decl_Pbod is added to account
220 -- for the case of subprograms declared in a protected type, where
221 -- only subprogram declarations and bodies can occur. The Pf_Pbod
222 -- case is for subunits.
224 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub
226 Pf_Flags /= Pf_Decl_Pbod
230 Error_Msg_SC ("overriding indicator not allowed here!");
232 elsif Token /= Tok_Function and then Token /= Tok_Procedure then
233 Error_Msg_SC -- CODEFIX
234 ("FUNCTION or PROCEDURE expected!");
238 Func := (Token = Tok_Function);
239 Fproc_Sloc := Token_Ptr;
240 Scan; -- past FUNCTION or PROCEDURE
245 Name_Node := P_Defining_Designator;
247 if Nkind (Name_Node) = N_Defining_Operator_Symbol
248 and then Scope.Last = 1
250 Error_Msg_SP ("operator symbol not allowed at library level");
251 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
253 -- Set name from file name, we need some junk name, and that's
254 -- as good as anything. This is only approximate, since we do
255 -- not do anything with non-standard name translations.
257 Get_Name_String (File_Name (Current_Source_File));
259 for J in 1 .. Name_Len loop
260 if Name_Buffer (J) = '.' then
266 Set_Chars (Name_Node, Name_Find);
267 Set_Error_Posted (Name_Node);
271 Name_Node := P_Defining_Program_Unit_Name;
274 Scope.Table (Scope.Last).Labl := Name_Node;
277 -- Deal with generic instantiation, the one case in which we do not
278 -- have a subprogram specification as part of whatever we are parsing
280 if Token = Tok_Is then
281 Save_Scan_State (Scan_State); -- at the IS
282 T_Is; -- checks for redundant IS
284 if Token = Tok_New then
285 if not Pf_Flags.Gins then
286 Error_Msg_SC ("generic instantiation not allowed here!");
292 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
293 Set_Name (Inst_Node, P_Function_Name);
295 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
296 Set_Name (Inst_Node, P_Qualified_Simple_Name);
299 Set_Defining_Unit_Name (Inst_Node, Name_Node);
300 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
302 Pop_Scope_Stack; -- Don't need scope stack entry in this case
304 if Is_Overriding then
305 Set_Must_Override (Inst_Node);
307 elsif Not_Overriding then
308 Set_Must_Not_Override (Inst_Node);
314 Restore_Scan_State (Scan_State); -- to the IS
318 -- If not a generic instantiation, then we definitely have a subprogram
319 -- specification (all possibilities at this stage include one here)
321 Fpart_Sloc := Token_Ptr;
323 Check_Misspelling_Of (Tok_Return);
325 -- Scan formal part. First a special error check. If we have an
326 -- identifier here, then we have a definite error. If this identifier
327 -- is on the same line as the designator, then we assume it is the
328 -- first formal after a missing left parenthesis
330 if Token = Tok_Identifier
331 and then not Token_Is_At_Start_Of_Line
333 T_Left_Paren; -- to generate message
334 Fpart_List := P_Formal_Part;
336 -- Otherwise scan out an optional formal part in the usual manner
339 Fpart_List := P_Parameter_Profile;
342 -- We treat what we have as a function specification if FUNCTION was
343 -- used, or if a RETURN is present. This gives better error recovery
344 -- since later RETURN statements will be valid in either case.
346 Check_Junk_Semicolon_Before_Return;
347 Result_Node := Error;
349 if Token = Tok_Return then
351 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
357 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
359 -- Ada 2005 (AI-318-02)
361 if Token = Tok_Access then
362 if Ada_Version < Ada_05 then
364 ("anonymous access result type is an Ada 2005 extension");
365 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
368 Result_Node := P_Access_Definition (Result_Not_Null);
371 Result_Node := P_Subtype_Mark;
377 Ignore (Tok_Right_Paren);
383 Specification_Node :=
384 New_Node (N_Function_Specification, Fproc_Sloc);
386 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
387 Set_Result_Definition (Specification_Node, Result_Node);
390 Specification_Node :=
391 New_Node (N_Procedure_Specification, Fproc_Sloc);
394 Set_Defining_Unit_Name (Specification_Node, Name_Node);
395 Set_Parameter_Specifications (Specification_Node, Fpart_List);
397 if Is_Overriding then
398 Set_Must_Override (Specification_Node);
400 elsif Not_Overriding then
401 Set_Must_Not_Override (Specification_Node);
404 -- Error check: barriers not allowed on protected functions/procedures
406 if Token = Tok_When then
408 Error_Msg_SC ("barrier not allowed on function, only on entry");
410 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
414 Discard_Junk_Node (P_Expression);
417 -- Deal with semicolon followed by IS. We want to treat this as IS
419 if Token = Tok_Semicolon then
420 Save_Scan_State (Scan_State);
421 Scan; -- past semicolon
423 if Token = Tok_Is then
424 Error_Msg_SP ("extra "";"" ignored");
426 Restore_Scan_State (Scan_State);
430 -- Deal with case of semicolon ending a subprogram declaration
432 if Token = Tok_Semicolon then
433 if not Pf_Flags.Decl then
437 Scan; -- past semicolon
439 -- If semicolon is immediately followed by IS, then ignore the
440 -- semicolon, and go process the body.
442 if Token = Tok_Is then
443 Error_Msg_SP ("|extra "";"" ignored");
444 T_Is; -- scan past IS
445 goto Subprogram_Body;
447 -- If BEGIN follows in an appropriate column, we immediately
448 -- commence the error action of assuming that the previous
449 -- subprogram declaration should have been a subprogram body,
450 -- i.e. that the terminating semicolon should have been IS.
452 elsif Token = Tok_Begin
453 and then Start_Column >= Scope.Table (Scope.Last).Ecol
455 Error_Msg_SP ("|"";"" should be IS!");
456 goto Subprogram_Body;
459 goto Subprogram_Declaration;
462 -- Case of not followed by semicolon
465 -- Subprogram renaming declaration case
467 Check_Misspelling_Of (Tok_Renames);
469 if Token = Tok_Renames then
470 if not Pf_Flags.Rnam then
471 Error_Msg_SC ("renaming declaration not allowed here!");
475 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
476 Scan; -- past RENAMES
477 Set_Name (Rename_Node, P_Name);
478 Set_Specification (Rename_Node, Specification_Node);
483 -- Case of IS following subprogram specification
485 elsif Token = Tok_Is then
486 T_Is; -- ignore redundant Is's
488 if Token_Name = Name_Abstract then
489 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
492 -- Deal nicely with (now obsolete) use of <> in place of abstract
494 if Token = Tok_Box then
495 Error_Msg_SC ("ABSTRACT expected");
496 Token := Tok_Abstract;
499 -- Abstract subprogram declaration case
501 if Token = Tok_Abstract then
503 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
504 Set_Specification (Absdec_Node, Specification_Node);
505 Pop_Scope_Stack; -- discard unneeded entry
506 Scan; -- past ABSTRACT
510 -- Ada 2005 (AI-248): Parse a null procedure declaration
512 elsif Token = Tok_Null then
513 if Ada_Version < Ada_05 then
514 Error_Msg_SP ("null procedures are an Ada 2005 extension");
515 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
521 Error_Msg_SP ("only procedures can be null");
523 Set_Null_Present (Specification_Node);
527 goto Subprogram_Declaration;
529 -- Check for IS NEW with Formal_Part present and handle nicely
531 elsif Token = Tok_New then
533 ("formal part not allowed in instantiation", Fpart_Sloc);
537 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
540 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
543 Set_Defining_Unit_Name (Inst_Node, Name_Node);
544 Set_Name (Inst_Node, P_Name);
545 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
547 Pop_Scope_Stack; -- Don't need scope stack entry in this case
551 goto Subprogram_Body;
554 -- Here we have a missing IS or missing semicolon, we always guess
555 -- a missing semicolon, since we are pretty good at fixing up a
556 -- semicolon which should really be an IS
559 Error_Msg_AP ("|missing "";""");
560 SIS_Missing_Semicolon_Message := Get_Msg_Id;
561 goto Subprogram_Declaration;
565 -- Processing for subprogram body
568 if not Pf_Flags.Pbod then
569 Error_Msg_SP ("subprogram body not allowed here!");
572 -- Subprogram body stub case
574 if Separate_Present then
575 if not Pf_Flags.Stub then
576 Error_Msg_SC ("body stub not allowed here!");
579 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
581 ("operator symbol cannot be used as subunit name",
586 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
587 Set_Specification (Stub_Node, Specification_Node);
588 Scan; -- past SEPARATE
593 -- Subprogram body case
596 -- Here is the test for a suspicious IS (i.e. one that looks
597 -- like it might more properly be a semicolon). See separate
598 -- section discussing use of IS instead of semicolon in
601 if (Token in Token_Class_Declk
603 Token = Tok_Identifier)
604 and then Start_Column <= Scope.Table (Scope.Last).Ecol
605 and then Scope.Last /= 1
607 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
608 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
612 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
613 Set_Specification (Body_Node, Specification_Node);
614 Parse_Decls_Begin_End (Body_Node);
618 -- Processing for subprogram declaration
620 <<Subprogram_Declaration>>
622 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
623 Set_Specification (Decl_Node, Specification_Node);
625 -- If this is a context in which a subprogram body is permitted,
626 -- set active SIS entry in case (see section titled "Handling
627 -- Semicolon Used in Place of IS" in body of Parser package)
628 -- Note that SIS_Missing_Semicolon_Message is already set properly.
630 if Pf_Flags.Pbod then
631 SIS_Labl := Scope.Table (Scope.Last).Labl;
632 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
633 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
634 SIS_Declaration_Node := Decl_Node;
635 SIS_Semicolon_Sloc := Prev_Token_Ptr;
636 SIS_Entry_Active := True;
644 ---------------------------------
645 -- 6.1 Subprogram Declaration --
646 ---------------------------------
648 -- Parsed by P_Subprogram (6.1)
650 ------------------------------------------
651 -- 6.1 Abstract Subprogram Declaration --
652 ------------------------------------------
654 -- Parsed by P_Subprogram (6.1)
656 -----------------------------------
657 -- 6.1 Subprogram Specification --
658 -----------------------------------
660 -- SUBPROGRAM_SPECIFICATION ::=
661 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
662 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
664 -- PARAMETER_PROFILE ::= [FORMAL_PART]
666 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
668 -- Subprogram specifications that appear in subprogram declarations
669 -- are parsed by P_Subprogram (6.1). This routine is used in other
670 -- contexts where subprogram specifications occur.
672 -- Note: this routine does not affect the scope stack in any way
674 -- Error recovery: can raise Error_Resync
676 function P_Subprogram_Specification return Node_Id is
677 Specification_Node : Node_Id;
678 Result_Not_Null : Boolean;
679 Result_Node : Node_Id;
682 if Token = Tok_Function then
683 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
684 Scan; -- past FUNCTION
686 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
687 Set_Parameter_Specifications
688 (Specification_Node, P_Parameter_Profile);
689 Check_Junk_Semicolon_Before_Return;
692 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
694 -- Ada 2005 (AI-318-02)
696 if Token = Tok_Access then
697 if Ada_Version < Ada_05 then
699 ("anonymous access result type is an Ada 2005 extension");
700 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
703 Result_Node := P_Access_Definition (Result_Not_Null);
706 Result_Node := P_Subtype_Mark;
710 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
711 Set_Result_Definition (Specification_Node, Result_Node);
712 return Specification_Node;
714 elsif Token = Tok_Procedure then
715 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
716 Scan; -- past PROCEDURE
718 Set_Defining_Unit_Name
719 (Specification_Node, P_Defining_Program_Unit_Name);
720 Set_Parameter_Specifications
721 (Specification_Node, P_Parameter_Profile);
722 return Specification_Node;
725 Error_Msg_SC ("subprogram specification expected");
728 end P_Subprogram_Specification;
730 ---------------------
732 ---------------------
735 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
737 -- The caller has checked that the initial token is an identifier,
738 -- operator symbol, or string literal. Note that we don't bother to
739 -- do much error diagnosis in this routine, since it is only used for
740 -- the label on END lines, and the routines in package Par.Endh will
741 -- check that the label is appropriate.
743 -- Error recovery: cannot raise Error_Resync
745 function P_Designator return Node_Id is
746 Ident_Node : Node_Id;
748 Prefix_Node : Node_Id;
750 function Real_Dot return Boolean;
751 -- Tests if a current token is an interesting period, i.e. is followed
752 -- by an identifier or operator symbol or string literal. If not, it is
753 -- probably just incorrect punctuation to be caught by our caller. Note
754 -- that the case of an operator symbol or string literal is also an
755 -- error, but that is an error that we catch here. If the result is
756 -- True, a real dot has been scanned and we are positioned past it,
757 -- if the result is False, the scan position is unchanged.
763 function Real_Dot return Boolean is
764 Scan_State : Saved_Scan_State;
767 if Token /= Tok_Dot then
771 Save_Scan_State (Scan_State);
774 if Token = Tok_Identifier
775 or else Token = Tok_Operator_Symbol
776 or else Token = Tok_String_Literal
781 Restore_Scan_State (Scan_State);
787 -- Start of processing for P_Designator
790 Ident_Node := Token_Node;
791 Scan; -- past initial token
793 if Prev_Token = Tok_Operator_Symbol
794 or else Prev_Token = Tok_String_Literal
802 Prefix_Node := Ident_Node;
804 -- Loop through child names, on entry to this loop, Prefix contains
805 -- the name scanned so far, and Ident_Node is the last identifier.
808 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
809 Set_Prefix (Name_Node, Prefix_Node);
810 Ident_Node := P_Identifier;
811 Set_Selector_Name (Name_Node, Ident_Node);
812 Prefix_Node := Name_Node;
813 exit when not Real_Dot;
816 -- On exit from the loop, Ident_Node is the last identifier scanned,
817 -- i.e. the defining identifier, and Prefix_Node is a node for the
818 -- entire name, structured (incorrectly!) as a selected component.
820 Name_Node := Prefix (Prefix_Node);
821 Change_Node (Prefix_Node, N_Designator);
822 Set_Name (Prefix_Node, Name_Node);
823 Set_Identifier (Prefix_Node, Ident_Node);
829 while Token = Tok_Dot or else Token = Tok_Identifier loop
836 ------------------------------
837 -- 6.1 Defining Designator --
838 ------------------------------
840 -- DEFINING_DESIGNATOR ::=
841 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
843 -- Error recovery: cannot raise Error_Resync
845 function P_Defining_Designator return Node_Id is
847 if Token = Tok_Operator_Symbol then
848 return P_Defining_Operator_Symbol;
850 elsif Token = Tok_String_Literal then
851 Error_Msg_SC ("invalid operator name");
852 Scan; -- past junk string
856 return P_Defining_Program_Unit_Name;
858 end P_Defining_Designator;
860 -------------------------------------
861 -- 6.1 Defining Program Unit Name --
862 -------------------------------------
864 -- DEFINING_PROGRAM_UNIT_NAME ::=
865 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
867 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
869 -- Error recovery: cannot raise Error_Resync
871 function P_Defining_Program_Unit_Name return Node_Id is
872 Ident_Node : Node_Id;
874 Prefix_Node : Node_Id;
877 -- Set identifier casing if not already set and scan initial identifier
879 if Token = Tok_Identifier
880 and then Identifier_Casing (Current_Source_File) = Unknown
882 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
885 Ident_Node := P_Identifier (C_Dot);
886 Merge_Identifier (Ident_Node, Tok_Return);
888 -- Normal case (not child library unit name)
890 if Token /= Tok_Dot then
891 Change_Identifier_To_Defining_Identifier (Ident_Node);
894 -- Child library unit name case
897 if Scope.Last > 1 then
898 Error_Msg_SP ("child unit allowed only at library level");
901 elsif Ada_Version = Ada_83 then
902 Error_Msg_SP ("(Ada 83) child unit not allowed!");
906 Prefix_Node := Ident_Node;
908 -- Loop through child names, on entry to this loop, Prefix contains
909 -- the name scanned so far, and Ident_Node is the last identifier.
912 exit when Token /= Tok_Dot;
913 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
915 Set_Prefix (Name_Node, Prefix_Node);
916 Ident_Node := P_Identifier (C_Dot);
917 Set_Selector_Name (Name_Node, Ident_Node);
918 Prefix_Node := Name_Node;
921 -- On exit from the loop, Ident_Node is the last identifier scanned,
922 -- i.e. the defining identifier, and Prefix_Node is a node for the
923 -- entire name, structured (incorrectly!) as a selected component.
925 Name_Node := Prefix (Prefix_Node);
926 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
927 Set_Name (Prefix_Node, Name_Node);
928 Change_Identifier_To_Defining_Identifier (Ident_Node);
929 Set_Defining_Identifier (Prefix_Node, Ident_Node);
931 -- All set with unit name parsed
938 while Token = Tok_Dot or else Token = Tok_Identifier loop
943 end P_Defining_Program_Unit_Name;
945 --------------------------
946 -- 6.1 Operator Symbol --
947 --------------------------
949 -- OPERATOR_SYMBOL ::= STRING_LITERAL
951 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
953 -----------------------------------
954 -- 6.1 Defining Operator Symbol --
955 -----------------------------------
957 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
959 -- The caller has checked that the initial symbol is an operator symbol
961 function P_Defining_Operator_Symbol return Node_Id is
965 Op_Node := Token_Node;
966 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
967 Scan; -- past operator symbol
969 end P_Defining_Operator_Symbol;
971 ----------------------------
972 -- 6.1 Parameter_Profile --
973 ----------------------------
975 -- PARAMETER_PROFILE ::= [FORMAL_PART]
977 -- Empty is returned if no formal part is present
979 -- Error recovery: cannot raise Error_Resync
981 function P_Parameter_Profile return List_Id is
983 if Token = Tok_Left_Paren then
984 Scan; -- part left paren
985 return P_Formal_Part;
989 end P_Parameter_Profile;
991 ---------------------------------------
992 -- 6.1 Parameter And Result Profile --
993 ---------------------------------------
995 -- Parsed by its parent construct, which uses P_Parameter_Profile to
996 -- parse the parameters, and P_Subtype_Mark to parse the return type.
998 ----------------------
999 -- 6.1 Formal part --
1000 ----------------------
1002 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1004 -- PARAMETER_SPECIFICATION ::=
1005 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
1006 -- [:= DEFAULT_EXPRESSION]
1007 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1008 -- [:= DEFAULT_EXPRESSION]
1010 -- This scans the construct Formal_Part. The caller has already checked
1011 -- that the initial token is a left parenthesis, and skipped past it, so
1012 -- that on entry Token is the first token following the left parenthesis.
1014 -- Error recovery: cannot raise Error_Resync
1016 function P_Formal_Part return List_Id is
1017 Specification_List : List_Id;
1018 Specification_Node : Node_Id;
1019 Scan_State : Saved_Scan_State;
1022 Ident_Sloc : Source_Ptr;
1023 Not_Null_Present : Boolean := False;
1024 Not_Null_Sloc : Source_Ptr;
1026 Idents : array (Int range 1 .. 4096) of Entity_Id;
1027 -- This array holds the list of defining identifiers. The upper bound
1028 -- of 4096 is intended to be essentially infinite, and we do not even
1029 -- bother to check for it being exceeded.
1032 Specification_List := New_List;
1033 Specification_Loop : loop
1035 if Token = Tok_Pragma then
1036 Error_Msg_SC ("pragma not allowed in formal part");
1037 Discard_Junk_Node (P_Pragma (Skipping => True));
1040 Ignore (Tok_Left_Paren);
1041 Ident_Sloc := Token_Ptr;
1042 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1046 exit Ident_Loop when Token = Tok_Colon;
1048 -- The only valid tokens are colon and comma, so if we have
1049 -- neither do a bit of investigation to see which is the
1050 -- better choice for insertion.
1052 if Token /= Tok_Comma then
1054 -- Assume colon if IN or OUT keyword found
1056 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1058 -- Otherwise scan ahead
1060 Save_Scan_State (Scan_State);
1063 -- If we run into a semicolon, then assume that a
1064 -- colon was missing, e.g. Parms (X Y; ...). Also
1065 -- assume missing colon on EOF (a real disaster!)
1066 -- and on a right paren, e.g. Parms (X Y), and also
1067 -- on an assignment symbol, e.g. Parms (X Y := ..)
1069 if Token = Tok_Semicolon
1070 or else Token = Tok_Right_Paren
1071 or else Token = Tok_EOF
1072 or else Token = Tok_Colon_Equal
1074 Restore_Scan_State (Scan_State);
1077 -- If we run into a colon, assume that we had a missing
1078 -- comma, e.g. Parms (A B : ...). Also assume a missing
1079 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1081 elsif Token = Tok_Colon
1082 or else Token = Tok_Comma
1084 Restore_Scan_State (Scan_State);
1089 end loop Look_Ahead;
1092 -- Here if a comma is present, or to be assumed
1095 Num_Idents := Num_Idents + 1;
1096 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1097 end loop Ident_Loop;
1099 -- Fall through the loop on encountering a colon, or deciding
1100 -- that there is a missing colon.
1104 -- If there are multiple identifiers, we repeatedly scan the
1105 -- type and initialization expression information by resetting
1106 -- the scan pointer (so that we get completely separate trees
1107 -- for each occurrence).
1109 if Num_Idents > 1 then
1110 Save_Scan_State (Scan_State);
1113 -- Loop through defining identifiers in list
1117 Ident_List_Loop : loop
1118 Specification_Node :=
1119 New_Node (N_Parameter_Specification, Ident_Sloc);
1120 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1122 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1124 Not_Null_Sloc := Token_Ptr;
1126 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1128 -- Case of ACCESS keyword present
1130 if Token = Tok_Access then
1131 Set_Null_Exclusion_Present
1132 (Specification_Node, Not_Null_Present);
1134 if Ada_Version = Ada_83 then
1135 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1139 (Specification_Node,
1140 P_Access_Definition (Not_Null_Present));
1142 -- Case of IN or OUT present
1145 if Token = Tok_In or else Token = Tok_Out then
1146 if Not_Null_Present then
1148 ("`NOT NULL` can only be used with `ACCESS`",
1151 if Token = Tok_In then
1153 ("\`IN` not allowed together with `ACCESS`",
1157 ("\`OUT` not allowed together with `ACCESS`",
1162 P_Mode (Specification_Node);
1163 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1166 Set_Null_Exclusion_Present
1167 (Specification_Node, Not_Null_Present);
1169 if Token = Tok_Procedure
1171 Token = Tok_Function
1173 Error_Msg_SC ("formal subprogram parameter not allowed");
1176 if Token = Tok_Left_Paren then
1177 Discard_Junk_List (P_Formal_Part);
1180 if Token = Tok_Return then
1182 Discard_Junk_Node (P_Subtype_Mark);
1185 Set_Parameter_Type (Specification_Node, Error);
1188 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1193 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1196 Set_Prev_Ids (Specification_Node, True);
1199 if Ident < Num_Idents then
1200 Set_More_Ids (Specification_Node, True);
1203 Append (Specification_Node, Specification_List);
1204 exit Ident_List_Loop when Ident = Num_Idents;
1206 Restore_Scan_State (Scan_State);
1207 end loop Ident_List_Loop;
1210 when Error_Resync =>
1211 Resync_Semicolon_List;
1214 if Token = Tok_Semicolon then
1215 Save_Scan_State (Scan_State);
1216 Scan; -- past semicolon
1218 -- If we have RETURN or IS after the semicolon, then assume
1219 -- that semicolon should have been a right parenthesis and exit
1221 if Token = Tok_Is or else Token = Tok_Return then
1222 Error_Msg_SP ("|"";"" should be "")""");
1223 exit Specification_Loop;
1226 -- If we have a declaration keyword after the semicolon, then
1227 -- assume we had a missing right parenthesis and terminate list
1229 if Token in Token_Class_Declk then
1230 Error_Msg_AP ("missing "")""");
1231 Restore_Scan_State (Scan_State);
1232 exit Specification_Loop;
1235 elsif Token = Tok_Right_Paren then
1236 Scan; -- past right paren
1237 exit Specification_Loop;
1239 -- Special check for common error of using comma instead of semicolon
1241 elsif Token = Tok_Comma then
1245 -- Special check for omitted separator
1247 elsif Token = Tok_Identifier then
1250 -- If nothing sensible, skip to next semicolon or right paren
1254 Resync_Semicolon_List;
1256 if Token = Tok_Semicolon then
1257 Scan; -- past semicolon
1260 exit Specification_Loop;
1263 end loop Specification_Loop;
1265 return Specification_List;
1268 ----------------------------------
1269 -- 6.1 Parameter Specification --
1270 ----------------------------------
1272 -- Parsed by P_Formal_Part (6.1)
1278 -- MODE ::= [in] | in out | out
1280 -- There is no explicit node in the tree for the Mode. Instead the
1281 -- In_Present and Out_Present flags are set in the parent node to
1282 -- record the presence of keywords specifying the mode.
1284 -- Error_Recovery: cannot raise Error_Resync
1286 procedure P_Mode (Node : Node_Id) is
1288 if Token = Tok_In then
1290 Set_In_Present (Node, True);
1292 if Style.Mode_In_Check and then Token /= Tok_Out then
1293 Error_Msg_SP ("(style) IN should be omitted");
1296 if Token = Tok_Access then
1297 Error_Msg_SP ("IN not allowed together with ACCESS");
1298 Scan; -- past ACCESS
1302 if Token = Tok_Out then
1304 Set_Out_Present (Node, True);
1307 if Token = Tok_In then
1308 Error_Msg_SC -- CODEFIX ???
1309 ("IN must precede OUT in parameter mode");
1311 Set_In_Present (Node, True);
1315 --------------------------
1316 -- 6.3 Subprogram Body --
1317 --------------------------
1319 -- Parsed by P_Subprogram (6.1)
1321 -----------------------------------
1322 -- 6.4 Procedure Call Statement --
1323 -----------------------------------
1325 -- Parsed by P_Sequence_Of_Statements (5.1)
1327 ------------------------
1328 -- 6.4 Function Call --
1329 ------------------------
1331 -- Parsed by P_Call_Or_Name (4.1)
1333 --------------------------------
1334 -- 6.4 Actual Parameter Part --
1335 --------------------------------
1337 -- Parsed by P_Call_Or_Name (4.1)
1339 --------------------------------
1340 -- 6.4 Parameter Association --
1341 --------------------------------
1343 -- Parsed by P_Call_Or_Name (4.1)
1345 ------------------------------------
1346 -- 6.4 Explicit Actual Parameter --
1347 ------------------------------------
1349 -- Parsed by P_Call_Or_Name (4.1)
1351 ---------------------------
1352 -- 6.5 Return Statement --
1353 ---------------------------
1355 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1357 -- EXTENDED_RETURN_STATEMENT ::=
1358 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1359 -- [:= EXPRESSION] [do
1360 -- HANDLED_SEQUENCE_OF_STATEMENTS
1363 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1365 -- RETURN_STATEMENT ::= return [EXPRESSION];
1367 -- Error recovery: can raise Error_Resync
1369 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1371 -- Note: We don't need to check Ada_Version here, because this is
1372 -- only called in >= Ada 2005 cases anyway.
1374 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1377 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1379 if Token = Tok_Access then
1380 Set_Object_Definition
1381 (Decl_Node, P_Access_Definition (Not_Null_Present));
1383 Set_Object_Definition
1384 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1386 end P_Return_Subtype_Indication;
1388 -- Error recovery: can raise Error_Resync
1390 function P_Return_Object_Declaration return Node_Id is
1391 Return_Obj : Node_Id;
1392 Decl_Node : Node_Id;
1395 Return_Obj := Token_Node;
1396 Change_Identifier_To_Defining_Identifier (Return_Obj);
1397 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1398 Set_Defining_Identifier (Decl_Node, Return_Obj);
1400 Scan; -- past identifier
1403 -- First an error check, if we have two identifiers in a row, a likely
1404 -- possibility is that the first of the identifiers is an incorrectly
1405 -- spelled keyword. See similar check in P_Identifier_Declarations.
1407 if Token = Tok_Identifier then
1409 SS : Saved_Scan_State;
1413 Save_Scan_State (SS);
1414 Scan; -- past initial identifier
1415 I2 := (Token = Tok_Identifier);
1416 Restore_Scan_State (SS);
1420 (Bad_Spelling_Of (Tok_Access) or else
1421 Bad_Spelling_Of (Tok_Aliased) or else
1422 Bad_Spelling_Of (Tok_Constant))
1429 -- We allow "constant" here (as in "return Result : constant
1430 -- T..."). This is not in the latest RM, but the ARG is considering an
1431 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1433 if Token = Tok_Constant then
1434 Scan; -- past CONSTANT
1435 Set_Constant_Present (Decl_Node);
1437 if Token = Tok_Aliased then
1438 Error_Msg_SC -- CODEFIX
1439 ("ALIASED should be before CONSTANT");
1440 Scan; -- past ALIASED
1441 Set_Aliased_Present (Decl_Node);
1444 elsif Token = Tok_Aliased then
1445 Scan; -- past ALIASED
1446 Set_Aliased_Present (Decl_Node);
1448 if Token = Tok_Constant then
1449 Scan; -- past CONSTANT
1450 Set_Constant_Present (Decl_Node);
1454 P_Return_Subtype_Indication (Decl_Node);
1456 if Token = Tok_Colon_Equal then
1458 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1462 end P_Return_Object_Declaration;
1464 -- Error recovery: can raise Error_Resync
1466 function P_Return_Statement return Node_Id is
1467 -- The caller has checked that the initial token is RETURN
1469 function Is_Simple return Boolean;
1470 -- Scan state is just after RETURN (and is left that way).
1471 -- Determine whether this is a simple or extended return statement
1472 -- by looking ahead for "identifier :", which implies extended.
1478 function Is_Simple return Boolean is
1479 Scan_State : Saved_Scan_State;
1480 Result : Boolean := True;
1483 if Token = Tok_Identifier then
1484 Save_Scan_State (Scan_State); -- at identifier
1485 Scan; -- past identifier
1487 if Token = Tok_Colon then
1488 Result := False; -- It's an extended_return_statement.
1491 Restore_Scan_State (Scan_State); -- to identifier
1497 Return_Sloc : constant Source_Ptr := Token_Ptr;
1498 Return_Node : Node_Id;
1500 -- Start of processing for P_Return_Statement
1503 Scan; -- past RETURN
1505 -- Simple_return_statement, no expression, return an
1506 -- N_Simple_Return_Statement node with the expression field left Empty.
1508 if Token = Tok_Semicolon then
1510 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1515 -- Simple_return_statement with expression
1517 -- We avoid trying to scan an expression if we are at an
1518 -- expression terminator since in that case the best error
1519 -- message is probably that we have a missing semicolon.
1522 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1524 if Token not in Token_Class_Eterm then
1525 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1528 -- Extended_return_statement (Ada 2005 only -- AI-318):
1531 if Ada_Version < Ada_05 then
1533 (" extended_return_statement is an Ada 2005 extension");
1534 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1537 Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1538 Set_Return_Object_Declarations
1539 (Return_Node, New_List (P_Return_Object_Declaration));
1541 if Token = Tok_Do then
1543 Scope.Table (Scope.Last).Etyp := E_Return;
1544 Scope.Table (Scope.Last).Ecol := Start_Column;
1545 Scope.Table (Scope.Last).Sloc := Return_Sloc;
1548 Set_Handled_Statement_Sequence
1549 (Return_Node, P_Handled_Sequence_Of_Statements);
1552 -- Do we need to handle Error_Resync here???
1560 end P_Return_Statement;