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 ("|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, renamings, stubs, and instantiations.
220 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub then
221 Error_Msg_SC ("overriding indicator not allowed here!");
223 elsif Token /= Tok_Function
224 and then Token /= Tok_Procedure
226 Error_Msg_SC ("FUNCTION or PROCEDURE expected!");
230 Func := (Token = Tok_Function);
231 Fproc_Sloc := Token_Ptr;
232 Scan; -- past FUNCTION or PROCEDURE
237 Name_Node := P_Defining_Designator;
239 if Nkind (Name_Node) = N_Defining_Operator_Symbol
240 and then Scope.Last = 1
242 Error_Msg_SP ("operator symbol not allowed at library level");
243 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
245 -- Set name from file name, we need some junk name, and that's
246 -- as good as anything. This is only approximate, since we do
247 -- not do anything with non-standard name translations.
249 Get_Name_String (File_Name (Current_Source_File));
251 for J in 1 .. Name_Len loop
252 if Name_Buffer (J) = '.' then
258 Set_Chars (Name_Node, Name_Find);
259 Set_Error_Posted (Name_Node);
263 Name_Node := P_Defining_Program_Unit_Name;
266 Scope.Table (Scope.Last).Labl := Name_Node;
269 -- Deal with generic instantiation, the one case in which we do not
270 -- have a subprogram specification as part of whatever we are parsing
272 if Token = Tok_Is then
273 Save_Scan_State (Scan_State); -- at the IS
274 T_Is; -- checks for redundant IS
276 if Token = Tok_New then
277 if not Pf_Flags.Gins then
278 Error_Msg_SC ("generic instantiation not allowed here!");
284 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
285 Set_Name (Inst_Node, P_Function_Name);
287 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
288 Set_Name (Inst_Node, P_Qualified_Simple_Name);
291 Set_Defining_Unit_Name (Inst_Node, Name_Node);
292 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
294 Pop_Scope_Stack; -- Don't need scope stack entry in this case
296 if Is_Overriding then
297 Set_Must_Override (Inst_Node);
299 elsif Not_Overriding then
300 Set_Must_Not_Override (Inst_Node);
306 Restore_Scan_State (Scan_State); -- to the IS
310 -- If not a generic instantiation, then we definitely have a subprogram
311 -- specification (all possibilities at this stage include one here)
313 Fpart_Sloc := Token_Ptr;
315 Check_Misspelling_Of (Tok_Return);
317 -- Scan formal part. First a special error check. If we have an
318 -- identifier here, then we have a definite error. If this identifier
319 -- is on the same line as the designator, then we assume it is the
320 -- first formal after a missing left parenthesis
322 if Token = Tok_Identifier
323 and then not Token_Is_At_Start_Of_Line
325 T_Left_Paren; -- to generate message
326 Fpart_List := P_Formal_Part;
328 -- Otherwise scan out an optional formal part in the usual manner
331 Fpart_List := P_Parameter_Profile;
334 -- We treat what we have as a function specification if FUNCTION was
335 -- used, or if a RETURN is present. This gives better error recovery
336 -- since later RETURN statements will be valid in either case.
338 Check_Junk_Semicolon_Before_Return;
339 Result_Node := Error;
341 if Token = Tok_Return then
343 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
349 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
351 -- Ada 2005 (AI-318-02)
353 if Token = Tok_Access then
354 if Ada_Version < Ada_05 then
356 ("anonymous access result type is an Ada 2005 extension");
357 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
360 Result_Node := P_Access_Definition (Result_Not_Null);
363 Result_Node := P_Subtype_Mark;
369 Ignore (Tok_Right_Paren);
375 Specification_Node :=
376 New_Node (N_Function_Specification, Fproc_Sloc);
378 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
379 Set_Result_Definition (Specification_Node, Result_Node);
382 Specification_Node :=
383 New_Node (N_Procedure_Specification, Fproc_Sloc);
386 Set_Defining_Unit_Name (Specification_Node, Name_Node);
387 Set_Parameter_Specifications (Specification_Node, Fpart_List);
389 if Is_Overriding then
390 Set_Must_Override (Specification_Node);
392 elsif Not_Overriding then
393 Set_Must_Not_Override (Specification_Node);
396 -- Error check: barriers not allowed on protected functions/procedures
398 if Token = Tok_When then
400 Error_Msg_SC ("barrier not allowed on function, only on entry");
402 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
406 Discard_Junk_Node (P_Expression);
409 -- Deal with semicolon followed by IS. We want to treat this as IS
411 if Token = Tok_Semicolon then
412 Save_Scan_State (Scan_State);
413 Scan; -- past semicolon
415 if Token = Tok_Is then
416 Error_Msg_SP ("extra "";"" ignored");
418 Restore_Scan_State (Scan_State);
422 -- Deal with case of semicolon ending a subprogram declaration
424 if Token = Tok_Semicolon then
425 if not Pf_Flags.Decl then
429 Scan; -- past semicolon
431 -- If semicolon is immediately followed by IS, then ignore the
432 -- semicolon, and go process the body.
434 if Token = Tok_Is then
435 Error_Msg_SP ("|extra "";"" ignored");
436 T_Is; -- scan past IS
437 goto Subprogram_Body;
439 -- If BEGIN follows in an appropriate column, we immediately
440 -- commence the error action of assuming that the previous
441 -- subprogram declaration should have been a subprogram body,
442 -- i.e. that the terminating semicolon should have been IS.
444 elsif Token = Tok_Begin
445 and then Start_Column >= Scope.Table (Scope.Last).Ecol
447 Error_Msg_SP ("|"";"" should be IS!");
448 goto Subprogram_Body;
451 goto Subprogram_Declaration;
454 -- Case of not followed by semicolon
457 -- Subprogram renaming declaration case
459 Check_Misspelling_Of (Tok_Renames);
461 if Token = Tok_Renames then
462 if not Pf_Flags.Rnam then
463 Error_Msg_SC ("renaming declaration not allowed here!");
467 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
468 Scan; -- past RENAMES
469 Set_Name (Rename_Node, P_Name);
470 Set_Specification (Rename_Node, Specification_Node);
475 -- Case of IS following subprogram specification
477 elsif Token = Tok_Is then
478 T_Is; -- ignore redundant Is's
480 if Token_Name = Name_Abstract then
481 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
484 -- Deal nicely with (now obsolete) use of <> in place of abstract
486 if Token = Tok_Box then
487 Error_Msg_SC ("ABSTRACT expected");
488 Token := Tok_Abstract;
491 -- Abstract subprogram declaration case
493 if Token = Tok_Abstract then
495 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
496 Set_Specification (Absdec_Node, Specification_Node);
497 Pop_Scope_Stack; -- discard unneeded entry
498 Scan; -- past ABSTRACT
502 -- Ada 2005 (AI-248): Parse a null procedure declaration
504 elsif Token = Tok_Null then
505 if Ada_Version < Ada_05 then
506 Error_Msg_SP ("null procedures are an Ada 2005 extension");
507 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
513 Error_Msg_SP ("only procedures can be null");
515 Set_Null_Present (Specification_Node);
519 goto Subprogram_Declaration;
521 -- Check for IS NEW with Formal_Part present and handle nicely
523 elsif Token = Tok_New then
525 ("formal part not allowed in instantiation", Fpart_Sloc);
529 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
532 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
535 Set_Defining_Unit_Name (Inst_Node, Name_Node);
536 Set_Name (Inst_Node, P_Name);
537 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
539 Pop_Scope_Stack; -- Don't need scope stack entry in this case
543 goto Subprogram_Body;
546 -- Here we have a missing IS or missing semicolon, we always guess
547 -- a missing semicolon, since we are pretty good at fixing up a
548 -- semicolon which should really be an IS
551 Error_Msg_AP ("|missing "";""");
552 SIS_Missing_Semicolon_Message := Get_Msg_Id;
553 goto Subprogram_Declaration;
557 -- Processing for subprogram body
560 if not Pf_Flags.Pbod then
561 Error_Msg_SP ("subprogram body not allowed here!");
564 -- Subprogram body stub case
566 if Separate_Present then
567 if not Pf_Flags.Stub then
568 Error_Msg_SC ("body stub not allowed here!");
571 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
573 ("operator symbol cannot be used as subunit name",
578 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
579 Set_Specification (Stub_Node, Specification_Node);
580 Scan; -- past SEPARATE
585 -- Subprogram body case
588 -- Here is the test for a suspicious IS (i.e. one that looks
589 -- like it might more properly be a semicolon). See separate
590 -- section discussing use of IS instead of semicolon in
593 if (Token in Token_Class_Declk
595 Token = Tok_Identifier)
596 and then Start_Column <= Scope.Table (Scope.Last).Ecol
597 and then Scope.Last /= 1
599 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
600 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
604 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
605 Set_Specification (Body_Node, Specification_Node);
606 Parse_Decls_Begin_End (Body_Node);
610 -- Processing for subprogram declaration
612 <<Subprogram_Declaration>>
614 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
615 Set_Specification (Decl_Node, Specification_Node);
617 -- If this is a context in which a subprogram body is permitted,
618 -- set active SIS entry in case (see section titled "Handling
619 -- Semicolon Used in Place of IS" in body of Parser package)
620 -- Note that SIS_Missing_Semicolon_Message is already set properly.
622 if Pf_Flags.Pbod then
623 SIS_Labl := Scope.Table (Scope.Last).Labl;
624 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
625 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
626 SIS_Declaration_Node := Decl_Node;
627 SIS_Semicolon_Sloc := Prev_Token_Ptr;
628 SIS_Entry_Active := True;
636 ---------------------------------
637 -- 6.1 Subprogram Declaration --
638 ---------------------------------
640 -- Parsed by P_Subprogram (6.1)
642 ------------------------------------------
643 -- 6.1 Abstract Subprogram Declaration --
644 ------------------------------------------
646 -- Parsed by P_Subprogram (6.1)
648 -----------------------------------
649 -- 6.1 Subprogram Specification --
650 -----------------------------------
652 -- SUBPROGRAM_SPECIFICATION ::=
653 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
654 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
656 -- PARAMETER_PROFILE ::= [FORMAL_PART]
658 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
660 -- Subprogram specifications that appear in subprogram declarations
661 -- are parsed by P_Subprogram (6.1). This routine is used in other
662 -- contexts where subprogram specifications occur.
664 -- Note: this routine does not affect the scope stack in any way
666 -- Error recovery: can raise Error_Resync
668 function P_Subprogram_Specification return Node_Id is
669 Specification_Node : Node_Id;
670 Result_Not_Null : Boolean;
671 Result_Node : Node_Id;
674 if Token = Tok_Function then
675 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
676 Scan; -- past FUNCTION
678 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
679 Set_Parameter_Specifications
680 (Specification_Node, P_Parameter_Profile);
681 Check_Junk_Semicolon_Before_Return;
684 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
686 -- Ada 2005 (AI-318-02)
688 if Token = Tok_Access then
689 if Ada_Version < Ada_05 then
691 ("anonymous access result type is an Ada 2005 extension");
692 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
695 Result_Node := P_Access_Definition (Result_Not_Null);
698 Result_Node := P_Subtype_Mark;
702 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
703 Set_Result_Definition (Specification_Node, Result_Node);
704 return Specification_Node;
706 elsif Token = Tok_Procedure then
707 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
708 Scan; -- past PROCEDURE
710 Set_Defining_Unit_Name
711 (Specification_Node, P_Defining_Program_Unit_Name);
712 Set_Parameter_Specifications
713 (Specification_Node, P_Parameter_Profile);
714 return Specification_Node;
717 Error_Msg_SC ("subprogram specification expected");
720 end P_Subprogram_Specification;
722 ---------------------
724 ---------------------
727 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
729 -- The caller has checked that the initial token is an identifier,
730 -- operator symbol, or string literal. Note that we don't bother to
731 -- do much error diagnosis in this routine, since it is only used for
732 -- the label on END lines, and the routines in package Par.Endh will
733 -- check that the label is appropriate.
735 -- Error recovery: cannot raise Error_Resync
737 function P_Designator return Node_Id is
738 Ident_Node : Node_Id;
740 Prefix_Node : Node_Id;
742 function Real_Dot return Boolean;
743 -- Tests if a current token is an interesting period, i.e. is followed
744 -- by an identifier or operator symbol or string literal. If not, it is
745 -- probably just incorrect punctuation to be caught by our caller. Note
746 -- that the case of an operator symbol or string literal is also an
747 -- error, but that is an error that we catch here. If the result is
748 -- True, a real dot has been scanned and we are positioned past it,
749 -- if the result is False, the scan position is unchanged.
755 function Real_Dot return Boolean is
756 Scan_State : Saved_Scan_State;
759 if Token /= Tok_Dot then
763 Save_Scan_State (Scan_State);
766 if Token = Tok_Identifier
767 or else Token = Tok_Operator_Symbol
768 or else Token = Tok_String_Literal
773 Restore_Scan_State (Scan_State);
779 -- Start of processing for P_Designator
782 Ident_Node := Token_Node;
783 Scan; -- past initial token
785 if Prev_Token = Tok_Operator_Symbol
786 or else Prev_Token = Tok_String_Literal
794 Prefix_Node := Ident_Node;
796 -- Loop through child names, on entry to this loop, Prefix contains
797 -- the name scanned so far, and Ident_Node is the last identifier.
800 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
801 Set_Prefix (Name_Node, Prefix_Node);
802 Ident_Node := P_Identifier;
803 Set_Selector_Name (Name_Node, Ident_Node);
804 Prefix_Node := Name_Node;
805 exit when not Real_Dot;
808 -- On exit from the loop, Ident_Node is the last identifier scanned,
809 -- i.e. the defining identifier, and Prefix_Node is a node for the
810 -- entire name, structured (incorrectly!) as a selected component.
812 Name_Node := Prefix (Prefix_Node);
813 Change_Node (Prefix_Node, N_Designator);
814 Set_Name (Prefix_Node, Name_Node);
815 Set_Identifier (Prefix_Node, Ident_Node);
821 while Token = Tok_Dot or else Token = Tok_Identifier loop
828 ------------------------------
829 -- 6.1 Defining Designator --
830 ------------------------------
832 -- DEFINING_DESIGNATOR ::=
833 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
835 -- Error recovery: cannot raise Error_Resync
837 function P_Defining_Designator return Node_Id is
839 if Token = Tok_Operator_Symbol then
840 return P_Defining_Operator_Symbol;
842 elsif Token = Tok_String_Literal then
843 Error_Msg_SC ("invalid operator name");
844 Scan; -- past junk string
848 return P_Defining_Program_Unit_Name;
850 end P_Defining_Designator;
852 -------------------------------------
853 -- 6.1 Defining Program Unit Name --
854 -------------------------------------
856 -- DEFINING_PROGRAM_UNIT_NAME ::=
857 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
859 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
861 -- Error recovery: cannot raise Error_Resync
863 function P_Defining_Program_Unit_Name return Node_Id is
864 Ident_Node : Node_Id;
866 Prefix_Node : Node_Id;
869 -- Set identifier casing if not already set and scan initial identifier
871 if Token = Tok_Identifier
872 and then Identifier_Casing (Current_Source_File) = Unknown
874 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
877 Ident_Node := P_Identifier (C_Dot);
878 Merge_Identifier (Ident_Node, Tok_Return);
880 -- Normal case (not child library unit name)
882 if Token /= Tok_Dot then
883 Change_Identifier_To_Defining_Identifier (Ident_Node);
886 -- Child library unit name case
889 if Scope.Last > 1 then
890 Error_Msg_SP ("child unit allowed only at library level");
893 elsif Ada_Version = Ada_83 then
894 Error_Msg_SP ("(Ada 83) child unit not allowed!");
898 Prefix_Node := Ident_Node;
900 -- Loop through child names, on entry to this loop, Prefix contains
901 -- the name scanned so far, and Ident_Node is the last identifier.
904 exit when Token /= Tok_Dot;
905 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
907 Set_Prefix (Name_Node, Prefix_Node);
908 Ident_Node := P_Identifier (C_Dot);
909 Set_Selector_Name (Name_Node, Ident_Node);
910 Prefix_Node := Name_Node;
913 -- On exit from the loop, Ident_Node is the last identifier scanned,
914 -- i.e. the defining identifier, and Prefix_Node is a node for the
915 -- entire name, structured (incorrectly!) as a selected component.
917 Name_Node := Prefix (Prefix_Node);
918 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
919 Set_Name (Prefix_Node, Name_Node);
920 Change_Identifier_To_Defining_Identifier (Ident_Node);
921 Set_Defining_Identifier (Prefix_Node, Ident_Node);
923 -- All set with unit name parsed
930 while Token = Tok_Dot or else Token = Tok_Identifier loop
935 end P_Defining_Program_Unit_Name;
937 --------------------------
938 -- 6.1 Operator Symbol --
939 --------------------------
941 -- OPERATOR_SYMBOL ::= STRING_LITERAL
943 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
945 -----------------------------------
946 -- 6.1 Defining Operator Symbol --
947 -----------------------------------
949 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
951 -- The caller has checked that the initial symbol is an operator symbol
953 function P_Defining_Operator_Symbol return Node_Id is
957 Op_Node := Token_Node;
958 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
959 Scan; -- past operator symbol
961 end P_Defining_Operator_Symbol;
963 ----------------------------
964 -- 6.1 Parameter_Profile --
965 ----------------------------
967 -- PARAMETER_PROFILE ::= [FORMAL_PART]
969 -- Empty is returned if no formal part is present
971 -- Error recovery: cannot raise Error_Resync
973 function P_Parameter_Profile return List_Id is
975 if Token = Tok_Left_Paren then
976 Scan; -- part left paren
977 return P_Formal_Part;
981 end P_Parameter_Profile;
983 ---------------------------------------
984 -- 6.1 Parameter And Result Profile --
985 ---------------------------------------
987 -- Parsed by its parent construct, which uses P_Parameter_Profile to
988 -- parse the parameters, and P_Subtype_Mark to parse the return type.
990 ----------------------
991 -- 6.1 Formal part --
992 ----------------------
994 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
996 -- PARAMETER_SPECIFICATION ::=
997 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
998 -- [:= DEFAULT_EXPRESSION]
999 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1000 -- [:= DEFAULT_EXPRESSION]
1002 -- This scans the construct Formal_Part. The caller has already checked
1003 -- that the initial token is a left parenthesis, and skipped past it, so
1004 -- that on entry Token is the first token following the left parenthesis.
1006 -- Error recovery: cannot raise Error_Resync
1008 function P_Formal_Part return List_Id is
1009 Specification_List : List_Id;
1010 Specification_Node : Node_Id;
1011 Scan_State : Saved_Scan_State;
1014 Ident_Sloc : Source_Ptr;
1015 Not_Null_Present : Boolean := False;
1016 Not_Null_Sloc : Source_Ptr;
1018 Idents : array (Int range 1 .. 4096) of Entity_Id;
1019 -- This array holds the list of defining identifiers. The upper bound
1020 -- of 4096 is intended to be essentially infinite, and we do not even
1021 -- bother to check for it being exceeded.
1024 Specification_List := New_List;
1025 Specification_Loop : loop
1027 if Token = Tok_Pragma then
1028 Error_Msg_SC ("pragma not allowed in formal part");
1029 Discard_Junk_Node (P_Pragma (Skipping => True));
1032 Ignore (Tok_Left_Paren);
1033 Ident_Sloc := Token_Ptr;
1034 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1038 exit Ident_Loop when Token = Tok_Colon;
1040 -- The only valid tokens are colon and comma, so if we have
1041 -- neither do a bit of investigation to see which is the
1042 -- better choice for insertion.
1044 if Token /= Tok_Comma then
1046 -- Assume colon if IN or OUT keyword found
1048 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1050 -- Otherwise scan ahead
1052 Save_Scan_State (Scan_State);
1055 -- If we run into a semicolon, then assume that a
1056 -- colon was missing, e.g. Parms (X Y; ...). Also
1057 -- assume missing colon on EOF (a real disaster!)
1058 -- and on a right paren, e.g. Parms (X Y), and also
1059 -- on an assignment symbol, e.g. Parms (X Y := ..)
1061 if Token = Tok_Semicolon
1062 or else Token = Tok_Right_Paren
1063 or else Token = Tok_EOF
1064 or else Token = Tok_Colon_Equal
1066 Restore_Scan_State (Scan_State);
1069 -- If we run into a colon, assume that we had a missing
1070 -- comma, e.g. Parms (A B : ...). Also assume a missing
1071 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1073 elsif Token = Tok_Colon
1074 or else Token = Tok_Comma
1076 Restore_Scan_State (Scan_State);
1081 end loop Look_Ahead;
1084 -- Here if a comma is present, or to be assumed
1087 Num_Idents := Num_Idents + 1;
1088 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1089 end loop Ident_Loop;
1091 -- Fall through the loop on encountering a colon, or deciding
1092 -- that there is a missing colon.
1096 -- If there are multiple identifiers, we repeatedly scan the
1097 -- type and initialization expression information by resetting
1098 -- the scan pointer (so that we get completely separate trees
1099 -- for each occurrence).
1101 if Num_Idents > 1 then
1102 Save_Scan_State (Scan_State);
1105 -- Loop through defining identifiers in list
1109 Ident_List_Loop : loop
1110 Specification_Node :=
1111 New_Node (N_Parameter_Specification, Ident_Sloc);
1112 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1114 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1116 Not_Null_Sloc := Token_Ptr;
1118 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1120 -- Case of ACCESS keyword present
1122 if Token = Tok_Access then
1123 Set_Null_Exclusion_Present
1124 (Specification_Node, Not_Null_Present);
1126 if Ada_Version = Ada_83 then
1127 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1131 (Specification_Node,
1132 P_Access_Definition (Not_Null_Present));
1134 -- Case of IN or OUT present
1137 if Token = Tok_In or else Token = Tok_Out then
1138 if Not_Null_Present then
1140 ("`NOT NULL` can only be used with `ACCESS`",
1143 if Token = Tok_In then
1145 ("\`IN` not allowed together with `ACCESS`",
1149 ("\`OUT` not allowed together with `ACCESS`",
1154 P_Mode (Specification_Node);
1155 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1158 Set_Null_Exclusion_Present
1159 (Specification_Node, Not_Null_Present);
1161 if Token = Tok_Procedure
1163 Token = Tok_Function
1165 Error_Msg_SC ("formal subprogram parameter not allowed");
1168 if Token = Tok_Left_Paren then
1169 Discard_Junk_List (P_Formal_Part);
1172 if Token = Tok_Return then
1174 Discard_Junk_Node (P_Subtype_Mark);
1177 Set_Parameter_Type (Specification_Node, Error);
1180 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1185 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1188 Set_Prev_Ids (Specification_Node, True);
1191 if Ident < Num_Idents then
1192 Set_More_Ids (Specification_Node, True);
1195 Append (Specification_Node, Specification_List);
1196 exit Ident_List_Loop when Ident = Num_Idents;
1198 Restore_Scan_State (Scan_State);
1199 end loop Ident_List_Loop;
1202 when Error_Resync =>
1203 Resync_Semicolon_List;
1206 if Token = Tok_Semicolon then
1207 Save_Scan_State (Scan_State);
1208 Scan; -- past semicolon
1210 -- If we have RETURN or IS after the semicolon, then assume
1211 -- that semicolon should have been a right parenthesis and exit
1213 if Token = Tok_Is or else Token = Tok_Return then
1214 Error_Msg_SP ("|"";"" should be "")""");
1215 exit Specification_Loop;
1218 -- If we have a declaration keyword after the semicolon, then
1219 -- assume we had a missing right parenthesis and terminate list
1221 if Token in Token_Class_Declk then
1222 Error_Msg_AP ("missing "")""");
1223 Restore_Scan_State (Scan_State);
1224 exit Specification_Loop;
1227 elsif Token = Tok_Right_Paren then
1228 Scan; -- past right paren
1229 exit Specification_Loop;
1231 -- Special check for common error of using comma instead of semicolon
1233 elsif Token = Tok_Comma then
1237 -- Special check for omitted separator
1239 elsif Token = Tok_Identifier then
1242 -- If nothing sensible, skip to next semicolon or right paren
1246 Resync_Semicolon_List;
1248 if Token = Tok_Semicolon then
1249 Scan; -- past semicolon
1252 exit Specification_Loop;
1255 end loop Specification_Loop;
1257 return Specification_List;
1260 ----------------------------------
1261 -- 6.1 Parameter Specification --
1262 ----------------------------------
1264 -- Parsed by P_Formal_Part (6.1)
1270 -- MODE ::= [in] | in out | out
1272 -- There is no explicit node in the tree for the Mode. Instead the
1273 -- In_Present and Out_Present flags are set in the parent node to
1274 -- record the presence of keywords specifying the mode.
1276 -- Error_Recovery: cannot raise Error_Resync
1278 procedure P_Mode (Node : Node_Id) is
1280 if Token = Tok_In then
1282 Set_In_Present (Node, True);
1284 if Style.Mode_In_Check and then Token /= Tok_Out then
1285 Error_Msg_SP ("(style) IN should be omitted");
1288 if Token = Tok_Access then
1289 Error_Msg_SP ("IN not allowed together with ACCESS");
1290 Scan; -- past ACCESS
1294 if Token = Tok_Out then
1296 Set_Out_Present (Node, True);
1299 if Token = Tok_In then
1300 Error_Msg_SC ("IN must precede OUT in parameter mode");
1302 Set_In_Present (Node, True);
1306 --------------------------
1307 -- 6.3 Subprogram Body --
1308 --------------------------
1310 -- Parsed by P_Subprogram (6.1)
1312 -----------------------------------
1313 -- 6.4 Procedure Call Statement --
1314 -----------------------------------
1316 -- Parsed by P_Sequence_Of_Statements (5.1)
1318 ------------------------
1319 -- 6.4 Function Call --
1320 ------------------------
1322 -- Parsed by P_Call_Or_Name (4.1)
1324 --------------------------------
1325 -- 6.4 Actual Parameter Part --
1326 --------------------------------
1328 -- Parsed by P_Call_Or_Name (4.1)
1330 --------------------------------
1331 -- 6.4 Parameter Association --
1332 --------------------------------
1334 -- Parsed by P_Call_Or_Name (4.1)
1336 ------------------------------------
1337 -- 6.4 Explicit Actual Parameter --
1338 ------------------------------------
1340 -- Parsed by P_Call_Or_Name (4.1)
1342 ---------------------------
1343 -- 6.5 Return Statement --
1344 ---------------------------
1346 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1348 -- EXTENDED_RETURN_STATEMENT ::=
1349 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1350 -- [:= EXPRESSION] [do
1351 -- HANDLED_SEQUENCE_OF_STATEMENTS
1354 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1356 -- RETURN_STATEMENT ::= return [EXPRESSION];
1358 -- Error recovery: can raise Error_Resync
1360 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1362 -- Note: We don't need to check Ada_Version here, because this is
1363 -- only called in >= Ada 2005 cases anyway.
1365 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1368 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1370 if Token = Tok_Access then
1371 Set_Object_Definition
1372 (Decl_Node, P_Access_Definition (Not_Null_Present));
1374 Set_Object_Definition
1375 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1377 end P_Return_Subtype_Indication;
1379 -- Error recovery: can raise Error_Resync
1381 function P_Return_Object_Declaration return Node_Id is
1382 Return_Obj : Node_Id;
1383 Decl_Node : Node_Id;
1386 Return_Obj := Token_Node;
1387 Change_Identifier_To_Defining_Identifier (Return_Obj);
1388 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1389 Set_Defining_Identifier (Decl_Node, Return_Obj);
1391 Scan; -- past identifier
1394 -- First an error check, if we have two identifiers in a row, a likely
1395 -- possibility is that the first of the identifiers is an incorrectly
1396 -- spelled keyword. See similar check in P_Identifier_Declarations.
1398 if Token = Tok_Identifier then
1400 SS : Saved_Scan_State;
1404 Save_Scan_State (SS);
1405 Scan; -- past initial identifier
1406 I2 := (Token = Tok_Identifier);
1407 Restore_Scan_State (SS);
1411 (Bad_Spelling_Of (Tok_Access) or else
1412 Bad_Spelling_Of (Tok_Aliased) or else
1413 Bad_Spelling_Of (Tok_Constant))
1420 -- We allow "constant" here (as in "return Result : constant
1421 -- T..."). This is not in the latest RM, but the ARG is considering an
1422 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1424 if Token = Tok_Constant then
1425 Scan; -- past CONSTANT
1426 Set_Constant_Present (Decl_Node);
1428 if Token = Tok_Aliased then
1429 Error_Msg_SC ("ALIASED should be before CONSTANT");
1430 Scan; -- past ALIASED
1431 Set_Aliased_Present (Decl_Node);
1434 elsif Token = Tok_Aliased then
1435 Scan; -- past ALIASED
1436 Set_Aliased_Present (Decl_Node);
1438 if Token = Tok_Constant then
1439 Scan; -- past CONSTANT
1440 Set_Constant_Present (Decl_Node);
1444 P_Return_Subtype_Indication (Decl_Node);
1446 if Token = Tok_Colon_Equal then
1448 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1452 end P_Return_Object_Declaration;
1454 -- Error recovery: can raise Error_Resync
1456 function P_Return_Statement return Node_Id is
1457 -- The caller has checked that the initial token is RETURN
1459 function Is_Simple return Boolean;
1460 -- Scan state is just after RETURN (and is left that way).
1461 -- Determine whether this is a simple or extended return statement
1462 -- by looking ahead for "identifier :", which implies extended.
1468 function Is_Simple return Boolean is
1469 Scan_State : Saved_Scan_State;
1470 Result : Boolean := True;
1473 if Token = Tok_Identifier then
1474 Save_Scan_State (Scan_State); -- at identifier
1475 Scan; -- past identifier
1477 if Token = Tok_Colon then
1478 Result := False; -- It's an extended_return_statement.
1481 Restore_Scan_State (Scan_State); -- to identifier
1487 Return_Sloc : constant Source_Ptr := Token_Ptr;
1488 Return_Node : Node_Id;
1490 -- Start of processing for P_Return_Statement
1493 Scan; -- past RETURN
1495 -- Simple_return_statement, no expression, return an
1496 -- N_Simple_Return_Statement node with the expression field left Empty.
1498 if Token = Tok_Semicolon then
1500 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1505 -- Simple_return_statement with expression
1507 -- We avoid trying to scan an expression if we are at an
1508 -- expression terminator since in that case the best error
1509 -- message is probably that we have a missing semicolon.
1512 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1514 if Token not in Token_Class_Eterm then
1515 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1518 -- Extended_return_statement (Ada 2005 only -- AI-318):
1521 if Ada_Version < Ada_05 then
1523 (" extended_return_statement is an Ada 2005 extension");
1524 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1527 Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1528 Set_Return_Object_Declarations
1529 (Return_Node, New_List (P_Return_Object_Declaration));
1531 if Token = Tok_Do then
1533 Scope.Table (Scope.Last).Etyp := E_Return;
1534 Scope.Table (Scope.Last).Ecol := Start_Column;
1535 Scope.Table (Scope.Last).Sloc := Return_Sloc;
1538 Set_Handled_Statement_Sequence
1539 (Return_Node, P_Handled_Sequence_Of_Statements);
1542 -- Do we need to handle Error_Resync here???
1550 end P_Return_Statement;