1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 -- CODEFIX
68 ("|extra "";"" ignored");
69 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.
85 -- It also handles the new Ada 2012 parameterized expression form
87 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
89 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
90 -- SUBPROGRAM_SPECIFICATION is abstract;
92 -- SUBPROGRAM_SPECIFICATION ::=
93 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
94 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
96 -- PARAMETER_PROFILE ::= [FORMAL_PART]
98 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
100 -- SUBPROGRAM_BODY ::=
101 -- SUBPROGRAM_SPECIFICATION is
104 -- HANDLED_SEQUENCE_OF_STATEMENTS
107 -- SUBPROGRAM_RENAMING_DECLARATION ::=
108 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
110 -- SUBPROGRAM_BODY_STUB ::=
111 -- SUBPROGRAM_SPECIFICATION is separate;
113 -- GENERIC_INSTANTIATION ::=
114 -- procedure DEFINING_PROGRAM_UNIT_NAME is
115 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
116 -- | function DEFINING_DESIGNATOR is
117 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
119 -- NULL_PROCEDURE_DECLARATION ::=
120 -- SUBPROGRAM_SPECIFICATION is null;
122 -- Null procedures are an Ada 2005 feature. A null procedure declaration
123 -- is classified as a basic declarative item, but it is parsed here, with
124 -- other subprogram constructs.
126 -- PARAMETERIZED_EXPRESSION ::=
127 -- FUNCTION SPECIFICATION IS (EXPRESSION);
129 -- The value in Pf_Flags indicates which of these possible declarations
130 -- is acceptable to the caller:
132 -- Pf_Flags.Decl Set if declaration OK
133 -- Pf_Flags.Gins Set if generic instantiation OK
134 -- Pf_Flags.Pbod Set if proper body OK
135 -- Pf_Flags.Rnam Set if renaming declaration OK
136 -- Pf_Flags.Stub Set if body stub OK
137 -- Pf_Flags.Pexp Set if parameterized expression OK
139 -- If an inappropriate form is encountered, it is scanned out but an
140 -- error message indicating that it is appearing in an inappropriate
141 -- context is issued. The only possible values for Pf_Flags are those
142 -- defined as constants in the Par package.
144 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
145 -- NOT or OVERRIDING.
147 -- Error recovery: cannot raise Error_Resync
149 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
150 Specification_Node : Node_Id;
152 Fpart_List : List_Id;
153 Fpart_Sloc : Source_Ptr;
154 Result_Not_Null : Boolean := False;
155 Result_Node : Node_Id;
159 Rename_Node : Node_Id;
160 Absdec_Node : Node_Id;
162 Fproc_Sloc : Source_Ptr;
164 Scan_State : Saved_Scan_State;
166 -- Flags for optional overriding indication. Two flags are needed,
167 -- to distinguish positive and negative overriding indicators from
168 -- the absence of any indicator.
170 Is_Overriding : Boolean := False;
171 Not_Overriding : Boolean := False;
174 -- Set up scope stack entry. Note that the Labl field will be set later
176 SIS_Entry_Active := False;
177 SIS_Missing_Semicolon_Message := No_Error_Msg;
179 Scope.Table (Scope.Last).Sloc := Token_Ptr;
180 Scope.Table (Scope.Last).Etyp := E_Name;
181 Scope.Table (Scope.Last).Ecol := Start_Column;
182 Scope.Table (Scope.Last).Lreq := False;
184 -- Ada2005: scan leading NOT OVERRIDING indicator
186 if Token = Tok_Not then
189 if Token = Tok_Overriding then
190 Scan; -- past OVERRIDING
191 Not_Overriding := True;
193 -- Overriding keyword used in non Ada 2005 mode
195 elsif Token = Tok_Identifier
196 and then Token_Name = Name_Overriding
198 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
199 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
200 Scan; -- past Overriding
201 Not_Overriding := True;
204 Error_Msg_SC -- CODEFIX
205 ("OVERRIDING expected!");
208 -- Ada 2005: scan leading OVERRIDING indicator
210 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
211 -- declaration circuit already gave an error message and changed the
212 -- token to Tok_Overriding.
214 elsif Token = Tok_Overriding then
215 Scan; -- past OVERRIDING
216 Is_Overriding := True;
219 if Is_Overriding or else Not_Overriding then
221 -- Note that if we are not in Ada_05 mode, error messages have
222 -- already been given, so no need to give another message here.
224 -- An overriding indicator is allowed for subprogram declarations,
225 -- bodies (including subunits), renamings, stubs, and instantiations.
226 -- The test against Pf_Decl_Pbod is added to account for the case of
227 -- subprograms declared in a protected type, where only subprogram
228 -- declarations and bodies can occur. The Pf_Pbod case is for
231 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
233 Pf_Flags /= Pf_Decl_Pbod_Pexp
235 Pf_Flags /= Pf_Pbod_Pexp
237 Error_Msg_SC ("overriding indicator not allowed here!");
239 elsif Token /= Tok_Function and then Token /= Tok_Procedure then
240 Error_Msg_SC -- CODEFIX
241 ("FUNCTION or PROCEDURE expected!");
245 Func := (Token = Tok_Function);
246 Fproc_Sloc := Token_Ptr;
247 Scan; -- past FUNCTION or PROCEDURE
252 Name_Node := P_Defining_Designator;
254 if Nkind (Name_Node) = N_Defining_Operator_Symbol
255 and then Scope.Last = 1
257 Error_Msg_SP ("operator symbol not allowed at library level");
258 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
260 -- Set name from file name, we need some junk name, and that's
261 -- as good as anything. This is only approximate, since we do
262 -- not do anything with non-standard name translations.
264 Get_Name_String (File_Name (Current_Source_File));
266 for J in 1 .. Name_Len loop
267 if Name_Buffer (J) = '.' then
273 Set_Chars (Name_Node, Name_Find);
274 Set_Error_Posted (Name_Node);
278 Name_Node := P_Defining_Program_Unit_Name;
281 Scope.Table (Scope.Last).Labl := Name_Node;
284 -- Deal with generic instantiation, the one case in which we do not
285 -- have a subprogram specification as part of whatever we are parsing
287 if Token = Tok_Is then
288 Save_Scan_State (Scan_State); -- at the IS
289 T_Is; -- checks for redundant IS
291 if Token = Tok_New then
292 if not Pf_Flags.Gins then
293 Error_Msg_SC ("generic instantiation not allowed here!");
299 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
300 Set_Name (Inst_Node, P_Function_Name);
302 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
303 Set_Name (Inst_Node, P_Qualified_Simple_Name);
306 Set_Defining_Unit_Name (Inst_Node, Name_Node);
307 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
309 Pop_Scope_Stack; -- Don't need scope stack entry in this case
311 if Is_Overriding then
312 Set_Must_Override (Inst_Node);
314 elsif Not_Overriding then
315 Set_Must_Not_Override (Inst_Node);
321 Restore_Scan_State (Scan_State); -- to the IS
325 -- If not a generic instantiation, then we definitely have a subprogram
326 -- specification (all possibilities at this stage include one here)
328 Fpart_Sloc := Token_Ptr;
330 Check_Misspelling_Of (Tok_Return);
332 -- Scan formal part. First a special error check. If we have an
333 -- identifier here, then we have a definite error. If this identifier
334 -- is on the same line as the designator, then we assume it is the
335 -- first formal after a missing left parenthesis
337 if Token = Tok_Identifier
338 and then not Token_Is_At_Start_Of_Line
340 T_Left_Paren; -- to generate message
341 Fpart_List := P_Formal_Part;
343 -- Otherwise scan out an optional formal part in the usual manner
346 Fpart_List := P_Parameter_Profile;
349 -- We treat what we have as a function specification if FUNCTION was
350 -- used, or if a RETURN is present. This gives better error recovery
351 -- since later RETURN statements will be valid in either case.
353 Check_Junk_Semicolon_Before_Return;
354 Result_Node := Error;
356 if Token = Tok_Return then
359 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
365 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
367 -- Ada 2005 (AI-318-02)
369 if Token = Tok_Access then
370 if Ada_Version < Ada_05 then
372 ("anonymous access result type is an Ada 2005 extension");
373 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
376 Result_Node := P_Access_Definition (Result_Not_Null);
379 Result_Node := P_Subtype_Mark;
384 -- Skip extra parenthesis at end of formal part
386 Ignore (Tok_Right_Paren);
388 -- For function, scan result subtype
393 if Prev_Token = Tok_Return then
394 Result_Node := P_Subtype_Mark;
400 Specification_Node :=
401 New_Node (N_Function_Specification, Fproc_Sloc);
403 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
404 Set_Result_Definition (Specification_Node, Result_Node);
407 Specification_Node :=
408 New_Node (N_Procedure_Specification, Fproc_Sloc);
411 Set_Defining_Unit_Name (Specification_Node, Name_Node);
412 Set_Parameter_Specifications (Specification_Node, Fpart_List);
414 if Is_Overriding then
415 Set_Must_Override (Specification_Node);
417 elsif Not_Overriding then
418 Set_Must_Not_Override (Specification_Node);
421 -- Error check: barriers not allowed on protected functions/procedures
423 if Token = Tok_When then
425 Error_Msg_SC ("barrier not allowed on function, only on entry");
427 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
431 Discard_Junk_Node (P_Expression);
434 -- Deal with semicolon followed by IS. We want to treat this as IS
436 if Token = Tok_Semicolon then
437 Save_Scan_State (Scan_State);
438 Scan; -- past semicolon
440 if Token = Tok_Is then
441 Error_Msg_SP -- CODEFIX
442 ("extra "";"" ignored");
444 Restore_Scan_State (Scan_State);
448 -- Deal with case of semicolon ending a subprogram declaration
450 if Token = Tok_Semicolon then
451 if not Pf_Flags.Decl then
455 Scan; -- past semicolon
457 -- If semicolon is immediately followed by IS, then ignore the
458 -- semicolon, and go process the body.
460 if Token = Tok_Is then
461 Error_Msg_SP -- CODEFIX
462 ("|extra "";"" ignored");
463 T_Is; -- scan past IS
464 goto Subprogram_Body;
466 -- If BEGIN follows in an appropriate column, we immediately
467 -- commence the error action of assuming that the previous
468 -- subprogram declaration should have been a subprogram body,
469 -- i.e. that the terminating semicolon should have been IS.
471 elsif Token = Tok_Begin
472 and then Start_Column >= Scope.Table (Scope.Last).Ecol
474 Error_Msg_SP -- CODEFIX
475 ("|"";"" should be IS!");
476 goto Subprogram_Body;
479 goto Subprogram_Declaration;
482 -- Case of not followed by semicolon
485 -- Subprogram renaming declaration case
487 Check_Misspelling_Of (Tok_Renames);
489 if Token = Tok_Renames then
490 if not Pf_Flags.Rnam then
491 Error_Msg_SC ("renaming declaration not allowed here!");
495 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
496 Scan; -- past RENAMES
497 Set_Name (Rename_Node, P_Name);
498 Set_Specification (Rename_Node, Specification_Node);
503 -- Case of IS following subprogram specification
505 elsif Token = Tok_Is then
506 T_Is; -- ignore redundant Is's
508 if Token_Name = Name_Abstract then
509 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
512 -- Deal nicely with (now obsolete) use of <> in place of abstract
514 if Token = Tok_Box then
515 Error_Msg_SC -- CODEFIX
516 ("ABSTRACT expected");
517 Token := Tok_Abstract;
520 -- Abstract subprogram declaration case
522 if Token = Tok_Abstract then
524 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
525 Set_Specification (Absdec_Node, Specification_Node);
526 Pop_Scope_Stack; -- discard unneeded entry
527 Scan; -- past ABSTRACT
531 -- Ada 2005 (AI-248): Parse a null procedure declaration
533 elsif Token = Tok_Null then
534 if Ada_Version < Ada_05 then
535 Error_Msg_SP ("null procedures are an Ada 2005 extension");
536 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
542 Error_Msg_SP ("only procedures can be null");
544 Set_Null_Present (Specification_Node);
548 goto Subprogram_Declaration;
550 -- Check for IS NEW with Formal_Part present and handle nicely
552 elsif Token = Tok_New then
554 ("formal part not allowed in instantiation", Fpart_Sloc);
558 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
561 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
564 Set_Defining_Unit_Name (Inst_Node, Name_Node);
565 Set_Name (Inst_Node, P_Name);
566 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
568 Pop_Scope_Stack; -- Don't need scope stack entry in this case
572 goto Subprogram_Body;
575 -- Here we have a missing IS or missing semicolon, we always guess
576 -- a missing semicolon, since we are pretty good at fixing up a
577 -- semicolon which should really be an IS
580 Error_Msg_AP -- CODEFIX
582 SIS_Missing_Semicolon_Message := Get_Msg_Id;
583 goto Subprogram_Declaration;
587 -- Processing for stub or subprogram body or parameterized expression
591 -- Subprogram body stub case
593 if Separate_Present then
594 if not Pf_Flags.Stub then
595 Error_Msg_SC ("body stub not allowed here!");
598 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
600 ("operator symbol cannot be used as subunit name",
605 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
606 Set_Specification (Stub_Node, Specification_Node);
607 Scan; -- past SEPARATE
612 -- Subprogram body or parameterized expression case
615 Scan_Body_Or_Parameterized_Expression : declare
617 function Likely_Parameterized_Expression return Boolean;
618 -- Returns True if we have a probably case of a parameterized
619 -- expression omitting the parentheses, if so, returns True
620 -- and emits an appropriate error message, else returns False.
622 -------------------------------------
623 -- Likely_Parameterized_Expression --
624 -------------------------------------
626 function Likely_Parameterized_Expression return Boolean is
628 -- If currently pointing to BEGIN or a declaration keyword
629 -- or a pragma, then we definitely have a subprogram body.
630 -- This is a common case, so worth testing first.
633 or else Token in Token_Class_Declk
634 or else Token = Tok_Pragma
638 -- Test for tokens which could only start an expression and
639 -- thus signal the case of a parameterized expression.
641 elsif Token in Token_Class_Literal
642 or else Token in Token_Class_Unary_Addop
643 or else Token = Tok_Left_Paren
644 or else Token = Tok_Abs
645 or else Token = Tok_Null
646 or else Token = Tok_New
647 or else Token = Tok_Not
651 -- Anything other than an identifier must be a body
653 elsif Token /= Tok_Identifier then
656 -- Here for an identifier
659 -- If the identifier is the first token on its line, then
660 -- let's assume that we have a missing begin and this is
661 -- intended as a subprogram body.
663 if Token_Is_At_Start_Of_Line then
666 -- Otherwise we have to scan ahead. If the identifier is
667 -- followed by a colon or a comma, it is a declaration
668 -- and hence we have a subprogram body. Otherwise assume
669 -- a parameterized expression.
673 Scan_State : Saved_Scan_State;
676 Save_Scan_State (Scan_State);
677 Scan; -- past identifier
679 Restore_Scan_State (Scan_State);
681 if Tok = Tok_Colon or else Tok = Tok_Comma then
688 -- Fall through if we have a likely parameterized expression
691 ("parameterized expression must be "
692 & "enclosed in parentheses");
694 end Likely_Parameterized_Expression;
696 -- Start of processing for Scan_Body_Or_Parameterized_Expression
699 -- Parameterized_Expression case
701 if Token = Tok_Left_Paren
702 or else Likely_Parameterized_Expression
704 -- Check parameterized expression allowed here
706 if not Pf_Flags.Pexp then
708 ("parameterized expression not allowed here!");
711 -- Check we are in Ada 2012 mode
713 if Ada_Version < Ada_12 then
715 ("parameterized expression is an Ada 2012 feature!");
717 ("\unit must be compiled with -gnat2012 switch!");
720 -- Parse out expression and build parameterized expression
724 (N_Parameterized_Expression, Sloc (Specification_Node));
725 Set_Specification (Body_Node, Specification_Node);
726 Set_Expression (Body_Node, P_Expression);
730 -- Subprogram body case
733 -- Check body allowed here
735 if not Pf_Flags.Pbod then
736 Error_Msg_SP ("subprogram body not allowed here!");
739 -- Here is the test for a suspicious IS (i.e. one that
740 -- looks like it might more properly be a semicolon).
741 -- See separate section describing use of IS instead
742 -- of semicolon in package Parse.
744 if (Token in Token_Class_Declk
746 Token = Tok_Identifier)
747 and then Start_Column <= Scope.Table (Scope.Last).Ecol
748 and then Scope.Last /= 1
750 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
751 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
754 -- Build and return subprogram body, parsing declarations
755 -- and statement sequence that belong to the body.
758 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
759 Set_Specification (Body_Node, Specification_Node);
760 Parse_Decls_Begin_End (Body_Node);
764 end Scan_Body_Or_Parameterized_Expression;
767 -- Processing for subprogram declaration
769 <<Subprogram_Declaration>>
771 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
772 Set_Specification (Decl_Node, Specification_Node);
774 -- If this is a context in which a subprogram body is permitted,
775 -- set active SIS entry in case (see section titled "Handling
776 -- Semicolon Used in Place of IS" in body of Parser package)
777 -- Note that SIS_Missing_Semicolon_Message is already set properly.
779 if Pf_Flags.Pbod then
780 SIS_Labl := Scope.Table (Scope.Last).Labl;
781 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
782 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
783 SIS_Declaration_Node := Decl_Node;
784 SIS_Semicolon_Sloc := Prev_Token_Ptr;
785 SIS_Entry_Active := True;
792 ---------------------------------
793 -- 6.1 Subprogram Declaration --
794 ---------------------------------
796 -- Parsed by P_Subprogram (6.1)
798 ------------------------------------------
799 -- 6.1 Abstract Subprogram Declaration --
800 ------------------------------------------
802 -- Parsed by P_Subprogram (6.1)
804 -----------------------------------
805 -- 6.1 Subprogram Specification --
806 -----------------------------------
808 -- SUBPROGRAM_SPECIFICATION ::=
809 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
810 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
812 -- PARAMETER_PROFILE ::= [FORMAL_PART]
814 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
816 -- Subprogram specifications that appear in subprogram declarations
817 -- are parsed by P_Subprogram (6.1). This routine is used in other
818 -- contexts where subprogram specifications occur.
820 -- Note: this routine does not affect the scope stack in any way
822 -- Error recovery: can raise Error_Resync
824 function P_Subprogram_Specification return Node_Id is
825 Specification_Node : Node_Id;
826 Result_Not_Null : Boolean;
827 Result_Node : Node_Id;
830 if Token = Tok_Function then
831 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
832 Scan; -- past FUNCTION
834 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
835 Set_Parameter_Specifications
836 (Specification_Node, P_Parameter_Profile);
837 Check_Junk_Semicolon_Before_Return;
840 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
842 -- Ada 2005 (AI-318-02)
844 if Token = Tok_Access then
845 if Ada_Version < Ada_05 then
847 ("anonymous access result type is an Ada 2005 extension");
848 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
851 Result_Node := P_Access_Definition (Result_Not_Null);
854 Result_Node := P_Subtype_Mark;
858 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
859 Set_Result_Definition (Specification_Node, Result_Node);
860 return Specification_Node;
862 elsif Token = Tok_Procedure then
863 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
864 Scan; -- past PROCEDURE
866 Set_Defining_Unit_Name
867 (Specification_Node, P_Defining_Program_Unit_Name);
868 Set_Parameter_Specifications
869 (Specification_Node, P_Parameter_Profile);
870 return Specification_Node;
873 Error_Msg_SC ("subprogram specification expected");
876 end P_Subprogram_Specification;
878 ---------------------
880 ---------------------
883 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
885 -- The caller has checked that the initial token is an identifier,
886 -- operator symbol, or string literal. Note that we don't bother to
887 -- do much error diagnosis in this routine, since it is only used for
888 -- the label on END lines, and the routines in package Par.Endh will
889 -- check that the label is appropriate.
891 -- Error recovery: cannot raise Error_Resync
893 function P_Designator return Node_Id is
894 Ident_Node : Node_Id;
896 Prefix_Node : Node_Id;
898 function Real_Dot return Boolean;
899 -- Tests if a current token is an interesting period, i.e. is followed
900 -- by an identifier or operator symbol or string literal. If not, it is
901 -- probably just incorrect punctuation to be caught by our caller. Note
902 -- that the case of an operator symbol or string literal is also an
903 -- error, but that is an error that we catch here. If the result is
904 -- True, a real dot has been scanned and we are positioned past it,
905 -- if the result is False, the scan position is unchanged.
911 function Real_Dot return Boolean is
912 Scan_State : Saved_Scan_State;
915 if Token /= Tok_Dot then
919 Save_Scan_State (Scan_State);
922 if Token = Tok_Identifier
923 or else Token = Tok_Operator_Symbol
924 or else Token = Tok_String_Literal
929 Restore_Scan_State (Scan_State);
935 -- Start of processing for P_Designator
938 Ident_Node := Token_Node;
939 Scan; -- past initial token
941 if Prev_Token = Tok_Operator_Symbol
942 or else Prev_Token = Tok_String_Literal
950 Prefix_Node := Ident_Node;
952 -- Loop through child names, on entry to this loop, Prefix contains
953 -- the name scanned so far, and Ident_Node is the last identifier.
956 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
957 Set_Prefix (Name_Node, Prefix_Node);
958 Ident_Node := P_Identifier;
959 Set_Selector_Name (Name_Node, Ident_Node);
960 Prefix_Node := Name_Node;
961 exit when not Real_Dot;
964 -- On exit from the loop, Ident_Node is the last identifier scanned,
965 -- i.e. the defining identifier, and Prefix_Node is a node for the
966 -- entire name, structured (incorrectly!) as a selected component.
968 Name_Node := Prefix (Prefix_Node);
969 Change_Node (Prefix_Node, N_Designator);
970 Set_Name (Prefix_Node, Name_Node);
971 Set_Identifier (Prefix_Node, Ident_Node);
977 while Token = Tok_Dot or else Token = Tok_Identifier loop
984 ------------------------------
985 -- 6.1 Defining Designator --
986 ------------------------------
988 -- DEFINING_DESIGNATOR ::=
989 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
991 -- Error recovery: cannot raise Error_Resync
993 function P_Defining_Designator return Node_Id is
995 if Token = Tok_Operator_Symbol then
996 return P_Defining_Operator_Symbol;
998 elsif Token = Tok_String_Literal then
999 Error_Msg_SC ("invalid operator name");
1000 Scan; -- past junk string
1004 return P_Defining_Program_Unit_Name;
1006 end P_Defining_Designator;
1008 -------------------------------------
1009 -- 6.1 Defining Program Unit Name --
1010 -------------------------------------
1012 -- DEFINING_PROGRAM_UNIT_NAME ::=
1013 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1015 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1017 -- Error recovery: cannot raise Error_Resync
1019 function P_Defining_Program_Unit_Name return Node_Id is
1020 Ident_Node : Node_Id;
1021 Name_Node : Node_Id;
1022 Prefix_Node : Node_Id;
1025 -- Set identifier casing if not already set and scan initial identifier
1027 if Token = Tok_Identifier
1028 and then Identifier_Casing (Current_Source_File) = Unknown
1030 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1033 Ident_Node := P_Identifier (C_Dot);
1034 Merge_Identifier (Ident_Node, Tok_Return);
1036 -- Normal case (not child library unit name)
1038 if Token /= Tok_Dot then
1039 Change_Identifier_To_Defining_Identifier (Ident_Node);
1042 -- Child library unit name case
1045 if Scope.Last > 1 then
1046 Error_Msg_SP ("child unit allowed only at library level");
1049 elsif Ada_Version = Ada_83 then
1050 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1054 Prefix_Node := Ident_Node;
1056 -- Loop through child names, on entry to this loop, Prefix contains
1057 -- the name scanned so far, and Ident_Node is the last identifier.
1060 exit when Token /= Tok_Dot;
1061 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1062 Scan; -- past period
1063 Set_Prefix (Name_Node, Prefix_Node);
1064 Ident_Node := P_Identifier (C_Dot);
1065 Set_Selector_Name (Name_Node, Ident_Node);
1066 Prefix_Node := Name_Node;
1069 -- On exit from the loop, Ident_Node is the last identifier scanned,
1070 -- i.e. the defining identifier, and Prefix_Node is a node for the
1071 -- entire name, structured (incorrectly!) as a selected component.
1073 Name_Node := Prefix (Prefix_Node);
1074 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1075 Set_Name (Prefix_Node, Name_Node);
1076 Change_Identifier_To_Defining_Identifier (Ident_Node);
1077 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1079 -- All set with unit name parsed
1085 when Error_Resync =>
1086 while Token = Tok_Dot or else Token = Tok_Identifier loop
1091 end P_Defining_Program_Unit_Name;
1093 --------------------------
1094 -- 6.1 Operator Symbol --
1095 --------------------------
1097 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1099 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1101 -----------------------------------
1102 -- 6.1 Defining Operator Symbol --
1103 -----------------------------------
1105 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1107 -- The caller has checked that the initial symbol is an operator symbol
1109 function P_Defining_Operator_Symbol return Node_Id is
1113 Op_Node := Token_Node;
1114 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1115 Scan; -- past operator symbol
1117 end P_Defining_Operator_Symbol;
1119 ----------------------------
1120 -- 6.1 Parameter_Profile --
1121 ----------------------------
1123 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1125 -- Empty is returned if no formal part is present
1127 -- Error recovery: cannot raise Error_Resync
1129 function P_Parameter_Profile return List_Id is
1131 if Token = Tok_Left_Paren then
1132 Scan; -- part left paren
1133 return P_Formal_Part;
1137 end P_Parameter_Profile;
1139 ---------------------------------------
1140 -- 6.1 Parameter And Result Profile --
1141 ---------------------------------------
1143 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1144 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1146 ----------------------
1147 -- 6.1 Formal part --
1148 ----------------------
1150 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1152 -- PARAMETER_SPECIFICATION ::=
1153 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
1154 -- [:= DEFAULT_EXPRESSION]
1155 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1156 -- [:= DEFAULT_EXPRESSION]
1158 -- This scans the construct Formal_Part. The caller has already checked
1159 -- that the initial token is a left parenthesis, and skipped past it, so
1160 -- that on entry Token is the first token following the left parenthesis.
1162 -- Error recovery: cannot raise Error_Resync
1164 function P_Formal_Part return List_Id is
1165 Specification_List : List_Id;
1166 Specification_Node : Node_Id;
1167 Scan_State : Saved_Scan_State;
1170 Ident_Sloc : Source_Ptr;
1171 Not_Null_Present : Boolean := False;
1172 Not_Null_Sloc : Source_Ptr;
1174 Idents : array (Int range 1 .. 4096) of Entity_Id;
1175 -- This array holds the list of defining identifiers. The upper bound
1176 -- of 4096 is intended to be essentially infinite, and we do not even
1177 -- bother to check for it being exceeded.
1180 Specification_List := New_List;
1181 Specification_Loop : loop
1183 if Token = Tok_Pragma then
1184 Error_Msg_SC ("pragma not allowed in formal part");
1185 Discard_Junk_Node (P_Pragma (Skipping => True));
1188 Ignore (Tok_Left_Paren);
1189 Ident_Sloc := Token_Ptr;
1190 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1194 exit Ident_Loop when Token = Tok_Colon;
1196 -- The only valid tokens are colon and comma, so if we have
1197 -- neither do a bit of investigation to see which is the
1198 -- better choice for insertion.
1200 if Token /= Tok_Comma then
1202 -- Assume colon if IN or OUT keyword found
1204 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1206 -- Otherwise scan ahead
1208 Save_Scan_State (Scan_State);
1211 -- If we run into a semicolon, then assume that a
1212 -- colon was missing, e.g. Parms (X Y; ...). Also
1213 -- assume missing colon on EOF (a real disaster!)
1214 -- and on a right paren, e.g. Parms (X Y), and also
1215 -- on an assignment symbol, e.g. Parms (X Y := ..)
1217 if Token = Tok_Semicolon
1218 or else Token = Tok_Right_Paren
1219 or else Token = Tok_EOF
1220 or else Token = Tok_Colon_Equal
1222 Restore_Scan_State (Scan_State);
1225 -- If we run into a colon, assume that we had a missing
1226 -- comma, e.g. Parms (A B : ...). Also assume a missing
1227 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1229 elsif Token = Tok_Colon
1230 or else Token = Tok_Comma
1232 Restore_Scan_State (Scan_State);
1237 end loop Look_Ahead;
1240 -- Here if a comma is present, or to be assumed
1243 Num_Idents := Num_Idents + 1;
1244 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1245 end loop Ident_Loop;
1247 -- Fall through the loop on encountering a colon, or deciding
1248 -- that there is a missing colon.
1252 -- If there are multiple identifiers, we repeatedly scan the
1253 -- type and initialization expression information by resetting
1254 -- the scan pointer (so that we get completely separate trees
1255 -- for each occurrence).
1257 if Num_Idents > 1 then
1258 Save_Scan_State (Scan_State);
1261 -- Loop through defining identifiers in list
1265 Ident_List_Loop : loop
1266 Specification_Node :=
1267 New_Node (N_Parameter_Specification, Ident_Sloc);
1268 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1270 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1272 Not_Null_Sloc := Token_Ptr;
1274 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1276 -- Case of ACCESS keyword present
1278 if Token = Tok_Access then
1279 Set_Null_Exclusion_Present
1280 (Specification_Node, Not_Null_Present);
1282 if Ada_Version = Ada_83 then
1283 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1287 (Specification_Node,
1288 P_Access_Definition (Not_Null_Present));
1290 -- Case of IN or OUT present
1293 if Token = Tok_In or else Token = Tok_Out then
1294 if Not_Null_Present then
1296 ("`NOT NULL` can only be used with `ACCESS`",
1299 if Token = Tok_In then
1301 ("\`IN` not allowed together with `ACCESS`",
1305 ("\`OUT` not allowed together with `ACCESS`",
1310 P_Mode (Specification_Node);
1311 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1314 Set_Null_Exclusion_Present
1315 (Specification_Node, Not_Null_Present);
1317 if Token = Tok_Procedure
1319 Token = Tok_Function
1321 Error_Msg_SC ("formal subprogram parameter not allowed");
1324 if Token = Tok_Left_Paren then
1325 Discard_Junk_List (P_Formal_Part);
1328 if Token = Tok_Return then
1330 Discard_Junk_Node (P_Subtype_Mark);
1333 Set_Parameter_Type (Specification_Node, Error);
1336 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1341 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1344 Set_Prev_Ids (Specification_Node, True);
1347 if Ident < Num_Idents then
1348 Set_More_Ids (Specification_Node, True);
1351 Append (Specification_Node, Specification_List);
1352 exit Ident_List_Loop when Ident = Num_Idents;
1354 Restore_Scan_State (Scan_State);
1355 end loop Ident_List_Loop;
1358 when Error_Resync =>
1359 Resync_Semicolon_List;
1362 if Token = Tok_Semicolon then
1363 Save_Scan_State (Scan_State);
1364 Scan; -- past semicolon
1366 -- If we have RETURN or IS after the semicolon, then assume
1367 -- that semicolon should have been a right parenthesis and exit
1369 if Token = Tok_Is or else Token = Tok_Return then
1370 Error_Msg_SP -- CODEFIX
1371 ("|"";"" should be "")""");
1372 exit Specification_Loop;
1375 -- If we have a declaration keyword after the semicolon, then
1376 -- assume we had a missing right parenthesis and terminate list
1378 if Token in Token_Class_Declk then
1379 Error_Msg_AP -- CODEFIX
1381 Restore_Scan_State (Scan_State);
1382 exit Specification_Loop;
1385 elsif Token = Tok_Right_Paren then
1386 Scan; -- past right paren
1387 exit Specification_Loop;
1389 -- Special check for common error of using comma instead of semicolon
1391 elsif Token = Tok_Comma then
1395 -- Special check for omitted separator
1397 elsif Token = Tok_Identifier then
1400 -- If nothing sensible, skip to next semicolon or right paren
1404 Resync_Semicolon_List;
1406 if Token = Tok_Semicolon then
1407 Scan; -- past semicolon
1410 exit Specification_Loop;
1413 end loop Specification_Loop;
1415 return Specification_List;
1418 ----------------------------------
1419 -- 6.1 Parameter Specification --
1420 ----------------------------------
1422 -- Parsed by P_Formal_Part (6.1)
1428 -- MODE ::= [in] | in out | out
1430 -- There is no explicit node in the tree for the Mode. Instead the
1431 -- In_Present and Out_Present flags are set in the parent node to
1432 -- record the presence of keywords specifying the mode.
1434 -- Error_Recovery: cannot raise Error_Resync
1436 procedure P_Mode (Node : Node_Id) is
1438 if Token = Tok_In then
1440 Set_In_Present (Node, True);
1442 if Style.Mode_In_Check and then Token /= Tok_Out then
1443 Error_Msg_SP -- CODEFIX
1444 ("(style) IN should be omitted");
1447 if Token = Tok_Access then
1448 Error_Msg_SP ("IN not allowed together with ACCESS");
1449 Scan; -- past ACCESS
1453 if Token = Tok_Out then
1455 Set_Out_Present (Node, True);
1458 if Token = Tok_In then
1459 Error_Msg_SC ("IN must precede OUT in parameter mode");
1461 Set_In_Present (Node, True);
1465 --------------------------
1466 -- 6.3 Subprogram Body --
1467 --------------------------
1469 -- Parsed by P_Subprogram (6.1)
1471 -----------------------------------
1472 -- 6.4 Procedure Call Statement --
1473 -----------------------------------
1475 -- Parsed by P_Sequence_Of_Statements (5.1)
1477 ------------------------
1478 -- 6.4 Function Call --
1479 ------------------------
1481 -- Parsed by P_Call_Or_Name (4.1)
1483 --------------------------------
1484 -- 6.4 Actual Parameter Part --
1485 --------------------------------
1487 -- Parsed by P_Call_Or_Name (4.1)
1489 --------------------------------
1490 -- 6.4 Parameter Association --
1491 --------------------------------
1493 -- Parsed by P_Call_Or_Name (4.1)
1495 ------------------------------------
1496 -- 6.4 Explicit Actual Parameter --
1497 ------------------------------------
1499 -- Parsed by P_Call_Or_Name (4.1)
1501 ---------------------------
1502 -- 6.5 Return Statement --
1503 ---------------------------
1505 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1507 -- EXTENDED_RETURN_STATEMENT ::=
1508 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1509 -- [:= EXPRESSION] [do
1510 -- HANDLED_SEQUENCE_OF_STATEMENTS
1513 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1515 -- RETURN_STATEMENT ::= return [EXPRESSION];
1517 -- Error recovery: can raise Error_Resync
1519 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1521 -- Note: We don't need to check Ada_Version here, because this is
1522 -- only called in >= Ada 2005 cases anyway.
1524 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1527 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1529 if Token = Tok_Access then
1530 Set_Object_Definition
1531 (Decl_Node, P_Access_Definition (Not_Null_Present));
1533 Set_Object_Definition
1534 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1536 end P_Return_Subtype_Indication;
1538 -- Error recovery: can raise Error_Resync
1540 function P_Return_Object_Declaration return Node_Id is
1541 Return_Obj : Node_Id;
1542 Decl_Node : Node_Id;
1545 Return_Obj := Token_Node;
1546 Change_Identifier_To_Defining_Identifier (Return_Obj);
1547 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1548 Set_Defining_Identifier (Decl_Node, Return_Obj);
1550 Scan; -- past identifier
1553 -- First an error check, if we have two identifiers in a row, a likely
1554 -- possibility is that the first of the identifiers is an incorrectly
1555 -- spelled keyword. See similar check in P_Identifier_Declarations.
1557 if Token = Tok_Identifier then
1559 SS : Saved_Scan_State;
1563 Save_Scan_State (SS);
1564 Scan; -- past initial identifier
1565 I2 := (Token = Tok_Identifier);
1566 Restore_Scan_State (SS);
1570 (Bad_Spelling_Of (Tok_Access) or else
1571 Bad_Spelling_Of (Tok_Aliased) or else
1572 Bad_Spelling_Of (Tok_Constant))
1579 -- We allow "constant" here (as in "return Result : constant
1580 -- T..."). This is not in the latest RM, but the ARG is considering an
1581 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1583 if Token = Tok_Constant then
1584 Scan; -- past CONSTANT
1585 Set_Constant_Present (Decl_Node);
1587 if Token = Tok_Aliased then
1588 Error_Msg_SC -- CODEFIX
1589 ("ALIASED should be before CONSTANT");
1590 Scan; -- past ALIASED
1591 Set_Aliased_Present (Decl_Node);
1594 elsif Token = Tok_Aliased then
1595 Scan; -- past ALIASED
1596 Set_Aliased_Present (Decl_Node);
1598 if Token = Tok_Constant then
1599 Scan; -- past CONSTANT
1600 Set_Constant_Present (Decl_Node);
1604 P_Return_Subtype_Indication (Decl_Node);
1606 if Token = Tok_Colon_Equal then
1608 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1612 end P_Return_Object_Declaration;
1614 -- Error recovery: can raise Error_Resync
1616 function P_Return_Statement return Node_Id is
1617 -- The caller has checked that the initial token is RETURN
1619 function Is_Simple return Boolean;
1620 -- Scan state is just after RETURN (and is left that way).
1621 -- Determine whether this is a simple or extended return statement
1622 -- by looking ahead for "identifier :", which implies extended.
1628 function Is_Simple return Boolean is
1629 Scan_State : Saved_Scan_State;
1630 Result : Boolean := True;
1633 if Token = Tok_Identifier then
1634 Save_Scan_State (Scan_State); -- at identifier
1635 Scan; -- past identifier
1637 if Token = Tok_Colon then
1638 Result := False; -- It's an extended_return_statement.
1641 Restore_Scan_State (Scan_State); -- to identifier
1647 Return_Sloc : constant Source_Ptr := Token_Ptr;
1648 Return_Node : Node_Id;
1650 -- Start of processing for P_Return_Statement
1653 Scan; -- past RETURN
1655 -- Simple_return_statement, no expression, return an
1656 -- N_Simple_Return_Statement node with the expression field left Empty.
1658 if Token = Tok_Semicolon then
1660 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1665 -- Simple_return_statement with expression
1667 -- We avoid trying to scan an expression if we are at an
1668 -- expression terminator since in that case the best error
1669 -- message is probably that we have a missing semicolon.
1672 Return_Node := New_Node (N_Simple_Return_Statement, Return_Sloc);
1674 if Token not in Token_Class_Eterm then
1675 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1678 -- Extended_return_statement (Ada 2005 only -- AI-318):
1681 if Ada_Version < Ada_05 then
1683 (" extended_return_statement is an Ada 2005 extension");
1684 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1687 Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1688 Set_Return_Object_Declarations
1689 (Return_Node, New_List (P_Return_Object_Declaration));
1691 if Token = Tok_Do then
1693 Scope.Table (Scope.Last).Etyp := E_Return;
1694 Scope.Table (Scope.Last).Ecol := Start_Column;
1695 Scope.Table (Scope.Last).Sloc := Return_Sloc;
1698 Set_Handled_Statement_Sequence
1699 (Return_Node, P_Handled_Sequence_Of_Statements);
1702 -- Do we need to handle Error_Resync here???
1710 end P_Return_Statement;