1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 pragma Style_Checks (All_Checks);
28 -- Turn off subprogram body ordering check. Subprograms are in order
29 -- by RM section rather than alphabetical
31 with Sinfo.CN; use Sinfo.CN;
36 -- Local subprograms, used only in this chapter
38 function P_Defining_Designator return Node_Id;
39 function P_Defining_Operator_Symbol return Node_Id;
40 function P_Return_Object_Declaration return Node_Id;
42 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id);
43 -- Decl_Node is a N_Object_Declaration.
44 -- Set the Null_Exclusion_Present and Object_Definition fields of
47 procedure Check_Junk_Semicolon_Before_Return;
49 -- Check for common error of junk semicolon before RETURN keyword of
50 -- function specification. If present, skip over it with appropriate
51 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
52 -- routine also deals with a possibly misspelled version of Return.
54 ----------------------------------------
55 -- Check_Junk_Semicolon_Before_Return --
56 ----------------------------------------
58 procedure Check_Junk_Semicolon_Before_Return is
59 Scan_State : Saved_Scan_State;
62 if Token = Tok_Semicolon then
63 Save_Scan_State (Scan_State);
64 Scan; -- past the semicolon
66 if Token = Tok_Return then
67 Restore_Scan_State (Scan_State);
68 Error_Msg_SC ("unexpected semicolon ignored");
69 Scan; -- rescan past junk semicolon
72 Restore_Scan_State (Scan_State);
75 elsif Bad_Spelling_Of (Tok_Return) then
78 end Check_Junk_Semicolon_Before_Return;
80 -----------------------------------------------------
81 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
82 -----------------------------------------------------
84 -- This routine scans out a subprogram declaration, subprogram body,
85 -- subprogram renaming declaration or subprogram generic instantiation.
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 -- The value in Pf_Flags indicates which of these possible declarations
127 -- is acceptable to the caller:
129 -- Pf_Flags.Decl Set if declaration OK
130 -- Pf_Flags.Gins Set if generic instantiation OK
131 -- Pf_Flags.Pbod Set if proper body OK
132 -- Pf_Flags.Rnam Set if renaming declaration OK
133 -- Pf_Flags.Stub Set if body stub OK
135 -- If an inappropriate form is encountered, it is scanned out but an
136 -- error message indicating that it is appearing in an inappropriate
137 -- context is issued. The only possible values for Pf_Flags are those
138 -- defined as constants in the Par package.
140 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
141 -- NOT or OVERRIDING.
143 -- Error recovery: cannot raise Error_Resync
145 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
146 Specification_Node : Node_Id;
148 Fpart_List : List_Id;
149 Fpart_Sloc : Source_Ptr;
150 Result_Not_Null : Boolean := False;
151 Result_Node : Node_Id;
155 Rename_Node : Node_Id;
156 Absdec_Node : Node_Id;
158 Fproc_Sloc : Source_Ptr;
160 Scan_State : Saved_Scan_State;
162 -- Flags for optional overriding indication. Two flags are needed,
163 -- to distinguish positive and negative overriding indicators from
164 -- the absence of any indicator.
166 Is_Overriding : Boolean := False;
167 Not_Overriding : Boolean := False;
170 -- Set up scope stack entry. Note that the Labl field will be set later
172 SIS_Entry_Active := False;
173 SIS_Missing_Semicolon_Message := No_Error_Msg;
175 Scope.Table (Scope.Last).Sloc := Token_Ptr;
176 Scope.Table (Scope.Last).Etyp := E_Name;
177 Scope.Table (Scope.Last).Ecol := Start_Column;
178 Scope.Table (Scope.Last).Lreq := False;
180 -- Ada2005: scan leading overriding indicator
182 if Token = Tok_Not then
185 if Token = Tok_Overriding then
186 Scan; -- past OVERRIDING
187 Not_Overriding := True;
189 Error_Msg_SC ("OVERRIDING expected!");
192 elsif Token = Tok_Overriding then
193 Scan; -- past OVERRIDING
194 Is_Overriding := True;
197 if (Is_Overriding or else Not_Overriding) then
198 if Ada_Version < Ada_05 then
199 Error_Msg_SP (" overriding indicator is an Ada 2005 extension");
200 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
202 -- An overriding indicator is allowed for subprogram declarations,
203 -- bodies, renamings, stubs, and instantiations.
205 elsif Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub then
206 Error_Msg_SC ("overriding indicator not allowed here!");
208 elsif Token /= Tok_Function
209 and then Token /= Tok_Procedure
211 Error_Msg_SC ("FUNCTION or PROCEDURE expected!");
215 Func := (Token = Tok_Function);
216 Fproc_Sloc := Token_Ptr;
217 Scan; -- past FUNCTION or PROCEDURE
222 Name_Node := P_Defining_Designator;
224 if Nkind (Name_Node) = N_Defining_Operator_Symbol
225 and then Scope.Last = 1
227 Error_Msg_SP ("operator symbol not allowed at library level");
228 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
230 -- Set name from file name, we need some junk name, and that's
231 -- as good as anything. This is only approximate, since we do
232 -- not do anything with non-standard name translations.
234 Get_Name_String (File_Name (Current_Source_File));
236 for J in 1 .. Name_Len loop
237 if Name_Buffer (J) = '.' then
243 Set_Chars (Name_Node, Name_Find);
244 Set_Error_Posted (Name_Node);
248 Name_Node := P_Defining_Program_Unit_Name;
251 Scope.Table (Scope.Last).Labl := Name_Node;
253 if Token = Tok_Colon then
254 Error_Msg_SC ("redundant colon ignored");
258 -- Deal with generic instantiation, the one case in which we do not
259 -- have a subprogram specification as part of whatever we are parsing
261 if Token = Tok_Is then
262 Save_Scan_State (Scan_State); -- at the IS
263 T_Is; -- checks for redundant IS
265 if Token = Tok_New then
266 if not Pf_Flags.Gins then
267 Error_Msg_SC ("generic instantation not allowed here!");
273 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
274 Set_Name (Inst_Node, P_Function_Name);
276 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
277 Set_Name (Inst_Node, P_Qualified_Simple_Name);
280 Set_Defining_Unit_Name (Inst_Node, Name_Node);
281 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
283 Pop_Scope_Stack; -- Don't need scope stack entry in this case
285 if Is_Overriding then
286 Set_Must_Override (Inst_Node);
288 elsif Not_Overriding then
289 Set_Must_Not_Override (Inst_Node);
295 Restore_Scan_State (Scan_State); -- to the IS
299 -- If not a generic instantiation, then we definitely have a subprogram
300 -- specification (all possibilities at this stage include one here)
302 Fpart_Sloc := Token_Ptr;
304 Check_Misspelling_Of (Tok_Return);
306 -- Scan formal part. First a special error check. If we have an
307 -- identifier here, then we have a definite error. If this identifier
308 -- is on the same line as the designator, then we assume it is the
309 -- first formal after a missing left parenthesis
311 if Token = Tok_Identifier
312 and then not Token_Is_At_Start_Of_Line
314 T_Left_Paren; -- to generate message
315 Fpart_List := P_Formal_Part;
317 -- Otherwise scan out an optional formal part in the usual manner
320 Fpart_List := P_Parameter_Profile;
323 -- We treat what we have as a function specification if FUNCTION was
324 -- used, or if a RETURN is present. This gives better error recovery
325 -- since later RETURN statements will be valid in either case.
327 Check_Junk_Semicolon_Before_Return;
328 Result_Node := Error;
330 if Token = Tok_Return then
332 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
338 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
340 -- Ada 2005 (AI-318-02)
342 if Token = Tok_Access then
343 if Ada_Version < Ada_05 then
345 ("anonymous access result type is an Ada 2005 extension");
346 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
349 Result_Node := P_Access_Definition (Result_Not_Null);
352 Result_Node := P_Subtype_Mark;
358 Ignore (Tok_Right_Paren);
364 Specification_Node :=
365 New_Node (N_Function_Specification, Fproc_Sloc);
367 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
368 Set_Result_Definition (Specification_Node, Result_Node);
371 Specification_Node :=
372 New_Node (N_Procedure_Specification, Fproc_Sloc);
375 Set_Defining_Unit_Name (Specification_Node, Name_Node);
376 Set_Parameter_Specifications (Specification_Node, Fpart_List);
378 if Is_Overriding then
379 Set_Must_Override (Specification_Node);
381 elsif Not_Overriding then
382 Set_Must_Not_Override (Specification_Node);
385 -- Error check: barriers not allowed on protected functions/procedures
387 if Token = Tok_When then
389 Error_Msg_SC ("barrier not allowed on function, only on entry");
391 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
395 Discard_Junk_Node (P_Expression);
398 -- Deal with case of semicolon ending a subprogram declaration
400 if Token = Tok_Semicolon then
401 if not Pf_Flags.Decl then
405 Scan; -- past semicolon
407 -- If semicolon is immediately followed by IS, then ignore the
408 -- semicolon, and go process the body.
410 if Token = Tok_Is then
411 Error_Msg_SP ("unexpected semicolon ignored");
412 T_Is; -- ignroe redundant IS's
413 goto Subprogram_Body;
415 -- If BEGIN follows in an appropriate column, we immediately
416 -- commence the error action of assuming that the previous
417 -- subprogram declaration should have been a subprogram body,
418 -- i.e. that the terminating semicolon should have been IS.
420 elsif Token = Tok_Begin
421 and then Start_Column >= Scope.Table (Scope.Last).Ecol
423 Error_Msg_SP (""";"" should be IS!");
424 goto Subprogram_Body;
427 goto Subprogram_Declaration;
430 -- Case of not followed by semicolon
433 -- Subprogram renaming declaration case
435 Check_Misspelling_Of (Tok_Renames);
437 if Token = Tok_Renames then
438 if not Pf_Flags.Rnam then
439 Error_Msg_SC ("renaming declaration not allowed here!");
443 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
444 Scan; -- past RENAMES
445 Set_Name (Rename_Node, P_Name);
446 Set_Specification (Rename_Node, Specification_Node);
451 -- Case of IS following subprogram specification
453 elsif Token = Tok_Is then
454 T_Is; -- ignore redundant Is's
456 if Token_Name = Name_Abstract then
457 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
460 -- Deal nicely with (now obsolete) use of <> in place of abstract
462 if Token = Tok_Box then
463 Error_Msg_SC ("ABSTRACT expected");
464 Token := Tok_Abstract;
467 -- Abstract subprogram declaration case
469 if Token = Tok_Abstract then
471 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
472 Set_Specification (Absdec_Node, Specification_Node);
473 Pop_Scope_Stack; -- discard unneeded entry
474 Scan; -- past ABSTRACT
478 -- Ada 2005 (AI-248): Parse a null procedure declaration
480 elsif Token = Tok_Null then
481 if Ada_Version < Ada_05 then
482 Error_Msg_SP ("null procedures are an Ada 2005 extension");
483 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
489 Error_Msg_SP ("only procedures can be null");
491 Set_Null_Present (Specification_Node);
495 goto Subprogram_Declaration;
497 -- Check for IS NEW with Formal_Part present and handle nicely
499 elsif Token = Tok_New then
501 ("formal part not allowed in instantiation", Fpart_Sloc);
505 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
508 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
511 Set_Defining_Unit_Name (Inst_Node, Name_Node);
512 Set_Name (Inst_Node, P_Name);
513 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
515 Pop_Scope_Stack; -- Don't need scope stack entry in this case
519 goto Subprogram_Body;
522 -- Here we have a missing IS or missing semicolon, we always guess
523 -- a missing semicolon, since we are pretty good at fixing up a
524 -- semicolon which should really be an IS
527 Error_Msg_AP ("missing "";""");
528 SIS_Missing_Semicolon_Message := Get_Msg_Id;
529 goto Subprogram_Declaration;
533 -- Processing for subprogram body
536 if not Pf_Flags.Pbod then
537 Error_Msg_SP ("subprogram body not allowed here!");
540 -- Subprogram body stub case
542 if Separate_Present then
543 if not Pf_Flags.Stub then
544 Error_Msg_SC ("body stub not allowed here!");
547 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
549 ("operator symbol cannot be used as subunit name",
554 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
555 Set_Specification (Stub_Node, Specification_Node);
556 Scan; -- past SEPARATE
561 -- Subprogram body case
564 -- Here is the test for a suspicious IS (i.e. one that looks
565 -- like it might more properly be a semicolon). See separate
566 -- section discussing use of IS instead of semicolon in
569 if (Token in Token_Class_Declk
571 Token = Tok_Identifier)
572 and then Start_Column <= Scope.Table (Scope.Last).Ecol
573 and then Scope.Last /= 1
575 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
576 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
580 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
581 Set_Specification (Body_Node, Specification_Node);
582 Parse_Decls_Begin_End (Body_Node);
586 -- Processing for subprogram declaration
588 <<Subprogram_Declaration>>
590 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
591 Set_Specification (Decl_Node, Specification_Node);
593 -- If this is a context in which a subprogram body is permitted,
594 -- set active SIS entry in case (see section titled "Handling
595 -- Semicolon Used in Place of IS" in body of Parser package)
596 -- Note that SIS_Missing_Semicolon_Message is already set properly.
598 if Pf_Flags.Pbod then
599 SIS_Labl := Scope.Table (Scope.Last).Labl;
600 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
601 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
602 SIS_Declaration_Node := Decl_Node;
603 SIS_Semicolon_Sloc := Prev_Token_Ptr;
604 SIS_Entry_Active := True;
612 ---------------------------------
613 -- 6.1 Subprogram Declaration --
614 ---------------------------------
616 -- Parsed by P_Subprogram (6.1)
618 ------------------------------------------
619 -- 6.1 Abstract Subprogram Declaration --
620 ------------------------------------------
622 -- Parsed by P_Subprogram (6.1)
624 -----------------------------------
625 -- 6.1 Subprogram Specification --
626 -----------------------------------
628 -- SUBPROGRAM_SPECIFICATION ::=
629 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
630 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
632 -- PARAMETER_PROFILE ::= [FORMAL_PART]
634 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
636 -- Subprogram specifications that appear in subprogram declarations
637 -- are parsed by P_Subprogram (6.1). This routine is used in other
638 -- contexts where subprogram specifications occur.
640 -- Note: this routine does not affect the scope stack in any way
642 -- Error recovery: can raise Error_Resync
644 function P_Subprogram_Specification return Node_Id is
645 Specification_Node : Node_Id;
646 Result_Not_Null : Boolean;
647 Result_Node : Node_Id;
650 if Token = Tok_Function then
651 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
652 Scan; -- past FUNCTION
654 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
655 Set_Parameter_Specifications
656 (Specification_Node, P_Parameter_Profile);
657 Check_Junk_Semicolon_Before_Return;
660 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
662 -- Ada 2005 (AI-318-02)
664 if Token = Tok_Access then
665 if Ada_Version < Ada_05 then
667 ("anonymous access result type is an Ada 2005 extension");
668 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
671 Result_Node := P_Access_Definition (Result_Not_Null);
674 Result_Node := P_Subtype_Mark;
678 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
679 Set_Result_Definition (Specification_Node, Result_Node);
680 return Specification_Node;
682 elsif Token = Tok_Procedure then
683 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
684 Scan; -- past PROCEDURE
686 Set_Defining_Unit_Name
687 (Specification_Node, P_Defining_Program_Unit_Name);
688 Set_Parameter_Specifications
689 (Specification_Node, P_Parameter_Profile);
690 return Specification_Node;
693 Error_Msg_SC ("subprogram specification expected");
696 end P_Subprogram_Specification;
698 ---------------------
700 ---------------------
703 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
705 -- The caller has checked that the initial token is an identifier,
706 -- operator symbol, or string literal. Note that we don't bother to
707 -- do much error diagnosis in this routine, since it is only used for
708 -- the label on END lines, and the routines in package Par.Endh will
709 -- check that the label is appropriate.
711 -- Error recovery: cannot raise Error_Resync
713 function P_Designator return Node_Id is
714 Ident_Node : Node_Id;
716 Prefix_Node : Node_Id;
718 function Real_Dot return Boolean;
719 -- Tests if a current token is an interesting period, i.e. is followed
720 -- by an identifier or operator symbol or string literal. If not, it is
721 -- probably just incorrect punctuation to be caught by our caller. Note
722 -- that the case of an operator symbol or string literal is also an
723 -- error, but that is an error that we catch here. If the result is
724 -- True, a real dot has been scanned and we are positioned past it,
725 -- if the result is False, the scan position is unchanged.
731 function Real_Dot return Boolean is
732 Scan_State : Saved_Scan_State;
735 if Token /= Tok_Dot then
739 Save_Scan_State (Scan_State);
742 if Token = Tok_Identifier
743 or else Token = Tok_Operator_Symbol
744 or else Token = Tok_String_Literal
749 Restore_Scan_State (Scan_State);
755 -- Start of processing for P_Designator
758 Ident_Node := Token_Node;
759 Scan; -- past initial token
761 if Prev_Token = Tok_Operator_Symbol
762 or else Prev_Token = Tok_String_Literal
770 Prefix_Node := Ident_Node;
772 -- Loop through child names, on entry to this loop, Prefix contains
773 -- the name scanned so far, and Ident_Node is the last identifier.
776 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
777 Set_Prefix (Name_Node, Prefix_Node);
778 Ident_Node := P_Identifier;
779 Set_Selector_Name (Name_Node, Ident_Node);
780 Prefix_Node := Name_Node;
781 exit when not Real_Dot;
784 -- On exit from the loop, Ident_Node is the last identifier scanned,
785 -- i.e. the defining identifier, and Prefix_Node is a node for the
786 -- entire name, structured (incorrectly!) as a selected component.
788 Name_Node := Prefix (Prefix_Node);
789 Change_Node (Prefix_Node, N_Designator);
790 Set_Name (Prefix_Node, Name_Node);
791 Set_Identifier (Prefix_Node, Ident_Node);
797 while Token = Tok_Dot or else Token = Tok_Identifier loop
804 ------------------------------
805 -- 6.1 Defining Designator --
806 ------------------------------
808 -- DEFINING_DESIGNATOR ::=
809 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
811 -- Error recovery: cannot raise Error_Resync
813 function P_Defining_Designator return Node_Id is
815 if Token = Tok_Operator_Symbol then
816 return P_Defining_Operator_Symbol;
818 elsif Token = Tok_String_Literal then
819 Error_Msg_SC ("invalid operator name");
820 Scan; -- past junk string
824 return P_Defining_Program_Unit_Name;
826 end P_Defining_Designator;
828 -------------------------------------
829 -- 6.1 Defining Program Unit Name --
830 -------------------------------------
832 -- DEFINING_PROGRAM_UNIT_NAME ::=
833 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
835 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
837 -- Error recovery: cannot raise Error_Resync
839 function P_Defining_Program_Unit_Name return Node_Id is
840 Ident_Node : Node_Id;
842 Prefix_Node : Node_Id;
845 -- Set identifier casing if not already set and scan initial identifier
847 if Token = Tok_Identifier
848 and then Identifier_Casing (Current_Source_File) = Unknown
850 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
853 Ident_Node := P_Identifier (C_Dot);
854 Merge_Identifier (Ident_Node, Tok_Return);
856 -- Normal case (not child library unit name)
858 if Token /= Tok_Dot then
859 Change_Identifier_To_Defining_Identifier (Ident_Node);
862 -- Child library unit name case
865 if Scope.Last > 1 then
866 Error_Msg_SP ("child unit allowed only at library level");
869 elsif Ada_Version = Ada_83 then
870 Error_Msg_SP ("(Ada 83) child unit not allowed!");
874 Prefix_Node := Ident_Node;
876 -- Loop through child names, on entry to this loop, Prefix contains
877 -- the name scanned so far, and Ident_Node is the last identifier.
880 exit when Token /= Tok_Dot;
881 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
883 Set_Prefix (Name_Node, Prefix_Node);
884 Ident_Node := P_Identifier (C_Dot);
885 Set_Selector_Name (Name_Node, Ident_Node);
886 Prefix_Node := Name_Node;
889 -- On exit from the loop, Ident_Node is the last identifier scanned,
890 -- i.e. the defining identifier, and Prefix_Node is a node for the
891 -- entire name, structured (incorrectly!) as a selected component.
893 Name_Node := Prefix (Prefix_Node);
894 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
895 Set_Name (Prefix_Node, Name_Node);
896 Change_Identifier_To_Defining_Identifier (Ident_Node);
897 Set_Defining_Identifier (Prefix_Node, Ident_Node);
899 -- All set with unit name parsed
906 while Token = Tok_Dot or else Token = Tok_Identifier loop
911 end P_Defining_Program_Unit_Name;
913 --------------------------
914 -- 6.1 Operator Symbol --
915 --------------------------
917 -- OPERATOR_SYMBOL ::= STRING_LITERAL
919 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
921 -----------------------------------
922 -- 6.1 Defining Operator Symbol --
923 -----------------------------------
925 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
927 -- The caller has checked that the initial symbol is an operator symbol
929 function P_Defining_Operator_Symbol return Node_Id is
933 Op_Node := Token_Node;
934 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
935 Scan; -- past operator symbol
937 end P_Defining_Operator_Symbol;
939 ----------------------------
940 -- 6.1 Parameter_Profile --
941 ----------------------------
943 -- PARAMETER_PROFILE ::= [FORMAL_PART]
945 -- Empty is returned if no formal part is present
947 -- Error recovery: cannot raise Error_Resync
949 function P_Parameter_Profile return List_Id is
951 if Token = Tok_Left_Paren then
952 Scan; -- part left paren
953 return P_Formal_Part;
957 end P_Parameter_Profile;
959 ---------------------------------------
960 -- 6.1 Parameter And Result Profile --
961 ---------------------------------------
963 -- Parsed by its parent construct, which uses P_Parameter_Profile to
964 -- parse the parameters, and P_Subtype_Mark to parse the return type.
966 ----------------------
967 -- 6.1 Formal part --
968 ----------------------
970 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
972 -- PARAMETER_SPECIFICATION ::=
973 -- DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
974 -- [:= DEFAULT_EXPRESSION]
975 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
976 -- [:= DEFAULT_EXPRESSION]
978 -- This scans the construct Formal_Part. The caller has already checked
979 -- that the initial token is a left parenthesis, and skipped past it, so
980 -- that on entry Token is the first token following the left parenthesis.
982 -- Error recovery: cannot raise Error_Resync
984 function P_Formal_Part return List_Id is
985 Specification_List : List_Id;
986 Specification_Node : Node_Id;
987 Scan_State : Saved_Scan_State;
990 Ident_Sloc : Source_Ptr;
991 Not_Null_Present : Boolean := False;
993 Idents : array (Int range 1 .. 4096) of Entity_Id;
994 -- This array holds the list of defining identifiers. The upper bound
995 -- of 4096 is intended to be essentially infinite, and we do not even
996 -- bother to check for it being exceeded.
999 Specification_List := New_List;
1000 Specification_Loop : loop
1002 if Token = Tok_Pragma then
1003 P_Pragmas_Misplaced;
1006 Ignore (Tok_Left_Paren);
1007 Ident_Sloc := Token_Ptr;
1008 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1012 exit Ident_Loop when Token = Tok_Colon;
1014 -- The only valid tokens are colon and comma, so if we have
1015 -- neither do a bit of investigation to see which is the
1016 -- better choice for insertion.
1018 if Token /= Tok_Comma then
1020 -- Assume colon if IN or OUT keyword found
1022 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1024 -- Otherwise scan ahead
1026 Save_Scan_State (Scan_State);
1029 -- If we run into a semicolon, then assume that a
1030 -- colon was missing, e.g. Parms (X Y; ...). Also
1031 -- assume missing colon on EOF (a real disaster!)
1032 -- and on a right paren, e.g. Parms (X Y), and also
1033 -- on an assignment symbol, e.g. Parms (X Y := ..)
1035 if Token = Tok_Semicolon
1036 or else Token = Tok_Right_Paren
1037 or else Token = Tok_EOF
1038 or else Token = Tok_Colon_Equal
1040 Restore_Scan_State (Scan_State);
1043 -- If we run into a colon, assume that we had a missing
1044 -- comma, e.g. Parms (A B : ...). Also assume a missing
1045 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1047 elsif Token = Tok_Colon
1048 or else Token = Tok_Comma
1050 Restore_Scan_State (Scan_State);
1055 end loop Look_Ahead;
1058 -- Here if a comma is present, or to be assumed
1061 Num_Idents := Num_Idents + 1;
1062 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1063 end loop Ident_Loop;
1065 -- Fall through the loop on encountering a colon, or deciding
1066 -- that there is a missing colon.
1070 -- If there are multiple identifiers, we repeatedly scan the
1071 -- type and initialization expression information by resetting
1072 -- the scan pointer (so that we get completely separate trees
1073 -- for each occurrence).
1075 if Num_Idents > 1 then
1076 Save_Scan_State (Scan_State);
1079 -- Loop through defining identifiers in list
1083 Ident_List_Loop : loop
1084 Specification_Node :=
1085 New_Node (N_Parameter_Specification, Ident_Sloc);
1086 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1088 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1091 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1093 -- Case of ACCESS keyword present
1095 if Token = Tok_Access then
1096 Set_Null_Exclusion_Present
1097 (Specification_Node, Not_Null_Present);
1099 if Ada_Version = Ada_83 then
1100 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1104 (Specification_Node,
1105 P_Access_Definition (Not_Null_Present));
1107 -- Case of IN or OUT present
1110 if Token = Tok_In or else Token = Tok_Out then
1111 if Not_Null_Present then
1113 ("ACCESS must be placed after the parameter mode");
1116 P_Mode (Specification_Node);
1117 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1120 Set_Null_Exclusion_Present
1121 (Specification_Node, Not_Null_Present);
1123 if Token = Tok_Procedure
1125 Token = Tok_Function
1127 Error_Msg_SC ("formal subprogram parameter not allowed");
1130 if Token = Tok_Left_Paren then
1131 Discard_Junk_List (P_Formal_Part);
1134 if Token = Tok_Return then
1136 Discard_Junk_Node (P_Subtype_Mark);
1139 Set_Parameter_Type (Specification_Node, Error);
1142 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1147 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1150 Set_Prev_Ids (Specification_Node, True);
1153 if Ident < Num_Idents then
1154 Set_More_Ids (Specification_Node, True);
1157 Append (Specification_Node, Specification_List);
1158 exit Ident_List_Loop when Ident = Num_Idents;
1160 Restore_Scan_State (Scan_State);
1161 end loop Ident_List_Loop;
1164 when Error_Resync =>
1165 Resync_Semicolon_List;
1168 if Token = Tok_Semicolon then
1169 Save_Scan_State (Scan_State);
1170 Scan; -- past semicolon
1172 -- If we have RETURN or IS after the semicolon, then assume
1173 -- that semicolon should have been a right parenthesis and exit
1175 if Token = Tok_Is or else Token = Tok_Return then
1176 Error_Msg_SP ("expected "")"" in place of "";""");
1177 exit Specification_Loop;
1180 -- If we have a declaration keyword after the semicolon, then
1181 -- assume we had a missing right parenthesis and terminate list
1183 if Token in Token_Class_Declk then
1184 Error_Msg_AP ("missing "")""");
1185 Restore_Scan_State (Scan_State);
1186 exit Specification_Loop;
1189 elsif Token = Tok_Right_Paren then
1190 Scan; -- past right paren
1191 exit Specification_Loop;
1193 -- Special check for common error of using comma instead of semicolon
1195 elsif Token = Tok_Comma then
1199 -- Special check for omitted separator
1201 elsif Token = Tok_Identifier then
1204 -- If nothing sensible, skip to next semicolon or right paren
1208 Resync_Semicolon_List;
1210 if Token = Tok_Semicolon then
1211 Scan; -- past semicolon
1214 exit Specification_Loop;
1217 end loop Specification_Loop;
1219 return Specification_List;
1222 ----------------------------------
1223 -- 6.1 Parameter Specification --
1224 ----------------------------------
1226 -- Parsed by P_Formal_Part (6.1)
1232 -- MODE ::= [in] | in out | out
1234 -- There is no explicit node in the tree for the Mode. Instead the
1235 -- In_Present and Out_Present flags are set in the parent node to
1236 -- record the presence of keywords specifying the mode.
1238 -- Error_Recovery: cannot raise Error_Resync
1240 procedure P_Mode (Node : Node_Id) is
1242 if Token = Tok_In then
1244 Set_In_Present (Node, True);
1246 if Style.Mode_In_Check and then Token /= Tok_Out then
1247 Error_Msg_SP ("(style) IN should be omitted");
1250 if Token = Tok_Access then
1251 Error_Msg_SP ("IN not allowed together with ACCESS");
1252 Scan; -- past ACCESS
1256 if Token = Tok_Out then
1258 Set_Out_Present (Node, True);
1261 if Token = Tok_In then
1262 Error_Msg_SC ("IN must preceed OUT in parameter mode");
1264 Set_In_Present (Node, True);
1268 --------------------------
1269 -- 6.3 Subprogram Body --
1270 --------------------------
1272 -- Parsed by P_Subprogram (6.1)
1274 -----------------------------------
1275 -- 6.4 Procedure Call Statement --
1276 -----------------------------------
1278 -- Parsed by P_Sequence_Of_Statements (5.1)
1280 ------------------------
1281 -- 6.4 Function Call --
1282 ------------------------
1284 -- Parsed by P_Call_Or_Name (4.1)
1286 --------------------------------
1287 -- 6.4 Actual Parameter Part --
1288 --------------------------------
1290 -- Parsed by P_Call_Or_Name (4.1)
1292 --------------------------------
1293 -- 6.4 Parameter Association --
1294 --------------------------------
1296 -- Parsed by P_Call_Or_Name (4.1)
1298 ------------------------------------
1299 -- 6.4 Explicit Actual Parameter --
1300 ------------------------------------
1302 -- Parsed by P_Call_Or_Name (4.1)
1304 ---------------------------
1305 -- 6.5 Return Statement --
1306 ---------------------------
1308 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1310 -- EXTENDED_RETURN_STATEMENT ::=
1311 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1312 -- [:= EXPRESSION] [do
1313 -- HANDLED_SEQUENCE_OF_STATEMENTS
1316 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1318 -- RETURN_STATEMENT ::= return [EXPRESSION];
1320 -- Error recovery: can raise Error_Resync
1322 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1324 -- Note: We don't need to check Ada_Version here, because this is
1325 -- only called in >= Ada 2005 cases anyway.
1327 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1330 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1332 if Token = Tok_Access then
1333 Set_Object_Definition
1334 (Decl_Node, P_Access_Definition (Not_Null_Present));
1336 Set_Object_Definition
1337 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1339 end P_Return_Subtype_Indication;
1341 -- Error recovery: can raise Error_Resync
1343 function P_Return_Object_Declaration return Node_Id is
1344 Return_Obj : Node_Id;
1345 Decl_Node : Node_Id;
1348 Return_Obj := Token_Node;
1349 Change_Identifier_To_Defining_Identifier (Return_Obj);
1350 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1351 Set_Defining_Identifier (Decl_Node, Return_Obj);
1353 Scan; -- past identifier
1356 -- First an error check, if we have two identifiers in a row, a likely
1357 -- possibility is that the first of the identifiers is an incorrectly
1358 -- spelled keyword. See similar check in P_Identifier_Declarations.
1360 if Token = Tok_Identifier then
1362 SS : Saved_Scan_State;
1366 Save_Scan_State (SS);
1367 Scan; -- past initial identifier
1368 I2 := (Token = Tok_Identifier);
1369 Restore_Scan_State (SS);
1373 (Bad_Spelling_Of (Tok_Access) or else
1374 Bad_Spelling_Of (Tok_Aliased) or else
1375 Bad_Spelling_Of (Tok_Constant))
1382 -- We allow "constant" here (as in "return Result : constant
1383 -- T..."). This is not in the latest RM, but the ARG is considering an
1384 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1386 if Token = Tok_Constant then
1387 Scan; -- past CONSTANT
1388 Set_Constant_Present (Decl_Node);
1390 if Token = Tok_Aliased then
1391 Error_Msg_SC ("ALIASED should be before CONSTANT");
1392 Scan; -- past ALIASED
1393 Set_Aliased_Present (Decl_Node);
1396 elsif Token = Tok_Aliased then
1397 Scan; -- past ALIASED
1398 Set_Aliased_Present (Decl_Node);
1400 if Token = Tok_Constant then
1401 Scan; -- past CONSTANT
1402 Set_Constant_Present (Decl_Node);
1406 P_Return_Subtype_Indication (Decl_Node);
1408 if Token = Tok_Colon_Equal then
1410 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1414 end P_Return_Object_Declaration;
1416 -- Error recovery: can raise Error_Resync
1418 function P_Return_Statement return Node_Id is
1419 -- The caller has checked that the initial token is RETURN
1421 function Is_Simple return Boolean;
1422 -- Scan state is just after RETURN (and is left that way).
1423 -- Determine whether this is a simple or extended return statement
1424 -- by looking ahead for "identifier :", which implies extended.
1430 function Is_Simple return Boolean is
1431 Scan_State : Saved_Scan_State;
1432 Result : Boolean := True;
1435 if Token = Tok_Identifier then
1436 Save_Scan_State (Scan_State); -- at identifier
1437 Scan; -- past identifier
1439 if Token = Tok_Colon then
1440 Result := False; -- It's an extended_return_statement.
1443 Restore_Scan_State (Scan_State); -- to identifier
1449 Return_Sloc : constant Source_Ptr := Token_Ptr;
1450 Return_Node : Node_Id;
1452 -- Start of processing for P_Return_Statement
1455 Scan; -- past RETURN
1457 -- Simple_return_statement, no expression, return an N_Return_Statement
1458 -- node with the expression field left Empty.
1460 if Token = Tok_Semicolon then
1462 Return_Node := New_Node (N_Return_Statement, Return_Sloc);
1467 -- Simple_return_statement with expression
1469 -- We avoid trying to scan an expression if we are at an
1470 -- expression terminator since in that case the best error
1471 -- message is probably that we have a missing semicolon.
1474 Return_Node := New_Node (N_Return_Statement, Return_Sloc);
1476 if Token not in Token_Class_Eterm then
1477 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1480 -- Extended_return_statement (Ada 2005 only -- AI-318):
1483 if Ada_Version < Ada_05 then
1485 (" extended_return_statement is an Ada 2005 extension");
1486 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1489 Return_Node := New_Node (N_Extended_Return_Statement, Return_Sloc);
1490 Set_Return_Object_Declarations
1491 (Return_Node, New_List (P_Return_Object_Declaration));
1493 if Token = Tok_Do then
1495 Scope.Table (Scope.Last).Etyp := E_Return;
1496 Scope.Table (Scope.Last).Ecol := Start_Column;
1497 Scope.Table (Scope.Last).Sloc := Return_Sloc;
1500 Set_Handled_Statement_Sequence
1501 (Return_Node, P_Handled_Sequence_Of_Statements);
1504 -- Do we need to handle Error_Resync here???
1512 end P_Return_Statement;