1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Hostparm; use Hostparm;
32 with Sinfo.CN; use Sinfo.CN;
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 function P_Component_List return Node_Id;
43 function P_Defining_Character_Literal return Node_Id;
44 function P_Delta_Constraint return Node_Id;
45 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id;
46 function P_Digits_Constraint return Node_Id;
47 function P_Discriminant_Association return Node_Id;
48 function P_Enumeration_Literal_Specification return Node_Id;
49 function P_Enumeration_Type_Definition return Node_Id;
50 function P_Fixed_Point_Definition return Node_Id;
51 function P_Floating_Point_Definition return Node_Id;
52 function P_Index_Or_Discriminant_Constraint return Node_Id;
53 function P_Real_Range_Specification_Opt return Node_Id;
54 function P_Subtype_Declaration return Node_Id;
55 function P_Type_Declaration return Node_Id;
56 function P_Modular_Type_Definition return Node_Id;
57 function P_Variant return Node_Id;
58 function P_Variant_Part return Node_Id;
60 procedure P_Declarative_Items
64 -- Scans out a single declarative item, or, in the case of a declaration
65 -- with a list of identifiers, a list of declarations, one for each of
66 -- the identifiers in the list. The declaration or declarations scanned
67 -- are appended to the given list. Done indicates whether or not there
68 -- may be additional declarative items to scan. If Done is True, then
69 -- a decision has been made that there are no more items to scan. If
70 -- Done is False, then there may be additional declarations to scan.
71 -- In_Spec is true if we are scanning a package declaration, and is used
72 -- to generate an appropriate message if a statement is encountered in
75 procedure P_Identifier_Declarations
79 -- Scans out a set of declarations for an identifier or list of
80 -- identifiers, and appends them to the given list. The parameters have
81 -- the same significance as for P_Declarative_Items.
83 procedure Statement_When_Declaration_Expected
87 -- Called when a statement is found at a point where a declaration was
88 -- expected. The parameters are as described for P_Declarative_Items.
90 procedure Set_Declaration_Expected;
91 -- Posts a "declaration expected" error messages at the start of the
92 -- current token, and if this is the first such message issued, saves
93 -- the message id in Missing_Begin_Msg, for possible later replacement.
99 function Init_Expr_Opt (P : Boolean := False) return Node_Id is
101 -- For colon, assume it means := unless it is at the end of
102 -- a line, in which case guess that it means a semicolon.
104 if Token = Tok_Colon then
105 if Token_Is_At_End_Of_Line then
110 -- Here if := or something that we will take as equivalent
112 elsif Token = Tok_Colon_Equal
113 or else Token = Tok_Equal
114 or else Token = Tok_Is
118 -- Another possibility. If we have a literal followed by a semicolon,
119 -- we assume that we have a missing colon-equal.
121 elsif Token in Token_Class_Literal then
123 Scan_State : Saved_Scan_State;
126 Save_Scan_State (Scan_State);
127 Scan; -- past literal or identifier
129 if Token = Tok_Semicolon then
130 Restore_Scan_State (Scan_State);
132 Restore_Scan_State (Scan_State);
137 -- Otherwise we definitely have no initialization expression
143 -- Merge here if we have an initialization expression
150 return P_Expression_No_Right_Paren;
154 ----------------------------
155 -- 3.1 Basic Declaration --
156 ----------------------------
158 -- Parsed by P_Basic_Declarative_Items (3.9)
160 ------------------------------
161 -- 3.1 Defining Identifier --
162 ------------------------------
164 -- DEFINING_IDENTIFIER ::= IDENTIFIER
166 -- Error recovery: can raise Error_Resync
168 function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
169 Ident_Node : Node_Id;
172 -- Scan out the identifier. Note that this code is essentially identical
173 -- to P_Identifier, except that in the call to Scan_Reserved_Identifier
174 -- we set Force_Msg to True, since we want at least one message for each
175 -- separate declaration (but not use) of a reserved identifier.
177 if Token = Tok_Identifier then
180 -- If we have a reserved identifier, manufacture an identifier with
181 -- a corresponding name after posting an appropriate error message
183 elsif Is_Reserved_Identifier (C) then
184 Scan_Reserved_Identifier (Force_Msg => True);
186 -- Otherwise we have junk that cannot be interpreted as an identifier
189 T_Identifier; -- to give message
193 Ident_Node := Token_Node;
194 Scan; -- past the reserved identifier
196 if Ident_Node /= Error then
197 Change_Identifier_To_Defining_Identifier (Ident_Node);
201 end P_Defining_Identifier;
203 -----------------------------
204 -- 3.2.1 Type Declaration --
205 -----------------------------
207 -- TYPE_DECLARATION ::=
208 -- FULL_TYPE_DECLARATION
209 -- | INCOMPLETE_TYPE_DECLARATION
210 -- | PRIVATE_TYPE_DECLARATION
211 -- | PRIVATE_EXTENSION_DECLARATION
213 -- FULL_TYPE_DECLARATION ::=
214 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION;
215 -- | CONCURRENT_TYPE_DECLARATION
217 -- INCOMPLETE_TYPE_DECLARATION ::=
218 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART];
220 -- PRIVATE_TYPE_DECLARATION ::=
221 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
222 -- is [abstract] [tagged] [limited] private;
224 -- PRIVATE_EXTENSION_DECLARATION ::=
225 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
226 -- [abstract] new ancestor_SUBTYPE_INDICATION with private;
228 -- TYPE_DEFINITION ::=
229 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
230 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
231 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
232 -- | DERIVED_TYPE_DEFINITION
234 -- INTEGER_TYPE_DEFINITION ::=
235 -- SIGNED_INTEGER_TYPE_DEFINITION
236 -- MODULAR_TYPE_DEFINITION
238 -- Error recovery: can raise Error_Resync
240 -- Note: The processing for full type declaration, incomplete type
241 -- declaration, private type declaration and type definition is
242 -- included in this function. The processing for concurrent type
243 -- declarations is NOT here, but rather in chapter 9 (i.e. this
244 -- function handles only declarations starting with TYPE).
246 function P_Type_Declaration return Node_Id is
247 Type_Loc : Source_Ptr;
248 Type_Start_Col : Column_Number;
249 Ident_Node : Node_Id;
251 Discr_List : List_Id;
252 Unknown_Dis : Boolean;
253 Discr_Sloc : Source_Ptr;
254 Abstract_Present : Boolean;
255 Abstract_Loc : Source_Ptr;
258 Typedef_Node : Node_Id;
259 -- Normally holds type definition, except in the case of a private
260 -- extension declaration, in which case it holds the declaration itself
263 Type_Loc := Token_Ptr;
264 Type_Start_Col := Start_Column;
266 Ident_Node := P_Defining_Identifier (C_Is);
267 Discr_Sloc := Token_Ptr;
269 if P_Unknown_Discriminant_Part_Opt then
271 Discr_List := No_List;
273 Unknown_Dis := False;
274 Discr_List := P_Known_Discriminant_Part_Opt;
277 -- Incomplete type declaration. We complete the processing for this
278 -- case here and return the resulting incomplete type declaration node
280 if Token = Tok_Semicolon then
282 Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
283 Set_Defining_Identifier (Decl_Node, Ident_Node);
284 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
285 Set_Discriminant_Specifications (Decl_Node, Discr_List);
292 -- Full type declaration or private type declaration, must have IS
294 if Token = Tok_Equal then
296 Scan; -- past = used in place of IS
298 elsif Token = Tok_Renames then
299 Error_Msg_SC ("RENAMES should be IS");
300 Scan; -- past RENAMES used in place of IS
306 -- First an error check, if we have two identifiers in a row, a likely
307 -- possibility is that the first of the identifiers is an incorrectly
310 if Token = Tok_Identifier then
312 SS : Saved_Scan_State;
316 Save_Scan_State (SS);
317 Scan; -- past initial identifier
318 I2 := (Token = Tok_Identifier);
319 Restore_Scan_State (SS);
323 (Bad_Spelling_Of (Tok_Abstract) or else
324 Bad_Spelling_Of (Tok_Access) or else
325 Bad_Spelling_Of (Tok_Aliased) or else
326 Bad_Spelling_Of (Tok_Constant))
333 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
335 if Token_Name = Name_Abstract then
336 Check_95_Keyword (Tok_Abstract, Tok_Tagged);
337 Check_95_Keyword (Tok_Abstract, Tok_New);
340 -- Check cases of misuse of ABSTRACT
342 if Token = Tok_Abstract then
343 Abstract_Present := True;
344 Abstract_Loc := Token_Ptr;
345 Scan; -- past ABSTRACT
347 if Token = Tok_Limited
348 or else Token = Tok_Private
349 or else Token = Tok_Record
350 or else Token = Tok_Null
352 Error_Msg_AP ("TAGGED expected");
356 Abstract_Present := False;
357 Abstract_Loc := No_Location;
360 -- Check for misuse of Ada 95 keyword Tagged
362 if Token_Name = Name_Tagged then
363 Check_95_Keyword (Tok_Tagged, Tok_Private);
364 Check_95_Keyword (Tok_Tagged, Tok_Limited);
365 Check_95_Keyword (Tok_Tagged, Tok_Record);
368 -- Special check for misuse of Aliased
370 if Token = Tok_Aliased or else Token_Name = Name_Aliased then
371 Error_Msg_SC ("ALIASED not allowed in type definition");
372 Scan; -- past ALIASED
375 -- The following procesing deals with either a private type declaration
376 -- or a full type declaration. In the private type case, we build the
377 -- N_Private_Type_Declaration node, setting its Tagged_Present and
378 -- Limited_Present flags, on encountering the Private keyword, and
379 -- leave Typedef_Node set to Empty. For the full type declaration
380 -- case, Typedef_Node gets set to the type definition.
382 Typedef_Node := Empty;
384 -- Switch on token following the IS. The loop normally runs once. It
385 -- only runs more than once if an error is detected, to try again after
386 -- detecting and fixing up the error.
392 Typedef_Node := P_Access_Type_Definition;
397 Typedef_Node := P_Array_Type_Definition;
402 Typedef_Node := P_Fixed_Point_Definition;
407 Typedef_Node := P_Floating_Point_Definition;
414 when Tok_Integer_Literal =>
416 Typedef_Node := P_Signed_Integer_Type_Definition;
421 Typedef_Node := P_Record_Definition;
425 when Tok_Left_Paren =>
426 Typedef_Node := P_Enumeration_Type_Definition;
429 Make_Identifier (Token_Ptr,
430 Chars => Chars (Ident_Node));
431 Set_Comes_From_Source (End_Labl, False);
433 Set_End_Label (Typedef_Node, End_Labl);
438 Typedef_Node := P_Modular_Type_Definition;
443 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
445 if Nkind (Typedef_Node) = N_Derived_Type_Definition
446 and then Present (Record_Extension_Part (Typedef_Node))
449 Make_Identifier (Token_Ptr,
450 Chars => Chars (Ident_Node));
451 Set_Comes_From_Source (End_Labl, False);
454 (Record_Extension_Part (Typedef_Node), End_Labl);
461 Typedef_Node := P_Signed_Integer_Type_Definition;
466 Typedef_Node := P_Record_Definition;
469 Make_Identifier (Token_Ptr,
470 Chars => Chars (Ident_Node));
471 Set_Comes_From_Source (End_Labl, False);
473 Set_End_Label (Typedef_Node, End_Labl);
480 if Token = Tok_Abstract then
481 Error_Msg_SC ("ABSTRACT must come before TAGGED");
482 Abstract_Present := True;
483 Abstract_Loc := Token_Ptr;
484 Scan; -- past ABSTRACT
487 if Token = Tok_Limited then
488 Scan; -- past LIMITED
490 -- TAGGED LIMITED PRIVATE case
492 if Token = Tok_Private then
494 New_Node (N_Private_Type_Declaration, Type_Loc);
495 Set_Tagged_Present (Decl_Node, True);
496 Set_Limited_Present (Decl_Node, True);
497 Scan; -- past PRIVATE
499 -- TAGGED LIMITED RECORD
502 Typedef_Node := P_Record_Definition;
503 Set_Tagged_Present (Typedef_Node, True);
504 Set_Limited_Present (Typedef_Node, True);
507 Make_Identifier (Token_Ptr,
508 Chars => Chars (Ident_Node));
509 Set_Comes_From_Source (End_Labl, False);
511 Set_End_Label (Typedef_Node, End_Labl);
517 if Token = Tok_Private then
519 New_Node (N_Private_Type_Declaration, Type_Loc);
520 Set_Tagged_Present (Decl_Node, True);
521 Scan; -- past PRIVATE
526 Typedef_Node := P_Record_Definition;
527 Set_Tagged_Present (Typedef_Node, True);
530 Make_Identifier (Token_Ptr,
531 Chars => Chars (Ident_Node));
532 Set_Comes_From_Source (End_Labl, False);
534 Set_End_Label (Typedef_Node, End_Labl);
542 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
543 Scan; -- past PRIVATE
548 Scan; -- past LIMITED
551 if Token = Tok_Tagged then
552 Error_Msg_SC ("TAGGED must come before LIMITED");
555 elsif Token = Tok_Abstract then
556 Error_Msg_SC ("ABSTRACT must come before LIMITED");
557 Scan; -- past ABSTRACT
564 -- LIMITED RECORD or LIMITED NULL RECORD
566 if Token = Tok_Record or else Token = Tok_Null then
569 ("(Ada 83) limited record declaration not allowed!");
572 Typedef_Node := P_Record_Definition;
573 Set_Limited_Present (Typedef_Node, True);
575 -- LIMITED PRIVATE is the only remaining possibility here
578 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
579 Set_Limited_Present (Decl_Node, True);
580 T_Private; -- past PRIVATE (or complain if not there!)
586 -- Here we have an identifier after the IS, which is certainly
587 -- wrong and which might be one of several different mistakes.
589 when Tok_Identifier =>
591 -- First case, if identifier is on same line, then probably we
592 -- have something like "type X is Integer .." and the best
593 -- diagnosis is a missing NEW. Note: the missing new message
594 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
596 if not Token_Is_At_Start_Of_Line then
597 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
600 -- If the identifier is at the start of the line, and is in the
601 -- same column as the type declaration itself then we consider
602 -- that we had a missing type definition on the previous line
604 elsif Start_Column <= Type_Start_Col then
605 Error_Msg_AP ("type definition expected");
606 Typedef_Node := Error;
608 -- If the identifier is at the start of the line, and is in
609 -- a column to the right of the type declaration line, then we
610 -- may have something like:
615 -- and the best diagnosis is a missing record keyword
618 Typedef_Node := P_Record_Definition;
624 -- Anything else is an error
627 if Bad_Spelling_Of (Tok_Access)
629 Bad_Spelling_Of (Tok_Array)
631 Bad_Spelling_Of (Tok_Delta)
633 Bad_Spelling_Of (Tok_Digits)
635 Bad_Spelling_Of (Tok_Limited)
637 Bad_Spelling_Of (Tok_Private)
639 Bad_Spelling_Of (Tok_Range)
641 Bad_Spelling_Of (Tok_Record)
643 Bad_Spelling_Of (Tok_Tagged)
648 Error_Msg_AP ("type definition expected");
655 -- For the private type declaration case, the private type declaration
656 -- node has been built, with the Tagged_Present and Limited_Present
657 -- flags set as needed, and Typedef_Node is left set to Empty.
659 if No (Typedef_Node) then
660 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
661 Set_Abstract_Present (Decl_Node, Abstract_Present);
663 -- For a private extension declaration, Typedef_Node contains the
664 -- N_Private_Extension_Declaration node, which we now complete. Note
665 -- that the private extension declaration, unlike a full type
666 -- declaration, does permit unknown discriminants.
668 elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
669 Decl_Node := Typedef_Node;
670 Set_Sloc (Decl_Node, Type_Loc);
671 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
672 Set_Abstract_Present (Typedef_Node, Abstract_Present);
674 -- In the full type declaration case, Typedef_Node has the type
675 -- definition and here is where we build the full type declaration
676 -- node. This is also where we check for improper use of an unknown
677 -- discriminant part (not allowed for full type declaration).
680 if Nkind (Typedef_Node) = N_Record_Definition
681 or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
682 and then Present (Record_Extension_Part (Typedef_Node)))
684 Set_Abstract_Present (Typedef_Node, Abstract_Present);
686 elsif Abstract_Present then
687 Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
690 Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
691 Set_Type_Definition (Decl_Node, Typedef_Node);
695 ("Full type declaration cannot have unknown discriminants",
700 -- Remaining processing is common for all three cases
702 Set_Defining_Identifier (Decl_Node, Ident_Node);
703 Set_Discriminant_Specifications (Decl_Node, Discr_List);
705 end P_Type_Declaration;
707 ----------------------------------
708 -- 3.2.1 Full Type Declaration --
709 ----------------------------------
711 -- Parsed by P_Type_Declaration (3.2.1)
713 ----------------------------
714 -- 3.2.1 Type Definition --
715 ----------------------------
717 -- Parsed by P_Type_Declaration (3.2.1)
719 --------------------------------
720 -- 3.2.2 Subtype Declaration --
721 --------------------------------
723 -- SUBTYPE_DECLARATION ::=
724 -- subtype DEFINING_IDENTIFIER is SUBTYPE_INDICATION;
726 -- The caller has checked that the initial token is SUBTYPE
728 -- Error recovery: can raise Error_Resync
730 function P_Subtype_Declaration return Node_Id is
734 Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
735 Scan; -- past SUBTYPE
736 Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
739 if Token = Tok_New then
740 Error_Msg_SC ("NEW ignored (only allowed in type declaration)");
744 Set_Subtype_Indication (Decl_Node, P_Subtype_Indication);
747 end P_Subtype_Declaration;
749 -------------------------------
750 -- 3.2.2 Subtype Indication --
751 -------------------------------
753 -- SUBTYPE_INDICATION ::= SUBTYPE_MARK [CONSTRAINT]
755 -- Error recovery: can raise Error_Resync
757 function P_Subtype_Indication return Node_Id is
761 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
762 Type_Node := P_Subtype_Mark;
763 return P_Subtype_Indication (Type_Node);
766 -- Check for error of using record definition and treat it nicely,
767 -- otherwise things are really messed up, so resynchronize.
769 if Token = Tok_Record then
770 Error_Msg_SC ("anonymous record definitions are not permitted");
771 Discard_Junk_Node (P_Record_Definition);
775 Error_Msg_AP ("subtype indication expected");
779 end P_Subtype_Indication;
781 -- The following function is identical except that it is called with
782 -- the subtype mark already scanned out, and it scans out the constraint
784 -- Error recovery: can raise Error_Resync
786 function P_Subtype_Indication (Subtype_Mark : Node_Id) return Node_Id is
787 Indic_Node : Node_Id;
788 Constr_Node : Node_Id;
791 Constr_Node := P_Constraint_Opt;
793 if No (Constr_Node) then
796 Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
797 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
798 Set_Constraint (Indic_Node, Constr_Node);
801 end P_Subtype_Indication;
803 -------------------------
804 -- 3.2.2 Subtype Mark --
805 -------------------------
807 -- SUBTYPE_MARK ::= subtype_NAME;
809 -- Note: The subtype mark which appears after an IN or NOT IN
810 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
812 -- Error recovery: cannot raise Error_Resync
814 function P_Subtype_Mark return Node_Id is
816 return P_Subtype_Mark_Resync;
823 -- This routine differs from P_Subtype_Mark in that it insists that an
824 -- identifier be present, and if it is not, it raises Error_Resync.
826 -- Error recovery: can raise Error_Resync
828 function P_Subtype_Mark_Resync return Node_Id is
832 if Token = Tok_Access then
833 Error_Msg_SC ("anonymous access type definition not allowed here");
837 if Token = Tok_Array then
838 Error_Msg_SC ("anonymous array definition not allowed here");
839 Discard_Junk_Node (P_Array_Type_Definition);
843 Type_Node := P_Qualified_Simple_Name_Resync;
845 -- Check for a subtype mark attribute. The only valid possibilities
846 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
847 -- as well catch it here.
849 if Token = Tok_Apostrophe then
850 return P_Subtype_Mark_Attribute (Type_Node);
855 end P_Subtype_Mark_Resync;
857 -- The following function is called to scan out a subtype mark attribute.
858 -- The caller has already scanned out the subtype mark, which is passed in
859 -- as the argument, and has checked that the current token is apostrophe.
861 -- Only a special subclass of attributes, called type attributes
862 -- (see Snames package) are allowed in this syntactic position.
864 -- Note: if the apostrophe is followed by other than an identifier, then
865 -- the input expression is returned unchanged, and the scan pointer is
866 -- left pointing to the apostrophe.
868 -- Error recovery: can raise Error_Resync
870 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
871 Attr_Node : Node_Id := Empty;
872 Scan_State : Saved_Scan_State;
876 Prefix := Check_Subtype_Mark (Type_Node);
878 if Prefix = Error then
882 -- Loop through attributes appearing (more than one can appear as for
883 -- for example in X'Base'Class). We are at an apostrophe on entry to
884 -- this loop, and it runs once for each attribute parsed, with
885 -- Prefix being the current possible prefix if it is an attribute.
888 Save_Scan_State (Scan_State); -- at Apostrophe
889 Scan; -- past apostrophe
891 if Token /= Tok_Identifier then
892 Restore_Scan_State (Scan_State); -- to apostrophe
893 return Prefix; -- no attribute after all
895 elsif not Is_Type_Attribute_Name (Token_Name) then
897 ("attribute & may not be used in a subtype mark", Token_Node);
902 Make_Attribute_Reference (Prev_Token_Ptr,
904 Attribute_Name => Token_Name);
905 Delete_Node (Token_Node);
906 Scan; -- past type attribute identifier
909 exit when Token /= Tok_Apostrophe;
913 -- Fall through here after scanning type attribute
916 end P_Subtype_Mark_Attribute;
918 -----------------------
919 -- 3.2.2 Constraint --
920 -----------------------
922 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
924 -- SCALAR_CONSTRAINT ::=
925 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
927 -- COMPOSITE_CONSTRAINT ::=
928 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
930 -- If no constraint is present, this function returns Empty
932 -- Error recovery: can raise Error_Resync
934 function P_Constraint_Opt return Node_Id is
937 or else Bad_Spelling_Of (Tok_Range)
939 return P_Range_Constraint;
941 elsif Token = Tok_Digits
942 or else Bad_Spelling_Of (Tok_Digits)
944 return P_Digits_Constraint;
946 elsif Token = Tok_Delta
947 or else Bad_Spelling_Of (Tok_Delta)
949 return P_Delta_Constraint;
951 elsif Token = Tok_Left_Paren then
952 return P_Index_Or_Discriminant_Constraint;
954 elsif Token = Tok_In then
956 return P_Constraint_Opt;
961 end P_Constraint_Opt;
963 ------------------------------
964 -- 3.2.2 Scalar Constraint --
965 ------------------------------
967 -- Parsed by P_Constraint_Opt (3.2.2)
969 ---------------------------------
970 -- 3.2.2 Composite Constraint --
971 ---------------------------------
973 -- Parsed by P_Constraint_Opt (3.2.2)
975 --------------------------------------------------------
976 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
977 --------------------------------------------------------
979 -- This routine scans out a declaration starting with an identifier:
981 -- OBJECT_DECLARATION ::=
982 -- DEFINING_IDENTIFIER_LIST : [constant] [aliased]
983 -- SUBTYPE_INDICATION [:= EXPRESSION];
984 -- | DEFINING_IDENTIFIER_LIST : [constant] [aliased]
985 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
987 -- NUMBER_DECLARATION ::=
988 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
990 -- OBJECT_RENAMING_DECLARATION ::=
991 -- DEFINING_IDENTIFIER : SUBTYPE_MARK renames object_NAME;
992 -- | DEFINING_IDENTIFIER : ACCESS_DEFINITION renames object_NAME;
994 -- EXCEPTION_RENAMING_DECLARATION ::=
995 -- DEFINING_IDENTIFIER : exception renames exception_NAME;
997 -- EXCEPTION_DECLARATION ::=
998 -- DEFINING_IDENTIFIER_LIST : exception;
1000 -- Note that the ALIASED indication in an object declaration is
1001 -- marked by a flag in the parent node.
1003 -- The caller has checked that the initial token is an identifier
1005 -- The value returned is a list of declarations, one for each identifier
1006 -- in the list (as described in Sinfo, we always split up multiple
1007 -- declarations into the equivalent sequence of single declarations
1008 -- using the More_Ids and Prev_Ids flags to preserve the source).
1010 -- If the identifier turns out to be a probable statement rather than
1011 -- an identifier, then the scan is left pointing to the identifier and
1012 -- No_List is returned.
1014 -- Error recovery: can raise Error_Resync
1016 procedure P_Identifier_Declarations
1022 Decl_Node : Node_Id;
1023 Type_Node : Node_Id;
1024 Ident_Sloc : Source_Ptr;
1025 Scan_State : Saved_Scan_State;
1026 List_OK : Boolean := True;
1028 Init_Expr : Node_Id;
1029 Init_Loc : Source_Ptr;
1030 Con_Loc : Source_Ptr;
1032 Idents : array (Int range 1 .. 4096) of Entity_Id;
1033 -- Used to save identifiers in the identifier list. The upper bound
1034 -- of 4096 is expected to be infinite in practice, and we do not even
1035 -- bother to check if this upper bound is exceeded.
1037 Num_Idents : Nat := 1;
1038 -- Number of identifiers stored in Idents
1041 -- This procedure is called in renames cases to make sure that we do
1042 -- not have more than one identifier. If we do have more than one
1043 -- then an error message is issued (and the declaration is split into
1044 -- multiple declarations)
1046 function Token_Is_Renames return Boolean;
1047 -- Checks if current token is RENAMES, and if so, scans past it and
1048 -- returns True, otherwise returns False. Includes checking for some
1049 -- common error cases.
1051 procedure No_List is
1053 if Num_Idents > 1 then
1054 Error_Msg ("identifier list not allowed for RENAMES",
1061 function Token_Is_Renames return Boolean is
1062 At_Colon : Saved_Scan_State;
1065 if Token = Tok_Colon then
1066 Save_Scan_State (At_Colon);
1068 Check_Misspelling_Of (Tok_Renames);
1070 if Token = Tok_Renames then
1071 Error_Msg_SP ("extra "":"" ignored");
1072 Scan; -- past RENAMES
1075 Restore_Scan_State (At_Colon);
1080 Check_Misspelling_Of (Tok_Renames);
1082 if Token = Tok_Renames then
1083 Scan; -- past RENAMES
1089 end Token_Is_Renames;
1091 -- Start of processing for P_Identifier_Declarations
1094 Ident_Sloc := Token_Ptr;
1095 Save_Scan_State (Scan_State); -- at first identifier
1096 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1098 -- If we have a colon after the identifier, then we can assume that
1099 -- this is in fact a valid identifier declaration and can steam ahead.
1101 if Token = Tok_Colon then
1104 -- If we have a comma, then scan out the list of identifiers
1106 elsif Token = Tok_Comma then
1108 while Comma_Present loop
1109 Num_Idents := Num_Idents + 1;
1110 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1113 Save_Scan_State (Scan_State); -- at colon
1116 -- If we have identifier followed by := then we assume that what is
1117 -- really meant is an assignment statement. The assignment statement
1118 -- is scanned out and added to the list of declarations. An exception
1119 -- occurs if the := is followed by the keyword constant, in which case
1120 -- we assume it was meant to be a colon.
1122 elsif Token = Tok_Colon_Equal then
1125 if Token = Tok_Constant then
1126 Error_Msg_SP ("colon expected");
1129 Restore_Scan_State (Scan_State);
1130 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1134 -- If we have an IS keyword, then assume the TYPE keyword was missing
1136 elsif Token = Tok_Is then
1137 Restore_Scan_State (Scan_State);
1138 Append_To (Decls, P_Type_Declaration);
1142 -- Otherwise we have an error situation
1145 Restore_Scan_State (Scan_State);
1147 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1148 -- so, fix the keyword and return to scan the protected declaration.
1150 if Token_Name = Name_Protected then
1151 Check_95_Keyword (Tok_Protected, Tok_Identifier);
1152 Check_95_Keyword (Tok_Protected, Tok_Type);
1153 Check_95_Keyword (Tok_Protected, Tok_Body);
1155 if Token = Tok_Protected then
1160 -- Check misspelling possibilities. If so, correct the misspelling
1161 -- and return to scan out the resulting declaration.
1163 elsif Bad_Spelling_Of (Tok_Function)
1164 or else Bad_Spelling_Of (Tok_Procedure)
1165 or else Bad_Spelling_Of (Tok_Package)
1166 or else Bad_Spelling_Of (Tok_Pragma)
1167 or else Bad_Spelling_Of (Tok_Protected)
1168 or else Bad_Spelling_Of (Tok_Generic)
1169 or else Bad_Spelling_Of (Tok_Subtype)
1170 or else Bad_Spelling_Of (Tok_Type)
1171 or else Bad_Spelling_Of (Tok_Task)
1172 or else Bad_Spelling_Of (Tok_Use)
1173 or else Bad_Spelling_Of (Tok_For)
1178 -- Otherwise we definitely have an ordinary identifier with a junk
1179 -- token after it. Just complain that we expect a declaration, and
1180 -- skip to a semicolon
1183 Set_Declaration_Expected;
1184 Resync_Past_Semicolon;
1190 -- Come here with an identifier list and colon scanned out. We now
1191 -- build the nodes for the declarative items. One node is built for
1192 -- each identifier in the list, with the type information being
1193 -- repeated by rescanning the appropriate section of source.
1195 -- First an error check, if we have two identifiers in a row, a likely
1196 -- possibility is that the first of the identifiers is an incorrectly
1199 if Token = Tok_Identifier then
1201 SS : Saved_Scan_State;
1205 Save_Scan_State (SS);
1206 Scan; -- past initial identifier
1207 I2 := (Token = Tok_Identifier);
1208 Restore_Scan_State (SS);
1212 (Bad_Spelling_Of (Tok_Access) or else
1213 Bad_Spelling_Of (Tok_Aliased) or else
1214 Bad_Spelling_Of (Tok_Constant))
1221 -- Loop through identifiers
1226 -- Check for some cases of misused Ada 95 keywords
1228 if Token_Name = Name_Aliased then
1229 Check_95_Keyword (Tok_Aliased, Tok_Array);
1230 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1231 Check_95_Keyword (Tok_Aliased, Tok_Constant);
1236 if Token = Tok_Constant then
1237 Con_Loc := Token_Ptr;
1238 Scan; -- past CONSTANT
1240 -- Number declaration, initialization required
1242 Init_Expr := Init_Expr_Opt;
1244 if Present (Init_Expr) then
1245 Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1246 Set_Expression (Decl_Node, Init_Expr);
1248 -- Constant object declaration
1251 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1252 Set_Constant_Present (Decl_Node, True);
1254 if Token_Name = Name_Aliased then
1255 Check_95_Keyword (Tok_Aliased, Tok_Array);
1256 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1259 if Token = Tok_Aliased then
1260 Error_Msg_SC ("ALIASED should be before CONSTANT");
1261 Scan; -- past ALIASED
1262 Set_Aliased_Present (Decl_Node, True);
1265 if Token = Tok_Array then
1266 Set_Object_Definition
1267 (Decl_Node, P_Array_Type_Definition);
1269 Set_Object_Definition (Decl_Node, P_Subtype_Indication);
1272 if Token = Tok_Renames then
1274 ("CONSTANT not permitted in renaming declaration",
1276 Scan; -- Past renames
1277 Discard_Junk_Node (P_Name);
1283 elsif Token = Tok_Exception then
1284 Scan; -- past EXCEPTION
1286 if Token_Is_Renames then
1289 New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1290 Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1293 Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1296 -- Aliased case (note that an object definition is required)
1298 elsif Token = Tok_Aliased then
1299 Scan; -- past ALIASED
1300 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1301 Set_Aliased_Present (Decl_Node, True);
1303 if Token = Tok_Constant then
1304 Scan; -- past CONSTANT
1305 Set_Constant_Present (Decl_Node, True);
1308 if Token = Tok_Array then
1309 Set_Object_Definition
1310 (Decl_Node, P_Array_Type_Definition);
1312 Set_Object_Definition (Decl_Node, P_Subtype_Indication);
1317 elsif Token = Tok_Array then
1318 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1319 Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1321 -- Ada 0Y (AI-230): Access Definition case
1323 elsif Token = Tok_Access then
1324 if not Extensions_Allowed then
1326 ("generalized use of anonymous access types " &
1327 "is an Ada 0Y extension");
1331 ("\unit must be compiled with " &
1332 "'/'E'X'T'E'N'S'I'O'N'S'_'A'L'L'O'W'E'D qualifier");
1335 ("\unit must be compiled with -gnatX switch");
1339 Acc_Node := P_Access_Definition;
1341 if Token /= Tok_Renames then
1342 Error_Msg_SC ("'RENAMES' expected");
1346 Scan; -- past renames
1349 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1350 Set_Access_Definition (Decl_Node, Acc_Node);
1351 Set_Name (Decl_Node, P_Name);
1353 -- Subtype indication case
1356 Type_Node := P_Subtype_Mark;
1358 -- Object renaming declaration
1360 if Token_Is_Renames then
1363 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1364 Set_Subtype_Mark (Decl_Node, Type_Node);
1365 Set_Name (Decl_Node, P_Name);
1367 -- Object declaration
1370 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1371 Set_Object_Definition
1372 (Decl_Node, P_Subtype_Indication (Type_Node));
1374 -- RENAMES at this point means that we had the combination of
1375 -- a constraint on the Type_Node and renames, which is illegal
1377 if Token_Is_Renames then
1379 ("constraint not allowed in object renaming declaration",
1380 Constraint (Object_Definition (Decl_Node)));
1386 -- Scan out initialization, allowed only for object declaration
1388 Init_Loc := Token_Ptr;
1389 Init_Expr := Init_Expr_Opt;
1391 if Present (Init_Expr) then
1392 if Nkind (Decl_Node) = N_Object_Declaration then
1393 Set_Expression (Decl_Node, Init_Expr);
1395 Error_Msg ("initialization not allowed here", Init_Loc);
1400 Set_Defining_Identifier (Decl_Node, Idents (Ident));
1403 if Ident < Num_Idents then
1404 Set_More_Ids (Decl_Node, True);
1408 Set_Prev_Ids (Decl_Node, True);
1412 Append (Decl_Node, Decls);
1413 exit Ident_Loop when Ident = Num_Idents;
1414 Restore_Scan_State (Scan_State);
1417 end loop Ident_Loop;
1420 end P_Identifier_Declarations;
1422 -------------------------------
1423 -- 3.3.1 Object Declaration --
1424 -------------------------------
1426 -- OBJECT DECLARATION ::=
1427 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1428 -- SUBTYPE_INDICATION [:= EXPRESSION];
1429 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1430 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1431 -- | SINGLE_TASK_DECLARATION
1432 -- | SINGLE_PROTECTED_DECLARATION
1434 -- Cases starting with TASK are parsed by P_Task (9.1)
1435 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1436 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1438 -------------------------------------
1439 -- 3.3.1 Defining Identifier List --
1440 -------------------------------------
1442 -- DEFINING_IDENTIFIER_LIST ::=
1443 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1445 -- Always parsed by the construct in which it appears. See special
1446 -- section on "Handling of Defining Identifier Lists" in this unit.
1448 -------------------------------
1449 -- 3.3.2 Number Declaration --
1450 -------------------------------
1452 -- Parsed by P_Identifier_Declarations (3.3)
1454 -------------------------------------------------------------------------
1455 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
1456 -------------------------------------------------------------------------
1458 -- DERIVED_TYPE_DEFINITION ::=
1459 -- [abstract] new parent_SUBTYPE_INDICATION [RECORD_EXTENSION_PART]
1461 -- PRIVATE_EXTENSION_DECLARATION ::=
1462 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1463 -- [abstract] new ancestor_SUBTYPE_INDICATION with PRIVATE;
1465 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1467 -- The caller has already scanned out the part up to the NEW, and Token
1468 -- either contains Tok_New (or ought to, if it doesn't this procedure
1469 -- will post an appropriate "NEW expected" message).
1471 -- Note: the caller is responsible for filling in the Sloc field of
1472 -- the returned node in the private extension declaration case as
1473 -- well as the stuff relating to the discriminant part.
1475 -- Error recovery: can raise Error_Resync;
1477 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
1478 Typedef_Node : Node_Id;
1479 Typedecl_Node : Node_Id;
1482 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
1485 if Token = Tok_Abstract then
1486 Error_Msg_SC ("ABSTRACT must come before NEW, not after");
1490 Set_Subtype_Indication (Typedef_Node, P_Subtype_Indication);
1492 -- Deal with record extension, note that we assume that a WITH is
1493 -- missing in the case of "type X is new Y record ..." or in the
1494 -- case of "type X is new Y null record".
1497 or else Token = Tok_Record
1498 or else Token = Tok_Null
1500 T_With; -- past WITH or give error message
1502 if Token = Tok_Limited then
1504 ("LIMITED keyword not allowed in private extension");
1505 Scan; -- ignore LIMITED
1508 -- Private extension declaration
1510 if Token = Tok_Private then
1511 Scan; -- past PRIVATE
1513 -- Throw away the type definition node and build the type
1514 -- declaration node. Note the caller must set the Sloc,
1515 -- Discriminant_Specifications, Unknown_Discriminants_Present,
1516 -- and Defined_Identifier fields in the returned node.
1519 Make_Private_Extension_Declaration (No_Location,
1520 Defining_Identifier => Empty,
1521 Subtype_Indication => Subtype_Indication (Typedef_Node),
1522 Abstract_Present => Abstract_Present (Typedef_Node));
1524 Delete_Node (Typedef_Node);
1525 return Typedecl_Node;
1527 -- Derived type definition with record extension part
1530 Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
1531 return Typedef_Node;
1534 -- Derived type definition with no record extension part
1537 return Typedef_Node;
1539 end P_Derived_Type_Def_Or_Private_Ext_Decl;
1541 ---------------------------
1542 -- 3.5 Range Constraint --
1543 ---------------------------
1545 -- RANGE_CONSTRAINT ::= range RANGE
1547 -- The caller has checked that the initial token is RANGE
1549 -- Error recovery: cannot raise Error_Resync
1551 function P_Range_Constraint return Node_Id is
1552 Range_Node : Node_Id;
1555 Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
1557 Set_Range_Expression (Range_Node, P_Range);
1559 end P_Range_Constraint;
1566 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1568 -- Note: the range that appears in a membership test is parsed by
1569 -- P_Range_Or_Subtype_Mark (3.5).
1571 -- Error recovery: cannot raise Error_Resync
1573 function P_Range return Node_Id is
1574 Expr_Node : Node_Id;
1575 Range_Node : Node_Id;
1578 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
1580 if Expr_Form = EF_Range_Attr then
1583 elsif Token = Tok_Dot_Dot then
1584 Range_Node := New_Node (N_Range, Token_Ptr);
1585 Set_Low_Bound (Range_Node, Expr_Node);
1587 Expr_Node := P_Expression;
1588 Check_Simple_Expression (Expr_Node);
1589 Set_High_Bound (Range_Node, Expr_Node);
1592 -- Anything else is an error
1595 T_Dot_Dot; -- force missing .. message
1600 ----------------------------------
1601 -- 3.5 P_Range_Or_Subtype_Mark --
1602 ----------------------------------
1605 -- RANGE_ATTRIBUTE_REFERENCE
1606 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1608 -- This routine scans out the range or subtype mark that forms the right
1609 -- operand of a membership test.
1611 -- Note: as documented in the Sinfo interface, although the syntax only
1612 -- allows a subtype mark, we in fact allow any simple expression to be
1613 -- returned from this routine. The semantics is responsible for issuing
1614 -- an appropriate message complaining if the argument is not a name.
1615 -- This simplifies the coding and error recovery processing in the
1616 -- parser, and in any case it is preferable not to consider this a
1617 -- syntax error and to continue with the semantic analysis.
1619 -- Error recovery: cannot raise Error_Resync
1621 function P_Range_Or_Subtype_Mark return Node_Id is
1622 Expr_Node : Node_Id;
1623 Range_Node : Node_Id;
1626 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
1628 if Expr_Form = EF_Range_Attr then
1631 -- Simple_Expression .. Simple_Expression
1633 elsif Token = Tok_Dot_Dot then
1634 Check_Simple_Expression (Expr_Node);
1635 Range_Node := New_Node (N_Range, Token_Ptr);
1636 Set_Low_Bound (Range_Node, Expr_Node);
1638 Set_High_Bound (Range_Node, P_Simple_Expression);
1641 -- Case of subtype mark (optionally qualified simple name or an
1642 -- attribute whose prefix is an optionally qualifed simple name)
1644 elsif Expr_Form = EF_Simple_Name
1645 or else Nkind (Expr_Node) = N_Attribute_Reference
1647 -- Check for error of range constraint after a subtype mark
1649 if Token = Tok_Range then
1651 ("range constraint not allowed in membership test");
1655 -- Check for error of DIGITS or DELTA after a subtype mark
1657 elsif Token = Tok_Digits or else Token = Tok_Delta then
1659 ("accuracy definition not allowed in membership test");
1660 Scan; -- past DIGITS or DELTA
1663 elsif Token = Tok_Apostrophe then
1664 return P_Subtype_Mark_Attribute (Expr_Node);
1670 -- At this stage, we have some junk following the expression. We
1671 -- really can't tell what is wrong, might be a missing semicolon,
1672 -- or a missing THEN, or whatever. Our caller will figure it out!
1677 end P_Range_Or_Subtype_Mark;
1679 ----------------------------------------
1680 -- 3.5.1 Enumeration Type Definition --
1681 ----------------------------------------
1683 -- ENUMERATION_TYPE_DEFINITION ::=
1684 -- (ENUMERATION_LITERAL_SPECIFICATION
1685 -- {, ENUMERATION_LITERAL_SPECIFICATION})
1687 -- The caller has already scanned out the TYPE keyword
1689 -- Error recovery: can raise Error_Resync;
1691 function P_Enumeration_Type_Definition return Node_Id is
1692 Typedef_Node : Node_Id;
1695 Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
1696 Set_Literals (Typedef_Node, New_List);
1701 Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
1702 exit when not Comma_Present;
1706 return Typedef_Node;
1707 end P_Enumeration_Type_Definition;
1709 ----------------------------------------------
1710 -- 3.5.1 Enumeration Literal Specification --
1711 ----------------------------------------------
1713 -- ENUMERATION_LITERAL_SPECIFICATION ::=
1714 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
1716 -- Error recovery: can raise Error_Resync
1718 function P_Enumeration_Literal_Specification return Node_Id is
1720 if Token = Tok_Char_Literal then
1721 return P_Defining_Character_Literal;
1723 return P_Defining_Identifier (C_Comma_Right_Paren);
1725 end P_Enumeration_Literal_Specification;
1727 ---------------------------------------
1728 -- 3.5.1 Defining_Character_Literal --
1729 ---------------------------------------
1731 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
1733 -- Error recovery: cannot raise Error_Resync
1735 -- The caller has checked that the current token is a character literal
1737 function P_Defining_Character_Literal return Node_Id is
1738 Literal_Node : Node_Id;
1741 Literal_Node := Token_Node;
1742 Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
1743 Scan; -- past character literal
1744 return Literal_Node;
1745 end P_Defining_Character_Literal;
1747 ------------------------------------
1748 -- 3.5.4 Integer Type Definition --
1749 ------------------------------------
1751 -- Parsed by P_Type_Declaration (3.2.1)
1753 -------------------------------------------
1754 -- 3.5.4 Signed Integer Type Definition --
1755 -------------------------------------------
1757 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
1758 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
1760 -- Normally the initial token on entry is RANGE, but in some
1761 -- error conditions, the range token was missing and control is
1762 -- passed with Token pointing to first token of the first expression.
1764 -- Error recovery: cannot raise Error_Resync
1766 function P_Signed_Integer_Type_Definition return Node_Id is
1767 Typedef_Node : Node_Id;
1768 Expr_Node : Node_Id;
1771 Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
1773 if Token = Tok_Range then
1777 Expr_Node := P_Expression;
1778 Check_Simple_Expression (Expr_Node);
1779 Set_Low_Bound (Typedef_Node, Expr_Node);
1781 Expr_Node := P_Expression;
1782 Check_Simple_Expression (Expr_Node);
1783 Set_High_Bound (Typedef_Node, Expr_Node);
1784 return Typedef_Node;
1785 end P_Signed_Integer_Type_Definition;
1787 ------------------------------------
1788 -- 3.5.4 Modular Type Definition --
1789 ------------------------------------
1791 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
1793 -- The caller has checked that the initial token is MOD
1795 -- Error recovery: cannot raise Error_Resync
1797 function P_Modular_Type_Definition return Node_Id is
1798 Typedef_Node : Node_Id;
1802 Error_Msg_SC ("(Ada 83): modular types not allowed");
1805 Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
1807 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
1809 -- Handle mod L..R cleanly
1811 if Token = Tok_Dot_Dot then
1812 Error_Msg_SC ("range not allowed for modular type");
1814 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
1817 return Typedef_Node;
1818 end P_Modular_Type_Definition;
1820 ---------------------------------
1821 -- 3.5.6 Real Type Definition --
1822 ---------------------------------
1824 -- Parsed by P_Type_Declaration (3.2.1)
1826 --------------------------------------
1827 -- 3.5.7 Floating Point Definition --
1828 --------------------------------------
1830 -- FLOATING_POINT_DEFINITION ::=
1831 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
1833 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
1835 -- The caller has checked that the initial token is DIGITS
1837 -- Error recovery: cannot raise Error_Resync
1839 function P_Floating_Point_Definition return Node_Id is
1840 Digits_Loc : constant Source_Ptr := Token_Ptr;
1842 Expr_Node : Node_Id;
1845 Scan; -- past DIGITS
1846 Expr_Node := P_Expression_No_Right_Paren;
1847 Check_Simple_Expression_In_Ada_83 (Expr_Node);
1849 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
1851 if Token = Tok_Delta then
1852 Error_Msg_SC ("DELTA must come before DIGITS");
1853 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
1855 Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
1857 -- OK floating-point definition
1860 Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
1863 Set_Digits_Expression (Def_Node, Expr_Node);
1864 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
1866 end P_Floating_Point_Definition;
1868 -------------------------------------
1869 -- 3.5.7 Real Range Specification --
1870 -------------------------------------
1872 -- REAL_RANGE_SPECIFICATION ::=
1873 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
1875 -- Error recovery: cannot raise Error_Resync
1877 function P_Real_Range_Specification_Opt return Node_Id is
1878 Specification_Node : Node_Id;
1879 Expr_Node : Node_Id;
1882 if Token = Tok_Range then
1883 Specification_Node :=
1884 New_Node (N_Real_Range_Specification, Token_Ptr);
1886 Expr_Node := P_Expression_No_Right_Paren;
1887 Check_Simple_Expression (Expr_Node);
1888 Set_Low_Bound (Specification_Node, Expr_Node);
1890 Expr_Node := P_Expression_No_Right_Paren;
1891 Check_Simple_Expression (Expr_Node);
1892 Set_High_Bound (Specification_Node, Expr_Node);
1893 return Specification_Node;
1897 end P_Real_Range_Specification_Opt;
1899 -----------------------------------
1900 -- 3.5.9 Fixed Point Definition --
1901 -----------------------------------
1903 -- FIXED_POINT_DEFINITION ::=
1904 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
1906 -- ORDINARY_FIXED_POINT_DEFINITION ::=
1907 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
1909 -- DECIMAL_FIXED_POINT_DEFINITION ::=
1910 -- delta static_EXPRESSION
1911 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
1913 -- The caller has checked that the initial token is DELTA
1915 -- Error recovery: cannot raise Error_Resync
1917 function P_Fixed_Point_Definition return Node_Id is
1918 Delta_Node : Node_Id;
1919 Delta_Loc : Source_Ptr;
1921 Expr_Node : Node_Id;
1924 Delta_Loc := Token_Ptr;
1926 Delta_Node := P_Expression_No_Right_Paren;
1927 Check_Simple_Expression_In_Ada_83 (Delta_Node);
1929 if Token = Tok_Digits then
1931 Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
1934 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
1935 Scan; -- past DIGITS
1936 Expr_Node := P_Expression_No_Right_Paren;
1937 Check_Simple_Expression_In_Ada_83 (Expr_Node);
1938 Set_Digits_Expression (Def_Node, Expr_Node);
1941 Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
1943 -- Range is required in ordinary fixed point case
1945 if Token /= Tok_Range then
1946 Error_Msg_AP ("range must be given for fixed-point type");
1951 Set_Delta_Expression (Def_Node, Delta_Node);
1952 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
1954 end P_Fixed_Point_Definition;
1956 --------------------------------------------
1957 -- 3.5.9 Ordinary Fixed Point Definition --
1958 --------------------------------------------
1960 -- Parsed by P_Fixed_Point_Definition (3.5.9)
1962 -------------------------------------------
1963 -- 3.5.9 Decimal Fixed Point Definition --
1964 -------------------------------------------
1966 -- Parsed by P_Decimal_Point_Definition (3.5.9)
1968 ------------------------------
1969 -- 3.5.9 Digits Constraint --
1970 ------------------------------
1972 -- DIGITS_CONSTRAINT ::=
1973 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
1975 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
1977 -- The caller has checked that the initial token is DIGITS
1979 function P_Digits_Constraint return Node_Id is
1980 Constraint_Node : Node_Id;
1981 Expr_Node : Node_Id;
1984 Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
1985 Scan; -- past DIGITS
1986 Expr_Node := P_Expression_No_Right_Paren;
1987 Check_Simple_Expression_In_Ada_83 (Expr_Node);
1988 Set_Digits_Expression (Constraint_Node, Expr_Node);
1990 if Token = Tok_Range then
1991 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
1994 return Constraint_Node;
1995 end P_Digits_Constraint;
1997 -----------------------------
1998 -- 3.5.9 Delta Constraint --
1999 -----------------------------
2001 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2003 -- Note: this is an obsolescent feature in Ada 95 (I.3)
2005 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2007 -- The caller has checked that the initial token is DELTA
2009 -- Error recovery: cannot raise Error_Resync
2011 function P_Delta_Constraint return Node_Id is
2012 Constraint_Node : Node_Id;
2013 Expr_Node : Node_Id;
2016 Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2018 Expr_Node := P_Expression_No_Right_Paren;
2019 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2020 Set_Delta_Expression (Constraint_Node, Expr_Node);
2022 if Token = Tok_Range then
2023 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2026 return Constraint_Node;
2027 end P_Delta_Constraint;
2029 --------------------------------
2030 -- 3.6 Array Type Definition --
2031 --------------------------------
2033 -- ARRAY_TYPE_DEFINITION ::=
2034 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2036 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2037 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2038 -- COMPONENT_DEFINITION
2040 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2042 -- CONSTRAINED_ARRAY_DEFINITION ::=
2043 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2044 -- COMPONENT_DEFINITION
2046 -- DISCRETE_SUBTYPE_DEFINITION ::=
2047 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2049 -- COMPONENT_DEFINITION ::=
2050 -- [aliased] SUBTYPE_INDICATION | ACCESS_DEFINITION
2052 -- The caller has checked that the initial token is ARRAY
2054 -- Error recovery: can raise Error_Resync
2056 function P_Array_Type_Definition return Node_Id is
2057 Array_Loc : Source_Ptr;
2058 CompDef_Node : Node_Id;
2060 Subs_List : List_Id;
2061 Scan_State : Saved_Scan_State;
2064 Array_Loc := Token_Ptr;
2066 Subs_List := New_List;
2069 -- It's quite tricky to disentangle these two possibilities, so we do
2070 -- a prescan to determine which case we have and then reset the scan.
2071 -- The prescan skips past possible subtype mark tokens.
2073 Save_Scan_State (Scan_State); -- just after paren
2075 while Token in Token_Class_Desig or else
2076 Token = Tok_Dot or else
2077 Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2082 -- If we end up on RANGE <> then we have the unconstrained case. We
2083 -- will also allow the RANGE to be omitted, just to improve error
2084 -- handling for a case like array (integer <>) of integer;
2086 Scan; -- past possible RANGE or <>
2088 if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2089 Prev_Token = Tok_Box
2091 Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2092 Restore_Scan_State (Scan_State); -- to first subtype mark
2095 Append (P_Subtype_Mark_Resync, Subs_List);
2098 exit when Token = Tok_Right_Paren or else Token = Tok_Of;
2102 Set_Subtype_Marks (Def_Node, Subs_List);
2105 Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2106 Restore_Scan_State (Scan_State); -- to first discrete range
2109 Append (P_Discrete_Subtype_Definition, Subs_List);
2110 exit when not Comma_Present;
2113 Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2119 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2121 -- Ada 0Y (AI-230): Access Definition case
2123 if Token = Tok_Access then
2124 if not Extensions_Allowed then
2126 ("generalized use of anonymous access types " &
2127 "is an Ada 0Y extension");
2131 ("\unit must be compiled with " &
2132 "'/'E'X'T'E'N'S'I'O'N'S'_'A'L'L'O'W'E'D qualifier");
2135 ("\unit must be compiled with -gnatX switch");
2139 Set_Subtype_Indication (CompDef_Node, Empty);
2140 Set_Aliased_Present (CompDef_Node, False);
2141 Set_Access_Definition (CompDef_Node, P_Access_Definition);
2143 Set_Access_Definition (CompDef_Node, Empty);
2145 if Token_Name = Name_Aliased then
2146 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2149 if Token = Tok_Aliased then
2150 Set_Aliased_Present (CompDef_Node, True);
2151 Scan; -- past ALIASED
2154 Set_Subtype_Indication (CompDef_Node, P_Subtype_Indication);
2157 Set_Component_Definition (Def_Node, CompDef_Node);
2160 end P_Array_Type_Definition;
2162 -----------------------------------------
2163 -- 3.6 Unconstrained Array Definition --
2164 -----------------------------------------
2166 -- Parsed by P_Array_Type_Definition (3.6)
2168 ---------------------------------------
2169 -- 3.6 Constrained Array Definition --
2170 ---------------------------------------
2172 -- Parsed by P_Array_Type_Definition (3.6)
2174 --------------------------------------
2175 -- 3.6 Discrete Subtype Definition --
2176 --------------------------------------
2178 -- DISCRETE_SUBTYPE_DEFINITION ::=
2179 -- discrete_SUBTYPE_INDICATION | RANGE
2181 -- Note: the discrete subtype definition appearing in a constrained
2182 -- array definition is parsed by P_Array_Type_Definition (3.6)
2184 -- Error recovery: cannot raise Error_Resync
2186 function P_Discrete_Subtype_Definition return Node_Id is
2188 -- The syntax of a discrete subtype definition is identical to that
2189 -- of a discrete range, so we simply share the same parsing code.
2191 return P_Discrete_Range;
2192 end P_Discrete_Subtype_Definition;
2194 -------------------------------
2195 -- 3.6 Component Definition --
2196 -------------------------------
2198 -- For the array case, parsed by P_Array_Type_Definition (3.6)
2199 -- For the record case, parsed by P_Component_Declaration (3.8)
2201 -----------------------------
2202 -- 3.6.1 Index Constraint --
2203 -----------------------------
2205 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2207 ---------------------------
2208 -- 3.6.1 Discrete Range --
2209 ---------------------------
2211 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2213 -- The possible forms for a discrete range are:
2215 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
2216 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
2217 -- Range_Attribute (RANGE, 3.5)
2218 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
2220 -- Error recovery: cannot raise Error_Resync
2222 function P_Discrete_Range return Node_Id is
2223 Expr_Node : Node_Id;
2224 Range_Node : Node_Id;
2227 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2229 if Expr_Form = EF_Range_Attr then
2232 elsif Token = Tok_Range then
2233 if Expr_Form /= EF_Simple_Name then
2234 Error_Msg_SC ("range must be preceded by subtype mark");
2237 return P_Subtype_Indication (Expr_Node);
2239 -- Check Expression .. Expression case
2241 elsif Token = Tok_Dot_Dot then
2242 Range_Node := New_Node (N_Range, Token_Ptr);
2243 Set_Low_Bound (Range_Node, Expr_Node);
2245 Expr_Node := P_Expression;
2246 Check_Simple_Expression (Expr_Node);
2247 Set_High_Bound (Range_Node, Expr_Node);
2250 -- Otherwise we must have a subtype mark
2252 elsif Expr_Form = EF_Simple_Name then
2255 -- If incorrect, complain that we expect ..
2261 end P_Discrete_Range;
2263 ----------------------------
2264 -- 3.7 Discriminant Part --
2265 ----------------------------
2267 -- DISCRIMINANT_PART ::=
2268 -- UNKNOWN_DISCRIMINANT_PART
2269 -- | KNOWN_DISCRIMINANT_PART
2271 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2272 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2274 ------------------------------------
2275 -- 3.7 Unknown Discriminant Part --
2276 ------------------------------------
2278 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
2280 -- If no unknown discriminant part is present, then False is returned,
2281 -- otherwise the unknown discriminant is scanned out and True is returned.
2283 -- Error recovery: cannot raise Error_Resync
2285 function P_Unknown_Discriminant_Part_Opt return Boolean is
2286 Scan_State : Saved_Scan_State;
2289 if Token /= Tok_Left_Paren then
2293 Save_Scan_State (Scan_State);
2294 Scan; -- past the left paren
2296 if Token = Tok_Box then
2298 Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
2301 Scan; -- past the box
2302 T_Right_Paren; -- must be followed by right paren
2306 Restore_Scan_State (Scan_State);
2310 end P_Unknown_Discriminant_Part_Opt;
2312 ----------------------------------
2313 -- 3.7 Known Discriminant Part --
2314 ----------------------------------
2316 -- KNOWN_DISCRIMINANT_PART ::=
2317 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2319 -- DISCRIMINANT_SPECIFICATION ::=
2320 -- DEFINING_IDENTIFIER_LIST : SUBTYPE_MARK
2321 -- [:= DEFAULT_EXPRESSION]
2322 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2323 -- [:= DEFAULT_EXPRESSION]
2325 -- If no known discriminant part is present, then No_List is returned
2327 -- Error recovery: cannot raise Error_Resync
2329 function P_Known_Discriminant_Part_Opt return List_Id is
2330 Specification_Node : Node_Id;
2331 Specification_List : List_Id;
2332 Ident_Sloc : Source_Ptr;
2333 Scan_State : Saved_Scan_State;
2337 Idents : array (Int range 1 .. 4096) of Entity_Id;
2338 -- This array holds the list of defining identifiers. The upper bound
2339 -- of 4096 is intended to be essentially infinite, and we do not even
2340 -- bother to check for it being exceeded.
2343 if Token = Tok_Left_Paren then
2344 Specification_List := New_List;
2346 P_Pragmas_Misplaced;
2348 Specification_Loop : loop
2350 Ident_Sloc := Token_Ptr;
2351 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2354 while Comma_Present loop
2355 Num_Idents := Num_Idents + 1;
2356 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2361 -- If there are multiple identifiers, we repeatedly scan the
2362 -- type and initialization expression information by resetting
2363 -- the scan pointer (so that we get completely separate trees
2364 -- for each occurrence).
2366 if Num_Idents > 1 then
2367 Save_Scan_State (Scan_State);
2370 -- Loop through defining identifiers in list
2374 Specification_Node :=
2375 New_Node (N_Discriminant_Specification, Ident_Sloc);
2376 Set_Defining_Identifier (Specification_Node, Idents (Ident));
2378 if Token = Tok_Access then
2381 ("(Ada 83) access discriminant not allowed!");
2384 Set_Discriminant_Type
2385 (Specification_Node, P_Access_Definition);
2387 Set_Discriminant_Type
2388 (Specification_Node, P_Subtype_Mark);
2393 (Specification_Node, Init_Expr_Opt (True));
2396 Set_Prev_Ids (Specification_Node, True);
2399 if Ident < Num_Idents then
2400 Set_More_Ids (Specification_Node, True);
2403 Append (Specification_Node, Specification_List);
2404 exit Ident_Loop when Ident = Num_Idents;
2406 Restore_Scan_State (Scan_State);
2407 end loop Ident_Loop;
2409 exit Specification_Loop when Token /= Tok_Semicolon;
2411 P_Pragmas_Misplaced;
2412 end loop Specification_Loop;
2415 return Specification_List;
2420 end P_Known_Discriminant_Part_Opt;
2422 -------------------------------------
2423 -- 3.7 DIscriminant Specification --
2424 -------------------------------------
2426 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
2428 -----------------------------
2429 -- 3.7 Default Expression --
2430 -----------------------------
2432 -- Always parsed (simply as an Expression) by the parent construct
2434 ------------------------------------
2435 -- 3.7.1 Discriminant Constraint --
2436 ------------------------------------
2438 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2440 --------------------------------------------------------
2441 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
2442 --------------------------------------------------------
2444 -- DISCRIMINANT_CONSTRAINT ::=
2445 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
2447 -- DISCRIMINANT_ASSOCIATION ::=
2448 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2451 -- This routine parses either an index or a discriminant constraint. As
2452 -- is clear from the above grammar, it is often possible to clearly
2453 -- determine which of the two possibilities we have, but there are
2454 -- cases (those in which we have a series of expressions of the same
2455 -- syntactic form as subtype indications), where we cannot tell. Since
2456 -- this means that in any case the semantic phase has to distinguish
2457 -- between the two, there is not much point in the parser trying to
2458 -- distinguish even those cases where the difference is clear. In any
2459 -- case, if we have a situation like:
2461 -- (A => 123, 235 .. 500)
2463 -- it is not clear which of the two items is the wrong one, better to
2464 -- let the semantic phase give a clear message. Consequently, this
2465 -- routine in general returns a list of items which can be either
2466 -- discrete ranges or discriminant associations.
2468 -- The caller has checked that the initial token is a left paren
2470 -- Error recovery: can raise Error_Resync
2472 function P_Index_Or_Discriminant_Constraint return Node_Id is
2473 Scan_State : Saved_Scan_State;
2474 Constr_Node : Node_Id;
2475 Constr_List : List_Id;
2476 Expr_Node : Node_Id;
2477 Result_Node : Node_Id;
2480 Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
2482 Constr_List := New_List;
2483 Set_Constraints (Result_Node, Constr_List);
2485 -- The two syntactic forms are a little mixed up, so what we are doing
2486 -- here is looking at the first entry to determine which case we have
2488 -- A discriminant constraint is a list of discriminant associations,
2489 -- which have one of the following possible forms:
2493 -- Id | Id | .. | Id => Expression
2495 -- An index constraint is a list of discrete ranges which have one
2496 -- of the following possible forms:
2499 -- Subtype_Mark range Range
2501 -- Simple_Expression .. Simple_Expression
2503 -- Loop through discriminants in list
2506 -- Check cases of Id => Expression or Id | Id => Expression
2508 if Token = Tok_Identifier then
2509 Save_Scan_State (Scan_State); -- at Id
2512 if Token = Tok_Arrow or else Token = Tok_Vertical_Bar then
2513 Restore_Scan_State (Scan_State); -- to Id
2514 Append (P_Discriminant_Association, Constr_List);
2517 Restore_Scan_State (Scan_State); -- to Id
2521 -- Otherwise scan out an expression and see what we have got
2523 Expr_Node := P_Expression_Or_Range_Attribute;
2525 if Expr_Form = EF_Range_Attr then
2526 Append (Expr_Node, Constr_List);
2528 elsif Token = Tok_Range then
2529 if Expr_Form /= EF_Simple_Name then
2530 Error_Msg_SC ("subtype mark required before RANGE");
2533 Append (P_Subtype_Indication (Expr_Node), Constr_List);
2536 -- Check Simple_Expression .. Simple_Expression case
2538 elsif Token = Tok_Dot_Dot then
2539 Check_Simple_Expression (Expr_Node);
2540 Constr_Node := New_Node (N_Range, Token_Ptr);
2541 Set_Low_Bound (Constr_Node, Expr_Node);
2543 Expr_Node := P_Expression;
2544 Check_Simple_Expression (Expr_Node);
2545 Set_High_Bound (Constr_Node, Expr_Node);
2546 Append (Constr_Node, Constr_List);
2549 -- Case of an expression which could be either form
2552 Append (Expr_Node, Constr_List);
2556 -- Here with a single entry scanned
2559 exit when not Comma_Present;
2565 end P_Index_Or_Discriminant_Constraint;
2567 -------------------------------------
2568 -- 3.7.1 Discriminant Association --
2569 -------------------------------------
2571 -- DISCRIMINANT_ASSOCIATION ::=
2572 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2575 -- This routine is used only when the name list is present and the caller
2576 -- has already checked this (by scanning ahead and repositioning the
2579 -- Error_Recovery: cannot raise Error_Resync;
2581 function P_Discriminant_Association return Node_Id is
2582 Discr_Node : Node_Id;
2583 Names_List : List_Id;
2584 Ident_Sloc : Source_Ptr;
2587 Ident_Sloc := Token_Ptr;
2588 Names_List := New_List;
2591 Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
2592 exit when Token /= Tok_Vertical_Bar;
2596 Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
2597 Set_Selector_Names (Discr_Node, Names_List);
2599 Set_Expression (Discr_Node, P_Expression);
2601 end P_Discriminant_Association;
2603 ---------------------------------
2604 -- 3.8 Record Type Definition --
2605 ---------------------------------
2607 -- RECORD_TYPE_DEFINITION ::=
2608 -- [[abstract] tagged] [limited] RECORD_DEFINITION
2610 -- There is no node in the tree for a record type definition. Instead
2611 -- a record definition node appears, with possible Abstract_Present,
2612 -- Tagged_Present, and Limited_Present flags set appropriately.
2614 ----------------------------
2615 -- 3.8 Record Definition --
2616 ----------------------------
2618 -- RECORD_DEFINITION ::=
2624 -- Note: in the case where a record definition node is used to represent
2625 -- a record type definition, the caller sets the Tagged_Present and
2626 -- Limited_Present flags in the resulting N_Record_Definition node as
2629 -- Note that the RECORD token at the start may be missing in certain
2630 -- error situations, so this function is expected to post the error
2632 -- Error recovery: can raise Error_Resync
2634 function P_Record_Definition return Node_Id is
2638 Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
2642 if Token = Tok_Null then
2645 Set_Null_Present (Rec_Node, True);
2647 -- Case starting with RECORD keyword. Build scope stack entry. For the
2648 -- column, we use the first non-blank character on the line, to deal
2649 -- with situations such as:
2655 -- which is not official RM indentation, but is not uncommon usage
2659 Scope.Table (Scope.Last).Etyp := E_Record;
2660 Scope.Table (Scope.Last).Ecol := Start_Column;
2661 Scope.Table (Scope.Last).Sloc := Token_Ptr;
2662 Scope.Table (Scope.Last).Labl := Error;
2663 Scope.Table (Scope.Last).Junk := (Token /= Tok_Record);
2667 Set_Component_List (Rec_Node, P_Component_List);
2670 exit when Check_End;
2671 Discard_Junk_Node (P_Component_List);
2676 end P_Record_Definition;
2678 -------------------------
2679 -- 3.8 Component List --
2680 -------------------------
2682 -- COMPONENT_LIST ::=
2683 -- COMPONENT_ITEM {COMPONENT_ITEM}
2684 -- | {COMPONENT_ITEM} VARIANT_PART
2687 -- Error recovery: cannot raise Error_Resync
2689 function P_Component_List return Node_Id is
2690 Component_List_Node : Node_Id;
2691 Decls_List : List_Id;
2692 Scan_State : Saved_Scan_State;
2695 Component_List_Node := New_Node (N_Component_List, Token_Ptr);
2696 Decls_List := New_List;
2698 if Token = Tok_Null then
2701 P_Pragmas_Opt (Decls_List);
2702 Set_Null_Present (Component_List_Node, True);
2703 return Component_List_Node;
2706 P_Pragmas_Opt (Decls_List);
2708 if Token /= Tok_Case then
2709 Component_Scan_Loop : loop
2710 P_Component_Items (Decls_List);
2711 P_Pragmas_Opt (Decls_List);
2713 exit Component_Scan_Loop when Token = Tok_End
2714 or else Token = Tok_Case
2715 or else Token = Tok_When;
2717 -- We are done if we do not have an identifier. However, if
2718 -- we have a misspelled reserved identifier that is in a column
2719 -- to the right of the record definition, we will treat it as
2720 -- an identifier. It turns out to be too dangerous in practice
2721 -- to accept such a mis-spelled identifier which does not have
2722 -- this additional clue that confirms the incorrect spelling.
2724 if Token /= Tok_Identifier then
2725 if Start_Column > Scope.Table (Scope.Last).Ecol
2726 and then Is_Reserved_Identifier
2728 Save_Scan_State (Scan_State); -- at reserved id
2729 Scan; -- possible reserved id
2731 if Token = Tok_Comma or else Token = Tok_Colon then
2732 Restore_Scan_State (Scan_State);
2733 Scan_Reserved_Identifier (Force_Msg => True);
2735 -- Note reserved identifier used as field name after
2736 -- all because not followed by colon or comma
2739 Restore_Scan_State (Scan_State);
2740 exit Component_Scan_Loop;
2743 -- Non-identifier that definitely was not reserved id
2746 exit Component_Scan_Loop;
2749 end loop Component_Scan_Loop;
2752 if Token = Tok_Case then
2753 Set_Variant_Part (Component_List_Node, P_Variant_Part);
2755 -- Check for junk after variant part
2757 if Token = Tok_Identifier then
2758 Save_Scan_State (Scan_State);
2759 Scan; -- past identifier
2761 if Token = Tok_Colon then
2762 Restore_Scan_State (Scan_State);
2763 Error_Msg_SC ("component may not follow variant part");
2764 Discard_Junk_Node (P_Component_List);
2766 elsif Token = Tok_Case then
2767 Restore_Scan_State (Scan_State);
2768 Error_Msg_SC ("only one variant part allowed in a record");
2769 Discard_Junk_Node (P_Component_List);
2772 Restore_Scan_State (Scan_State);
2778 Set_Component_Items (Component_List_Node, Decls_List);
2779 return Component_List_Node;
2780 end P_Component_List;
2782 -------------------------
2783 -- 3.8 Component Item --
2784 -------------------------
2786 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
2788 -- COMPONENT_DECLARATION ::=
2789 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
2790 -- [:= DEFAULT_EXPRESSION];
2792 -- COMPONENT_DEFINITION ::=
2793 -- [aliased] SUBTYPE_INDICATION | ACCESS_DEFINITION
2795 -- Error recovery: cannot raise Error_Resync, if an error occurs,
2796 -- the scan is positioned past the following semicolon.
2798 -- Note: we do not yet allow representation clauses to appear as component
2799 -- items, do we need to add this capability sometime in the future ???
2801 procedure P_Component_Items (Decls : List_Id) is
2802 CompDef_Node : Node_Id;
2803 Decl_Node : Node_Id;
2804 Scan_State : Saved_Scan_State;
2807 Ident_Sloc : Source_Ptr;
2809 Idents : array (Int range 1 .. 4096) of Entity_Id;
2810 -- This array holds the list of defining identifiers. The upper bound
2811 -- of 4096 is intended to be essentially infinite, and we do not even
2812 -- bother to check for it being exceeded.
2815 if Token /= Tok_Identifier then
2816 Error_Msg_SC ("component declaration expected");
2817 Resync_Past_Semicolon;
2821 Ident_Sloc := Token_Ptr;
2822 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2825 while Comma_Present loop
2826 Num_Idents := Num_Idents + 1;
2827 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2832 -- If there are multiple identifiers, we repeatedly scan the
2833 -- type and initialization expression information by resetting
2834 -- the scan pointer (so that we get completely separate trees
2835 -- for each occurrence).
2837 if Num_Idents > 1 then
2838 Save_Scan_State (Scan_State);
2841 -- Loop through defining identifiers in list
2846 -- The following block is present to catch Error_Resync
2847 -- which causes the parse to be reset past the semicolon
2850 Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
2851 Set_Defining_Identifier (Decl_Node, Idents (Ident));
2853 if Token = Tok_Constant then
2854 Error_Msg_SC ("constant components are not permitted");
2858 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2860 if Token = Tok_Access then
2861 if not Extensions_Allowed then
2863 ("Generalized use of anonymous access types " &
2864 "is an Ada0X extension");
2868 ("\unit must be compiled with " &
2869 "'/'E'X'T'E'N'S'I'O'N'S'_'A'L'L'O'W'E'D qualifier");
2872 ("\unit must be compiled with -gnatX switch");
2876 Set_Subtype_Indication (CompDef_Node, Empty);
2877 Set_Aliased_Present (CompDef_Node, False);
2878 Set_Access_Definition (CompDef_Node, P_Access_Definition);
2881 Set_Access_Definition (CompDef_Node, Empty);
2883 if Token_Name = Name_Aliased then
2884 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2887 if Token = Tok_Aliased then
2888 Scan; -- past ALIASED
2889 Set_Aliased_Present (CompDef_Node, True);
2892 if Token = Tok_Array then
2894 ("anonymous arrays not allowed as components");
2898 Set_Subtype_Indication (CompDef_Node, P_Subtype_Indication);
2901 Set_Component_Definition (Decl_Node, CompDef_Node);
2902 Set_Expression (Decl_Node, Init_Expr_Opt);
2905 Set_Prev_Ids (Decl_Node, True);
2908 if Ident < Num_Idents then
2909 Set_More_Ids (Decl_Node, True);
2912 Append (Decl_Node, Decls);
2915 when Error_Resync =>
2916 if Token /= Tok_End then
2917 Resync_Past_Semicolon;
2921 exit Ident_Loop when Ident = Num_Idents;
2923 Restore_Scan_State (Scan_State);
2925 end loop Ident_Loop;
2928 end P_Component_Items;
2930 --------------------------------
2931 -- 3.8 Component Declaration --
2932 --------------------------------
2934 -- Parsed by P_Component_Items (3.8)
2936 -------------------------
2937 -- 3.8.1 Variant Part --
2938 -------------------------
2941 -- case discriminant_DIRECT_NAME is
2946 -- The caller has checked that the initial token is CASE
2948 -- Error recovery: cannot raise Error_Resync
2950 function P_Variant_Part return Node_Id is
2951 Variant_Part_Node : Node_Id;
2952 Variants_List : List_Id;
2953 Case_Node : Node_Id;
2956 Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
2958 Scope.Table (Scope.Last).Etyp := E_Case;
2959 Scope.Table (Scope.Last).Sloc := Token_Ptr;
2960 Scope.Table (Scope.Last).Ecol := Start_Column;
2963 Case_Node := P_Expression;
2964 Set_Name (Variant_Part_Node, Case_Node);
2966 if Nkind (Case_Node) /= N_Identifier then
2967 Set_Name (Variant_Part_Node, Error);
2968 Error_Msg ("discriminant name expected", Sloc (Case_Node));
2972 Variants_List := New_List;
2973 P_Pragmas_Opt (Variants_List);
2975 -- Test missing variant
2977 if Token = Tok_End then
2978 Error_Msg_BC ("WHEN expected (must have at least one variant)");
2980 Append (P_Variant, Variants_List);
2983 -- Loop through variants, note that we allow if in place of when,
2984 -- this error will be detected and handled in P_Variant.
2987 P_Pragmas_Opt (Variants_List);
2989 if Token /= Tok_When
2990 and then Token /= Tok_If
2991 and then Token /= Tok_Others
2993 exit when Check_End;
2996 Append (P_Variant, Variants_List);
2999 Set_Variants (Variant_Part_Node, Variants_List);
3000 return Variant_Part_Node;
3003 --------------------
3005 --------------------
3008 -- when DISCRETE_CHOICE_LIST =>
3011 -- Error recovery: cannot raise Error_Resync
3013 -- The initial token on entry is either WHEN, IF or OTHERS
3015 function P_Variant return Node_Id is
3016 Variant_Node : Node_Id;
3019 -- Special check to recover nicely from use of IF in place of WHEN
3021 if Token = Tok_If then
3028 Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3029 Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3031 Set_Component_List (Variant_Node, P_Component_List);
3032 return Variant_Node;
3035 ---------------------------------
3036 -- 3.8.1 Discrete Choice List --
3037 ---------------------------------
3039 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
3041 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
3043 -- Note: in Ada 83, the expression must be a simple expression
3045 -- Error recovery: cannot raise Error_Resync
3047 function P_Discrete_Choice_List return List_Id is
3049 Expr_Node : Node_Id;
3050 Choice_Node : Node_Id;
3053 Choices := New_List;
3056 if Token = Tok_Others then
3057 Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
3058 Scan; -- past OTHERS
3062 Expr_Node := No_Right_Paren (P_Expression_Or_Range_Attribute);
3064 if Token = Tok_Colon
3065 and then Nkind (Expr_Node) = N_Identifier
3067 Error_Msg_SP ("label not permitted in this context");
3070 elsif Expr_Form = EF_Range_Attr then
3071 Append (Expr_Node, Choices);
3073 elsif Token = Tok_Dot_Dot then
3074 Check_Simple_Expression (Expr_Node);
3075 Choice_Node := New_Node (N_Range, Token_Ptr);
3076 Set_Low_Bound (Choice_Node, Expr_Node);
3078 Expr_Node := P_Expression_No_Right_Paren;
3079 Check_Simple_Expression (Expr_Node);
3080 Set_High_Bound (Choice_Node, Expr_Node);
3081 Append (Choice_Node, Choices);
3083 elsif Expr_Form = EF_Simple_Name then
3084 if Token = Tok_Range then
3085 Append (P_Subtype_Indication (Expr_Node), Choices);
3087 elsif Token in Token_Class_Consk then
3089 ("the only constraint allowed here " &
3090 "is a range constraint");
3091 Discard_Junk_Node (P_Constraint_Opt);
3092 Append (Expr_Node, Choices);
3095 Append (Expr_Node, Choices);
3099 Check_Simple_Expression_In_Ada_83 (Expr_Node);
3100 Append (Expr_Node, Choices);
3104 when Error_Resync =>
3110 if Token = Tok_Comma then
3111 Error_Msg_SC (""","" should be ""'|""");
3113 exit when Token /= Tok_Vertical_Bar;
3116 Scan; -- past | or comma
3120 end P_Discrete_Choice_List;
3122 ----------------------------
3123 -- 3.8.1 Discrete Choice --
3124 ----------------------------
3126 -- Parsed by P_Discrete_Choice_List (3.8.1)
3128 ----------------------------------
3129 -- 3.9.1 Record Extension Part --
3130 ----------------------------------
3132 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3134 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3136 ----------------------------------
3137 -- 3.10 Access Type Definition --
3138 ----------------------------------
3140 -- ACCESS_TYPE_DEFINITION ::=
3141 -- ACCESS_TO_OBJECT_DEFINITION
3142 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3144 -- ACCESS_TO_OBJECT_DEFINITION ::=
3145 -- access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3147 -- GENERAL_ACCESS_MODIFIER ::= all | constant
3149 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3150 -- access [protected] procedure PARAMETER_PROFILE
3151 -- | access [protected] function PARAMETER_AND_RESULT_PROFILE
3153 -- PARAMETER_PROFILE ::= [FORMAL_PART]
3155 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3157 -- The caller has checked that the initial token is ACCESS
3159 -- Error recovery: can raise Error_Resync
3161 function P_Access_Type_Definition return Node_Id is
3162 Prot_Flag : Boolean;
3163 Access_Loc : Source_Ptr;
3164 Type_Def_Node : Node_Id;
3166 procedure Check_Junk_Subprogram_Name;
3167 -- Used in access to subprogram definition cases to check for an
3168 -- identifier or operator symbol that does not belong.
3170 procedure Check_Junk_Subprogram_Name is
3171 Saved_State : Saved_Scan_State;
3174 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
3175 Save_Scan_State (Saved_State);
3176 Scan; -- past possible junk subprogram name
3178 if Token = Tok_Left_Paren or else Token = Tok_Semicolon then
3179 Error_Msg_SP ("unexpected subprogram name ignored");
3183 Restore_Scan_State (Saved_State);
3186 end Check_Junk_Subprogram_Name;
3188 -- Start of processing for P_Access_Type_Definition
3191 Access_Loc := Token_Ptr;
3192 Scan; -- past ACCESS
3194 if Token_Name = Name_Protected then
3195 Check_95_Keyword (Tok_Protected, Tok_Procedure);
3196 Check_95_Keyword (Tok_Protected, Tok_Function);
3199 Prot_Flag := (Token = Tok_Protected);
3202 Scan; -- past PROTECTED
3204 if Token /= Tok_Procedure and then Token /= Tok_Function then
3205 Error_Msg_SC ("FUNCTION or PROCEDURE expected");
3209 if Token = Tok_Procedure then
3211 Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
3214 Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
3215 Scan; -- past PROCEDURE
3216 Check_Junk_Subprogram_Name;
3217 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3218 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3220 elsif Token = Tok_Function then
3222 Error_Msg_SC ("(Ada 83) access to function not allowed!");
3225 Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
3226 Scan; -- past FUNCTION
3227 Check_Junk_Subprogram_Name;
3228 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3229 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3231 Set_Subtype_Mark (Type_Def_Node, P_Subtype_Mark);
3236 New_Node (N_Access_To_Object_Definition, Access_Loc);
3238 if Token = Tok_All or else Token = Tok_Constant then
3240 Error_Msg_SC ("(Ada 83) access modifier not allowed!");
3243 if Token = Tok_All then
3244 Set_All_Present (Type_Def_Node, True);
3247 Set_Constant_Present (Type_Def_Node, True);
3250 Scan; -- past ALL or CONSTANT
3253 Set_Subtype_Indication (Type_Def_Node, P_Subtype_Indication);
3256 return Type_Def_Node;
3257 end P_Access_Type_Definition;
3259 ---------------------------------------
3260 -- 3.10 Access To Object Definition --
3261 ---------------------------------------
3263 -- Parsed by P_Access_Type_Definition (3.10)
3265 -----------------------------------
3266 -- 3.10 General Access Modifier --
3267 -----------------------------------
3269 -- Parsed by P_Access_Type_Definition (3.10)
3271 -------------------------------------------
3272 -- 3.10 Access To Subprogram Definition --
3273 -------------------------------------------
3275 -- Parsed by P_Access_Type_Definition (3.10)
3277 -----------------------------
3278 -- 3.10 Access Definition --
3279 -----------------------------
3281 -- ACCESS_DEFINITION ::= access SUBTYPE_MARK
3283 -- The caller has checked that the initial token is ACCESS
3285 -- Error recovery: cannot raise Error_Resync
3287 function P_Access_Definition return Node_Id is
3291 Def_Node := New_Node (N_Access_Definition, Token_Ptr);
3292 Scan; -- past ACCESS
3293 Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
3296 end P_Access_Definition;
3298 -----------------------------------------
3299 -- 3.10.1 Incomplete Type Declaration --
3300 -----------------------------------------
3302 -- Parsed by P_Type_Declaration (3.2.1)
3304 ----------------------------
3305 -- 3.11 Declarative Part --
3306 ----------------------------
3308 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
3310 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Items
3311 -- handles errors, and returns cleanly after an error has occurred)
3313 function P_Declarative_Part return List_Id is
3318 -- Indicate no bad declarations detected yet. This will be reset by
3319 -- P_Declarative_Items if a bad declaration is discovered.
3321 Missing_Begin_Msg := No_Error_Msg;
3323 -- Get rid of active SIS entry from outer scope. This means we will
3324 -- miss some nested cases, but it doesn't seem worth the effort. See
3325 -- discussion in Par for further details
3327 SIS_Entry_Active := False;
3330 -- Loop to scan out the declarations
3333 P_Declarative_Items (Decls, Done, In_Spec => False);
3337 -- Get rid of active SIS entry which is left set only if we scanned a
3338 -- procedure declaration and have not found the body. We could give
3339 -- an error message, but that really would be usurping the role of
3340 -- semantic analysis (this really is a missing body case).
3342 SIS_Entry_Active := False;
3344 end P_Declarative_Part;
3346 ----------------------------
3347 -- 3.11 Declarative Item --
3348 ----------------------------
3350 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
3352 -- Can return Error if a junk declaration is found, or Empty if no
3353 -- declaration is found (i.e. a token ending declarations, such as
3354 -- BEGIN or END is encountered).
3356 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
3357 -- then the scan is set past the next semicolon and Error is returned.
3359 procedure P_Declarative_Items
3364 Scan_State : Saved_Scan_State;
3367 if Style_Check then Style.Check_Indentation; end if;
3371 when Tok_Function =>
3373 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
3379 -- Check for loop (premature statement)
3381 Save_Scan_State (Scan_State);
3384 if Token = Tok_Identifier then
3385 Scan; -- past identifier
3387 if Token = Tok_In then
3388 Restore_Scan_State (Scan_State);
3389 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
3394 -- Not a loop, so must be rep clause
3396 Restore_Scan_State (Scan_State);
3397 Append (P_Representation_Clause, Decls);
3402 Append (P_Generic, Decls);
3405 when Tok_Identifier =>
3407 P_Identifier_Declarations (Decls, Done, In_Spec);
3411 Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
3415 Append (P_Pragma, Decls);
3418 when Tok_Procedure =>
3420 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
3423 when Tok_Protected =>
3425 Scan; -- past PROTECTED
3426 Append (P_Protected, Decls);
3431 Append (P_Subtype_Declaration, Decls);
3437 Append (P_Task, Decls);
3442 Append (P_Type_Declaration, Decls);
3447 Append (P_Use_Clause, Decls);
3452 Error_Msg_SC ("WITH can only appear in context clause");
3455 -- BEGIN terminates the scan of a sequence of declarations unless
3456 -- there is a missing subprogram body, see section on handling
3457 -- semicolon in place of IS. We only treat the begin as satisfying
3458 -- the subprogram declaration if it falls in the expected column
3462 if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
3464 -- Here we have the case where a BEGIN is encountered during
3465 -- declarations in a declarative part, or at the outer level,
3466 -- and there is a subprogram declaration outstanding for which
3467 -- no body has been supplied. This is the case where we assume
3468 -- that the semicolon in the subprogram declaration should
3469 -- really have been is. The active SIS entry describes the
3470 -- subprogram declaration. On return the declaration has been
3471 -- modified to become a body.
3474 Specification_Node : Node_Id;
3475 Decl_Node : Node_Id;
3476 Body_Node : Node_Id;
3479 -- First issue the error message. If we had a missing
3480 -- semicolon in the declaration, then change the message
3481 -- to <missing "is">
3483 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
3484 Change_Error_Text -- Replace: "missing "";"" "
3485 (SIS_Missing_Semicolon_Message, "missing ""is""");
3487 -- Otherwise we saved the semicolon position, so complain
3490 Error_Msg (""";"" should be IS", SIS_Semicolon_Sloc);
3493 -- The next job is to fix up any declarations that occurred
3494 -- between the procedure header and the BEGIN. These got
3495 -- chained to the outer declarative region (immediately
3496 -- after the procedure declaration) and they should be
3497 -- chained to the subprogram itself, which is a body
3498 -- rather than a spec.
3500 Specification_Node := Specification (SIS_Declaration_Node);
3501 Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
3502 Body_Node := SIS_Declaration_Node;
3503 Set_Specification (Body_Node, Specification_Node);
3504 Set_Declarations (Body_Node, New_List);
3507 Decl_Node := Remove_Next (Body_Node);
3508 exit when Decl_Node = Empty;
3509 Append (Decl_Node, Declarations (Body_Node));
3512 -- Now make the scope table entry for the Begin-End and
3516 Scope.Table (Scope.Last).Sloc := SIS_Sloc;
3517 Scope.Table (Scope.Last).Etyp := E_Name;
3518 Scope.Table (Scope.Last).Ecol := SIS_Ecol;
3519 Scope.Table (Scope.Last).Labl := SIS_Labl;
3520 Scope.Table (Scope.Last).Lreq := False;
3521 SIS_Entry_Active := False;
3523 Set_Handled_Statement_Sequence (Body_Node,
3524 P_Handled_Sequence_Of_Statements);
3525 End_Statements (Handled_Statement_Sequence (Body_Node));
3534 -- Normally an END terminates the scan for basic declarative
3535 -- items. The one exception is END RECORD, which is probably
3536 -- left over from some other junk.
3539 Save_Scan_State (Scan_State); -- at END
3542 if Token = Tok_Record then
3543 Error_Msg_SP ("no RECORD for this `end record`!");
3544 Scan; -- past RECORD
3548 Restore_Scan_State (Scan_State); -- to END
3552 -- The following tokens which can only be the start of a statement
3553 -- are considered to end a declarative part (i.e. we have a missing
3554 -- BEGIN situation). We are fairly conservative in making this
3555 -- judgment, because it is a real mess to go into statement mode
3556 -- prematurely in response to a junk declaration.
3571 -- But before we decide that it's a statement, let's check for
3572 -- a reserved word misused as an identifier.
3574 if Is_Reserved_Identifier then
3575 Save_Scan_State (Scan_State);
3576 Scan; -- past the token
3578 -- If reserved identifier not followed by colon or comma, then
3579 -- this is most likely an assignment statement to the bad id.
3581 if Token /= Tok_Colon and then Token /= Tok_Comma then
3582 Restore_Scan_State (Scan_State);
3583 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
3586 -- Otherwise we have a declaration of the bad id
3589 Restore_Scan_State (Scan_State);
3590 Scan_Reserved_Identifier (Force_Msg => True);
3591 P_Identifier_Declarations (Decls, Done, In_Spec);
3594 -- If not reserved identifier, then it's definitely a statement
3597 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
3601 -- The token RETURN may well also signal a missing BEGIN situation,
3602 -- however, we never let it end the declarative part, because it may
3603 -- also be part of a half-baked function declaration.
3606 Error_Msg_SC ("misplaced RETURN statement");
3609 -- PRIVATE definitely terminates the declarations in a spec,
3610 -- and is an error in a body.
3616 Error_Msg_SC ("PRIVATE not allowed in body");
3617 Scan; -- past PRIVATE
3620 -- An end of file definitely terminates the declarations!
3625 -- The remaining tokens do not end the scan, but cannot start a
3626 -- valid declaration, so we signal an error and resynchronize.
3627 -- But first check for misuse of a reserved identifier.
3631 -- Here we check for a reserved identifier
3633 if Is_Reserved_Identifier then
3634 Save_Scan_State (Scan_State);
3635 Scan; -- past the token
3637 if Token /= Tok_Colon and then Token /= Tok_Comma then
3638 Restore_Scan_State (Scan_State);
3639 Set_Declaration_Expected;
3642 Restore_Scan_State (Scan_State);
3643 Scan_Reserved_Identifier (Force_Msg => True);
3645 P_Identifier_Declarations (Decls, Done, In_Spec);
3649 Set_Declaration_Expected;
3654 -- To resynchronize after an error, we scan to the next semicolon and
3655 -- return with Done = False, indicating that there may still be more
3656 -- valid declarations to come.
3659 when Error_Resync =>
3660 Resync_Past_Semicolon;
3662 end P_Declarative_Items;
3664 ----------------------------------
3665 -- 3.11 Basic Declarative Item --
3666 ----------------------------------
3668 -- BASIC_DECLARATIVE_ITEM ::=
3669 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
3671 -- Scan zero or more basic declarative items
3673 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
3674 -- the scan pointer is repositioned past the next semicolon, and the scan
3675 -- for declarative items continues.
3677 function P_Basic_Declarative_Items return List_Id is
3684 -- Indicate no bad declarations detected yet in the current context:
3685 -- visible or private declarations of a package spec.
3687 Missing_Begin_Msg := No_Error_Msg;
3689 -- Get rid of active SIS entry from outer scope. This means we will
3690 -- miss some nested cases, but it doesn't seem worth the effort. See
3691 -- discussion in Par for further details
3693 SIS_Entry_Active := False;
3695 -- Loop to scan out declarations
3700 P_Declarative_Items (Decls, Done, In_Spec => True);
3704 -- Get rid of active SIS entry. This is set only if we have scanned a
3705 -- procedure declaration and have not found the body. We could give
3706 -- an error message, but that really would be usurping the role of
3707 -- semantic analysis (this really is a case of a missing body).
3709 SIS_Entry_Active := False;
3711 -- Test for assorted illegal declarations not diagnosed elsewhere.
3713 Decl := First (Decls);
3715 while Present (Decl) loop
3716 Kind := Nkind (Decl);
3718 -- Test for body scanned, not acceptable as basic decl item
3720 if Kind = N_Subprogram_Body or else
3721 Kind = N_Package_Body or else
3722 Kind = N_Task_Body or else
3723 Kind = N_Protected_Body
3726 ("proper body not allowed in package spec", Sloc (Decl));
3728 -- Test for body stub scanned, not acceptable as basic decl item
3730 elsif Kind in N_Body_Stub then
3732 ("body stub not allowed in package spec", Sloc (Decl));
3734 elsif Kind = N_Assignment_Statement then
3736 ("assignment statement not allowed in package spec",
3744 end P_Basic_Declarative_Items;
3750 -- For proper body, see below
3751 -- For body stub, see 10.1.3
3753 -----------------------
3754 -- 3.11 Proper Body --
3755 -----------------------
3757 -- Subprogram body is parsed by P_Subprogram (6.1)
3758 -- Package body is parsed by P_Package (7.1)
3759 -- Task body is parsed by P_Task (9.1)
3760 -- Protected body is parsed by P_Protected (9.4)
3762 ------------------------------
3763 -- Set_Declaration_Expected --
3764 ------------------------------
3766 procedure Set_Declaration_Expected is
3768 Error_Msg_SC ("declaration expected");
3770 if Missing_Begin_Msg = No_Error_Msg then
3771 Missing_Begin_Msg := Get_Msg_Id;
3773 end Set_Declaration_Expected;
3775 ----------------------
3776 -- Skip_Declaration --
3777 ----------------------
3779 procedure Skip_Declaration (S : List_Id) is
3780 Dummy_Done : Boolean;
3783 P_Declarative_Items (S, Dummy_Done, False);
3784 end Skip_Declaration;
3786 -----------------------------------------
3787 -- Statement_When_Declaration_Expected --
3788 -----------------------------------------
3790 procedure Statement_When_Declaration_Expected
3796 -- Case of second occurrence of statement in one declaration sequence
3798 if Missing_Begin_Msg /= No_Error_Msg then
3800 -- In the procedure spec case, just ignore it, we only give one
3801 -- message for the first occurrence, since otherwise we may get
3802 -- horrible cascading if BODY was missing in the header line.
3807 -- In the declarative part case, take a second statement as a sure
3808 -- sign that we really have a missing BEGIN, and end the declarative
3809 -- part now. Note that the caller will fix up the first message to
3810 -- say "missing BEGIN" so that's how the error will be signalled.
3817 -- Case of first occurrence of unexpected statement
3820 -- If we are in a package spec, then give message of statement
3821 -- not allowed in package spec. This message never gets changed.
3824 Error_Msg_SC ("statement not allowed in package spec");
3826 -- If in declarative part, then we give the message complaining
3827 -- about finding a statement when a declaration is expected. This
3828 -- gets changed to a complaint about a missing BEGIN if we later
3829 -- find that no BEGIN is present.
3832 Error_Msg_SC ("statement not allowed in declarative part");
3835 -- Capture message Id. This is used for two purposes, first to
3836 -- stop multiple messages, see test above, and second, to allow
3837 -- the replacement of the message in the declarative part case.
3839 Missing_Begin_Msg := Get_Msg_Id;
3842 -- In all cases except the case in which we decided to terminate the
3843 -- declaration sequence on a second error, we scan out the statement
3844 -- and append it to the list of declarations (note that the semantics
3845 -- can handle statements in a declaration list so if we proceed to
3846 -- call the semantic phase, all will be (reasonably) well!
3848 Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
3850 -- Done is set to False, since we want to continue the scan of
3851 -- declarations, hoping that this statement was a temporary glitch.
3852 -- If we indeed are now in the statement part (i.e. this was a missing
3853 -- BEGIN, then it's not terrible, we will simply keep calling this
3854 -- procedure to process the statements one by one, and then finally
3855 -- hit the missing BEGIN, which will clean up the error message.
3858 end Statement_When_Declaration_Expected;