Skip various cmp-mem-const tests on lp64 hppa*-*-*
[official-gcc.git] / gcc / ada / par-ch3.adb
blobfddb1d9d163639fde9d747aaa0c50a9afb3d7013
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
32 separate (Par)
34 ---------
35 -- Ch3 --
36 ---------
38 package body Ch3 is
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_Item
81 (Decls : List_Id;
82 Done : out Boolean;
83 Declare_Expression : Boolean;
84 In_Spec : Boolean;
85 In_Statements : Boolean);
86 -- Parses a single declarative item. The parameters have the same meaning
87 -- as for P_Declarative_Items. If the declarative item has multiple
88 -- identifiers, as in "X, Y, Z : ...", then one declaration is appended to
89 -- Decls for each of the identifiers.
91 procedure P_Identifier_Declarations
92 (Decls : List_Id;
93 Done : out Boolean;
94 In_Spec : Boolean;
95 In_Statements : Boolean);
96 -- Parses a sequence of declarations for an identifier or list of
97 -- identifiers, and appends them to the given list. The parameters
98 -- have the same meaning as for P_Declarative_Items.
100 procedure Statement_When_Declaration_Expected
101 (Decls : List_Id;
102 Done : out Boolean;
103 In_Spec : Boolean);
104 -- Called when a statement is found at a point where a declaration was
105 -- expected. The parameters have the same meaning as for
106 -- P_Declarative_Items.
108 procedure Set_Declaration_Expected;
109 -- Posts a "declaration expected" error messages at the start of the
110 -- current token, and if this is the first such message issued, saves
111 -- the message id in Missing_Begin_Msg, for possible later replacement.
113 ---------------------------------
114 -- Check_Restricted_Expression --
115 ---------------------------------
117 procedure Check_Restricted_Expression (N : Node_Id) is
118 begin
119 if Nkind (N) in N_Op_And | N_Op_Or | N_Op_Xor | N_And_Then | N_Or_Else
120 then
121 Check_Restricted_Expression (Left_Opnd (N));
122 Check_Restricted_Expression (Right_Opnd (N));
124 elsif Nkind (N) in N_In | N_Not_In
125 and then Paren_Count (N) = 0
126 then
127 Error_Msg_N ("|this expression must be parenthesized!", N);
128 end if;
129 end Check_Restricted_Expression;
131 -------------------
132 -- Init_Expr_Opt --
133 -------------------
135 function Init_Expr_Opt (P : Boolean := False) return Node_Id is
136 begin
137 -- For colon, assume it means := unless it is at the end of
138 -- a line, in which case guess that it means a semicolon.
140 if Token = Tok_Colon then
141 if Token_Is_At_End_Of_Line then
142 T_Semicolon;
143 return Empty;
144 end if;
146 -- Here if := or something that we will take as equivalent
148 elsif Token in Tok_Colon_Equal | Tok_Equal | Tok_Is then
149 null;
151 -- Another possibility. If we have a literal followed by a semicolon,
152 -- we assume that we have a missing colon-equal.
154 elsif Token in Token_Class_Literal then
155 declare
156 Scan_State : Saved_Scan_State;
158 begin
159 Save_Scan_State (Scan_State);
160 Scan; -- past literal or identifier
162 if Token = Tok_Semicolon then
163 Restore_Scan_State (Scan_State);
164 else
165 Restore_Scan_State (Scan_State);
166 return Empty;
167 end if;
168 end;
170 -- Otherwise we definitely have no initialization expression
172 else
173 return Empty;
174 end if;
176 -- Merge here if we have an initialization expression
178 T_Colon_Equal;
180 if P then
181 return P_Expression;
182 else
183 return P_Expression_No_Right_Paren;
184 end if;
185 end Init_Expr_Opt;
187 ----------------------------
188 -- 3.1 Basic Declaration --
189 ----------------------------
191 -- Parsed by P_Basic_Declarative_Items (3.9)
193 ------------------------------
194 -- 3.1 Defining Identifier --
195 ------------------------------
197 -- DEFINING_IDENTIFIER ::= IDENTIFIER
199 -- Error recovery: can raise Error_Resync
201 function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
202 Ident_Node : Node_Id := P_Identifier (C, True);
204 begin
205 -- If we already have a defining identifier, clean it out and make
206 -- a new clean identifier. This situation arises in some error cases
207 -- and we need to fix it.
209 if Nkind (Ident_Node) = N_Defining_Identifier then
210 Ident_Node := Make_Identifier (Sloc (Ident_Node), Chars (Ident_Node));
211 end if;
213 -- Change identifier to defining identifier if not in error
215 if Ident_Node /= Error then
216 Change_Identifier_To_Defining_Identifier (Ident_Node);
218 -- Warn if standard redefinition, except that we never warn on a
219 -- record field definition (since this is always a harmless case).
221 if not Inside_Record_Definition then
222 Warn_If_Standard_Redefinition (Ident_Node);
223 end if;
224 end if;
226 return Ident_Node;
227 end P_Defining_Identifier;
229 -----------------------------
230 -- 3.2.1 Type Declaration --
231 -----------------------------
233 -- TYPE_DECLARATION ::=
234 -- FULL_TYPE_DECLARATION
235 -- | INCOMPLETE_TYPE_DECLARATION
236 -- | PRIVATE_TYPE_DECLARATION
237 -- | PRIVATE_EXTENSION_DECLARATION
239 -- FULL_TYPE_DECLARATION ::=
240 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION
241 -- [ASPECT_SPECIFICATIONS];
242 -- | CONCURRENT_TYPE_DECLARATION
244 -- INCOMPLETE_TYPE_DECLARATION ::=
245 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] [is tagged];
247 -- PRIVATE_TYPE_DECLARATION ::=
248 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
249 -- is [abstract] [tagged] [limited] private
250 -- [ASPECT_SPECIFICATIONS];
252 -- PRIVATE_EXTENSION_DECLARATION ::=
253 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
254 -- [abstract] [limited | synchronized]
255 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
256 -- with private [ASPECT_SPECIFICATIONS];
258 -- TYPE_DEFINITION ::=
259 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
260 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
261 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
262 -- | DERIVED_TYPE_DEFINITION | INTERFACE_TYPE_DEFINITION
264 -- INTEGER_TYPE_DEFINITION ::=
265 -- SIGNED_INTEGER_TYPE_DEFINITION
266 -- MODULAR_TYPE_DEFINITION
268 -- INTERFACE_TYPE_DEFINITION ::=
269 -- [limited | task | protected | synchronized ] interface
270 -- [and INTERFACE_LIST]
272 -- Error recovery: can raise Error_Resync
274 -- The processing for full type declarations, incomplete type declarations,
275 -- private type declarations and type definitions is included in this
276 -- function. The processing for concurrent type declarations is NOT here,
277 -- but rather in chapter 9 (this function handles only declarations
278 -- starting with TYPE).
280 function P_Type_Declaration return Node_Id is
281 Abstract_Present : Boolean := False;
282 Abstract_Loc : Source_Ptr := No_Location;
283 Decl_Node : Node_Id;
284 Discr_List : List_Id;
285 Discr_Sloc : Source_Ptr;
286 End_Labl : Node_Id;
287 Ident_Node : Node_Id;
288 Is_Derived_Iface : Boolean := False;
289 Type_Loc : Source_Ptr;
290 Type_Start_Col : Column_Number;
291 Unknown_Dis : Boolean;
293 Typedef_Node : Node_Id;
294 -- Normally holds type definition, except in the case of a private
295 -- extension declaration, in which case it holds the declaration itself
297 begin
298 Type_Loc := Token_Ptr;
299 Type_Start_Col := Start_Column;
301 -- If we have TYPE, then proceed ahead and scan identifier
303 if Token = Tok_Type then
304 Type_Token_Location := Type_Loc;
305 Scan; -- past TYPE
306 Ident_Node := P_Defining_Identifier (C_Is);
308 -- Otherwise this is an error case
310 else
311 T_Type;
312 Type_Token_Location := Type_Loc;
313 Ident_Node := P_Defining_Identifier (C_Is);
314 end if;
316 Discr_Sloc := Token_Ptr;
318 if P_Unknown_Discriminant_Part_Opt then
319 Unknown_Dis := True;
320 Discr_List := No_List;
321 else
322 Unknown_Dis := False;
323 Discr_List := P_Known_Discriminant_Part_Opt;
324 end if;
326 -- Incomplete type declaration. We complete the processing for this
327 -- case here and return the resulting incomplete type declaration node
329 if Token = Tok_Semicolon then
330 Scan; -- past ;
331 Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
332 Set_Defining_Identifier (Decl_Node, Ident_Node);
333 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
334 Set_Discriminant_Specifications (Decl_Node, Discr_List);
335 return Decl_Node;
337 else
338 Decl_Node := Empty;
339 end if;
341 -- Full type declaration or private type declaration, must have IS
343 if Token = Tok_Equal then
344 TF_Is;
345 Scan; -- past = used in place of IS
347 elsif Token = Tok_Renames then
348 Error_Msg_SC -- CODEFIX
349 ("RENAMES should be IS");
350 Scan; -- past RENAMES used in place of IS
352 else
353 TF_Is;
354 end if;
356 -- First an error check, if we have two identifiers in a row, a likely
357 -- possibility is that the first of the identifiers is an incorrectly
358 -- spelled keyword.
360 if Token = Tok_Identifier then
361 declare
362 SS : Saved_Scan_State;
363 I2 : Boolean;
365 begin
366 Save_Scan_State (SS);
367 Scan; -- past initial identifier
368 I2 := (Token = Tok_Identifier);
369 Restore_Scan_State (SS);
371 if I2
372 and then
373 (Bad_Spelling_Of (Tok_Abstract) or else
374 Bad_Spelling_Of (Tok_Access) or else
375 Bad_Spelling_Of (Tok_Aliased) or else
376 Bad_Spelling_Of (Tok_Constant))
377 then
378 null;
379 end if;
380 end;
381 end if;
383 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
385 if Token_Name = Name_Abstract then
386 Check_95_Keyword (Tok_Abstract, Tok_Tagged);
387 Check_95_Keyword (Tok_Abstract, Tok_New);
388 end if;
390 -- Check cases of misuse of ABSTRACT
392 if Token = Tok_Abstract then
393 Abstract_Present := True;
394 Abstract_Loc := Token_Ptr;
395 Scan; -- past ABSTRACT
397 -- Ada 2005 (AI-419): AARM 3.4 (2/2)
399 if (Ada_Version < Ada_2005 and then Token = Tok_Limited)
400 or else Token in Tok_Private | Tok_Record | Tok_Null
401 then
402 Error_Msg_AP ("TAGGED expected");
403 end if;
404 end if;
406 -- Check for misuse of Ada 95 keyword Tagged
408 if Token_Name = Name_Tagged then
409 Check_95_Keyword (Tok_Tagged, Tok_Private);
410 Check_95_Keyword (Tok_Tagged, Tok_Limited);
411 Check_95_Keyword (Tok_Tagged, Tok_Record);
412 end if;
414 -- Special check for misuse of Aliased
416 if Token = Tok_Aliased or else Token_Name = Name_Aliased then
417 Error_Msg_SC ("ALIASED not allowed in type definition");
418 Scan; -- past ALIASED
419 end if;
421 -- The following processing deals with either a private type declaration
422 -- or a full type declaration. In the private type case, we build the
423 -- N_Private_Type_Declaration node, setting its Tagged_Present and
424 -- Limited_Present flags, on encountering the Private keyword, and
425 -- leave Typedef_Node set to Empty. For the full type declaration
426 -- case, Typedef_Node gets set to the type definition.
428 Typedef_Node := Empty;
430 -- Switch on token following the IS. The loop normally runs once. It
431 -- only runs more than once if an error is detected, to try again after
432 -- detecting and fixing up the error.
434 loop
435 case Token is
436 when Tok_Access
437 | Tok_Not -- Ada 2005 (AI-231)
439 Typedef_Node := P_Access_Type_Definition;
440 exit;
442 when Tok_Array =>
443 Typedef_Node := P_Array_Type_Definition;
444 exit;
446 when Tok_Delta =>
447 Typedef_Node := P_Fixed_Point_Definition;
448 exit;
450 when Tok_Digits =>
451 Typedef_Node := P_Floating_Point_Definition;
452 exit;
454 when Tok_In =>
455 Ignore (Tok_In);
457 when Tok_Integer_Literal =>
458 T_Range;
459 Typedef_Node := P_Signed_Integer_Type_Definition;
460 exit;
462 when Tok_Null =>
463 Typedef_Node := P_Record_Definition;
464 exit;
466 when Tok_Left_Paren =>
467 Typedef_Node := P_Enumeration_Type_Definition;
469 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
470 Set_Comes_From_Source (End_Labl, False);
472 Set_End_Label (Typedef_Node, End_Labl);
473 exit;
475 when Tok_Mod =>
476 Typedef_Node := P_Modular_Type_Definition;
477 exit;
479 when Tok_New =>
480 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
482 if Nkind (Typedef_Node) = N_Derived_Type_Definition
483 and then Present (Record_Extension_Part (Typedef_Node))
484 then
485 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
486 Set_Comes_From_Source (End_Labl, False);
488 Set_End_Label
489 (Record_Extension_Part (Typedef_Node), End_Labl);
490 end if;
492 exit;
494 when Tok_Range =>
495 Typedef_Node := P_Signed_Integer_Type_Definition;
496 exit;
498 when Tok_Record =>
499 Typedef_Node := P_Record_Definition;
501 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
502 Set_Comes_From_Source (End_Labl, False);
504 Set_End_Label (Typedef_Node, End_Labl);
505 exit;
507 when Tok_Tagged =>
508 Scan; -- past TAGGED
510 -- Ada 2005 (AI-326): If the words IS TAGGED appear, the type
511 -- is a tagged incomplete type.
513 if Ada_Version >= Ada_2005
514 and then Token = Tok_Semicolon
515 then
516 Scan; -- past ;
518 Decl_Node :=
519 New_Node (N_Incomplete_Type_Declaration, Type_Loc);
520 Set_Defining_Identifier (Decl_Node, Ident_Node);
521 Set_Tagged_Present (Decl_Node);
522 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
523 Set_Discriminant_Specifications (Decl_Node, Discr_List);
525 return Decl_Node;
526 end if;
528 if Token = Tok_Abstract then
529 Error_Msg_SC -- CODEFIX
530 ("ABSTRACT must come before TAGGED");
531 Abstract_Present := True;
532 Abstract_Loc := Token_Ptr;
533 Scan; -- past ABSTRACT
534 end if;
536 if Token = Tok_Limited then
537 Scan; -- past LIMITED
539 -- TAGGED LIMITED PRIVATE case
541 if Token = Tok_Private then
542 Decl_Node :=
543 New_Node (N_Private_Type_Declaration, Type_Loc);
544 Set_Tagged_Present (Decl_Node, True);
545 Set_Limited_Present (Decl_Node, True);
546 Scan; -- past PRIVATE
548 -- TAGGED LIMITED RECORD
550 else
551 Typedef_Node := P_Record_Definition;
552 Set_Tagged_Present (Typedef_Node, True);
553 Set_Limited_Present (Typedef_Node, True);
555 End_Labl :=
556 Make_Identifier (Token_Ptr, Chars (Ident_Node));
557 Set_Comes_From_Source (End_Labl, False);
559 Set_End_Label (Typedef_Node, End_Labl);
560 end if;
562 else
563 -- TAGGED PRIVATE
565 if Token = Tok_Private then
566 Decl_Node :=
567 New_Node (N_Private_Type_Declaration, Type_Loc);
568 Set_Tagged_Present (Decl_Node, True);
569 Scan; -- past PRIVATE
571 -- TAGGED RECORD
573 else
574 Typedef_Node := P_Record_Definition;
575 Set_Tagged_Present (Typedef_Node, True);
577 End_Labl :=
578 Make_Identifier (Token_Ptr, Chars (Ident_Node));
579 Set_Comes_From_Source (End_Labl, False);
581 Set_End_Label (Typedef_Node, End_Labl);
582 end if;
583 end if;
585 exit;
587 when Tok_Limited =>
588 Scan; -- past LIMITED
590 loop
591 if Token = Tok_Tagged then
592 Error_Msg_SC -- CODEFIX
593 ("TAGGED must come before LIMITED");
594 Scan; -- past TAGGED
596 elsif Token = Tok_Abstract then
597 Error_Msg_SC -- CODEFIX
598 ("ABSTRACT must come before LIMITED");
599 Scan; -- past ABSTRACT
601 else
602 exit;
603 end if;
604 end loop;
606 -- LIMITED RECORD or LIMITED NULL RECORD
608 if Token in Tok_Record | Tok_Null then
609 if Ada_Version = Ada_83 then
610 Error_Msg_SP
611 ("(Ada 83) limited record declaration not allowed!");
613 -- In Ada 2005, "abstract limited" can appear before "new",
614 -- but it cannot be part of an untagged record declaration.
616 elsif Abstract_Present
617 and then Prev_Token /= Tok_Tagged
618 then
619 Error_Msg_SP ("TAGGED expected");
620 end if;
622 Typedef_Node := P_Record_Definition;
623 Set_Limited_Present (Typedef_Node, True);
624 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
625 Set_Comes_From_Source (End_Labl, False);
627 Set_End_Label (Typedef_Node, End_Labl);
629 -- Ada 2005 (AI-251): LIMITED INTERFACE
631 -- If we are compiling in Ada 83 or Ada 95 mode, "interface"
632 -- is not a reserved word but we force its analysis to
633 -- generate the corresponding usage error.
635 elsif Token = Tok_Interface
636 or else (Token = Tok_Identifier
637 and then Chars (Token_Node) = Name_Interface)
638 then
639 Typedef_Node :=
640 P_Interface_Type_Definition (Abstract_Present);
641 Abstract_Present := True;
642 Set_Limited_Present (Typedef_Node);
644 if Nkind (Typedef_Node) = N_Derived_Type_Definition then
645 Is_Derived_Iface := True;
646 end if;
648 -- Ada 2005 (AI-419): LIMITED NEW
650 elsif Token = Tok_New then
651 Error_Msg_Ada_2005_Extension ("LIMITED in derived type");
653 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
654 Set_Limited_Present (Typedef_Node);
656 if Nkind (Typedef_Node) = N_Derived_Type_Definition
657 and then Present (Record_Extension_Part (Typedef_Node))
658 then
659 End_Labl :=
660 Make_Identifier (Token_Ptr, Chars (Ident_Node));
661 Set_Comes_From_Source (End_Labl, False);
663 Set_End_Label
664 (Record_Extension_Part (Typedef_Node), End_Labl);
665 end if;
667 -- LIMITED PRIVATE is the only remaining possibility here
669 else
670 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
671 Set_Limited_Present (Decl_Node, True);
672 T_Private; -- past PRIVATE (or complain if not there)
673 end if;
675 exit;
677 -- Here we have an identifier after the IS, which is certainly
678 -- wrong and which might be one of several different mistakes.
680 when Tok_Identifier =>
682 -- First case, if identifier is on same line, then probably we
683 -- have something like "type X is Integer .." and the best
684 -- diagnosis is a missing NEW. Note: the missing new message
685 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
687 if not Token_Is_At_Start_Of_Line then
688 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
690 -- If the identifier is at the start of the line, and is in the
691 -- same column as the type declaration itself then we consider
692 -- that we had a missing type definition on the previous line
694 elsif Start_Column <= Type_Start_Col then
695 Error_Msg_AP ("type definition expected");
696 Typedef_Node := Error;
698 -- If the identifier is at the start of the line, and is in
699 -- a column to the right of the type declaration line, then we
700 -- may have something like:
702 -- type x is
703 -- r : integer
705 -- and the best diagnosis is a missing record keyword
707 else
708 Typedef_Node := P_Record_Definition;
709 end if;
711 exit;
713 -- Ada 2005 (AI-251): INTERFACE
715 when Tok_Interface =>
716 Typedef_Node := P_Interface_Type_Definition (Abstract_Present);
717 Abstract_Present := True;
718 exit;
720 when Tok_Private =>
721 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
722 Scan; -- past PRIVATE
724 -- Check error cases of private [abstract] tagged
726 if Token = Tok_Abstract then
727 Error_Msg_SC ("`ABSTRACT TAGGED` must come before PRIVATE");
728 Scan; -- past ABSTRACT
730 if Token = Tok_Tagged then
731 Scan; -- past TAGGED
732 end if;
734 elsif Token = Tok_Tagged then
735 Error_Msg_SC ("TAGGED must come before PRIVATE");
736 Scan; -- past TAGGED
737 end if;
739 exit;
741 -- Ada 2005 (AI-345): Protected, synchronized or task interface
742 -- or Ada 2005 (AI-443): Synchronized private extension.
744 when Tok_Protected
745 | Tok_Synchronized
746 | Tok_Task
748 declare
749 Saved_Token : constant Token_Type := Token;
751 begin
752 Scan; -- past TASK, PROTECTED or SYNCHRONIZED
754 -- Synchronized private extension
756 if Token = Tok_New then
757 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
759 if Saved_Token = Tok_Synchronized then
760 if Nkind (Typedef_Node) =
761 N_Derived_Type_Definition
762 then
763 Error_Msg_N
764 ("SYNCHRONIZED not allowed for record extension",
765 Typedef_Node);
766 else
767 Set_Synchronized_Present (Typedef_Node);
768 end if;
770 else
771 Error_Msg_SC ("invalid kind of private extension");
772 end if;
774 -- Interface
776 else
777 if Token /= Tok_Interface then
778 Error_Msg_SC ("NEW or INTERFACE expected");
779 end if;
781 Typedef_Node :=
782 P_Interface_Type_Definition (Abstract_Present);
783 Abstract_Present := True;
785 case Saved_Token is
786 when Tok_Task =>
787 Set_Task_Present (Typedef_Node);
789 when Tok_Protected =>
790 Set_Protected_Present (Typedef_Node);
792 when Tok_Synchronized =>
793 Set_Synchronized_Present (Typedef_Node);
795 when others =>
796 pragma Assert (False);
797 null;
798 end case;
799 end if;
800 end;
802 exit;
804 -- Anything else is an error
806 when others =>
807 if Bad_Spelling_Of (Tok_Access)
808 or else
809 Bad_Spelling_Of (Tok_Array)
810 or else
811 Bad_Spelling_Of (Tok_Delta)
812 or else
813 Bad_Spelling_Of (Tok_Digits)
814 or else
815 Bad_Spelling_Of (Tok_Limited)
816 or else
817 Bad_Spelling_Of (Tok_Private)
818 or else
819 Bad_Spelling_Of (Tok_Range)
820 or else
821 Bad_Spelling_Of (Tok_Record)
822 or else
823 Bad_Spelling_Of (Tok_Tagged)
824 then
825 null;
827 else
828 Error_Msg_AP ("type definition expected");
829 raise Error_Resync;
830 end if;
831 end case;
832 end loop;
834 -- For the private type declaration case, the private type declaration
835 -- node has been built, with the Tagged_Present and Limited_Present
836 -- flags set as needed, and Typedef_Node is left set to Empty.
838 if No (Typedef_Node) then
839 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
840 Set_Abstract_Present (Decl_Node, Abstract_Present);
842 -- For a private extension declaration, Typedef_Node contains the
843 -- N_Private_Extension_Declaration node, which we now complete. Note
844 -- that the private extension declaration, unlike a full type
845 -- declaration, does permit unknown discriminants.
847 elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
848 Decl_Node := Typedef_Node;
849 Set_Sloc (Decl_Node, Type_Loc);
850 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
851 Set_Abstract_Present (Typedef_Node, Abstract_Present);
853 -- In the full type declaration case, Typedef_Node has the type
854 -- definition and here is where we build the full type declaration
855 -- node. This is also where we check for improper use of an unknown
856 -- discriminant part (not allowed for full type declaration).
858 else
859 if Nkind (Typedef_Node) = N_Record_Definition
860 or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
861 and then Present (Record_Extension_Part (Typedef_Node)))
862 or else Is_Derived_Iface
863 then
864 Set_Abstract_Present (Typedef_Node, Abstract_Present);
866 elsif Abstract_Present then
867 Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
868 end if;
870 Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
871 Set_Type_Definition (Decl_Node, Typedef_Node);
873 if Unknown_Dis then
874 Error_Msg
875 ("full type declaration cannot have unknown discriminants",
876 Discr_Sloc);
877 end if;
878 end if;
880 -- Remaining processing is common for all three cases
882 Set_Defining_Identifier (Decl_Node, Ident_Node);
883 Set_Discriminant_Specifications (Decl_Node, Discr_List);
884 P_Aspect_Specifications (Decl_Node);
885 return Decl_Node;
886 end P_Type_Declaration;
888 ----------------------------------
889 -- 3.2.1 Full Type Declaration --
890 ----------------------------------
892 -- Parsed by P_Type_Declaration (3.2.1)
894 ----------------------------
895 -- 3.2.1 Type Definition --
896 ----------------------------
898 -- Parsed by P_Type_Declaration (3.2.1)
900 --------------------------------
901 -- 3.2.2 Subtype Declaration --
902 --------------------------------
904 -- SUBTYPE_DECLARATION ::=
905 -- subtype DEFINING_IDENTIFIER is [NULL_EXCLUSION] SUBTYPE_INDICATION
906 -- [ASPECT_SPECIFICATIONS];
908 -- The caller has checked that the initial token is SUBTYPE
910 -- Error recovery: can raise Error_Resync
912 function P_Subtype_Declaration return Node_Id is
913 Decl_Node : Node_Id;
914 Not_Null_Present : Boolean := False;
916 begin
917 Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
918 Scan; -- past SUBTYPE
919 Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
920 TF_Is;
922 if Token = Tok_New then
923 Error_Msg_SC -- CODEFIX
924 ("NEW ignored (only allowed in type declaration)");
925 Scan; -- past NEW
926 end if;
928 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
929 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
931 Set_Subtype_Indication
932 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
933 P_Aspect_Specifications (Decl_Node);
934 return Decl_Node;
935 end P_Subtype_Declaration;
937 -------------------------------
938 -- 3.2.2 Subtype Indication --
939 -------------------------------
941 -- SUBTYPE_INDICATION ::=
942 -- [not null] SUBTYPE_MARK [CONSTRAINT]
944 -- Error recovery: can raise Error_Resync
946 function P_Null_Exclusion
947 (Allow_Anonymous_In_95 : Boolean := False) return Boolean
949 Not_Loc : constant Source_Ptr := Token_Ptr;
950 -- Source position of "not", if present
952 begin
953 if Token /= Tok_Not then
954 return False;
956 else
957 Scan; -- past NOT
959 if Token = Tok_Null then
960 Scan; -- past NULL
962 -- Ada 2005 (AI-441, AI-447): null_exclusion is illegal in Ada 95,
963 -- except in the case of anonymous access types.
965 -- Allow_Anonymous_In_95 will be True if we're parsing a formal
966 -- parameter or discriminant, which are the only places where
967 -- anonymous access types occur in Ada 95. "Formal : not null
968 -- access ..." is legal in Ada 95, whereas "Formal : not null
969 -- Named_Access_Type" is not.
971 if Ada_Version >= Ada_2005
972 or else (Ada_Version >= Ada_95
973 and then Allow_Anonymous_In_95
974 and then Token = Tok_Access)
975 then
976 null; -- OK
978 else
979 Error_Msg
980 ("`NOT NULL` access type is an Ada 2005 extension", Not_Loc);
981 Error_Msg
982 ("\unit should be compiled with -gnat05 switch", Not_Loc);
983 end if;
985 else
986 Error_Msg_SP ("NULL expected");
987 end if;
989 if Token = Tok_New then
990 Error_Msg ("`NOT NULL` comes after NEW, not before", Not_Loc);
991 end if;
993 return True;
994 end if;
995 end P_Null_Exclusion;
997 function P_Subtype_Indication
998 (Not_Null_Present : Boolean := False) return Node_Id
1000 Type_Node : Node_Id;
1002 begin
1003 if Token in Tok_Identifier | Tok_Operator_Symbol then
1004 Type_Node := P_Subtype_Mark;
1005 return P_Subtype_Indication (Type_Node, Not_Null_Present);
1007 else
1008 -- Check for error of using record definition and treat it nicely,
1009 -- otherwise things are really messed up, so resynchronize.
1011 if Token = Tok_Record then
1012 Error_Msg_SC ("anonymous record definition not permitted");
1013 Discard_Junk_Node (P_Record_Definition);
1014 return Error;
1016 else
1017 Error_Msg_AP ("subtype indication expected");
1018 raise Error_Resync;
1019 end if;
1020 end if;
1021 end P_Subtype_Indication;
1023 -- The following function is identical except that it is called with
1024 -- the subtype mark already scanned out, and it scans out the constraint
1026 -- Error recovery: can raise Error_Resync
1028 function P_Subtype_Indication
1029 (Subtype_Mark : Node_Id;
1030 Not_Null_Present : Boolean := False) return Node_Id
1032 Indic_Node : Node_Id;
1033 Constr_Node : Node_Id;
1035 begin
1036 Constr_Node := P_Constraint_Opt;
1038 if No (Constr_Node)
1039 or else
1040 (Nkind (Constr_Node) = N_Range_Constraint
1041 and then Nkind (Range_Expression (Constr_Node)) = N_Error)
1042 then
1043 return Subtype_Mark;
1044 else
1045 if Not_Null_Present then
1046 Error_Msg_SP ("`NOT NULL` not allowed if constraint given");
1047 end if;
1049 Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
1050 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
1051 Set_Constraint (Indic_Node, Constr_Node);
1052 return Indic_Node;
1053 end if;
1054 end P_Subtype_Indication;
1056 -------------------------
1057 -- 3.2.2 Subtype Mark --
1058 -------------------------
1060 -- SUBTYPE_MARK ::= subtype_NAME;
1062 -- Note: The subtype mark which appears after an IN or NOT IN
1063 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
1065 -- Error recovery: cannot raise Error_Resync
1067 function P_Subtype_Mark return Node_Id is
1068 begin
1069 return P_Subtype_Mark_Resync;
1070 exception
1071 when Error_Resync =>
1072 return Error;
1073 end P_Subtype_Mark;
1075 -- This routine differs from P_Subtype_Mark in that it insists that an
1076 -- identifier be present, and if it is not, it raises Error_Resync.
1078 -- Error recovery: can raise Error_Resync
1080 function P_Subtype_Mark_Resync return Node_Id is
1081 Type_Node : Node_Id;
1083 begin
1084 if Token = Tok_Access then
1085 Error_Msg_SC ("anonymous access type definition not allowed here");
1086 Scan; -- past ACCESS
1087 end if;
1089 if Token = Tok_Array then
1090 Error_Msg_SC ("anonymous array definition not allowed here");
1091 Discard_Junk_Node (P_Array_Type_Definition);
1092 return Error;
1094 else
1095 Type_Node := P_Qualified_Simple_Name_Resync;
1097 -- Check for a subtype mark attribute. The only valid possibilities
1098 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
1099 -- as well catch it here.
1101 if Token = Tok_Apostrophe then
1102 return P_Subtype_Mark_Attribute (Type_Node);
1103 else
1104 return Type_Node;
1105 end if;
1106 end if;
1107 end P_Subtype_Mark_Resync;
1109 -- The following function is called to scan out a subtype mark attribute.
1110 -- The caller has already scanned out the subtype mark, which is passed in
1111 -- as the argument, and has checked that the current token is apostrophe.
1113 -- Only a special subclass of attributes, called type attributes
1114 -- (see Snames package) are allowed in this syntactic position.
1116 -- Note: if the apostrophe is followed by other than an identifier, then
1117 -- the input expression is returned unchanged, and the scan pointer is
1118 -- left pointing to the apostrophe.
1120 -- Error recovery: can raise Error_Resync
1122 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
1123 Attr_Node : Node_Id := Empty;
1124 Scan_State : Saved_Scan_State;
1125 Prefix : Node_Id;
1127 begin
1128 Prefix := Check_Subtype_Mark (Type_Node);
1130 if Prefix = Error then
1131 raise Error_Resync;
1132 end if;
1134 -- Loop through attributes appearing (more than one can appear as for
1135 -- for example in X'Base'Class). We are at an apostrophe on entry to
1136 -- this loop, and it runs once for each attribute parsed, with
1137 -- Prefix being the current possible prefix if it is an attribute.
1139 loop
1140 Save_Scan_State (Scan_State); -- at Apostrophe
1141 Scan; -- past apostrophe
1143 if Token /= Tok_Identifier then
1144 Restore_Scan_State (Scan_State); -- to apostrophe
1145 return Prefix; -- no attribute after all
1147 elsif not Is_Type_Attribute_Name (Token_Name) then
1148 Error_Msg_N
1149 ("attribute & may not be used in a subtype mark", Token_Node);
1150 raise Error_Resync;
1152 else
1153 Attr_Node :=
1154 Make_Attribute_Reference (Prev_Token_Ptr,
1155 Prefix => Prefix,
1156 Attribute_Name => Token_Name);
1157 Scan; -- past type attribute identifier
1158 end if;
1160 exit when Token /= Tok_Apostrophe;
1161 Prefix := Attr_Node;
1162 end loop;
1164 -- Fall through here after scanning type attribute
1166 return Attr_Node;
1167 end P_Subtype_Mark_Attribute;
1169 -----------------------
1170 -- 3.2.2 Constraint --
1171 -----------------------
1173 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
1175 -- SCALAR_CONSTRAINT ::=
1176 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
1178 -- COMPOSITE_CONSTRAINT ::=
1179 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
1181 -- If no constraint is present, this function returns Empty
1183 -- Error recovery: can raise Error_Resync
1185 function P_Constraint_Opt return Node_Id is
1186 begin
1187 if Token = Tok_Range or else Bad_Spelling_Of (Tok_Range) then
1188 return P_Range_Constraint;
1190 elsif Token = Tok_Digits or else Bad_Spelling_Of (Tok_Digits) then
1191 return P_Digits_Constraint;
1193 elsif Token = Tok_Delta or else Bad_Spelling_Of (Tok_Delta) then
1194 return P_Delta_Constraint;
1196 elsif Token = Tok_Left_Paren then
1197 return P_Index_Or_Discriminant_Constraint;
1199 elsif Token = Tok_In then
1200 Ignore (Tok_In);
1201 return P_Constraint_Opt;
1203 -- One more possibility is e.g. 1 .. 10 (i.e. missing RANGE keyword)
1205 elsif Token = Tok_Identifier or else
1206 Token = Tok_Integer_Literal or else
1207 Token = Tok_Real_Literal
1208 then
1209 declare
1210 Scan_State : Saved_Scan_State;
1212 begin
1213 Save_Scan_State (Scan_State); -- at identifier or literal
1214 Scan; -- past identifier or literal
1216 if Token = Tok_Dot_Dot then
1217 Restore_Scan_State (Scan_State);
1218 Error_Msg_BC ("missing RANGE keyword");
1219 return P_Range_Constraint;
1220 else
1221 Restore_Scan_State (Scan_State);
1222 return Empty;
1223 end if;
1224 end;
1226 -- Nothing worked, no constraint there
1228 else
1229 return Empty;
1230 end if;
1231 end P_Constraint_Opt;
1233 ------------------------------
1234 -- 3.2.2 Scalar Constraint --
1235 ------------------------------
1237 -- Parsed by P_Constraint_Opt (3.2.2)
1239 ---------------------------------
1240 -- 3.2.2 Composite Constraint --
1241 ---------------------------------
1243 -- Parsed by P_Constraint_Opt (3.2.2)
1245 --------------------------------------------------------
1246 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
1247 --------------------------------------------------------
1249 -- This routine scans out a declaration starting with an identifier:
1251 -- OBJECT_DECLARATION ::=
1252 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1253 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION]
1254 -- [ASPECT_SPECIFICATIONS];
1255 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1256 -- ACCESS_DEFINITION [:= EXPRESSION]
1257 -- [ASPECT_SPECIFICATIONS];
1258 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1259 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION]
1260 -- [ASPECT_SPECIFICATIONS];
1262 -- NUMBER_DECLARATION ::=
1263 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
1265 -- OBJECT_RENAMING_DECLARATION ::=
1266 -- DEFINING_IDENTIFIER :
1267 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME
1268 -- [ASPECT_SPECIFICATIONS];
1269 -- | DEFINING_IDENTIFIER :
1270 -- ACCESS_DEFINITION renames object_NAME
1271 -- [ASPECT_SPECIFICATIONS];
1273 -- EXCEPTION_RENAMING_DECLARATION ::=
1274 -- DEFINING_IDENTIFIER : exception renames exception_NAME
1275 -- [ASPECT_SPECIFICATIONS];
1277 -- EXCEPTION_DECLARATION ::=
1278 -- DEFINING_IDENTIFIER_LIST : exception
1279 -- [ASPECT_SPECIFICATIONS];
1281 -- Note that the ALIASED indication in an object declaration is
1282 -- marked by a flag in the parent node.
1284 -- The caller has checked that the initial token is an identifier
1286 -- The value returned is a list of declarations, one for each identifier
1287 -- in the list (as described in Sinfo, we always split up multiple
1288 -- declarations into the equivalent sequence of single declarations
1289 -- using the More_Ids and Prev_Ids flags to preserve the source).
1291 -- If the identifier turns out to be a probable statement rather than
1292 -- an identifier, then the scan is left pointing to the identifier and
1293 -- No_List is returned.
1295 -- Error recovery: can raise Error_Resync
1297 procedure P_Identifier_Declarations
1298 (Decls : List_Id;
1299 Done : out Boolean;
1300 In_Spec : Boolean;
1301 In_Statements : Boolean)
1303 Acc_Node : Node_Id;
1304 Decl_Node : Node_Id;
1305 Type_Node : Node_Id;
1306 Ident_Sloc : Source_Ptr;
1307 Scan_State : Saved_Scan_State;
1308 List_OK : Boolean := True;
1309 Ident : Nat;
1310 Init_Expr : Node_Id;
1311 Init_Loc : Source_Ptr;
1312 Con_Loc : Source_Ptr;
1313 Not_Null_Present : Boolean := False;
1315 Idents : array (Int range 1 .. 4096) of Entity_Id;
1316 -- Used to save identifiers in the identifier list. The upper bound
1317 -- of 4096 is expected to be infinite in practice, and we do not even
1318 -- bother to check if this upper bound is exceeded.
1320 Num_Idents : Nat := 1;
1321 -- Number of identifiers stored in Idents
1323 function Identifier_Starts_Statement return Boolean;
1324 -- Called with Token being an identifier that might start a declaration
1325 -- or a statement. True if we are parsing declarations in a sequence of
1326 -- statements, and this identifier is the start of a statement. If this
1327 -- is true, we quit parsing declarations, and return Done = True so the
1328 -- caller will switch to parsing statements.
1330 procedure No_List;
1331 -- This procedure is called in renames cases to make sure that we do
1332 -- not have more than one identifier. If we do have more than one
1333 -- then an error message is issued (and the declaration is split into
1334 -- multiple declarations)
1336 function Token_Is_Renames return Boolean;
1337 -- Checks if current token is RENAMES, and if so, scans past it and
1338 -- returns True, otherwise returns False. Includes checking for some
1339 -- common error cases.
1341 ---------------------------------
1342 -- Identifier_Starts_Statement --
1343 ---------------------------------
1345 function Identifier_Starts_Statement return Boolean is
1346 pragma Assert (Token = Tok_Identifier);
1347 Scan_State : Saved_Scan_State;
1348 Result : Boolean := False;
1349 begin
1350 if not In_Statements then
1351 return False;
1352 end if;
1354 Save_Scan_State (Scan_State);
1355 Scan;
1357 case Token is
1358 when Tok_Comma => -- "X, ..." is a declaration
1359 null;
1361 when Tok_Colon =>
1362 -- "X : ..." is usually a declaration, but "X : begin..." is
1363 -- not. We return true for things like "X : Y : begin...",
1364 -- which is a syntax error, because that gives better error
1365 -- recovery for some ACATS.
1367 Scan;
1369 if Token in Token_Class_Labeled_Stmt then
1370 Result := True;
1372 elsif Token = Tok_Identifier then
1373 Scan;
1374 if Token = Tok_Colon then
1375 Scan;
1376 if Token in Token_Class_Labeled_Stmt then
1377 Result := True;
1378 end if;
1379 end if;
1380 end if;
1382 when others =>
1383 Result := True;
1384 end case;
1386 Restore_Scan_State (Scan_State);
1387 return Result;
1388 end Identifier_Starts_Statement;
1390 -------------
1391 -- No_List --
1392 -------------
1394 procedure No_List is
1395 begin
1396 if Num_Idents > 1 then
1397 Error_Msg_N
1398 ("identifier list not allowed for RENAMES",
1399 Idents (2));
1400 end if;
1402 List_OK := False;
1403 end No_List;
1405 ----------------------
1406 -- Token_Is_Renames --
1407 ----------------------
1409 function Token_Is_Renames return Boolean is
1410 At_Colon : Saved_Scan_State;
1412 begin
1413 if Token = Tok_Colon then
1414 Save_Scan_State (At_Colon);
1415 Scan; -- past colon
1416 Check_Misspelling_Of (Tok_Renames);
1418 if Token = Tok_Renames then
1419 Error_Msg_SP -- CODEFIX
1420 ("|extra "":"" ignored");
1421 Scan; -- past RENAMES
1422 return True;
1423 else
1424 Restore_Scan_State (At_Colon);
1425 return False;
1426 end if;
1428 else
1429 Check_Misspelling_Of (Tok_Renames);
1431 if Token = Tok_Renames then
1432 Scan; -- past RENAMES
1433 return True;
1434 else
1435 return False;
1436 end if;
1437 end if;
1438 end Token_Is_Renames;
1440 -- Start of processing for P_Identifier_Declarations
1442 begin
1443 if Identifier_Starts_Statement then
1444 Done := True;
1445 return;
1446 end if;
1448 Ident_Sloc := Token_Ptr;
1449 Save_Scan_State (Scan_State); -- at first identifier
1450 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1452 -- If we have a colon after the identifier, then we can assume that
1453 -- this is in fact a valid identifier declaration and can steam ahead.
1455 if Token = Tok_Colon then
1456 Scan; -- past colon
1458 -- If we have a comma, then scan out the list of identifiers
1460 elsif Token = Tok_Comma then
1461 while Comma_Present loop
1462 Num_Idents := Num_Idents + 1;
1463 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1464 end loop;
1466 Save_Scan_State (Scan_State); -- at colon
1467 T_Colon;
1469 -- If we have an identifier followed by := then we assume that what is
1470 -- really meant is an assignment statement. The assignment statement
1471 -- is scanned out and added to the list of declarations. An exception
1472 -- occurs if the := is followed by the keyword constant, in which case
1473 -- we assume it was meant to be a colon.
1475 elsif Token = Tok_Colon_Equal then
1476 Scan; -- past :=
1478 if Token = Tok_Constant then
1479 Error_Msg_SP ("colon expected");
1481 else
1482 Restore_Scan_State (Scan_State);
1484 -- Reset Token_Node, because it already got changed from an
1485 -- Identifier to a Defining_Identifier, and we don't want that
1486 -- for a statement!
1488 Token_Node :=
1489 Make_Identifier (Sloc (Token_Node), Chars (Token_Node));
1491 -- And now scan out one or more statements
1493 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1494 return;
1495 end if;
1497 -- If we have an IS keyword, then assume the TYPE keyword was missing
1499 elsif Token = Tok_Is then
1500 Restore_Scan_State (Scan_State);
1501 Append_To (Decls, P_Type_Declaration);
1502 Done := False;
1503 return;
1505 -- AI12-0275: Object renaming declaration without subtype_mark or
1506 -- access_definition
1508 elsif Token = Tok_Renames then
1509 Error_Msg_Ada_2022_Feature
1510 ("object renaming without subtype", Token_Ptr);
1512 Scan; -- past renames
1514 Decl_Node :=
1515 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1516 Set_Name (Decl_Node, P_Name);
1517 Set_Defining_Identifier (Decl_Node, Idents (1));
1519 P_Aspect_Specifications (Decl_Node, Semicolon => False);
1521 T_Semicolon;
1523 Append (Decl_Node, Decls);
1524 Done := False;
1526 return;
1528 -- Otherwise we have an error situation
1530 else
1531 Restore_Scan_State (Scan_State);
1533 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1534 -- so, fix the keyword and return to scan the protected declaration.
1536 if Token_Name = Name_Protected then
1537 Check_95_Keyword (Tok_Protected, Tok_Identifier);
1538 Check_95_Keyword (Tok_Protected, Tok_Type);
1539 Check_95_Keyword (Tok_Protected, Tok_Body);
1541 if Token = Tok_Protected then
1542 Done := False;
1543 return;
1544 end if;
1546 -- Check misspelling possibilities. If so, correct the misspelling
1547 -- and return to scan out the resulting declaration.
1549 elsif Bad_Spelling_Of (Tok_Function)
1550 or else Bad_Spelling_Of (Tok_Procedure)
1551 or else Bad_Spelling_Of (Tok_Package)
1552 or else Bad_Spelling_Of (Tok_Pragma)
1553 or else Bad_Spelling_Of (Tok_Protected)
1554 or else Bad_Spelling_Of (Tok_Generic)
1555 or else Bad_Spelling_Of (Tok_Subtype)
1556 or else Bad_Spelling_Of (Tok_Type)
1557 or else Bad_Spelling_Of (Tok_Task)
1558 or else Bad_Spelling_Of (Tok_Use)
1559 or else Bad_Spelling_Of (Tok_For)
1560 then
1561 Done := False;
1562 return;
1564 -- Otherwise we definitely have an ordinary identifier with a junk
1565 -- token after it.
1567 elsif In_Statements then
1568 Done := True;
1569 return;
1571 else
1572 -- If in -gnatd.2 mode, try for statements
1574 if Debug_Flag_Dot_2 then
1575 Restore_Scan_State (Scan_State);
1577 -- Reset Token_Node, because it already got changed from an
1578 -- Identifier to a Defining_Identifier, and we don't want that
1579 -- for a statement!
1581 Token_Node :=
1582 Make_Identifier (Sloc (Token_Node), Chars (Token_Node));
1584 -- And now scan out one or more statements
1586 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1587 return;
1589 -- Normal case, just complain and skip to semicolon
1591 else
1592 Set_Declaration_Expected;
1593 Resync_Past_Semicolon;
1594 Done := False;
1595 return;
1596 end if;
1597 end if;
1598 end if;
1600 -- Come here with an identifier list and colon scanned out. We now
1601 -- build the nodes for the declarative items. One node is built for
1602 -- each identifier in the list, with the type information being
1603 -- repeated by rescanning the appropriate section of source.
1605 -- First an error check, if we have two identifiers in a row, a likely
1606 -- possibility is that the first of the identifiers is an incorrectly
1607 -- spelled keyword.
1609 if Token = Tok_Identifier then
1610 declare
1611 SS : Saved_Scan_State;
1612 I2 : Boolean;
1614 begin
1615 Save_Scan_State (SS);
1616 Scan; -- past initial identifier
1617 I2 := (Token = Tok_Identifier);
1618 Restore_Scan_State (SS);
1620 if I2
1621 and then
1622 (Bad_Spelling_Of (Tok_Access) or else
1623 Bad_Spelling_Of (Tok_Aliased) or else
1624 Bad_Spelling_Of (Tok_Constant))
1625 then
1626 null;
1627 end if;
1628 end;
1629 end if;
1631 -- Loop through identifiers
1633 Ident := 1;
1634 Ident_Loop : loop
1636 -- Check for some cases of misused Ada 95 keywords
1638 if Token_Name = Name_Aliased then
1639 Check_95_Keyword (Tok_Aliased, Tok_Array);
1640 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1641 Check_95_Keyword (Tok_Aliased, Tok_Constant);
1642 end if;
1644 -- Constant cases
1646 if Token = Tok_Constant then
1647 Con_Loc := Token_Ptr;
1648 Scan; -- past CONSTANT
1650 -- Number declaration, initialization required
1652 Init_Expr := Init_Expr_Opt;
1654 if Present (Init_Expr) then
1655 if Not_Null_Present then
1656 Error_Msg_SP
1657 ("`NOT NULL` not allowed in numeric expression");
1658 end if;
1660 Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1661 Set_Expression (Decl_Node, Init_Expr);
1663 -- Constant object declaration
1665 else
1666 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1667 Set_Constant_Present (Decl_Node, True);
1669 if Token_Name = Name_Aliased then
1670 Check_95_Keyword (Tok_Aliased, Tok_Array);
1671 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1672 end if;
1674 if Token = Tok_Aliased then
1675 Error_Msg_SC -- CODEFIX
1676 ("ALIASED should be before CONSTANT");
1677 Scan; -- past ALIASED
1678 Set_Aliased_Present (Decl_Node, True);
1679 end if;
1681 if Token = Tok_Array then
1682 Set_Object_Definition
1683 (Decl_Node, P_Array_Type_Definition);
1685 else
1686 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1687 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1689 if Token = Tok_Access then
1690 Error_Msg_Ada_2005_Extension
1691 ("generalized use of anonymous access types");
1693 Set_Object_Definition
1694 (Decl_Node, P_Access_Definition (Not_Null_Present));
1695 else
1696 Set_Object_Definition
1697 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1698 end if;
1699 end if;
1701 if Token = Tok_Renames then
1702 Error_Msg
1703 ("CONSTANT not permitted in renaming declaration",
1704 Con_Loc);
1705 Scan; -- Past renames
1706 Discard_Junk_Node (P_Name);
1707 end if;
1708 end if;
1710 -- Exception cases
1712 elsif Token = Tok_Exception then
1713 Scan; -- past EXCEPTION
1715 if Token_Is_Renames then
1716 No_List;
1717 Decl_Node :=
1718 New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1719 Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1720 No_Constraint;
1721 else
1722 Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1723 end if;
1725 -- Aliased case (note that an object definition is required)
1727 elsif Token = Tok_Aliased then
1728 Scan; -- past ALIASED
1729 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1730 Set_Aliased_Present (Decl_Node, True);
1732 if Token = Tok_Constant then
1733 Scan; -- past CONSTANT
1734 Set_Constant_Present (Decl_Node, True);
1735 end if;
1737 if Token = Tok_Array then
1738 Set_Object_Definition
1739 (Decl_Node, P_Array_Type_Definition);
1741 else
1742 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1743 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1745 -- Access definition (AI-406) or subtype indication
1747 if Token = Tok_Access then
1748 Error_Msg_Ada_2005_Extension
1749 ("generalized use of anonymous access types");
1751 Set_Object_Definition
1752 (Decl_Node, P_Access_Definition (Not_Null_Present));
1753 else
1754 Set_Object_Definition
1755 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1756 end if;
1757 end if;
1759 -- Array case
1761 elsif Token = Tok_Array then
1762 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1763 Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1765 -- Ada 2005 (AI-254, AI-406)
1767 elsif Token = Tok_Not then
1769 -- OBJECT_DECLARATION ::=
1770 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1771 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION]
1772 -- [ASPECT_SPECIFICATIONS];
1773 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1774 -- ACCESS_DEFINITION [:= EXPRESSION]
1775 -- [ASPECT_SPECIFICATIONS];
1777 -- OBJECT_RENAMING_DECLARATION ::=
1778 -- DEFINING_IDENTIFIER :
1779 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME
1780 -- [ASPECT_SPECIFICATIONS];
1781 -- | DEFINING_IDENTIFIER :
1782 -- ACCESS_DEFINITION renames object_NAME
1783 -- [ASPECT_SPECIFICATIONS];
1785 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/423)
1787 if Token = Tok_Access then
1788 Error_Msg_Ada_2005_Extension
1789 ("generalized use of anonymous access types");
1791 Acc_Node := P_Access_Definition (Not_Null_Present);
1793 if Token /= Tok_Renames then
1794 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1795 Set_Object_Definition (Decl_Node, Acc_Node);
1797 else
1798 Scan; -- past renames
1799 No_List;
1800 Decl_Node :=
1801 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1802 Set_Access_Definition (Decl_Node, Acc_Node);
1803 Set_Name (Decl_Node, P_Name);
1804 end if;
1806 else
1807 Type_Node := P_Subtype_Mark;
1809 -- Object renaming declaration
1811 if Token_Is_Renames then
1812 if Ada_Version < Ada_2005 then
1813 Error_Msg_SP
1814 ("`NOT NULL` not allowed in object renaming");
1815 raise Error_Resync;
1817 -- Ada 2005 (AI-423): Object renaming declaration with
1818 -- a null exclusion.
1820 else
1821 No_List;
1822 Decl_Node :=
1823 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1824 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1825 Set_Subtype_Mark (Decl_Node, Type_Node);
1826 Set_Name (Decl_Node, P_Name);
1827 end if;
1829 -- Object declaration
1831 else
1832 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1833 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1834 Set_Object_Definition
1835 (Decl_Node,
1836 P_Subtype_Indication (Type_Node, Not_Null_Present));
1838 -- RENAMES at this point means that we had the combination
1839 -- of a constraint on the Type_Node and renames, which is
1840 -- illegal
1842 if Token_Is_Renames then
1843 Error_Msg_N
1844 ("constraint not allowed in object renaming "
1845 & "declaration",
1846 Constraint (Object_Definition (Decl_Node)));
1847 raise Error_Resync;
1848 end if;
1849 end if;
1850 end if;
1852 -- Ada 2005 (AI-230): Access Definition case
1854 elsif Token = Tok_Access then
1855 Error_Msg_Ada_2005_Extension
1856 ("generalized use of anonymous access types");
1858 Acc_Node := P_Access_Definition (Null_Exclusion_Present => False);
1860 -- Object declaration with access definition, or renaming
1862 if Token /= Tok_Renames then
1863 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1864 Set_Object_Definition (Decl_Node, Acc_Node);
1866 else
1867 Scan; -- past renames
1868 No_List;
1869 Decl_Node :=
1870 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1871 Set_Access_Definition (Decl_Node, Acc_Node);
1872 Set_Name (Decl_Node, P_Name);
1873 end if;
1875 -- Subtype indication case
1877 else
1878 Type_Node := P_Subtype_Mark;
1880 -- Object renaming declaration
1882 if Token_Is_Renames then
1883 No_List;
1884 Decl_Node :=
1885 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1886 Set_Subtype_Mark (Decl_Node, Type_Node);
1887 Set_Name (Decl_Node, P_Name);
1889 -- Object declaration
1891 else
1892 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1893 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1894 Set_Object_Definition
1895 (Decl_Node,
1896 P_Subtype_Indication (Type_Node, Not_Null_Present));
1898 -- RENAMES at this point means that we had the combination of
1899 -- a constraint on the Type_Node and renames, which is illegal
1901 if Token_Is_Renames then
1902 Error_Msg_N
1903 ("constraint not allowed in object renaming declaration",
1904 Constraint (Object_Definition (Decl_Node)));
1905 raise Error_Resync;
1906 end if;
1907 end if;
1908 end if;
1910 -- Scan out initialization, allowed only for object declaration
1912 Init_Loc := Token_Ptr;
1913 Init_Expr := Init_Expr_Opt;
1915 if Present (Init_Expr) then
1916 if Nkind (Decl_Node) = N_Object_Declaration then
1917 Set_Expression (Decl_Node, Init_Expr);
1918 Set_Has_Init_Expression (Decl_Node);
1919 else
1920 Error_Msg ("initialization not allowed here", Init_Loc);
1921 end if;
1922 end if;
1924 Set_Defining_Identifier (Decl_Node, Idents (Ident));
1925 P_Aspect_Specifications (Decl_Node, Semicolon => False);
1927 -- Allow initialization expression to follow aspects (note that in
1928 -- this case P_Aspect_Specifications already issued an error msg).
1930 if Token = Tok_Colon_Equal then
1931 if Is_Non_Empty_List (Aspect_Specifications (Decl_Node)) then
1932 Error_Msg
1933 ("aspect specifications must come after initialization "
1934 & "expression",
1935 Sloc (First (Aspect_Specifications (Decl_Node))));
1937 else
1938 -- In any case, the assignment symbol doesn't belong.
1940 Error_Msg ("misplaced assignment symbol", Scan_Ptr);
1941 end if;
1943 Set_Expression (Decl_Node, Init_Expr_Opt);
1944 Set_Has_Init_Expression (Decl_Node);
1945 end if;
1947 -- Now scan out the semicolon, which we deferred above
1949 T_Semicolon;
1951 if List_OK then
1952 if Ident < Num_Idents then
1953 Set_More_Ids (Decl_Node, True);
1954 end if;
1956 if Ident > 1 then
1957 Set_Prev_Ids (Decl_Node, True);
1958 end if;
1959 end if;
1961 Append (Decl_Node, Decls);
1962 exit Ident_Loop when Ident = Num_Idents;
1963 Restore_Scan_State (Scan_State);
1964 T_Colon;
1965 Ident := Ident + 1;
1966 end loop Ident_Loop;
1968 Done := False;
1969 end P_Identifier_Declarations;
1971 -------------------------------
1972 -- 3.3.1 Object Declaration --
1973 -------------------------------
1975 -- OBJECT DECLARATION ::=
1976 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1977 -- SUBTYPE_INDICATION [:= EXPRESSION];
1978 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1979 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1980 -- | SINGLE_TASK_DECLARATION
1981 -- | SINGLE_PROTECTED_DECLARATION
1983 -- Cases starting with TASK are parsed by P_Task (9.1)
1984 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1985 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1987 -------------------------------------
1988 -- 3.3.1 Defining Identifier List --
1989 -------------------------------------
1991 -- DEFINING_IDENTIFIER_LIST ::=
1992 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1994 -- Always parsed by the construct in which it appears. See special
1995 -- section on "Handling of Defining Identifier Lists" in this unit.
1997 -------------------------------
1998 -- 3.3.2 Number Declaration --
1999 -------------------------------
2001 -- Parsed by P_Identifier_Declarations (3.3)
2003 -------------------------------------------------------------------------
2004 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
2005 -------------------------------------------------------------------------
2007 -- DERIVED_TYPE_DEFINITION ::=
2008 -- [abstract] [limited] new [NULL_EXCLUSION] parent_SUBTYPE_INDICATION
2009 -- [[and INTERFACE_LIST] RECORD_EXTENSION_PART]
2011 -- PRIVATE_EXTENSION_DECLARATION ::=
2012 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
2013 -- [abstract] [limited | synchronized]
2014 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
2015 -- with private [ASPECT_SPECIFICATIONS];
2017 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
2019 -- The caller has already scanned out the part up to the NEW, and Token
2020 -- either contains Tok_New (or ought to, if it doesn't this procedure
2021 -- will post an appropriate "NEW expected" message).
2023 -- Note: the caller is responsible for filling in the Sloc field of
2024 -- the returned node in the private extension declaration case as
2025 -- well as the stuff relating to the discriminant part.
2027 -- Error recovery: can raise Error_Resync;
2029 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
2030 Typedef_Node : Node_Id;
2031 Typedecl_Node : Node_Id;
2032 Not_Null_Present : Boolean := False;
2034 begin
2035 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
2037 if Ada_Version < Ada_2005
2038 and then Token = Tok_Identifier
2039 and then Token_Name = Name_Interface
2040 then
2041 Error_Msg_SP
2042 ("abstract interface is an Ada 2005 extension");
2043 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
2044 else
2045 T_New;
2046 end if;
2048 if Token = Tok_Abstract then
2049 Error_Msg_SC -- CODEFIX
2050 ("ABSTRACT must come before NEW, not after");
2051 Scan;
2052 end if;
2054 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
2055 Set_Null_Exclusion_Present (Typedef_Node, Not_Null_Present);
2056 Set_Subtype_Indication (Typedef_Node,
2057 P_Subtype_Indication (Not_Null_Present));
2059 -- Ada 2005 (AI-251): Deal with interfaces
2061 if Token = Tok_And then
2062 Scan; -- past AND
2064 Error_Msg_Ada_2005_Extension ("abstract interface");
2066 Set_Interface_List (Typedef_Node, New_List);
2068 loop
2069 Append (P_Qualified_Simple_Name, Interface_List (Typedef_Node));
2070 exit when Token /= Tok_And;
2071 Scan; -- past AND
2072 end loop;
2074 if Token /= Tok_With then
2075 Error_Msg_SC ("WITH expected");
2076 raise Error_Resync;
2077 end if;
2078 end if;
2080 -- Deal with record extension, note that we assume that a WITH is
2081 -- missing in the case of "type X is new Y record ..." or in the
2082 -- case of "type X is new Y null record".
2084 -- First make sure we don't have an aspect specification. If we do
2085 -- return now, so that our caller can check it (the WITH here is not
2086 -- part of a type extension).
2088 if Aspect_Specifications_Present then
2089 return Typedef_Node;
2091 -- OK, not an aspect specification, so continue test for extension
2093 elsif Token in Tok_With | Tok_Record | Tok_Null then
2094 T_With; -- past WITH or give error message
2096 if Token = Tok_Limited then
2097 Error_Msg_SC ("LIMITED keyword not allowed in private extension");
2098 Scan; -- ignore LIMITED
2099 end if;
2101 -- Private extension declaration
2103 if Token = Tok_Private then
2104 Scan; -- past PRIVATE
2106 -- Throw away the type definition node and build the type
2107 -- declaration node. Note the caller must set the Sloc,
2108 -- Discriminant_Specifications, Unknown_Discriminants_Present,
2109 -- and Defined_Identifier fields in the returned node.
2111 Typedecl_Node :=
2112 Make_Private_Extension_Declaration (No_Location,
2113 Defining_Identifier => Empty,
2114 Subtype_Indication => Subtype_Indication (Typedef_Node),
2115 Abstract_Present => Abstract_Present (Typedef_Node),
2116 Interface_List => Interface_List (Typedef_Node));
2118 return Typedecl_Node;
2120 -- Derived type definition with record extension part
2122 else
2123 Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
2124 return Typedef_Node;
2125 end if;
2127 -- Derived type definition with no record extension part
2129 else
2130 return Typedef_Node;
2131 end if;
2132 end P_Derived_Type_Def_Or_Private_Ext_Decl;
2134 ---------------------------
2135 -- 3.5 Range Constraint --
2136 ---------------------------
2138 -- RANGE_CONSTRAINT ::= range RANGE
2140 -- The caller has checked that the initial token is RANGE or some
2141 -- misspelling of it, or it may be absent completely (and a message
2142 -- has already been issued).
2144 -- Error recovery: cannot raise Error_Resync
2146 function P_Range_Constraint return Node_Id is
2147 Range_Node : Node_Id;
2149 begin
2150 Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
2152 -- Skip range keyword if present
2154 if Token = Tok_Range or else Bad_Spelling_Of (Tok_Range) then
2155 Scan; -- past RANGE
2156 end if;
2158 Set_Range_Expression (Range_Node, P_Range);
2159 return Range_Node;
2160 end P_Range_Constraint;
2162 ----------------
2163 -- 3.5 Range --
2164 ----------------
2166 -- RANGE ::=
2167 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2169 -- Note: the range that appears in a membership test is parsed by
2170 -- P_Range_Or_Subtype_Mark (3.5).
2172 -- Error recovery: cannot raise Error_Resync
2174 function P_Range return Node_Id is
2175 Expr_Node : Node_Id;
2176 Range_Node : Node_Id;
2178 begin
2179 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2181 if Expr_Form = EF_Range_Attr then
2182 return Expr_Node;
2184 elsif Token = Tok_Dot_Dot then
2185 Range_Node := New_Node (N_Range, Token_Ptr);
2186 Set_Low_Bound (Range_Node, Expr_Node);
2187 Scan; -- past ..
2188 Expr_Node := P_Expression;
2189 Check_Simple_Expression (Expr_Node);
2190 Set_High_Bound (Range_Node, Expr_Node);
2191 return Range_Node;
2193 -- Anything else is an error
2195 else
2196 T_Dot_Dot; -- force missing .. message
2197 return Error;
2198 end if;
2199 end P_Range;
2201 ----------------------------------
2202 -- 3.5 P_Range_Or_Subtype_Mark --
2203 ----------------------------------
2205 -- RANGE ::=
2206 -- RANGE_ATTRIBUTE_REFERENCE
2207 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2209 -- This routine scans out the range or subtype mark that forms the right
2210 -- operand of a membership test (it is not used in any other contexts, and
2211 -- error messages are specialized with this knowledge in mind).
2213 -- Note: as documented in the Sinfo interface, although the syntax only
2214 -- allows a subtype mark, we in fact allow any simple expression to be
2215 -- returned from this routine. The semantics is responsible for issuing
2216 -- an appropriate message complaining if the argument is not a name.
2217 -- This simplifies the coding and error recovery processing in the
2218 -- parser, and in any case it is preferable not to consider this a
2219 -- syntax error and to continue with the semantic analysis.
2221 -- Error recovery: cannot raise Error_Resync
2223 function P_Range_Or_Subtype_Mark
2224 (Allow_Simple_Expression : Boolean := False) return Node_Id
2226 Expr_Node : Node_Id;
2227 Range_Node : Node_Id;
2228 Save_Loc : Source_Ptr;
2230 -- Start of processing for P_Range_Or_Subtype_Mark
2232 begin
2233 -- Save location of possible junk parentheses
2235 Save_Loc := Token_Ptr;
2237 -- Scan out either a simple expression or a range (this accepts more
2238 -- than is legal here, but as explained above, we like to allow more
2239 -- with a proper diagnostic, and in the case of a membership operation
2240 -- where sets are allowed, a simple expression is permissible anyway.
2242 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2244 -- Range attribute
2246 if Expr_Form = EF_Range_Attr then
2247 return Expr_Node;
2249 -- Simple_Expression .. Simple_Expression
2251 elsif Token = Tok_Dot_Dot then
2252 Check_Simple_Expression (Expr_Node);
2253 Range_Node := New_Node (N_Range, Token_Ptr);
2254 Set_Low_Bound (Range_Node, Expr_Node);
2255 Scan; -- past ..
2256 Set_High_Bound (Range_Node, P_Simple_Expression);
2257 return Range_Node;
2259 -- Case of subtype mark (optionally qualified simple name or an
2260 -- attribute whose prefix is an optionally qualified simple name)
2262 elsif Expr_Form = EF_Simple_Name
2263 or else Nkind (Expr_Node) = N_Attribute_Reference
2264 then
2265 -- Check for error of range constraint after a subtype mark
2267 if Token = Tok_Range then
2268 Error_Msg_SC ("range constraint not allowed in membership test");
2269 Scan; -- past RANGE
2270 raise Error_Resync;
2272 -- Check for error of DIGITS or DELTA after a subtype mark
2274 elsif Token in Tok_Digits | Tok_Delta then
2275 Error_Msg_SC
2276 ("accuracy definition not allowed in membership test");
2277 Scan; -- past DIGITS or DELTA
2278 raise Error_Resync;
2280 -- Attribute reference, may or may not be OK, but in any case we
2281 -- will scan it out
2283 elsif Token = Tok_Apostrophe then
2284 return P_Subtype_Mark_Attribute (Expr_Node);
2286 -- OK case of simple name, just return it
2288 else
2289 return Expr_Node;
2290 end if;
2292 -- Simple expression case
2294 elsif Expr_Form = EF_Simple and then Allow_Simple_Expression then
2295 return Expr_Node;
2297 -- Here we have some kind of error situation. Check for junk parens
2298 -- then return what we have, caller will deal with other errors.
2300 else
2301 if Nkind (Expr_Node) in N_Subexpr
2302 and then Paren_Count (Expr_Node) /= 0
2303 then
2304 Error_Msg ("|parentheses not allowed for subtype mark", Save_Loc);
2305 Set_Paren_Count (Expr_Node, 0);
2306 end if;
2308 return Expr_Node;
2309 end if;
2310 end P_Range_Or_Subtype_Mark;
2312 ----------------------------------------
2313 -- 3.5.1 Enumeration Type Definition --
2314 ----------------------------------------
2316 -- ENUMERATION_TYPE_DEFINITION ::=
2317 -- (ENUMERATION_LITERAL_SPECIFICATION
2318 -- {, ENUMERATION_LITERAL_SPECIFICATION})
2320 -- The caller has already scanned out the TYPE keyword
2322 -- Error recovery: can raise Error_Resync;
2324 function P_Enumeration_Type_Definition return Node_Id is
2325 Typedef_Node : Node_Id;
2327 begin
2328 Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
2329 Set_Literals (Typedef_Node, New_List);
2331 T_Left_Paren;
2333 loop
2334 Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
2335 exit when not Comma_Present;
2336 end loop;
2338 T_Right_Paren;
2339 return Typedef_Node;
2340 end P_Enumeration_Type_Definition;
2342 ----------------------------------------------
2343 -- 3.5.1 Enumeration Literal Specification --
2344 ----------------------------------------------
2346 -- ENUMERATION_LITERAL_SPECIFICATION ::=
2347 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
2349 -- Error recovery: can raise Error_Resync
2351 function P_Enumeration_Literal_Specification return Node_Id is
2352 begin
2353 if Token = Tok_Char_Literal then
2354 return P_Defining_Character_Literal;
2355 else
2356 return P_Defining_Identifier (C_Comma_Right_Paren);
2357 end if;
2358 end P_Enumeration_Literal_Specification;
2360 ---------------------------------------
2361 -- 3.5.1 Defining_Character_Literal --
2362 ---------------------------------------
2364 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
2366 -- Error recovery: cannot raise Error_Resync
2368 -- The caller has checked that the current token is a character literal
2370 function P_Defining_Character_Literal return Node_Id is
2371 Literal_Node : Node_Id;
2372 begin
2373 Literal_Node := Token_Node;
2374 Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
2375 Scan; -- past character literal
2376 return Literal_Node;
2377 end P_Defining_Character_Literal;
2379 ------------------------------------
2380 -- 3.5.4 Integer Type Definition --
2381 ------------------------------------
2383 -- Parsed by P_Type_Declaration (3.2.1)
2385 -------------------------------------------
2386 -- 3.5.4 Signed Integer Type Definition --
2387 -------------------------------------------
2389 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
2390 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2392 -- Normally the initial token on entry is RANGE, but in some
2393 -- error conditions, the range token was missing and control is
2394 -- passed with Token pointing to first token of the first expression.
2396 -- Error recovery: cannot raise Error_Resync
2398 function P_Signed_Integer_Type_Definition return Node_Id is
2399 Typedef_Node : Node_Id;
2400 Expr_Node : Node_Id;
2402 begin
2403 Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
2405 if Token = Tok_Range then
2406 Scan; -- past RANGE
2407 end if;
2409 Expr_Node := P_Expression_Or_Range_Attribute;
2411 -- Range case (not permitted by the grammar, this is surprising but
2412 -- the grammar in the RM is as quoted above, and does not allow Range).
2414 if Expr_Form = EF_Range_Attr then
2415 Error_Msg_N
2416 ("Range attribute not allowed here, use First .. Last", Expr_Node);
2417 Set_Low_Bound (Typedef_Node, Expr_Node);
2418 Set_Attribute_Name (Expr_Node, Name_First);
2419 Set_High_Bound (Typedef_Node, Copy_Separate_Tree (Expr_Node));
2420 Set_Attribute_Name (High_Bound (Typedef_Node), Name_Last);
2422 -- Normal case of explicit range
2424 else
2425 Check_Simple_Expression (Expr_Node);
2426 Set_Low_Bound (Typedef_Node, Expr_Node);
2427 T_Dot_Dot;
2428 Expr_Node := P_Expression;
2429 Check_Simple_Expression (Expr_Node);
2430 Set_High_Bound (Typedef_Node, Expr_Node);
2431 end if;
2433 return Typedef_Node;
2434 end P_Signed_Integer_Type_Definition;
2436 ------------------------------------
2437 -- 3.5.4 Modular Type Definition --
2438 ------------------------------------
2440 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
2442 -- The caller has checked that the initial token is MOD
2444 -- Error recovery: cannot raise Error_Resync
2446 function P_Modular_Type_Definition return Node_Id is
2447 Typedef_Node : Node_Id;
2449 begin
2450 if Ada_Version = Ada_83 then
2451 Error_Msg_SC ("(Ada 83) modular types not allowed");
2452 end if;
2454 Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
2455 Scan; -- past MOD
2456 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2458 -- Handle mod L..R cleanly
2460 if Token = Tok_Dot_Dot then
2461 Error_Msg_SC ("range not allowed for modular type");
2462 Scan; -- past ..
2463 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2464 end if;
2466 return Typedef_Node;
2467 end P_Modular_Type_Definition;
2469 ---------------------------------
2470 -- 3.5.6 Real Type Definition --
2471 ---------------------------------
2473 -- Parsed by P_Type_Declaration (3.2.1)
2475 --------------------------------------
2476 -- 3.5.7 Floating Point Definition --
2477 --------------------------------------
2479 -- FLOATING_POINT_DEFINITION ::=
2480 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2482 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
2484 -- The caller has checked that the initial token is DIGITS
2486 -- Error recovery: cannot raise Error_Resync
2488 function P_Floating_Point_Definition return Node_Id is
2489 Digits_Loc : constant Source_Ptr := Token_Ptr;
2490 Def_Node : Node_Id;
2491 Expr_Node : Node_Id;
2493 begin
2494 Scan; -- past DIGITS
2495 Expr_Node := P_Expression_No_Right_Paren;
2496 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2498 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
2500 if Token = Tok_Delta then
2501 Error_Msg_SC -- CODEFIX
2502 ("|DELTA must come before DIGITS");
2503 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
2504 Scan; -- past DELTA
2505 Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
2507 -- OK floating-point definition
2509 else
2510 Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
2511 end if;
2513 Set_Digits_Expression (Def_Node, Expr_Node);
2514 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2515 return Def_Node;
2516 end P_Floating_Point_Definition;
2518 -------------------------------------
2519 -- 3.5.7 Real Range Specification --
2520 -------------------------------------
2522 -- REAL_RANGE_SPECIFICATION ::=
2523 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2525 -- Error recovery: cannot raise Error_Resync
2527 function P_Real_Range_Specification_Opt return Node_Id is
2528 Specification_Node : Node_Id;
2529 Expr_Node : Node_Id;
2531 begin
2532 if Token = Tok_Range then
2533 Specification_Node :=
2534 New_Node (N_Real_Range_Specification, Token_Ptr);
2535 Scan; -- past RANGE
2536 Expr_Node := P_Expression_No_Right_Paren;
2537 Check_Simple_Expression (Expr_Node);
2538 Set_Low_Bound (Specification_Node, Expr_Node);
2539 T_Dot_Dot;
2540 Expr_Node := P_Expression_No_Right_Paren;
2541 Check_Simple_Expression (Expr_Node);
2542 Set_High_Bound (Specification_Node, Expr_Node);
2543 return Specification_Node;
2544 else
2545 return Empty;
2546 end if;
2547 end P_Real_Range_Specification_Opt;
2549 -----------------------------------
2550 -- 3.5.9 Fixed Point Definition --
2551 -----------------------------------
2553 -- FIXED_POINT_DEFINITION ::=
2554 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
2556 -- ORDINARY_FIXED_POINT_DEFINITION ::=
2557 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
2559 -- DECIMAL_FIXED_POINT_DEFINITION ::=
2560 -- delta static_EXPRESSION
2561 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2563 -- The caller has checked that the initial token is DELTA
2565 -- Error recovery: cannot raise Error_Resync
2567 function P_Fixed_Point_Definition return Node_Id is
2568 Delta_Node : Node_Id;
2569 Delta_Loc : Source_Ptr;
2570 Def_Node : Node_Id;
2571 Expr_Node : Node_Id;
2573 begin
2574 Delta_Loc := Token_Ptr;
2575 Scan; -- past DELTA
2576 Delta_Node := P_Expression_No_Right_Paren;
2577 Check_Simple_Expression_In_Ada_83 (Delta_Node);
2579 if Token = Tok_Digits then
2580 if Ada_Version = Ada_83 then
2581 Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
2582 end if;
2584 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
2585 Scan; -- past DIGITS
2586 Expr_Node := P_Expression_No_Right_Paren;
2587 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2588 Set_Digits_Expression (Def_Node, Expr_Node);
2590 else
2591 Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
2593 -- Range is required in ordinary fixed point case
2595 if Token /= Tok_Range then
2596 Error_Msg_AP ("range must be given for fixed-point type");
2597 T_Range;
2598 end if;
2599 end if;
2601 Set_Delta_Expression (Def_Node, Delta_Node);
2602 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2603 return Def_Node;
2604 end P_Fixed_Point_Definition;
2606 --------------------------------------------
2607 -- 3.5.9 Ordinary Fixed Point Definition --
2608 --------------------------------------------
2610 -- Parsed by P_Fixed_Point_Definition (3.5.9)
2612 -------------------------------------------
2613 -- 3.5.9 Decimal Fixed Point Definition --
2614 -------------------------------------------
2616 -- Parsed by P_Decimal_Point_Definition (3.5.9)
2618 ------------------------------
2619 -- 3.5.9 Digits Constraint --
2620 ------------------------------
2622 -- DIGITS_CONSTRAINT ::=
2623 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
2625 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2627 -- The caller has checked that the initial token is DIGITS
2629 function P_Digits_Constraint return Node_Id is
2630 Constraint_Node : Node_Id;
2631 Expr_Node : Node_Id;
2633 begin
2634 Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
2635 Scan; -- past DIGITS
2636 Expr_Node := P_Expression;
2637 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2638 Set_Digits_Expression (Constraint_Node, Expr_Node);
2640 if Token = Tok_Range then
2641 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2642 end if;
2644 return Constraint_Node;
2645 end P_Digits_Constraint;
2647 -----------------------------
2648 -- 3.5.9 Delta Constraint --
2649 -----------------------------
2651 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2653 -- Note: this is an obsolescent feature in Ada 95 (I.3)
2655 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2656 -- (also true in formal modes).
2658 -- The caller has checked that the initial token is DELTA
2660 -- Error recovery: cannot raise Error_Resync
2662 function P_Delta_Constraint return Node_Id is
2663 Constraint_Node : Node_Id;
2664 Expr_Node : Node_Id;
2666 begin
2667 Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2668 Scan; -- past DELTA
2669 Expr_Node := P_Expression;
2670 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2672 Set_Delta_Expression (Constraint_Node, Expr_Node);
2674 if Token = Tok_Range then
2675 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2676 end if;
2678 return Constraint_Node;
2679 end P_Delta_Constraint;
2681 --------------------------------
2682 -- 3.6 Array Type Definition --
2683 --------------------------------
2685 -- ARRAY_TYPE_DEFINITION ::=
2686 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2688 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2689 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2690 -- COMPONENT_DEFINITION
2692 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2694 -- CONSTRAINED_ARRAY_DEFINITION ::=
2695 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2696 -- COMPONENT_DEFINITION
2698 -- DISCRETE_SUBTYPE_DEFINITION ::=
2699 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2701 -- COMPONENT_DEFINITION ::=
2702 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
2704 -- The caller has checked that the initial token is ARRAY
2706 -- Error recovery: can raise Error_Resync
2708 function P_Array_Type_Definition return Node_Id is
2709 Array_Loc : Source_Ptr;
2710 CompDef_Node : Node_Id;
2711 Def_Node : Node_Id;
2712 Not_Null_Present : Boolean := False;
2713 Subs_List : List_Id;
2714 Scan_State : Saved_Scan_State;
2715 Aliased_Present : Boolean := False;
2717 procedure P_Index_Subtype_Def_With_Fixed_Lower_Bound
2718 (Subtype_Mark : Node_Id);
2719 -- Parse an unconstrained index range with a fixed lower bound:
2720 -- subtype_mark range <expression> .. <>
2721 -- This procedure creates a subtype_indication node for the index.
2723 --------------------------------------------
2724 -- P_Index_Range_With_Fixed_Lower_Bound --
2725 --------------------------------------------
2727 procedure P_Index_Subtype_Def_With_Fixed_Lower_Bound
2728 (Subtype_Mark : Node_Id)
2730 Low_Expr_Node : constant Node_Id := P_Expression;
2731 High_Expr_Node : Node_Id;
2732 Indic_Node : Node_Id;
2733 Constr_Node : Node_Id;
2734 Range_Node : Node_Id;
2736 begin
2737 T_Dot_Dot; -- Error if no ..
2739 -- A box is required at this point, and we'll set the upper bound to
2740 -- the same expression as the lower bound (see further below), to
2741 -- avoid problems with trying to analyze an Empty node. Analysis can
2742 -- still tell that this is a fixed-lower-bound range because the
2743 -- index is represented by a subtype_indication in an unconstrained
2744 -- array type definition.
2746 if Token = Tok_Box then
2747 Scan;
2748 High_Expr_Node := Low_Expr_Node;
2750 -- Error if no <> was found, and try to parse an expression since
2751 -- it's likely one was given in place of the <>.
2753 else
2754 Error_Msg_AP -- CODEFIX
2755 ("missing ""'<'>""");
2757 High_Expr_Node := P_Expression;
2758 end if;
2760 Constr_Node := New_Node (N_Range_Constraint, Token_Ptr);
2761 Range_Node := New_Node (N_Range, Token_Ptr);
2762 Set_Range_Expression (Constr_Node, Range_Node);
2764 Check_Simple_Expression (Low_Expr_Node);
2766 Set_Low_Bound (Range_Node, Low_Expr_Node);
2767 Set_High_Bound (Range_Node, High_Expr_Node);
2769 Indic_Node :=
2770 New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
2771 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
2772 Set_Constraint (Indic_Node, Constr_Node);
2774 Append (Indic_Node, Subs_List);
2775 end P_Index_Subtype_Def_With_Fixed_Lower_Bound;
2777 -- Local variables
2779 Is_Constrained_Array_Def : Boolean := True;
2780 Subtype_Mark_Node : Node_Id;
2782 -- Start of processing for P_Array_Type_Definition
2784 begin
2785 Array_Loc := Token_Ptr;
2786 Scan; -- past ARRAY
2787 Subs_List := New_List;
2788 T_Left_Paren;
2790 -- It's quite tricky to disentangle these two possibilities, so we do
2791 -- a prescan to determine which case we have and then reset the scan.
2792 -- The prescan skips past possible subtype mark tokens.
2794 Save_Scan_State (Scan_State); -- just after paren
2796 while Token in Token_Class_Desig or else
2797 Token = Tok_Dot or else
2798 Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2799 loop
2800 Scan;
2801 end loop;
2803 -- If we end up on RANGE <> then we have the unconstrained case. We
2804 -- will also allow the RANGE to be omitted, just to improve error
2805 -- handling for a case like array (integer <>) of integer;
2807 Scan; -- past possible RANGE or <>
2809 if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2810 Prev_Token = Tok_Box
2811 then
2812 Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2813 Restore_Scan_State (Scan_State); -- to first subtype mark
2815 Is_Constrained_Array_Def := False;
2817 -- Now parse a sequence of indexes where each is either of form:
2818 -- <subtype_mark> range <>
2819 -- or
2820 -- <subtype_mark> range <expr> .. <>
2822 -- The latter syntax indicates an index with a fixed lower bound,
2823 -- and only applies when extensions are enabled (-gnatX).
2825 loop
2826 Subtype_Mark_Node := P_Subtype_Mark_Resync;
2828 T_Range;
2830 -- Normal "subtype_mark range <>" form, so simply append
2831 -- the subtype reference.
2833 if Token = Tok_Box then
2834 Append (Subtype_Mark_Node, Subs_List);
2835 Scan;
2837 -- Fixed-lower-bound form ("subtype_mark range <expr> .. <>")
2839 else
2840 P_Index_Subtype_Def_With_Fixed_Lower_Bound (Subtype_Mark_Node);
2842 Error_Msg_GNAT_Extension ("fixed-lower-bound array", Token_Ptr,
2843 Is_Core_Extension => True);
2844 end if;
2846 exit when Token in Tok_Right_Paren | Tok_Of;
2847 T_Comma;
2848 end loop;
2850 Set_Subtype_Marks (Def_Node, Subs_List);
2852 -- If we don't have "range <>", then "range" will be followed by an
2853 -- expression, for either a normal range or a fixed-lower-bound range
2854 -- ("<exp> .. <>"), and we have to know which, in order to determine
2855 -- whether to parse the indexes for an unconstrained or constrained
2856 -- array definition. So we look ahead to see if "<>" follows the "..".
2857 -- If not, then this must be a discrete_subtype_indication for a
2858 -- constrained_array_definition, which will be processed further below.
2860 elsif Prev_Token = Tok_Range
2861 and then Token not in Tok_Right_Paren | Tok_Comma
2862 then
2863 -- If we have an expression followed by "..", then scan farther
2864 -- and check for "<>" to see if we have a fixed-lower-bound range.
2866 if P_Expression_Or_Range_Attribute /= Error
2867 and then Expr_Form /= EF_Range_Attr
2868 and then Token = Tok_Dot_Dot
2869 then
2870 Scan;
2872 -- If there's a "<>", then we know we have a fixed-lower-bound
2873 -- index, so we can proceed with parsing an unconstrained array
2874 -- definition.
2876 if Token = Tok_Box then
2877 Is_Constrained_Array_Def := False;
2879 Def_Node :=
2880 New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2882 Restore_Scan_State (Scan_State); -- to first subtype mark
2884 -- Now parse a sequence of indexes where each is either of
2885 -- form:
2886 -- <subtype_mark> range <>
2887 -- or
2888 -- <subtype_mark> range <expr> .. <>
2890 -- The latter indicates an index with a fixed lower bound,
2891 -- and only applies when extensions are enabled (-gnatX).
2893 loop
2894 Subtype_Mark_Node := P_Subtype_Mark_Resync;
2896 T_Range;
2898 -- Normal "subtype_mark range <>" form, so simply append
2899 -- the subtype reference.
2901 if Token = Tok_Box then
2902 Append (Subtype_Mark_Node, Subs_List);
2903 Scan;
2905 -- This must be an index of form:
2906 -- <subtype_mark> range <expr> .. <>"
2908 else
2909 P_Index_Subtype_Def_With_Fixed_Lower_Bound
2910 (Subtype_Mark_Node);
2912 Error_Msg_GNAT_Extension
2913 ("fixed-lower-bound array", Token_Ptr,
2914 Is_Core_Extension => True);
2915 end if;
2917 exit when Token in Tok_Right_Paren | Tok_Of;
2918 T_Comma;
2919 end loop;
2921 Set_Subtype_Marks (Def_Node, Subs_List);
2922 end if;
2923 end if;
2924 end if;
2926 if Is_Constrained_Array_Def then
2927 Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2928 Restore_Scan_State (Scan_State); -- to first discrete range
2930 loop
2931 Append (P_Discrete_Subtype_Definition, Subs_List);
2932 exit when not Comma_Present;
2933 end loop;
2935 Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2936 end if;
2938 T_Right_Paren;
2939 T_Of;
2941 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2943 if Token_Name = Name_Aliased then
2944 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2945 end if;
2947 if Token = Tok_Aliased then
2948 Aliased_Present := True;
2949 Scan; -- past ALIASED
2950 end if;
2952 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
2954 -- Ada 2005 (AI-230): Access Definition case
2956 if Token = Tok_Access then
2957 Error_Msg_Ada_2005_Extension
2958 ("generalized use of anonymous access types");
2960 -- AI95-406 makes "aliased" legal (and useless) in this context so
2961 -- followintg code which used to be needed is commented out.
2963 -- if Aliased_Present then
2964 -- Error_Msg_SP ("ALIASED not allowed here");
2965 -- end if;
2967 Set_Subtype_Indication (CompDef_Node, Empty);
2968 Set_Aliased_Present (CompDef_Node, Aliased_Present);
2969 Set_Access_Definition (CompDef_Node,
2970 P_Access_Definition (Not_Null_Present));
2971 else
2973 Set_Access_Definition (CompDef_Node, Empty);
2974 Set_Aliased_Present (CompDef_Node, Aliased_Present);
2975 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
2976 Set_Subtype_Indication (CompDef_Node,
2977 P_Subtype_Indication (Not_Null_Present));
2978 end if;
2980 Set_Component_Definition (Def_Node, CompDef_Node);
2982 return Def_Node;
2983 end P_Array_Type_Definition;
2985 -----------------------------------------
2986 -- 3.6 Unconstrained Array Definition --
2987 -----------------------------------------
2989 -- Parsed by P_Array_Type_Definition (3.6)
2991 ---------------------------------------
2992 -- 3.6 Constrained Array Definition --
2993 ---------------------------------------
2995 -- Parsed by P_Array_Type_Definition (3.6)
2997 --------------------------------------
2998 -- 3.6 Discrete Subtype Definition --
2999 --------------------------------------
3001 -- DISCRETE_SUBTYPE_DEFINITION ::=
3002 -- discrete_SUBTYPE_INDICATION | RANGE
3004 -- Note: the discrete subtype definition appearing in a constrained
3005 -- array definition is parsed by P_Array_Type_Definition (3.6)
3007 -- Error recovery: cannot raise Error_Resync
3009 function P_Discrete_Subtype_Definition return Node_Id is
3010 begin
3011 -- The syntax of a discrete subtype definition is identical to that
3012 -- of a discrete range, so we simply share the same parsing code.
3014 return P_Discrete_Range;
3015 end P_Discrete_Subtype_Definition;
3017 -------------------------------
3018 -- 3.6 Component Definition --
3019 -------------------------------
3021 -- For the array case, parsed by P_Array_Type_Definition (3.6)
3022 -- For the record case, parsed by P_Component_Declaration (3.8)
3024 -----------------------------
3025 -- 3.6.1 Index Constraint --
3026 -----------------------------
3028 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
3030 ---------------------------
3031 -- 3.6.1 Discrete Range --
3032 ---------------------------
3034 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
3036 -- The possible forms for a discrete range are:
3038 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
3039 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
3040 -- Range_Attribute (RANGE, 3.5)
3041 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
3043 -- Error recovery: cannot raise Error_Resync
3045 function P_Discrete_Range return Node_Id is
3046 Expr_Node : Node_Id;
3047 Range_Node : Node_Id;
3049 begin
3050 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
3052 if Expr_Form = EF_Range_Attr then
3053 return Expr_Node;
3055 elsif Token = Tok_Range then
3056 if Expr_Form /= EF_Simple_Name then
3057 Error_Msg_SC ("range must be preceded by subtype mark");
3058 end if;
3060 return P_Subtype_Indication (Expr_Node);
3062 -- Check Expression .. Expression case
3064 elsif Token = Tok_Dot_Dot then
3065 Range_Node := New_Node (N_Range, Token_Ptr);
3066 Set_Low_Bound (Range_Node, Expr_Node);
3068 if Style_Check then
3069 Style.Check_Xtra_Parens (Expr_Node);
3070 end if;
3072 Scan; -- past ..
3073 Expr_Node := P_Expression;
3074 Check_Simple_Expression (Expr_Node);
3075 Set_High_Bound (Range_Node, Expr_Node);
3077 -- If Expr_Node (ignoring parentheses) is not a simple expression
3078 -- then emit a style check.
3080 if Style_Check
3081 and then Nkind (Expr_Node) not in N_Op_Boolean | N_Subexpr
3082 then
3083 Style.Check_Xtra_Parens (Expr_Node);
3084 end if;
3086 return Range_Node;
3088 -- Otherwise we must have a subtype mark, or an Ada 2012 iterator
3090 elsif Expr_Form = EF_Simple_Name then
3091 return Expr_Node;
3093 -- The domain of iteration must be a name. Semantics will determine that
3094 -- the expression has the proper form.
3096 elsif Ada_Version >= Ada_2012 then
3097 return Expr_Node;
3099 -- If incorrect, complain that we expect ..
3101 else
3102 T_Dot_Dot;
3103 return Expr_Node;
3104 end if;
3105 end P_Discrete_Range;
3107 ----------------------------
3108 -- 3.7 Discriminant Part --
3109 ----------------------------
3111 -- DISCRIMINANT_PART ::=
3112 -- UNKNOWN_DISCRIMINANT_PART
3113 -- | KNOWN_DISCRIMINANT_PART
3115 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
3116 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
3118 ------------------------------------
3119 -- 3.7 Unknown Discriminant Part --
3120 ------------------------------------
3122 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
3124 -- If no unknown discriminant part is present, then False is returned,
3125 -- otherwise the unknown discriminant is scanned out and True is returned.
3127 -- Error recovery: cannot raise Error_Resync
3129 function P_Unknown_Discriminant_Part_Opt return Boolean is
3130 Scan_State : Saved_Scan_State;
3132 begin
3133 -- If <> right now, then this is missing left paren
3135 if Token = Tok_Box then
3136 U_Left_Paren;
3138 -- If not <> or left paren, then definitely no box
3140 elsif Token /= Tok_Left_Paren then
3141 return False;
3143 -- Left paren, so might be a box after it
3145 else
3146 Save_Scan_State (Scan_State);
3147 Scan; -- past the left paren
3149 if Token /= Tok_Box then
3150 Restore_Scan_State (Scan_State);
3151 return False;
3152 end if;
3153 end if;
3155 -- We are now pointing to the box
3157 if Ada_Version = Ada_83 then
3158 Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
3159 end if;
3161 Scan; -- past the box
3162 U_Right_Paren; -- must be followed by right paren
3163 return True;
3164 end P_Unknown_Discriminant_Part_Opt;
3166 ----------------------------------
3167 -- 3.7 Known Discriminant Part --
3168 ----------------------------------
3170 -- KNOWN_DISCRIMINANT_PART ::=
3171 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
3173 -- DISCRIMINANT_SPECIFICATION ::=
3174 -- DEFINING_IDENTIFIER_LIST : [NULL_EXCLUSION] SUBTYPE_MARK
3175 -- [:= DEFAULT_EXPRESSION] [ASPECT_SPECIFICATION]
3176 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
3177 -- [:= DEFAULT_EXPRESSION] [ASPECT_SPECIFICATION]
3179 -- If no known discriminant part is present, then No_List is returned
3181 -- Error recovery: cannot raise Error_Resync
3183 function P_Known_Discriminant_Part_Opt return List_Id is
3184 Specification_Node : Node_Id;
3185 Specification_List : List_Id;
3186 Ident_Sloc : Source_Ptr;
3187 Scan_State : Saved_Scan_State;
3188 Num_Idents : Nat;
3189 Not_Null_Present : Boolean;
3190 Ident : Nat;
3192 Idents : array (Int range 1 .. 4096) of Entity_Id;
3193 -- This array holds the list of defining identifiers. The upper bound
3194 -- of 4096 is intended to be essentially infinite, and we do not even
3195 -- bother to check for it being exceeded.
3197 begin
3198 if Token = Tok_Left_Paren then
3199 Specification_List := New_List;
3200 Scan; -- past (
3201 P_Pragmas_Misplaced;
3203 Specification_Loop : loop
3205 Ident_Sloc := Token_Ptr;
3206 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3207 Num_Idents := 1;
3209 while Comma_Present loop
3210 Num_Idents := Num_Idents + 1;
3211 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3212 end loop;
3214 -- If there are multiple identifiers, we repeatedly scan the
3215 -- type and initialization expression information by resetting
3216 -- the scan pointer (so that we get completely separate trees
3217 -- for each occurrence).
3219 if Num_Idents > 1 then
3220 Save_Scan_State (Scan_State);
3221 end if;
3223 T_Colon;
3225 -- Loop through defining identifiers in list
3227 Ident := 1;
3228 Ident_Loop : loop
3229 Specification_Node :=
3230 New_Node (N_Discriminant_Specification, Ident_Sloc);
3231 Set_Defining_Identifier (Specification_Node, Idents (Ident));
3232 Not_Null_Present := -- Ada 2005 (AI-231, AI-447)
3233 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
3235 if Token = Tok_Access then
3236 if Ada_Version = Ada_83 then
3237 Error_Msg_SC
3238 ("(Ada 83) access discriminant not allowed!");
3239 end if;
3241 Set_Discriminant_Type
3242 (Specification_Node,
3243 P_Access_Definition (Not_Null_Present));
3245 -- Catch ouf-of-order keywords
3247 elsif Token = Tok_Constant then
3248 Scan;
3250 if Token = Tok_Access then
3251 Error_Msg_SC -- CODEFIX
3252 ("ACCESS must come before CONSTANT");
3253 Set_Discriminant_Type
3254 (Specification_Node,
3255 P_Access_Definition (Not_Null_Present));
3257 else
3258 Error_Msg_SC ("misplaced CONSTANT");
3259 end if;
3261 else
3262 Set_Discriminant_Type
3263 (Specification_Node, P_Subtype_Mark);
3264 No_Constraint;
3265 Set_Null_Exclusion_Present -- Ada 2005 (AI-231)
3266 (Specification_Node, Not_Null_Present);
3267 end if;
3269 Set_Expression
3270 (Specification_Node, Init_Expr_Opt (True));
3272 if Token = Tok_With then
3273 P_Aspect_Specifications (Specification_Node, False);
3274 end if;
3276 if Ident > 1 then
3277 Set_Prev_Ids (Specification_Node, True);
3278 end if;
3280 if Ident < Num_Idents then
3281 Set_More_Ids (Specification_Node, True);
3282 end if;
3284 Append (Specification_Node, Specification_List);
3285 exit Ident_Loop when Ident = Num_Idents;
3286 Ident := Ident + 1;
3287 Restore_Scan_State (Scan_State);
3288 T_Colon;
3289 end loop Ident_Loop;
3291 exit Specification_Loop when Token /= Tok_Semicolon;
3292 Scan; -- past ;
3293 P_Pragmas_Misplaced;
3294 end loop Specification_Loop;
3296 T_Right_Paren;
3297 return Specification_List;
3299 else
3300 return No_List;
3301 end if;
3302 end P_Known_Discriminant_Part_Opt;
3304 -------------------------------------
3305 -- 3.7 Discriminant Specification --
3306 -------------------------------------
3308 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
3310 -----------------------------
3311 -- 3.7 Default Expression --
3312 -----------------------------
3314 -- Always parsed (simply as an Expression) by the parent construct
3316 ------------------------------------
3317 -- 3.7.1 Discriminant Constraint --
3318 ------------------------------------
3320 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
3322 --------------------------------------------------------
3323 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
3324 --------------------------------------------------------
3326 -- DISCRIMINANT_CONSTRAINT ::=
3327 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
3329 -- DISCRIMINANT_ASSOCIATION ::=
3330 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3331 -- EXPRESSION
3333 -- This routine parses either an index or a discriminant constraint. As
3334 -- is clear from the above grammar, it is often possible to clearly
3335 -- determine which of the two possibilities we have, but there are
3336 -- cases (those in which we have a series of expressions of the same
3337 -- syntactic form as subtype indications), where we cannot tell. Since
3338 -- this means that in any case the semantic phase has to distinguish
3339 -- between the two, there is not much point in the parser trying to
3340 -- distinguish even those cases where the difference is clear. In any
3341 -- case, if we have a situation like:
3343 -- (A => 123, 235 .. 500)
3345 -- it is not clear which of the two items is the wrong one, better to
3346 -- let the semantic phase give a clear message. Consequently, this
3347 -- routine in general returns a list of items which can be either
3348 -- discrete ranges or discriminant associations.
3350 -- The caller has checked that the initial token is a left paren
3352 -- Error recovery: can raise Error_Resync
3354 function P_Index_Or_Discriminant_Constraint return Node_Id is
3355 Scan_State : Saved_Scan_State;
3356 Constr_Node : Node_Id;
3357 Constr_List : List_Id;
3358 Expr_Node : Node_Id;
3359 Result_Node : Node_Id;
3361 begin
3362 Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
3363 Scan; -- past (
3364 Constr_List := New_List;
3365 Set_Constraints (Result_Node, Constr_List);
3367 -- The two syntactic forms are a little mixed up, so what we are doing
3368 -- here is looking at the first entry to determine which case we have
3370 -- A discriminant constraint is a list of discriminant associations,
3371 -- which have one of the following possible forms:
3373 -- Expression
3374 -- Id => Expression
3375 -- Id | Id | .. | Id => Expression
3377 -- An index constraint is a list of discrete ranges which have one
3378 -- of the following possible forms:
3380 -- Subtype_Mark
3381 -- Subtype_Mark range Range
3382 -- Range_Attribute
3383 -- Simple_Expression .. Simple_Expression
3385 -- Loop through discriminants in list
3387 loop
3388 -- Check cases of Id => Expression or Id | Id => Expression
3390 if Token = Tok_Identifier then
3391 Save_Scan_State (Scan_State); -- at Id
3392 Scan; -- past Id
3394 if Token in Tok_Arrow | Tok_Vertical_Bar then
3395 Restore_Scan_State (Scan_State); -- to Id
3396 Append (P_Discriminant_Association, Constr_List);
3397 goto Loop_Continue;
3398 else
3399 Restore_Scan_State (Scan_State); -- to Id
3400 end if;
3401 end if;
3403 -- Otherwise scan out an expression and see what we have got
3405 Expr_Node := P_Expression_Or_Range_Attribute;
3407 if Expr_Form = EF_Range_Attr then
3408 Append (Expr_Node, Constr_List);
3410 elsif Token = Tok_Range then
3411 if Expr_Form /= EF_Simple_Name then
3412 Error_Msg_SC ("subtype mark required before RANGE");
3413 end if;
3415 Append (P_Subtype_Indication (Expr_Node), Constr_List);
3416 goto Loop_Continue;
3418 -- Check Simple_Expression .. Simple_Expression case
3420 elsif Token = Tok_Dot_Dot then
3421 Check_Simple_Expression (Expr_Node);
3422 Constr_Node := New_Node (N_Range, Token_Ptr);
3423 Set_Low_Bound (Constr_Node, Expr_Node);
3424 Scan; -- past ..
3426 -- If the upper bound is given by "<>", this is an index for
3427 -- a fixed-lower-bound subtype, so set the expression to Empty
3428 -- for now (it will be set to the ranges maximum upper bound
3429 -- later during analysis), and scan to the next token.
3431 if Token = Tok_Box then
3432 Error_Msg_GNAT_Extension ("fixed-lower-bound array", Token_Ptr,
3433 Is_Core_Extension => True);
3435 Expr_Node := Empty;
3436 Scan;
3438 -- Otherwise parse the range's upper bound expression
3440 else
3441 Expr_Node := P_Expression;
3442 Check_Simple_Expression (Expr_Node);
3443 end if;
3445 Set_High_Bound (Constr_Node, Expr_Node);
3446 Append (Constr_Node, Constr_List);
3447 goto Loop_Continue;
3449 -- Case of an expression which could be either form
3451 else
3452 Append (Expr_Node, Constr_List);
3453 goto Loop_Continue;
3454 end if;
3456 -- Here with a single entry scanned
3458 <<Loop_Continue>>
3459 exit when not Comma_Present;
3461 end loop;
3463 T_Right_Paren;
3464 return Result_Node;
3465 end P_Index_Or_Discriminant_Constraint;
3467 -------------------------------------
3468 -- 3.7.1 Discriminant Association --
3469 -------------------------------------
3471 -- DISCRIMINANT_ASSOCIATION ::=
3472 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3473 -- EXPRESSION
3475 -- This routine is used only when the name list is present and the caller
3476 -- has already checked this (by scanning ahead and repositioning the
3477 -- scan).
3479 -- Error_Recovery: cannot raise Error_Resync;
3481 function P_Discriminant_Association return Node_Id is
3482 Discr_Node : Node_Id;
3483 Names_List : List_Id;
3484 Ident_Sloc : Source_Ptr;
3486 begin
3487 Ident_Sloc := Token_Ptr;
3488 Names_List := New_List;
3490 loop
3491 Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
3492 exit when Token /= Tok_Vertical_Bar;
3493 Scan; -- past |
3494 end loop;
3496 Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
3497 Set_Selector_Names (Discr_Node, Names_List);
3498 TF_Arrow;
3499 Set_Expression (Discr_Node, P_Expression);
3500 return Discr_Node;
3501 end P_Discriminant_Association;
3503 ---------------------------------
3504 -- 3.8 Record Type Definition --
3505 ---------------------------------
3507 -- RECORD_TYPE_DEFINITION ::=
3508 -- [[abstract] tagged] [limited] RECORD_DEFINITION
3510 -- There is no node in the tree for a record type definition. Instead
3511 -- a record definition node appears, with possible Abstract_Present,
3512 -- Tagged_Present, and Limited_Present flags set appropriately.
3514 ----------------------------
3515 -- 3.8 Record Definition --
3516 ----------------------------
3518 -- RECORD_DEFINITION ::=
3519 -- record
3520 -- COMPONENT_LIST
3521 -- end record
3522 -- | null record
3524 -- Note: in the case where a record definition node is used to represent
3525 -- a record type definition, the caller sets the Tagged_Present and
3526 -- Limited_Present flags in the resulting N_Record_Definition node as
3527 -- required.
3529 -- Note that the RECORD token at the start may be missing in certain
3530 -- error situations, so this function is expected to post the error
3532 -- Error recovery: can raise Error_Resync
3534 function P_Record_Definition return Node_Id is
3536 procedure Catch_Out_Of_Order_Keywords (Keyword : String);
3537 -- Catch ouf-of-order keywords in a record definition
3539 ---------------------------------
3540 -- Catch_Out_Of_Order_Keywords --
3541 ---------------------------------
3543 procedure Catch_Out_Of_Order_Keywords (Keyword : String) is
3544 begin
3545 loop
3546 if Token = Tok_Abstract then
3547 Error_Msg_SC -- CODEFIX
3548 ("ABSTRACT must come before " & Keyword);
3549 Scan; -- past ABSTRACT
3551 elsif Token = Tok_Tagged then
3552 Error_Msg_SC -- CODEFIX
3553 ("TAGGED must come before " & Keyword);
3554 Scan; -- past TAGGED
3556 elsif Token = Tok_Limited then
3557 Error_Msg_SC -- CODEFIX
3558 ("LIMITED must come before " & Keyword);
3559 Scan; -- past LIMITED
3561 else
3562 exit;
3563 end if;
3564 end loop;
3565 end Catch_Out_Of_Order_Keywords;
3567 Rec_Node : Node_Id;
3569 -- Start of processing for P_Record_Definition
3571 begin
3572 Inside_Record_Definition := True;
3573 Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
3575 -- Null record case
3577 if Token = Tok_Null then
3578 Scan; -- past NULL
3580 Catch_Out_Of_Order_Keywords ("NULL");
3581 T_Record;
3582 Set_Null_Present (Rec_Node, True);
3583 Catch_Out_Of_Order_Keywords ("RECORD");
3585 -- Catch incomplete declaration to prevent cascaded errors, see
3586 -- ACATS B393002 for an example.
3588 elsif Token = Tok_Semicolon then
3589 Error_Msg_AP ("missing record definition");
3591 -- Case starting with RECORD keyword. Build scope stack entry. For the
3592 -- column, we use the first non-blank character on the line, to deal
3593 -- with situations such as:
3595 -- type X is record
3596 -- ...
3597 -- end record;
3599 -- which is not official RM indentation, but is not uncommon usage, and
3600 -- in particular is standard GNAT coding style, so handle it nicely.
3602 else
3603 Push_Scope_Stack;
3604 Scopes (Scope.Last).Etyp := E_Record;
3605 Scopes (Scope.Last).Ecol := Start_Column;
3606 Scopes (Scope.Last).Sloc := Token_Ptr;
3607 Scopes (Scope.Last).Labl := Error;
3608 Scopes (Scope.Last).Junk := (Token /= Tok_Record);
3610 T_Record;
3611 Catch_Out_Of_Order_Keywords ("RECORD");
3613 Set_Component_List (Rec_Node, P_Component_List);
3615 loop
3616 exit when Check_End;
3617 Discard_Junk_Node (P_Component_List);
3618 end loop;
3619 end if;
3621 Inside_Record_Definition := False;
3622 return Rec_Node;
3623 end P_Record_Definition;
3625 -------------------------
3626 -- 3.8 Component List --
3627 -------------------------
3629 -- COMPONENT_LIST ::=
3630 -- COMPONENT_ITEM {COMPONENT_ITEM}
3631 -- | {COMPONENT_ITEM} VARIANT_PART
3632 -- | null;
3634 -- Error recovery: cannot raise Error_Resync
3636 function P_Component_List return Node_Id is
3637 Component_List_Node : Node_Id;
3638 Decls_List : List_Id;
3639 Scan_State : Saved_Scan_State;
3640 Null_Loc : Source_Ptr;
3642 begin
3643 Component_List_Node := New_Node (N_Component_List, Token_Ptr);
3644 Decls_List := New_List;
3646 -- Handle null
3648 if Token = Tok_Null then
3649 Null_Loc := Token_Ptr;
3650 Scan; -- past NULL
3651 TF_Semicolon;
3652 P_Pragmas_Opt (Decls_List);
3654 -- If we have an END or WHEN now, everything is fine, otherwise we
3655 -- complain about the null, ignore it, and scan for more components.
3657 if Token in Tok_End | Tok_When then
3658 Set_Null_Present (Component_List_Node, True);
3659 return Component_List_Node;
3660 else
3661 Error_Msg ("NULL component only allowed in null record", Null_Loc);
3662 end if;
3663 end if;
3665 -- Scan components for non-null record
3667 P_Pragmas_Opt (Decls_List);
3669 if Token /= Tok_Case then
3670 loop
3671 P_Component_Items (Decls_List);
3672 P_Pragmas_Opt (Decls_List);
3674 exit when Token in Tok_End | Tok_Case | Tok_When;
3676 -- We are done if we do not have an identifier. However, if we
3677 -- have a misspelled reserved identifier that is in a column to
3678 -- the right of the record definition, we will treat it as an
3679 -- identifier. It turns out to be too dangerous in practice to
3680 -- accept such a mis-spelled identifier which does not have this
3681 -- additional clue that confirms the incorrect spelling.
3683 if Token /= Tok_Identifier then
3684 if Start_Column > Scopes (Scope.Last).Ecol
3685 and then Is_Reserved_Identifier
3686 then
3687 Save_Scan_State (Scan_State); -- at reserved id
3688 Scan; -- possible reserved id
3690 if Token in Tok_Comma | Tok_Colon then
3691 Restore_Scan_State (Scan_State);
3692 Scan_Reserved_Identifier (Force_Msg => True);
3694 -- Note reserved identifier used as field name after all
3695 -- because not followed by colon or comma.
3697 else
3698 Restore_Scan_State (Scan_State);
3699 exit;
3700 end if;
3702 -- Non-identifier that definitely was not reserved id
3704 else
3705 exit;
3706 end if;
3707 end if;
3708 end loop;
3709 end if;
3711 if Token = Tok_Case then
3712 Set_Variant_Part (Component_List_Node, P_Variant_Part);
3714 -- Check for junk after variant part
3716 if Token = Tok_Identifier then
3717 Save_Scan_State (Scan_State);
3718 Scan; -- past identifier
3720 if Token = Tok_Colon then
3721 Restore_Scan_State (Scan_State);
3722 Error_Msg_SC ("component may not follow variant part");
3723 Discard_Junk_Node (P_Component_List);
3725 elsif Token = Tok_Case then
3726 Restore_Scan_State (Scan_State);
3727 Error_Msg_SC ("only one variant part allowed in a record");
3728 Discard_Junk_Node (P_Component_List);
3730 else
3731 Restore_Scan_State (Scan_State);
3732 end if;
3733 end if;
3734 end if;
3736 Set_Component_Items (Component_List_Node, Decls_List);
3737 return Component_List_Node;
3738 end P_Component_List;
3740 -------------------------
3741 -- 3.8 Component Item --
3742 -------------------------
3744 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
3746 -- COMPONENT_DECLARATION ::=
3747 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
3748 -- [:= DEFAULT_EXPRESSION]
3749 -- [ASPECT_SPECIFICATIONS];
3751 -- COMPONENT_DEFINITION ::=
3752 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
3754 -- Error recovery: cannot raise Error_Resync, if an error occurs,
3755 -- the scan is positioned past the following semicolon.
3757 -- Note: we do not yet allow representation clauses to appear as component
3758 -- items, do we need to add this capability sometime in the future ???
3760 procedure P_Component_Items (Decls : List_Id) is
3761 Aliased_Present : Boolean := False;
3762 CompDef_Node : Node_Id;
3763 Decl_Node : Node_Id := Empty; -- initialize to prevent warning
3764 Scan_State : Saved_Scan_State;
3765 Not_Null_Present : Boolean := False;
3766 Num_Idents : Nat;
3767 Ident : Nat;
3768 Ident_Sloc : Source_Ptr;
3770 Idents : array (Int range 1 .. 4096) of Entity_Id;
3771 -- This array holds the list of defining identifiers. The upper bound
3772 -- of 4096 is intended to be essentially infinite, and we do not even
3773 -- bother to check for it being exceeded.
3775 begin
3776 if Token /= Tok_Identifier then
3777 Error_Msg_SC ("component declaration expected");
3778 Resync_Past_Semicolon;
3779 return;
3780 end if;
3782 Ident_Sloc := Token_Ptr;
3783 Check_Bad_Layout;
3784 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3785 Num_Idents := 1;
3787 while Comma_Present loop
3788 Num_Idents := Num_Idents + 1;
3789 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3790 end loop;
3792 -- If there are multiple identifiers, we repeatedly scan the
3793 -- type and initialization expression information by resetting
3794 -- the scan pointer (so that we get completely separate trees
3795 -- for each occurrence).
3797 if Num_Idents > 1 then
3798 Save_Scan_State (Scan_State);
3799 end if;
3801 T_Colon;
3803 -- Loop through defining identifiers in list
3805 Ident := 1;
3806 Ident_Loop : loop
3808 -- The following block is present to catch Error_Resync
3809 -- which causes the parse to be reset past the semicolon
3811 begin
3812 Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
3813 Set_Defining_Identifier (Decl_Node, Idents (Ident));
3815 if Token = Tok_Constant then
3816 Error_Msg_SC ("constant component not permitted");
3817 Scan;
3818 end if;
3820 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
3822 if Token_Name = Name_Aliased then
3823 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
3824 end if;
3826 if Token = Tok_Aliased then
3827 Aliased_Present := True;
3828 Scan; -- past ALIASED
3829 end if;
3831 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
3833 -- Ada 2005 (AI-230): Access Definition case
3835 if Token = Tok_Access then
3836 Error_Msg_Ada_2005_Extension
3837 ("generalized use of anonymous access types");
3839 -- AI95-406 makes "aliased" legal (and useless) here, so the
3840 -- following code which used to be required is commented out.
3842 -- if Aliased_Present then
3843 -- Error_Msg_SP ("ALIASED not allowed here");
3844 -- end if;
3846 Set_Subtype_Indication (CompDef_Node, Empty);
3847 Set_Aliased_Present (CompDef_Node, False);
3848 Set_Access_Definition (CompDef_Node,
3849 P_Access_Definition (Not_Null_Present));
3850 else
3852 Set_Access_Definition (CompDef_Node, Empty);
3853 Set_Aliased_Present (CompDef_Node, Aliased_Present);
3854 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
3856 if Token = Tok_Array then
3857 Error_Msg_SC ("anonymous array not allowed as component");
3858 raise Error_Resync;
3859 end if;
3861 Set_Subtype_Indication (CompDef_Node,
3862 P_Subtype_Indication (Not_Null_Present));
3863 end if;
3865 Set_Component_Definition (Decl_Node, CompDef_Node);
3866 Set_Expression (Decl_Node, Init_Expr_Opt);
3868 if Ident > 1 then
3869 Set_Prev_Ids (Decl_Node, True);
3870 end if;
3872 if Ident < Num_Idents then
3873 Set_More_Ids (Decl_Node, True);
3874 end if;
3876 Append (Decl_Node, Decls);
3878 exception
3879 when Error_Resync =>
3880 if Token /= Tok_End then
3881 Resync_Past_Semicolon;
3882 end if;
3883 end;
3885 exit Ident_Loop when Ident = Num_Idents;
3886 Ident := Ident + 1;
3887 Restore_Scan_State (Scan_State);
3888 T_Colon;
3889 end loop Ident_Loop;
3891 P_Aspect_Specifications (Decl_Node);
3892 end P_Component_Items;
3894 --------------------------------
3895 -- 3.8 Component Declaration --
3896 --------------------------------
3898 -- Parsed by P_Component_Items (3.8)
3900 -------------------------
3901 -- 3.8.1 Variant Part --
3902 -------------------------
3904 -- VARIANT_PART ::=
3905 -- case discriminant_DIRECT_NAME is
3906 -- VARIANT
3907 -- {VARIANT}
3908 -- end case;
3910 -- The caller has checked that the initial token is CASE
3912 -- Error recovery: cannot raise Error_Resync
3914 function P_Variant_Part return Node_Id is
3915 Variant_Part_Node : Node_Id;
3916 Variants_List : List_Id;
3917 Case_Node : Node_Id;
3919 begin
3920 Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
3921 Push_Scope_Stack;
3922 Scopes (Scope.Last).Etyp := E_Case;
3923 Scopes (Scope.Last).Sloc := Token_Ptr;
3924 Scopes (Scope.Last).Ecol := Start_Column;
3926 Scan; -- past CASE
3927 Case_Node := P_Expression;
3928 Set_Name (Variant_Part_Node, Case_Node);
3930 if Nkind (Case_Node) /= N_Identifier then
3931 Set_Name (Variant_Part_Node, Error);
3932 Error_Msg ("discriminant name expected", Sloc (Case_Node));
3934 elsif Paren_Count (Case_Node) /= 0 then
3935 Error_Msg
3936 ("|discriminant name may not be parenthesized",
3937 Sloc (Case_Node));
3938 Set_Paren_Count (Case_Node, 0);
3939 end if;
3941 TF_Is;
3942 Variants_List := New_List;
3943 P_Pragmas_Opt (Variants_List);
3945 -- Test missing variant
3947 if Token = Tok_End then
3948 Error_Msg_BC ("WHEN expected (must have at least one variant)");
3949 else
3950 Append (P_Variant, Variants_List);
3951 end if;
3953 -- Loop through variants, note that we allow if in place of when,
3954 -- this error will be detected and handled in P_Variant.
3956 loop
3957 P_Pragmas_Opt (Variants_List);
3959 if Token not in Tok_When | Tok_If | Tok_Others then
3960 exit when Check_End;
3961 end if;
3963 Append (P_Variant, Variants_List);
3964 end loop;
3966 Set_Variants (Variant_Part_Node, Variants_List);
3967 return Variant_Part_Node;
3968 end P_Variant_Part;
3970 --------------------
3971 -- 3.8.1 Variant --
3972 --------------------
3974 -- VARIANT ::=
3975 -- when DISCRETE_CHOICE_LIST =>
3976 -- COMPONENT_LIST
3978 -- Error recovery: cannot raise Error_Resync
3980 -- The initial token on entry is either WHEN, IF or OTHERS
3982 function P_Variant return Node_Id is
3983 Variant_Node : Node_Id;
3985 begin
3986 -- Special check to recover nicely from use of IF in place of WHEN
3988 if Token = Tok_If then
3989 T_When;
3990 Scan; -- past IF
3991 else
3992 T_When;
3993 end if;
3995 Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3996 Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3997 TF_Arrow;
3998 Set_Component_List (Variant_Node, P_Component_List);
3999 return Variant_Node;
4000 end P_Variant;
4002 ---------------------------------
4003 -- 3.8.1 Discrete Choice List --
4004 ---------------------------------
4006 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
4008 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
4010 -- Note: in Ada 83, the expression must be a simple expression
4012 -- Error recovery: cannot raise Error_Resync
4014 function P_Discrete_Choice_List return List_Id is
4015 Choices : List_Id;
4016 Expr_Node : Node_Id := Empty; -- initialize to prevent warning
4017 Choice_Node : Node_Id;
4019 begin
4020 Choices := New_List;
4021 loop
4022 if Token = Tok_Others then
4023 Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
4024 Scan; -- past OTHERS
4026 else
4027 begin
4028 -- Scan out expression or range attribute
4030 Expr_Node := P_Expression_Or_Range_Attribute;
4031 Ignore (Tok_Right_Paren);
4033 if Token = Tok_Colon
4034 and then Nkind (Expr_Node) = N_Identifier
4035 then
4036 Error_Msg_SP ("label not permitted in this context");
4037 Scan; -- past colon
4039 -- Range attribute
4041 elsif Expr_Form = EF_Range_Attr then
4042 Append (Expr_Node, Choices);
4044 -- Explicit range
4046 elsif Token = Tok_Dot_Dot then
4047 Check_Simple_Expression (Expr_Node);
4048 Choice_Node := New_Node (N_Range, Token_Ptr);
4049 Set_Low_Bound (Choice_Node, Expr_Node);
4050 Scan; -- past ..
4051 Expr_Node := P_Expression_No_Right_Paren;
4052 Check_Simple_Expression (Expr_Node);
4053 Set_High_Bound (Choice_Node, Expr_Node);
4054 Append (Choice_Node, Choices);
4056 -- Simple name, must be subtype, so range allowed
4058 elsif Expr_Form = EF_Simple_Name then
4059 if Token = Tok_Range then
4060 Append (P_Subtype_Indication (Expr_Node), Choices);
4062 elsif Token in Token_Class_Consk then
4063 Error_Msg_SC
4064 ("the only constraint allowed here " &
4065 "is a range constraint");
4066 Discard_Junk_Node (P_Constraint_Opt);
4067 Append (Expr_Node, Choices);
4069 else
4070 Append (Expr_Node, Choices);
4071 end if;
4073 -- Expression
4075 else
4076 -- In Ada 2012 mode, the expression must be a simple
4077 -- expression. The reason for this restriction (i.e. going
4078 -- back to the Ada 83 rule) is to avoid ambiguities when set
4079 -- membership operations are allowed, consider the
4080 -- following:
4082 -- when A in 1 .. 10 | 12 =>
4084 -- This is ambiguous without parentheses, so we require one
4085 -- of the following two parenthesized forms to disambiguate:
4087 -- one of the following:
4089 -- when (A in 1 .. 10 | 12) =>
4090 -- when (A in 1 .. 10) | 12 =>
4092 -- To solve this, in Ada 2012 mode, we disallow the use of
4093 -- membership operations in expressions in choices.
4095 -- Technically in the grammar, the expression must match the
4096 -- grammar for restricted expression.
4098 if Ada_Version >= Ada_2012 then
4099 Check_Restricted_Expression (Expr_Node);
4101 -- In Ada 83 mode, the syntax required a simple expression
4103 else
4104 Check_Simple_Expression_In_Ada_83 (Expr_Node);
4105 end if;
4107 Append (Expr_Node, Choices);
4108 end if;
4110 exception
4111 when Error_Resync =>
4112 Resync_Choice;
4113 return Error_List;
4114 end;
4115 end if;
4117 if Token = Tok_Comma then
4118 if Nkind (Expr_Node) = N_Iterated_Component_Association then
4119 return Choices;
4120 end if;
4122 Scan; -- past comma
4124 if Token = Tok_Vertical_Bar then
4125 Error_Msg_SP -- CODEFIX
4126 ("|extra "","" ignored");
4127 Scan; -- past |
4129 else
4130 Error_Msg_SP -- CODEFIX
4131 (""","" should be ""'|""");
4132 end if;
4134 else
4135 exit when Token /= Tok_Vertical_Bar;
4136 Scan; -- past |
4137 end if;
4139 end loop;
4141 return Choices;
4142 end P_Discrete_Choice_List;
4144 ----------------------------
4145 -- 3.8.1 Discrete Choice --
4146 ----------------------------
4148 -- Parsed by P_Discrete_Choice_List (3.8.1)
4150 ----------------------------------
4151 -- 3.9.1 Record Extension Part --
4152 ----------------------------------
4154 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
4156 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
4158 --------------------------------------
4159 -- 3.9.4 Interface Type Definition --
4160 --------------------------------------
4162 -- INTERFACE_TYPE_DEFINITION ::=
4163 -- [limited | task | protected | synchronized] interface
4164 -- [and INTERFACE_LIST]
4166 -- Error recovery: cannot raise Error_Resync
4168 function P_Interface_Type_Definition
4169 (Abstract_Present : Boolean) return Node_Id
4171 Typedef_Node : Node_Id;
4173 begin
4174 Error_Msg_Ada_2005_Extension ("abstract interface");
4176 if Abstract_Present then
4177 Error_Msg_SP
4178 ("ABSTRACT not allowed in interface type definition " &
4179 "(RM 3.9.4(2/2))");
4180 end if;
4182 Scan; -- past INTERFACE
4184 -- Ada 2005 (AI-345): In case of interfaces with a null list of
4185 -- interfaces we build a record_definition node.
4187 if Token = Tok_Semicolon or else Aspect_Specifications_Present then
4188 Typedef_Node := New_Node (N_Record_Definition, Token_Ptr);
4190 Set_Abstract_Present (Typedef_Node);
4191 Set_Tagged_Present (Typedef_Node);
4192 Set_Null_Present (Typedef_Node);
4193 Set_Interface_Present (Typedef_Node);
4195 -- Ada 2005 (AI-251): In case of not-synchronized interfaces that have
4196 -- a list of interfaces we build a derived_type_definition node. This
4197 -- simplifies the semantic analysis (and hence further maintenance)
4199 else
4200 if Token /= Tok_And then
4201 Error_Msg_AP ("AND expected");
4202 else
4203 Scan; -- past AND
4204 end if;
4206 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
4208 Set_Abstract_Present (Typedef_Node);
4209 Set_Interface_Present (Typedef_Node);
4210 Set_Subtype_Indication (Typedef_Node, P_Qualified_Simple_Name);
4212 Set_Record_Extension_Part (Typedef_Node,
4213 New_Node (N_Record_Definition, Token_Ptr));
4214 Set_Null_Present (Record_Extension_Part (Typedef_Node));
4216 if Token = Tok_And then
4217 Set_Interface_List (Typedef_Node, New_List);
4218 Scan; -- past AND
4220 loop
4221 Append (P_Qualified_Simple_Name,
4222 Interface_List (Typedef_Node));
4223 exit when Token /= Tok_And;
4224 Scan; -- past AND
4225 end loop;
4226 end if;
4227 end if;
4229 return Typedef_Node;
4230 end P_Interface_Type_Definition;
4232 ----------------------------------
4233 -- 3.10 Access Type Definition --
4234 ----------------------------------
4236 -- ACCESS_TYPE_DEFINITION ::=
4237 -- ACCESS_TO_OBJECT_DEFINITION
4238 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
4240 -- ACCESS_TO_OBJECT_DEFINITION ::=
4241 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
4243 -- GENERAL_ACCESS_MODIFIER ::= all | constant
4245 -- ACCESS_TO_SUBPROGRAM_DEFINITION
4246 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
4247 -- | [NULL_EXCLUSION] access [protected] function
4248 -- PARAMETER_AND_RESULT_PROFILE
4250 -- PARAMETER_PROFILE ::= [FORMAL_PART]
4252 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
4254 -- Ada 2005 (AI-254): If Header_Already_Parsed then the caller has already
4255 -- parsed the null_exclusion part and has also removed the ACCESS token;
4256 -- otherwise the caller has just checked that the initial token is ACCESS
4258 -- Error recovery: can raise Error_Resync
4260 function P_Access_Type_Definition
4261 (Header_Already_Parsed : Boolean := False) return Node_Id
4263 procedure Check_Junk_Subprogram_Name;
4264 -- Used in access to subprogram definition cases to check for an
4265 -- identifier or operator symbol that does not belong.
4267 --------------------------------
4268 -- Check_Junk_Subprogram_Name --
4269 --------------------------------
4271 procedure Check_Junk_Subprogram_Name is
4272 Saved_State : Saved_Scan_State;
4274 begin
4275 if Token in Tok_Identifier | Tok_Operator_Symbol then
4276 Save_Scan_State (Saved_State);
4277 Scan; -- past possible junk subprogram name
4279 if Token in Tok_Left_Paren | Tok_Semicolon then
4280 Error_Msg_SP ("unexpected subprogram name ignored");
4281 else
4282 Restore_Scan_State (Saved_State);
4283 end if;
4284 end if;
4285 end Check_Junk_Subprogram_Name;
4287 Access_Loc : constant Source_Ptr := Token_Ptr;
4288 Prot_Flag : Boolean;
4289 Not_Null_Present : Boolean := False;
4290 Not_Null_Subtype : Boolean := False;
4291 Not_Null_Subtype_Loc : Source_Ptr; -- loc of second "not null"
4292 Type_Def_Node : Node_Id;
4293 Result_Not_Null : Boolean;
4294 Result_Node : Node_Id;
4296 -- Start of processing for P_Access_Type_Definition
4298 begin
4299 if not Header_Already_Parsed then
4300 -- NOT NULL ACCESS... is a common form of access definition. ACCESS
4301 -- NOT NULL... is certainly rare, but syntactically legal. NOT NULL
4302 -- ACCESS NOT NULL... is rarer yet, and also legal. The last two
4303 -- cases are only meaningful if the following subtype indication
4304 -- denotes an access type. We check below for "not null procedure"
4305 -- and "not null function"; in the access-to-object case it is a
4306 -- semantic check. The flag Not_Null_Subtype indicates that this
4307 -- second null exclusion is present in the access type definition.
4309 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
4311 if Token /= Tok_Access then
4312 Error_Msg
4313 ("ACCESS expected",
4314 Token_Ptr);
4315 end if;
4317 Scan; -- past ACCESS
4319 Not_Null_Subtype_Loc := Token_Ptr;
4320 Not_Null_Subtype := P_Null_Exclusion; -- Might also appear
4321 end if;
4323 if Token_Name = Name_Protected then
4324 Check_95_Keyword (Tok_Protected, Tok_Procedure);
4325 Check_95_Keyword (Tok_Protected, Tok_Function);
4326 end if;
4328 Prot_Flag := (Token = Tok_Protected);
4330 if Prot_Flag then
4331 Scan; -- past PROTECTED
4333 if Token not in Tok_Procedure | Tok_Function then
4334 Error_Msg_SC -- CODEFIX
4335 ("FUNCTION or PROCEDURE expected");
4336 end if;
4337 end if;
4339 -- Access-to-subprogram case
4341 if Token in Tok_Procedure | Tok_Function then
4343 -- Check for "not null [protected] procedure" and "not null
4344 -- [protected] function".
4346 if Not_Null_Subtype then
4347 Error_Msg
4348 ("null exclusion must apply to access type",
4349 Not_Null_Subtype_Loc);
4350 end if;
4351 end if;
4353 if Token = Tok_Procedure then
4354 if Ada_Version = Ada_83 then
4355 Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
4356 end if;
4358 Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
4359 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4360 Scan; -- past PROCEDURE
4361 Check_Junk_Subprogram_Name;
4362 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
4363 Set_Protected_Present (Type_Def_Node, Prot_Flag);
4365 elsif Token = Tok_Function then
4366 if Ada_Version = Ada_83 then
4367 Error_Msg_SC ("(Ada 83) access to function not allowed!");
4368 end if;
4370 Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
4371 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4372 Scan; -- past FUNCTION
4373 Check_Junk_Subprogram_Name;
4374 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
4375 Set_Protected_Present (Type_Def_Node, Prot_Flag);
4376 TF_Return;
4378 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
4380 -- Ada 2005 (AI-318-02)
4382 if Token = Tok_Access then
4383 Error_Msg_Ada_2005_Extension ("anonymous access result type");
4385 Result_Node := P_Access_Definition (Result_Not_Null);
4387 else
4388 Result_Node := P_Subtype_Mark;
4389 No_Constraint;
4391 -- A null exclusion on the result type must be recorded in a flag
4392 -- distinct from the one used for the access-to-subprogram type's
4393 -- null exclusion.
4395 Set_Null_Exclusion_In_Return_Present
4396 (Type_Def_Node, Result_Not_Null);
4397 end if;
4399 Set_Result_Definition (Type_Def_Node, Result_Node);
4401 -- Access-to-object case
4403 else
4404 Type_Def_Node := New_Node (N_Access_To_Object_Definition, Access_Loc);
4405 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4406 Set_Null_Excluding_Subtype (Type_Def_Node, Not_Null_Subtype);
4408 if Token in Tok_All | Tok_Constant then
4409 if Ada_Version = Ada_83 then
4410 Error_Msg_SC ("(Ada 83) access modifier not allowed!");
4411 end if;
4413 if Token = Tok_All then
4414 Set_All_Present (Type_Def_Node, True);
4416 else
4417 Set_Constant_Present (Type_Def_Node, True);
4418 end if;
4420 Scan; -- past ALL or CONSTANT
4421 end if;
4423 Set_Subtype_Indication (Type_Def_Node,
4424 P_Subtype_Indication (Not_Null_Present));
4425 end if;
4427 return Type_Def_Node;
4428 end P_Access_Type_Definition;
4430 ---------------------------------------
4431 -- 3.10 Access To Object Definition --
4432 ---------------------------------------
4434 -- Parsed by P_Access_Type_Definition (3.10)
4436 -----------------------------------
4437 -- 3.10 General Access Modifier --
4438 -----------------------------------
4440 -- Parsed by P_Access_Type_Definition (3.10)
4442 -------------------------------------------
4443 -- 3.10 Access To Subprogram Definition --
4444 -------------------------------------------
4446 -- Parsed by P_Access_Type_Definition (3.10)
4448 -----------------------------
4449 -- 3.10 Access Definition --
4450 -----------------------------
4452 -- ACCESS_DEFINITION ::=
4453 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4454 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
4456 -- ACCESS_TO_SUBPROGRAM_DEFINITION
4457 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
4458 -- | [NULL_EXCLUSION] access [protected] function
4459 -- PARAMETER_AND_RESULT_PROFILE
4461 -- The caller has parsed the null-exclusion part and it has also checked
4462 -- that the next token is ACCESS
4464 -- Error recovery: cannot raise Error_Resync
4466 function P_Access_Definition
4467 (Null_Exclusion_Present : Boolean) return Node_Id
4469 Def_Node : Node_Id;
4470 Subp_Node : Node_Id;
4472 begin
4473 Def_Node := New_Node (N_Access_Definition, Token_Ptr);
4474 Scan; -- past ACCESS
4476 -- Ada 2005 (AI-254): Access_To_Subprogram_Definition
4478 if Token in Tok_Protected | Tok_Procedure | Tok_Function then
4479 Error_Msg_Ada_2005_Extension ("access-to-subprogram");
4481 Subp_Node := P_Access_Type_Definition (Header_Already_Parsed => True);
4482 Set_Null_Exclusion_Present (Subp_Node, Null_Exclusion_Present);
4483 Set_Access_To_Subprogram_Definition (Def_Node, Subp_Node);
4485 -- Ada 2005 (AI-231)
4486 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4488 else
4489 Set_Null_Exclusion_Present (Def_Node, Null_Exclusion_Present);
4491 if Token = Tok_All then
4492 if Ada_Version < Ada_2005 then
4493 Error_Msg_SP
4494 ("ALL not permitted for anonymous access type");
4495 end if;
4497 Scan; -- past ALL
4498 Set_All_Present (Def_Node);
4500 elsif Token = Tok_Constant then
4501 Error_Msg_Ada_2005_Extension ("access-to-constant");
4503 Scan; -- past CONSTANT
4504 Set_Constant_Present (Def_Node);
4505 end if;
4507 Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
4508 No_Constraint;
4509 end if;
4511 return Def_Node;
4512 end P_Access_Definition;
4514 -----------------------------------------
4515 -- 3.10.1 Incomplete Type Declaration --
4516 -----------------------------------------
4518 -- Parsed by P_Type_Declaration (3.2.1)
4520 ----------------------------
4521 -- 3.11 Declarative Part --
4522 ----------------------------
4524 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
4526 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Item
4527 -- handles errors, and returns cleanly after an error has occurred)
4529 function P_Declarative_Part return List_Id is
4530 Decls : constant List_Id := New_List;
4531 begin
4532 -- Indicate no bad declarations detected yet. This will be reset by
4533 -- P_Declarative_Items if a bad declaration is discovered.
4535 Missing_Begin_Msg := No_Error_Msg;
4537 -- Get rid of active SIS entry from outer scope. This means we will
4538 -- miss some nested cases, but it doesn't seem worth the effort. See
4539 -- discussion in Par for further details
4541 SIS_Entry_Active := False;
4543 P_Declarative_Items
4544 (Decls, Declare_Expression => False,
4545 In_Spec => False, In_Statements => False);
4547 -- Get rid of active SIS entry which is left set only if we scanned a
4548 -- procedure declaration and have not found the body. We could give
4549 -- an error message, but that really would be usurping the role of
4550 -- semantic analysis (this really is a missing body case).
4552 SIS_Entry_Active := False;
4553 return Decls;
4554 end P_Declarative_Part;
4556 ----------------------------
4557 -- 3.11 Declarative Item --
4558 ----------------------------
4560 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
4562 -- Can return Error if a junk declaration is found, or Empty if no
4563 -- declaration is found (i.e. a token ending declarations, such as
4564 -- BEGIN or END is encountered).
4566 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
4567 -- then the scan is set past the next semicolon and Error is returned.
4569 procedure P_Declarative_Item
4570 (Decls : List_Id;
4571 Done : out Boolean;
4572 Declare_Expression : Boolean;
4573 In_Spec : Boolean;
4574 In_Statements : Boolean)
4576 Scan_State : Saved_Scan_State;
4578 begin
4579 Done := False;
4581 -- In -gnatg mode, we don't want a "bad indentation" error inside a
4582 -- declare_expression.
4584 if Style_Check and not Declare_Expression then
4585 Style.Check_Indentation;
4586 end if;
4588 case Token is
4589 when Tok_Function
4590 | Tok_Not
4591 | Tok_Overriding
4592 | Tok_Procedure
4594 Check_Bad_Layout;
4595 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4597 when Tok_For =>
4598 Check_Bad_Layout;
4600 -- Check for loop (premature statement)
4602 Save_Scan_State (Scan_State);
4603 Scan; -- past FOR
4605 declare
4606 Is_Statement : Boolean := True;
4607 begin
4608 if Token = Tok_Identifier then
4609 Scan; -- past identifier
4610 if Token in Tok_Use | Tok_Apostrophe then
4611 Is_Statement := False;
4612 elsif Token = Tok_Dot then
4613 Scan;
4614 if Token = Tok_Identifier then
4615 Scan;
4616 Is_Statement := Token in Tok_In | Tok_Of;
4617 end if;
4618 end if;
4619 else
4620 Is_Statement := False;
4621 end if;
4623 Restore_Scan_State (Scan_State);
4625 if Is_Statement then
4626 if not In_Statements then
4627 Statement_When_Declaration_Expected
4628 (Decls, Done, In_Spec);
4629 end if;
4631 Done := True;
4632 else
4633 Append (P_Representation_Clause, Decls);
4634 end if;
4635 end;
4637 when Tok_Generic =>
4638 Check_Bad_Layout;
4639 Append (P_Generic, Decls);
4641 when Tok_Identifier =>
4642 Check_Bad_Layout;
4644 -- Special check for misuse of overriding not in Ada 2005 mode
4646 if Token_Name = Name_Overriding
4647 and then not Next_Token_Is (Tok_Colon)
4648 then
4649 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
4650 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
4652 Token := Tok_Overriding;
4653 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4655 -- Normal case, no overriding, or overriding followed by colon
4657 else
4658 P_Identifier_Declarations (Decls, Done, In_Spec, In_Statements);
4659 end if;
4661 when Tok_Package =>
4662 Check_Bad_Layout;
4663 Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4665 when Tok_Pragma =>
4666 -- If we see a pragma and In_Statements is true, we want to let
4667 -- the statement-parser deal with it.
4669 if In_Statements then
4670 Done := True;
4671 else
4672 Append (P_Pragma, Decls);
4673 end if;
4675 when Tok_Protected =>
4676 Check_Bad_Layout;
4677 Scan; -- past PROTECTED
4678 Append (P_Protected, Decls);
4680 when Tok_Subtype =>
4681 Check_Bad_Layout;
4682 Append (P_Subtype_Declaration, Decls);
4684 when Tok_Task =>
4685 Check_Bad_Layout;
4686 Scan; -- past TASK
4687 Append (P_Task, Decls);
4689 when Tok_Type =>
4690 Check_Bad_Layout;
4691 Append (P_Type_Declaration, Decls);
4693 when Tok_Use =>
4694 Check_Bad_Layout;
4695 P_Use_Clause (Decls);
4697 when Tok_With =>
4698 Check_Bad_Layout;
4700 if Aspect_Specifications_Present (Strict => True) then
4702 -- If we are after a semicolon, complain that it was ignored.
4703 -- But we don't really ignore it, since we dump the aspects,
4704 -- so we make the error message a normal fatal message which
4705 -- will inhibit semantic analysis anyway).
4707 if Prev_Token = Tok_Semicolon then
4708 Error_Msg_SP -- CODEFIX
4709 ("extra "";"" ignored");
4711 -- If not just past semicolon, just complain that aspects are
4712 -- not allowed at this point.
4714 else
4715 Error_Msg_SC ("aspect specifications not allowed here");
4716 end if;
4718 -- Assume that this is a misplaced aspect specification within
4719 -- a declarative list. After discarding the misplaced aspects
4720 -- we can continue the scan.
4722 declare
4723 Dummy_Node : constant Node_Id :=
4724 New_Node (N_Package_Specification, Token_Ptr);
4725 pragma Warnings (Off, Dummy_Node);
4726 -- Dummy node to attach aspect specifications to. We will
4727 -- then throw them away.
4729 begin
4730 P_Aspect_Specifications (Dummy_Node, Semicolon => True);
4731 end;
4733 -- Here if not aspect specifications case
4735 else
4736 Error_Msg_SC ("WITH can only appear in context clause");
4737 raise Error_Resync;
4738 end if;
4740 -- BEGIN terminates the scan of a sequence of declarations unless
4741 -- there is a missing subprogram body, see section on handling
4742 -- semicolon in place of IS. We only treat the begin as satisfying
4743 -- the subprogram declaration if it falls in the expected column
4744 -- or to its right.
4746 when Tok_Begin =>
4747 if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
4749 -- Here we have the case where a BEGIN is encountered during
4750 -- declarations in a declarative part, or at the outer level,
4751 -- and there is a subprogram declaration outstanding for which
4752 -- no body has been supplied. This is the case where we assume
4753 -- that the semicolon in the subprogram declaration should
4754 -- really have been is. The active SIS entry describes the
4755 -- subprogram declaration. On return the declaration has been
4756 -- modified to become a body.
4758 declare
4759 Specification_Node : Node_Id;
4760 Decl_Node : Node_Id;
4761 Body_Node : Node_Id;
4763 begin
4764 -- First issue the error message. If we had a missing
4765 -- semicolon in the declaration, then change the message
4766 -- to <missing "is">
4768 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
4769 Change_Error_Text -- Replace: "missing "";"" "
4770 (SIS_Missing_Semicolon_Message, "missing ""is""");
4772 -- Otherwise we saved the semicolon position, so complain
4774 else
4775 Error_Msg -- CODEFIX
4776 ("|"";"" should be IS", SIS_Semicolon_Sloc);
4777 end if;
4779 -- The next job is to fix up any declarations that occurred
4780 -- between the procedure header and the BEGIN. These got
4781 -- chained to the outer declarative region (immediately
4782 -- after the procedure declaration) and they should be
4783 -- chained to the subprogram itself, which is a body
4784 -- rather than a spec.
4786 Specification_Node := Specification (SIS_Declaration_Node);
4787 Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
4788 Body_Node := SIS_Declaration_Node;
4789 Set_Specification (Body_Node, Specification_Node);
4790 Set_Declarations (Body_Node, New_List);
4792 loop
4793 Decl_Node := Remove_Next (Body_Node);
4794 exit when Decl_Node = Empty;
4795 Append (Decl_Node, Declarations (Body_Node));
4796 end loop;
4798 -- Now make the scope table entry for the Begin-End and
4799 -- scan it out
4801 Push_Scope_Stack;
4802 Scopes (Scope.Last).Sloc := SIS_Sloc;
4803 Scopes (Scope.Last).Etyp := E_Name;
4804 Scopes (Scope.Last).Ecol := SIS_Ecol;
4805 Scopes (Scope.Last).Labl := SIS_Labl;
4806 Scopes (Scope.Last).Lreq := False;
4807 SIS_Entry_Active := False;
4808 Scan; -- past BEGIN
4809 Set_Handled_Statement_Sequence (Body_Node,
4810 P_Handled_Sequence_Of_Statements);
4811 End_Statements (Handled_Statement_Sequence (Body_Node));
4812 end;
4814 else
4815 Done := True;
4816 end if;
4818 -- Normally an END terminates the scan for basic declarative items.
4819 -- The one exception is END RECORD, which is probably left over from
4820 -- some other junk.
4822 when Tok_End =>
4823 Save_Scan_State (Scan_State); -- at END
4824 Scan; -- past END
4826 if Token = Tok_Record then
4827 Error_Msg_SP ("no RECORD for this `end record`!");
4828 Scan; -- past RECORD
4829 TF_Semicolon;
4831 -- This might happen because of misplaced aspect specification.
4832 -- After discarding the misplaced aspects we can continue the
4833 -- scan.
4835 else
4836 Restore_Scan_State (Scan_State); -- to END
4837 Done := True;
4838 end if;
4840 -- The following tokens which can only be the start of a statement
4841 -- are considered to end a declarative part (i.e. we have a missing
4842 -- BEGIN situation). We are fairly conservative in making this
4843 -- judgment, because it is a real mess to go into statement mode
4844 -- prematurely in response to a junk declaration.
4846 when Tok_Abort
4847 | Tok_Accept
4848 | Tok_Declare
4849 | Tok_Delay
4850 | Tok_Exit
4851 | Tok_Goto
4852 | Tok_If
4853 | Tok_Loop
4854 | Tok_Null
4855 | Tok_Requeue
4856 | Tok_Select
4857 | Tok_While
4859 -- If we parsing declarations in a sequence of statements, we want
4860 -- to let the caller continue parsing statements.
4862 if In_Statements then
4863 Done := True;
4865 -- Otherwise, give an error. But before we decide that it's a
4866 -- statement, check for a reserved word misused as an identifier.
4868 elsif Is_Reserved_Identifier then
4869 Save_Scan_State (Scan_State);
4870 Scan; -- past the token
4872 -- If reserved identifier not followed by colon or comma, then
4873 -- this is most likely an assignment statement to the bad id.
4875 if Token not in Tok_Colon | Tok_Comma then
4876 Restore_Scan_State (Scan_State);
4877 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4879 -- Otherwise we have a declaration of the bad id
4881 else
4882 Restore_Scan_State (Scan_State);
4883 Scan_Reserved_Identifier (Force_Msg => True);
4884 P_Identifier_Declarations
4885 (Decls, Done, In_Spec, In_Statements);
4886 end if;
4888 -- If not reserved identifier, then it's an incorrectly placed a
4889 -- statement.
4891 else
4892 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4893 end if;
4895 -- The token RETURN may well also signal a missing BEGIN situation,
4896 -- however, we never let it end the declarative part, because it
4897 -- might also be part of a half-baked function declaration. If we are
4898 -- In_Statements, then let the caller parse it; otherwise, it's an
4899 -- error.
4901 when Tok_Return =>
4902 if In_Statements then
4903 Done := True;
4904 else
4905 Error_Msg_SC ("misplaced RETURN statement");
4906 raise Error_Resync;
4907 end if;
4909 -- PRIVATE definitely terminates the declarations in a spec,
4910 -- and is an error in a body.
4912 when Tok_Private =>
4913 if In_Spec then
4914 Done := True;
4915 else
4916 Error_Msg_SC ("PRIVATE not allowed in body");
4917 Scan; -- past PRIVATE
4918 end if;
4920 -- An end of file definitely terminates the declarations
4922 when Tok_EOF =>
4923 Done := True;
4925 -- The remaining tokens do not end the scan, but cannot start a
4926 -- valid declaration, so we signal an error and resynchronize.
4927 -- But first check for misuse of a reserved identifier.
4929 when others =>
4930 if In_Statements then
4931 Done := True;
4932 return;
4933 end if;
4935 -- Here we check for a reserved identifier
4937 if Is_Reserved_Identifier then
4938 Save_Scan_State (Scan_State);
4939 Scan; -- past the token
4941 if Token not in Tok_Colon | Tok_Comma then
4942 Restore_Scan_State (Scan_State);
4943 Set_Declaration_Expected;
4944 raise Error_Resync;
4945 else
4946 Restore_Scan_State (Scan_State);
4947 Scan_Reserved_Identifier (Force_Msg => True);
4948 Check_Bad_Layout;
4949 P_Identifier_Declarations
4950 (Decls, Done, In_Spec, In_Statements);
4951 end if;
4953 else
4954 Set_Declaration_Expected;
4955 raise Error_Resync;
4956 end if;
4957 end case;
4959 -- To resynchronize after an error, we scan to the next semicolon and
4960 -- return with Done = False, indicating that there may still be more
4961 -- valid declarations to come.
4963 exception
4964 when Error_Resync =>
4965 Resync_Past_Semicolon;
4966 end P_Declarative_Item;
4968 procedure P_Declarative_Items
4969 (Decls : List_Id;
4970 Declare_Expression : Boolean;
4971 In_Spec : Boolean;
4972 In_Statements : Boolean)
4974 Done : Boolean;
4975 begin
4976 loop
4977 P_Declarative_Item
4978 (Decls, Done, Declare_Expression, In_Spec, In_Statements);
4979 exit when Done;
4980 end loop;
4981 end P_Declarative_Items;
4983 ----------------------------------
4984 -- 3.11 Basic Declarative Item --
4985 ----------------------------------
4987 -- BASIC_DECLARATIVE_ITEM ::=
4988 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
4990 -- Scan zero or more basic declarative items
4992 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
4993 -- the scan pointer is repositioned past the next semicolon, and the scan
4994 -- for declarative items continues.
4996 function P_Basic_Declarative_Items
4997 (Declare_Expression : Boolean) return List_Id
4999 Decl : Node_Id;
5000 Decls : constant List_Id := New_List;
5001 Kind : Node_Kind;
5003 begin
5004 -- Indicate no bad declarations detected yet in the current context:
5005 -- visible or private declarations of a package spec.
5007 Missing_Begin_Msg := No_Error_Msg;
5009 -- Get rid of active SIS entry from outer scope. This means we will
5010 -- miss some nested cases, but it doesn't seem worth the effort. See
5011 -- discussion in Par for further details
5013 SIS_Entry_Active := False;
5015 P_Declarative_Items
5016 (Decls, Declare_Expression, In_Spec => True, In_Statements => False);
5018 -- Get rid of active SIS entry. This is set only if we have scanned a
5019 -- procedure declaration and have not found the body. We could give
5020 -- an error message, but that really would be usurping the role of
5021 -- semantic analysis (this really is a case of a missing body).
5023 SIS_Entry_Active := False;
5025 -- Test for assorted illegal declarations not diagnosed elsewhere
5027 Decl := First (Decls);
5029 while Present (Decl) loop
5030 Kind := Nkind (Decl);
5032 -- Test for body scanned, not acceptable as basic decl item
5034 if Kind = N_Subprogram_Body or else
5035 Kind = N_Package_Body or else
5036 Kind = N_Task_Body or else
5037 Kind = N_Protected_Body
5038 then
5039 if Declare_Expression then
5040 Error_Msg
5041 ("proper body not allowed in declare_expression",
5042 Sloc (Decl));
5043 else
5044 Error_Msg
5045 ("proper body not allowed in package spec",
5046 Sloc (Decl));
5047 end if;
5049 -- Complete declaration of mangled subprogram body, for better
5050 -- recovery if analysis is attempted.
5052 if Nkind (Decl) in N_Subprogram_Body | N_Package_Body | N_Task_Body
5053 and then No (Handled_Statement_Sequence (Decl))
5054 then
5055 Set_Handled_Statement_Sequence (Decl,
5056 Make_Handled_Sequence_Of_Statements (Sloc (Decl),
5057 Statements => New_List));
5058 end if;
5060 -- Test for body stub scanned, not acceptable as basic decl item
5062 elsif Kind in N_Body_Stub then
5063 Error_Msg ("body stub not allowed in package spec", Sloc (Decl));
5065 elsif Kind = N_Assignment_Statement then
5066 Error_Msg
5067 ("assignment statement not allowed in package spec",
5068 Sloc (Decl));
5069 end if;
5071 Next (Decl);
5072 end loop;
5074 return Decls;
5075 end P_Basic_Declarative_Items;
5077 ----------------
5078 -- 3.11 Body --
5079 ----------------
5081 -- For proper body, see below
5082 -- For body stub, see 10.1.3
5084 -----------------------
5085 -- 3.11 Proper Body --
5086 -----------------------
5088 -- Subprogram body is parsed by P_Subprogram (6.1)
5089 -- Package body is parsed by P_Package (7.1)
5090 -- Task body is parsed by P_Task (9.1)
5091 -- Protected body is parsed by P_Protected (9.4)
5093 ------------------------------
5094 -- Set_Declaration_Expected --
5095 ------------------------------
5097 procedure Set_Declaration_Expected is
5098 begin
5099 Error_Msg_SC ("declaration expected");
5101 if Missing_Begin_Msg = No_Error_Msg then
5102 Missing_Begin_Msg := Get_Msg_Id;
5103 end if;
5104 end Set_Declaration_Expected;
5106 ----------------------
5107 -- Skip_Declaration --
5108 ----------------------
5110 procedure Skip_Declaration (S : List_Id) is
5111 Ignored_Done : Boolean;
5112 begin
5113 P_Declarative_Item
5114 (S, Ignored_Done, Declare_Expression => False, In_Spec => False,
5115 In_Statements => False);
5116 end Skip_Declaration;
5118 -----------------------------------------
5119 -- Statement_When_Declaration_Expected --
5120 -----------------------------------------
5122 procedure Statement_When_Declaration_Expected
5123 (Decls : List_Id;
5124 Done : out Boolean;
5125 In_Spec : Boolean)
5127 begin
5128 -- Case of second occurrence of statement in one declaration sequence
5130 if Missing_Begin_Msg /= No_Error_Msg then
5132 -- In the procedure spec case, just ignore it, we only give one
5133 -- message for the first occurrence, since otherwise we may get
5134 -- horrible cascading if BODY was missing in the header line.
5136 if In_Spec then
5137 null;
5139 -- Just ignore it if we are in -gnatd.2 (allow statements to appear
5140 -- in declaration sequences) mode.
5142 elsif Debug_Flag_Dot_2 then
5143 null;
5145 -- In the declarative part case, take a second statement as a sure
5146 -- sign that we really have a missing BEGIN, and end the declarative
5147 -- part now. Note that the caller will fix up the first message to
5148 -- say "missing BEGIN" so that's how the error will be signalled.
5150 else
5151 Done := True;
5152 return;
5153 end if;
5155 -- Case of first occurrence of unexpected statement
5157 else
5158 -- Do not give error message if we are operating in -gnatd.2 mode
5159 -- (alllow statements to appear in declarative parts).
5161 if not Debug_Flag_Dot_2 then
5163 -- If we are in a package spec, then give message of statement
5164 -- not allowed in package spec. This message never gets changed.
5166 if In_Spec then
5167 Error_Msg_SC ("statement not allowed in package spec");
5169 -- If in declarative part, then we give the message complaining
5170 -- about finding a statement when a declaration is expected. This
5171 -- gets changed to a complaint about a missing BEGIN if we later
5172 -- find that no BEGIN is present.
5174 else
5175 Error_Msg_SC ("statement not allowed in declarative part");
5176 end if;
5178 -- Capture message Id. This is used for two purposes, first to
5179 -- stop multiple messages, see test above, and second, to allow
5180 -- the replacement of the message in the declarative part case.
5182 Missing_Begin_Msg := Get_Msg_Id;
5183 end if;
5184 end if;
5186 -- In all cases except the case in which we decided to terminate the
5187 -- declaration sequence on a second error, we scan out the statement
5188 -- and append it to the list of declarations (note that the semantics
5189 -- can handle statements in a declaration list so if we proceed to
5190 -- call the semantic phase, all will be (reasonably) well.
5192 Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
5194 -- Done is set to False, since we want to continue the scan of
5195 -- declarations, hoping that this statement was a temporary glitch.
5196 -- If we indeed are now in the statement part (i.e. this was a missing
5197 -- BEGIN, then it's not terrible, we will simply keep calling this
5198 -- procedure to process the statements one by one, and then finally
5199 -- hit the missing BEGIN, which will clean up the error message.
5201 Done := False;
5202 end Statement_When_Declaration_Expected;
5204 end Ch3;