1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 pragma Style_Checks (All_Checks);
29 -- Turn off subprogram body ordering check. Subprograms are in order
30 -- by RM section rather than alphabetical
32 with Sinfo.CN; use Sinfo.CN;
37 -- Local subprograms, used only in this chapter
39 function P_Defining_Designator return Node_Id;
40 function P_Defining_Operator_Symbol return Node_Id;
42 procedure Check_Junk_Semicolon_Before_Return;
43 -- Check for common error of junk semicolon before RETURN keyword of
44 -- function specification. If present, skip over it with appropriate
45 -- error message, leaving Scan_Ptr pointing to the RETURN after. This
46 -- routine also deals with a possibly misspelled version of Return.
48 ----------------------------------------
49 -- Check_Junk_Semicolon_Before_Return --
50 ----------------------------------------
52 procedure Check_Junk_Semicolon_Before_Return is
53 Scan_State : Saved_Scan_State;
56 if Token = Tok_Semicolon then
57 Save_Scan_State (Scan_State);
58 Scan; -- past the semicolon
60 if Token = Tok_Return then
61 Restore_Scan_State (Scan_State);
62 Error_Msg_SC ("Unexpected semicolon ignored");
63 Scan; -- rescan past junk semicolon
66 Restore_Scan_State (Scan_State);
69 elsif Bad_Spelling_Of (Tok_Return) then
72 end Check_Junk_Semicolon_Before_Return;
74 -----------------------------------------------------
75 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
76 -----------------------------------------------------
78 -- This routine scans out a subprogram declaration, subprogram body,
79 -- subprogram renaming declaration or subprogram generic instantiation.
81 -- SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
83 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
84 -- SUBPROGRAM_SPECIFICATION is abstract;
86 -- SUBPROGRAM_SPECIFICATION ::=
87 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
88 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
90 -- PARAMETER_PROFILE ::= [FORMAL_PART]
92 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
94 -- SUBPROGRAM_BODY ::=
95 -- SUBPROGRAM_SPECIFICATION is
98 -- HANDLED_SEQUENCE_OF_STATEMENTS
101 -- SUBPROGRAM_RENAMING_DECLARATION ::=
102 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
104 -- SUBPROGRAM_BODY_STUB ::=
105 -- SUBPROGRAM_SPECIFICATION is separate;
107 -- GENERIC_INSTANTIATION ::=
108 -- procedure DEFINING_PROGRAM_UNIT_NAME is
109 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART];
110 -- | function DEFINING_DESIGNATOR is
111 -- new generic_function_NAME [GENERIC_ACTUAL_PART];
113 -- The value in Pf_Flags indicates which of these possible declarations
114 -- is acceptable to the caller:
116 -- Pf_Flags.Decl Set if declaration OK
117 -- Pf_Flags.Gins Set if generic instantiation OK
118 -- Pf_Flags.Pbod Set if proper body OK
119 -- Pf_Flags.Rnam Set if renaming declaration OK
120 -- Pf_Flags.Stub Set if body stub OK
122 -- If an inappropriate form is encountered, it is scanned out but an
123 -- error message indicating that it is appearing in an inappropriate
124 -- context is issued. The only possible values for Pf_Flags are those
125 -- defined as constants in the Par package.
127 -- The caller has checked that the initial token is FUNCTION or PROCEDURE
129 -- Error recovery: cannot raise Error_Resync
131 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
132 Specification_Node : Node_Id;
134 Fpart_List : List_Id;
135 Fpart_Sloc : Source_Ptr;
136 Return_Node : Node_Id;
140 Rename_Node : Node_Id;
141 Absdec_Node : Node_Id;
143 Fproc_Sloc : Source_Ptr;
145 Scan_State : Saved_Scan_State;
148 -- Set up scope stack entry. Note that the Labl field will be set later
150 SIS_Entry_Active := False;
151 SIS_Missing_Semicolon_Message := No_Error_Msg;
153 Scope.Table (Scope.Last).Sloc := Token_Ptr;
154 Scope.Table (Scope.Last).Etyp := E_Name;
155 Scope.Table (Scope.Last).Ecol := Start_Column;
156 Scope.Table (Scope.Last).Lreq := False;
158 Func := (Token = Tok_Function);
159 Fproc_Sloc := Token_Ptr;
160 Scan; -- past FUNCTION or PROCEDURE
165 Name_Node := P_Defining_Designator;
167 if Nkind (Name_Node) = N_Defining_Operator_Symbol
168 and then Scope.Last = 1
170 Error_Msg_SP ("operator symbol not allowed at library level");
171 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
173 -- Set name from file name, we need some junk name, and that's
174 -- as good as anything. This is only approximate, since we do
175 -- not do anything with non-standard name translations.
177 Get_Name_String (File_Name (Current_Source_File));
179 for J in 1 .. Name_Len loop
180 if Name_Buffer (J) = '.' then
186 Set_Chars (Name_Node, Name_Find);
187 Set_Error_Posted (Name_Node);
191 Name_Node := P_Defining_Program_Unit_Name;
194 Scope.Table (Scope.Last).Labl := Name_Node;
196 if Token = Tok_Colon then
197 Error_Msg_SC ("redundant colon ignored");
201 -- Deal with generic instantiation, the one case in which we do not
202 -- have a subprogram specification as part of whatever we are parsing
204 if Token = Tok_Is then
205 Save_Scan_State (Scan_State); -- at the IS
206 T_Is; -- checks for redundant IS's
208 if Token = Tok_New then
209 if not Pf_Flags.Gins then
210 Error_Msg_SC ("generic instantiation not allowed here!");
216 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
217 Set_Name (Inst_Node, P_Function_Name);
219 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
220 Set_Name (Inst_Node, P_Qualified_Simple_Name);
223 Set_Defining_Unit_Name (Inst_Node, Name_Node);
224 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
226 Pop_Scope_Stack; -- Don't need scope stack entry in this case
230 Restore_Scan_State (Scan_State); -- to the IS
234 -- If not a generic instantiation, then we definitely have a subprogram
235 -- specification (all possibilities at this stage include one here)
237 Fpart_Sloc := Token_Ptr;
239 Check_Misspelling_Of (Tok_Return);
241 -- Scan formal part. First a special error check. If we have an
242 -- identifier here, then we have a definite error. If this identifier
243 -- is on the same line as the designator, then we assume it is the
244 -- first formal after a missing left parenthesis
246 if Token = Tok_Identifier
247 and then not Token_Is_At_Start_Of_Line
249 T_Left_Paren; -- to generate message
250 Fpart_List := P_Formal_Part;
252 -- Otherwise scan out an optional formal part in the usual manner
255 Fpart_List := P_Parameter_Profile;
258 -- We treat what we have as a function specification if FUNCTION was
259 -- used, or if a RETURN is present. This gives better error recovery
260 -- since later RETURN statements will be valid in either case.
262 Check_Junk_Semicolon_Before_Return;
263 Return_Node := Error;
265 if Token = Tok_Return then
267 Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
272 Return_Node := P_Subtype_Mark;
277 Ignore (Tok_Right_Paren);
283 Specification_Node :=
284 New_Node (N_Function_Specification, Fproc_Sloc);
285 Set_Subtype_Mark (Specification_Node, Return_Node);
288 Specification_Node :=
289 New_Node (N_Procedure_Specification, Fproc_Sloc);
292 Set_Defining_Unit_Name (Specification_Node, Name_Node);
293 Set_Parameter_Specifications (Specification_Node, Fpart_List);
295 -- Error check: barriers not allowed on protected functions/procedures
297 if Token = Tok_When then
299 Error_Msg_SC ("barrier not allowed on function, only on entry");
301 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
305 Discard_Junk_Node (P_Expression);
308 -- Deal with case of semicolon ending a subprogram declaration
310 if Token = Tok_Semicolon then
311 if not Pf_Flags.Decl then
315 Scan; -- past semicolon
317 -- If semicolon is immediately followed by IS, then ignore the
318 -- semicolon, and go process the body.
320 if Token = Tok_Is then
321 Error_Msg_SP ("unexpected semicolon ignored");
322 T_Is; -- ignroe redundant IS's
323 goto Subprogram_Body;
325 -- If BEGIN follows in an appropriate column, we immediately
326 -- commence the error action of assuming that the previous
327 -- subprogram declaration should have been a subprogram body,
328 -- i.e. that the terminating semicolon should have been IS.
330 elsif Token = Tok_Begin
331 and then Start_Column >= Scope.Table (Scope.Last).Ecol
333 Error_Msg_SP (""";"" should be IS!");
334 goto Subprogram_Body;
337 goto Subprogram_Declaration;
340 -- Case of not followed by semicolon
343 -- Subprogram renaming declaration case
345 Check_Misspelling_Of (Tok_Renames);
347 if Token = Tok_Renames then
348 if not Pf_Flags.Rnam then
349 Error_Msg_SC ("renaming declaration not allowed here!");
353 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
354 Scan; -- past RENAMES
355 Set_Name (Rename_Node, P_Name);
356 Set_Specification (Rename_Node, Specification_Node);
361 -- Case of IS following subprogram specification
363 elsif Token = Tok_Is then
364 T_Is; -- ignore redundant Is's
366 if Token_Name = Name_Abstract then
367 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
370 -- Deal nicely with (now obsolete) use of <> in place of abstract
372 if Token = Tok_Box then
373 Error_Msg_SC ("ABSTRACT expected");
374 Token := Tok_Abstract;
377 -- Abstract subprogram declaration case
379 if Token = Tok_Abstract then
381 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
382 Set_Specification (Absdec_Node, Specification_Node);
383 Pop_Scope_Stack; -- discard unneeded entry
384 Scan; -- past ABSTRACT
388 -- Check for IS NEW with Formal_Part present and handle nicely
390 elsif Token = Tok_New then
392 ("formal part not allowed in instantiation", Fpart_Sloc);
396 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
399 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
402 Set_Defining_Unit_Name (Inst_Node, Name_Node);
403 Set_Name (Inst_Node, P_Name);
404 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
406 Pop_Scope_Stack; -- Don't need scope stack entry in this case
410 goto Subprogram_Body;
413 -- Here we have a missing IS or missing semicolon, we always guess
414 -- a missing semicolon, since we are pretty good at fixing up a
415 -- semicolon which should really be an IS
418 Error_Msg_AP ("missing "";""");
419 SIS_Missing_Semicolon_Message := Get_Msg_Id;
420 goto Subprogram_Declaration;
424 -- Processing for subprogram body
427 if not Pf_Flags.Pbod then
428 Error_Msg_SP ("subprogram body not allowed here!");
431 -- Subprogram body stub case
433 if Separate_Present then
434 if not Pf_Flags.Stub then
435 Error_Msg_SC ("body stub not allowed here!");
438 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
440 ("operator symbol cannot be used as subunit name",
445 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
446 Set_Specification (Stub_Node, Specification_Node);
447 Scan; -- past SEPARATE
452 -- Subprogram body case
455 -- Here is the test for a suspicious IS (i.e. one that looks
456 -- like it might more properly be a semicolon). See separate
457 -- section discussing use of IS instead of semicolon in
460 if (Token in Token_Class_Declk
462 Token = Tok_Identifier)
463 and then Start_Column <= Scope.Table (Scope.Last).Ecol
464 and then Scope.Last /= 1
466 Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
467 Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
471 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
472 Set_Specification (Body_Node, Specification_Node);
473 Parse_Decls_Begin_End (Body_Node);
477 -- Processing for subprogram declaration
479 <<Subprogram_Declaration>>
481 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
482 Set_Specification (Decl_Node, Specification_Node);
484 -- If this is a context in which a subprogram body is permitted,
485 -- set active SIS entry in case (see section titled "Handling
486 -- Semicolon Used in Place of IS" in body of Parser package)
487 -- Note that SIS_Missing_Semicolon_Message is already set properly.
489 if Pf_Flags.Pbod then
490 SIS_Labl := Scope.Table (Scope.Last).Labl;
491 SIS_Sloc := Scope.Table (Scope.Last).Sloc;
492 SIS_Ecol := Scope.Table (Scope.Last).Ecol;
493 SIS_Declaration_Node := Decl_Node;
494 SIS_Semicolon_Sloc := Prev_Token_Ptr;
495 SIS_Entry_Active := True;
503 ---------------------------------
504 -- 6.1 Subprogram Declaration --
505 ---------------------------------
507 -- Parsed by P_Subprogram (6.1)
509 ------------------------------------------
510 -- 6.1 Abstract Subprogram Declaration --
511 ------------------------------------------
513 -- Parsed by P_Subprogram (6.1)
515 -----------------------------------
516 -- 6.1 Subprogram Specification --
517 -----------------------------------
519 -- SUBPROGRAM_SPECIFICATION ::=
520 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
521 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
523 -- PARAMETER_PROFILE ::= [FORMAL_PART]
525 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
527 -- Subprogram specifications that appear in subprogram declarations
528 -- are parsed by P_Subprogram (6.1). This routine is used in other
529 -- contexts where subprogram specifications occur.
531 -- Note: this routine does not affect the scope stack in any way
533 -- Error recovery: can raise Error_Resync
535 function P_Subprogram_Specification return Node_Id is
536 Specification_Node : Node_Id;
539 if Token = Tok_Function then
540 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
541 Scan; -- past FUNCTION
543 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
544 Set_Parameter_Specifications
545 (Specification_Node, P_Parameter_Profile);
546 Check_Junk_Semicolon_Before_Return;
548 Set_Subtype_Mark (Specification_Node, P_Subtype_Mark);
550 return Specification_Node;
552 elsif Token = Tok_Procedure then
553 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
554 Scan; -- past PROCEDURE
556 Set_Defining_Unit_Name
557 (Specification_Node, P_Defining_Program_Unit_Name);
558 Set_Parameter_Specifications
559 (Specification_Node, P_Parameter_Profile);
560 return Specification_Node;
563 Error_Msg_SC ("subprogram specification expected");
566 end P_Subprogram_Specification;
568 ---------------------
570 ---------------------
573 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
575 -- The caller has checked that the initial token is an identifier,
576 -- operator symbol, or string literal. Note that we don't bother to
577 -- do much error diagnosis in this routine, since it is only used for
578 -- the label on END lines, and the routines in package Par.Endh will
579 -- check that the label is appropriate.
581 -- Error recovery: cannot raise Error_Resync
583 function P_Designator return Node_Id is
584 Ident_Node : Node_Id;
586 Prefix_Node : Node_Id;
588 function Real_Dot return Boolean;
589 -- Tests if a current token is an interesting period, i.e. is followed
590 -- by an identifier or operator symbol or string literal. If not, it is
591 -- probably just incorrect punctuation to be caught by our caller. Note
592 -- that the case of an operator symbol or string literal is also an
593 -- error, but that is an error that we catch here. If the result is
594 -- True, a real dot has been scanned and we are positioned past it,
595 -- if the result is False, the scan position is unchanged.
597 function Real_Dot return Boolean is
598 Scan_State : Saved_Scan_State;
601 if Token /= Tok_Dot then
605 Save_Scan_State (Scan_State);
608 if Token = Tok_Identifier
609 or else Token = Tok_Operator_Symbol
610 or else Token = Tok_String_Literal
615 Restore_Scan_State (Scan_State);
621 -- Start of processing for P_Designator
624 Ident_Node := Token_Node;
625 Scan; -- past initial token
627 if Prev_Token = Tok_Operator_Symbol
628 or else Prev_Token = Tok_String_Literal
636 Prefix_Node := Ident_Node;
638 -- Loop through child names, on entry to this loop, Prefix contains
639 -- the name scanned so far, and Ident_Node is the last identifier.
642 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
643 Set_Prefix (Name_Node, Prefix_Node);
644 Ident_Node := P_Identifier;
645 Set_Selector_Name (Name_Node, Ident_Node);
646 Prefix_Node := Name_Node;
647 exit when not Real_Dot;
650 -- On exit from the loop, Ident_Node is the last identifier scanned,
651 -- i.e. the defining identifier, and Prefix_Node is a node for the
652 -- entire name, structured (incorrectly!) as a selected component.
654 Name_Node := Prefix (Prefix_Node);
655 Change_Node (Prefix_Node, N_Designator);
656 Set_Name (Prefix_Node, Name_Node);
657 Set_Identifier (Prefix_Node, Ident_Node);
663 while Token = Tok_Dot or else Token = Tok_Identifier loop
670 ------------------------------
671 -- 6.1 Defining Designator --
672 ------------------------------
674 -- DEFINING_DESIGNATOR ::=
675 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
677 -- Error recovery: cannot raise Error_Resync
679 function P_Defining_Designator return Node_Id is
681 if Token = Tok_Operator_Symbol then
682 return P_Defining_Operator_Symbol;
684 elsif Token = Tok_String_Literal then
685 Error_Msg_SC ("invalid operator name");
686 Scan; -- past junk string
690 return P_Defining_Program_Unit_Name;
692 end P_Defining_Designator;
694 -------------------------------------
695 -- 6.1 Defining Program Unit Name --
696 -------------------------------------
698 -- DEFINING_PROGRAM_UNIT_NAME ::=
699 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
701 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
703 -- Error recovery: cannot raise Error_Resync
705 function P_Defining_Program_Unit_Name return Node_Id is
706 Ident_Node : Node_Id;
708 Prefix_Node : Node_Id;
711 -- Set identifier casing if not already set and scan initial identifier
713 if Token = Tok_Identifier
714 and then Identifier_Casing (Current_Source_File) = Unknown
716 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
719 Ident_Node := P_Identifier;
720 Merge_Identifier (Ident_Node, Tok_Return);
722 -- Normal case (not child library unit name)
724 if Token /= Tok_Dot then
725 Change_Identifier_To_Defining_Identifier (Ident_Node);
728 -- Child library unit name case
731 if Scope.Last > 1 then
732 Error_Msg_SP ("child unit allowed only at library level");
736 Error_Msg_SP ("(Ada 83) child unit not allowed!");
740 Prefix_Node := Ident_Node;
742 -- Loop through child names, on entry to this loop, Prefix contains
743 -- the name scanned so far, and Ident_Node is the last identifier.
746 exit when Token /= Tok_Dot;
747 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
749 Set_Prefix (Name_Node, Prefix_Node);
750 Ident_Node := P_Identifier;
751 Set_Selector_Name (Name_Node, Ident_Node);
752 Prefix_Node := Name_Node;
755 -- On exit from the loop, Ident_Node is the last identifier scanned,
756 -- i.e. the defining identifier, and Prefix_Node is a node for the
757 -- entire name, structured (incorrectly!) as a selected component.
759 Name_Node := Prefix (Prefix_Node);
760 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
761 Set_Name (Prefix_Node, Name_Node);
762 Change_Identifier_To_Defining_Identifier (Ident_Node);
763 Set_Defining_Identifier (Prefix_Node, Ident_Node);
765 -- All set with unit name parsed
772 while Token = Tok_Dot or else Token = Tok_Identifier loop
777 end P_Defining_Program_Unit_Name;
779 --------------------------
780 -- 6.1 Operator Symbol --
781 --------------------------
783 -- OPERATOR_SYMBOL ::= STRING_LITERAL
785 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
787 -----------------------------------
788 -- 6.1 Defining Operator Symbol --
789 -----------------------------------
791 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
793 -- The caller has checked that the initial symbol is an operator symbol
795 function P_Defining_Operator_Symbol return Node_Id is
799 Op_Node := Token_Node;
800 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
801 Scan; -- past operator symbol
803 end P_Defining_Operator_Symbol;
805 ----------------------------
806 -- 6.1 Parameter_Profile --
807 ----------------------------
809 -- PARAMETER_PROFILE ::= [FORMAL_PART]
811 -- Empty is returned if no formal part is present
813 -- Error recovery: cannot raise Error_Resync
815 function P_Parameter_Profile return List_Id is
817 if Token = Tok_Left_Paren then
818 Scan; -- part left paren
819 return P_Formal_Part;
823 end P_Parameter_Profile;
825 ---------------------------------------
826 -- 6.1 Parameter And Result Profile --
827 ---------------------------------------
829 -- Parsed by its parent construct, which uses P_Parameter_Profile to
830 -- parse the parameters, and P_Subtype_Mark to parse the return type.
832 ----------------------
833 -- 6.1 Formal part --
834 ----------------------
836 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
838 -- PARAMETER_SPECIFICATION ::=
839 -- DEFINING_IDENTIFIER_LIST : MODE SUBTYPE_MARK
840 -- [:= DEFAULT_EXPRESSION]
841 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
842 -- [:= DEFAULT_EXPRESSION]
844 -- This scans the construct Formal_Part. The caller has already checked
845 -- that the initial token is a left parenthesis, and skipped past it, so
846 -- that on entry Token is the first token following the left parenthesis.
848 -- Error recovery: cannot raise Error_Resync
850 function P_Formal_Part return List_Id is
851 Specification_List : List_Id;
852 Specification_Node : Node_Id;
853 Scan_State : Saved_Scan_State;
856 Ident_Sloc : Source_Ptr;
858 Idents : array (Int range 1 .. 4096) of Entity_Id;
859 -- This array holds the list of defining identifiers. The upper bound
860 -- of 4096 is intended to be essentially infinite, and we do not even
861 -- bother to check for it being exceeded.
864 Specification_List := New_List;
866 Specification_Loop : loop
868 if Token = Tok_Pragma then
872 Ignore (Tok_Left_Paren);
873 Ident_Sloc := Token_Ptr;
874 Idents (1) := P_Defining_Identifier;
878 exit Ident_Loop when Token = Tok_Colon;
880 -- The only valid tokens are colon and comma, so if we have
881 -- neither do a bit of investigation to see which is the
882 -- better choice for insertion.
884 if Token /= Tok_Comma then
886 -- Assume colon if IN or OUT keyword found
888 exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
890 -- Otherwise scan ahead
892 Save_Scan_State (Scan_State);
895 -- If we run into a semicolon, then assume that a
896 -- colon was missing, e.g. Parms (X Y; ...). Also
897 -- assume missing colon on EOF (a real disaster!)
898 -- and on a right paren, e.g. Parms (X Y), and also
899 -- on an assignment symbol, e.g. Parms (X Y := ..)
901 if Token = Tok_Semicolon
902 or else Token = Tok_Right_Paren
903 or else Token = Tok_EOF
904 or else Token = Tok_Colon_Equal
906 Restore_Scan_State (Scan_State);
909 -- If we run into a colon, assume that we had a missing
910 -- comma, e.g. Parms (A B : ...). Also assume a missing
911 -- comma if we hit another comma, e.g. Parms (A B, C ..)
913 elsif Token = Tok_Colon
914 or else Token = Tok_Comma
916 Restore_Scan_State (Scan_State);
924 -- Here if a comma is present, or to be assumed
927 Num_Idents := Num_Idents + 1;
928 Idents (Num_Idents) := P_Defining_Identifier;
931 -- Fall through the loop on encountering a colon, or deciding
932 -- that there is a missing colon.
936 -- If there are multiple identifiers, we repeatedly scan the
937 -- type and initialization expression information by resetting
938 -- the scan pointer (so that we get completely separate trees
939 -- for each occurrence).
941 if Num_Idents > 1 then
942 Save_Scan_State (Scan_State);
945 -- Loop through defining identifiers in list
949 Ident_List_Loop : loop
950 Specification_Node :=
951 New_Node (N_Parameter_Specification, Ident_Sloc);
952 Set_Defining_Identifier (Specification_Node, Idents (Ident));
954 if Token = Tok_Access then
956 Error_Msg_SC ("(Ada 83) access parameters not allowed");
960 (Specification_Node, P_Access_Definition);
963 P_Mode (Specification_Node);
965 if Token = Tok_Procedure
969 Error_Msg_SC ("formal subprogram parameter not allowed");
972 if Token = Tok_Left_Paren then
973 Discard_Junk_List (P_Formal_Part);
976 if Token = Tok_Return then
978 Discard_Junk_Node (P_Subtype_Mark);
981 Set_Parameter_Type (Specification_Node, Error);
984 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
989 Set_Expression (Specification_Node, Init_Expr_Opt (True));
992 Set_Prev_Ids (Specification_Node, True);
995 if Ident < Num_Idents then
996 Set_More_Ids (Specification_Node, True);
999 Append (Specification_Node, Specification_List);
1000 exit Ident_List_Loop when Ident = Num_Idents;
1002 Restore_Scan_State (Scan_State);
1003 end loop Ident_List_Loop;
1006 when Error_Resync =>
1007 Resync_Semicolon_List;
1010 if Token = Tok_Semicolon then
1011 Scan; -- past semicolon
1013 -- If we have RETURN or IS after the semicolon, then assume
1014 -- that semicolon should have been a right parenthesis and exit
1016 if Token = Tok_Is or else Token = Tok_Return then
1017 Error_Msg_SP ("expected "")"" in place of "";""");
1018 exit Specification_Loop;
1021 elsif Token = Tok_Right_Paren then
1022 Scan; -- past right paren
1023 exit Specification_Loop;
1025 -- Special check for common error of using comma instead of semicolon
1027 elsif Token = Tok_Comma then
1031 -- Special check for omitted separator
1033 elsif Token = Tok_Identifier then
1036 -- If nothing sensible, skip to next semicolon or right paren
1040 Resync_Semicolon_List;
1042 if Token = Tok_Semicolon then
1043 Scan; -- past semicolon
1046 exit Specification_Loop;
1049 end loop Specification_Loop;
1051 return Specification_List;
1054 ----------------------------------
1055 -- 6.1 Parameter Specification --
1056 ----------------------------------
1058 -- Parsed by P_Formal_Part (6.1)
1064 -- MODE ::= [in] | in out | out
1066 -- There is no explicit node in the tree for the Mode. Instead the
1067 -- In_Present and Out_Present flags are set in the parent node to
1068 -- record the presence of keywords specifying the mode.
1070 -- Error_Recovery: cannot raise Error_Resync
1072 procedure P_Mode (Node : Node_Id) is
1074 if Token = Tok_In then
1076 Set_In_Present (Node, True);
1079 if Token = Tok_Out then
1081 Set_Out_Present (Node, True);
1084 if Token = Tok_In then
1085 Error_Msg_SC ("IN must precede OUT in parameter mode");
1087 Set_In_Present (Node, True);
1091 --------------------------
1092 -- 6.3 Subprogram Body --
1093 --------------------------
1095 -- Parsed by P_Subprogram (6.1)
1097 -----------------------------------
1098 -- 6.4 Procedure Call Statement --
1099 -----------------------------------
1101 -- Parsed by P_Sequence_Of_Statements (5.1)
1103 ------------------------
1104 -- 6.4 Function Call --
1105 ------------------------
1107 -- Parsed by P_Call_Or_Name (4.1)
1109 --------------------------------
1110 -- 6.4 Actual Parameter Part --
1111 --------------------------------
1113 -- Parsed by P_Call_Or_Name (4.1)
1115 --------------------------------
1116 -- 6.4 Parameter Association --
1117 --------------------------------
1119 -- Parsed by P_Call_Or_Name (4.1)
1121 ------------------------------------
1122 -- 6.4 Explicit Actual Parameter --
1123 ------------------------------------
1125 -- Parsed by P_Call_Or_Name (4.1)
1127 ---------------------------
1128 -- 6.5 Return Statement --
1129 ---------------------------
1131 -- RETURN_STATEMENT ::= return [EXPRESSION];
1133 -- The caller has checked that the initial token is RETURN
1135 -- Error recovery: can raise Error_Resync
1137 function P_Return_Statement return Node_Id is
1138 Return_Node : Node_Id;
1141 Return_Node := New_Node (N_Return_Statement, Token_Ptr);
1143 -- Sloc points to RETURN
1146 Scan; -- past RETURN
1148 if Token /= Tok_Semicolon then
1150 -- If no semicolon, then scan an expression, except that
1151 -- we avoid trying to scan an expression if we are at an
1152 -- expression terminator since in that case the best error
1153 -- message is probably that we have a missing semicolon.
1155 if Token not in Token_Class_Eterm then
1156 Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1162 end P_Return_Statement;