1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical.
30 with Sinfo.CN; use Sinfo.CN;
40 -----------------------
41 -- Local Subprograms --
42 -----------------------
44 function P_Component_List return Node_Id;
45 function P_Defining_Character_Literal return Node_Id;
46 function P_Delta_Constraint return Node_Id;
47 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id;
48 function P_Digits_Constraint return Node_Id;
49 function P_Discriminant_Association return Node_Id;
50 function P_Enumeration_Literal_Specification return Node_Id;
51 function P_Enumeration_Type_Definition return Node_Id;
52 function P_Fixed_Point_Definition return Node_Id;
53 function P_Floating_Point_Definition return Node_Id;
54 function P_Index_Or_Discriminant_Constraint return Node_Id;
55 function P_Real_Range_Specification_Opt return Node_Id;
56 function P_Subtype_Declaration return Node_Id;
57 function P_Type_Declaration return Node_Id;
58 function P_Modular_Type_Definition return Node_Id;
59 function P_Variant return Node_Id;
60 function P_Variant_Part return Node_Id;
62 procedure Check_Restricted_Expression (N : Node_Id);
63 -- Check that the expression N meets the Restricted_Expression syntax.
64 -- The syntax is as follows:
66 -- RESTRICTED_EXPRESSION ::=
67 -- RESTRICTED_RELATION {and RESTRICTED_RELATION}
68 -- | RESTRICTED_RELATION {and then RESTRICTED_RELATION}
69 -- | RESTRICTED_RELATION {or RESTRICTED_RELATION}
70 -- | RESTRICTED_RELATION {or else RESTRICTED_RELATION}
71 -- | RESTRICTED_RELATION {xor RESTRICTED_RELATION}
73 -- RESTRICTED_RELATION ::=
74 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
76 -- This syntax is used for choices when extensions (and set notations)
77 -- are enabled, to remove the ambiguity of "when X in A | B". We consider
78 -- it very unlikely that this will ever arise in practice.
80 procedure P_Declarative_Items
84 -- Scans out a single declarative item, or, in the case of a declaration
85 -- with a list of identifiers, a list of declarations, one for each of the
86 -- identifiers in the list. The declaration or declarations scanned are
87 -- appended to the given list. Done indicates whether or not there may be
88 -- additional declarative items to scan. If Done is True, then a decision
89 -- has been made that there are no more items to scan. If Done is False,
90 -- then there may be additional declarations to scan. In_Spec is true if
91 -- we are scanning a package declaration, and is used to generate an
92 -- appropriate message if a statement is encountered in such a context.
94 procedure P_Identifier_Declarations
98 -- Scans out a set of declarations for an identifier or list of
99 -- identifiers, and appends them to the given list. The parameters have
100 -- the same significance as for P_Declarative_Items.
102 procedure Statement_When_Declaration_Expected
106 -- Called when a statement is found at a point where a declaration was
107 -- expected. The parameters are as described for P_Declarative_Items.
109 procedure Set_Declaration_Expected;
110 -- Posts a "declaration expected" error messages at the start of the
111 -- current token, and if this is the first such message issued, saves
112 -- the message id in Missing_Begin_Msg, for possible later replacement.
114 ---------------------------------
115 -- Check_Restricted_Expression --
116 ---------------------------------
118 procedure Check_Restricted_Expression (N : Node_Id) is
120 if Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor, N_And_Then, N_Or_Else) then
121 Check_Restricted_Expression (Left_Opnd (N));
122 Check_Restricted_Expression (Right_Opnd (N));
124 elsif Nkind_In (N, N_In, N_Not_In)
125 and then Paren_Count (N) = 0
128 ("|this expression must be parenthesized in Ada 2012 mode!", N);
130 end Check_Restricted_Expression;
136 function Init_Expr_Opt (P : Boolean := False) return Node_Id is
138 -- For colon, assume it means := unless it is at the end of
139 -- a line, in which case guess that it means a semicolon.
141 if Token = Tok_Colon then
142 if Token_Is_At_End_Of_Line then
147 -- Here if := or something that we will take as equivalent
149 elsif Token = Tok_Colon_Equal
150 or else Token = Tok_Equal
151 or else Token = Tok_Is
155 -- Another possibility. If we have a literal followed by a semicolon,
156 -- we assume that we have a missing colon-equal.
158 elsif Token in Token_Class_Literal then
160 Scan_State : Saved_Scan_State;
163 Save_Scan_State (Scan_State);
164 Scan; -- past literal or identifier
166 if Token = Tok_Semicolon then
167 Restore_Scan_State (Scan_State);
169 Restore_Scan_State (Scan_State);
174 -- Otherwise we definitely have no initialization expression
180 -- Merge here if we have an initialization expression
187 return P_Expression_No_Right_Paren;
191 ----------------------------
192 -- 3.1 Basic Declaration --
193 ----------------------------
195 -- Parsed by P_Basic_Declarative_Items (3.9)
197 ------------------------------
198 -- 3.1 Defining Identifier --
199 ------------------------------
201 -- DEFINING_IDENTIFIER ::= IDENTIFIER
203 -- Error recovery: can raise Error_Resync
205 function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
206 Ident_Node : Node_Id;
209 -- Scan out the identifier. Note that this code is essentially identical
210 -- to P_Identifier, except that in the call to Scan_Reserved_Identifier
211 -- we set Force_Msg to True, since we want at least one message for each
212 -- separate declaration (but not use) of a reserved identifier.
214 if Token = Tok_Identifier then
216 -- Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
217 -- OVERRIDING, and SYNCHRONIZED are new reserved words. Note that
218 -- in the case where these keywords are misused in Ada 95 mode,
219 -- this routine will generally not be called at all.
221 if Ada_Version = Ada_95
222 and then Warn_On_Ada_2005_Compatibility
224 if Token_Name = Name_Overriding
225 or else Token_Name = Name_Synchronized
226 or else (Token_Name = Name_Interface
227 and then Prev_Token /= Tok_Pragma)
229 Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
233 -- If we have a reserved identifier, manufacture an identifier with
234 -- a corresponding name after posting an appropriate error message
236 elsif Is_Reserved_Identifier (C) then
237 Scan_Reserved_Identifier (Force_Msg => True);
239 -- Otherwise we have junk that cannot be interpreted as an identifier
242 T_Identifier; -- to give message
246 Ident_Node := Token_Node;
247 Scan; -- past the reserved identifier
249 -- If we already have a defining identifier, clean it out and make
250 -- a new clean identifier. This situation arises in some error cases
251 -- and we need to fix it.
253 if Nkind (Ident_Node) = N_Defining_Identifier then
255 Make_Identifier (Sloc (Ident_Node),
256 Chars => Chars (Ident_Node));
259 -- Change identifier to defining identifier if not in error
261 if Ident_Node /= Error then
262 Change_Identifier_To_Defining_Identifier (Ident_Node);
266 end P_Defining_Identifier;
268 -----------------------------
269 -- 3.2.1 Type Declaration --
270 -----------------------------
272 -- TYPE_DECLARATION ::=
273 -- FULL_TYPE_DECLARATION
274 -- | INCOMPLETE_TYPE_DECLARATION
275 -- | PRIVATE_TYPE_DECLARATION
276 -- | PRIVATE_EXTENSION_DECLARATION
278 -- FULL_TYPE_DECLARATION ::=
279 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION;
280 -- | CONCURRENT_TYPE_DECLARATION
282 -- INCOMPLETE_TYPE_DECLARATION ::=
283 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] [is tagged];
285 -- PRIVATE_TYPE_DECLARATION ::=
286 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
287 -- is [abstract] [tagged] [limited] private;
289 -- PRIVATE_EXTENSION_DECLARATION ::=
290 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
291 -- [abstract] [limited | synchronized]
292 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
295 -- TYPE_DEFINITION ::=
296 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
297 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
298 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
299 -- | DERIVED_TYPE_DEFINITION | INTERFACE_TYPE_DEFINITION
301 -- INTEGER_TYPE_DEFINITION ::=
302 -- SIGNED_INTEGER_TYPE_DEFINITION
303 -- MODULAR_TYPE_DEFINITION
305 -- INTERFACE_TYPE_DEFINITION ::=
306 -- [limited | task | protected | synchronized ] interface
307 -- [and INTERFACE_LIST]
309 -- Error recovery: can raise Error_Resync
311 -- Note: The processing for full type declaration, incomplete type
312 -- declaration, private type declaration and type definition is
313 -- included in this function. The processing for concurrent type
314 -- declarations is NOT here, but rather in chapter 9 (i.e. this
315 -- function handles only declarations starting with TYPE).
317 function P_Type_Declaration return Node_Id is
318 Abstract_Present : Boolean := False;
319 Abstract_Loc : Source_Ptr := No_Location;
321 Discr_List : List_Id;
322 Discr_Sloc : Source_Ptr;
324 Ident_Node : Node_Id;
325 Is_Derived_Iface : Boolean := False;
326 Type_Loc : Source_Ptr;
327 Type_Start_Col : Column_Number;
328 Unknown_Dis : Boolean;
330 Typedef_Node : Node_Id;
331 -- Normally holds type definition, except in the case of a private
332 -- extension declaration, in which case it holds the declaration itself
335 Type_Loc := Token_Ptr;
336 Type_Start_Col := Start_Column;
338 -- If we have TYPE, then proceed ahead and scan identifier
340 if Token = Tok_Type then
341 Type_Token_Location := Type_Loc;
343 Ident_Node := P_Defining_Identifier (C_Is);
345 -- Otherwise this is an error case
349 Type_Token_Location := Type_Loc;
350 Ident_Node := P_Defining_Identifier (C_Is);
353 Discr_Sloc := Token_Ptr;
355 if P_Unknown_Discriminant_Part_Opt then
357 Discr_List := No_List;
359 Unknown_Dis := False;
360 Discr_List := P_Known_Discriminant_Part_Opt;
363 -- Incomplete type declaration. We complete the processing for this
364 -- case here and return the resulting incomplete type declaration node
366 if Token = Tok_Semicolon then
368 Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
369 Set_Defining_Identifier (Decl_Node, Ident_Node);
370 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
371 Set_Discriminant_Specifications (Decl_Node, Discr_List);
378 -- Full type declaration or private type declaration, must have IS
380 if Token = Tok_Equal then
382 Scan; -- past = used in place of IS
384 elsif Token = Tok_Renames then
385 Error_Msg_SC -- CODEFIX
386 ("RENAMES should be IS");
387 Scan; -- past RENAMES used in place of IS
393 -- First an error check, if we have two identifiers in a row, a likely
394 -- possibility is that the first of the identifiers is an incorrectly
397 if Token = Tok_Identifier then
399 SS : Saved_Scan_State;
403 Save_Scan_State (SS);
404 Scan; -- past initial identifier
405 I2 := (Token = Tok_Identifier);
406 Restore_Scan_State (SS);
410 (Bad_Spelling_Of (Tok_Abstract) or else
411 Bad_Spelling_Of (Tok_Access) or else
412 Bad_Spelling_Of (Tok_Aliased) or else
413 Bad_Spelling_Of (Tok_Constant))
420 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
422 if Token_Name = Name_Abstract then
423 Check_95_Keyword (Tok_Abstract, Tok_Tagged);
424 Check_95_Keyword (Tok_Abstract, Tok_New);
427 -- Check cases of misuse of ABSTRACT
429 if Token = Tok_Abstract then
430 Abstract_Present := True;
431 Abstract_Loc := Token_Ptr;
432 Scan; -- past ABSTRACT
434 -- Ada 2005 (AI-419): AARM 3.4 (2/2)
436 if (Ada_Version < Ada_05 and then Token = Tok_Limited)
437 or else Token = Tok_Private
438 or else Token = Tok_Record
439 or else Token = Tok_Null
441 Error_Msg_AP ("TAGGED expected");
445 -- Check for misuse of Ada 95 keyword Tagged
447 if Token_Name = Name_Tagged then
448 Check_95_Keyword (Tok_Tagged, Tok_Private);
449 Check_95_Keyword (Tok_Tagged, Tok_Limited);
450 Check_95_Keyword (Tok_Tagged, Tok_Record);
453 -- Special check for misuse of Aliased
455 if Token = Tok_Aliased or else Token_Name = Name_Aliased then
456 Error_Msg_SC ("ALIASED not allowed in type definition");
457 Scan; -- past ALIASED
460 -- The following processing deals with either a private type declaration
461 -- or a full type declaration. In the private type case, we build the
462 -- N_Private_Type_Declaration node, setting its Tagged_Present and
463 -- Limited_Present flags, on encountering the Private keyword, and
464 -- leave Typedef_Node set to Empty. For the full type declaration
465 -- case, Typedef_Node gets set to the type definition.
467 Typedef_Node := Empty;
469 -- Switch on token following the IS. The loop normally runs once. It
470 -- only runs more than once if an error is detected, to try again after
471 -- detecting and fixing up the error.
477 Tok_Not => -- Ada 2005 (AI-231)
478 Typedef_Node := P_Access_Type_Definition;
483 Typedef_Node := P_Array_Type_Definition;
488 Typedef_Node := P_Fixed_Point_Definition;
493 Typedef_Node := P_Floating_Point_Definition;
500 when Tok_Integer_Literal =>
502 Typedef_Node := P_Signed_Integer_Type_Definition;
507 Typedef_Node := P_Record_Definition;
511 when Tok_Left_Paren =>
512 Typedef_Node := P_Enumeration_Type_Definition;
515 Make_Identifier (Token_Ptr,
516 Chars => Chars (Ident_Node));
517 Set_Comes_From_Source (End_Labl, False);
519 Set_End_Label (Typedef_Node, End_Labl);
524 Typedef_Node := P_Modular_Type_Definition;
529 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
531 if Nkind (Typedef_Node) = N_Derived_Type_Definition
532 and then Present (Record_Extension_Part (Typedef_Node))
535 Make_Identifier (Token_Ptr,
536 Chars => Chars (Ident_Node));
537 Set_Comes_From_Source (End_Labl, False);
540 (Record_Extension_Part (Typedef_Node), End_Labl);
547 Typedef_Node := P_Signed_Integer_Type_Definition;
552 Typedef_Node := P_Record_Definition;
555 Make_Identifier (Token_Ptr,
556 Chars => Chars (Ident_Node));
557 Set_Comes_From_Source (End_Labl, False);
559 Set_End_Label (Typedef_Node, End_Labl);
566 -- Ada 2005 (AI-326): If the words IS TAGGED appear, the type
567 -- is a tagged incomplete type.
569 if Ada_Version >= Ada_05
570 and then Token = Tok_Semicolon
575 New_Node (N_Incomplete_Type_Declaration, Type_Loc);
576 Set_Defining_Identifier (Decl_Node, Ident_Node);
577 Set_Tagged_Present (Decl_Node);
578 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
579 Set_Discriminant_Specifications (Decl_Node, Discr_List);
584 if Token = Tok_Abstract then
585 Error_Msg_SC -- CODEFIX
586 ("ABSTRACT must come before TAGGED");
587 Abstract_Present := True;
588 Abstract_Loc := Token_Ptr;
589 Scan; -- past ABSTRACT
592 if Token = Tok_Limited then
593 Scan; -- past LIMITED
595 -- TAGGED LIMITED PRIVATE case
597 if Token = Tok_Private then
599 New_Node (N_Private_Type_Declaration, Type_Loc);
600 Set_Tagged_Present (Decl_Node, True);
601 Set_Limited_Present (Decl_Node, True);
602 Scan; -- past PRIVATE
604 -- TAGGED LIMITED RECORD
607 Typedef_Node := P_Record_Definition;
608 Set_Tagged_Present (Typedef_Node, True);
609 Set_Limited_Present (Typedef_Node, True);
612 Make_Identifier (Token_Ptr,
613 Chars => Chars (Ident_Node));
614 Set_Comes_From_Source (End_Labl, False);
616 Set_End_Label (Typedef_Node, End_Labl);
622 if Token = Tok_Private then
624 New_Node (N_Private_Type_Declaration, Type_Loc);
625 Set_Tagged_Present (Decl_Node, True);
626 Scan; -- past PRIVATE
631 Typedef_Node := P_Record_Definition;
632 Set_Tagged_Present (Typedef_Node, True);
635 Make_Identifier (Token_Ptr,
636 Chars => Chars (Ident_Node));
637 Set_Comes_From_Source (End_Labl, False);
639 Set_End_Label (Typedef_Node, End_Labl);
647 Scan; -- past LIMITED
650 if Token = Tok_Tagged then
651 Error_Msg_SC -- CODEFIX
652 ("TAGGED must come before LIMITED");
655 elsif Token = Tok_Abstract then
656 Error_Msg_SC -- CODEFIX
657 ("ABSTRACT must come before LIMITED");
658 Scan; -- past ABSTRACT
665 -- LIMITED RECORD or LIMITED NULL RECORD
667 if Token = Tok_Record or else Token = Tok_Null then
668 if Ada_Version = Ada_83 then
670 ("(Ada 83) limited record declaration not allowed!");
672 -- In Ada2005, "abstract limited" can appear before "new",
673 -- but it cannot be part of an untagged record declaration.
675 elsif Abstract_Present
676 and then Prev_Token /= Tok_Tagged
678 Error_Msg_SP ("TAGGED expected");
681 Typedef_Node := P_Record_Definition;
682 Set_Limited_Present (Typedef_Node, True);
684 -- Ada 2005 (AI-251): LIMITED INTERFACE
686 -- If we are compiling in Ada 83 or Ada 95 mode, "interface"
687 -- is not a reserved word but we force its analysis to
688 -- generate the corresponding usage error.
690 elsif Token = Tok_Interface
691 or else (Token = Tok_Identifier
692 and then Chars (Token_Node) = Name_Interface)
695 P_Interface_Type_Definition (Abstract_Present);
696 Abstract_Present := True;
697 Set_Limited_Present (Typedef_Node);
699 if Nkind (Typedef_Node) = N_Derived_Type_Definition then
700 Is_Derived_Iface := True;
703 -- Ada 2005 (AI-419): LIMITED NEW
705 elsif Token = Tok_New then
706 if Ada_Version < Ada_05 then
708 ("LIMITED in derived type is an Ada 2005 extension");
710 ("\unit must be compiled with -gnat05 switch");
713 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
714 Set_Limited_Present (Typedef_Node);
716 if Nkind (Typedef_Node) = N_Derived_Type_Definition
717 and then Present (Record_Extension_Part (Typedef_Node))
720 Make_Identifier (Token_Ptr,
721 Chars => Chars (Ident_Node));
722 Set_Comes_From_Source (End_Labl, False);
725 (Record_Extension_Part (Typedef_Node), End_Labl);
728 -- LIMITED PRIVATE is the only remaining possibility here
731 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
732 Set_Limited_Present (Decl_Node, True);
733 T_Private; -- past PRIVATE (or complain if not there!)
739 -- Here we have an identifier after the IS, which is certainly
740 -- wrong and which might be one of several different mistakes.
742 when Tok_Identifier =>
744 -- First case, if identifier is on same line, then probably we
745 -- have something like "type X is Integer .." and the best
746 -- diagnosis is a missing NEW. Note: the missing new message
747 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
749 if not Token_Is_At_Start_Of_Line then
750 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
753 -- If the identifier is at the start of the line, and is in the
754 -- same column as the type declaration itself then we consider
755 -- that we had a missing type definition on the previous line
757 elsif Start_Column <= Type_Start_Col then
758 Error_Msg_AP ("type definition expected");
759 Typedef_Node := Error;
761 -- If the identifier is at the start of the line, and is in
762 -- a column to the right of the type declaration line, then we
763 -- may have something like:
768 -- and the best diagnosis is a missing record keyword
771 Typedef_Node := P_Record_Definition;
777 -- Ada 2005 (AI-251): INTERFACE
779 when Tok_Interface =>
780 Typedef_Node := P_Interface_Type_Definition (Abstract_Present);
781 Abstract_Present := True;
786 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
787 Scan; -- past PRIVATE
791 -- Ada 2005 (AI-345): Protected, synchronized or task interface
792 -- or Ada 2005 (AI-443): Synchronized private extension.
799 Saved_Token : constant Token_Type := Token;
802 Scan; -- past TASK, PROTECTED or SYNCHRONIZED
804 -- Synchronized private extension
806 if Token = Tok_New then
807 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
809 if Saved_Token = Tok_Synchronized then
810 if Nkind (Typedef_Node) =
811 N_Derived_Type_Definition
814 ("SYNCHRONIZED not allowed for record extension",
817 Set_Synchronized_Present (Typedef_Node);
821 Error_Msg_SC ("invalid kind of private extension");
827 if Token /= Tok_Interface then
828 Error_Msg_SC ("NEW or INTERFACE expected");
832 P_Interface_Type_Definition (Abstract_Present);
833 Abstract_Present := True;
837 Set_Task_Present (Typedef_Node);
839 when Tok_Protected =>
840 Set_Protected_Present (Typedef_Node);
842 when Tok_Synchronized =>
843 Set_Synchronized_Present (Typedef_Node);
846 pragma Assert (False);
855 -- Anything else is an error
858 if Bad_Spelling_Of (Tok_Access)
860 Bad_Spelling_Of (Tok_Array)
862 Bad_Spelling_Of (Tok_Delta)
864 Bad_Spelling_Of (Tok_Digits)
866 Bad_Spelling_Of (Tok_Limited)
868 Bad_Spelling_Of (Tok_Private)
870 Bad_Spelling_Of (Tok_Range)
872 Bad_Spelling_Of (Tok_Record)
874 Bad_Spelling_Of (Tok_Tagged)
879 Error_Msg_AP ("type definition expected");
886 -- For the private type declaration case, the private type declaration
887 -- node has been built, with the Tagged_Present and Limited_Present
888 -- flags set as needed, and Typedef_Node is left set to Empty.
890 if No (Typedef_Node) then
891 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
892 Set_Abstract_Present (Decl_Node, Abstract_Present);
894 -- For a private extension declaration, Typedef_Node contains the
895 -- N_Private_Extension_Declaration node, which we now complete. Note
896 -- that the private extension declaration, unlike a full type
897 -- declaration, does permit unknown discriminants.
899 elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
900 Decl_Node := Typedef_Node;
901 Set_Sloc (Decl_Node, Type_Loc);
902 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
903 Set_Abstract_Present (Typedef_Node, Abstract_Present);
905 -- In the full type declaration case, Typedef_Node has the type
906 -- definition and here is where we build the full type declaration
907 -- node. This is also where we check for improper use of an unknown
908 -- discriminant part (not allowed for full type declaration).
911 if Nkind (Typedef_Node) = N_Record_Definition
912 or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
913 and then Present (Record_Extension_Part (Typedef_Node)))
914 or else Is_Derived_Iface
916 Set_Abstract_Present (Typedef_Node, Abstract_Present);
918 elsif Abstract_Present then
919 Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
922 Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
923 Set_Type_Definition (Decl_Node, Typedef_Node);
927 ("Full type declaration cannot have unknown discriminants",
932 -- Remaining processing is common for all three cases
934 Set_Defining_Identifier (Decl_Node, Ident_Node);
935 Set_Discriminant_Specifications (Decl_Node, Discr_List);
937 end P_Type_Declaration;
939 ----------------------------------
940 -- 3.2.1 Full Type Declaration --
941 ----------------------------------
943 -- Parsed by P_Type_Declaration (3.2.1)
945 ----------------------------
946 -- 3.2.1 Type Definition --
947 ----------------------------
949 -- Parsed by P_Type_Declaration (3.2.1)
951 --------------------------------
952 -- 3.2.2 Subtype Declaration --
953 --------------------------------
955 -- SUBTYPE_DECLARATION ::=
956 -- subtype DEFINING_IDENTIFIER is [NULL_EXCLUSION] SUBTYPE_INDICATION;
958 -- The caller has checked that the initial token is SUBTYPE
960 -- Error recovery: can raise Error_Resync
962 function P_Subtype_Declaration return Node_Id is
964 Not_Null_Present : Boolean := False;
967 Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
968 Scan; -- past SUBTYPE
969 Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
972 if Token = Tok_New then
973 Error_Msg_SC -- CODEFIX
974 ("NEW ignored (only allowed in type declaration)");
978 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
979 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
981 Set_Subtype_Indication
982 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
985 end P_Subtype_Declaration;
987 -------------------------------
988 -- 3.2.2 Subtype Indication --
989 -------------------------------
991 -- SUBTYPE_INDICATION ::=
992 -- [not null] SUBTYPE_MARK [CONSTRAINT]
994 -- Error recovery: can raise Error_Resync
996 function P_Null_Exclusion
997 (Allow_Anonymous_In_95 : Boolean := False) return Boolean
999 Not_Loc : constant Source_Ptr := Token_Ptr;
1000 -- Source position of "not", if present
1003 if Token /= Tok_Not then
1009 if Token = Tok_Null then
1012 -- Ada 2005 (AI-441, AI-447): null_exclusion is illegal in Ada 95,
1013 -- except in the case of anonymous access types.
1015 -- Allow_Anonymous_In_95 will be True if we're parsing a formal
1016 -- parameter or discriminant, which are the only places where
1017 -- anonymous access types occur in Ada 95. "Formal : not null
1018 -- access ..." is legal in Ada 95, whereas "Formal : not null
1019 -- Named_Access_Type" is not.
1021 if Ada_Version >= Ada_05
1022 or else (Ada_Version >= Ada_95
1023 and then Allow_Anonymous_In_95
1024 and then Token = Tok_Access)
1030 ("`NOT NULL` access type is an Ada 2005 extension", Not_Loc);
1032 ("\unit should be compiled with -gnat05 switch", Not_Loc);
1036 Error_Msg_SP ("NULL expected");
1039 if Token = Tok_New then
1040 Error_Msg ("`NOT NULL` comes after NEW, not before", Not_Loc);
1045 end P_Null_Exclusion;
1047 function P_Subtype_Indication
1048 (Not_Null_Present : Boolean := False) return Node_Id
1050 Type_Node : Node_Id;
1053 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
1054 Type_Node := P_Subtype_Mark;
1055 return P_Subtype_Indication (Type_Node, Not_Null_Present);
1058 -- Check for error of using record definition and treat it nicely,
1059 -- otherwise things are really messed up, so resynchronize.
1061 if Token = Tok_Record then
1062 Error_Msg_SC ("anonymous record definitions are not permitted");
1063 Discard_Junk_Node (P_Record_Definition);
1067 Error_Msg_AP ("subtype indication expected");
1071 end P_Subtype_Indication;
1073 -- The following function is identical except that it is called with
1074 -- the subtype mark already scanned out, and it scans out the constraint
1076 -- Error recovery: can raise Error_Resync
1078 function P_Subtype_Indication
1079 (Subtype_Mark : Node_Id;
1080 Not_Null_Present : Boolean := False) return Node_Id
1082 Indic_Node : Node_Id;
1083 Constr_Node : Node_Id;
1086 Constr_Node := P_Constraint_Opt;
1088 if No (Constr_Node) then
1089 return Subtype_Mark;
1091 if Not_Null_Present then
1092 Error_Msg_SP ("`NOT NULL` not allowed if constraint given");
1095 Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
1096 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
1097 Set_Constraint (Indic_Node, Constr_Node);
1100 end P_Subtype_Indication;
1102 -------------------------
1103 -- 3.2.2 Subtype Mark --
1104 -------------------------
1106 -- SUBTYPE_MARK ::= subtype_NAME;
1108 -- Note: The subtype mark which appears after an IN or NOT IN
1109 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
1111 -- Error recovery: cannot raise Error_Resync
1113 function P_Subtype_Mark return Node_Id is
1115 return P_Subtype_Mark_Resync;
1117 when Error_Resync =>
1121 -- This routine differs from P_Subtype_Mark in that it insists that an
1122 -- identifier be present, and if it is not, it raises Error_Resync.
1124 -- Error recovery: can raise Error_Resync
1126 function P_Subtype_Mark_Resync return Node_Id is
1127 Type_Node : Node_Id;
1130 if Token = Tok_Access then
1131 Error_Msg_SC ("anonymous access type definition not allowed here");
1132 Scan; -- past ACCESS
1135 if Token = Tok_Array then
1136 Error_Msg_SC ("anonymous array definition not allowed here");
1137 Discard_Junk_Node (P_Array_Type_Definition);
1141 Type_Node := P_Qualified_Simple_Name_Resync;
1143 -- Check for a subtype mark attribute. The only valid possibilities
1144 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
1145 -- as well catch it here.
1147 if Token = Tok_Apostrophe then
1148 return P_Subtype_Mark_Attribute (Type_Node);
1153 end P_Subtype_Mark_Resync;
1155 -- The following function is called to scan out a subtype mark attribute.
1156 -- The caller has already scanned out the subtype mark, which is passed in
1157 -- as the argument, and has checked that the current token is apostrophe.
1159 -- Only a special subclass of attributes, called type attributes
1160 -- (see Snames package) are allowed in this syntactic position.
1162 -- Note: if the apostrophe is followed by other than an identifier, then
1163 -- the input expression is returned unchanged, and the scan pointer is
1164 -- left pointing to the apostrophe.
1166 -- Error recovery: can raise Error_Resync
1168 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
1169 Attr_Node : Node_Id := Empty;
1170 Scan_State : Saved_Scan_State;
1174 Prefix := Check_Subtype_Mark (Type_Node);
1176 if Prefix = Error then
1180 -- Loop through attributes appearing (more than one can appear as for
1181 -- for example in X'Base'Class). We are at an apostrophe on entry to
1182 -- this loop, and it runs once for each attribute parsed, with
1183 -- Prefix being the current possible prefix if it is an attribute.
1186 Save_Scan_State (Scan_State); -- at Apostrophe
1187 Scan; -- past apostrophe
1189 if Token /= Tok_Identifier then
1190 Restore_Scan_State (Scan_State); -- to apostrophe
1191 return Prefix; -- no attribute after all
1193 elsif not Is_Type_Attribute_Name (Token_Name) then
1195 ("attribute & may not be used in a subtype mark", Token_Node);
1200 Make_Attribute_Reference (Prev_Token_Ptr,
1202 Attribute_Name => Token_Name);
1203 Scan; -- past type attribute identifier
1206 exit when Token /= Tok_Apostrophe;
1207 Prefix := Attr_Node;
1210 -- Fall through here after scanning type attribute
1213 end P_Subtype_Mark_Attribute;
1215 -----------------------
1216 -- 3.2.2 Constraint --
1217 -----------------------
1219 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
1221 -- SCALAR_CONSTRAINT ::=
1222 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
1224 -- COMPOSITE_CONSTRAINT ::=
1225 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
1227 -- If no constraint is present, this function returns Empty
1229 -- Error recovery: can raise Error_Resync
1231 function P_Constraint_Opt return Node_Id is
1233 if Token = Tok_Range
1234 or else Bad_Spelling_Of (Tok_Range)
1236 return P_Range_Constraint;
1238 elsif Token = Tok_Digits
1239 or else Bad_Spelling_Of (Tok_Digits)
1241 return P_Digits_Constraint;
1243 elsif Token = Tok_Delta
1244 or else Bad_Spelling_Of (Tok_Delta)
1246 return P_Delta_Constraint;
1248 elsif Token = Tok_Left_Paren then
1249 return P_Index_Or_Discriminant_Constraint;
1251 elsif Token = Tok_In then
1253 return P_Constraint_Opt;
1258 end P_Constraint_Opt;
1260 ------------------------------
1261 -- 3.2.2 Scalar Constraint --
1262 ------------------------------
1264 -- Parsed by P_Constraint_Opt (3.2.2)
1266 ---------------------------------
1267 -- 3.2.2 Composite Constraint --
1268 ---------------------------------
1270 -- Parsed by P_Constraint_Opt (3.2.2)
1272 --------------------------------------------------------
1273 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
1274 --------------------------------------------------------
1276 -- This routine scans out a declaration starting with an identifier:
1278 -- OBJECT_DECLARATION ::=
1279 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1280 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1281 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1282 -- ACCESS_DEFINITION [:= EXPRESSION];
1283 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1284 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1286 -- NUMBER_DECLARATION ::=
1287 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
1289 -- OBJECT_RENAMING_DECLARATION ::=
1290 -- DEFINING_IDENTIFIER :
1291 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1292 -- | DEFINING_IDENTIFIER :
1293 -- ACCESS_DEFINITION renames object_NAME;
1295 -- EXCEPTION_RENAMING_DECLARATION ::=
1296 -- DEFINING_IDENTIFIER : exception renames exception_NAME;
1298 -- EXCEPTION_DECLARATION ::=
1299 -- DEFINING_IDENTIFIER_LIST : exception;
1301 -- Note that the ALIASED indication in an object declaration is
1302 -- marked by a flag in the parent node.
1304 -- The caller has checked that the initial token is an identifier
1306 -- The value returned is a list of declarations, one for each identifier
1307 -- in the list (as described in Sinfo, we always split up multiple
1308 -- declarations into the equivalent sequence of single declarations
1309 -- using the More_Ids and Prev_Ids flags to preserve the source).
1311 -- If the identifier turns out to be a probable statement rather than
1312 -- an identifier, then the scan is left pointing to the identifier and
1313 -- No_List is returned.
1315 -- Error recovery: can raise Error_Resync
1317 procedure P_Identifier_Declarations
1323 Decl_Node : Node_Id;
1324 Type_Node : Node_Id;
1325 Ident_Sloc : Source_Ptr;
1326 Scan_State : Saved_Scan_State;
1327 List_OK : Boolean := True;
1329 Init_Expr : Node_Id;
1330 Init_Loc : Source_Ptr;
1331 Con_Loc : Source_Ptr;
1332 Not_Null_Present : Boolean := False;
1334 Idents : array (Int range 1 .. 4096) of Entity_Id;
1335 -- Used to save identifiers in the identifier list. The upper bound
1336 -- of 4096 is expected to be infinite in practice, and we do not even
1337 -- bother to check if this upper bound is exceeded.
1339 Num_Idents : Nat := 1;
1340 -- Number of identifiers stored in Idents
1343 -- This procedure is called in renames cases to make sure that we do
1344 -- not have more than one identifier. If we do have more than one
1345 -- then an error message is issued (and the declaration is split into
1346 -- multiple declarations)
1348 function Token_Is_Renames return Boolean;
1349 -- Checks if current token is RENAMES, and if so, scans past it and
1350 -- returns True, otherwise returns False. Includes checking for some
1351 -- common error cases.
1357 procedure No_List is
1359 if Num_Idents > 1 then
1361 ("identifier list not allowed for RENAMES",
1368 ----------------------
1369 -- Token_Is_Renames --
1370 ----------------------
1372 function Token_Is_Renames return Boolean is
1373 At_Colon : Saved_Scan_State;
1376 if Token = Tok_Colon then
1377 Save_Scan_State (At_Colon);
1379 Check_Misspelling_Of (Tok_Renames);
1381 if Token = Tok_Renames then
1382 Error_Msg_SP -- CODEFIX
1383 ("|extra "":"" ignored");
1384 Scan; -- past RENAMES
1387 Restore_Scan_State (At_Colon);
1392 Check_Misspelling_Of (Tok_Renames);
1394 if Token = Tok_Renames then
1395 Scan; -- past RENAMES
1401 end Token_Is_Renames;
1403 -- Start of processing for P_Identifier_Declarations
1406 Ident_Sloc := Token_Ptr;
1407 Save_Scan_State (Scan_State); -- at first identifier
1408 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1410 -- If we have a colon after the identifier, then we can assume that
1411 -- this is in fact a valid identifier declaration and can steam ahead.
1413 if Token = Tok_Colon then
1416 -- If we have a comma, then scan out the list of identifiers
1418 elsif Token = Tok_Comma then
1419 while Comma_Present loop
1420 Num_Idents := Num_Idents + 1;
1421 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1424 Save_Scan_State (Scan_State); -- at colon
1427 -- If we have identifier followed by := then we assume that what is
1428 -- really meant is an assignment statement. The assignment statement
1429 -- is scanned out and added to the list of declarations. An exception
1430 -- occurs if the := is followed by the keyword constant, in which case
1431 -- we assume it was meant to be a colon.
1433 elsif Token = Tok_Colon_Equal then
1436 if Token = Tok_Constant then
1437 Error_Msg_SP ("colon expected");
1440 Restore_Scan_State (Scan_State);
1441 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1445 -- If we have an IS keyword, then assume the TYPE keyword was missing
1447 elsif Token = Tok_Is then
1448 Restore_Scan_State (Scan_State);
1449 Append_To (Decls, P_Type_Declaration);
1453 -- Otherwise we have an error situation
1456 Restore_Scan_State (Scan_State);
1458 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1459 -- so, fix the keyword and return to scan the protected declaration.
1461 if Token_Name = Name_Protected then
1462 Check_95_Keyword (Tok_Protected, Tok_Identifier);
1463 Check_95_Keyword (Tok_Protected, Tok_Type);
1464 Check_95_Keyword (Tok_Protected, Tok_Body);
1466 if Token = Tok_Protected then
1471 -- Check misspelling possibilities. If so, correct the misspelling
1472 -- and return to scan out the resulting declaration.
1474 elsif Bad_Spelling_Of (Tok_Function)
1475 or else Bad_Spelling_Of (Tok_Procedure)
1476 or else Bad_Spelling_Of (Tok_Package)
1477 or else Bad_Spelling_Of (Tok_Pragma)
1478 or else Bad_Spelling_Of (Tok_Protected)
1479 or else Bad_Spelling_Of (Tok_Generic)
1480 or else Bad_Spelling_Of (Tok_Subtype)
1481 or else Bad_Spelling_Of (Tok_Type)
1482 or else Bad_Spelling_Of (Tok_Task)
1483 or else Bad_Spelling_Of (Tok_Use)
1484 or else Bad_Spelling_Of (Tok_For)
1489 -- Otherwise we definitely have an ordinary identifier with a junk
1490 -- token after it. Just complain that we expect a declaration, and
1491 -- skip to a semicolon
1494 Set_Declaration_Expected;
1495 Resync_Past_Semicolon;
1501 -- Come here with an identifier list and colon scanned out. We now
1502 -- build the nodes for the declarative items. One node is built for
1503 -- each identifier in the list, with the type information being
1504 -- repeated by rescanning the appropriate section of source.
1506 -- First an error check, if we have two identifiers in a row, a likely
1507 -- possibility is that the first of the identifiers is an incorrectly
1510 if Token = Tok_Identifier then
1512 SS : Saved_Scan_State;
1516 Save_Scan_State (SS);
1517 Scan; -- past initial identifier
1518 I2 := (Token = Tok_Identifier);
1519 Restore_Scan_State (SS);
1523 (Bad_Spelling_Of (Tok_Access) or else
1524 Bad_Spelling_Of (Tok_Aliased) or else
1525 Bad_Spelling_Of (Tok_Constant))
1532 -- Loop through identifiers
1537 -- Check for some cases of misused Ada 95 keywords
1539 if Token_Name = Name_Aliased then
1540 Check_95_Keyword (Tok_Aliased, Tok_Array);
1541 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1542 Check_95_Keyword (Tok_Aliased, Tok_Constant);
1547 if Token = Tok_Constant then
1548 Con_Loc := Token_Ptr;
1549 Scan; -- past CONSTANT
1551 -- Number declaration, initialization required
1553 Init_Expr := Init_Expr_Opt;
1555 if Present (Init_Expr) then
1556 if Not_Null_Present then
1558 ("`NOT NULL` not allowed in numeric expression");
1561 Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1562 Set_Expression (Decl_Node, Init_Expr);
1564 -- Constant object declaration
1567 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1568 Set_Constant_Present (Decl_Node, True);
1570 if Token_Name = Name_Aliased then
1571 Check_95_Keyword (Tok_Aliased, Tok_Array);
1572 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1575 if Token = Tok_Aliased then
1576 Error_Msg_SC -- CODEFIX
1577 ("ALIASED should be before CONSTANT");
1578 Scan; -- past ALIASED
1579 Set_Aliased_Present (Decl_Node, True);
1582 if Token = Tok_Array then
1583 Set_Object_Definition
1584 (Decl_Node, P_Array_Type_Definition);
1587 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1588 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1590 if Token = Tok_Access then
1591 if Ada_Version < Ada_05 then
1593 ("generalized use of anonymous access types " &
1594 "is an Ada 2005 extension");
1596 ("\unit must be compiled with -gnat05 switch");
1599 Set_Object_Definition
1600 (Decl_Node, P_Access_Definition (Not_Null_Present));
1602 Set_Object_Definition
1603 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1607 if Token = Tok_Renames then
1609 ("CONSTANT not permitted in renaming declaration",
1611 Scan; -- Past renames
1612 Discard_Junk_Node (P_Name);
1618 elsif Token = Tok_Exception then
1619 Scan; -- past EXCEPTION
1621 if Token_Is_Renames then
1624 New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1625 Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1628 Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1631 -- Aliased case (note that an object definition is required)
1633 elsif Token = Tok_Aliased then
1634 Scan; -- past ALIASED
1635 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1636 Set_Aliased_Present (Decl_Node, True);
1638 if Token = Tok_Constant then
1639 Scan; -- past CONSTANT
1640 Set_Constant_Present (Decl_Node, True);
1643 if Token = Tok_Array then
1644 Set_Object_Definition
1645 (Decl_Node, P_Array_Type_Definition);
1648 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1649 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1651 -- Access definition (AI-406) or subtype indication
1653 if Token = Tok_Access then
1654 if Ada_Version < Ada_05 then
1656 ("generalized use of anonymous access types " &
1657 "is an Ada 2005 extension");
1659 ("\unit must be compiled with -gnat05 switch");
1662 Set_Object_Definition
1663 (Decl_Node, P_Access_Definition (Not_Null_Present));
1665 Set_Object_Definition
1666 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1672 elsif Token = Tok_Array then
1673 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1674 Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1676 -- Ada 2005 (AI-254, AI-406)
1678 elsif Token = Tok_Not then
1680 -- OBJECT_DECLARATION ::=
1681 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1682 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1683 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1684 -- ACCESS_DEFINITION [:= EXPRESSION];
1686 -- OBJECT_RENAMING_DECLARATION ::=
1687 -- DEFINING_IDENTIFIER :
1688 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1689 -- | DEFINING_IDENTIFIER :
1690 -- ACCESS_DEFINITION renames object_NAME;
1692 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/423)
1694 if Token = Tok_Access then
1695 if Ada_Version < Ada_05 then
1697 ("generalized use of anonymous access types " &
1698 "is an Ada 2005 extension");
1699 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1702 Acc_Node := P_Access_Definition (Not_Null_Present);
1704 if Token /= Tok_Renames then
1705 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1706 Set_Object_Definition (Decl_Node, Acc_Node);
1709 Scan; -- past renames
1712 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1713 Set_Access_Definition (Decl_Node, Acc_Node);
1714 Set_Name (Decl_Node, P_Name);
1718 Type_Node := P_Subtype_Mark;
1720 -- Object renaming declaration
1722 if Token_Is_Renames then
1723 if Ada_Version < Ada_05 then
1725 ("`NOT NULL` not allowed in object renaming");
1728 -- Ada 2005 (AI-423): Object renaming declaration with
1729 -- a null exclusion.
1734 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1735 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1736 Set_Subtype_Mark (Decl_Node, Type_Node);
1737 Set_Name (Decl_Node, P_Name);
1740 -- Object declaration
1743 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1744 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1745 Set_Object_Definition
1747 P_Subtype_Indication (Type_Node, Not_Null_Present));
1749 -- RENAMES at this point means that we had the combination
1750 -- of a constraint on the Type_Node and renames, which is
1753 if Token_Is_Renames then
1755 ("constraint not allowed in object renaming "
1757 Constraint (Object_Definition (Decl_Node)));
1763 -- Ada 2005 (AI-230): Access Definition case
1765 elsif Token = Tok_Access then
1766 if Ada_Version < Ada_05 then
1768 ("generalized use of anonymous access types " &
1769 "is an Ada 2005 extension");
1770 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1773 Acc_Node := P_Access_Definition (Null_Exclusion_Present => False);
1775 -- Object declaration with access definition, or renaming
1777 if Token /= Tok_Renames then
1778 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1779 Set_Object_Definition (Decl_Node, Acc_Node);
1782 Scan; -- past renames
1785 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1786 Set_Access_Definition (Decl_Node, Acc_Node);
1787 Set_Name (Decl_Node, P_Name);
1790 -- Subtype indication case
1793 Type_Node := P_Subtype_Mark;
1795 -- Object renaming declaration
1797 if Token_Is_Renames then
1800 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1801 Set_Subtype_Mark (Decl_Node, Type_Node);
1802 Set_Name (Decl_Node, P_Name);
1804 -- Object declaration
1807 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1808 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1809 Set_Object_Definition
1811 P_Subtype_Indication (Type_Node, Not_Null_Present));
1813 -- RENAMES at this point means that we had the combination of
1814 -- a constraint on the Type_Node and renames, which is illegal
1816 if Token_Is_Renames then
1818 ("constraint not allowed in object renaming declaration",
1819 Constraint (Object_Definition (Decl_Node)));
1825 -- Scan out initialization, allowed only for object declaration
1827 Init_Loc := Token_Ptr;
1828 Init_Expr := Init_Expr_Opt;
1830 if Present (Init_Expr) then
1831 if Nkind (Decl_Node) = N_Object_Declaration then
1832 Set_Expression (Decl_Node, Init_Expr);
1833 Set_Has_Init_Expression (Decl_Node);
1835 Error_Msg ("initialization not allowed here", Init_Loc);
1840 Set_Defining_Identifier (Decl_Node, Idents (Ident));
1843 if Ident < Num_Idents then
1844 Set_More_Ids (Decl_Node, True);
1848 Set_Prev_Ids (Decl_Node, True);
1852 Append (Decl_Node, Decls);
1853 exit Ident_Loop when Ident = Num_Idents;
1854 Restore_Scan_State (Scan_State);
1857 end loop Ident_Loop;
1860 end P_Identifier_Declarations;
1862 -------------------------------
1863 -- 3.3.1 Object Declaration --
1864 -------------------------------
1866 -- OBJECT DECLARATION ::=
1867 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1868 -- SUBTYPE_INDICATION [:= EXPRESSION];
1869 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1870 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1871 -- | SINGLE_TASK_DECLARATION
1872 -- | SINGLE_PROTECTED_DECLARATION
1874 -- Cases starting with TASK are parsed by P_Task (9.1)
1875 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1876 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1878 -------------------------------------
1879 -- 3.3.1 Defining Identifier List --
1880 -------------------------------------
1882 -- DEFINING_IDENTIFIER_LIST ::=
1883 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1885 -- Always parsed by the construct in which it appears. See special
1886 -- section on "Handling of Defining Identifier Lists" in this unit.
1888 -------------------------------
1889 -- 3.3.2 Number Declaration --
1890 -------------------------------
1892 -- Parsed by P_Identifier_Declarations (3.3)
1894 -------------------------------------------------------------------------
1895 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
1896 -------------------------------------------------------------------------
1898 -- DERIVED_TYPE_DEFINITION ::=
1899 -- [abstract] [limited] new [NULL_EXCLUSION] parent_SUBTYPE_INDICATION
1900 -- [[and INTERFACE_LIST] RECORD_EXTENSION_PART]
1902 -- PRIVATE_EXTENSION_DECLARATION ::=
1903 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1904 -- [abstract] [limited | synchronized]
1905 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
1908 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1910 -- The caller has already scanned out the part up to the NEW, and Token
1911 -- either contains Tok_New (or ought to, if it doesn't this procedure
1912 -- will post an appropriate "NEW expected" message).
1914 -- Note: the caller is responsible for filling in the Sloc field of
1915 -- the returned node in the private extension declaration case as
1916 -- well as the stuff relating to the discriminant part.
1918 -- Error recovery: can raise Error_Resync;
1920 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
1921 Typedef_Node : Node_Id;
1922 Typedecl_Node : Node_Id;
1923 Not_Null_Present : Boolean := False;
1926 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
1928 if Ada_Version < Ada_05
1929 and then Token = Tok_Identifier
1930 and then Token_Name = Name_Interface
1933 ("abstract interface is an Ada 2005 extension");
1934 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1939 if Token = Tok_Abstract then
1940 Error_Msg_SC -- CODEFIX
1941 ("ABSTRACT must come before NEW, not after");
1945 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1946 Set_Null_Exclusion_Present (Typedef_Node, Not_Null_Present);
1947 Set_Subtype_Indication (Typedef_Node,
1948 P_Subtype_Indication (Not_Null_Present));
1950 -- Ada 2005 (AI-251): Deal with interfaces
1952 if Token = Tok_And then
1955 if Ada_Version < Ada_05 then
1957 ("abstract interface is an Ada 2005 extension");
1958 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1961 Set_Interface_List (Typedef_Node, New_List);
1964 Append (P_Qualified_Simple_Name, Interface_List (Typedef_Node));
1965 exit when Token /= Tok_And;
1969 if Token /= Tok_With then
1970 Error_Msg_SC ("WITH expected");
1975 -- Deal with record extension, note that we assume that a WITH is
1976 -- missing in the case of "type X is new Y record ..." or in the
1977 -- case of "type X is new Y null record".
1980 or else Token = Tok_Record
1981 or else Token = Tok_Null
1983 T_With; -- past WITH or give error message
1985 if Token = Tok_Limited then
1986 Error_Msg_SC ("LIMITED keyword not allowed in private extension");
1987 Scan; -- ignore LIMITED
1990 -- Private extension declaration
1992 if Token = Tok_Private then
1993 Scan; -- past PRIVATE
1995 -- Throw away the type definition node and build the type
1996 -- declaration node. Note the caller must set the Sloc,
1997 -- Discriminant_Specifications, Unknown_Discriminants_Present,
1998 -- and Defined_Identifier fields in the returned node.
2001 Make_Private_Extension_Declaration (No_Location,
2002 Defining_Identifier => Empty,
2003 Subtype_Indication => Subtype_Indication (Typedef_Node),
2004 Abstract_Present => Abstract_Present (Typedef_Node),
2005 Interface_List => Interface_List (Typedef_Node));
2007 return Typedecl_Node;
2009 -- Derived type definition with record extension part
2012 Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
2013 return Typedef_Node;
2016 -- Derived type definition with no record extension part
2019 return Typedef_Node;
2021 end P_Derived_Type_Def_Or_Private_Ext_Decl;
2023 ---------------------------
2024 -- 3.5 Range Constraint --
2025 ---------------------------
2027 -- RANGE_CONSTRAINT ::= range RANGE
2029 -- The caller has checked that the initial token is RANGE
2031 -- Error recovery: cannot raise Error_Resync
2033 function P_Range_Constraint return Node_Id is
2034 Range_Node : Node_Id;
2037 Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
2039 Set_Range_Expression (Range_Node, P_Range);
2041 end P_Range_Constraint;
2048 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2050 -- Note: the range that appears in a membership test is parsed by
2051 -- P_Range_Or_Subtype_Mark (3.5).
2053 -- Error recovery: cannot raise Error_Resync
2055 function P_Range return Node_Id is
2056 Expr_Node : Node_Id;
2057 Range_Node : Node_Id;
2060 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2062 if Expr_Form = EF_Range_Attr then
2065 elsif Token = Tok_Dot_Dot then
2066 Range_Node := New_Node (N_Range, Token_Ptr);
2067 Set_Low_Bound (Range_Node, Expr_Node);
2069 Expr_Node := P_Expression;
2070 Check_Simple_Expression (Expr_Node);
2071 Set_High_Bound (Range_Node, Expr_Node);
2074 -- Anything else is an error
2077 T_Dot_Dot; -- force missing .. message
2082 ----------------------------------
2083 -- 3.5 P_Range_Or_Subtype_Mark --
2084 ----------------------------------
2087 -- RANGE_ATTRIBUTE_REFERENCE
2088 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2090 -- This routine scans out the range or subtype mark that forms the right
2091 -- operand of a membership test (it is not used in any other contexts, and
2092 -- error messages are specialized with this knowledge in mind).
2094 -- Note: as documented in the Sinfo interface, although the syntax only
2095 -- allows a subtype mark, we in fact allow any simple expression to be
2096 -- returned from this routine. The semantics is responsible for issuing
2097 -- an appropriate message complaining if the argument is not a name.
2098 -- This simplifies the coding and error recovery processing in the
2099 -- parser, and in any case it is preferable not to consider this a
2100 -- syntax error and to continue with the semantic analysis.
2102 -- Error recovery: cannot raise Error_Resync
2104 function P_Range_Or_Subtype_Mark
2105 (Allow_Simple_Expression : Boolean := False) return Node_Id
2107 Expr_Node : Node_Id;
2108 Range_Node : Node_Id;
2109 Save_Loc : Source_Ptr;
2111 -- Start of processing for P_Range_Or_Subtype_Mark
2114 -- Save location of possible junk parentheses
2116 Save_Loc := Token_Ptr;
2118 -- Scan out either a simple expression or a range (this accepts more
2119 -- than is legal here, but as explained above, we like to allow more
2120 -- with a proper diagnostic, and in the case of a membership operation
2121 -- where sets are allowed, a simple expression is permissible anyway.
2123 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2127 if Expr_Form = EF_Range_Attr then
2130 -- Simple_Expression .. Simple_Expression
2132 elsif Token = Tok_Dot_Dot then
2133 Check_Simple_Expression (Expr_Node);
2134 Range_Node := New_Node (N_Range, Token_Ptr);
2135 Set_Low_Bound (Range_Node, Expr_Node);
2137 Set_High_Bound (Range_Node, P_Simple_Expression);
2140 -- Case of subtype mark (optionally qualified simple name or an
2141 -- attribute whose prefix is an optionally qualified simple name)
2143 elsif Expr_Form = EF_Simple_Name
2144 or else Nkind (Expr_Node) = N_Attribute_Reference
2146 -- Check for error of range constraint after a subtype mark
2148 if Token = Tok_Range then
2149 Error_Msg_SC ("range constraint not allowed in membership test");
2153 -- Check for error of DIGITS or DELTA after a subtype mark
2155 elsif Token = Tok_Digits or else Token = Tok_Delta then
2157 ("accuracy definition not allowed in membership test");
2158 Scan; -- past DIGITS or DELTA
2161 -- Attribute reference, may or may not be OK, but in any case we
2164 elsif Token = Tok_Apostrophe then
2165 return P_Subtype_Mark_Attribute (Expr_Node);
2167 -- OK case of simple name, just return it
2173 -- Simple expression case
2175 elsif Expr_Form = EF_Simple and then Allow_Simple_Expression then
2178 -- Here we have some kind of error situation. Check for junk parens
2179 -- then return what we have, caller will deal with other errors.
2182 if Nkind (Expr_Node) in N_Subexpr
2183 and then Paren_Count (Expr_Node) /= 0
2185 Error_Msg ("|parentheses not allowed for subtype mark", Save_Loc);
2186 Set_Paren_Count (Expr_Node, 0);
2191 end P_Range_Or_Subtype_Mark;
2193 ----------------------------------------
2194 -- 3.5.1 Enumeration Type Definition --
2195 ----------------------------------------
2197 -- ENUMERATION_TYPE_DEFINITION ::=
2198 -- (ENUMERATION_LITERAL_SPECIFICATION
2199 -- {, ENUMERATION_LITERAL_SPECIFICATION})
2201 -- The caller has already scanned out the TYPE keyword
2203 -- Error recovery: can raise Error_Resync;
2205 function P_Enumeration_Type_Definition return Node_Id is
2206 Typedef_Node : Node_Id;
2209 Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
2210 Set_Literals (Typedef_Node, New_List);
2215 Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
2216 exit when not Comma_Present;
2220 return Typedef_Node;
2221 end P_Enumeration_Type_Definition;
2223 ----------------------------------------------
2224 -- 3.5.1 Enumeration Literal Specification --
2225 ----------------------------------------------
2227 -- ENUMERATION_LITERAL_SPECIFICATION ::=
2228 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
2230 -- Error recovery: can raise Error_Resync
2232 function P_Enumeration_Literal_Specification return Node_Id is
2234 if Token = Tok_Char_Literal then
2235 return P_Defining_Character_Literal;
2237 return P_Defining_Identifier (C_Comma_Right_Paren);
2239 end P_Enumeration_Literal_Specification;
2241 ---------------------------------------
2242 -- 3.5.1 Defining_Character_Literal --
2243 ---------------------------------------
2245 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
2247 -- Error recovery: cannot raise Error_Resync
2249 -- The caller has checked that the current token is a character literal
2251 function P_Defining_Character_Literal return Node_Id is
2252 Literal_Node : Node_Id;
2254 Literal_Node := Token_Node;
2255 Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
2256 Scan; -- past character literal
2257 return Literal_Node;
2258 end P_Defining_Character_Literal;
2260 ------------------------------------
2261 -- 3.5.4 Integer Type Definition --
2262 ------------------------------------
2264 -- Parsed by P_Type_Declaration (3.2.1)
2266 -------------------------------------------
2267 -- 3.5.4 Signed Integer Type Definition --
2268 -------------------------------------------
2270 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
2271 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2273 -- Normally the initial token on entry is RANGE, but in some
2274 -- error conditions, the range token was missing and control is
2275 -- passed with Token pointing to first token of the first expression.
2277 -- Error recovery: cannot raise Error_Resync
2279 function P_Signed_Integer_Type_Definition return Node_Id is
2280 Typedef_Node : Node_Id;
2281 Expr_Node : Node_Id;
2284 Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
2286 if Token = Tok_Range then
2290 Expr_Node := P_Expression;
2291 Check_Simple_Expression (Expr_Node);
2292 Set_Low_Bound (Typedef_Node, Expr_Node);
2294 Expr_Node := P_Expression;
2295 Check_Simple_Expression (Expr_Node);
2296 Set_High_Bound (Typedef_Node, Expr_Node);
2297 return Typedef_Node;
2298 end P_Signed_Integer_Type_Definition;
2300 ------------------------------------
2301 -- 3.5.4 Modular Type Definition --
2302 ------------------------------------
2304 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
2306 -- The caller has checked that the initial token is MOD
2308 -- Error recovery: cannot raise Error_Resync
2310 function P_Modular_Type_Definition return Node_Id is
2311 Typedef_Node : Node_Id;
2314 if Ada_Version = Ada_83 then
2315 Error_Msg_SC ("(Ada 83): modular types not allowed");
2318 Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
2320 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2322 -- Handle mod L..R cleanly
2324 if Token = Tok_Dot_Dot then
2325 Error_Msg_SC ("range not allowed for modular type");
2327 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2330 return Typedef_Node;
2331 end P_Modular_Type_Definition;
2333 ---------------------------------
2334 -- 3.5.6 Real Type Definition --
2335 ---------------------------------
2337 -- Parsed by P_Type_Declaration (3.2.1)
2339 --------------------------------------
2340 -- 3.5.7 Floating Point Definition --
2341 --------------------------------------
2343 -- FLOATING_POINT_DEFINITION ::=
2344 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2346 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
2348 -- The caller has checked that the initial token is DIGITS
2350 -- Error recovery: cannot raise Error_Resync
2352 function P_Floating_Point_Definition return Node_Id is
2353 Digits_Loc : constant Source_Ptr := Token_Ptr;
2355 Expr_Node : Node_Id;
2358 Scan; -- past DIGITS
2359 Expr_Node := P_Expression_No_Right_Paren;
2360 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2362 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
2364 if Token = Tok_Delta then
2365 Error_Msg_SC -- CODEFIX
2366 ("|DELTA must come before DIGITS");
2367 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
2369 Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
2371 -- OK floating-point definition
2374 Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
2377 Set_Digits_Expression (Def_Node, Expr_Node);
2378 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2380 end P_Floating_Point_Definition;
2382 -------------------------------------
2383 -- 3.5.7 Real Range Specification --
2384 -------------------------------------
2386 -- REAL_RANGE_SPECIFICATION ::=
2387 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2389 -- Error recovery: cannot raise Error_Resync
2391 function P_Real_Range_Specification_Opt return Node_Id is
2392 Specification_Node : Node_Id;
2393 Expr_Node : Node_Id;
2396 if Token = Tok_Range then
2397 Specification_Node :=
2398 New_Node (N_Real_Range_Specification, Token_Ptr);
2400 Expr_Node := P_Expression_No_Right_Paren;
2401 Check_Simple_Expression (Expr_Node);
2402 Set_Low_Bound (Specification_Node, Expr_Node);
2404 Expr_Node := P_Expression_No_Right_Paren;
2405 Check_Simple_Expression (Expr_Node);
2406 Set_High_Bound (Specification_Node, Expr_Node);
2407 return Specification_Node;
2411 end P_Real_Range_Specification_Opt;
2413 -----------------------------------
2414 -- 3.5.9 Fixed Point Definition --
2415 -----------------------------------
2417 -- FIXED_POINT_DEFINITION ::=
2418 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
2420 -- ORDINARY_FIXED_POINT_DEFINITION ::=
2421 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
2423 -- DECIMAL_FIXED_POINT_DEFINITION ::=
2424 -- delta static_EXPRESSION
2425 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2427 -- The caller has checked that the initial token is DELTA
2429 -- Error recovery: cannot raise Error_Resync
2431 function P_Fixed_Point_Definition return Node_Id is
2432 Delta_Node : Node_Id;
2433 Delta_Loc : Source_Ptr;
2435 Expr_Node : Node_Id;
2438 Delta_Loc := Token_Ptr;
2440 Delta_Node := P_Expression_No_Right_Paren;
2441 Check_Simple_Expression_In_Ada_83 (Delta_Node);
2443 if Token = Tok_Digits then
2444 if Ada_Version = Ada_83 then
2445 Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
2448 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
2449 Scan; -- past DIGITS
2450 Expr_Node := P_Expression_No_Right_Paren;
2451 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2452 Set_Digits_Expression (Def_Node, Expr_Node);
2455 Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
2457 -- Range is required in ordinary fixed point case
2459 if Token /= Tok_Range then
2460 Error_Msg_AP ("range must be given for fixed-point type");
2465 Set_Delta_Expression (Def_Node, Delta_Node);
2466 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2468 end P_Fixed_Point_Definition;
2470 --------------------------------------------
2471 -- 3.5.9 Ordinary Fixed Point Definition --
2472 --------------------------------------------
2474 -- Parsed by P_Fixed_Point_Definition (3.5.9)
2476 -------------------------------------------
2477 -- 3.5.9 Decimal Fixed Point Definition --
2478 -------------------------------------------
2480 -- Parsed by P_Decimal_Point_Definition (3.5.9)
2482 ------------------------------
2483 -- 3.5.9 Digits Constraint --
2484 ------------------------------
2486 -- DIGITS_CONSTRAINT ::=
2487 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
2489 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2491 -- The caller has checked that the initial token is DIGITS
2493 function P_Digits_Constraint return Node_Id is
2494 Constraint_Node : Node_Id;
2495 Expr_Node : Node_Id;
2498 Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
2499 Scan; -- past DIGITS
2500 Expr_Node := P_Expression;
2501 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2502 Set_Digits_Expression (Constraint_Node, Expr_Node);
2504 if Token = Tok_Range then
2505 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2508 return Constraint_Node;
2509 end P_Digits_Constraint;
2511 -----------------------------
2512 -- 3.5.9 Delta Constraint --
2513 -----------------------------
2515 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2517 -- Note: this is an obsolescent feature in Ada 95 (I.3)
2519 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2521 -- The caller has checked that the initial token is DELTA
2523 -- Error recovery: cannot raise Error_Resync
2525 function P_Delta_Constraint return Node_Id is
2526 Constraint_Node : Node_Id;
2527 Expr_Node : Node_Id;
2530 Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2532 Expr_Node := P_Expression;
2533 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2534 Set_Delta_Expression (Constraint_Node, Expr_Node);
2536 if Token = Tok_Range then
2537 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2540 return Constraint_Node;
2541 end P_Delta_Constraint;
2543 --------------------------------
2544 -- 3.6 Array Type Definition --
2545 --------------------------------
2547 -- ARRAY_TYPE_DEFINITION ::=
2548 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2550 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2551 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2552 -- COMPONENT_DEFINITION
2554 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2556 -- CONSTRAINED_ARRAY_DEFINITION ::=
2557 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2558 -- COMPONENT_DEFINITION
2560 -- DISCRETE_SUBTYPE_DEFINITION ::=
2561 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2563 -- COMPONENT_DEFINITION ::=
2564 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
2566 -- The caller has checked that the initial token is ARRAY
2568 -- Error recovery: can raise Error_Resync
2570 function P_Array_Type_Definition return Node_Id is
2571 Array_Loc : Source_Ptr;
2572 CompDef_Node : Node_Id;
2574 Not_Null_Present : Boolean := False;
2575 Subs_List : List_Id;
2576 Scan_State : Saved_Scan_State;
2577 Aliased_Present : Boolean := False;
2580 Array_Loc := Token_Ptr;
2582 Subs_List := New_List;
2585 -- It's quite tricky to disentangle these two possibilities, so we do
2586 -- a prescan to determine which case we have and then reset the scan.
2587 -- The prescan skips past possible subtype mark tokens.
2589 Save_Scan_State (Scan_State); -- just after paren
2591 while Token in Token_Class_Desig or else
2592 Token = Tok_Dot or else
2593 Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2598 -- If we end up on RANGE <> then we have the unconstrained case. We
2599 -- will also allow the RANGE to be omitted, just to improve error
2600 -- handling for a case like array (integer <>) of integer;
2602 Scan; -- past possible RANGE or <>
2604 if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2605 Prev_Token = Tok_Box
2607 Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2608 Restore_Scan_State (Scan_State); -- to first subtype mark
2611 Append (P_Subtype_Mark_Resync, Subs_List);
2614 exit when Token = Tok_Right_Paren or else Token = Tok_Of;
2618 Set_Subtype_Marks (Def_Node, Subs_List);
2621 Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2622 Restore_Scan_State (Scan_State); -- to first discrete range
2625 Append (P_Discrete_Subtype_Definition, Subs_List);
2626 exit when not Comma_Present;
2629 Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2635 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2637 if Token_Name = Name_Aliased then
2638 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2641 if Token = Tok_Aliased then
2642 Aliased_Present := True;
2643 Scan; -- past ALIASED
2646 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
2648 -- Ada 2005 (AI-230): Access Definition case
2650 if Token = Tok_Access then
2651 if Ada_Version < Ada_05 then
2653 ("generalized use of anonymous access types " &
2654 "is an Ada 2005 extension");
2655 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
2658 if Aliased_Present then
2659 Error_Msg_SP ("ALIASED not allowed here");
2662 Set_Subtype_Indication (CompDef_Node, Empty);
2663 Set_Aliased_Present (CompDef_Node, False);
2664 Set_Access_Definition (CompDef_Node,
2665 P_Access_Definition (Not_Null_Present));
2668 Set_Access_Definition (CompDef_Node, Empty);
2669 Set_Aliased_Present (CompDef_Node, Aliased_Present);
2670 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
2671 Set_Subtype_Indication (CompDef_Node,
2672 P_Subtype_Indication (Not_Null_Present));
2675 Set_Component_Definition (Def_Node, CompDef_Node);
2678 end P_Array_Type_Definition;
2680 -----------------------------------------
2681 -- 3.6 Unconstrained Array Definition --
2682 -----------------------------------------
2684 -- Parsed by P_Array_Type_Definition (3.6)
2686 ---------------------------------------
2687 -- 3.6 Constrained Array Definition --
2688 ---------------------------------------
2690 -- Parsed by P_Array_Type_Definition (3.6)
2692 --------------------------------------
2693 -- 3.6 Discrete Subtype Definition --
2694 --------------------------------------
2696 -- DISCRETE_SUBTYPE_DEFINITION ::=
2697 -- discrete_SUBTYPE_INDICATION | RANGE
2699 -- Note: the discrete subtype definition appearing in a constrained
2700 -- array definition is parsed by P_Array_Type_Definition (3.6)
2702 -- Error recovery: cannot raise Error_Resync
2704 function P_Discrete_Subtype_Definition return Node_Id is
2706 -- The syntax of a discrete subtype definition is identical to that
2707 -- of a discrete range, so we simply share the same parsing code.
2709 return P_Discrete_Range;
2710 end P_Discrete_Subtype_Definition;
2712 -------------------------------
2713 -- 3.6 Component Definition --
2714 -------------------------------
2716 -- For the array case, parsed by P_Array_Type_Definition (3.6)
2717 -- For the record case, parsed by P_Component_Declaration (3.8)
2719 -----------------------------
2720 -- 3.6.1 Index Constraint --
2721 -----------------------------
2723 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2725 ---------------------------
2726 -- 3.6.1 Discrete Range --
2727 ---------------------------
2729 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2731 -- The possible forms for a discrete range are:
2733 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
2734 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
2735 -- Range_Attribute (RANGE, 3.5)
2736 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
2738 -- Error recovery: cannot raise Error_Resync
2740 function P_Discrete_Range return Node_Id is
2741 Expr_Node : Node_Id;
2742 Range_Node : Node_Id;
2745 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2747 if Expr_Form = EF_Range_Attr then
2750 elsif Token = Tok_Range then
2751 if Expr_Form /= EF_Simple_Name then
2752 Error_Msg_SC ("range must be preceded by subtype mark");
2755 return P_Subtype_Indication (Expr_Node);
2757 -- Check Expression .. Expression case
2759 elsif Token = Tok_Dot_Dot then
2760 Range_Node := New_Node (N_Range, Token_Ptr);
2761 Set_Low_Bound (Range_Node, Expr_Node);
2763 Expr_Node := P_Expression;
2764 Check_Simple_Expression (Expr_Node);
2765 Set_High_Bound (Range_Node, Expr_Node);
2768 -- Otherwise we must have a subtype mark
2770 elsif Expr_Form = EF_Simple_Name then
2773 -- If incorrect, complain that we expect ..
2779 end P_Discrete_Range;
2781 ----------------------------
2782 -- 3.7 Discriminant Part --
2783 ----------------------------
2785 -- DISCRIMINANT_PART ::=
2786 -- UNKNOWN_DISCRIMINANT_PART
2787 -- | KNOWN_DISCRIMINANT_PART
2789 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2790 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2792 ------------------------------------
2793 -- 3.7 Unknown Discriminant Part --
2794 ------------------------------------
2796 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
2798 -- If no unknown discriminant part is present, then False is returned,
2799 -- otherwise the unknown discriminant is scanned out and True is returned.
2801 -- Error recovery: cannot raise Error_Resync
2803 function P_Unknown_Discriminant_Part_Opt return Boolean is
2804 Scan_State : Saved_Scan_State;
2807 -- If <> right now, then this is missing left paren
2809 if Token = Tok_Box then
2812 -- If not <> or left paren, then definitely no box
2814 elsif Token /= Tok_Left_Paren then
2817 -- Left paren, so might be a box after it
2820 Save_Scan_State (Scan_State);
2821 Scan; -- past the left paren
2823 if Token /= Tok_Box then
2824 Restore_Scan_State (Scan_State);
2829 -- We are now pointing to the box
2831 if Ada_Version = Ada_83 then
2832 Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
2835 Scan; -- past the box
2836 U_Right_Paren; -- must be followed by right paren
2838 end P_Unknown_Discriminant_Part_Opt;
2840 ----------------------------------
2841 -- 3.7 Known Discriminant Part --
2842 ----------------------------------
2844 -- KNOWN_DISCRIMINANT_PART ::=
2845 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2847 -- DISCRIMINANT_SPECIFICATION ::=
2848 -- DEFINING_IDENTIFIER_LIST : [NULL_EXCLUSION] SUBTYPE_MARK
2849 -- [:= DEFAULT_EXPRESSION]
2850 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2851 -- [:= DEFAULT_EXPRESSION]
2853 -- If no known discriminant part is present, then No_List is returned
2855 -- Error recovery: cannot raise Error_Resync
2857 function P_Known_Discriminant_Part_Opt return List_Id is
2858 Specification_Node : Node_Id;
2859 Specification_List : List_Id;
2860 Ident_Sloc : Source_Ptr;
2861 Scan_State : Saved_Scan_State;
2863 Not_Null_Present : Boolean;
2866 Idents : array (Int range 1 .. 4096) of Entity_Id;
2867 -- This array holds the list of defining identifiers. The upper bound
2868 -- of 4096 is intended to be essentially infinite, and we do not even
2869 -- bother to check for it being exceeded.
2872 if Token = Tok_Left_Paren then
2873 Specification_List := New_List;
2875 P_Pragmas_Misplaced;
2877 Specification_Loop : loop
2879 Ident_Sloc := Token_Ptr;
2880 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2883 while Comma_Present loop
2884 Num_Idents := Num_Idents + 1;
2885 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2888 -- If there are multiple identifiers, we repeatedly scan the
2889 -- type and initialization expression information by resetting
2890 -- the scan pointer (so that we get completely separate trees
2891 -- for each occurrence).
2893 if Num_Idents > 1 then
2894 Save_Scan_State (Scan_State);
2899 -- Loop through defining identifiers in list
2903 Specification_Node :=
2904 New_Node (N_Discriminant_Specification, Ident_Sloc);
2905 Set_Defining_Identifier (Specification_Node, Idents (Ident));
2906 Not_Null_Present := -- Ada 2005 (AI-231, AI-447)
2907 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
2909 if Token = Tok_Access then
2910 if Ada_Version = Ada_83 then
2912 ("(Ada 83) access discriminant not allowed!");
2915 Set_Discriminant_Type
2916 (Specification_Node,
2917 P_Access_Definition (Not_Null_Present));
2920 Set_Discriminant_Type
2921 (Specification_Node, P_Subtype_Mark);
2923 Set_Null_Exclusion_Present -- Ada 2005 (AI-231)
2924 (Specification_Node, Not_Null_Present);
2928 (Specification_Node, Init_Expr_Opt (True));
2931 Set_Prev_Ids (Specification_Node, True);
2934 if Ident < Num_Idents then
2935 Set_More_Ids (Specification_Node, True);
2938 Append (Specification_Node, Specification_List);
2939 exit Ident_Loop when Ident = Num_Idents;
2941 Restore_Scan_State (Scan_State);
2943 end loop Ident_Loop;
2945 exit Specification_Loop when Token /= Tok_Semicolon;
2947 P_Pragmas_Misplaced;
2948 end loop Specification_Loop;
2951 return Specification_List;
2956 end P_Known_Discriminant_Part_Opt;
2958 -------------------------------------
2959 -- 3.7 Discriminant Specification --
2960 -------------------------------------
2962 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
2964 -----------------------------
2965 -- 3.7 Default Expression --
2966 -----------------------------
2968 -- Always parsed (simply as an Expression) by the parent construct
2970 ------------------------------------
2971 -- 3.7.1 Discriminant Constraint --
2972 ------------------------------------
2974 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2976 --------------------------------------------------------
2977 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
2978 --------------------------------------------------------
2980 -- DISCRIMINANT_CONSTRAINT ::=
2981 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
2983 -- DISCRIMINANT_ASSOCIATION ::=
2984 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2987 -- This routine parses either an index or a discriminant constraint. As
2988 -- is clear from the above grammar, it is often possible to clearly
2989 -- determine which of the two possibilities we have, but there are
2990 -- cases (those in which we have a series of expressions of the same
2991 -- syntactic form as subtype indications), where we cannot tell. Since
2992 -- this means that in any case the semantic phase has to distinguish
2993 -- between the two, there is not much point in the parser trying to
2994 -- distinguish even those cases where the difference is clear. In any
2995 -- case, if we have a situation like:
2997 -- (A => 123, 235 .. 500)
2999 -- it is not clear which of the two items is the wrong one, better to
3000 -- let the semantic phase give a clear message. Consequently, this
3001 -- routine in general returns a list of items which can be either
3002 -- discrete ranges or discriminant associations.
3004 -- The caller has checked that the initial token is a left paren
3006 -- Error recovery: can raise Error_Resync
3008 function P_Index_Or_Discriminant_Constraint return Node_Id is
3009 Scan_State : Saved_Scan_State;
3010 Constr_Node : Node_Id;
3011 Constr_List : List_Id;
3012 Expr_Node : Node_Id;
3013 Result_Node : Node_Id;
3016 Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
3018 Constr_List := New_List;
3019 Set_Constraints (Result_Node, Constr_List);
3021 -- The two syntactic forms are a little mixed up, so what we are doing
3022 -- here is looking at the first entry to determine which case we have
3024 -- A discriminant constraint is a list of discriminant associations,
3025 -- which have one of the following possible forms:
3029 -- Id | Id | .. | Id => Expression
3031 -- An index constraint is a list of discrete ranges which have one
3032 -- of the following possible forms:
3035 -- Subtype_Mark range Range
3037 -- Simple_Expression .. Simple_Expression
3039 -- Loop through discriminants in list
3042 -- Check cases of Id => Expression or Id | Id => Expression
3044 if Token = Tok_Identifier then
3045 Save_Scan_State (Scan_State); -- at Id
3048 if Token = Tok_Arrow or else Token = Tok_Vertical_Bar then
3049 Restore_Scan_State (Scan_State); -- to Id
3050 Append (P_Discriminant_Association, Constr_List);
3053 Restore_Scan_State (Scan_State); -- to Id
3057 -- Otherwise scan out an expression and see what we have got
3059 Expr_Node := P_Expression_Or_Range_Attribute;
3061 if Expr_Form = EF_Range_Attr then
3062 Append (Expr_Node, Constr_List);
3064 elsif Token = Tok_Range then
3065 if Expr_Form /= EF_Simple_Name then
3066 Error_Msg_SC ("subtype mark required before RANGE");
3069 Append (P_Subtype_Indication (Expr_Node), Constr_List);
3072 -- Check Simple_Expression .. Simple_Expression case
3074 elsif Token = Tok_Dot_Dot then
3075 Check_Simple_Expression (Expr_Node);
3076 Constr_Node := New_Node (N_Range, Token_Ptr);
3077 Set_Low_Bound (Constr_Node, Expr_Node);
3079 Expr_Node := P_Expression;
3080 Check_Simple_Expression (Expr_Node);
3081 Set_High_Bound (Constr_Node, Expr_Node);
3082 Append (Constr_Node, Constr_List);
3085 -- Case of an expression which could be either form
3088 Append (Expr_Node, Constr_List);
3092 -- Here with a single entry scanned
3095 exit when not Comma_Present;
3101 end P_Index_Or_Discriminant_Constraint;
3103 -------------------------------------
3104 -- 3.7.1 Discriminant Association --
3105 -------------------------------------
3107 -- DISCRIMINANT_ASSOCIATION ::=
3108 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3111 -- This routine is used only when the name list is present and the caller
3112 -- has already checked this (by scanning ahead and repositioning the
3115 -- Error_Recovery: cannot raise Error_Resync;
3117 function P_Discriminant_Association return Node_Id is
3118 Discr_Node : Node_Id;
3119 Names_List : List_Id;
3120 Ident_Sloc : Source_Ptr;
3123 Ident_Sloc := Token_Ptr;
3124 Names_List := New_List;
3127 Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
3128 exit when Token /= Tok_Vertical_Bar;
3132 Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
3133 Set_Selector_Names (Discr_Node, Names_List);
3135 Set_Expression (Discr_Node, P_Expression);
3137 end P_Discriminant_Association;
3139 ---------------------------------
3140 -- 3.8 Record Type Definition --
3141 ---------------------------------
3143 -- RECORD_TYPE_DEFINITION ::=
3144 -- [[abstract] tagged] [limited] RECORD_DEFINITION
3146 -- There is no node in the tree for a record type definition. Instead
3147 -- a record definition node appears, with possible Abstract_Present,
3148 -- Tagged_Present, and Limited_Present flags set appropriately.
3150 ----------------------------
3151 -- 3.8 Record Definition --
3152 ----------------------------
3154 -- RECORD_DEFINITION ::=
3160 -- Note: in the case where a record definition node is used to represent
3161 -- a record type definition, the caller sets the Tagged_Present and
3162 -- Limited_Present flags in the resulting N_Record_Definition node as
3165 -- Note that the RECORD token at the start may be missing in certain
3166 -- error situations, so this function is expected to post the error
3168 -- Error recovery: can raise Error_Resync
3170 function P_Record_Definition return Node_Id is
3174 Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
3178 if Token = Tok_Null then
3181 Set_Null_Present (Rec_Node, True);
3183 -- Catch incomplete declaration to prevent cascaded errors, see
3184 -- ACATS B393002 for an example.
3186 elsif Token = Tok_Semicolon then
3187 Error_Msg_AP ("missing record definition");
3189 -- Case starting with RECORD keyword. Build scope stack entry. For the
3190 -- column, we use the first non-blank character on the line, to deal
3191 -- with situations such as:
3197 -- which is not official RM indentation, but is not uncommon usage, and
3198 -- in particular is standard GNAT coding style, so handle it nicely.
3202 Scope.Table (Scope.Last).Etyp := E_Record;
3203 Scope.Table (Scope.Last).Ecol := Start_Column;
3204 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3205 Scope.Table (Scope.Last).Labl := Error;
3206 Scope.Table (Scope.Last).Junk := (Token /= Tok_Record);
3210 Set_Component_List (Rec_Node, P_Component_List);
3213 exit when Check_End;
3214 Discard_Junk_Node (P_Component_List);
3219 end P_Record_Definition;
3221 -------------------------
3222 -- 3.8 Component List --
3223 -------------------------
3225 -- COMPONENT_LIST ::=
3226 -- COMPONENT_ITEM {COMPONENT_ITEM}
3227 -- | {COMPONENT_ITEM} VARIANT_PART
3230 -- Error recovery: cannot raise Error_Resync
3232 function P_Component_List return Node_Id is
3233 Component_List_Node : Node_Id;
3234 Decls_List : List_Id;
3235 Scan_State : Saved_Scan_State;
3238 Component_List_Node := New_Node (N_Component_List, Token_Ptr);
3239 Decls_List := New_List;
3241 if Token = Tok_Null then
3244 P_Pragmas_Opt (Decls_List);
3245 Set_Null_Present (Component_List_Node, True);
3246 return Component_List_Node;
3249 P_Pragmas_Opt (Decls_List);
3251 if Token /= Tok_Case then
3252 Component_Scan_Loop : loop
3253 P_Component_Items (Decls_List);
3254 P_Pragmas_Opt (Decls_List);
3256 exit Component_Scan_Loop when Token = Tok_End
3257 or else Token = Tok_Case
3258 or else Token = Tok_When;
3260 -- We are done if we do not have an identifier. However, if
3261 -- we have a misspelled reserved identifier that is in a column
3262 -- to the right of the record definition, we will treat it as
3263 -- an identifier. It turns out to be too dangerous in practice
3264 -- to accept such a mis-spelled identifier which does not have
3265 -- this additional clue that confirms the incorrect spelling.
3267 if Token /= Tok_Identifier then
3268 if Start_Column > Scope.Table (Scope.Last).Ecol
3269 and then Is_Reserved_Identifier
3271 Save_Scan_State (Scan_State); -- at reserved id
3272 Scan; -- possible reserved id
3274 if Token = Tok_Comma or else Token = Tok_Colon then
3275 Restore_Scan_State (Scan_State);
3276 Scan_Reserved_Identifier (Force_Msg => True);
3278 -- Note reserved identifier used as field name after
3279 -- all because not followed by colon or comma
3282 Restore_Scan_State (Scan_State);
3283 exit Component_Scan_Loop;
3286 -- Non-identifier that definitely was not reserved id
3289 exit Component_Scan_Loop;
3292 end loop Component_Scan_Loop;
3295 if Token = Tok_Case then
3296 Set_Variant_Part (Component_List_Node, P_Variant_Part);
3298 -- Check for junk after variant part
3300 if Token = Tok_Identifier then
3301 Save_Scan_State (Scan_State);
3302 Scan; -- past identifier
3304 if Token = Tok_Colon then
3305 Restore_Scan_State (Scan_State);
3306 Error_Msg_SC ("component may not follow variant part");
3307 Discard_Junk_Node (P_Component_List);
3309 elsif Token = Tok_Case then
3310 Restore_Scan_State (Scan_State);
3311 Error_Msg_SC ("only one variant part allowed in a record");
3312 Discard_Junk_Node (P_Component_List);
3315 Restore_Scan_State (Scan_State);
3321 Set_Component_Items (Component_List_Node, Decls_List);
3322 return Component_List_Node;
3323 end P_Component_List;
3325 -------------------------
3326 -- 3.8 Component Item --
3327 -------------------------
3329 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
3331 -- COMPONENT_DECLARATION ::=
3332 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
3333 -- [:= DEFAULT_EXPRESSION];
3335 -- COMPONENT_DEFINITION ::=
3336 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
3338 -- Error recovery: cannot raise Error_Resync, if an error occurs,
3339 -- the scan is positioned past the following semicolon.
3341 -- Note: we do not yet allow representation clauses to appear as component
3342 -- items, do we need to add this capability sometime in the future ???
3344 procedure P_Component_Items (Decls : List_Id) is
3345 Aliased_Present : Boolean := False;
3346 CompDef_Node : Node_Id;
3347 Decl_Node : Node_Id;
3348 Scan_State : Saved_Scan_State;
3349 Not_Null_Present : Boolean := False;
3352 Ident_Sloc : Source_Ptr;
3354 Idents : array (Int range 1 .. 4096) of Entity_Id;
3355 -- This array holds the list of defining identifiers. The upper bound
3356 -- of 4096 is intended to be essentially infinite, and we do not even
3357 -- bother to check for it being exceeded.
3360 if Token /= Tok_Identifier then
3361 Error_Msg_SC ("component declaration expected");
3362 Resync_Past_Semicolon;
3366 Ident_Sloc := Token_Ptr;
3367 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3370 while Comma_Present loop
3371 Num_Idents := Num_Idents + 1;
3372 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3375 -- If there are multiple identifiers, we repeatedly scan the
3376 -- type and initialization expression information by resetting
3377 -- the scan pointer (so that we get completely separate trees
3378 -- for each occurrence).
3380 if Num_Idents > 1 then
3381 Save_Scan_State (Scan_State);
3386 -- Loop through defining identifiers in list
3391 -- The following block is present to catch Error_Resync
3392 -- which causes the parse to be reset past the semicolon
3395 Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
3396 Set_Defining_Identifier (Decl_Node, Idents (Ident));
3398 if Token = Tok_Constant then
3399 Error_Msg_SC ("constant components are not permitted");
3403 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
3405 if Token_Name = Name_Aliased then
3406 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
3409 if Token = Tok_Aliased then
3410 Aliased_Present := True;
3411 Scan; -- past ALIASED
3414 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
3416 -- Ada 2005 (AI-230): Access Definition case
3418 if Token = Tok_Access then
3419 if Ada_Version < Ada_05 then
3421 ("generalized use of anonymous access types " &
3422 "is an Ada 2005 extension");
3423 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3426 if Aliased_Present then
3427 Error_Msg_SP ("ALIASED not allowed here");
3430 Set_Subtype_Indication (CompDef_Node, Empty);
3431 Set_Aliased_Present (CompDef_Node, False);
3432 Set_Access_Definition (CompDef_Node,
3433 P_Access_Definition (Not_Null_Present));
3436 Set_Access_Definition (CompDef_Node, Empty);
3437 Set_Aliased_Present (CompDef_Node, Aliased_Present);
3438 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
3440 if Token = Tok_Array then
3441 Error_Msg_SC ("anonymous arrays not allowed as components");
3445 Set_Subtype_Indication (CompDef_Node,
3446 P_Subtype_Indication (Not_Null_Present));
3449 Set_Component_Definition (Decl_Node, CompDef_Node);
3450 Set_Expression (Decl_Node, Init_Expr_Opt);
3453 Set_Prev_Ids (Decl_Node, True);
3456 if Ident < Num_Idents then
3457 Set_More_Ids (Decl_Node, True);
3460 Append (Decl_Node, Decls);
3463 when Error_Resync =>
3464 if Token /= Tok_End then
3465 Resync_Past_Semicolon;
3469 exit Ident_Loop when Ident = Num_Idents;
3471 Restore_Scan_State (Scan_State);
3474 end loop Ident_Loop;
3477 end P_Component_Items;
3479 --------------------------------
3480 -- 3.8 Component Declaration --
3481 --------------------------------
3483 -- Parsed by P_Component_Items (3.8)
3485 -------------------------
3486 -- 3.8.1 Variant Part --
3487 -------------------------
3490 -- case discriminant_DIRECT_NAME is
3495 -- The caller has checked that the initial token is CASE
3497 -- Error recovery: cannot raise Error_Resync
3499 function P_Variant_Part return Node_Id is
3500 Variant_Part_Node : Node_Id;
3501 Variants_List : List_Id;
3502 Case_Node : Node_Id;
3505 Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
3507 Scope.Table (Scope.Last).Etyp := E_Case;
3508 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3509 Scope.Table (Scope.Last).Ecol := Start_Column;
3512 Case_Node := P_Expression;
3513 Set_Name (Variant_Part_Node, Case_Node);
3515 if Nkind (Case_Node) /= N_Identifier then
3516 Set_Name (Variant_Part_Node, Error);
3517 Error_Msg ("discriminant name expected", Sloc (Case_Node));
3519 elsif Paren_Count (Case_Node) /= 0 then
3521 ("|discriminant name may not be parenthesized",
3523 Set_Paren_Count (Case_Node, 0);
3527 Variants_List := New_List;
3528 P_Pragmas_Opt (Variants_List);
3530 -- Test missing variant
3532 if Token = Tok_End then
3533 Error_Msg_BC ("WHEN expected (must have at least one variant)");
3535 Append (P_Variant, Variants_List);
3538 -- Loop through variants, note that we allow if in place of when,
3539 -- this error will be detected and handled in P_Variant.
3542 P_Pragmas_Opt (Variants_List);
3544 if Token /= Tok_When
3545 and then Token /= Tok_If
3546 and then Token /= Tok_Others
3548 exit when Check_End;
3551 Append (P_Variant, Variants_List);
3554 Set_Variants (Variant_Part_Node, Variants_List);
3555 return Variant_Part_Node;
3558 --------------------
3560 --------------------
3563 -- when DISCRETE_CHOICE_LIST =>
3566 -- Error recovery: cannot raise Error_Resync
3568 -- The initial token on entry is either WHEN, IF or OTHERS
3570 function P_Variant return Node_Id is
3571 Variant_Node : Node_Id;
3574 -- Special check to recover nicely from use of IF in place of WHEN
3576 if Token = Tok_If then
3583 Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3584 Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3586 Set_Component_List (Variant_Node, P_Component_List);
3587 return Variant_Node;
3590 ---------------------------------
3591 -- 3.8.1 Discrete Choice List --
3592 ---------------------------------
3594 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
3596 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
3598 -- Note: in Ada 83, the expression must be a simple expression
3600 -- Error recovery: cannot raise Error_Resync
3602 function P_Discrete_Choice_List return List_Id is
3604 Expr_Node : Node_Id;
3605 Choice_Node : Node_Id;
3608 Choices := New_List;
3610 if Token = Tok_Others then
3611 Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
3612 Scan; -- past OTHERS
3616 -- Scan out expression or range attribute
3618 Expr_Node := P_Expression_Or_Range_Attribute;
3619 Ignore (Tok_Right_Paren);
3621 if Token = Tok_Colon
3622 and then Nkind (Expr_Node) = N_Identifier
3624 Error_Msg_SP ("label not permitted in this context");
3629 elsif Expr_Form = EF_Range_Attr then
3630 Append (Expr_Node, Choices);
3634 elsif Token = Tok_Dot_Dot then
3635 Check_Simple_Expression (Expr_Node);
3636 Choice_Node := New_Node (N_Range, Token_Ptr);
3637 Set_Low_Bound (Choice_Node, Expr_Node);
3639 Expr_Node := P_Expression_No_Right_Paren;
3640 Check_Simple_Expression (Expr_Node);
3641 Set_High_Bound (Choice_Node, Expr_Node);
3642 Append (Choice_Node, Choices);
3644 -- Simple name, must be subtype, so range allowed
3646 elsif Expr_Form = EF_Simple_Name then
3647 if Token = Tok_Range then
3648 Append (P_Subtype_Indication (Expr_Node), Choices);
3650 elsif Token in Token_Class_Consk then
3652 ("the only constraint allowed here " &
3653 "is a range constraint");
3654 Discard_Junk_Node (P_Constraint_Opt);
3655 Append (Expr_Node, Choices);
3658 Append (Expr_Node, Choices);
3664 -- In Ada 2012 mode, the expression must be a simple
3665 -- expression. The resaon for this restriction (i.e. going
3666 -- back to the Ada 83 rule) is to avoid ambiguities when set
3667 -- membership operations are allowed, consider the
3670 -- when A in 1 .. 10 | 12 =>
3672 -- This is ambiguous without parentheses, so we require one
3673 -- of the following two parenthesized forms to disambuguate:
3675 -- one of the following:
3677 -- when (A in 1 .. 10 | 12) =>
3678 -- when (A in 1 .. 10) | 12 =>
3680 -- To solve this, in Ada 2012 mode, we disallow the use of
3681 -- membership operations in expressions in choices.
3683 -- Technically in the grammar, the expression must match the
3684 -- grammar for restricted expression.
3686 if Ada_Version >= Ada_2012 then
3687 Check_Restricted_Expression (Expr_Node);
3689 -- In Ada 83 mode, the syntax required a simple expression
3692 Check_Simple_Expression_In_Ada_83 (Expr_Node);
3695 Append (Expr_Node, Choices);
3699 when Error_Resync =>
3705 if Token = Tok_Comma then
3706 Error_Msg_SC -- CODEFIX
3707 (""","" should be ""'|""");
3709 exit when Token /= Tok_Vertical_Bar;
3712 Scan; -- past | or comma
3716 end P_Discrete_Choice_List;
3718 ----------------------------
3719 -- 3.8.1 Discrete Choice --
3720 ----------------------------
3722 -- Parsed by P_Discrete_Choice_List (3.8.1)
3724 ----------------------------------
3725 -- 3.9.1 Record Extension Part --
3726 ----------------------------------
3728 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3730 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3732 --------------------------------------
3733 -- 3.9.4 Interface Type Definition --
3734 --------------------------------------
3736 -- INTERFACE_TYPE_DEFINITION ::=
3737 -- [limited | task | protected | synchronized] interface
3738 -- [and INTERFACE_LIST]
3740 -- Error recovery: cannot raise Error_Resync
3742 function P_Interface_Type_Definition
3743 (Abstract_Present : Boolean) return Node_Id
3745 Typedef_Node : Node_Id;
3748 if Ada_Version < Ada_05 then
3749 Error_Msg_SP ("abstract interface is an Ada 2005 extension");
3750 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3753 if Abstract_Present then
3755 ("ABSTRACT not allowed in interface type definition " &
3759 Scan; -- past INTERFACE
3761 -- Ada 2005 (AI-345): In case of interfaces with a null list of
3762 -- interfaces we build a record_definition node.
3764 if Token = Tok_Semicolon then
3765 Typedef_Node := New_Node (N_Record_Definition, Token_Ptr);
3767 Set_Abstract_Present (Typedef_Node);
3768 Set_Tagged_Present (Typedef_Node);
3769 Set_Null_Present (Typedef_Node);
3770 Set_Interface_Present (Typedef_Node);
3772 -- Ada 2005 (AI-251): In case of not-synchronized interfaces that have
3773 -- a list of interfaces we build a derived_type_definition node. This
3774 -- simplifies the semantic analysis (and hence further maintenance)
3777 if Token /= Tok_And then
3778 Error_Msg_AP ("AND expected");
3783 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
3785 Set_Abstract_Present (Typedef_Node);
3786 Set_Interface_Present (Typedef_Node);
3787 Set_Subtype_Indication (Typedef_Node, P_Qualified_Simple_Name);
3789 Set_Record_Extension_Part (Typedef_Node,
3790 New_Node (N_Record_Definition, Token_Ptr));
3791 Set_Null_Present (Record_Extension_Part (Typedef_Node));
3793 if Token = Tok_And then
3794 Set_Interface_List (Typedef_Node, New_List);
3798 Append (P_Qualified_Simple_Name,
3799 Interface_List (Typedef_Node));
3800 exit when Token /= Tok_And;
3806 return Typedef_Node;
3807 end P_Interface_Type_Definition;
3809 ----------------------------------
3810 -- 3.10 Access Type Definition --
3811 ----------------------------------
3813 -- ACCESS_TYPE_DEFINITION ::=
3814 -- ACCESS_TO_OBJECT_DEFINITION
3815 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3817 -- ACCESS_TO_OBJECT_DEFINITION ::=
3818 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3820 -- GENERAL_ACCESS_MODIFIER ::= all | constant
3822 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3823 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3824 -- | [NULL_EXCLUSION] access [protected] function
3825 -- PARAMETER_AND_RESULT_PROFILE
3827 -- PARAMETER_PROFILE ::= [FORMAL_PART]
3829 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3831 -- Ada 2005 (AI-254): If Header_Already_Parsed then the caller has already
3832 -- parsed the null_exclusion part and has also removed the ACCESS token;
3833 -- otherwise the caller has just checked that the initial token is ACCESS
3835 -- Error recovery: can raise Error_Resync
3837 function P_Access_Type_Definition
3838 (Header_Already_Parsed : Boolean := False) return Node_Id
3840 Access_Loc : constant Source_Ptr := Token_Ptr;
3841 Prot_Flag : Boolean;
3842 Not_Null_Present : Boolean := False;
3843 Type_Def_Node : Node_Id;
3844 Result_Not_Null : Boolean;
3845 Result_Node : Node_Id;
3847 procedure Check_Junk_Subprogram_Name;
3848 -- Used in access to subprogram definition cases to check for an
3849 -- identifier or operator symbol that does not belong.
3851 --------------------------------
3852 -- Check_Junk_Subprogram_Name --
3853 --------------------------------
3855 procedure Check_Junk_Subprogram_Name is
3856 Saved_State : Saved_Scan_State;
3859 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
3860 Save_Scan_State (Saved_State);
3861 Scan; -- past possible junk subprogram name
3863 if Token = Tok_Left_Paren or else Token = Tok_Semicolon then
3864 Error_Msg_SP ("unexpected subprogram name ignored");
3868 Restore_Scan_State (Saved_State);
3871 end Check_Junk_Subprogram_Name;
3873 -- Start of processing for P_Access_Type_Definition
3876 if not Header_Already_Parsed then
3877 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
3878 Scan; -- past ACCESS
3881 if Token_Name = Name_Protected then
3882 Check_95_Keyword (Tok_Protected, Tok_Procedure);
3883 Check_95_Keyword (Tok_Protected, Tok_Function);
3886 Prot_Flag := (Token = Tok_Protected);
3889 Scan; -- past PROTECTED
3891 if Token /= Tok_Procedure and then Token /= Tok_Function then
3892 Error_Msg_SC -- CODEFIX
3893 ("FUNCTION or PROCEDURE expected");
3897 if Token = Tok_Procedure then
3898 if Ada_Version = Ada_83 then
3899 Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
3902 Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
3903 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3904 Scan; -- past PROCEDURE
3905 Check_Junk_Subprogram_Name;
3906 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3907 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3909 elsif Token = Tok_Function then
3910 if Ada_Version = Ada_83 then
3911 Error_Msg_SC ("(Ada 83) access to function not allowed!");
3914 Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
3915 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3916 Scan; -- past FUNCTION
3917 Check_Junk_Subprogram_Name;
3918 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3919 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3922 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
3924 -- Ada 2005 (AI-318-02)
3926 if Token = Tok_Access then
3927 if Ada_Version < Ada_05 then
3929 ("anonymous access result type is an Ada 2005 extension");
3930 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
3933 Result_Node := P_Access_Definition (Result_Not_Null);
3936 Result_Node := P_Subtype_Mark;
3939 -- A null exclusion on the result type must be recorded in a flag
3940 -- distinct from the one used for the access-to-subprogram type's
3943 Set_Null_Exclusion_In_Return_Present
3944 (Type_Def_Node, Result_Not_Null);
3947 Set_Result_Definition (Type_Def_Node, Result_Node);
3951 New_Node (N_Access_To_Object_Definition, Access_Loc);
3952 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3954 if Token = Tok_All or else Token = Tok_Constant then
3955 if Ada_Version = Ada_83 then
3956 Error_Msg_SC ("(Ada 83) access modifier not allowed!");
3959 if Token = Tok_All then
3960 Set_All_Present (Type_Def_Node, True);
3963 Set_Constant_Present (Type_Def_Node, True);
3966 Scan; -- past ALL or CONSTANT
3969 Set_Subtype_Indication (Type_Def_Node,
3970 P_Subtype_Indication (Not_Null_Present));
3973 return Type_Def_Node;
3974 end P_Access_Type_Definition;
3976 ---------------------------------------
3977 -- 3.10 Access To Object Definition --
3978 ---------------------------------------
3980 -- Parsed by P_Access_Type_Definition (3.10)
3982 -----------------------------------
3983 -- 3.10 General Access Modifier --
3984 -----------------------------------
3986 -- Parsed by P_Access_Type_Definition (3.10)
3988 -------------------------------------------
3989 -- 3.10 Access To Subprogram Definition --
3990 -------------------------------------------
3992 -- Parsed by P_Access_Type_Definition (3.10)
3994 -----------------------------
3995 -- 3.10 Access Definition --
3996 -----------------------------
3998 -- ACCESS_DEFINITION ::=
3999 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4000 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
4002 -- ACCESS_TO_SUBPROGRAM_DEFINITION
4003 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
4004 -- | [NULL_EXCLUSION] access [protected] function
4005 -- PARAMETER_AND_RESULT_PROFILE
4007 -- The caller has parsed the null-exclusion part and it has also checked
4008 -- that the next token is ACCESS
4010 -- Error recovery: cannot raise Error_Resync
4012 function P_Access_Definition
4013 (Null_Exclusion_Present : Boolean) return Node_Id
4016 Subp_Node : Node_Id;
4019 Def_Node := New_Node (N_Access_Definition, Token_Ptr);
4020 Scan; -- past ACCESS
4022 -- Ada 2005 (AI-254): Access_To_Subprogram_Definition
4024 if Token = Tok_Protected
4025 or else Token = Tok_Procedure
4026 or else Token = Tok_Function
4028 if Ada_Version < Ada_05 then
4029 Error_Msg_SP ("access-to-subprogram is an Ada 2005 extension");
4030 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
4033 Subp_Node := P_Access_Type_Definition (Header_Already_Parsed => True);
4034 Set_Null_Exclusion_Present (Subp_Node, Null_Exclusion_Present);
4035 Set_Access_To_Subprogram_Definition (Def_Node, Subp_Node);
4037 -- Ada 2005 (AI-231)
4038 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4041 Set_Null_Exclusion_Present (Def_Node, Null_Exclusion_Present);
4043 if Token = Tok_All then
4044 if Ada_Version < Ada_05 then
4046 ("ALL is not permitted for anonymous access types");
4050 Set_All_Present (Def_Node);
4052 elsif Token = Tok_Constant then
4053 if Ada_Version < Ada_05 then
4054 Error_Msg_SP ("access-to-constant is an Ada 2005 extension");
4055 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
4058 Scan; -- past CONSTANT
4059 Set_Constant_Present (Def_Node);
4062 Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
4067 end P_Access_Definition;
4069 -----------------------------------------
4070 -- 3.10.1 Incomplete Type Declaration --
4071 -----------------------------------------
4073 -- Parsed by P_Type_Declaration (3.2.1)
4075 ----------------------------
4076 -- 3.11 Declarative Part --
4077 ----------------------------
4079 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
4081 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Items
4082 -- handles errors, and returns cleanly after an error has occurred)
4084 function P_Declarative_Part return List_Id is
4089 -- Indicate no bad declarations detected yet. This will be reset by
4090 -- P_Declarative_Items if a bad declaration is discovered.
4092 Missing_Begin_Msg := No_Error_Msg;
4094 -- Get rid of active SIS entry from outer scope. This means we will
4095 -- miss some nested cases, but it doesn't seem worth the effort. See
4096 -- discussion in Par for further details
4098 SIS_Entry_Active := False;
4101 -- Loop to scan out the declarations
4104 P_Declarative_Items (Decls, Done, In_Spec => False);
4108 -- Get rid of active SIS entry which is left set only if we scanned a
4109 -- procedure declaration and have not found the body. We could give
4110 -- an error message, but that really would be usurping the role of
4111 -- semantic analysis (this really is a missing body case).
4113 SIS_Entry_Active := False;
4115 end P_Declarative_Part;
4117 ----------------------------
4118 -- 3.11 Declarative Item --
4119 ----------------------------
4121 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
4123 -- Can return Error if a junk declaration is found, or Empty if no
4124 -- declaration is found (i.e. a token ending declarations, such as
4125 -- BEGIN or END is encountered).
4127 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
4128 -- then the scan is set past the next semicolon and Error is returned.
4130 procedure P_Declarative_Items
4135 Scan_State : Saved_Scan_State;
4139 Style.Check_Indentation;
4144 when Tok_Function =>
4146 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4152 -- Check for loop (premature statement)
4154 Save_Scan_State (Scan_State);
4157 if Token = Tok_Identifier then
4158 Scan; -- past identifier
4160 if Token = Tok_In then
4161 Restore_Scan_State (Scan_State);
4162 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4167 -- Not a loop, so must be rep clause
4169 Restore_Scan_State (Scan_State);
4170 Append (P_Representation_Clause, Decls);
4175 Append (P_Generic, Decls);
4178 when Tok_Identifier =>
4181 -- Special check for misuse of overriding not in Ada 2005 mode
4183 if Token_Name = Name_Overriding
4184 and then not Next_Token_Is (Tok_Colon)
4186 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
4187 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
4189 Token := Tok_Overriding;
4190 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4193 -- Normal case, no overriding, or overriding followed by colon
4196 P_Identifier_Declarations (Decls, Done, In_Spec);
4199 -- Ada2005: A subprogram declaration can start with "not" or
4200 -- "overriding". In older versions, "overriding" is handled
4201 -- like an identifier, with the appropriate messages.
4205 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4208 when Tok_Overriding =>
4210 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4215 Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4219 Append (P_Pragma, Decls);
4222 when Tok_Procedure =>
4224 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4227 when Tok_Protected =>
4229 Scan; -- past PROTECTED
4230 Append (P_Protected, Decls);
4235 Append (P_Subtype_Declaration, Decls);
4241 Append (P_Task, Decls);
4246 Append (P_Type_Declaration, Decls);
4251 Append (P_Use_Clause, Decls);
4256 Error_Msg_SC ("WITH can only appear in context clause");
4259 -- BEGIN terminates the scan of a sequence of declarations unless
4260 -- there is a missing subprogram body, see section on handling
4261 -- semicolon in place of IS. We only treat the begin as satisfying
4262 -- the subprogram declaration if it falls in the expected column
4266 if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
4268 -- Here we have the case where a BEGIN is encountered during
4269 -- declarations in a declarative part, or at the outer level,
4270 -- and there is a subprogram declaration outstanding for which
4271 -- no body has been supplied. This is the case where we assume
4272 -- that the semicolon in the subprogram declaration should
4273 -- really have been is. The active SIS entry describes the
4274 -- subprogram declaration. On return the declaration has been
4275 -- modified to become a body.
4278 Specification_Node : Node_Id;
4279 Decl_Node : Node_Id;
4280 Body_Node : Node_Id;
4283 -- First issue the error message. If we had a missing
4284 -- semicolon in the declaration, then change the message
4285 -- to <missing "is">
4287 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
4288 Change_Error_Text -- Replace: "missing "";"" "
4289 (SIS_Missing_Semicolon_Message, "missing ""is""");
4291 -- Otherwise we saved the semicolon position, so complain
4294 Error_Msg -- CODEFIX
4295 ("|"";"" should be IS", SIS_Semicolon_Sloc);
4298 -- The next job is to fix up any declarations that occurred
4299 -- between the procedure header and the BEGIN. These got
4300 -- chained to the outer declarative region (immediately
4301 -- after the procedure declaration) and they should be
4302 -- chained to the subprogram itself, which is a body
4303 -- rather than a spec.
4305 Specification_Node := Specification (SIS_Declaration_Node);
4306 Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
4307 Body_Node := SIS_Declaration_Node;
4308 Set_Specification (Body_Node, Specification_Node);
4309 Set_Declarations (Body_Node, New_List);
4312 Decl_Node := Remove_Next (Body_Node);
4313 exit when Decl_Node = Empty;
4314 Append (Decl_Node, Declarations (Body_Node));
4317 -- Now make the scope table entry for the Begin-End and
4321 Scope.Table (Scope.Last).Sloc := SIS_Sloc;
4322 Scope.Table (Scope.Last).Etyp := E_Name;
4323 Scope.Table (Scope.Last).Ecol := SIS_Ecol;
4324 Scope.Table (Scope.Last).Labl := SIS_Labl;
4325 Scope.Table (Scope.Last).Lreq := False;
4326 SIS_Entry_Active := False;
4328 Set_Handled_Statement_Sequence (Body_Node,
4329 P_Handled_Sequence_Of_Statements);
4330 End_Statements (Handled_Statement_Sequence (Body_Node));
4339 -- Normally an END terminates the scan for basic declarative items.
4340 -- The one exception is END RECORD, which is probably left over from
4344 Save_Scan_State (Scan_State); -- at END
4347 if Token = Tok_Record then
4348 Error_Msg_SP ("no RECORD for this `end record`!");
4349 Scan; -- past RECORD
4353 Restore_Scan_State (Scan_State); -- to END
4357 -- The following tokens which can only be the start of a statement
4358 -- are considered to end a declarative part (i.e. we have a missing
4359 -- BEGIN situation). We are fairly conservative in making this
4360 -- judgment, because it is a real mess to go into statement mode
4361 -- prematurely in response to a junk declaration.
4376 -- But before we decide that it's a statement, let's check for
4377 -- a reserved word misused as an identifier.
4379 if Is_Reserved_Identifier then
4380 Save_Scan_State (Scan_State);
4381 Scan; -- past the token
4383 -- If reserved identifier not followed by colon or comma, then
4384 -- this is most likely an assignment statement to the bad id.
4386 if Token /= Tok_Colon and then Token /= Tok_Comma then
4387 Restore_Scan_State (Scan_State);
4388 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4391 -- Otherwise we have a declaration of the bad id
4394 Restore_Scan_State (Scan_State);
4395 Scan_Reserved_Identifier (Force_Msg => True);
4396 P_Identifier_Declarations (Decls, Done, In_Spec);
4399 -- If not reserved identifier, then it's definitely a statement
4402 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4406 -- The token RETURN may well also signal a missing BEGIN situation,
4407 -- however, we never let it end the declarative part, because it may
4408 -- also be part of a half-baked function declaration.
4411 Error_Msg_SC ("misplaced RETURN statement");
4414 -- PRIVATE definitely terminates the declarations in a spec,
4415 -- and is an error in a body.
4421 Error_Msg_SC ("PRIVATE not allowed in body");
4422 Scan; -- past PRIVATE
4425 -- An end of file definitely terminates the declarations!
4430 -- The remaining tokens do not end the scan, but cannot start a
4431 -- valid declaration, so we signal an error and resynchronize.
4432 -- But first check for misuse of a reserved identifier.
4436 -- Here we check for a reserved identifier
4438 if Is_Reserved_Identifier then
4439 Save_Scan_State (Scan_State);
4440 Scan; -- past the token
4442 if Token /= Tok_Colon and then Token /= Tok_Comma then
4443 Restore_Scan_State (Scan_State);
4444 Set_Declaration_Expected;
4447 Restore_Scan_State (Scan_State);
4448 Scan_Reserved_Identifier (Force_Msg => True);
4450 P_Identifier_Declarations (Decls, Done, In_Spec);
4454 Set_Declaration_Expected;
4459 -- To resynchronize after an error, we scan to the next semicolon and
4460 -- return with Done = False, indicating that there may still be more
4461 -- valid declarations to come.
4464 when Error_Resync =>
4465 Resync_Past_Semicolon;
4467 end P_Declarative_Items;
4469 ----------------------------------
4470 -- 3.11 Basic Declarative Item --
4471 ----------------------------------
4473 -- BASIC_DECLARATIVE_ITEM ::=
4474 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
4476 -- Scan zero or more basic declarative items
4478 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
4479 -- the scan pointer is repositioned past the next semicolon, and the scan
4480 -- for declarative items continues.
4482 function P_Basic_Declarative_Items return List_Id is
4489 -- Indicate no bad declarations detected yet in the current context:
4490 -- visible or private declarations of a package spec.
4492 Missing_Begin_Msg := No_Error_Msg;
4494 -- Get rid of active SIS entry from outer scope. This means we will
4495 -- miss some nested cases, but it doesn't seem worth the effort. See
4496 -- discussion in Par for further details
4498 SIS_Entry_Active := False;
4500 -- Loop to scan out declarations
4505 P_Declarative_Items (Decls, Done, In_Spec => True);
4509 -- Get rid of active SIS entry. This is set only if we have scanned a
4510 -- procedure declaration and have not found the body. We could give
4511 -- an error message, but that really would be usurping the role of
4512 -- semantic analysis (this really is a case of a missing body).
4514 SIS_Entry_Active := False;
4516 -- Test for assorted illegal declarations not diagnosed elsewhere
4518 Decl := First (Decls);
4520 while Present (Decl) loop
4521 Kind := Nkind (Decl);
4523 -- Test for body scanned, not acceptable as basic decl item
4525 if Kind = N_Subprogram_Body or else
4526 Kind = N_Package_Body or else
4527 Kind = N_Task_Body or else
4528 Kind = N_Protected_Body
4530 Error_Msg ("proper body not allowed in package spec", Sloc (Decl));
4532 -- Test for body stub scanned, not acceptable as basic decl item
4534 elsif Kind in N_Body_Stub then
4535 Error_Msg ("body stub not allowed in package spec", Sloc (Decl));
4537 elsif Kind = N_Assignment_Statement then
4539 ("assignment statement not allowed in package spec",
4547 end P_Basic_Declarative_Items;
4553 -- For proper body, see below
4554 -- For body stub, see 10.1.3
4556 -----------------------
4557 -- 3.11 Proper Body --
4558 -----------------------
4560 -- Subprogram body is parsed by P_Subprogram (6.1)
4561 -- Package body is parsed by P_Package (7.1)
4562 -- Task body is parsed by P_Task (9.1)
4563 -- Protected body is parsed by P_Protected (9.4)
4565 ------------------------------
4566 -- Set_Declaration_Expected --
4567 ------------------------------
4569 procedure Set_Declaration_Expected is
4571 Error_Msg_SC ("declaration expected");
4573 if Missing_Begin_Msg = No_Error_Msg then
4574 Missing_Begin_Msg := Get_Msg_Id;
4576 end Set_Declaration_Expected;
4578 ----------------------
4579 -- Skip_Declaration --
4580 ----------------------
4582 procedure Skip_Declaration (S : List_Id) is
4583 Dummy_Done : Boolean;
4584 pragma Warnings (Off, Dummy_Done);
4586 P_Declarative_Items (S, Dummy_Done, False);
4587 end Skip_Declaration;
4589 -----------------------------------------
4590 -- Statement_When_Declaration_Expected --
4591 -----------------------------------------
4593 procedure Statement_When_Declaration_Expected
4599 -- Case of second occurrence of statement in one declaration sequence
4601 if Missing_Begin_Msg /= No_Error_Msg then
4603 -- In the procedure spec case, just ignore it, we only give one
4604 -- message for the first occurrence, since otherwise we may get
4605 -- horrible cascading if BODY was missing in the header line.
4610 -- In the declarative part case, take a second statement as a sure
4611 -- sign that we really have a missing BEGIN, and end the declarative
4612 -- part now. Note that the caller will fix up the first message to
4613 -- say "missing BEGIN" so that's how the error will be signalled.
4620 -- Case of first occurrence of unexpected statement
4623 -- If we are in a package spec, then give message of statement
4624 -- not allowed in package spec. This message never gets changed.
4627 Error_Msg_SC ("statement not allowed in package spec");
4629 -- If in declarative part, then we give the message complaining
4630 -- about finding a statement when a declaration is expected. This
4631 -- gets changed to a complaint about a missing BEGIN if we later
4632 -- find that no BEGIN is present.
4635 Error_Msg_SC ("statement not allowed in declarative part");
4638 -- Capture message Id. This is used for two purposes, first to
4639 -- stop multiple messages, see test above, and second, to allow
4640 -- the replacement of the message in the declarative part case.
4642 Missing_Begin_Msg := Get_Msg_Id;
4645 -- In all cases except the case in which we decided to terminate the
4646 -- declaration sequence on a second error, we scan out the statement
4647 -- and append it to the list of declarations (note that the semantics
4648 -- can handle statements in a declaration list so if we proceed to
4649 -- call the semantic phase, all will be (reasonably) well!
4651 Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
4653 -- Done is set to False, since we want to continue the scan of
4654 -- declarations, hoping that this statement was a temporary glitch.
4655 -- If we indeed are now in the statement part (i.e. this was a missing
4656 -- BEGIN, then it's not terrible, we will simply keep calling this
4657 -- procedure to process the statements one by one, and then finally
4658 -- hit the missing BEGIN, which will clean up the error message.
4661 end Statement_When_Declaration_Expected;