1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003, 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 Sinfo
.CN
; use Sinfo
.CN
;
37 -----------------------
38 -- Local Subprograms --
39 -----------------------
41 function P_Component_List
return Node_Id
;
42 function P_Defining_Character_Literal
return Node_Id
;
43 function P_Delta_Constraint
return Node_Id
;
44 function P_Derived_Type_Def_Or_Private_Ext_Decl
return Node_Id
;
45 function P_Digits_Constraint
return Node_Id
;
46 function P_Discriminant_Association
return Node_Id
;
47 function P_Enumeration_Literal_Specification
return Node_Id
;
48 function P_Enumeration_Type_Definition
return Node_Id
;
49 function P_Fixed_Point_Definition
return Node_Id
;
50 function P_Floating_Point_Definition
return Node_Id
;
51 function P_Index_Or_Discriminant_Constraint
return Node_Id
;
52 function P_Real_Range_Specification_Opt
return Node_Id
;
53 function P_Subtype_Declaration
return Node_Id
;
54 function P_Type_Declaration
return Node_Id
;
55 function P_Modular_Type_Definition
return Node_Id
;
56 function P_Variant
return Node_Id
;
57 function P_Variant_Part
return Node_Id
;
59 procedure P_Declarative_Items
63 -- Scans out a single declarative item, or, in the case of a declaration
64 -- with a list of identifiers, a list of declarations, one for each of
65 -- the identifiers in the list. The declaration or declarations scanned
66 -- are appended to the given list. Done indicates whether or not there
67 -- may be additional declarative items to scan. If Done is True, then
68 -- a decision has been made that there are no more items to scan. If
69 -- Done is False, then there may be additional declarations to scan.
70 -- In_Spec is true if we are scanning a package declaration, and is used
71 -- to generate an appropriate message if a statement is encountered in
74 procedure P_Identifier_Declarations
78 -- Scans out a set of declarations for an identifier or list of
79 -- identifiers, and appends them to the given list. The parameters have
80 -- the same significance as for P_Declarative_Items.
82 procedure Statement_When_Declaration_Expected
86 -- Called when a statement is found at a point where a declaration was
87 -- expected. The parameters are as described for P_Declarative_Items.
89 procedure Set_Declaration_Expected
;
90 -- Posts a "declaration expected" error messages at the start of the
91 -- current token, and if this is the first such message issued, saves
92 -- the message id in Missing_Begin_Msg, for possible later replacement.
98 function Init_Expr_Opt
(P
: Boolean := False) return Node_Id
is
100 -- For colon, assume it means := unless it is at the end of
101 -- a line, in which case guess that it means a semicolon.
103 if Token
= Tok_Colon
then
104 if Token_Is_At_End_Of_Line
then
109 -- Here if := or something that we will take as equivalent
111 elsif Token
= Tok_Colon_Equal
112 or else Token
= Tok_Equal
113 or else Token
= Tok_Is
117 -- Another possibility. If we have a literal followed by a semicolon,
118 -- we assume that we have a missing colon-equal.
120 elsif Token
in Token_Class_Literal
then
122 Scan_State
: Saved_Scan_State
;
125 Save_Scan_State
(Scan_State
);
126 Scan
; -- past literal or identifier
128 if Token
= Tok_Semicolon
then
129 Restore_Scan_State
(Scan_State
);
131 Restore_Scan_State
(Scan_State
);
136 -- Otherwise we definitely have no initialization expression
142 -- Merge here if we have an initialization expression
149 return P_Expression_No_Right_Paren
;
153 ----------------------------
154 -- 3.1 Basic Declaration --
155 ----------------------------
157 -- Parsed by P_Basic_Declarative_Items (3.9)
159 ------------------------------
160 -- 3.1 Defining Identifier --
161 ------------------------------
163 -- DEFINING_IDENTIFIER ::= IDENTIFIER
165 -- Error recovery: can raise Error_Resync
167 function P_Defining_Identifier
(C
: Id_Check
:= None
) return Node_Id
is
168 Ident_Node
: Node_Id
;
171 -- Scan out the identifier. Note that this code is essentially identical
172 -- to P_Identifier, except that in the call to Scan_Reserved_Identifier
173 -- we set Force_Msg to True, since we want at least one message for each
174 -- separate declaration (but not use) of a reserved identifier.
176 if Token
= Tok_Identifier
then
179 -- If we have a reserved identifier, manufacture an identifier with
180 -- a corresponding name after posting an appropriate error message
182 elsif Is_Reserved_Identifier
(C
) then
183 Scan_Reserved_Identifier
(Force_Msg
=> True);
185 -- Otherwise we have junk that cannot be interpreted as an identifier
188 T_Identifier
; -- to give message
192 Ident_Node
:= Token_Node
;
193 Scan
; -- past the reserved identifier
195 if Ident_Node
/= Error
then
196 Change_Identifier_To_Defining_Identifier
(Ident_Node
);
200 end P_Defining_Identifier
;
202 -----------------------------
203 -- 3.2.1 Type Declaration --
204 -----------------------------
206 -- TYPE_DECLARATION ::=
207 -- FULL_TYPE_DECLARATION
208 -- | INCOMPLETE_TYPE_DECLARATION
209 -- | PRIVATE_TYPE_DECLARATION
210 -- | PRIVATE_EXTENSION_DECLARATION
212 -- FULL_TYPE_DECLARATION ::=
213 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION;
214 -- | CONCURRENT_TYPE_DECLARATION
216 -- INCOMPLETE_TYPE_DECLARATION ::=
217 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART];
219 -- PRIVATE_TYPE_DECLARATION ::=
220 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
221 -- is [abstract] [tagged] [limited] private;
223 -- PRIVATE_EXTENSION_DECLARATION ::=
224 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
225 -- [abstract] new ancestor_SUBTYPE_INDICATION with private;
227 -- TYPE_DEFINITION ::=
228 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
229 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
230 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
231 -- | DERIVED_TYPE_DEFINITION
233 -- INTEGER_TYPE_DEFINITION ::=
234 -- SIGNED_INTEGER_TYPE_DEFINITION
235 -- MODULAR_TYPE_DEFINITION
237 -- Error recovery: can raise Error_Resync
239 -- Note: The processing for full type declaration, incomplete type
240 -- declaration, private type declaration and type definition is
241 -- included in this function. The processing for concurrent type
242 -- declarations is NOT here, but rather in chapter 9 (i.e. this
243 -- function handles only declarations starting with TYPE).
245 function P_Type_Declaration
return Node_Id
is
246 Type_Loc
: Source_Ptr
;
247 Type_Start_Col
: Column_Number
;
248 Ident_Node
: Node_Id
;
250 Discr_List
: List_Id
;
251 Unknown_Dis
: Boolean;
252 Discr_Sloc
: Source_Ptr
;
253 Abstract_Present
: Boolean;
254 Abstract_Loc
: Source_Ptr
;
257 Typedef_Node
: Node_Id
;
258 -- Normally holds type definition, except in the case of a private
259 -- extension declaration, in which case it holds the declaration itself
262 Type_Loc
:= Token_Ptr
;
263 Type_Start_Col
:= Start_Column
;
265 Ident_Node
:= P_Defining_Identifier
(C_Is
);
266 Discr_Sloc
:= Token_Ptr
;
268 if P_Unknown_Discriminant_Part_Opt
then
270 Discr_List
:= No_List
;
272 Unknown_Dis
:= False;
273 Discr_List
:= P_Known_Discriminant_Part_Opt
;
276 -- Incomplete type declaration. We complete the processing for this
277 -- case here and return the resulting incomplete type declaration node
279 if Token
= Tok_Semicolon
then
281 Decl_Node
:= New_Node
(N_Incomplete_Type_Declaration
, Type_Loc
);
282 Set_Defining_Identifier
(Decl_Node
, Ident_Node
);
283 Set_Unknown_Discriminants_Present
(Decl_Node
, Unknown_Dis
);
284 Set_Discriminant_Specifications
(Decl_Node
, Discr_List
);
291 -- Full type declaration or private type declaration, must have IS
293 if Token
= Tok_Equal
then
295 Scan
; -- past = used in place of IS
297 elsif Token
= Tok_Renames
then
298 Error_Msg_SC
("RENAMES should be IS");
299 Scan
; -- past RENAMES used in place of IS
305 -- First an error check, if we have two identifiers in a row, a likely
306 -- possibility is that the first of the identifiers is an incorrectly
309 if Token
= Tok_Identifier
then
311 SS
: Saved_Scan_State
;
315 Save_Scan_State
(SS
);
316 Scan
; -- past initial identifier
317 I2
:= (Token
= Tok_Identifier
);
318 Restore_Scan_State
(SS
);
322 (Bad_Spelling_Of
(Tok_Abstract
) or else
323 Bad_Spelling_Of
(Tok_Access
) or else
324 Bad_Spelling_Of
(Tok_Aliased
) or else
325 Bad_Spelling_Of
(Tok_Constant
))
332 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
334 if Token_Name
= Name_Abstract
then
335 Check_95_Keyword
(Tok_Abstract
, Tok_Tagged
);
336 Check_95_Keyword
(Tok_Abstract
, Tok_New
);
339 -- Check cases of misuse of ABSTRACT
341 if Token
= Tok_Abstract
then
342 Abstract_Present
:= True;
343 Abstract_Loc
:= Token_Ptr
;
344 Scan
; -- past ABSTRACT
346 if Token
= Tok_Limited
347 or else Token
= Tok_Private
348 or else Token
= Tok_Record
349 or else Token
= Tok_Null
351 Error_Msg_AP
("TAGGED expected");
355 Abstract_Present
:= False;
356 Abstract_Loc
:= No_Location
;
359 -- Check for misuse of Ada 95 keyword Tagged
361 if Token_Name
= Name_Tagged
then
362 Check_95_Keyword
(Tok_Tagged
, Tok_Private
);
363 Check_95_Keyword
(Tok_Tagged
, Tok_Limited
);
364 Check_95_Keyword
(Tok_Tagged
, Tok_Record
);
367 -- Special check for misuse of Aliased
369 if Token
= Tok_Aliased
or else Token_Name
= Name_Aliased
then
370 Error_Msg_SC
("ALIASED not allowed in type definition");
371 Scan
; -- past ALIASED
374 -- The following procesing deals with either a private type declaration
375 -- or a full type declaration. In the private type case, we build the
376 -- N_Private_Type_Declaration node, setting its Tagged_Present and
377 -- Limited_Present flags, on encountering the Private keyword, and
378 -- leave Typedef_Node set to Empty. For the full type declaration
379 -- case, Typedef_Node gets set to the type definition.
381 Typedef_Node
:= Empty
;
383 -- Switch on token following the IS. The loop normally runs once. It
384 -- only runs more than once if an error is detected, to try again after
385 -- detecting and fixing up the error.
391 Typedef_Node
:= P_Access_Type_Definition
;
396 Typedef_Node
:= P_Array_Type_Definition
;
401 Typedef_Node
:= P_Fixed_Point_Definition
;
406 Typedef_Node
:= P_Floating_Point_Definition
;
413 when Tok_Integer_Literal
=>
415 Typedef_Node
:= P_Signed_Integer_Type_Definition
;
420 Typedef_Node
:= P_Record_Definition
;
424 when Tok_Left_Paren
=>
425 Typedef_Node
:= P_Enumeration_Type_Definition
;
428 Make_Identifier
(Token_Ptr
,
429 Chars
=> Chars
(Ident_Node
));
430 Set_Comes_From_Source
(End_Labl
, False);
432 Set_End_Label
(Typedef_Node
, End_Labl
);
437 Typedef_Node
:= P_Modular_Type_Definition
;
442 Typedef_Node
:= P_Derived_Type_Def_Or_Private_Ext_Decl
;
444 if Nkind
(Typedef_Node
) = N_Derived_Type_Definition
445 and then Present
(Record_Extension_Part
(Typedef_Node
))
448 Make_Identifier
(Token_Ptr
,
449 Chars
=> Chars
(Ident_Node
));
450 Set_Comes_From_Source
(End_Labl
, False);
453 (Record_Extension_Part
(Typedef_Node
), End_Labl
);
460 Typedef_Node
:= P_Signed_Integer_Type_Definition
;
465 Typedef_Node
:= P_Record_Definition
;
468 Make_Identifier
(Token_Ptr
,
469 Chars
=> Chars
(Ident_Node
));
470 Set_Comes_From_Source
(End_Labl
, False);
472 Set_End_Label
(Typedef_Node
, End_Labl
);
479 if Token
= Tok_Abstract
then
480 Error_Msg_SC
("ABSTRACT must come before TAGGED");
481 Abstract_Present
:= True;
482 Abstract_Loc
:= Token_Ptr
;
483 Scan
; -- past ABSTRACT
486 if Token
= Tok_Limited
then
487 Scan
; -- past LIMITED
489 -- TAGGED LIMITED PRIVATE case
491 if Token
= Tok_Private
then
493 New_Node
(N_Private_Type_Declaration
, Type_Loc
);
494 Set_Tagged_Present
(Decl_Node
, True);
495 Set_Limited_Present
(Decl_Node
, True);
496 Scan
; -- past PRIVATE
498 -- TAGGED LIMITED RECORD
501 Typedef_Node
:= P_Record_Definition
;
502 Set_Tagged_Present
(Typedef_Node
, True);
503 Set_Limited_Present
(Typedef_Node
, True);
506 Make_Identifier
(Token_Ptr
,
507 Chars
=> Chars
(Ident_Node
));
508 Set_Comes_From_Source
(End_Labl
, False);
510 Set_End_Label
(Typedef_Node
, End_Labl
);
516 if Token
= Tok_Private
then
518 New_Node
(N_Private_Type_Declaration
, Type_Loc
);
519 Set_Tagged_Present
(Decl_Node
, True);
520 Scan
; -- past PRIVATE
525 Typedef_Node
:= P_Record_Definition
;
526 Set_Tagged_Present
(Typedef_Node
, True);
529 Make_Identifier
(Token_Ptr
,
530 Chars
=> Chars
(Ident_Node
));
531 Set_Comes_From_Source
(End_Labl
, False);
533 Set_End_Label
(Typedef_Node
, End_Labl
);
541 Decl_Node
:= New_Node
(N_Private_Type_Declaration
, Type_Loc
);
542 Scan
; -- past PRIVATE
547 Scan
; -- past LIMITED
550 if Token
= Tok_Tagged
then
551 Error_Msg_SC
("TAGGED must come before LIMITED");
554 elsif Token
= Tok_Abstract
then
555 Error_Msg_SC
("ABSTRACT must come before LIMITED");
556 Scan
; -- past ABSTRACT
563 -- LIMITED RECORD or LIMITED NULL RECORD
565 if Token
= Tok_Record
or else Token
= Tok_Null
then
568 ("(Ada 83) limited record declaration not allowed!");
571 Typedef_Node
:= P_Record_Definition
;
572 Set_Limited_Present
(Typedef_Node
, True);
574 -- LIMITED PRIVATE is the only remaining possibility here
577 Decl_Node
:= New_Node
(N_Private_Type_Declaration
, Type_Loc
);
578 Set_Limited_Present
(Decl_Node
, True);
579 T_Private
; -- past PRIVATE (or complain if not there!)
585 -- Here we have an identifier after the IS, which is certainly
586 -- wrong and which might be one of several different mistakes.
588 when Tok_Identifier
=>
590 -- First case, if identifier is on same line, then probably we
591 -- have something like "type X is Integer .." and the best
592 -- diagnosis is a missing NEW. Note: the missing new message
593 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
595 if not Token_Is_At_Start_Of_Line
then
596 Typedef_Node
:= P_Derived_Type_Def_Or_Private_Ext_Decl
;
599 -- If the identifier is at the start of the line, and is in the
600 -- same column as the type declaration itself then we consider
601 -- that we had a missing type definition on the previous line
603 elsif Start_Column
<= Type_Start_Col
then
604 Error_Msg_AP
("type definition expected");
605 Typedef_Node
:= Error
;
607 -- If the identifier is at the start of the line, and is in
608 -- a column to the right of the type declaration line, then we
609 -- may have something like:
614 -- and the best diagnosis is a missing record keyword
617 Typedef_Node
:= P_Record_Definition
;
623 -- Anything else is an error
626 if Bad_Spelling_Of
(Tok_Access
)
628 Bad_Spelling_Of
(Tok_Array
)
630 Bad_Spelling_Of
(Tok_Delta
)
632 Bad_Spelling_Of
(Tok_Digits
)
634 Bad_Spelling_Of
(Tok_Limited
)
636 Bad_Spelling_Of
(Tok_Private
)
638 Bad_Spelling_Of
(Tok_Range
)
640 Bad_Spelling_Of
(Tok_Record
)
642 Bad_Spelling_Of
(Tok_Tagged
)
647 Error_Msg_AP
("type definition expected");
654 -- For the private type declaration case, the private type declaration
655 -- node has been built, with the Tagged_Present and Limited_Present
656 -- flags set as needed, and Typedef_Node is left set to Empty.
658 if No
(Typedef_Node
) then
659 Set_Unknown_Discriminants_Present
(Decl_Node
, Unknown_Dis
);
660 Set_Abstract_Present
(Decl_Node
, Abstract_Present
);
662 -- For a private extension declaration, Typedef_Node contains the
663 -- N_Private_Extension_Declaration node, which we now complete. Note
664 -- that the private extension declaration, unlike a full type
665 -- declaration, does permit unknown discriminants.
667 elsif Nkind
(Typedef_Node
) = N_Private_Extension_Declaration
then
668 Decl_Node
:= Typedef_Node
;
669 Set_Sloc
(Decl_Node
, Type_Loc
);
670 Set_Unknown_Discriminants_Present
(Decl_Node
, Unknown_Dis
);
671 Set_Abstract_Present
(Typedef_Node
, Abstract_Present
);
673 -- In the full type declaration case, Typedef_Node has the type
674 -- definition and here is where we build the full type declaration
675 -- node. This is also where we check for improper use of an unknown
676 -- discriminant part (not allowed for full type declaration).
679 if Nkind
(Typedef_Node
) = N_Record_Definition
680 or else (Nkind
(Typedef_Node
) = N_Derived_Type_Definition
681 and then Present
(Record_Extension_Part
(Typedef_Node
)))
683 Set_Abstract_Present
(Typedef_Node
, Abstract_Present
);
685 elsif Abstract_Present
then
686 Error_Msg
("ABSTRACT not allowed here, ignored", Abstract_Loc
);
689 Decl_Node
:= New_Node
(N_Full_Type_Declaration
, Type_Loc
);
690 Set_Type_Definition
(Decl_Node
, Typedef_Node
);
694 ("Full type declaration cannot have unknown discriminants",
699 -- Remaining processing is common for all three cases
701 Set_Defining_Identifier
(Decl_Node
, Ident_Node
);
702 Set_Discriminant_Specifications
(Decl_Node
, Discr_List
);
704 end P_Type_Declaration
;
706 ----------------------------------
707 -- 3.2.1 Full Type Declaration --
708 ----------------------------------
710 -- Parsed by P_Type_Declaration (3.2.1)
712 ----------------------------
713 -- 3.2.1 Type Definition --
714 ----------------------------
716 -- Parsed by P_Type_Declaration (3.2.1)
718 --------------------------------
719 -- 3.2.2 Subtype Declaration --
720 --------------------------------
722 -- SUBTYPE_DECLARATION ::=
723 -- subtype DEFINING_IDENTIFIER is SUBTYPE_INDICATION;
725 -- The caller has checked that the initial token is SUBTYPE
727 -- Error recovery: can raise Error_Resync
729 function P_Subtype_Declaration
return Node_Id
is
733 Decl_Node
:= New_Node
(N_Subtype_Declaration
, Token_Ptr
);
734 Scan
; -- past SUBTYPE
735 Set_Defining_Identifier
(Decl_Node
, P_Defining_Identifier
(C_Is
));
738 if Token
= Tok_New
then
739 Error_Msg_SC
("NEW ignored (only allowed in type declaration)");
743 Set_Subtype_Indication
(Decl_Node
, P_Subtype_Indication
);
746 end P_Subtype_Declaration
;
748 -------------------------------
749 -- 3.2.2 Subtype Indication --
750 -------------------------------
752 -- SUBTYPE_INDICATION ::= SUBTYPE_MARK [CONSTRAINT]
754 -- Error recovery: can raise Error_Resync
756 function P_Subtype_Indication
return Node_Id
is
760 if Token
= Tok_Identifier
or else Token
= Tok_Operator_Symbol
then
761 Type_Node
:= P_Subtype_Mark
;
762 return P_Subtype_Indication
(Type_Node
);
765 -- Check for error of using record definition and treat it nicely,
766 -- otherwise things are really messed up, so resynchronize.
768 if Token
= Tok_Record
then
769 Error_Msg_SC
("anonymous record definitions are not permitted");
770 Discard_Junk_Node
(P_Record_Definition
);
774 Error_Msg_AP
("subtype indication expected");
778 end P_Subtype_Indication
;
780 -- The following function is identical except that it is called with
781 -- the subtype mark already scanned out, and it scans out the constraint
783 -- Error recovery: can raise Error_Resync
785 function P_Subtype_Indication
(Subtype_Mark
: Node_Id
) return Node_Id
is
786 Indic_Node
: Node_Id
;
787 Constr_Node
: Node_Id
;
790 Constr_Node
:= P_Constraint_Opt
;
792 if No
(Constr_Node
) then
795 Indic_Node
:= New_Node
(N_Subtype_Indication
, Sloc
(Subtype_Mark
));
796 Set_Subtype_Mark
(Indic_Node
, Check_Subtype_Mark
(Subtype_Mark
));
797 Set_Constraint
(Indic_Node
, Constr_Node
);
800 end P_Subtype_Indication
;
802 -------------------------
803 -- 3.2.2 Subtype Mark --
804 -------------------------
806 -- SUBTYPE_MARK ::= subtype_NAME;
808 -- Note: The subtype mark which appears after an IN or NOT IN
809 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
811 -- Error recovery: cannot raise Error_Resync
813 function P_Subtype_Mark
return Node_Id
is
815 return P_Subtype_Mark_Resync
;
822 -- This routine differs from P_Subtype_Mark in that it insists that an
823 -- identifier be present, and if it is not, it raises Error_Resync.
825 -- Error recovery: can raise Error_Resync
827 function P_Subtype_Mark_Resync
return Node_Id
is
831 if Token
= Tok_Access
then
832 Error_Msg_SC
("anonymous access type definition not allowed here");
836 if Token
= Tok_Array
then
837 Error_Msg_SC
("anonymous array definition not allowed here");
838 Discard_Junk_Node
(P_Array_Type_Definition
);
842 Type_Node
:= P_Qualified_Simple_Name_Resync
;
844 -- Check for a subtype mark attribute. The only valid possibilities
845 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
846 -- as well catch it here.
848 if Token
= Tok_Apostrophe
then
849 return P_Subtype_Mark_Attribute
(Type_Node
);
854 end P_Subtype_Mark_Resync
;
856 -- The following function is called to scan out a subtype mark attribute.
857 -- The caller has already scanned out the subtype mark, which is passed in
858 -- as the argument, and has checked that the current token is apostrophe.
860 -- Only a special subclass of attributes, called type attributes
861 -- (see Snames package) are allowed in this syntactic position.
863 -- Note: if the apostrophe is followed by other than an identifier, then
864 -- the input expression is returned unchanged, and the scan pointer is
865 -- left pointing to the apostrophe.
867 -- Error recovery: can raise Error_Resync
869 function P_Subtype_Mark_Attribute
(Type_Node
: Node_Id
) return Node_Id
is
870 Attr_Node
: Node_Id
:= Empty
;
871 Scan_State
: Saved_Scan_State
;
875 Prefix
:= Check_Subtype_Mark
(Type_Node
);
877 if Prefix
= Error
then
881 -- Loop through attributes appearing (more than one can appear as for
882 -- for example in X'Base'Class). We are at an apostrophe on entry to
883 -- this loop, and it runs once for each attribute parsed, with
884 -- Prefix being the current possible prefix if it is an attribute.
887 Save_Scan_State
(Scan_State
); -- at Apostrophe
888 Scan
; -- past apostrophe
890 if Token
/= Tok_Identifier
then
891 Restore_Scan_State
(Scan_State
); -- to apostrophe
892 return Prefix
; -- no attribute after all
894 elsif not Is_Type_Attribute_Name
(Token_Name
) then
896 ("attribute & may not be used in a subtype mark", Token_Node
);
901 Make_Attribute_Reference
(Prev_Token_Ptr
,
903 Attribute_Name
=> Token_Name
);
904 Delete_Node
(Token_Node
);
905 Scan
; -- past type attribute identifier
908 exit when Token
/= Tok_Apostrophe
;
912 -- Fall through here after scanning type attribute
915 end P_Subtype_Mark_Attribute
;
917 -----------------------
918 -- 3.2.2 Constraint --
919 -----------------------
921 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
923 -- SCALAR_CONSTRAINT ::=
924 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
926 -- COMPOSITE_CONSTRAINT ::=
927 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
929 -- If no constraint is present, this function returns Empty
931 -- Error recovery: can raise Error_Resync
933 function P_Constraint_Opt
return Node_Id
is
936 or else Bad_Spelling_Of
(Tok_Range
)
938 return P_Range_Constraint
;
940 elsif Token
= Tok_Digits
941 or else Bad_Spelling_Of
(Tok_Digits
)
943 return P_Digits_Constraint
;
945 elsif Token
= Tok_Delta
946 or else Bad_Spelling_Of
(Tok_Delta
)
948 return P_Delta_Constraint
;
950 elsif Token
= Tok_Left_Paren
then
951 return P_Index_Or_Discriminant_Constraint
;
953 elsif Token
= Tok_In
then
955 return P_Constraint_Opt
;
960 end P_Constraint_Opt
;
962 ------------------------------
963 -- 3.2.2 Scalar Constraint --
964 ------------------------------
966 -- Parsed by P_Constraint_Opt (3.2.2)
968 ---------------------------------
969 -- 3.2.2 Composite Constraint --
970 ---------------------------------
972 -- Parsed by P_Constraint_Opt (3.2.2)
974 --------------------------------------------------------
975 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
976 --------------------------------------------------------
978 -- This routine scans out a declaration starting with an identifier:
980 -- OBJECT_DECLARATION ::=
981 -- DEFINING_IDENTIFIER_LIST : [constant] [aliased]
982 -- SUBTYPE_INDICATION [:= EXPRESSION];
983 -- | DEFINING_IDENTIFIER_LIST : [constant] [aliased]
984 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
986 -- NUMBER_DECLARATION ::=
987 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
989 -- OBJECT_RENAMING_DECLARATION ::=
990 -- DEFINING_IDENTIFIER : SUBTYPE_MARK renames object_NAME;
992 -- EXCEPTION_RENAMING_DECLARATION ::=
993 -- DEFINING_IDENTIFIER : exception renames exception_NAME;
995 -- EXCEPTION_DECLARATION ::=
996 -- DEFINING_IDENTIFIER_LIST : exception;
998 -- Note that the ALIASED indication in an object declaration is
999 -- marked by a flag in the parent node.
1001 -- The caller has checked that the initial token is an identifier
1003 -- The value returned is a list of declarations, one for each identifier
1004 -- in the list (as described in Sinfo, we always split up multiple
1005 -- declarations into the equivalent sequence of single declarations
1006 -- using the More_Ids and Prev_Ids flags to preserve the source).
1008 -- If the identifier turns out to be a probable statement rather than
1009 -- an identifier, then the scan is left pointing to the identifier and
1010 -- No_List is returned.
1012 -- Error recovery: can raise Error_Resync
1014 procedure P_Identifier_Declarations
1019 Decl_Node
: Node_Id
;
1020 Type_Node
: Node_Id
;
1021 Ident_Sloc
: Source_Ptr
;
1022 Scan_State
: Saved_Scan_State
;
1023 List_OK
: Boolean := True;
1025 Init_Expr
: Node_Id
;
1026 Init_Loc
: Source_Ptr
;
1027 Con_Loc
: Source_Ptr
;
1029 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
1030 -- Used to save identifiers in the identifier list. The upper bound
1031 -- of 4096 is expected to be infinite in practice, and we do not even
1032 -- bother to check if this upper bound is exceeded.
1034 Num_Idents
: Nat
:= 1;
1035 -- Number of identifiers stored in Idents
1038 -- This procedure is called in renames cases to make sure that we do
1039 -- not have more than one identifier. If we do have more than one
1040 -- then an error message is issued (and the declaration is split into
1041 -- multiple declarations)
1043 function Token_Is_Renames
return Boolean;
1044 -- Checks if current token is RENAMES, and if so, scans past it and
1045 -- returns True, otherwise returns False. Includes checking for some
1046 -- common error cases.
1048 procedure No_List
is
1050 if Num_Idents
> 1 then
1051 Error_Msg
("identifier list not allowed for RENAMES",
1058 function Token_Is_Renames
return Boolean is
1059 At_Colon
: Saved_Scan_State
;
1062 if Token
= Tok_Colon
then
1063 Save_Scan_State
(At_Colon
);
1065 Check_Misspelling_Of
(Tok_Renames
);
1067 if Token
= Tok_Renames
then
1068 Error_Msg_SP
("extra "":"" ignored");
1069 Scan
; -- past RENAMES
1072 Restore_Scan_State
(At_Colon
);
1077 Check_Misspelling_Of
(Tok_Renames
);
1079 if Token
= Tok_Renames
then
1080 Scan
; -- past RENAMES
1086 end Token_Is_Renames
;
1088 -- Start of processing for P_Identifier_Declarations
1091 Ident_Sloc
:= Token_Ptr
;
1092 Save_Scan_State
(Scan_State
); -- at first identifier
1093 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
1095 -- If we have a colon after the identifier, then we can assume that
1096 -- this is in fact a valid identifier declaration and can steam ahead.
1098 if Token
= Tok_Colon
then
1101 -- If we have a comma, then scan out the list of identifiers
1103 elsif Token
= Tok_Comma
then
1105 while Comma_Present
loop
1106 Num_Idents
:= Num_Idents
+ 1;
1107 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
1110 Save_Scan_State
(Scan_State
); -- at colon
1113 -- If we have identifier followed by := then we assume that what is
1114 -- really meant is an assignment statement. The assignment statement
1115 -- is scanned out and added to the list of declarations. An exception
1116 -- occurs if the := is followed by the keyword constant, in which case
1117 -- we assume it was meant to be a colon.
1119 elsif Token
= Tok_Colon_Equal
then
1122 if Token
= Tok_Constant
then
1123 Error_Msg_SP
("colon expected");
1126 Restore_Scan_State
(Scan_State
);
1127 Statement_When_Declaration_Expected
(Decls
, Done
, In_Spec
);
1131 -- If we have an IS keyword, then assume the TYPE keyword was missing
1133 elsif Token
= Tok_Is
then
1134 Restore_Scan_State
(Scan_State
);
1135 Append_To
(Decls
, P_Type_Declaration
);
1139 -- Otherwise we have an error situation
1142 Restore_Scan_State
(Scan_State
);
1144 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1145 -- so, fix the keyword and return to scan the protected declaration.
1147 if Token_Name
= Name_Protected
then
1148 Check_95_Keyword
(Tok_Protected
, Tok_Identifier
);
1149 Check_95_Keyword
(Tok_Protected
, Tok_Type
);
1150 Check_95_Keyword
(Tok_Protected
, Tok_Body
);
1152 if Token
= Tok_Protected
then
1157 -- Check misspelling possibilities. If so, correct the misspelling
1158 -- and return to scan out the resulting declaration.
1160 elsif Bad_Spelling_Of
(Tok_Function
)
1161 or else Bad_Spelling_Of
(Tok_Procedure
)
1162 or else Bad_Spelling_Of
(Tok_Package
)
1163 or else Bad_Spelling_Of
(Tok_Pragma
)
1164 or else Bad_Spelling_Of
(Tok_Protected
)
1165 or else Bad_Spelling_Of
(Tok_Generic
)
1166 or else Bad_Spelling_Of
(Tok_Subtype
)
1167 or else Bad_Spelling_Of
(Tok_Type
)
1168 or else Bad_Spelling_Of
(Tok_Task
)
1169 or else Bad_Spelling_Of
(Tok_Use
)
1170 or else Bad_Spelling_Of
(Tok_For
)
1175 -- Otherwise we definitely have an ordinary identifier with a junk
1176 -- token after it. Just complain that we expect a declaration, and
1177 -- skip to a semicolon
1180 Set_Declaration_Expected
;
1181 Resync_Past_Semicolon
;
1187 -- Come here with an identifier list and colon scanned out. We now
1188 -- build the nodes for the declarative items. One node is built for
1189 -- each identifier in the list, with the type information being
1190 -- repeated by rescanning the appropriate section of source.
1192 -- First an error check, if we have two identifiers in a row, a likely
1193 -- possibility is that the first of the identifiers is an incorrectly
1196 if Token
= Tok_Identifier
then
1198 SS
: Saved_Scan_State
;
1202 Save_Scan_State
(SS
);
1203 Scan
; -- past initial identifier
1204 I2
:= (Token
= Tok_Identifier
);
1205 Restore_Scan_State
(SS
);
1209 (Bad_Spelling_Of
(Tok_Access
) or else
1210 Bad_Spelling_Of
(Tok_Aliased
) or else
1211 Bad_Spelling_Of
(Tok_Constant
))
1218 -- Loop through identifiers
1223 -- Check for some cases of misused Ada 95 keywords
1225 if Token_Name
= Name_Aliased
then
1226 Check_95_Keyword
(Tok_Aliased
, Tok_Array
);
1227 Check_95_Keyword
(Tok_Aliased
, Tok_Identifier
);
1228 Check_95_Keyword
(Tok_Aliased
, Tok_Constant
);
1233 if Token
= Tok_Constant
then
1234 Con_Loc
:= Token_Ptr
;
1235 Scan
; -- past CONSTANT
1237 -- Number declaration, initialization required
1239 Init_Expr
:= Init_Expr_Opt
;
1241 if Present
(Init_Expr
) then
1242 Decl_Node
:= New_Node
(N_Number_Declaration
, Ident_Sloc
);
1243 Set_Expression
(Decl_Node
, Init_Expr
);
1245 -- Constant object declaration
1248 Decl_Node
:= New_Node
(N_Object_Declaration
, Ident_Sloc
);
1249 Set_Constant_Present
(Decl_Node
, True);
1251 if Token_Name
= Name_Aliased
then
1252 Check_95_Keyword
(Tok_Aliased
, Tok_Array
);
1253 Check_95_Keyword
(Tok_Aliased
, Tok_Identifier
);
1256 if Token
= Tok_Aliased
then
1257 Error_Msg_SC
("ALIASED should be before CONSTANT");
1258 Scan
; -- past ALIASED
1259 Set_Aliased_Present
(Decl_Node
, True);
1262 if Token
= Tok_Array
then
1263 Set_Object_Definition
1264 (Decl_Node
, P_Array_Type_Definition
);
1266 Set_Object_Definition
(Decl_Node
, P_Subtype_Indication
);
1269 if Token
= Tok_Renames
then
1271 ("CONSTANT not permitted in renaming declaration",
1273 Scan
; -- Past renames
1274 Discard_Junk_Node
(P_Name
);
1280 elsif Token
= Tok_Exception
then
1281 Scan
; -- past EXCEPTION
1283 if Token_Is_Renames
then
1286 New_Node
(N_Exception_Renaming_Declaration
, Ident_Sloc
);
1287 Set_Name
(Decl_Node
, P_Qualified_Simple_Name_Resync
);
1290 Decl_Node
:= New_Node
(N_Exception_Declaration
, Prev_Token_Ptr
);
1293 -- Aliased case (note that an object definition is required)
1295 elsif Token
= Tok_Aliased
then
1296 Scan
; -- past ALIASED
1297 Decl_Node
:= New_Node
(N_Object_Declaration
, Ident_Sloc
);
1298 Set_Aliased_Present
(Decl_Node
, True);
1300 if Token
= Tok_Constant
then
1301 Scan
; -- past CONSTANT
1302 Set_Constant_Present
(Decl_Node
, True);
1305 if Token
= Tok_Array
then
1306 Set_Object_Definition
1307 (Decl_Node
, P_Array_Type_Definition
);
1309 Set_Object_Definition
(Decl_Node
, P_Subtype_Indication
);
1314 elsif Token
= Tok_Array
then
1315 Decl_Node
:= New_Node
(N_Object_Declaration
, Ident_Sloc
);
1316 Set_Object_Definition
(Decl_Node
, P_Array_Type_Definition
);
1318 -- Subtype indication case
1321 Type_Node
:= P_Subtype_Mark
;
1323 -- Object renaming declaration
1325 if Token_Is_Renames
then
1328 New_Node
(N_Object_Renaming_Declaration
, Ident_Sloc
);
1329 Set_Subtype_Mark
(Decl_Node
, Type_Node
);
1330 Set_Name
(Decl_Node
, P_Name
);
1332 -- Object declaration
1335 Decl_Node
:= New_Node
(N_Object_Declaration
, Ident_Sloc
);
1336 Set_Object_Definition
1337 (Decl_Node
, P_Subtype_Indication
(Type_Node
));
1339 -- RENAMES at this point means that we had the combination of
1340 -- a constraint on the Type_Node and renames, which is illegal
1342 if Token_Is_Renames
then
1344 ("constraint not allowed in object renaming declaration",
1345 Constraint
(Object_Definition
(Decl_Node
)));
1351 -- Scan out initialization, allowed only for object declaration
1353 Init_Loc
:= Token_Ptr
;
1354 Init_Expr
:= Init_Expr_Opt
;
1356 if Present
(Init_Expr
) then
1357 if Nkind
(Decl_Node
) = N_Object_Declaration
then
1358 Set_Expression
(Decl_Node
, Init_Expr
);
1360 Error_Msg
("initialization not allowed here", Init_Loc
);
1365 Set_Defining_Identifier
(Decl_Node
, Idents
(Ident
));
1368 if Ident
< Num_Idents
then
1369 Set_More_Ids
(Decl_Node
, True);
1373 Set_Prev_Ids
(Decl_Node
, True);
1377 Append
(Decl_Node
, Decls
);
1378 exit Ident_Loop
when Ident
= Num_Idents
;
1379 Restore_Scan_State
(Scan_State
);
1382 end loop Ident_Loop
;
1385 end P_Identifier_Declarations
;
1387 -------------------------------
1388 -- 3.3.1 Object Declaration --
1389 -------------------------------
1391 -- OBJECT DECLARATION ::=
1392 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1393 -- SUBTYPE_INDICATION [:= EXPRESSION];
1394 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1395 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1396 -- | SINGLE_TASK_DECLARATION
1397 -- | SINGLE_PROTECTED_DECLARATION
1399 -- Cases starting with TASK are parsed by P_Task (9.1)
1400 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1401 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1403 -------------------------------------
1404 -- 3.3.1 Defining Identifier List --
1405 -------------------------------------
1407 -- DEFINING_IDENTIFIER_LIST ::=
1408 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1410 -- Always parsed by the construct in which it appears. See special
1411 -- section on "Handling of Defining Identifier Lists" in this unit.
1413 -------------------------------
1414 -- 3.3.2 Number Declaration --
1415 -------------------------------
1417 -- Parsed by P_Identifier_Declarations (3.3)
1419 -------------------------------------------------------------------------
1420 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
1421 -------------------------------------------------------------------------
1423 -- DERIVED_TYPE_DEFINITION ::=
1424 -- [abstract] new parent_SUBTYPE_INDICATION [RECORD_EXTENSION_PART]
1426 -- PRIVATE_EXTENSION_DECLARATION ::=
1427 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1428 -- [abstract] new ancestor_SUBTYPE_INDICATION with PRIVATE;
1430 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1432 -- The caller has already scanned out the part up to the NEW, and Token
1433 -- either contains Tok_New (or ought to, if it doesn't this procedure
1434 -- will post an appropriate "NEW expected" message).
1436 -- Note: the caller is responsible for filling in the Sloc field of
1437 -- the returned node in the private extension declaration case as
1438 -- well as the stuff relating to the discriminant part.
1440 -- Error recovery: can raise Error_Resync;
1442 function P_Derived_Type_Def_Or_Private_Ext_Decl
return Node_Id
is
1443 Typedef_Node
: Node_Id
;
1444 Typedecl_Node
: Node_Id
;
1447 Typedef_Node
:= New_Node
(N_Derived_Type_Definition
, Token_Ptr
);
1450 if Token
= Tok_Abstract
then
1451 Error_Msg_SC
("ABSTRACT must come before NEW, not after");
1455 Set_Subtype_Indication
(Typedef_Node
, P_Subtype_Indication
);
1457 -- Deal with record extension, note that we assume that a WITH is
1458 -- missing in the case of "type X is new Y record ..." or in the
1459 -- case of "type X is new Y null record".
1462 or else Token
= Tok_Record
1463 or else Token
= Tok_Null
1465 T_With
; -- past WITH or give error message
1467 if Token
= Tok_Limited
then
1469 ("LIMITED keyword not allowed in private extension");
1470 Scan
; -- ignore LIMITED
1473 -- Private extension declaration
1475 if Token
= Tok_Private
then
1476 Scan
; -- past PRIVATE
1478 -- Throw away the type definition node and build the type
1479 -- declaration node. Note the caller must set the Sloc,
1480 -- Discriminant_Specifications, Unknown_Discriminants_Present,
1481 -- and Defined_Identifier fields in the returned node.
1484 Make_Private_Extension_Declaration
(No_Location
,
1485 Defining_Identifier
=> Empty
,
1486 Subtype_Indication
=> Subtype_Indication
(Typedef_Node
),
1487 Abstract_Present
=> Abstract_Present
(Typedef_Node
));
1489 Delete_Node
(Typedef_Node
);
1490 return Typedecl_Node
;
1492 -- Derived type definition with record extension part
1495 Set_Record_Extension_Part
(Typedef_Node
, P_Record_Definition
);
1496 return Typedef_Node
;
1499 -- Derived type definition with no record extension part
1502 return Typedef_Node
;
1504 end P_Derived_Type_Def_Or_Private_Ext_Decl
;
1506 ---------------------------
1507 -- 3.5 Range Constraint --
1508 ---------------------------
1510 -- RANGE_CONSTRAINT ::= range RANGE
1512 -- The caller has checked that the initial token is RANGE
1514 -- Error recovery: cannot raise Error_Resync
1516 function P_Range_Constraint
return Node_Id
is
1517 Range_Node
: Node_Id
;
1520 Range_Node
:= New_Node
(N_Range_Constraint
, Token_Ptr
);
1522 Set_Range_Expression
(Range_Node
, P_Range
);
1524 end P_Range_Constraint
;
1531 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1533 -- Note: the range that appears in a membership test is parsed by
1534 -- P_Range_Or_Subtype_Mark (3.5).
1536 -- Error recovery: cannot raise Error_Resync
1538 function P_Range
return Node_Id
is
1539 Expr_Node
: Node_Id
;
1540 Range_Node
: Node_Id
;
1543 Expr_Node
:= P_Simple_Expression_Or_Range_Attribute
;
1545 if Expr_Form
= EF_Range_Attr
then
1548 elsif Token
= Tok_Dot_Dot
then
1549 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
1550 Set_Low_Bound
(Range_Node
, Expr_Node
);
1552 Expr_Node
:= P_Expression
;
1553 Check_Simple_Expression
(Expr_Node
);
1554 Set_High_Bound
(Range_Node
, Expr_Node
);
1557 -- Anything else is an error
1560 T_Dot_Dot
; -- force missing .. message
1565 ----------------------------------
1566 -- 3.5 P_Range_Or_Subtype_Mark --
1567 ----------------------------------
1570 -- RANGE_ATTRIBUTE_REFERENCE
1571 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1573 -- This routine scans out the range or subtype mark that forms the right
1574 -- operand of a membership test.
1576 -- Note: as documented in the Sinfo interface, although the syntax only
1577 -- allows a subtype mark, we in fact allow any simple expression to be
1578 -- returned from this routine. The semantics is responsible for issuing
1579 -- an appropriate message complaining if the argument is not a name.
1580 -- This simplifies the coding and error recovery processing in the
1581 -- parser, and in any case it is preferable not to consider this a
1582 -- syntax error and to continue with the semantic analysis.
1584 -- Error recovery: cannot raise Error_Resync
1586 function P_Range_Or_Subtype_Mark
return Node_Id
is
1587 Expr_Node
: Node_Id
;
1588 Range_Node
: Node_Id
;
1591 Expr_Node
:= P_Simple_Expression_Or_Range_Attribute
;
1593 if Expr_Form
= EF_Range_Attr
then
1596 -- Simple_Expression .. Simple_Expression
1598 elsif Token
= Tok_Dot_Dot
then
1599 Check_Simple_Expression
(Expr_Node
);
1600 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
1601 Set_Low_Bound
(Range_Node
, Expr_Node
);
1603 Set_High_Bound
(Range_Node
, P_Simple_Expression
);
1606 -- Case of subtype mark (optionally qualified simple name or an
1607 -- attribute whose prefix is an optionally qualifed simple name)
1609 elsif Expr_Form
= EF_Simple_Name
1610 or else Nkind
(Expr_Node
) = N_Attribute_Reference
1612 -- Check for error of range constraint after a subtype mark
1614 if Token
= Tok_Range
then
1616 ("range constraint not allowed in membership test");
1620 -- Check for error of DIGITS or DELTA after a subtype mark
1622 elsif Token
= Tok_Digits
or else Token
= Tok_Delta
then
1624 ("accuracy definition not allowed in membership test");
1625 Scan
; -- past DIGITS or DELTA
1628 elsif Token
= Tok_Apostrophe
then
1629 return P_Subtype_Mark_Attribute
(Expr_Node
);
1635 -- At this stage, we have some junk following the expression. We
1636 -- really can't tell what is wrong, might be a missing semicolon,
1637 -- or a missing THEN, or whatever. Our caller will figure it out!
1642 end P_Range_Or_Subtype_Mark
;
1644 ----------------------------------------
1645 -- 3.5.1 Enumeration Type Definition --
1646 ----------------------------------------
1648 -- ENUMERATION_TYPE_DEFINITION ::=
1649 -- (ENUMERATION_LITERAL_SPECIFICATION
1650 -- {, ENUMERATION_LITERAL_SPECIFICATION})
1652 -- The caller has already scanned out the TYPE keyword
1654 -- Error recovery: can raise Error_Resync;
1656 function P_Enumeration_Type_Definition
return Node_Id
is
1657 Typedef_Node
: Node_Id
;
1660 Typedef_Node
:= New_Node
(N_Enumeration_Type_Definition
, Token_Ptr
);
1661 Set_Literals
(Typedef_Node
, New_List
);
1666 Append
(P_Enumeration_Literal_Specification
, Literals
(Typedef_Node
));
1667 exit when not Comma_Present
;
1671 return Typedef_Node
;
1672 end P_Enumeration_Type_Definition
;
1674 ----------------------------------------------
1675 -- 3.5.1 Enumeration Literal Specification --
1676 ----------------------------------------------
1678 -- ENUMERATION_LITERAL_SPECIFICATION ::=
1679 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
1681 -- Error recovery: can raise Error_Resync
1683 function P_Enumeration_Literal_Specification
return Node_Id
is
1685 if Token
= Tok_Char_Literal
then
1686 return P_Defining_Character_Literal
;
1688 return P_Defining_Identifier
(C_Comma_Right_Paren
);
1690 end P_Enumeration_Literal_Specification
;
1692 ---------------------------------------
1693 -- 3.5.1 Defining_Character_Literal --
1694 ---------------------------------------
1696 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
1698 -- Error recovery: cannot raise Error_Resync
1700 -- The caller has checked that the current token is a character literal
1702 function P_Defining_Character_Literal
return Node_Id
is
1703 Literal_Node
: Node_Id
;
1706 Literal_Node
:= Token_Node
;
1707 Change_Character_Literal_To_Defining_Character_Literal
(Literal_Node
);
1708 Scan
; -- past character literal
1709 return Literal_Node
;
1710 end P_Defining_Character_Literal
;
1712 ------------------------------------
1713 -- 3.5.4 Integer Type Definition --
1714 ------------------------------------
1716 -- Parsed by P_Type_Declaration (3.2.1)
1718 -------------------------------------------
1719 -- 3.5.4 Signed Integer Type Definition --
1720 -------------------------------------------
1722 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
1723 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
1725 -- Normally the initial token on entry is RANGE, but in some
1726 -- error conditions, the range token was missing and control is
1727 -- passed with Token pointing to first token of the first expression.
1729 -- Error recovery: cannot raise Error_Resync
1731 function P_Signed_Integer_Type_Definition
return Node_Id
is
1732 Typedef_Node
: Node_Id
;
1733 Expr_Node
: Node_Id
;
1736 Typedef_Node
:= New_Node
(N_Signed_Integer_Type_Definition
, Token_Ptr
);
1738 if Token
= Tok_Range
then
1742 Expr_Node
:= P_Expression
;
1743 Check_Simple_Expression
(Expr_Node
);
1744 Set_Low_Bound
(Typedef_Node
, Expr_Node
);
1746 Expr_Node
:= P_Expression
;
1747 Check_Simple_Expression
(Expr_Node
);
1748 Set_High_Bound
(Typedef_Node
, Expr_Node
);
1749 return Typedef_Node
;
1750 end P_Signed_Integer_Type_Definition
;
1752 ------------------------------------
1753 -- 3.5.4 Modular Type Definition --
1754 ------------------------------------
1756 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
1758 -- The caller has checked that the initial token is MOD
1760 -- Error recovery: cannot raise Error_Resync
1762 function P_Modular_Type_Definition
return Node_Id
is
1763 Typedef_Node
: Node_Id
;
1767 Error_Msg_SC
("(Ada 83): modular types not allowed");
1770 Typedef_Node
:= New_Node
(N_Modular_Type_Definition
, Token_Ptr
);
1772 Set_Expression
(Typedef_Node
, P_Expression_No_Right_Paren
);
1774 -- Handle mod L..R cleanly
1776 if Token
= Tok_Dot_Dot
then
1777 Error_Msg_SC
("range not allowed for modular type");
1779 Set_Expression
(Typedef_Node
, P_Expression_No_Right_Paren
);
1782 return Typedef_Node
;
1783 end P_Modular_Type_Definition
;
1785 ---------------------------------
1786 -- 3.5.6 Real Type Definition --
1787 ---------------------------------
1789 -- Parsed by P_Type_Declaration (3.2.1)
1791 --------------------------------------
1792 -- 3.5.7 Floating Point Definition --
1793 --------------------------------------
1795 -- FLOATING_POINT_DEFINITION ::=
1796 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
1798 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
1800 -- The caller has checked that the initial token is DIGITS
1802 -- Error recovery: cannot raise Error_Resync
1804 function P_Floating_Point_Definition
return Node_Id
is
1805 Digits_Loc
: constant Source_Ptr
:= Token_Ptr
;
1807 Expr_Node
: Node_Id
;
1810 Scan
; -- past DIGITS
1811 Expr_Node
:= P_Expression_No_Right_Paren
;
1812 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
1814 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
1816 if Token
= Tok_Delta
then
1817 Error_Msg_SC
("DELTA must come before DIGITS");
1818 Def_Node
:= New_Node
(N_Decimal_Fixed_Point_Definition
, Digits_Loc
);
1820 Set_Delta_Expression
(Def_Node
, P_Expression_No_Right_Paren
);
1822 -- OK floating-point definition
1825 Def_Node
:= New_Node
(N_Floating_Point_Definition
, Digits_Loc
);
1828 Set_Digits_Expression
(Def_Node
, Expr_Node
);
1829 Set_Real_Range_Specification
(Def_Node
, P_Real_Range_Specification_Opt
);
1831 end P_Floating_Point_Definition
;
1833 -------------------------------------
1834 -- 3.5.7 Real Range Specification --
1835 -------------------------------------
1837 -- REAL_RANGE_SPECIFICATION ::=
1838 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
1840 -- Error recovery: cannot raise Error_Resync
1842 function P_Real_Range_Specification_Opt
return Node_Id
is
1843 Specification_Node
: Node_Id
;
1844 Expr_Node
: Node_Id
;
1847 if Token
= Tok_Range
then
1848 Specification_Node
:=
1849 New_Node
(N_Real_Range_Specification
, Token_Ptr
);
1851 Expr_Node
:= P_Expression_No_Right_Paren
;
1852 Check_Simple_Expression
(Expr_Node
);
1853 Set_Low_Bound
(Specification_Node
, Expr_Node
);
1855 Expr_Node
:= P_Expression_No_Right_Paren
;
1856 Check_Simple_Expression
(Expr_Node
);
1857 Set_High_Bound
(Specification_Node
, Expr_Node
);
1858 return Specification_Node
;
1862 end P_Real_Range_Specification_Opt
;
1864 -----------------------------------
1865 -- 3.5.9 Fixed Point Definition --
1866 -----------------------------------
1868 -- FIXED_POINT_DEFINITION ::=
1869 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
1871 -- ORDINARY_FIXED_POINT_DEFINITION ::=
1872 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
1874 -- DECIMAL_FIXED_POINT_DEFINITION ::=
1875 -- delta static_EXPRESSION
1876 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
1878 -- The caller has checked that the initial token is DELTA
1880 -- Error recovery: cannot raise Error_Resync
1882 function P_Fixed_Point_Definition
return Node_Id
is
1883 Delta_Node
: Node_Id
;
1884 Delta_Loc
: Source_Ptr
;
1886 Expr_Node
: Node_Id
;
1889 Delta_Loc
:= Token_Ptr
;
1891 Delta_Node
:= P_Expression_No_Right_Paren
;
1892 Check_Simple_Expression_In_Ada_83
(Delta_Node
);
1894 if Token
= Tok_Digits
then
1896 Error_Msg_SC
("(Ada 83) decimal fixed type not allowed!");
1899 Def_Node
:= New_Node
(N_Decimal_Fixed_Point_Definition
, Delta_Loc
);
1900 Scan
; -- past DIGITS
1901 Expr_Node
:= P_Expression_No_Right_Paren
;
1902 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
1903 Set_Digits_Expression
(Def_Node
, Expr_Node
);
1906 Def_Node
:= New_Node
(N_Ordinary_Fixed_Point_Definition
, Delta_Loc
);
1908 -- Range is required in ordinary fixed point case
1910 if Token
/= Tok_Range
then
1911 Error_Msg_AP
("range must be given for fixed-point type");
1916 Set_Delta_Expression
(Def_Node
, Delta_Node
);
1917 Set_Real_Range_Specification
(Def_Node
, P_Real_Range_Specification_Opt
);
1919 end P_Fixed_Point_Definition
;
1921 --------------------------------------------
1922 -- 3.5.9 Ordinary Fixed Point Definition --
1923 --------------------------------------------
1925 -- Parsed by P_Fixed_Point_Definition (3.5.9)
1927 -------------------------------------------
1928 -- 3.5.9 Decimal Fixed Point Definition --
1929 -------------------------------------------
1931 -- Parsed by P_Decimal_Point_Definition (3.5.9)
1933 ------------------------------
1934 -- 3.5.9 Digits Constraint --
1935 ------------------------------
1937 -- DIGITS_CONSTRAINT ::=
1938 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
1940 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
1942 -- The caller has checked that the initial token is DIGITS
1944 function P_Digits_Constraint
return Node_Id
is
1945 Constraint_Node
: Node_Id
;
1946 Expr_Node
: Node_Id
;
1949 Constraint_Node
:= New_Node
(N_Digits_Constraint
, Token_Ptr
);
1950 Scan
; -- past DIGITS
1951 Expr_Node
:= P_Expression_No_Right_Paren
;
1952 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
1953 Set_Digits_Expression
(Constraint_Node
, Expr_Node
);
1955 if Token
= Tok_Range
then
1956 Set_Range_Constraint
(Constraint_Node
, P_Range_Constraint
);
1959 return Constraint_Node
;
1960 end P_Digits_Constraint
;
1962 -----------------------------
1963 -- 3.5.9 Delta Constraint --
1964 -----------------------------
1966 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
1968 -- Note: this is an obsolescent feature in Ada 95 (I.3)
1970 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
1972 -- The caller has checked that the initial token is DELTA
1974 -- Error recovery: cannot raise Error_Resync
1976 function P_Delta_Constraint
return Node_Id
is
1977 Constraint_Node
: Node_Id
;
1978 Expr_Node
: Node_Id
;
1981 Constraint_Node
:= New_Node
(N_Delta_Constraint
, Token_Ptr
);
1983 Expr_Node
:= P_Expression_No_Right_Paren
;
1984 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
1985 Set_Delta_Expression
(Constraint_Node
, Expr_Node
);
1987 if Token
= Tok_Range
then
1988 Set_Range_Constraint
(Constraint_Node
, P_Range_Constraint
);
1991 return Constraint_Node
;
1992 end P_Delta_Constraint
;
1994 --------------------------------
1995 -- 3.6 Array Type Definition --
1996 --------------------------------
1998 -- ARRAY_TYPE_DEFINITION ::=
1999 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2001 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2002 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2003 -- COMPONENT_DEFINITION
2005 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2007 -- CONSTRAINED_ARRAY_DEFINITION ::=
2008 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2009 -- COMPONENT_DEFINITION
2011 -- DISCRETE_SUBTYPE_DEFINITION ::=
2012 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2014 -- COMPONENT_DEFINITION ::= [aliased] SUBTYPE_INDICATION
2016 -- The caller has checked that the initial token is ARRAY
2018 -- Error recovery: can raise Error_Resync
2020 function P_Array_Type_Definition
return Node_Id
is
2021 Array_Loc
: Source_Ptr
;
2023 Subs_List
: List_Id
;
2024 Scan_State
: Saved_Scan_State
;
2027 Array_Loc
:= Token_Ptr
;
2029 Subs_List
:= New_List
;
2032 -- It's quite tricky to disentangle these two possibilities, so we do
2033 -- a prescan to determine which case we have and then reset the scan.
2034 -- The prescan skips past possible subtype mark tokens.
2036 Save_Scan_State
(Scan_State
); -- just after paren
2038 while Token
in Token_Class_Desig
or else
2039 Token
= Tok_Dot
or else
2040 Token
= Tok_Apostrophe
-- because of 'BASE, 'CLASS
2045 -- If we end up on RANGE <> then we have the unconstrained case. We
2046 -- will also allow the RANGE to be omitted, just to improve error
2047 -- handling for a case like array (integer <>) of integer;
2049 Scan
; -- past possible RANGE or <>
2051 if (Prev_Token
= Tok_Range
and then Token
= Tok_Box
) or else
2052 Prev_Token
= Tok_Box
2054 Def_Node
:= New_Node
(N_Unconstrained_Array_Definition
, Array_Loc
);
2055 Restore_Scan_State
(Scan_State
); -- to first subtype mark
2058 Append
(P_Subtype_Mark_Resync
, Subs_List
);
2061 exit when Token
= Tok_Right_Paren
or else Token
= Tok_Of
;
2065 Set_Subtype_Marks
(Def_Node
, Subs_List
);
2068 Def_Node
:= New_Node
(N_Constrained_Array_Definition
, Array_Loc
);
2069 Restore_Scan_State
(Scan_State
); -- to first discrete range
2072 Append
(P_Discrete_Subtype_Definition
, Subs_List
);
2073 exit when not Comma_Present
;
2076 Set_Discrete_Subtype_Definitions
(Def_Node
, Subs_List
);
2082 if Token
= Tok_Aliased
then
2083 Set_Aliased_Present
(Def_Node
, True);
2084 Scan
; -- past ALIASED
2087 Set_Subtype_Indication
(Def_Node
, P_Subtype_Indication
);
2089 end P_Array_Type_Definition
;
2091 -----------------------------------------
2092 -- 3.6 Unconstrained Array Definition --
2093 -----------------------------------------
2095 -- Parsed by P_Array_Type_Definition (3.6)
2097 ---------------------------------------
2098 -- 3.6 Constrained Array Definition --
2099 ---------------------------------------
2101 -- Parsed by P_Array_Type_Definition (3.6)
2103 --------------------------------------
2104 -- 3.6 Discrete Subtype Definition --
2105 --------------------------------------
2107 -- DISCRETE_SUBTYPE_DEFINITION ::=
2108 -- discrete_SUBTYPE_INDICATION | RANGE
2110 -- Note: the discrete subtype definition appearing in a constrained
2111 -- array definition is parsed by P_Array_Type_Definition (3.6)
2113 -- Error recovery: cannot raise Error_Resync
2115 function P_Discrete_Subtype_Definition
return Node_Id
is
2117 -- The syntax of a discrete subtype definition is identical to that
2118 -- of a discrete range, so we simply share the same parsing code.
2120 return P_Discrete_Range
;
2121 end P_Discrete_Subtype_Definition
;
2123 -------------------------------
2124 -- 3.6 Component Definition --
2125 -------------------------------
2127 -- For the array case, parsed by P_Array_Type_Definition (3.6)
2128 -- For the record case, parsed by P_Component_Declaration (3.8)
2130 -----------------------------
2131 -- 3.6.1 Index Constraint --
2132 -----------------------------
2134 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2136 ---------------------------
2137 -- 3.6.1 Discrete Range --
2138 ---------------------------
2140 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2142 -- The possible forms for a discrete range are:
2144 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
2145 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
2146 -- Range_Attribute (RANGE, 3.5)
2147 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
2149 -- Error recovery: cannot raise Error_Resync
2151 function P_Discrete_Range
return Node_Id
is
2152 Expr_Node
: Node_Id
;
2153 Range_Node
: Node_Id
;
2156 Expr_Node
:= P_Simple_Expression_Or_Range_Attribute
;
2158 if Expr_Form
= EF_Range_Attr
then
2161 elsif Token
= Tok_Range
then
2162 if Expr_Form
/= EF_Simple_Name
then
2163 Error_Msg_SC
("range must be preceded by subtype mark");
2166 return P_Subtype_Indication
(Expr_Node
);
2168 -- Check Expression .. Expression case
2170 elsif Token
= Tok_Dot_Dot
then
2171 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
2172 Set_Low_Bound
(Range_Node
, Expr_Node
);
2174 Expr_Node
:= P_Expression
;
2175 Check_Simple_Expression
(Expr_Node
);
2176 Set_High_Bound
(Range_Node
, Expr_Node
);
2179 -- Otherwise we must have a subtype mark
2181 elsif Expr_Form
= EF_Simple_Name
then
2184 -- If incorrect, complain that we expect ..
2190 end P_Discrete_Range
;
2192 ----------------------------
2193 -- 3.7 Discriminant Part --
2194 ----------------------------
2196 -- DISCRIMINANT_PART ::=
2197 -- UNKNOWN_DISCRIMINANT_PART
2198 -- | KNOWN_DISCRIMINANT_PART
2200 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2201 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2203 ------------------------------------
2204 -- 3.7 Unknown Discriminant Part --
2205 ------------------------------------
2207 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
2209 -- If no unknown discriminant part is present, then False is returned,
2210 -- otherwise the unknown discriminant is scanned out and True is returned.
2212 -- Error recovery: cannot raise Error_Resync
2214 function P_Unknown_Discriminant_Part_Opt
return Boolean is
2215 Scan_State
: Saved_Scan_State
;
2218 if Token
/= Tok_Left_Paren
then
2222 Save_Scan_State
(Scan_State
);
2223 Scan
; -- past the left paren
2225 if Token
= Tok_Box
then
2228 Error_Msg_SC
("(Ada 83) unknown discriminant not allowed!");
2231 Scan
; -- past the box
2232 T_Right_Paren
; -- must be followed by right paren
2236 Restore_Scan_State
(Scan_State
);
2240 end P_Unknown_Discriminant_Part_Opt
;
2242 ----------------------------------
2243 -- 3.7 Known Discriminant Part --
2244 ----------------------------------
2246 -- KNOWN_DISCRIMINANT_PART ::=
2247 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2249 -- DISCRIMINANT_SPECIFICATION ::=
2250 -- DEFINING_IDENTIFIER_LIST : SUBTYPE_MARK
2251 -- [:= DEFAULT_EXPRESSION]
2252 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2253 -- [:= DEFAULT_EXPRESSION]
2255 -- If no known discriminant part is present, then No_List is returned
2257 -- Error recovery: cannot raise Error_Resync
2259 function P_Known_Discriminant_Part_Opt
return List_Id
is
2260 Specification_Node
: Node_Id
;
2261 Specification_List
: List_Id
;
2262 Ident_Sloc
: Source_Ptr
;
2263 Scan_State
: Saved_Scan_State
;
2267 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
2268 -- This array holds the list of defining identifiers. The upper bound
2269 -- of 4096 is intended to be essentially infinite, and we do not even
2270 -- bother to check for it being exceeded.
2273 if Token
= Tok_Left_Paren
then
2274 Specification_List
:= New_List
;
2276 P_Pragmas_Misplaced
;
2278 Specification_Loop
: loop
2280 Ident_Sloc
:= Token_Ptr
;
2281 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
2284 while Comma_Present
loop
2285 Num_Idents
:= Num_Idents
+ 1;
2286 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
2291 -- If there are multiple identifiers, we repeatedly scan the
2292 -- type and initialization expression information by resetting
2293 -- the scan pointer (so that we get completely separate trees
2294 -- for each occurrence).
2296 if Num_Idents
> 1 then
2297 Save_Scan_State
(Scan_State
);
2300 -- Loop through defining identifiers in list
2304 Specification_Node
:=
2305 New_Node
(N_Discriminant_Specification
, Ident_Sloc
);
2306 Set_Defining_Identifier
(Specification_Node
, Idents
(Ident
));
2308 if Token
= Tok_Access
then
2311 ("(Ada 83) access discriminant not allowed!");
2314 Set_Discriminant_Type
2315 (Specification_Node
, P_Access_Definition
);
2317 Set_Discriminant_Type
2318 (Specification_Node
, P_Subtype_Mark
);
2323 (Specification_Node
, Init_Expr_Opt
(True));
2326 Set_Prev_Ids
(Specification_Node
, True);
2329 if Ident
< Num_Idents
then
2330 Set_More_Ids
(Specification_Node
, True);
2333 Append
(Specification_Node
, Specification_List
);
2334 exit Ident_Loop
when Ident
= Num_Idents
;
2336 Restore_Scan_State
(Scan_State
);
2337 end loop Ident_Loop
;
2339 exit Specification_Loop
when Token
/= Tok_Semicolon
;
2341 P_Pragmas_Misplaced
;
2342 end loop Specification_Loop
;
2345 return Specification_List
;
2350 end P_Known_Discriminant_Part_Opt
;
2352 -------------------------------------
2353 -- 3.7 DIscriminant Specification --
2354 -------------------------------------
2356 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
2358 -----------------------------
2359 -- 3.7 Default Expression --
2360 -----------------------------
2362 -- Always parsed (simply as an Expression) by the parent construct
2364 ------------------------------------
2365 -- 3.7.1 Discriminant Constraint --
2366 ------------------------------------
2368 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2370 --------------------------------------------------------
2371 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
2372 --------------------------------------------------------
2374 -- DISCRIMINANT_CONSTRAINT ::=
2375 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
2377 -- DISCRIMINANT_ASSOCIATION ::=
2378 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2381 -- This routine parses either an index or a discriminant constraint. As
2382 -- is clear from the above grammar, it is often possible to clearly
2383 -- determine which of the two possibilities we have, but there are
2384 -- cases (those in which we have a series of expressions of the same
2385 -- syntactic form as subtype indications), where we cannot tell. Since
2386 -- this means that in any case the semantic phase has to distinguish
2387 -- between the two, there is not much point in the parser trying to
2388 -- distinguish even those cases where the difference is clear. In any
2389 -- case, if we have a situation like:
2391 -- (A => 123, 235 .. 500)
2393 -- it is not clear which of the two items is the wrong one, better to
2394 -- let the semantic phase give a clear message. Consequently, this
2395 -- routine in general returns a list of items which can be either
2396 -- discrete ranges or discriminant associations.
2398 -- The caller has checked that the initial token is a left paren
2400 -- Error recovery: can raise Error_Resync
2402 function P_Index_Or_Discriminant_Constraint
return Node_Id
is
2403 Scan_State
: Saved_Scan_State
;
2404 Constr_Node
: Node_Id
;
2405 Constr_List
: List_Id
;
2406 Expr_Node
: Node_Id
;
2407 Result_Node
: Node_Id
;
2410 Result_Node
:= New_Node
(N_Index_Or_Discriminant_Constraint
, Token_Ptr
);
2412 Constr_List
:= New_List
;
2413 Set_Constraints
(Result_Node
, Constr_List
);
2415 -- The two syntactic forms are a little mixed up, so what we are doing
2416 -- here is looking at the first entry to determine which case we have
2418 -- A discriminant constraint is a list of discriminant associations,
2419 -- which have one of the following possible forms:
2423 -- Id | Id | .. | Id => Expression
2425 -- An index constraint is a list of discrete ranges which have one
2426 -- of the following possible forms:
2429 -- Subtype_Mark range Range
2431 -- Simple_Expression .. Simple_Expression
2433 -- Loop through discriminants in list
2436 -- Check cases of Id => Expression or Id | Id => Expression
2438 if Token
= Tok_Identifier
then
2439 Save_Scan_State
(Scan_State
); -- at Id
2442 if Token
= Tok_Arrow
or else Token
= Tok_Vertical_Bar
then
2443 Restore_Scan_State
(Scan_State
); -- to Id
2444 Append
(P_Discriminant_Association
, Constr_List
);
2447 Restore_Scan_State
(Scan_State
); -- to Id
2451 -- Otherwise scan out an expression and see what we have got
2453 Expr_Node
:= P_Expression_Or_Range_Attribute
;
2455 if Expr_Form
= EF_Range_Attr
then
2456 Append
(Expr_Node
, Constr_List
);
2458 elsif Token
= Tok_Range
then
2459 if Expr_Form
/= EF_Simple_Name
then
2460 Error_Msg_SC
("subtype mark required before RANGE");
2463 Append
(P_Subtype_Indication
(Expr_Node
), Constr_List
);
2466 -- Check Simple_Expression .. Simple_Expression case
2468 elsif Token
= Tok_Dot_Dot
then
2469 Check_Simple_Expression
(Expr_Node
);
2470 Constr_Node
:= New_Node
(N_Range
, Token_Ptr
);
2471 Set_Low_Bound
(Constr_Node
, Expr_Node
);
2473 Expr_Node
:= P_Expression
;
2474 Check_Simple_Expression
(Expr_Node
);
2475 Set_High_Bound
(Constr_Node
, Expr_Node
);
2476 Append
(Constr_Node
, Constr_List
);
2479 -- Case of an expression which could be either form
2482 Append
(Expr_Node
, Constr_List
);
2486 -- Here with a single entry scanned
2489 exit when not Comma_Present
;
2495 end P_Index_Or_Discriminant_Constraint
;
2497 -------------------------------------
2498 -- 3.7.1 Discriminant Association --
2499 -------------------------------------
2501 -- DISCRIMINANT_ASSOCIATION ::=
2502 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2505 -- This routine is used only when the name list is present and the caller
2506 -- has already checked this (by scanning ahead and repositioning the
2509 -- Error_Recovery: cannot raise Error_Resync;
2511 function P_Discriminant_Association
return Node_Id
is
2512 Discr_Node
: Node_Id
;
2513 Names_List
: List_Id
;
2514 Ident_Sloc
: Source_Ptr
;
2517 Ident_Sloc
:= Token_Ptr
;
2518 Names_List
:= New_List
;
2521 Append
(P_Identifier
(C_Vertical_Bar_Arrow
), Names_List
);
2522 exit when Token
/= Tok_Vertical_Bar
;
2526 Discr_Node
:= New_Node
(N_Discriminant_Association
, Ident_Sloc
);
2527 Set_Selector_Names
(Discr_Node
, Names_List
);
2529 Set_Expression
(Discr_Node
, P_Expression
);
2531 end P_Discriminant_Association
;
2533 ---------------------------------
2534 -- 3.8 Record Type Definition --
2535 ---------------------------------
2537 -- RECORD_TYPE_DEFINITION ::=
2538 -- [[abstract] tagged] [limited] RECORD_DEFINITION
2540 -- There is no node in the tree for a record type definition. Instead
2541 -- a record definition node appears, with possible Abstract_Present,
2542 -- Tagged_Present, and Limited_Present flags set appropriately.
2544 ----------------------------
2545 -- 3.8 Record Definition --
2546 ----------------------------
2548 -- RECORD_DEFINITION ::=
2554 -- Note: in the case where a record definition node is used to represent
2555 -- a record type definition, the caller sets the Tagged_Present and
2556 -- Limited_Present flags in the resulting N_Record_Definition node as
2559 -- Note that the RECORD token at the start may be missing in certain
2560 -- error situations, so this function is expected to post the error
2562 -- Error recovery: can raise Error_Resync
2564 function P_Record_Definition
return Node_Id
is
2568 Rec_Node
:= New_Node
(N_Record_Definition
, Token_Ptr
);
2572 if Token
= Tok_Null
then
2575 Set_Null_Present
(Rec_Node
, True);
2577 -- Case starting with RECORD keyword. Build scope stack entry. For the
2578 -- column, we use the first non-blank character on the line, to deal
2579 -- with situations such as:
2585 -- which is not official RM indentation, but is not uncommon usage
2589 Scope
.Table
(Scope
.Last
).Etyp
:= E_Record
;
2590 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
2591 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
2592 Scope
.Table
(Scope
.Last
).Labl
:= Error
;
2593 Scope
.Table
(Scope
.Last
).Junk
:= (Token
/= Tok_Record
);
2597 Set_Component_List
(Rec_Node
, P_Component_List
);
2600 exit when Check_End
;
2601 Discard_Junk_Node
(P_Component_List
);
2606 end P_Record_Definition
;
2608 -------------------------
2609 -- 3.8 Component List --
2610 -------------------------
2612 -- COMPONENT_LIST ::=
2613 -- COMPONENT_ITEM {COMPONENT_ITEM}
2614 -- | {COMPONENT_ITEM} VARIANT_PART
2617 -- Error recovery: cannot raise Error_Resync
2619 function P_Component_List
return Node_Id
is
2620 Component_List_Node
: Node_Id
;
2621 Decls_List
: List_Id
;
2622 Scan_State
: Saved_Scan_State
;
2625 Component_List_Node
:= New_Node
(N_Component_List
, Token_Ptr
);
2626 Decls_List
:= New_List
;
2628 if Token
= Tok_Null
then
2631 P_Pragmas_Opt
(Decls_List
);
2632 Set_Null_Present
(Component_List_Node
, True);
2633 return Component_List_Node
;
2636 P_Pragmas_Opt
(Decls_List
);
2638 if Token
/= Tok_Case
then
2639 Component_Scan_Loop
: loop
2640 P_Component_Items
(Decls_List
);
2641 P_Pragmas_Opt
(Decls_List
);
2643 exit Component_Scan_Loop
when Token
= Tok_End
2644 or else Token
= Tok_Case
2645 or else Token
= Tok_When
;
2647 -- We are done if we do not have an identifier. However, if
2648 -- we have a misspelled reserved identifier that is in a column
2649 -- to the right of the record definition, we will treat it as
2650 -- an identifier. It turns out to be too dangerous in practice
2651 -- to accept such a mis-spelled identifier which does not have
2652 -- this additional clue that confirms the incorrect spelling.
2654 if Token
/= Tok_Identifier
then
2655 if Start_Column
> Scope
.Table
(Scope
.Last
).Ecol
2656 and then Is_Reserved_Identifier
2658 Save_Scan_State
(Scan_State
); -- at reserved id
2659 Scan
; -- possible reserved id
2661 if Token
= Tok_Comma
or else Token
= Tok_Colon
then
2662 Restore_Scan_State
(Scan_State
);
2663 Scan_Reserved_Identifier
(Force_Msg
=> True);
2665 -- Note reserved identifier used as field name after
2666 -- all because not followed by colon or comma
2669 Restore_Scan_State
(Scan_State
);
2670 exit Component_Scan_Loop
;
2673 -- Non-identifier that definitely was not reserved id
2676 exit Component_Scan_Loop
;
2679 end loop Component_Scan_Loop
;
2682 if Token
= Tok_Case
then
2683 Set_Variant_Part
(Component_List_Node
, P_Variant_Part
);
2685 -- Check for junk after variant part
2687 if Token
= Tok_Identifier
then
2688 Save_Scan_State
(Scan_State
);
2689 Scan
; -- past identifier
2691 if Token
= Tok_Colon
then
2692 Restore_Scan_State
(Scan_State
);
2693 Error_Msg_SC
("component may not follow variant part");
2694 Discard_Junk_Node
(P_Component_List
);
2696 elsif Token
= Tok_Case
then
2697 Restore_Scan_State
(Scan_State
);
2698 Error_Msg_SC
("only one variant part allowed in a record");
2699 Discard_Junk_Node
(P_Component_List
);
2702 Restore_Scan_State
(Scan_State
);
2708 Set_Component_Items
(Component_List_Node
, Decls_List
);
2709 return Component_List_Node
;
2710 end P_Component_List
;
2712 -------------------------
2713 -- 3.8 Component Item --
2714 -------------------------
2716 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
2718 -- COMPONENT_DECLARATION ::=
2719 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
2720 -- [:= DEFAULT_EXPRESSION];
2722 -- COMPONENT_DEFINITION ::= [aliased] SUBTYPE_INDICATION
2724 -- Error recovery: cannot raise Error_Resync, if an error occurs,
2725 -- the scan is positioned past the following semicolon.
2727 -- Note: we do not yet allow representation clauses to appear as component
2728 -- items, do we need to add this capability sometime in the future ???
2730 procedure P_Component_Items
(Decls
: List_Id
) is
2731 Decl_Node
: Node_Id
;
2732 Scan_State
: Saved_Scan_State
;
2735 Ident_Sloc
: Source_Ptr
;
2737 Idents
: array (Int
range 1 .. 4096) of Entity_Id
;
2738 -- This array holds the list of defining identifiers. The upper bound
2739 -- of 4096 is intended to be essentially infinite, and we do not even
2740 -- bother to check for it being exceeded.
2743 if Token
/= Tok_Identifier
then
2744 Error_Msg_SC
("component declaration expected");
2745 Resync_Past_Semicolon
;
2749 Ident_Sloc
:= Token_Ptr
;
2750 Idents
(1) := P_Defining_Identifier
(C_Comma_Colon
);
2753 while Comma_Present
loop
2754 Num_Idents
:= Num_Idents
+ 1;
2755 Idents
(Num_Idents
) := P_Defining_Identifier
(C_Comma_Colon
);
2760 -- If there are multiple identifiers, we repeatedly scan the
2761 -- type and initialization expression information by resetting
2762 -- the scan pointer (so that we get completely separate trees
2763 -- for each occurrence).
2765 if Num_Idents
> 1 then
2766 Save_Scan_State
(Scan_State
);
2769 -- Loop through defining identifiers in list
2774 -- The following block is present to catch Error_Resync
2775 -- which causes the parse to be reset past the semicolon
2778 Decl_Node
:= New_Node
(N_Component_Declaration
, Ident_Sloc
);
2779 Set_Defining_Identifier
(Decl_Node
, Idents
(Ident
));
2781 if Token
= Tok_Constant
then
2782 Error_Msg_SC
("constant components are not permitted");
2786 if Token_Name
= Name_Aliased
then
2787 Check_95_Keyword
(Tok_Aliased
, Tok_Identifier
);
2790 if Token
= Tok_Aliased
then
2791 Scan
; -- past ALIASED
2792 Set_Aliased_Present
(Decl_Node
, True);
2795 if Token
= Tok_Array
then
2796 Error_Msg_SC
("anonymous arrays not allowed as components");
2800 Set_Subtype_Indication
(Decl_Node
, P_Subtype_Indication
);
2801 Set_Expression
(Decl_Node
, Init_Expr_Opt
);
2804 Set_Prev_Ids
(Decl_Node
, True);
2807 if Ident
< Num_Idents
then
2808 Set_More_Ids
(Decl_Node
, True);
2811 Append
(Decl_Node
, Decls
);
2814 when Error_Resync
=>
2815 if Token
/= Tok_End
then
2816 Resync_Past_Semicolon
;
2820 exit Ident_Loop
when Ident
= Num_Idents
;
2822 Restore_Scan_State
(Scan_State
);
2824 end loop Ident_Loop
;
2827 end P_Component_Items
;
2829 --------------------------------
2830 -- 3.8 Component Declaration --
2831 --------------------------------
2833 -- Parsed by P_Component_Items (3.8)
2835 -------------------------
2836 -- 3.8.1 Variant Part --
2837 -------------------------
2840 -- case discriminant_DIRECT_NAME is
2845 -- The caller has checked that the initial token is CASE
2847 -- Error recovery: cannot raise Error_Resync
2849 function P_Variant_Part
return Node_Id
is
2850 Variant_Part_Node
: Node_Id
;
2851 Variants_List
: List_Id
;
2852 Case_Node
: Node_Id
;
2855 Variant_Part_Node
:= New_Node
(N_Variant_Part
, Token_Ptr
);
2857 Scope
.Table
(Scope
.Last
).Etyp
:= E_Case
;
2858 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
2859 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
2862 Case_Node
:= P_Expression
;
2863 Set_Name
(Variant_Part_Node
, Case_Node
);
2865 if Nkind
(Case_Node
) /= N_Identifier
then
2866 Set_Name
(Variant_Part_Node
, Error
);
2867 Error_Msg
("discriminant name expected", Sloc
(Case_Node
));
2871 Variants_List
:= New_List
;
2872 P_Pragmas_Opt
(Variants_List
);
2874 -- Test missing variant
2876 if Token
= Tok_End
then
2877 Error_Msg_BC
("WHEN expected (must have at least one variant)");
2879 Append
(P_Variant
, Variants_List
);
2882 -- Loop through variants, note that we allow if in place of when,
2883 -- this error will be detected and handled in P_Variant.
2886 P_Pragmas_Opt
(Variants_List
);
2888 if Token
/= Tok_When
2889 and then Token
/= Tok_If
2890 and then Token
/= Tok_Others
2892 exit when Check_End
;
2895 Append
(P_Variant
, Variants_List
);
2898 Set_Variants
(Variant_Part_Node
, Variants_List
);
2899 return Variant_Part_Node
;
2902 --------------------
2904 --------------------
2907 -- when DISCRETE_CHOICE_LIST =>
2910 -- Error recovery: cannot raise Error_Resync
2912 -- The initial token on entry is either WHEN, IF or OTHERS
2914 function P_Variant
return Node_Id
is
2915 Variant_Node
: Node_Id
;
2918 -- Special check to recover nicely from use of IF in place of WHEN
2920 if Token
= Tok_If
then
2927 Variant_Node
:= New_Node
(N_Variant
, Prev_Token_Ptr
);
2928 Set_Discrete_Choices
(Variant_Node
, P_Discrete_Choice_List
);
2930 Set_Component_List
(Variant_Node
, P_Component_List
);
2931 return Variant_Node
;
2934 ---------------------------------
2935 -- 3.8.1 Discrete Choice List --
2936 ---------------------------------
2938 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
2940 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
2942 -- Note: in Ada 83, the expression must be a simple expression
2944 -- Error recovery: cannot raise Error_Resync
2946 function P_Discrete_Choice_List
return List_Id
is
2948 Expr_Node
: Node_Id
;
2949 Choice_Node
: Node_Id
;
2952 Choices
:= New_List
;
2955 if Token
= Tok_Others
then
2956 Append
(New_Node
(N_Others_Choice
, Token_Ptr
), Choices
);
2957 Scan
; -- past OTHERS
2961 Expr_Node
:= No_Right_Paren
(P_Expression_Or_Range_Attribute
);
2963 if Token
= Tok_Colon
2964 and then Nkind
(Expr_Node
) = N_Identifier
2966 Error_Msg_SP
("label not permitted in this context");
2969 elsif Expr_Form
= EF_Range_Attr
then
2970 Append
(Expr_Node
, Choices
);
2972 elsif Token
= Tok_Dot_Dot
then
2973 Check_Simple_Expression
(Expr_Node
);
2974 Choice_Node
:= New_Node
(N_Range
, Token_Ptr
);
2975 Set_Low_Bound
(Choice_Node
, Expr_Node
);
2977 Expr_Node
:= P_Expression_No_Right_Paren
;
2978 Check_Simple_Expression
(Expr_Node
);
2979 Set_High_Bound
(Choice_Node
, Expr_Node
);
2980 Append
(Choice_Node
, Choices
);
2982 elsif Expr_Form
= EF_Simple_Name
then
2983 if Token
= Tok_Range
then
2984 Append
(P_Subtype_Indication
(Expr_Node
), Choices
);
2986 elsif Token
in Token_Class_Consk
then
2988 ("the only constraint allowed here " &
2989 "is a range constraint");
2990 Discard_Junk_Node
(P_Constraint_Opt
);
2991 Append
(Expr_Node
, Choices
);
2994 Append
(Expr_Node
, Choices
);
2998 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
2999 Append
(Expr_Node
, Choices
);
3003 when Error_Resync
=>
3009 if Token
= Tok_Comma
then
3010 Error_Msg_SC
(""","" should be ""'|""");
3012 exit when Token
/= Tok_Vertical_Bar
;
3015 Scan
; -- past | or comma
3019 end P_Discrete_Choice_List
;
3021 ----------------------------
3022 -- 3.8.1 Discrete Choice --
3023 ----------------------------
3025 -- Parsed by P_Discrete_Choice_List (3.8.1)
3027 ----------------------------------
3028 -- 3.9.1 Record Extension Part --
3029 ----------------------------------
3031 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3033 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3035 ----------------------------------
3036 -- 3.10 Access Type Definition --
3037 ----------------------------------
3039 -- ACCESS_TYPE_DEFINITION ::=
3040 -- ACCESS_TO_OBJECT_DEFINITION
3041 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3043 -- ACCESS_TO_OBJECT_DEFINITION ::=
3044 -- access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3046 -- GENERAL_ACCESS_MODIFIER ::= all | constant
3048 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3049 -- access [protected] procedure PARAMETER_PROFILE
3050 -- | access [protected] function PARAMETER_AND_RESULT_PROFILE
3052 -- PARAMETER_PROFILE ::= [FORMAL_PART]
3054 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3056 -- The caller has checked that the initial token is ACCESS
3058 -- Error recovery: can raise Error_Resync
3060 function P_Access_Type_Definition
return Node_Id
is
3061 Prot_Flag
: Boolean;
3062 Access_Loc
: Source_Ptr
;
3063 Type_Def_Node
: Node_Id
;
3065 procedure Check_Junk_Subprogram_Name
;
3066 -- Used in access to subprogram definition cases to check for an
3067 -- identifier or operator symbol that does not belong.
3069 procedure Check_Junk_Subprogram_Name
is
3070 Saved_State
: Saved_Scan_State
;
3073 if Token
= Tok_Identifier
or else Token
= Tok_Operator_Symbol
then
3074 Save_Scan_State
(Saved_State
);
3075 Scan
; -- past possible junk subprogram name
3077 if Token
= Tok_Left_Paren
or else Token
= Tok_Semicolon
then
3078 Error_Msg_SP
("unexpected subprogram name ignored");
3082 Restore_Scan_State
(Saved_State
);
3085 end Check_Junk_Subprogram_Name
;
3087 -- Start of processing for P_Access_Type_Definition
3090 Access_Loc
:= Token_Ptr
;
3091 Scan
; -- past ACCESS
3093 if Token_Name
= Name_Protected
then
3094 Check_95_Keyword
(Tok_Protected
, Tok_Procedure
);
3095 Check_95_Keyword
(Tok_Protected
, Tok_Function
);
3098 Prot_Flag
:= (Token
= Tok_Protected
);
3101 Scan
; -- past PROTECTED
3102 if Token
/= Tok_Procedure
and then Token
/= Tok_Function
then
3103 Error_Msg_SC
("FUNCTION or PROCEDURE expected");
3107 if Token
= Tok_Procedure
then
3109 Error_Msg_SC
("(Ada 83) access to procedure not allowed!");
3112 Type_Def_Node
:= New_Node
(N_Access_Procedure_Definition
, Access_Loc
);
3113 Scan
; -- past PROCEDURE
3114 Check_Junk_Subprogram_Name
;
3115 Set_Parameter_Specifications
(Type_Def_Node
, P_Parameter_Profile
);
3116 Set_Protected_Present
(Type_Def_Node
, Prot_Flag
);
3118 elsif Token
= Tok_Function
then
3120 Error_Msg_SC
("(Ada 83) access to function not allowed!");
3123 Type_Def_Node
:= New_Node
(N_Access_Function_Definition
, Access_Loc
);
3124 Scan
; -- past FUNCTION
3125 Check_Junk_Subprogram_Name
;
3126 Set_Parameter_Specifications
(Type_Def_Node
, P_Parameter_Profile
);
3127 Set_Protected_Present
(Type_Def_Node
, Prot_Flag
);
3129 Set_Subtype_Mark
(Type_Def_Node
, P_Subtype_Mark
);
3134 New_Node
(N_Access_To_Object_Definition
, Access_Loc
);
3136 if Token
= Tok_All
or else Token
= Tok_Constant
then
3138 Error_Msg_SC
("(Ada 83) access modifier not allowed!");
3141 if Token
= Tok_All
then
3142 Set_All_Present
(Type_Def_Node
, True);
3145 Set_Constant_Present
(Type_Def_Node
, True);
3148 Scan
; -- past ALL or CONSTANT
3151 Set_Subtype_Indication
(Type_Def_Node
, P_Subtype_Indication
);
3154 return Type_Def_Node
;
3155 end P_Access_Type_Definition
;
3157 ---------------------------------------
3158 -- 3.10 Access To Object Definition --
3159 ---------------------------------------
3161 -- Parsed by P_Access_Type_Definition (3.10)
3163 -----------------------------------
3164 -- 3.10 General Access Modifier --
3165 -----------------------------------
3167 -- Parsed by P_Access_Type_Definition (3.10)
3169 -------------------------------------------
3170 -- 3.10 Access To Subprogram Definition --
3171 -------------------------------------------
3173 -- Parsed by P_Access_Type_Definition (3.10)
3175 -----------------------------
3176 -- 3.10 Access Definition --
3177 -----------------------------
3179 -- ACCESS_DEFINITION ::= access SUBTYPE_MARK
3181 -- The caller has checked that the initial token is ACCESS
3183 -- Error recovery: cannot raise Error_Resync
3185 function P_Access_Definition
return Node_Id
is
3189 Def_Node
:= New_Node
(N_Access_Definition
, Token_Ptr
);
3190 Scan
; -- past ACCESS
3191 Set_Subtype_Mark
(Def_Node
, P_Subtype_Mark
);
3194 end P_Access_Definition
;
3196 -----------------------------------------
3197 -- 3.10.1 Incomplete Type Declaration --
3198 -----------------------------------------
3200 -- Parsed by P_Type_Declaration (3.2.1)
3202 ----------------------------
3203 -- 3.11 Declarative Part --
3204 ----------------------------
3206 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
3208 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Items
3209 -- handles errors, and returns cleanly after an error has occurred)
3211 function P_Declarative_Part
return List_Id
is
3216 -- Indicate no bad declarations detected yet. This will be reset by
3217 -- P_Declarative_Items if a bad declaration is discovered.
3219 Missing_Begin_Msg
:= No_Error_Msg
;
3221 -- Get rid of active SIS entry from outer scope. This means we will
3222 -- miss some nested cases, but it doesn't seem worth the effort. See
3223 -- discussion in Par for further details
3225 SIS_Entry_Active
:= False;
3228 -- Loop to scan out the declarations
3231 P_Declarative_Items
(Decls
, Done
, In_Spec
=> False);
3235 -- Get rid of active SIS entry which is left set only if we scanned a
3236 -- procedure declaration and have not found the body. We could give
3237 -- an error message, but that really would be usurping the role of
3238 -- semantic analysis (this really is a missing body case).
3240 SIS_Entry_Active
:= False;
3242 end P_Declarative_Part
;
3244 ----------------------------
3245 -- 3.11 Declarative Item --
3246 ----------------------------
3248 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
3250 -- Can return Error if a junk declaration is found, or Empty if no
3251 -- declaration is found (i.e. a token ending declarations, such as
3252 -- BEGIN or END is encountered).
3254 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
3255 -- then the scan is set past the next semicolon and Error is returned.
3257 procedure P_Declarative_Items
3262 Scan_State
: Saved_Scan_State
;
3265 if Style_Check
then Style
.Check_Indentation
; end if;
3269 when Tok_Function
=>
3271 Append
(P_Subprogram
(Pf_Decl_Gins_Pbod_Rnam_Stub
), Decls
);
3277 -- Check for loop (premature statement)
3279 Save_Scan_State
(Scan_State
);
3282 if Token
= Tok_Identifier
then
3283 Scan
; -- past identifier
3285 if Token
= Tok_In
then
3286 Restore_Scan_State
(Scan_State
);
3287 Statement_When_Declaration_Expected
(Decls
, Done
, In_Spec
);
3292 -- Not a loop, so must be rep clause
3294 Restore_Scan_State
(Scan_State
);
3295 Append
(P_Representation_Clause
, Decls
);
3300 Append
(P_Generic
, Decls
);
3303 when Tok_Identifier
=>
3305 P_Identifier_Declarations
(Decls
, Done
, In_Spec
);
3309 Append
(P_Package
(Pf_Decl_Gins_Pbod_Rnam_Stub
), Decls
);
3313 Append
(P_Pragma
, Decls
);
3316 when Tok_Procedure
=>
3318 Append
(P_Subprogram
(Pf_Decl_Gins_Pbod_Rnam_Stub
), Decls
);
3321 when Tok_Protected
=>
3323 Scan
; -- past PROTECTED
3324 Append
(P_Protected
, Decls
);
3329 Append
(P_Subtype_Declaration
, Decls
);
3335 Append
(P_Task
, Decls
);
3340 Append
(P_Type_Declaration
, Decls
);
3345 Append
(P_Use_Clause
, Decls
);
3350 Error_Msg_SC
("WITH can only appear in context clause");
3353 -- BEGIN terminates the scan of a sequence of declarations unless
3354 -- there is a missing subprogram body, see section on handling
3355 -- semicolon in place of IS. We only treat the begin as satisfying
3356 -- the subprogram declaration if it falls in the expected column
3360 if SIS_Entry_Active
and then Start_Column
>= SIS_Ecol
then
3362 -- Here we have the case where a BEGIN is encountered during
3363 -- declarations in a declarative part, or at the outer level,
3364 -- and there is a subprogram declaration outstanding for which
3365 -- no body has been supplied. This is the case where we assume
3366 -- that the semicolon in the subprogram declaration should
3367 -- really have been is. The active SIS entry describes the
3368 -- subprogram declaration. On return the declaration has been
3369 -- modified to become a body.
3372 Specification_Node
: Node_Id
;
3373 Decl_Node
: Node_Id
;
3374 Body_Node
: Node_Id
;
3377 -- First issue the error message. If we had a missing
3378 -- semicolon in the declaration, then change the message
3379 -- to <missing "is">
3381 if SIS_Missing_Semicolon_Message
/= No_Error_Msg
then
3382 Change_Error_Text
-- Replace: "missing "";"" "
3383 (SIS_Missing_Semicolon_Message
, "missing ""is""");
3385 -- Otherwise we saved the semicolon position, so complain
3388 Error_Msg
(""";"" should be IS", SIS_Semicolon_Sloc
);
3391 -- The next job is to fix up any declarations that occurred
3392 -- between the procedure header and the BEGIN. These got
3393 -- chained to the outer declarative region (immediately
3394 -- after the procedure declaration) and they should be
3395 -- chained to the subprogram itself, which is a body
3396 -- rather than a spec.
3398 Specification_Node
:= Specification
(SIS_Declaration_Node
);
3399 Change_Node
(SIS_Declaration_Node
, N_Subprogram_Body
);
3400 Body_Node
:= SIS_Declaration_Node
;
3401 Set_Specification
(Body_Node
, Specification_Node
);
3402 Set_Declarations
(Body_Node
, New_List
);
3405 Decl_Node
:= Remove_Next
(Body_Node
);
3406 exit when Decl_Node
= Empty
;
3407 Append
(Decl_Node
, Declarations
(Body_Node
));
3410 -- Now make the scope table entry for the Begin-End and
3414 Scope
.Table
(Scope
.Last
).Sloc
:= SIS_Sloc
;
3415 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
3416 Scope
.Table
(Scope
.Last
).Ecol
:= SIS_Ecol
;
3417 Scope
.Table
(Scope
.Last
).Labl
:= SIS_Labl
;
3418 Scope
.Table
(Scope
.Last
).Lreq
:= False;
3419 SIS_Entry_Active
:= False;
3421 Set_Handled_Statement_Sequence
(Body_Node
,
3422 P_Handled_Sequence_Of_Statements
);
3423 End_Statements
(Handled_Statement_Sequence
(Body_Node
));
3432 -- Normally an END terminates the scan for basic declarative
3433 -- items. The one exception is END RECORD, which is probably
3434 -- left over from some other junk.
3437 Save_Scan_State
(Scan_State
); -- at END
3440 if Token
= Tok_Record
then
3441 Error_Msg_SP
("no RECORD for this `end record`!");
3442 Scan
; -- past RECORD
3446 Restore_Scan_State
(Scan_State
); -- to END
3450 -- The following tokens which can only be the start of a statement
3451 -- are considered to end a declarative part (i.e. we have a missing
3452 -- BEGIN situation). We are fairly conservative in making this
3453 -- judgment, because it is a real mess to go into statement mode
3454 -- prematurely in response to a junk declaration.
3469 -- But before we decide that it's a statement, let's check for
3470 -- a reserved word misused as an identifier.
3472 if Is_Reserved_Identifier
then
3473 Save_Scan_State
(Scan_State
);
3474 Scan
; -- past the token
3476 -- If reserved identifier not followed by colon or comma, then
3477 -- this is most likely an assignment statement to the bad id.
3479 if Token
/= Tok_Colon
and then Token
/= Tok_Comma
then
3480 Restore_Scan_State
(Scan_State
);
3481 Statement_When_Declaration_Expected
(Decls
, Done
, In_Spec
);
3484 -- Otherwise we have a declaration of the bad id
3487 Restore_Scan_State
(Scan_State
);
3488 Scan_Reserved_Identifier
(Force_Msg
=> True);
3489 P_Identifier_Declarations
(Decls
, Done
, In_Spec
);
3492 -- If not reserved identifier, then it's definitely a statement
3495 Statement_When_Declaration_Expected
(Decls
, Done
, In_Spec
);
3499 -- The token RETURN may well also signal a missing BEGIN situation,
3500 -- however, we never let it end the declarative part, because it may
3501 -- also be part of a half-baked function declaration.
3504 Error_Msg_SC
("misplaced RETURN statement");
3507 -- PRIVATE definitely terminates the declarations in a spec,
3508 -- and is an error in a body.
3514 Error_Msg_SC
("PRIVATE not allowed in body");
3515 Scan
; -- past PRIVATE
3518 -- An end of file definitely terminates the declarations!
3523 -- The remaining tokens do not end the scan, but cannot start a
3524 -- valid declaration, so we signal an error and resynchronize.
3525 -- But first check for misuse of a reserved identifier.
3529 -- Here we check for a reserved identifier
3531 if Is_Reserved_Identifier
then
3532 Save_Scan_State
(Scan_State
);
3533 Scan
; -- past the token
3535 if Token
/= Tok_Colon
and then Token
/= Tok_Comma
then
3536 Restore_Scan_State
(Scan_State
);
3537 Set_Declaration_Expected
;
3540 Restore_Scan_State
(Scan_State
);
3541 Scan_Reserved_Identifier
(Force_Msg
=> True);
3543 P_Identifier_Declarations
(Decls
, Done
, In_Spec
);
3547 Set_Declaration_Expected
;
3552 -- To resynchronize after an error, we scan to the next semicolon and
3553 -- return with Done = False, indicating that there may still be more
3554 -- valid declarations to come.
3557 when Error_Resync
=>
3558 Resync_Past_Semicolon
;
3560 end P_Declarative_Items
;
3562 ----------------------------------
3563 -- 3.11 Basic Declarative Item --
3564 ----------------------------------
3566 -- BASIC_DECLARATIVE_ITEM ::=
3567 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
3569 -- Scan zero or more basic declarative items
3571 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
3572 -- the scan pointer is repositioned past the next semicolon, and the scan
3573 -- for declarative items continues.
3575 function P_Basic_Declarative_Items
return List_Id
is
3582 -- Indicate no bad declarations detected yet in the current context:
3583 -- visible or private declarations of a package spec.
3585 Missing_Begin_Msg
:= No_Error_Msg
;
3587 -- Get rid of active SIS entry from outer scope. This means we will
3588 -- miss some nested cases, but it doesn't seem worth the effort. See
3589 -- discussion in Par for further details
3591 SIS_Entry_Active
:= False;
3593 -- Loop to scan out declarations
3598 P_Declarative_Items
(Decls
, Done
, In_Spec
=> True);
3602 -- Get rid of active SIS entry. This is set only if we have scanned a
3603 -- procedure declaration and have not found the body. We could give
3604 -- an error message, but that really would be usurping the role of
3605 -- semantic analysis (this really is a case of a missing body).
3607 SIS_Entry_Active
:= False;
3609 -- Test for assorted illegal declarations not diagnosed elsewhere.
3611 Decl
:= First
(Decls
);
3613 while Present
(Decl
) loop
3614 Kind
:= Nkind
(Decl
);
3616 -- Test for body scanned, not acceptable as basic decl item
3618 if Kind
= N_Subprogram_Body
or else
3619 Kind
= N_Package_Body
or else
3620 Kind
= N_Task_Body
or else
3621 Kind
= N_Protected_Body
3624 ("proper body not allowed in package spec", Sloc
(Decl
));
3626 -- Test for body stub scanned, not acceptable as basic decl item
3628 elsif Kind
in N_Body_Stub
then
3630 ("body stub not allowed in package spec", Sloc
(Decl
));
3632 elsif Kind
= N_Assignment_Statement
then
3634 ("assignment statement not allowed in package spec",
3642 end P_Basic_Declarative_Items
;
3648 -- For proper body, see below
3649 -- For body stub, see 10.1.3
3651 -----------------------
3652 -- 3.11 Proper Body --
3653 -----------------------
3655 -- Subprogram body is parsed by P_Subprogram (6.1)
3656 -- Package body is parsed by P_Package (7.1)
3657 -- Task body is parsed by P_Task (9.1)
3658 -- Protected body is parsed by P_Protected (9.4)
3660 ------------------------------
3661 -- Set_Declaration_Expected --
3662 ------------------------------
3664 procedure Set_Declaration_Expected
is
3666 Error_Msg_SC
("declaration expected");
3668 if Missing_Begin_Msg
= No_Error_Msg
then
3669 Missing_Begin_Msg
:= Get_Msg_Id
;
3671 end Set_Declaration_Expected
;
3673 ----------------------
3674 -- Skip_Declaration --
3675 ----------------------
3677 procedure Skip_Declaration
(S
: List_Id
) is
3678 Dummy_Done
: Boolean;
3681 P_Declarative_Items
(S
, Dummy_Done
, False);
3682 end Skip_Declaration
;
3684 -----------------------------------------
3685 -- Statement_When_Declaration_Expected --
3686 -----------------------------------------
3688 procedure Statement_When_Declaration_Expected
3694 -- Case of second occurrence of statement in one declaration sequence
3696 if Missing_Begin_Msg
/= No_Error_Msg
then
3698 -- In the procedure spec case, just ignore it, we only give one
3699 -- message for the first occurrence, since otherwise we may get
3700 -- horrible cascading if BODY was missing in the header line.
3705 -- In the declarative part case, take a second statement as a sure
3706 -- sign that we really have a missing BEGIN, and end the declarative
3707 -- part now. Note that the caller will fix up the first message to
3708 -- say "missing BEGIN" so that's how the error will be signalled.
3715 -- Case of first occurrence of unexpected statement
3718 -- If we are in a package spec, then give message of statement
3719 -- not allowed in package spec. This message never gets changed.
3722 Error_Msg_SC
("statement not allowed in package spec");
3724 -- If in declarative part, then we give the message complaining
3725 -- about finding a statement when a declaration is expected. This
3726 -- gets changed to a complaint about a missing BEGIN if we later
3727 -- find that no BEGIN is present.
3730 Error_Msg_SC
("statement not allowed in declarative part");
3733 -- Capture message Id. This is used for two purposes, first to
3734 -- stop multiple messages, see test above, and second, to allow
3735 -- the replacement of the message in the declarative part case.
3737 Missing_Begin_Msg
:= Get_Msg_Id
;
3740 -- In all cases except the case in which we decided to terminate the
3741 -- declaration sequence on a second error, we scan out the statement
3742 -- and append it to the list of declarations (note that the semantics
3743 -- can handle statements in a declaration list so if we proceed to
3744 -- call the semantic phase, all will be (reasonably) well!
3746 Append_List_To
(Decls
, P_Sequence_Of_Statements
(SS_Unco
));
3748 -- Done is set to False, since we want to continue the scan of
3749 -- declarations, hoping that this statement was a temporary glitch.
3750 -- If we indeed are now in the statement part (i.e. this was a missing
3751 -- BEGIN, then it's not terrible, we will simply keep calling this
3752 -- procedure to process the statements one by one, and then finally
3753 -- hit the missing BEGIN, which will clean up the error message.
3756 end Statement_When_Declaration_Expected
;