control_flow_insn_p takes an insn
[official-gcc.git] / gcc / ada / par-ch3.adb
blob7e4dc8f2623c6b0d45ebcdadb118b61a1fc86441
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-2014, 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_Items
81 (Decls : List_Id;
82 Done : out Boolean;
83 In_Spec : Boolean);
84 -- Scans out a single declarative item, or, in the case of a declaration
85 -- with a list of identifiers, a list of declarations, one for each of the
86 -- identifiers in the list. The declaration or declarations scanned are
87 -- appended to the given list. Done indicates whether or not there may be
88 -- additional declarative items to scan. If Done is True, then a decision
89 -- has been made that there are no more items to scan. If Done is False,
90 -- then there may be additional declarations to scan. In_Spec is true if
91 -- we are scanning a package declaration, and is used to generate an
92 -- appropriate message if a statement is encountered in such a context.
94 procedure P_Identifier_Declarations
95 (Decls : List_Id;
96 Done : out Boolean;
97 In_Spec : Boolean);
98 -- Scans out a set of declarations for an identifier or list of
99 -- identifiers, and appends them to the given list. The parameters have
100 -- the same significance as for P_Declarative_Items.
102 procedure Statement_When_Declaration_Expected
103 (Decls : List_Id;
104 Done : out Boolean;
105 In_Spec : Boolean);
106 -- Called when a statement is found at a point where a declaration was
107 -- expected. The parameters are as described for P_Declarative_Items.
109 procedure Set_Declaration_Expected;
110 -- Posts a "declaration expected" error messages at the start of the
111 -- current token, and if this is the first such message issued, saves
112 -- the message id in Missing_Begin_Msg, for possible later replacement.
114 ---------------------------------
115 -- Check_Restricted_Expression --
116 ---------------------------------
118 procedure Check_Restricted_Expression (N : Node_Id) is
119 begin
120 if Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor, N_And_Then, N_Or_Else) then
121 Check_Restricted_Expression (Left_Opnd (N));
122 Check_Restricted_Expression (Right_Opnd (N));
124 elsif Nkind_In (N, N_In, N_Not_In)
125 and then Paren_Count (N) = 0
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 = Tok_Colon_Equal
149 or else Token = Tok_Equal
150 or else Token = Tok_Is
151 then
152 null;
154 -- Another possibility. If we have a literal followed by a semicolon,
155 -- we assume that we have a missing colon-equal.
157 elsif Token in Token_Class_Literal then
158 declare
159 Scan_State : Saved_Scan_State;
161 begin
162 Save_Scan_State (Scan_State);
163 Scan; -- past literal or identifier
165 if Token = Tok_Semicolon then
166 Restore_Scan_State (Scan_State);
167 else
168 Restore_Scan_State (Scan_State);
169 return Empty;
170 end if;
171 end;
173 -- Otherwise we definitely have no initialization expression
175 else
176 return Empty;
177 end if;
179 -- Merge here if we have an initialization expression
181 T_Colon_Equal;
183 if P then
184 return P_Expression;
185 else
186 return P_Expression_No_Right_Paren;
187 end if;
188 end Init_Expr_Opt;
190 ----------------------------
191 -- 3.1 Basic Declaration --
192 ----------------------------
194 -- Parsed by P_Basic_Declarative_Items (3.9)
196 ------------------------------
197 -- 3.1 Defining Identifier --
198 ------------------------------
200 -- DEFINING_IDENTIFIER ::= IDENTIFIER
202 -- Error recovery: can raise Error_Resync
204 function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
205 Ident_Node : Node_Id;
207 begin
208 -- Scan out the identifier. Note that this code is essentially identical
209 -- to P_Identifier, except that in the call to Scan_Reserved_Identifier
210 -- we set Force_Msg to True, since we want at least one message for each
211 -- separate declaration (but not use) of a reserved identifier.
213 -- Duplication should be removed, common code should be factored???
215 if Token = Tok_Identifier then
216 Check_Future_Keyword;
218 -- If we have a reserved identifier, manufacture an identifier with
219 -- a corresponding name after posting an appropriate error message
221 elsif Is_Reserved_Identifier (C) then
222 Scan_Reserved_Identifier (Force_Msg => True);
224 -- Otherwise we have junk that cannot be interpreted as an identifier
226 else
227 T_Identifier; -- to give message
228 raise Error_Resync;
229 end if;
231 Ident_Node := Token_Node;
232 Scan; -- past the reserved identifier
234 -- If we already have a defining identifier, clean it out and make
235 -- a new clean identifier. This situation arises in some error cases
236 -- and we need to fix it.
238 if Nkind (Ident_Node) = N_Defining_Identifier then
239 Ident_Node := Make_Identifier (Sloc (Ident_Node), Chars (Ident_Node));
240 end if;
242 -- Change identifier to defining identifier if not in error
244 if Ident_Node /= Error then
245 Change_Identifier_To_Defining_Identifier (Ident_Node);
247 -- Warn if standard redefinition, except that we never warn on a
248 -- record field definition (since this is always a harmless case).
250 if not Inside_Record_Definition then
251 Warn_If_Standard_Redefinition (Ident_Node);
252 end if;
253 end if;
255 return Ident_Node;
256 end P_Defining_Identifier;
258 -----------------------------
259 -- 3.2.1 Type Declaration --
260 -----------------------------
262 -- TYPE_DECLARATION ::=
263 -- FULL_TYPE_DECLARATION
264 -- | INCOMPLETE_TYPE_DECLARATION
265 -- | PRIVATE_TYPE_DECLARATION
266 -- | PRIVATE_EXTENSION_DECLARATION
268 -- FULL_TYPE_DECLARATION ::=
269 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION
270 -- [ASPECT_SPECIFICATIONS];
271 -- | CONCURRENT_TYPE_DECLARATION
273 -- INCOMPLETE_TYPE_DECLARATION ::=
274 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] [is tagged];
276 -- PRIVATE_TYPE_DECLARATION ::=
277 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
278 -- is [abstract] [tagged] [limited] private
279 -- [ASPECT_SPECIFICATIONS];
281 -- PRIVATE_EXTENSION_DECLARATION ::=
282 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
283 -- [abstract] [limited | synchronized]
284 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
285 -- with private [ASPECT_SPECIFICATIONS];
287 -- TYPE_DEFINITION ::=
288 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
289 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
290 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
291 -- | DERIVED_TYPE_DEFINITION | INTERFACE_TYPE_DEFINITION
293 -- INTEGER_TYPE_DEFINITION ::=
294 -- SIGNED_INTEGER_TYPE_DEFINITION
295 -- MODULAR_TYPE_DEFINITION
297 -- INTERFACE_TYPE_DEFINITION ::=
298 -- [limited | task | protected | synchronized ] interface
299 -- [and INTERFACE_LIST]
301 -- Error recovery: can raise Error_Resync
303 -- The processing for full type declarations, incomplete type declarations,
304 -- private type declarations and type definitions is included in this
305 -- function. The processing for concurrent type declarations is NOT here,
306 -- but rather in chapter 9 (this function handles only declarations
307 -- starting with TYPE).
309 function P_Type_Declaration return Node_Id is
310 Abstract_Present : Boolean := False;
311 Abstract_Loc : Source_Ptr := No_Location;
312 Decl_Node : Node_Id;
313 Discr_List : List_Id;
314 Discr_Sloc : Source_Ptr;
315 End_Labl : Node_Id;
316 Ident_Node : Node_Id;
317 Is_Derived_Iface : Boolean := False;
318 Type_Loc : Source_Ptr;
319 Type_Start_Col : Column_Number;
320 Unknown_Dis : Boolean;
322 Typedef_Node : Node_Id;
323 -- Normally holds type definition, except in the case of a private
324 -- extension declaration, in which case it holds the declaration itself
326 begin
327 Type_Loc := Token_Ptr;
328 Type_Start_Col := Start_Column;
330 -- If we have TYPE, then proceed ahead and scan identifier
332 if Token = Tok_Type then
333 Type_Token_Location := Type_Loc;
334 Scan; -- past TYPE
335 Ident_Node := P_Defining_Identifier (C_Is);
337 -- Otherwise this is an error case
339 else
340 T_Type;
341 Type_Token_Location := Type_Loc;
342 Ident_Node := P_Defining_Identifier (C_Is);
343 end if;
345 Discr_Sloc := Token_Ptr;
347 if P_Unknown_Discriminant_Part_Opt then
348 Unknown_Dis := True;
349 Discr_List := No_List;
350 else
351 Unknown_Dis := False;
352 Discr_List := P_Known_Discriminant_Part_Opt;
353 end if;
355 -- Incomplete type declaration. We complete the processing for this
356 -- case here and return the resulting incomplete type declaration node
358 if Token = Tok_Semicolon then
359 Scan; -- past ;
360 Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
361 Set_Defining_Identifier (Decl_Node, Ident_Node);
362 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
363 Set_Discriminant_Specifications (Decl_Node, Discr_List);
364 return Decl_Node;
366 else
367 Decl_Node := Empty;
368 end if;
370 -- Full type declaration or private type declaration, must have IS
372 if Token = Tok_Equal then
373 TF_Is;
374 Scan; -- past = used in place of IS
376 elsif Token = Tok_Renames then
377 Error_Msg_SC -- CODEFIX
378 ("RENAMES should be IS");
379 Scan; -- past RENAMES used in place of IS
381 else
382 TF_Is;
383 end if;
385 -- First an error check, if we have two identifiers in a row, a likely
386 -- possibility is that the first of the identifiers is an incorrectly
387 -- spelled keyword.
389 if Token = Tok_Identifier then
390 declare
391 SS : Saved_Scan_State;
392 I2 : Boolean;
394 begin
395 Save_Scan_State (SS);
396 Scan; -- past initial identifier
397 I2 := (Token = Tok_Identifier);
398 Restore_Scan_State (SS);
400 if I2
401 and then
402 (Bad_Spelling_Of (Tok_Abstract) or else
403 Bad_Spelling_Of (Tok_Access) or else
404 Bad_Spelling_Of (Tok_Aliased) or else
405 Bad_Spelling_Of (Tok_Constant))
406 then
407 null;
408 end if;
409 end;
410 end if;
412 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
414 if Token_Name = Name_Abstract then
415 Check_95_Keyword (Tok_Abstract, Tok_Tagged);
416 Check_95_Keyword (Tok_Abstract, Tok_New);
417 end if;
419 -- Check cases of misuse of ABSTRACT
421 if Token = Tok_Abstract then
422 Abstract_Present := True;
423 Abstract_Loc := Token_Ptr;
424 Scan; -- past ABSTRACT
426 -- Ada 2005 (AI-419): AARM 3.4 (2/2)
428 if (Ada_Version < Ada_2005 and then Token = Tok_Limited)
429 or else Token = Tok_Private
430 or else Token = Tok_Record
431 or else Token = Tok_Null
432 then
433 Error_Msg_AP ("TAGGED expected");
434 end if;
435 end if;
437 -- Check for misuse of Ada 95 keyword Tagged
439 if Token_Name = Name_Tagged then
440 Check_95_Keyword (Tok_Tagged, Tok_Private);
441 Check_95_Keyword (Tok_Tagged, Tok_Limited);
442 Check_95_Keyword (Tok_Tagged, Tok_Record);
443 end if;
445 -- Special check for misuse of Aliased
447 if Token = Tok_Aliased or else Token_Name = Name_Aliased then
448 Error_Msg_SC ("ALIASED not allowed in type definition");
449 Scan; -- past ALIASED
450 end if;
452 -- The following processing deals with either a private type declaration
453 -- or a full type declaration. In the private type case, we build the
454 -- N_Private_Type_Declaration node, setting its Tagged_Present and
455 -- Limited_Present flags, on encountering the Private keyword, and
456 -- leave Typedef_Node set to Empty. For the full type declaration
457 -- case, Typedef_Node gets set to the type definition.
459 Typedef_Node := Empty;
461 -- Switch on token following the IS. The loop normally runs once. It
462 -- only runs more than once if an error is detected, to try again after
463 -- detecting and fixing up the error.
465 loop
466 case Token is
468 when Tok_Access |
469 Tok_Not => -- Ada 2005 (AI-231)
470 Typedef_Node := P_Access_Type_Definition;
471 exit;
473 when Tok_Array =>
474 Typedef_Node := P_Array_Type_Definition;
475 exit;
477 when Tok_Delta =>
478 Typedef_Node := P_Fixed_Point_Definition;
479 exit;
481 when Tok_Digits =>
482 Typedef_Node := P_Floating_Point_Definition;
483 exit;
485 when Tok_In =>
486 Ignore (Tok_In);
488 when Tok_Integer_Literal =>
489 T_Range;
490 Typedef_Node := P_Signed_Integer_Type_Definition;
491 exit;
493 when Tok_Null =>
494 Typedef_Node := P_Record_Definition;
495 exit;
497 when Tok_Left_Paren =>
498 Typedef_Node := P_Enumeration_Type_Definition;
500 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
501 Set_Comes_From_Source (End_Labl, False);
503 Set_End_Label (Typedef_Node, End_Labl);
504 exit;
506 when Tok_Mod =>
507 Typedef_Node := P_Modular_Type_Definition;
508 exit;
510 when Tok_New =>
511 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
513 if Nkind (Typedef_Node) = N_Derived_Type_Definition
514 and then Present (Record_Extension_Part (Typedef_Node))
515 then
516 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
517 Set_Comes_From_Source (End_Labl, False);
519 Set_End_Label
520 (Record_Extension_Part (Typedef_Node), End_Labl);
521 end if;
523 exit;
525 when Tok_Range =>
526 Typedef_Node := P_Signed_Integer_Type_Definition;
527 exit;
529 when Tok_Record =>
530 Typedef_Node := P_Record_Definition;
532 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
533 Set_Comes_From_Source (End_Labl, False);
535 Set_End_Label (Typedef_Node, End_Labl);
536 exit;
538 when Tok_Tagged =>
539 Scan; -- past TAGGED
541 -- Ada 2005 (AI-326): If the words IS TAGGED appear, the type
542 -- is a tagged incomplete type.
544 if Ada_Version >= Ada_2005
545 and then Token = Tok_Semicolon
546 then
547 Scan; -- past ;
549 Decl_Node :=
550 New_Node (N_Incomplete_Type_Declaration, Type_Loc);
551 Set_Defining_Identifier (Decl_Node, Ident_Node);
552 Set_Tagged_Present (Decl_Node);
553 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
554 Set_Discriminant_Specifications (Decl_Node, Discr_List);
556 return Decl_Node;
557 end if;
559 if Token = Tok_Abstract then
560 Error_Msg_SC -- CODEFIX
561 ("ABSTRACT must come before TAGGED");
562 Abstract_Present := True;
563 Abstract_Loc := Token_Ptr;
564 Scan; -- past ABSTRACT
565 end if;
567 if Token = Tok_Limited then
568 Scan; -- past LIMITED
570 -- TAGGED LIMITED PRIVATE case
572 if Token = Tok_Private then
573 Decl_Node :=
574 New_Node (N_Private_Type_Declaration, Type_Loc);
575 Set_Tagged_Present (Decl_Node, True);
576 Set_Limited_Present (Decl_Node, True);
577 Scan; -- past PRIVATE
579 -- TAGGED LIMITED RECORD
581 else
582 Typedef_Node := P_Record_Definition;
583 Set_Tagged_Present (Typedef_Node, True);
584 Set_Limited_Present (Typedef_Node, True);
586 End_Labl :=
587 Make_Identifier (Token_Ptr, Chars (Ident_Node));
588 Set_Comes_From_Source (End_Labl, False);
590 Set_End_Label (Typedef_Node, End_Labl);
591 end if;
593 else
594 -- TAGGED PRIVATE
596 if Token = Tok_Private then
597 Decl_Node :=
598 New_Node (N_Private_Type_Declaration, Type_Loc);
599 Set_Tagged_Present (Decl_Node, True);
600 Scan; -- past PRIVATE
602 -- TAGGED RECORD
604 else
605 Typedef_Node := P_Record_Definition;
606 Set_Tagged_Present (Typedef_Node, True);
608 End_Labl :=
609 Make_Identifier (Token_Ptr, Chars (Ident_Node));
610 Set_Comes_From_Source (End_Labl, False);
612 Set_End_Label (Typedef_Node, End_Labl);
613 end if;
614 end if;
616 exit;
618 when Tok_Limited =>
619 Scan; -- past LIMITED
621 loop
622 if Token = Tok_Tagged then
623 Error_Msg_SC -- CODEFIX
624 ("TAGGED must come before LIMITED");
625 Scan; -- past TAGGED
627 elsif Token = Tok_Abstract then
628 Error_Msg_SC -- CODEFIX
629 ("ABSTRACT must come before LIMITED");
630 Scan; -- past ABSTRACT
632 else
633 exit;
634 end if;
635 end loop;
637 -- LIMITED RECORD or LIMITED NULL RECORD
639 if Token = Tok_Record or else Token = Tok_Null then
640 if Ada_Version = Ada_83 then
641 Error_Msg_SP
642 ("(Ada 83) limited record declaration not allowed!");
644 -- In Ada 2005, "abstract limited" can appear before "new",
645 -- but it cannot be part of an untagged record declaration.
647 elsif Abstract_Present
648 and then Prev_Token /= Tok_Tagged
649 then
650 Error_Msg_SP ("TAGGED expected");
651 end if;
653 Typedef_Node := P_Record_Definition;
654 Set_Limited_Present (Typedef_Node, True);
655 End_Labl := Make_Identifier (Token_Ptr, Chars (Ident_Node));
656 Set_Comes_From_Source (End_Labl, False);
658 Set_End_Label (Typedef_Node, End_Labl);
660 -- Ada 2005 (AI-251): LIMITED INTERFACE
662 -- If we are compiling in Ada 83 or Ada 95 mode, "interface"
663 -- is not a reserved word but we force its analysis to
664 -- generate the corresponding usage error.
666 elsif Token = Tok_Interface
667 or else (Token = Tok_Identifier
668 and then Chars (Token_Node) = Name_Interface)
669 then
670 Typedef_Node :=
671 P_Interface_Type_Definition (Abstract_Present);
672 Abstract_Present := True;
673 Set_Limited_Present (Typedef_Node);
675 if Nkind (Typedef_Node) = N_Derived_Type_Definition then
676 Is_Derived_Iface := True;
677 end if;
679 -- Ada 2005 (AI-419): LIMITED NEW
681 elsif Token = Tok_New then
682 if Ada_Version < Ada_2005 then
683 Error_Msg_SP
684 ("LIMITED in derived type is an Ada 2005 extension");
685 Error_Msg_SP
686 ("\unit must be compiled with -gnat05 switch");
687 end if;
689 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
690 Set_Limited_Present (Typedef_Node);
692 if Nkind (Typedef_Node) = N_Derived_Type_Definition
693 and then Present (Record_Extension_Part (Typedef_Node))
694 then
695 End_Labl :=
696 Make_Identifier (Token_Ptr, Chars (Ident_Node));
697 Set_Comes_From_Source (End_Labl, False);
699 Set_End_Label
700 (Record_Extension_Part (Typedef_Node), End_Labl);
701 end if;
703 -- LIMITED PRIVATE is the only remaining possibility here
705 else
706 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
707 Set_Limited_Present (Decl_Node, True);
708 T_Private; -- past PRIVATE (or complain if not there)
709 end if;
711 exit;
713 -- Here we have an identifier after the IS, which is certainly
714 -- wrong and which might be one of several different mistakes.
716 when Tok_Identifier =>
718 -- First case, if identifier is on same line, then probably we
719 -- have something like "type X is Integer .." and the best
720 -- diagnosis is a missing NEW. Note: the missing new message
721 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
723 if not Token_Is_At_Start_Of_Line then
724 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
726 -- If the identifier is at the start of the line, and is in the
727 -- same column as the type declaration itself then we consider
728 -- that we had a missing type definition on the previous line
730 elsif Start_Column <= Type_Start_Col then
731 Error_Msg_AP ("type definition expected");
732 Typedef_Node := Error;
734 -- If the identifier is at the start of the line, and is in
735 -- a column to the right of the type declaration line, then we
736 -- may have something like:
738 -- type x is
739 -- r : integer
741 -- and the best diagnosis is a missing record keyword
743 else
744 Typedef_Node := P_Record_Definition;
745 end if;
747 exit;
749 -- Ada 2005 (AI-251): INTERFACE
751 when Tok_Interface =>
752 Typedef_Node := P_Interface_Type_Definition (Abstract_Present);
753 Abstract_Present := True;
754 exit;
756 when Tok_Private =>
757 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
758 Scan; -- past PRIVATE
760 -- Check error cases of private [abstract] tagged
762 if Token = Tok_Abstract then
763 Error_Msg_SC ("`ABSTRACT TAGGED` must come before PRIVATE");
764 Scan; -- past ABSTRACT
766 if Token = Tok_Tagged then
767 Scan; -- past TAGGED
768 end if;
770 elsif Token = Tok_Tagged then
771 Error_Msg_SC ("TAGGED must come before PRIVATE");
772 Scan; -- past TAGGED
773 end if;
775 exit;
777 -- Ada 2005 (AI-345): Protected, synchronized or task interface
778 -- or Ada 2005 (AI-443): Synchronized private extension.
780 when Tok_Protected |
781 Tok_Synchronized |
782 Tok_Task =>
784 declare
785 Saved_Token : constant Token_Type := Token;
787 begin
788 Scan; -- past TASK, PROTECTED or SYNCHRONIZED
790 -- Synchronized private extension
792 if Token = Tok_New then
793 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
795 if Saved_Token = Tok_Synchronized then
796 if Nkind (Typedef_Node) =
797 N_Derived_Type_Definition
798 then
799 Error_Msg_N
800 ("SYNCHRONIZED not allowed for record extension",
801 Typedef_Node);
802 else
803 Set_Synchronized_Present (Typedef_Node);
804 end if;
806 else
807 Error_Msg_SC ("invalid kind of private extension");
808 end if;
810 -- Interface
812 else
813 if Token /= Tok_Interface then
814 Error_Msg_SC ("NEW or INTERFACE expected");
815 end if;
817 Typedef_Node :=
818 P_Interface_Type_Definition (Abstract_Present);
819 Abstract_Present := True;
821 case Saved_Token is
822 when Tok_Task =>
823 Set_Task_Present (Typedef_Node);
825 when Tok_Protected =>
826 Set_Protected_Present (Typedef_Node);
828 when Tok_Synchronized =>
829 Set_Synchronized_Present (Typedef_Node);
831 when others =>
832 pragma Assert (False);
833 null;
834 end case;
835 end if;
836 end;
838 exit;
840 -- Anything else is an error
842 when others =>
843 if Bad_Spelling_Of (Tok_Access)
844 or else
845 Bad_Spelling_Of (Tok_Array)
846 or else
847 Bad_Spelling_Of (Tok_Delta)
848 or else
849 Bad_Spelling_Of (Tok_Digits)
850 or else
851 Bad_Spelling_Of (Tok_Limited)
852 or else
853 Bad_Spelling_Of (Tok_Private)
854 or else
855 Bad_Spelling_Of (Tok_Range)
856 or else
857 Bad_Spelling_Of (Tok_Record)
858 or else
859 Bad_Spelling_Of (Tok_Tagged)
860 then
861 null;
863 else
864 Error_Msg_AP ("type definition expected");
865 raise Error_Resync;
866 end if;
868 end case;
869 end loop;
871 -- For the private type declaration case, the private type declaration
872 -- node has been built, with the Tagged_Present and Limited_Present
873 -- flags set as needed, and Typedef_Node is left set to Empty.
875 if No (Typedef_Node) then
876 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
877 Set_Abstract_Present (Decl_Node, Abstract_Present);
879 -- For a private extension declaration, Typedef_Node contains the
880 -- N_Private_Extension_Declaration node, which we now complete. Note
881 -- that the private extension declaration, unlike a full type
882 -- declaration, does permit unknown discriminants.
884 elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
885 Decl_Node := Typedef_Node;
886 Set_Sloc (Decl_Node, Type_Loc);
887 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
888 Set_Abstract_Present (Typedef_Node, Abstract_Present);
890 -- In the full type declaration case, Typedef_Node has the type
891 -- definition and here is where we build the full type declaration
892 -- node. This is also where we check for improper use of an unknown
893 -- discriminant part (not allowed for full type declaration).
895 else
896 if Nkind (Typedef_Node) = N_Record_Definition
897 or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
898 and then Present (Record_Extension_Part (Typedef_Node)))
899 or else Is_Derived_Iface
900 then
901 Set_Abstract_Present (Typedef_Node, Abstract_Present);
903 elsif Abstract_Present then
904 Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
905 end if;
907 Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
908 Set_Type_Definition (Decl_Node, Typedef_Node);
910 if Unknown_Dis then
911 Error_Msg
912 ("Full type declaration cannot have unknown discriminants",
913 Discr_Sloc);
914 end if;
915 end if;
917 -- Remaining processing is common for all three cases
919 Set_Defining_Identifier (Decl_Node, Ident_Node);
920 Set_Discriminant_Specifications (Decl_Node, Discr_List);
921 P_Aspect_Specifications (Decl_Node);
922 return Decl_Node;
923 end P_Type_Declaration;
925 ----------------------------------
926 -- 3.2.1 Full Type Declaration --
927 ----------------------------------
929 -- Parsed by P_Type_Declaration (3.2.1)
931 ----------------------------
932 -- 3.2.1 Type Definition --
933 ----------------------------
935 -- Parsed by P_Type_Declaration (3.2.1)
937 --------------------------------
938 -- 3.2.2 Subtype Declaration --
939 --------------------------------
941 -- SUBTYPE_DECLARATION ::=
942 -- subtype DEFINING_IDENTIFIER is [NULL_EXCLUSION] SUBTYPE_INDICATION
943 -- [ASPECT_SPECIFICATIONS];
945 -- The caller has checked that the initial token is SUBTYPE
947 -- Error recovery: can raise Error_Resync
949 function P_Subtype_Declaration return Node_Id is
950 Decl_Node : Node_Id;
951 Not_Null_Present : Boolean := False;
953 begin
954 Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
955 Scan; -- past SUBTYPE
956 Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
957 TF_Is;
959 if Token = Tok_New then
960 Error_Msg_SC -- CODEFIX
961 ("NEW ignored (only allowed in type declaration)");
962 Scan; -- past NEW
963 end if;
965 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
966 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
968 Set_Subtype_Indication
969 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
970 P_Aspect_Specifications (Decl_Node);
971 return Decl_Node;
972 end P_Subtype_Declaration;
974 -------------------------------
975 -- 3.2.2 Subtype Indication --
976 -------------------------------
978 -- SUBTYPE_INDICATION ::=
979 -- [not null] SUBTYPE_MARK [CONSTRAINT]
981 -- Error recovery: can raise Error_Resync
983 function P_Null_Exclusion
984 (Allow_Anonymous_In_95 : Boolean := False) return Boolean
986 Not_Loc : constant Source_Ptr := Token_Ptr;
987 -- Source position of "not", if present
989 begin
990 if Token /= Tok_Not then
991 return False;
993 else
994 Scan; -- past NOT
996 if Token = Tok_Null then
997 Scan; -- past NULL
999 -- Ada 2005 (AI-441, AI-447): null_exclusion is illegal in Ada 95,
1000 -- except in the case of anonymous access types.
1002 -- Allow_Anonymous_In_95 will be True if we're parsing a formal
1003 -- parameter or discriminant, which are the only places where
1004 -- anonymous access types occur in Ada 95. "Formal : not null
1005 -- access ..." is legal in Ada 95, whereas "Formal : not null
1006 -- Named_Access_Type" is not.
1008 if Ada_Version >= Ada_2005
1009 or else (Ada_Version >= Ada_95
1010 and then Allow_Anonymous_In_95
1011 and then Token = Tok_Access)
1012 then
1013 null; -- OK
1015 else
1016 Error_Msg
1017 ("`NOT NULL` access type is an Ada 2005 extension", Not_Loc);
1018 Error_Msg
1019 ("\unit should be compiled with -gnat05 switch", Not_Loc);
1020 end if;
1022 else
1023 Error_Msg_SP ("NULL expected");
1024 end if;
1026 if Token = Tok_New then
1027 Error_Msg ("`NOT NULL` comes after NEW, not before", Not_Loc);
1028 end if;
1030 return True;
1031 end if;
1032 end P_Null_Exclusion;
1034 function P_Subtype_Indication
1035 (Not_Null_Present : Boolean := False) return Node_Id
1037 Type_Node : Node_Id;
1039 begin
1040 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
1041 Type_Node := P_Subtype_Mark;
1042 return P_Subtype_Indication (Type_Node, Not_Null_Present);
1044 else
1045 -- Check for error of using record definition and treat it nicely,
1046 -- otherwise things are really messed up, so resynchronize.
1048 if Token = Tok_Record then
1049 Error_Msg_SC ("anonymous record definitions are not permitted");
1050 Discard_Junk_Node (P_Record_Definition);
1051 return Error;
1053 else
1054 Error_Msg_AP ("subtype indication expected");
1055 raise Error_Resync;
1056 end if;
1057 end if;
1058 end P_Subtype_Indication;
1060 -- The following function is identical except that it is called with
1061 -- the subtype mark already scanned out, and it scans out the constraint
1063 -- Error recovery: can raise Error_Resync
1065 function P_Subtype_Indication
1066 (Subtype_Mark : Node_Id;
1067 Not_Null_Present : Boolean := False) return Node_Id
1069 Indic_Node : Node_Id;
1070 Constr_Node : Node_Id;
1072 begin
1073 Constr_Node := P_Constraint_Opt;
1075 if No (Constr_Node)
1076 or else
1077 (Nkind (Constr_Node) = N_Range_Constraint
1078 and then Nkind (Range_Expression (Constr_Node)) = N_Error)
1079 then
1080 return Subtype_Mark;
1081 else
1082 if Not_Null_Present then
1083 Error_Msg_SP ("`NOT NULL` not allowed if constraint given");
1084 end if;
1086 Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
1087 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
1088 Set_Constraint (Indic_Node, Constr_Node);
1089 return Indic_Node;
1090 end if;
1091 end P_Subtype_Indication;
1093 -------------------------
1094 -- 3.2.2 Subtype Mark --
1095 -------------------------
1097 -- SUBTYPE_MARK ::= subtype_NAME;
1099 -- Note: The subtype mark which appears after an IN or NOT IN
1100 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
1102 -- Error recovery: cannot raise Error_Resync
1104 function P_Subtype_Mark return Node_Id is
1105 begin
1106 return P_Subtype_Mark_Resync;
1107 exception
1108 when Error_Resync =>
1109 return Error;
1110 end P_Subtype_Mark;
1112 -- This routine differs from P_Subtype_Mark in that it insists that an
1113 -- identifier be present, and if it is not, it raises Error_Resync.
1115 -- Error recovery: can raise Error_Resync
1117 function P_Subtype_Mark_Resync return Node_Id is
1118 Type_Node : Node_Id;
1120 begin
1121 if Token = Tok_Access then
1122 Error_Msg_SC ("anonymous access type definition not allowed here");
1123 Scan; -- past ACCESS
1124 end if;
1126 if Token = Tok_Array then
1127 Error_Msg_SC ("anonymous array definition not allowed here");
1128 Discard_Junk_Node (P_Array_Type_Definition);
1129 return Error;
1131 else
1132 Type_Node := P_Qualified_Simple_Name_Resync;
1134 -- Check for a subtype mark attribute. The only valid possibilities
1135 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
1136 -- as well catch it here.
1138 if Token = Tok_Apostrophe then
1139 return P_Subtype_Mark_Attribute (Type_Node);
1140 else
1141 return Type_Node;
1142 end if;
1143 end if;
1144 end P_Subtype_Mark_Resync;
1146 -- The following function is called to scan out a subtype mark attribute.
1147 -- The caller has already scanned out the subtype mark, which is passed in
1148 -- as the argument, and has checked that the current token is apostrophe.
1150 -- Only a special subclass of attributes, called type attributes
1151 -- (see Snames package) are allowed in this syntactic position.
1153 -- Note: if the apostrophe is followed by other than an identifier, then
1154 -- the input expression is returned unchanged, and the scan pointer is
1155 -- left pointing to the apostrophe.
1157 -- Error recovery: can raise Error_Resync
1159 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
1160 Attr_Node : Node_Id := Empty;
1161 Scan_State : Saved_Scan_State;
1162 Prefix : Node_Id;
1164 begin
1165 Prefix := Check_Subtype_Mark (Type_Node);
1167 if Prefix = Error then
1168 raise Error_Resync;
1169 end if;
1171 -- Loop through attributes appearing (more than one can appear as for
1172 -- for example in X'Base'Class). We are at an apostrophe on entry to
1173 -- this loop, and it runs once for each attribute parsed, with
1174 -- Prefix being the current possible prefix if it is an attribute.
1176 loop
1177 Save_Scan_State (Scan_State); -- at Apostrophe
1178 Scan; -- past apostrophe
1180 if Token /= Tok_Identifier then
1181 Restore_Scan_State (Scan_State); -- to apostrophe
1182 return Prefix; -- no attribute after all
1184 elsif not Is_Type_Attribute_Name (Token_Name) then
1185 Error_Msg_N
1186 ("attribute & may not be used in a subtype mark", Token_Node);
1187 raise Error_Resync;
1189 else
1190 Attr_Node :=
1191 Make_Attribute_Reference (Prev_Token_Ptr,
1192 Prefix => Prefix,
1193 Attribute_Name => Token_Name);
1194 Scan; -- past type attribute identifier
1195 end if;
1197 exit when Token /= Tok_Apostrophe;
1198 Prefix := Attr_Node;
1199 end loop;
1201 -- Fall through here after scanning type attribute
1203 return Attr_Node;
1204 end P_Subtype_Mark_Attribute;
1206 -----------------------
1207 -- 3.2.2 Constraint --
1208 -----------------------
1210 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
1212 -- SCALAR_CONSTRAINT ::=
1213 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
1215 -- COMPOSITE_CONSTRAINT ::=
1216 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
1218 -- If no constraint is present, this function returns Empty
1220 -- Error recovery: can raise Error_Resync
1222 function P_Constraint_Opt return Node_Id is
1223 begin
1224 if Token = Tok_Range or else Bad_Spelling_Of (Tok_Range) then
1225 return P_Range_Constraint;
1227 elsif Token = Tok_Digits or else Bad_Spelling_Of (Tok_Digits) then
1228 return P_Digits_Constraint;
1230 elsif Token = Tok_Delta or else Bad_Spelling_Of (Tok_Delta) then
1231 return P_Delta_Constraint;
1233 elsif Token = Tok_Left_Paren then
1234 return P_Index_Or_Discriminant_Constraint;
1236 elsif Token = Tok_In then
1237 Ignore (Tok_In);
1238 return P_Constraint_Opt;
1240 -- One more possibility is e.g. 1 .. 10 (i.e. missing RANGE keyword)
1242 elsif Token = Tok_Identifier or else
1243 Token = Tok_Integer_Literal or else
1244 Token = Tok_Real_Literal
1245 then
1246 declare
1247 Scan_State : Saved_Scan_State;
1249 begin
1250 Save_Scan_State (Scan_State); -- at identifier or literal
1251 Scan; -- past identifier or literal
1253 if Token = Tok_Dot_Dot then
1254 Restore_Scan_State (Scan_State);
1255 Error_Msg_BC ("missing RANGE keyword");
1256 return P_Range_Constraint;
1257 else
1258 Restore_Scan_State (Scan_State);
1259 return Empty;
1260 end if;
1261 end;
1263 -- Nothing worked, no constraint there
1265 else
1266 return Empty;
1267 end if;
1268 end P_Constraint_Opt;
1270 ------------------------------
1271 -- 3.2.2 Scalar Constraint --
1272 ------------------------------
1274 -- Parsed by P_Constraint_Opt (3.2.2)
1276 ---------------------------------
1277 -- 3.2.2 Composite Constraint --
1278 ---------------------------------
1280 -- Parsed by P_Constraint_Opt (3.2.2)
1282 --------------------------------------------------------
1283 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
1284 --------------------------------------------------------
1286 -- This routine scans out a declaration starting with an identifier:
1288 -- OBJECT_DECLARATION ::=
1289 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1290 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION]
1291 -- [ASPECT_SPECIFICATIONS];
1292 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1293 -- ACCESS_DEFINITION [:= EXPRESSION]
1294 -- [ASPECT_SPECIFICATIONS];
1295 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1296 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION]
1297 -- [ASPECT_SPECIFICATIONS];
1299 -- NUMBER_DECLARATION ::=
1300 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
1302 -- OBJECT_RENAMING_DECLARATION ::=
1303 -- DEFINING_IDENTIFIER :
1304 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME
1305 -- [ASPECT_SPECIFICATIONS];
1306 -- | DEFINING_IDENTIFIER :
1307 -- ACCESS_DEFINITION renames object_NAME
1308 -- [ASPECT_SPECIFICATIONS];
1310 -- EXCEPTION_RENAMING_DECLARATION ::=
1311 -- DEFINING_IDENTIFIER : exception renames exception_NAME
1312 -- [ASPECT_SPECIFICATIONS];
1314 -- EXCEPTION_DECLARATION ::=
1315 -- DEFINING_IDENTIFIER_LIST : exception
1316 -- [ASPECT_SPECIFICATIONS];
1318 -- Note that the ALIASED indication in an object declaration is
1319 -- marked by a flag in the parent node.
1321 -- The caller has checked that the initial token is an identifier
1323 -- The value returned is a list of declarations, one for each identifier
1324 -- in the list (as described in Sinfo, we always split up multiple
1325 -- declarations into the equivalent sequence of single declarations
1326 -- using the More_Ids and Prev_Ids flags to preserve the source).
1328 -- If the identifier turns out to be a probable statement rather than
1329 -- an identifier, then the scan is left pointing to the identifier and
1330 -- No_List is returned.
1332 -- Error recovery: can raise Error_Resync
1334 procedure P_Identifier_Declarations
1335 (Decls : List_Id;
1336 Done : out Boolean;
1337 In_Spec : Boolean)
1339 Acc_Node : Node_Id;
1340 Decl_Node : Node_Id;
1341 Type_Node : Node_Id;
1342 Ident_Sloc : Source_Ptr;
1343 Scan_State : Saved_Scan_State;
1344 List_OK : Boolean := True;
1345 Ident : Nat;
1346 Init_Expr : Node_Id;
1347 Init_Loc : Source_Ptr;
1348 Con_Loc : Source_Ptr;
1349 Not_Null_Present : Boolean := False;
1351 Idents : array (Int range 1 .. 4096) of Entity_Id;
1352 -- Used to save identifiers in the identifier list. The upper bound
1353 -- of 4096 is expected to be infinite in practice, and we do not even
1354 -- bother to check if this upper bound is exceeded.
1356 Num_Idents : Nat := 1;
1357 -- Number of identifiers stored in Idents
1359 procedure No_List;
1360 -- This procedure is called in renames cases to make sure that we do
1361 -- not have more than one identifier. If we do have more than one
1362 -- then an error message is issued (and the declaration is split into
1363 -- multiple declarations)
1365 function Token_Is_Renames return Boolean;
1366 -- Checks if current token is RENAMES, and if so, scans past it and
1367 -- returns True, otherwise returns False. Includes checking for some
1368 -- common error cases.
1370 -------------
1371 -- No_List --
1372 -------------
1374 procedure No_List is
1375 begin
1376 if Num_Idents > 1 then
1377 Error_Msg
1378 ("identifier list not allowed for RENAMES",
1379 Sloc (Idents (2)));
1380 end if;
1382 List_OK := False;
1383 end No_List;
1385 ----------------------
1386 -- Token_Is_Renames --
1387 ----------------------
1389 function Token_Is_Renames return Boolean is
1390 At_Colon : Saved_Scan_State;
1392 begin
1393 if Token = Tok_Colon then
1394 Save_Scan_State (At_Colon);
1395 Scan; -- past colon
1396 Check_Misspelling_Of (Tok_Renames);
1398 if Token = Tok_Renames then
1399 Error_Msg_SP -- CODEFIX
1400 ("|extra "":"" ignored");
1401 Scan; -- past RENAMES
1402 return True;
1403 else
1404 Restore_Scan_State (At_Colon);
1405 return False;
1406 end if;
1408 else
1409 Check_Misspelling_Of (Tok_Renames);
1411 if Token = Tok_Renames then
1412 Scan; -- past RENAMES
1413 return True;
1414 else
1415 return False;
1416 end if;
1417 end if;
1418 end Token_Is_Renames;
1420 -- Start of processing for P_Identifier_Declarations
1422 begin
1423 Ident_Sloc := Token_Ptr;
1424 Save_Scan_State (Scan_State); -- at first identifier
1425 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1427 -- If we have a colon after the identifier, then we can assume that
1428 -- this is in fact a valid identifier declaration and can steam ahead.
1430 if Token = Tok_Colon then
1431 Scan; -- past colon
1433 -- If we have a comma, then scan out the list of identifiers
1435 elsif Token = Tok_Comma then
1436 while Comma_Present loop
1437 Num_Idents := Num_Idents + 1;
1438 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1439 end loop;
1441 Save_Scan_State (Scan_State); -- at colon
1442 T_Colon;
1444 -- If we have identifier followed by := then we assume that what is
1445 -- really meant is an assignment statement. The assignment statement
1446 -- is scanned out and added to the list of declarations. An exception
1447 -- occurs if the := is followed by the keyword constant, in which case
1448 -- we assume it was meant to be a colon.
1450 elsif Token = Tok_Colon_Equal then
1451 Scan; -- past :=
1453 if Token = Tok_Constant then
1454 Error_Msg_SP ("colon expected");
1456 else
1457 Restore_Scan_State (Scan_State);
1458 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1459 return;
1460 end if;
1462 -- If we have an IS keyword, then assume the TYPE keyword was missing
1464 elsif Token = Tok_Is then
1465 Restore_Scan_State (Scan_State);
1466 Append_To (Decls, P_Type_Declaration);
1467 Done := False;
1468 return;
1470 -- Otherwise we have an error situation
1472 else
1473 Restore_Scan_State (Scan_State);
1475 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1476 -- so, fix the keyword and return to scan the protected declaration.
1478 if Token_Name = Name_Protected then
1479 Check_95_Keyword (Tok_Protected, Tok_Identifier);
1480 Check_95_Keyword (Tok_Protected, Tok_Type);
1481 Check_95_Keyword (Tok_Protected, Tok_Body);
1483 if Token = Tok_Protected then
1484 Done := False;
1485 return;
1486 end if;
1488 -- Check misspelling possibilities. If so, correct the misspelling
1489 -- and return to scan out the resulting declaration.
1491 elsif Bad_Spelling_Of (Tok_Function)
1492 or else Bad_Spelling_Of (Tok_Procedure)
1493 or else Bad_Spelling_Of (Tok_Package)
1494 or else Bad_Spelling_Of (Tok_Pragma)
1495 or else Bad_Spelling_Of (Tok_Protected)
1496 or else Bad_Spelling_Of (Tok_Generic)
1497 or else Bad_Spelling_Of (Tok_Subtype)
1498 or else Bad_Spelling_Of (Tok_Type)
1499 or else Bad_Spelling_Of (Tok_Task)
1500 or else Bad_Spelling_Of (Tok_Use)
1501 or else Bad_Spelling_Of (Tok_For)
1502 then
1503 Done := False;
1504 return;
1506 -- Otherwise we definitely have an ordinary identifier with a junk
1507 -- token after it. Just complain that we expect a declaration, and
1508 -- skip to a semicolon
1510 else
1511 Set_Declaration_Expected;
1512 Resync_Past_Semicolon;
1513 Done := False;
1514 return;
1515 end if;
1516 end if;
1518 -- Come here with an identifier list and colon scanned out. We now
1519 -- build the nodes for the declarative items. One node is built for
1520 -- each identifier in the list, with the type information being
1521 -- repeated by rescanning the appropriate section of source.
1523 -- First an error check, if we have two identifiers in a row, a likely
1524 -- possibility is that the first of the identifiers is an incorrectly
1525 -- spelled keyword.
1527 if Token = Tok_Identifier then
1528 declare
1529 SS : Saved_Scan_State;
1530 I2 : Boolean;
1532 begin
1533 Save_Scan_State (SS);
1534 Scan; -- past initial identifier
1535 I2 := (Token = Tok_Identifier);
1536 Restore_Scan_State (SS);
1538 if I2
1539 and then
1540 (Bad_Spelling_Of (Tok_Access) or else
1541 Bad_Spelling_Of (Tok_Aliased) or else
1542 Bad_Spelling_Of (Tok_Constant))
1543 then
1544 null;
1545 end if;
1546 end;
1547 end if;
1549 -- Loop through identifiers
1551 Ident := 1;
1552 Ident_Loop : loop
1554 -- Check for some cases of misused Ada 95 keywords
1556 if Token_Name = Name_Aliased then
1557 Check_95_Keyword (Tok_Aliased, Tok_Array);
1558 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1559 Check_95_Keyword (Tok_Aliased, Tok_Constant);
1560 end if;
1562 -- Constant cases
1564 if Token = Tok_Constant then
1565 Con_Loc := Token_Ptr;
1566 Scan; -- past CONSTANT
1568 -- Number declaration, initialization required
1570 Init_Expr := Init_Expr_Opt;
1572 if Present (Init_Expr) then
1573 if Not_Null_Present then
1574 Error_Msg_SP
1575 ("`NOT NULL` not allowed in numeric expression");
1576 end if;
1578 Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1579 Set_Expression (Decl_Node, Init_Expr);
1581 -- Constant object declaration
1583 else
1584 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1585 Set_Constant_Present (Decl_Node, True);
1587 if Token_Name = Name_Aliased then
1588 Check_95_Keyword (Tok_Aliased, Tok_Array);
1589 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1590 end if;
1592 if Token = Tok_Aliased then
1593 Error_Msg_SC -- CODEFIX
1594 ("ALIASED should be before CONSTANT");
1595 Scan; -- past ALIASED
1596 Set_Aliased_Present (Decl_Node, True);
1597 end if;
1599 if Token = Tok_Array then
1600 Set_Object_Definition
1601 (Decl_Node, P_Array_Type_Definition);
1603 else
1604 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1605 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1607 if Token = Tok_Access then
1608 if Ada_Version < Ada_2005 then
1609 Error_Msg_SP
1610 ("generalized use of anonymous access types " &
1611 "is an Ada 2005 extension");
1612 Error_Msg_SP
1613 ("\unit must be compiled with -gnat05 switch");
1614 end if;
1616 Set_Object_Definition
1617 (Decl_Node, P_Access_Definition (Not_Null_Present));
1618 else
1619 Set_Object_Definition
1620 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1621 end if;
1622 end if;
1624 if Token = Tok_Renames then
1625 Error_Msg
1626 ("CONSTANT not permitted in renaming declaration",
1627 Con_Loc);
1628 Scan; -- Past renames
1629 Discard_Junk_Node (P_Name);
1630 end if;
1631 end if;
1633 -- Exception cases
1635 elsif Token = Tok_Exception then
1636 Scan; -- past EXCEPTION
1638 if Token_Is_Renames then
1639 No_List;
1640 Decl_Node :=
1641 New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1642 Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1643 No_Constraint;
1644 else
1645 Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1646 end if;
1648 -- Aliased case (note that an object definition is required)
1650 elsif Token = Tok_Aliased then
1651 Scan; -- past ALIASED
1652 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1653 Set_Aliased_Present (Decl_Node, True);
1655 if Token = Tok_Constant then
1656 Scan; -- past CONSTANT
1657 Set_Constant_Present (Decl_Node, True);
1658 end if;
1660 if Token = Tok_Array then
1661 Set_Object_Definition
1662 (Decl_Node, P_Array_Type_Definition);
1664 else
1665 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1666 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1668 -- Access definition (AI-406) or subtype indication
1670 if Token = Tok_Access then
1671 if Ada_Version < Ada_2005 then
1672 Error_Msg_SP
1673 ("generalized use of anonymous access types " &
1674 "is an Ada 2005 extension");
1675 Error_Msg_SP
1676 ("\unit must be compiled with -gnat05 switch");
1677 end if;
1679 Set_Object_Definition
1680 (Decl_Node, P_Access_Definition (Not_Null_Present));
1681 else
1682 Set_Object_Definition
1683 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1684 end if;
1685 end if;
1687 -- Array case
1689 elsif Token = Tok_Array then
1690 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1691 Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1693 -- Ada 2005 (AI-254, AI-406)
1695 elsif Token = Tok_Not then
1697 -- OBJECT_DECLARATION ::=
1698 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1699 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION]
1700 -- [ASPECT_SPECIFICATIONS];
1701 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1702 -- ACCESS_DEFINITION [:= EXPRESSION]
1703 -- [ASPECT_SPECIFICATIONS];
1705 -- OBJECT_RENAMING_DECLARATION ::=
1706 -- DEFINING_IDENTIFIER :
1707 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME
1708 -- [ASPECT_SPECIFICATIONS];
1709 -- | DEFINING_IDENTIFIER :
1710 -- ACCESS_DEFINITION renames object_NAME
1711 -- [ASPECT_SPECIFICATIONS];
1713 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/423)
1715 if Token = Tok_Access then
1716 if Ada_Version < Ada_2005 then
1717 Error_Msg_SP
1718 ("generalized use of anonymous access types " &
1719 "is an Ada 2005 extension");
1720 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1721 end if;
1723 Acc_Node := P_Access_Definition (Not_Null_Present);
1725 if Token /= Tok_Renames then
1726 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1727 Set_Object_Definition (Decl_Node, Acc_Node);
1729 else
1730 Scan; -- past renames
1731 No_List;
1732 Decl_Node :=
1733 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1734 Set_Access_Definition (Decl_Node, Acc_Node);
1735 Set_Name (Decl_Node, P_Name);
1736 end if;
1738 else
1739 Type_Node := P_Subtype_Mark;
1741 -- Object renaming declaration
1743 if Token_Is_Renames then
1744 if Ada_Version < Ada_2005 then
1745 Error_Msg_SP
1746 ("`NOT NULL` not allowed in object renaming");
1747 raise Error_Resync;
1749 -- Ada 2005 (AI-423): Object renaming declaration with
1750 -- a null exclusion.
1752 else
1753 No_List;
1754 Decl_Node :=
1755 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1756 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1757 Set_Subtype_Mark (Decl_Node, Type_Node);
1758 Set_Name (Decl_Node, P_Name);
1759 end if;
1761 -- Object declaration
1763 else
1764 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1765 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1766 Set_Object_Definition
1767 (Decl_Node,
1768 P_Subtype_Indication (Type_Node, Not_Null_Present));
1770 -- RENAMES at this point means that we had the combination
1771 -- of a constraint on the Type_Node and renames, which is
1772 -- illegal
1774 if Token_Is_Renames then
1775 Error_Msg_N
1776 ("constraint not allowed in object renaming "
1777 & "declaration",
1778 Constraint (Object_Definition (Decl_Node)));
1779 raise Error_Resync;
1780 end if;
1781 end if;
1782 end if;
1784 -- Ada 2005 (AI-230): Access Definition case
1786 elsif Token = Tok_Access then
1787 if Ada_Version < Ada_2005 then
1788 Error_Msg_SP
1789 ("generalized use of anonymous access types " &
1790 "is an Ada 2005 extension");
1791 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1792 end if;
1794 Acc_Node := P_Access_Definition (Null_Exclusion_Present => False);
1796 -- Object declaration with access definition, or renaming
1798 if Token /= Tok_Renames then
1799 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1800 Set_Object_Definition (Decl_Node, Acc_Node);
1802 else
1803 Scan; -- past renames
1804 No_List;
1805 Decl_Node :=
1806 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1807 Set_Access_Definition (Decl_Node, Acc_Node);
1808 Set_Name (Decl_Node, P_Name);
1809 end if;
1811 -- Subtype indication case
1813 else
1814 Type_Node := P_Subtype_Mark;
1816 -- Object renaming declaration
1818 if Token_Is_Renames then
1819 No_List;
1820 Decl_Node :=
1821 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1822 Set_Subtype_Mark (Decl_Node, Type_Node);
1823 Set_Name (Decl_Node, P_Name);
1825 -- Object declaration
1827 else
1828 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1829 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1830 Set_Object_Definition
1831 (Decl_Node,
1832 P_Subtype_Indication (Type_Node, Not_Null_Present));
1834 -- RENAMES at this point means that we had the combination of
1835 -- a constraint on the Type_Node and renames, which is illegal
1837 if Token_Is_Renames then
1838 Error_Msg_N
1839 ("constraint not allowed in object renaming declaration",
1840 Constraint (Object_Definition (Decl_Node)));
1841 raise Error_Resync;
1842 end if;
1843 end if;
1844 end if;
1846 -- Scan out initialization, allowed only for object declaration
1848 Init_Loc := Token_Ptr;
1849 Init_Expr := Init_Expr_Opt;
1851 if Present (Init_Expr) then
1852 if Nkind (Decl_Node) = N_Object_Declaration then
1853 Set_Expression (Decl_Node, Init_Expr);
1854 Set_Has_Init_Expression (Decl_Node);
1855 else
1856 Error_Msg ("initialization not allowed here", Init_Loc);
1857 end if;
1858 end if;
1860 Set_Defining_Identifier (Decl_Node, Idents (Ident));
1861 P_Aspect_Specifications (Decl_Node);
1863 if List_OK then
1864 if Ident < Num_Idents then
1865 Set_More_Ids (Decl_Node, True);
1866 end if;
1868 if Ident > 1 then
1869 Set_Prev_Ids (Decl_Node, True);
1870 end if;
1871 end if;
1873 Append (Decl_Node, Decls);
1874 exit Ident_Loop when Ident = Num_Idents;
1875 Restore_Scan_State (Scan_State);
1876 T_Colon;
1877 Ident := Ident + 1;
1878 end loop Ident_Loop;
1880 Done := False;
1881 end P_Identifier_Declarations;
1883 -------------------------------
1884 -- 3.3.1 Object Declaration --
1885 -------------------------------
1887 -- OBJECT DECLARATION ::=
1888 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1889 -- SUBTYPE_INDICATION [:= EXPRESSION];
1890 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1891 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1892 -- | SINGLE_TASK_DECLARATION
1893 -- | SINGLE_PROTECTED_DECLARATION
1895 -- Cases starting with TASK are parsed by P_Task (9.1)
1896 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1897 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1899 -------------------------------------
1900 -- 3.3.1 Defining Identifier List --
1901 -------------------------------------
1903 -- DEFINING_IDENTIFIER_LIST ::=
1904 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1906 -- Always parsed by the construct in which it appears. See special
1907 -- section on "Handling of Defining Identifier Lists" in this unit.
1909 -------------------------------
1910 -- 3.3.2 Number Declaration --
1911 -------------------------------
1913 -- Parsed by P_Identifier_Declarations (3.3)
1915 -------------------------------------------------------------------------
1916 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
1917 -------------------------------------------------------------------------
1919 -- DERIVED_TYPE_DEFINITION ::=
1920 -- [abstract] [limited] new [NULL_EXCLUSION] parent_SUBTYPE_INDICATION
1921 -- [[and INTERFACE_LIST] RECORD_EXTENSION_PART]
1923 -- PRIVATE_EXTENSION_DECLARATION ::=
1924 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1925 -- [abstract] [limited | synchronized]
1926 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
1927 -- with private [ASPECT_SPECIFICATIONS];
1929 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1931 -- The caller has already scanned out the part up to the NEW, and Token
1932 -- either contains Tok_New (or ought to, if it doesn't this procedure
1933 -- will post an appropriate "NEW expected" message).
1935 -- Note: the caller is responsible for filling in the Sloc field of
1936 -- the returned node in the private extension declaration case as
1937 -- well as the stuff relating to the discriminant part.
1939 -- Error recovery: can raise Error_Resync;
1941 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
1942 Typedef_Node : Node_Id;
1943 Typedecl_Node : Node_Id;
1944 Not_Null_Present : Boolean := False;
1946 begin
1947 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
1949 if Ada_Version < Ada_2005
1950 and then Token = Tok_Identifier
1951 and then Token_Name = Name_Interface
1952 then
1953 Error_Msg_SP
1954 ("abstract interface is an Ada 2005 extension");
1955 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1956 else
1957 T_New;
1958 end if;
1960 if Token = Tok_Abstract then
1961 Error_Msg_SC -- CODEFIX
1962 ("ABSTRACT must come before NEW, not after");
1963 Scan;
1964 end if;
1966 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1967 Set_Null_Exclusion_Present (Typedef_Node, Not_Null_Present);
1968 Set_Subtype_Indication (Typedef_Node,
1969 P_Subtype_Indication (Not_Null_Present));
1971 -- Ada 2005 (AI-251): Deal with interfaces
1973 if Token = Tok_And then
1974 Scan; -- past AND
1976 if Ada_Version < Ada_2005 then
1977 Error_Msg_SP
1978 ("abstract interface is an Ada 2005 extension");
1979 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1980 end if;
1982 Set_Interface_List (Typedef_Node, New_List);
1984 loop
1985 Append (P_Qualified_Simple_Name, Interface_List (Typedef_Node));
1986 exit when Token /= Tok_And;
1987 Scan; -- past AND
1988 end loop;
1990 if Token /= Tok_With then
1991 Error_Msg_SC ("WITH expected");
1992 raise Error_Resync;
1993 end if;
1994 end if;
1996 -- Deal with record extension, note that we assume that a WITH is
1997 -- missing in the case of "type X is new Y record ..." or in the
1998 -- case of "type X is new Y null record".
2000 -- First make sure we don't have an aspect specification. If we do
2001 -- return now, so that our caller can check it (the WITH here is not
2002 -- part of a type extension).
2004 if Aspect_Specifications_Present then
2005 return Typedef_Node;
2007 -- OK, not an aspect specification, so continue test for extension
2009 elsif Token = Tok_With
2010 or else Token = Tok_Record
2011 or else Token = Tok_Null
2012 then
2013 T_With; -- past WITH or give error message
2015 if Token = Tok_Limited then
2016 Error_Msg_SC ("LIMITED keyword not allowed in private extension");
2017 Scan; -- ignore LIMITED
2018 end if;
2020 -- Private extension declaration
2022 if Token = Tok_Private then
2023 Scan; -- past PRIVATE
2025 -- Throw away the type definition node and build the type
2026 -- declaration node. Note the caller must set the Sloc,
2027 -- Discriminant_Specifications, Unknown_Discriminants_Present,
2028 -- and Defined_Identifier fields in the returned node.
2030 Typedecl_Node :=
2031 Make_Private_Extension_Declaration (No_Location,
2032 Defining_Identifier => Empty,
2033 Subtype_Indication => Subtype_Indication (Typedef_Node),
2034 Abstract_Present => Abstract_Present (Typedef_Node),
2035 Interface_List => Interface_List (Typedef_Node));
2037 return Typedecl_Node;
2039 -- Derived type definition with record extension part
2041 else
2042 Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
2043 return Typedef_Node;
2044 end if;
2046 -- Derived type definition with no record extension part
2048 else
2049 return Typedef_Node;
2050 end if;
2051 end P_Derived_Type_Def_Or_Private_Ext_Decl;
2053 ---------------------------
2054 -- 3.5 Range Constraint --
2055 ---------------------------
2057 -- RANGE_CONSTRAINT ::= range RANGE
2059 -- The caller has checked that the initial token is RANGE or some
2060 -- misspelling of it, or it may be absent completely (and a message
2061 -- has already been issued).
2063 -- Error recovery: cannot raise Error_Resync
2065 function P_Range_Constraint return Node_Id is
2066 Range_Node : Node_Id;
2068 begin
2069 Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
2071 -- Skip range keyword if present
2073 if Token = Tok_Range or else Bad_Spelling_Of (Tok_Range) then
2074 Scan; -- past RANGE
2075 end if;
2077 Set_Range_Expression (Range_Node, P_Range);
2078 return Range_Node;
2079 end P_Range_Constraint;
2081 ----------------
2082 -- 3.5 Range --
2083 ----------------
2085 -- RANGE ::=
2086 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2088 -- Note: the range that appears in a membership test is parsed by
2089 -- P_Range_Or_Subtype_Mark (3.5).
2091 -- Error recovery: cannot raise Error_Resync
2093 function P_Range return Node_Id is
2094 Expr_Node : Node_Id;
2095 Range_Node : Node_Id;
2097 begin
2098 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2100 if Expr_Form = EF_Range_Attr then
2101 return Expr_Node;
2103 elsif Token = Tok_Dot_Dot then
2104 Range_Node := New_Node (N_Range, Token_Ptr);
2105 Set_Low_Bound (Range_Node, Expr_Node);
2106 Scan; -- past ..
2107 Expr_Node := P_Expression;
2108 Check_Simple_Expression (Expr_Node);
2109 Set_High_Bound (Range_Node, Expr_Node);
2110 return Range_Node;
2112 -- Anything else is an error
2114 else
2115 T_Dot_Dot; -- force missing .. message
2116 return Error;
2117 end if;
2118 end P_Range;
2120 ----------------------------------
2121 -- 3.5 P_Range_Or_Subtype_Mark --
2122 ----------------------------------
2124 -- RANGE ::=
2125 -- RANGE_ATTRIBUTE_REFERENCE
2126 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2128 -- This routine scans out the range or subtype mark that forms the right
2129 -- operand of a membership test (it is not used in any other contexts, and
2130 -- error messages are specialized with this knowledge in mind).
2132 -- Note: as documented in the Sinfo interface, although the syntax only
2133 -- allows a subtype mark, we in fact allow any simple expression to be
2134 -- returned from this routine. The semantics is responsible for issuing
2135 -- an appropriate message complaining if the argument is not a name.
2136 -- This simplifies the coding and error recovery processing in the
2137 -- parser, and in any case it is preferable not to consider this a
2138 -- syntax error and to continue with the semantic analysis.
2140 -- Error recovery: cannot raise Error_Resync
2142 function P_Range_Or_Subtype_Mark
2143 (Allow_Simple_Expression : Boolean := False) return Node_Id
2145 Expr_Node : Node_Id;
2146 Range_Node : Node_Id;
2147 Save_Loc : Source_Ptr;
2149 -- Start of processing for P_Range_Or_Subtype_Mark
2151 begin
2152 -- Save location of possible junk parentheses
2154 Save_Loc := Token_Ptr;
2156 -- Scan out either a simple expression or a range (this accepts more
2157 -- than is legal here, but as explained above, we like to allow more
2158 -- with a proper diagnostic, and in the case of a membership operation
2159 -- where sets are allowed, a simple expression is permissible anyway.
2161 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2163 -- Range attribute
2165 if Expr_Form = EF_Range_Attr then
2166 return Expr_Node;
2168 -- Simple_Expression .. Simple_Expression
2170 elsif Token = Tok_Dot_Dot then
2171 Check_Simple_Expression (Expr_Node);
2172 Range_Node := New_Node (N_Range, Token_Ptr);
2173 Set_Low_Bound (Range_Node, Expr_Node);
2174 Scan; -- past ..
2175 Set_High_Bound (Range_Node, P_Simple_Expression);
2176 return Range_Node;
2178 -- Case of subtype mark (optionally qualified simple name or an
2179 -- attribute whose prefix is an optionally qualified simple name)
2181 elsif Expr_Form = EF_Simple_Name
2182 or else Nkind (Expr_Node) = N_Attribute_Reference
2183 then
2184 -- Check for error of range constraint after a subtype mark
2186 if Token = Tok_Range then
2187 Error_Msg_SC ("range constraint not allowed in membership test");
2188 Scan; -- past RANGE
2189 raise Error_Resync;
2191 -- Check for error of DIGITS or DELTA after a subtype mark
2193 elsif Token = Tok_Digits or else Token = Tok_Delta then
2194 Error_Msg_SC
2195 ("accuracy definition not allowed in membership test");
2196 Scan; -- past DIGITS or DELTA
2197 raise Error_Resync;
2199 -- Attribute reference, may or may not be OK, but in any case we
2200 -- will scan it out
2202 elsif Token = Tok_Apostrophe then
2203 return P_Subtype_Mark_Attribute (Expr_Node);
2205 -- OK case of simple name, just return it
2207 else
2208 return Expr_Node;
2209 end if;
2211 -- Simple expression case
2213 elsif Expr_Form = EF_Simple and then Allow_Simple_Expression then
2214 return Expr_Node;
2216 -- Here we have some kind of error situation. Check for junk parens
2217 -- then return what we have, caller will deal with other errors.
2219 else
2220 if Nkind (Expr_Node) in N_Subexpr
2221 and then Paren_Count (Expr_Node) /= 0
2222 then
2223 Error_Msg ("|parentheses not allowed for subtype mark", Save_Loc);
2224 Set_Paren_Count (Expr_Node, 0);
2225 end if;
2227 return Expr_Node;
2228 end if;
2229 end P_Range_Or_Subtype_Mark;
2231 ----------------------------------------
2232 -- 3.5.1 Enumeration Type Definition --
2233 ----------------------------------------
2235 -- ENUMERATION_TYPE_DEFINITION ::=
2236 -- (ENUMERATION_LITERAL_SPECIFICATION
2237 -- {, ENUMERATION_LITERAL_SPECIFICATION})
2239 -- The caller has already scanned out the TYPE keyword
2241 -- Error recovery: can raise Error_Resync;
2243 function P_Enumeration_Type_Definition return Node_Id is
2244 Typedef_Node : Node_Id;
2246 begin
2247 Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
2248 Set_Literals (Typedef_Node, New_List);
2250 T_Left_Paren;
2252 loop
2253 Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
2254 exit when not Comma_Present;
2255 end loop;
2257 T_Right_Paren;
2258 return Typedef_Node;
2259 end P_Enumeration_Type_Definition;
2261 ----------------------------------------------
2262 -- 3.5.1 Enumeration Literal Specification --
2263 ----------------------------------------------
2265 -- ENUMERATION_LITERAL_SPECIFICATION ::=
2266 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
2268 -- Error recovery: can raise Error_Resync
2270 function P_Enumeration_Literal_Specification return Node_Id is
2271 begin
2272 if Token = Tok_Char_Literal then
2273 return P_Defining_Character_Literal;
2274 else
2275 return P_Defining_Identifier (C_Comma_Right_Paren);
2276 end if;
2277 end P_Enumeration_Literal_Specification;
2279 ---------------------------------------
2280 -- 3.5.1 Defining_Character_Literal --
2281 ---------------------------------------
2283 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
2285 -- Error recovery: cannot raise Error_Resync
2287 -- The caller has checked that the current token is a character literal
2289 function P_Defining_Character_Literal return Node_Id is
2290 Literal_Node : Node_Id;
2291 begin
2292 Literal_Node := Token_Node;
2293 Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
2294 Scan; -- past character literal
2295 return Literal_Node;
2296 end P_Defining_Character_Literal;
2298 ------------------------------------
2299 -- 3.5.4 Integer Type Definition --
2300 ------------------------------------
2302 -- Parsed by P_Type_Declaration (3.2.1)
2304 -------------------------------------------
2305 -- 3.5.4 Signed Integer Type Definition --
2306 -------------------------------------------
2308 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
2309 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2311 -- Normally the initial token on entry is RANGE, but in some
2312 -- error conditions, the range token was missing and control is
2313 -- passed with Token pointing to first token of the first expression.
2315 -- Error recovery: cannot raise Error_Resync
2317 function P_Signed_Integer_Type_Definition return Node_Id is
2318 Typedef_Node : Node_Id;
2319 Expr_Node : Node_Id;
2321 begin
2322 Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
2324 if Token = Tok_Range then
2325 Scan; -- past RANGE
2326 end if;
2328 Expr_Node := P_Expression_Or_Range_Attribute;
2330 -- Range case (not permitted by the grammar, this is surprising but
2331 -- the grammar in the RM is as quoted above, and does not allow Range).
2333 if Expr_Form = EF_Range_Attr then
2334 Error_Msg_N
2335 ("Range attribute not allowed here, use First .. Last", Expr_Node);
2336 Set_Low_Bound (Typedef_Node, Expr_Node);
2337 Set_Attribute_Name (Expr_Node, Name_First);
2338 Set_High_Bound (Typedef_Node, Copy_Separate_Tree (Expr_Node));
2339 Set_Attribute_Name (High_Bound (Typedef_Node), Name_Last);
2341 -- Normal case of explicit range
2343 else
2344 Check_Simple_Expression (Expr_Node);
2345 Set_Low_Bound (Typedef_Node, Expr_Node);
2346 T_Dot_Dot;
2347 Expr_Node := P_Expression;
2348 Check_Simple_Expression (Expr_Node);
2349 Set_High_Bound (Typedef_Node, Expr_Node);
2350 end if;
2352 return Typedef_Node;
2353 end P_Signed_Integer_Type_Definition;
2355 ------------------------------------
2356 -- 3.5.4 Modular Type Definition --
2357 ------------------------------------
2359 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
2361 -- The caller has checked that the initial token is MOD
2363 -- Error recovery: cannot raise Error_Resync
2365 function P_Modular_Type_Definition return Node_Id is
2366 Typedef_Node : Node_Id;
2368 begin
2369 if Ada_Version = Ada_83 then
2370 Error_Msg_SC ("(Ada 83): modular types not allowed");
2371 end if;
2373 Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
2374 Scan; -- past MOD
2375 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2377 -- Handle mod L..R cleanly
2379 if Token = Tok_Dot_Dot then
2380 Error_Msg_SC ("range not allowed for modular type");
2381 Scan; -- past ..
2382 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2383 end if;
2385 return Typedef_Node;
2386 end P_Modular_Type_Definition;
2388 ---------------------------------
2389 -- 3.5.6 Real Type Definition --
2390 ---------------------------------
2392 -- Parsed by P_Type_Declaration (3.2.1)
2394 --------------------------------------
2395 -- 3.5.7 Floating Point Definition --
2396 --------------------------------------
2398 -- FLOATING_POINT_DEFINITION ::=
2399 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2401 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
2403 -- The caller has checked that the initial token is DIGITS
2405 -- Error recovery: cannot raise Error_Resync
2407 function P_Floating_Point_Definition return Node_Id is
2408 Digits_Loc : constant Source_Ptr := Token_Ptr;
2409 Def_Node : Node_Id;
2410 Expr_Node : Node_Id;
2412 begin
2413 Scan; -- past DIGITS
2414 Expr_Node := P_Expression_No_Right_Paren;
2415 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2417 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
2419 if Token = Tok_Delta then
2420 Error_Msg_SC -- CODEFIX
2421 ("|DELTA must come before DIGITS");
2422 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
2423 Scan; -- past DELTA
2424 Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
2426 -- OK floating-point definition
2428 else
2429 Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
2430 end if;
2432 Set_Digits_Expression (Def_Node, Expr_Node);
2433 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2434 return Def_Node;
2435 end P_Floating_Point_Definition;
2437 -------------------------------------
2438 -- 3.5.7 Real Range Specification --
2439 -------------------------------------
2441 -- REAL_RANGE_SPECIFICATION ::=
2442 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2444 -- Error recovery: cannot raise Error_Resync
2446 function P_Real_Range_Specification_Opt return Node_Id is
2447 Specification_Node : Node_Id;
2448 Expr_Node : Node_Id;
2450 begin
2451 if Token = Tok_Range then
2452 Specification_Node :=
2453 New_Node (N_Real_Range_Specification, Token_Ptr);
2454 Scan; -- past RANGE
2455 Expr_Node := P_Expression_No_Right_Paren;
2456 Check_Simple_Expression (Expr_Node);
2457 Set_Low_Bound (Specification_Node, Expr_Node);
2458 T_Dot_Dot;
2459 Expr_Node := P_Expression_No_Right_Paren;
2460 Check_Simple_Expression (Expr_Node);
2461 Set_High_Bound (Specification_Node, Expr_Node);
2462 return Specification_Node;
2463 else
2464 return Empty;
2465 end if;
2466 end P_Real_Range_Specification_Opt;
2468 -----------------------------------
2469 -- 3.5.9 Fixed Point Definition --
2470 -----------------------------------
2472 -- FIXED_POINT_DEFINITION ::=
2473 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
2475 -- ORDINARY_FIXED_POINT_DEFINITION ::=
2476 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
2478 -- DECIMAL_FIXED_POINT_DEFINITION ::=
2479 -- delta static_EXPRESSION
2480 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2482 -- The caller has checked that the initial token is DELTA
2484 -- Error recovery: cannot raise Error_Resync
2486 function P_Fixed_Point_Definition return Node_Id is
2487 Delta_Node : Node_Id;
2488 Delta_Loc : Source_Ptr;
2489 Def_Node : Node_Id;
2490 Expr_Node : Node_Id;
2492 begin
2493 Delta_Loc := Token_Ptr;
2494 Scan; -- past DELTA
2495 Delta_Node := P_Expression_No_Right_Paren;
2496 Check_Simple_Expression_In_Ada_83 (Delta_Node);
2498 if Token = Tok_Digits then
2499 if Ada_Version = Ada_83 then
2500 Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
2501 end if;
2503 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
2504 Scan; -- past DIGITS
2505 Expr_Node := P_Expression_No_Right_Paren;
2506 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2507 Set_Digits_Expression (Def_Node, Expr_Node);
2509 else
2510 Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
2512 -- Range is required in ordinary fixed point case
2514 if Token /= Tok_Range then
2515 Error_Msg_AP ("range must be given for fixed-point type");
2516 T_Range;
2517 end if;
2518 end if;
2520 Set_Delta_Expression (Def_Node, Delta_Node);
2521 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2522 return Def_Node;
2523 end P_Fixed_Point_Definition;
2525 --------------------------------------------
2526 -- 3.5.9 Ordinary Fixed Point Definition --
2527 --------------------------------------------
2529 -- Parsed by P_Fixed_Point_Definition (3.5.9)
2531 -------------------------------------------
2532 -- 3.5.9 Decimal Fixed Point Definition --
2533 -------------------------------------------
2535 -- Parsed by P_Decimal_Point_Definition (3.5.9)
2537 ------------------------------
2538 -- 3.5.9 Digits Constraint --
2539 ------------------------------
2541 -- DIGITS_CONSTRAINT ::=
2542 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
2544 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2546 -- The caller has checked that the initial token is DIGITS
2548 function P_Digits_Constraint return Node_Id is
2549 Constraint_Node : Node_Id;
2550 Expr_Node : Node_Id;
2552 begin
2553 Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
2554 Scan; -- past DIGITS
2555 Expr_Node := P_Expression;
2556 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2557 Set_Digits_Expression (Constraint_Node, Expr_Node);
2559 if Token = Tok_Range then
2560 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2561 end if;
2563 return Constraint_Node;
2564 end P_Digits_Constraint;
2566 -----------------------------
2567 -- 3.5.9 Delta Constraint --
2568 -----------------------------
2570 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2572 -- Note: this is an obsolescent feature in Ada 95 (I.3)
2574 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2575 -- (also true in formal modes).
2577 -- The caller has checked that the initial token is DELTA
2579 -- Error recovery: cannot raise Error_Resync
2581 function P_Delta_Constraint return Node_Id is
2582 Constraint_Node : Node_Id;
2583 Expr_Node : Node_Id;
2585 begin
2586 Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2587 Scan; -- past DELTA
2588 Expr_Node := P_Expression;
2589 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2591 Set_Delta_Expression (Constraint_Node, Expr_Node);
2593 if Token = Tok_Range then
2594 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2595 end if;
2597 return Constraint_Node;
2598 end P_Delta_Constraint;
2600 --------------------------------
2601 -- 3.6 Array Type Definition --
2602 --------------------------------
2604 -- ARRAY_TYPE_DEFINITION ::=
2605 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2607 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2608 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2609 -- COMPONENT_DEFINITION
2611 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2613 -- CONSTRAINED_ARRAY_DEFINITION ::=
2614 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2615 -- COMPONENT_DEFINITION
2617 -- DISCRETE_SUBTYPE_DEFINITION ::=
2618 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2620 -- COMPONENT_DEFINITION ::=
2621 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
2623 -- The caller has checked that the initial token is ARRAY
2625 -- Error recovery: can raise Error_Resync
2627 function P_Array_Type_Definition return Node_Id is
2628 Array_Loc : Source_Ptr;
2629 CompDef_Node : Node_Id;
2630 Def_Node : Node_Id;
2631 Not_Null_Present : Boolean := False;
2632 Subs_List : List_Id;
2633 Scan_State : Saved_Scan_State;
2634 Aliased_Present : Boolean := False;
2636 begin
2637 Array_Loc := Token_Ptr;
2638 Scan; -- past ARRAY
2639 Subs_List := New_List;
2640 T_Left_Paren;
2642 -- It's quite tricky to disentangle these two possibilities, so we do
2643 -- a prescan to determine which case we have and then reset the scan.
2644 -- The prescan skips past possible subtype mark tokens.
2646 Save_Scan_State (Scan_State); -- just after paren
2648 while Token in Token_Class_Desig or else
2649 Token = Tok_Dot or else
2650 Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2651 loop
2652 Scan;
2653 end loop;
2655 -- If we end up on RANGE <> then we have the unconstrained case. We
2656 -- will also allow the RANGE to be omitted, just to improve error
2657 -- handling for a case like array (integer <>) of integer;
2659 Scan; -- past possible RANGE or <>
2661 if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2662 Prev_Token = Tok_Box
2663 then
2664 Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2665 Restore_Scan_State (Scan_State); -- to first subtype mark
2667 loop
2668 Append (P_Subtype_Mark_Resync, Subs_List);
2669 T_Range;
2670 T_Box;
2671 exit when Token = Tok_Right_Paren or else Token = Tok_Of;
2672 T_Comma;
2673 end loop;
2675 Set_Subtype_Marks (Def_Node, Subs_List);
2677 else
2678 Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2679 Restore_Scan_State (Scan_State); -- to first discrete range
2681 loop
2682 Append (P_Discrete_Subtype_Definition, Subs_List);
2683 exit when not Comma_Present;
2684 end loop;
2686 Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2687 end if;
2689 T_Right_Paren;
2690 T_Of;
2692 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2694 if Token_Name = Name_Aliased then
2695 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2696 end if;
2698 if Token = Tok_Aliased then
2699 Aliased_Present := True;
2700 Scan; -- past ALIASED
2701 end if;
2703 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
2705 -- Ada 2005 (AI-230): Access Definition case
2707 if Token = Tok_Access then
2708 if Ada_Version < Ada_2005 then
2709 Error_Msg_SP
2710 ("generalized use of anonymous access types " &
2711 "is an Ada 2005 extension");
2712 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
2713 end if;
2715 -- AI95-406 makes "aliased" legal (and useless) in this context so
2716 -- followintg code which used to be needed is commented out.
2718 -- if Aliased_Present then
2719 -- Error_Msg_SP ("ALIASED not allowed here");
2720 -- end if;
2722 Set_Subtype_Indication (CompDef_Node, Empty);
2723 Set_Aliased_Present (CompDef_Node, False);
2724 Set_Access_Definition (CompDef_Node,
2725 P_Access_Definition (Not_Null_Present));
2726 else
2728 Set_Access_Definition (CompDef_Node, Empty);
2729 Set_Aliased_Present (CompDef_Node, Aliased_Present);
2730 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
2731 Set_Subtype_Indication (CompDef_Node,
2732 P_Subtype_Indication (Not_Null_Present));
2733 end if;
2735 Set_Component_Definition (Def_Node, CompDef_Node);
2737 return Def_Node;
2738 end P_Array_Type_Definition;
2740 -----------------------------------------
2741 -- 3.6 Unconstrained Array Definition --
2742 -----------------------------------------
2744 -- Parsed by P_Array_Type_Definition (3.6)
2746 ---------------------------------------
2747 -- 3.6 Constrained Array Definition --
2748 ---------------------------------------
2750 -- Parsed by P_Array_Type_Definition (3.6)
2752 --------------------------------------
2753 -- 3.6 Discrete Subtype Definition --
2754 --------------------------------------
2756 -- DISCRETE_SUBTYPE_DEFINITION ::=
2757 -- discrete_SUBTYPE_INDICATION | RANGE
2759 -- Note: the discrete subtype definition appearing in a constrained
2760 -- array definition is parsed by P_Array_Type_Definition (3.6)
2762 -- Error recovery: cannot raise Error_Resync
2764 function P_Discrete_Subtype_Definition return Node_Id is
2765 begin
2766 -- The syntax of a discrete subtype definition is identical to that
2767 -- of a discrete range, so we simply share the same parsing code.
2769 return P_Discrete_Range;
2770 end P_Discrete_Subtype_Definition;
2772 -------------------------------
2773 -- 3.6 Component Definition --
2774 -------------------------------
2776 -- For the array case, parsed by P_Array_Type_Definition (3.6)
2777 -- For the record case, parsed by P_Component_Declaration (3.8)
2779 -----------------------------
2780 -- 3.6.1 Index Constraint --
2781 -----------------------------
2783 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2785 ---------------------------
2786 -- 3.6.1 Discrete Range --
2787 ---------------------------
2789 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2791 -- The possible forms for a discrete range are:
2793 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
2794 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
2795 -- Range_Attribute (RANGE, 3.5)
2796 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
2798 -- Error recovery: cannot raise Error_Resync
2800 function P_Discrete_Range return Node_Id is
2801 Expr_Node : Node_Id;
2802 Range_Node : Node_Id;
2804 begin
2805 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2807 if Expr_Form = EF_Range_Attr then
2808 return Expr_Node;
2810 elsif Token = Tok_Range then
2811 if Expr_Form /= EF_Simple_Name then
2812 Error_Msg_SC ("range must be preceded by subtype mark");
2813 end if;
2815 return P_Subtype_Indication (Expr_Node);
2817 -- Check Expression .. Expression case
2819 elsif Token = Tok_Dot_Dot then
2820 Range_Node := New_Node (N_Range, Token_Ptr);
2821 Set_Low_Bound (Range_Node, Expr_Node);
2822 Scan; -- past ..
2823 Expr_Node := P_Expression;
2824 Check_Simple_Expression (Expr_Node);
2825 Set_High_Bound (Range_Node, Expr_Node);
2826 return Range_Node;
2828 -- Otherwise we must have a subtype mark, or an Ada 2012 iterator
2830 elsif Expr_Form = EF_Simple_Name then
2831 return Expr_Node;
2833 -- The domain of iteration must be a name. Semantics will determine that
2834 -- the expression has the proper form.
2836 elsif Ada_Version >= Ada_2012 then
2837 return Expr_Node;
2839 -- If incorrect, complain that we expect ..
2841 else
2842 T_Dot_Dot;
2843 return Expr_Node;
2844 end if;
2845 end P_Discrete_Range;
2847 ----------------------------
2848 -- 3.7 Discriminant Part --
2849 ----------------------------
2851 -- DISCRIMINANT_PART ::=
2852 -- UNKNOWN_DISCRIMINANT_PART
2853 -- | KNOWN_DISCRIMINANT_PART
2855 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2856 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2858 ------------------------------------
2859 -- 3.7 Unknown Discriminant Part --
2860 ------------------------------------
2862 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
2864 -- If no unknown discriminant part is present, then False is returned,
2865 -- otherwise the unknown discriminant is scanned out and True is returned.
2867 -- Error recovery: cannot raise Error_Resync
2869 function P_Unknown_Discriminant_Part_Opt return Boolean is
2870 Scan_State : Saved_Scan_State;
2872 begin
2873 -- If <> right now, then this is missing left paren
2875 if Token = Tok_Box then
2876 U_Left_Paren;
2878 -- If not <> or left paren, then definitely no box
2880 elsif Token /= Tok_Left_Paren then
2881 return False;
2883 -- Left paren, so might be a box after it
2885 else
2886 Save_Scan_State (Scan_State);
2887 Scan; -- past the left paren
2889 if Token /= Tok_Box then
2890 Restore_Scan_State (Scan_State);
2891 return False;
2892 end if;
2893 end if;
2895 -- We are now pointing to the box
2897 if Ada_Version = Ada_83 then
2898 Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
2899 end if;
2901 Scan; -- past the box
2902 U_Right_Paren; -- must be followed by right paren
2903 return True;
2904 end P_Unknown_Discriminant_Part_Opt;
2906 ----------------------------------
2907 -- 3.7 Known Discriminant Part --
2908 ----------------------------------
2910 -- KNOWN_DISCRIMINANT_PART ::=
2911 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2913 -- DISCRIMINANT_SPECIFICATION ::=
2914 -- DEFINING_IDENTIFIER_LIST : [NULL_EXCLUSION] SUBTYPE_MARK
2915 -- [:= DEFAULT_EXPRESSION]
2916 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2917 -- [:= DEFAULT_EXPRESSION]
2919 -- If no known discriminant part is present, then No_List is returned
2921 -- Error recovery: cannot raise Error_Resync
2923 function P_Known_Discriminant_Part_Opt return List_Id is
2924 Specification_Node : Node_Id;
2925 Specification_List : List_Id;
2926 Ident_Sloc : Source_Ptr;
2927 Scan_State : Saved_Scan_State;
2928 Num_Idents : Nat;
2929 Not_Null_Present : Boolean;
2930 Ident : Nat;
2932 Idents : array (Int range 1 .. 4096) of Entity_Id;
2933 -- This array holds the list of defining identifiers. The upper bound
2934 -- of 4096 is intended to be essentially infinite, and we do not even
2935 -- bother to check for it being exceeded.
2937 begin
2938 if Token = Tok_Left_Paren then
2939 Specification_List := New_List;
2940 Scan; -- past (
2941 P_Pragmas_Misplaced;
2943 Specification_Loop : loop
2945 Ident_Sloc := Token_Ptr;
2946 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2947 Num_Idents := 1;
2949 while Comma_Present loop
2950 Num_Idents := Num_Idents + 1;
2951 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2952 end loop;
2954 -- If there are multiple identifiers, we repeatedly scan the
2955 -- type and initialization expression information by resetting
2956 -- the scan pointer (so that we get completely separate trees
2957 -- for each occurrence).
2959 if Num_Idents > 1 then
2960 Save_Scan_State (Scan_State);
2961 end if;
2963 T_Colon;
2965 -- Loop through defining identifiers in list
2967 Ident := 1;
2968 Ident_Loop : loop
2969 Specification_Node :=
2970 New_Node (N_Discriminant_Specification, Ident_Sloc);
2971 Set_Defining_Identifier (Specification_Node, Idents (Ident));
2972 Not_Null_Present := -- Ada 2005 (AI-231, AI-447)
2973 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
2975 if Token = Tok_Access then
2976 if Ada_Version = Ada_83 then
2977 Error_Msg_SC
2978 ("(Ada 83) access discriminant not allowed!");
2979 end if;
2981 Set_Discriminant_Type
2982 (Specification_Node,
2983 P_Access_Definition (Not_Null_Present));
2984 else
2986 Set_Discriminant_Type
2987 (Specification_Node, P_Subtype_Mark);
2988 No_Constraint;
2989 Set_Null_Exclusion_Present -- Ada 2005 (AI-231)
2990 (Specification_Node, Not_Null_Present);
2991 end if;
2993 Set_Expression
2994 (Specification_Node, Init_Expr_Opt (True));
2996 if Ident > 1 then
2997 Set_Prev_Ids (Specification_Node, True);
2998 end if;
3000 if Ident < Num_Idents then
3001 Set_More_Ids (Specification_Node, True);
3002 end if;
3004 Append (Specification_Node, Specification_List);
3005 exit Ident_Loop when Ident = Num_Idents;
3006 Ident := Ident + 1;
3007 Restore_Scan_State (Scan_State);
3008 T_Colon;
3009 end loop Ident_Loop;
3011 exit Specification_Loop when Token /= Tok_Semicolon;
3012 Scan; -- past ;
3013 P_Pragmas_Misplaced;
3014 end loop Specification_Loop;
3016 T_Right_Paren;
3017 return Specification_List;
3019 else
3020 return No_List;
3021 end if;
3022 end P_Known_Discriminant_Part_Opt;
3024 -------------------------------------
3025 -- 3.7 Discriminant Specification --
3026 -------------------------------------
3028 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
3030 -----------------------------
3031 -- 3.7 Default Expression --
3032 -----------------------------
3034 -- Always parsed (simply as an Expression) by the parent construct
3036 ------------------------------------
3037 -- 3.7.1 Discriminant Constraint --
3038 ------------------------------------
3040 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
3042 --------------------------------------------------------
3043 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
3044 --------------------------------------------------------
3046 -- DISCRIMINANT_CONSTRAINT ::=
3047 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
3049 -- DISCRIMINANT_ASSOCIATION ::=
3050 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3051 -- EXPRESSION
3053 -- This routine parses either an index or a discriminant constraint. As
3054 -- is clear from the above grammar, it is often possible to clearly
3055 -- determine which of the two possibilities we have, but there are
3056 -- cases (those in which we have a series of expressions of the same
3057 -- syntactic form as subtype indications), where we cannot tell. Since
3058 -- this means that in any case the semantic phase has to distinguish
3059 -- between the two, there is not much point in the parser trying to
3060 -- distinguish even those cases where the difference is clear. In any
3061 -- case, if we have a situation like:
3063 -- (A => 123, 235 .. 500)
3065 -- it is not clear which of the two items is the wrong one, better to
3066 -- let the semantic phase give a clear message. Consequently, this
3067 -- routine in general returns a list of items which can be either
3068 -- discrete ranges or discriminant associations.
3070 -- The caller has checked that the initial token is a left paren
3072 -- Error recovery: can raise Error_Resync
3074 function P_Index_Or_Discriminant_Constraint return Node_Id is
3075 Scan_State : Saved_Scan_State;
3076 Constr_Node : Node_Id;
3077 Constr_List : List_Id;
3078 Expr_Node : Node_Id;
3079 Result_Node : Node_Id;
3081 begin
3082 Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
3083 Scan; -- past (
3084 Constr_List := New_List;
3085 Set_Constraints (Result_Node, Constr_List);
3087 -- The two syntactic forms are a little mixed up, so what we are doing
3088 -- here is looking at the first entry to determine which case we have
3090 -- A discriminant constraint is a list of discriminant associations,
3091 -- which have one of the following possible forms:
3093 -- Expression
3094 -- Id => Expression
3095 -- Id | Id | .. | Id => Expression
3097 -- An index constraint is a list of discrete ranges which have one
3098 -- of the following possible forms:
3100 -- Subtype_Mark
3101 -- Subtype_Mark range Range
3102 -- Range_Attribute
3103 -- Simple_Expression .. Simple_Expression
3105 -- Loop through discriminants in list
3107 loop
3108 -- Check cases of Id => Expression or Id | Id => Expression
3110 if Token = Tok_Identifier then
3111 Save_Scan_State (Scan_State); -- at Id
3112 Scan; -- past Id
3114 if Token = Tok_Arrow or else Token = Tok_Vertical_Bar then
3115 Restore_Scan_State (Scan_State); -- to Id
3116 Append (P_Discriminant_Association, Constr_List);
3117 goto Loop_Continue;
3118 else
3119 Restore_Scan_State (Scan_State); -- to Id
3120 end if;
3121 end if;
3123 -- Otherwise scan out an expression and see what we have got
3125 Expr_Node := P_Expression_Or_Range_Attribute;
3127 if Expr_Form = EF_Range_Attr then
3128 Append (Expr_Node, Constr_List);
3130 elsif Token = Tok_Range then
3131 if Expr_Form /= EF_Simple_Name then
3132 Error_Msg_SC ("subtype mark required before RANGE");
3133 end if;
3135 Append (P_Subtype_Indication (Expr_Node), Constr_List);
3136 goto Loop_Continue;
3138 -- Check Simple_Expression .. Simple_Expression case
3140 elsif Token = Tok_Dot_Dot then
3141 Check_Simple_Expression (Expr_Node);
3142 Constr_Node := New_Node (N_Range, Token_Ptr);
3143 Set_Low_Bound (Constr_Node, Expr_Node);
3144 Scan; -- past ..
3145 Expr_Node := P_Expression;
3146 Check_Simple_Expression (Expr_Node);
3147 Set_High_Bound (Constr_Node, Expr_Node);
3148 Append (Constr_Node, Constr_List);
3149 goto Loop_Continue;
3151 -- Case of an expression which could be either form
3153 else
3154 Append (Expr_Node, Constr_List);
3155 goto Loop_Continue;
3156 end if;
3158 -- Here with a single entry scanned
3160 <<Loop_Continue>>
3161 exit when not Comma_Present;
3163 end loop;
3165 T_Right_Paren;
3166 return Result_Node;
3167 end P_Index_Or_Discriminant_Constraint;
3169 -------------------------------------
3170 -- 3.7.1 Discriminant Association --
3171 -------------------------------------
3173 -- DISCRIMINANT_ASSOCIATION ::=
3174 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3175 -- EXPRESSION
3177 -- This routine is used only when the name list is present and the caller
3178 -- has already checked this (by scanning ahead and repositioning the
3179 -- scan).
3181 -- Error_Recovery: cannot raise Error_Resync;
3183 function P_Discriminant_Association return Node_Id is
3184 Discr_Node : Node_Id;
3185 Names_List : List_Id;
3186 Ident_Sloc : Source_Ptr;
3188 begin
3189 Ident_Sloc := Token_Ptr;
3190 Names_List := New_List;
3192 loop
3193 Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
3194 exit when Token /= Tok_Vertical_Bar;
3195 Scan; -- past |
3196 end loop;
3198 Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
3199 Set_Selector_Names (Discr_Node, Names_List);
3200 TF_Arrow;
3201 Set_Expression (Discr_Node, P_Expression);
3202 return Discr_Node;
3203 end P_Discriminant_Association;
3205 ---------------------------------
3206 -- 3.8 Record Type Definition --
3207 ---------------------------------
3209 -- RECORD_TYPE_DEFINITION ::=
3210 -- [[abstract] tagged] [limited] RECORD_DEFINITION
3212 -- There is no node in the tree for a record type definition. Instead
3213 -- a record definition node appears, with possible Abstract_Present,
3214 -- Tagged_Present, and Limited_Present flags set appropriately.
3216 ----------------------------
3217 -- 3.8 Record Definition --
3218 ----------------------------
3220 -- RECORD_DEFINITION ::=
3221 -- record
3222 -- COMPONENT_LIST
3223 -- end record
3224 -- | null record
3226 -- Note: in the case where a record definition node is used to represent
3227 -- a record type definition, the caller sets the Tagged_Present and
3228 -- Limited_Present flags in the resulting N_Record_Definition node as
3229 -- required.
3231 -- Note that the RECORD token at the start may be missing in certain
3232 -- error situations, so this function is expected to post the error
3234 -- Error recovery: can raise Error_Resync
3236 function P_Record_Definition return Node_Id is
3237 Rec_Node : Node_Id;
3239 begin
3240 Inside_Record_Definition := True;
3241 Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
3243 -- Null record case
3245 if Token = Tok_Null then
3246 Scan; -- past NULL
3247 T_Record;
3248 Set_Null_Present (Rec_Node, True);
3250 -- Catch incomplete declaration to prevent cascaded errors, see
3251 -- ACATS B393002 for an example.
3253 elsif Token = Tok_Semicolon then
3254 Error_Msg_AP ("missing record definition");
3256 -- Case starting with RECORD keyword. Build scope stack entry. For the
3257 -- column, we use the first non-blank character on the line, to deal
3258 -- with situations such as:
3260 -- type X is record
3261 -- ...
3262 -- end record;
3264 -- which is not official RM indentation, but is not uncommon usage, and
3265 -- in particular is standard GNAT coding style, so handle it nicely.
3267 else
3268 Push_Scope_Stack;
3269 Scope.Table (Scope.Last).Etyp := E_Record;
3270 Scope.Table (Scope.Last).Ecol := Start_Column;
3271 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3272 Scope.Table (Scope.Last).Labl := Error;
3273 Scope.Table (Scope.Last).Junk := (Token /= Tok_Record);
3275 T_Record;
3277 Set_Component_List (Rec_Node, P_Component_List);
3279 loop
3280 exit when Check_End;
3281 Discard_Junk_Node (P_Component_List);
3282 end loop;
3283 end if;
3285 Inside_Record_Definition := False;
3286 return Rec_Node;
3287 end P_Record_Definition;
3289 -------------------------
3290 -- 3.8 Component List --
3291 -------------------------
3293 -- COMPONENT_LIST ::=
3294 -- COMPONENT_ITEM {COMPONENT_ITEM}
3295 -- | {COMPONENT_ITEM} VARIANT_PART
3296 -- | null;
3298 -- Error recovery: cannot raise Error_Resync
3300 function P_Component_List return Node_Id is
3301 Component_List_Node : Node_Id;
3302 Decls_List : List_Id;
3303 Scan_State : Saved_Scan_State;
3304 Null_Loc : Source_Ptr;
3306 begin
3307 Component_List_Node := New_Node (N_Component_List, Token_Ptr);
3308 Decls_List := New_List;
3310 -- Handle null
3312 if Token = Tok_Null then
3313 Null_Loc := Token_Ptr;
3314 Scan; -- past NULL
3315 TF_Semicolon;
3316 P_Pragmas_Opt (Decls_List);
3318 -- If we have an END or WHEN now, everything is fine, otherwise we
3319 -- complain about the null, ignore it, and scan for more components.
3321 if Token = Tok_End or else Token = Tok_When then
3322 Set_Null_Present (Component_List_Node, True);
3323 return Component_List_Node;
3324 else
3325 Error_Msg ("NULL component only allowed in null record", Null_Loc);
3326 end if;
3327 end if;
3329 -- Scan components for non-null record
3331 P_Pragmas_Opt (Decls_List);
3333 if Token /= Tok_Case then
3334 Component_Scan_Loop : loop
3335 P_Component_Items (Decls_List);
3336 P_Pragmas_Opt (Decls_List);
3338 exit Component_Scan_Loop when Token = Tok_End
3339 or else Token = Tok_Case
3340 or else Token = Tok_When;
3342 -- We are done if we do not have an identifier. However, if we
3343 -- have a misspelled reserved identifier that is in a column to
3344 -- the right of the record definition, we will treat it as an
3345 -- identifier. It turns out to be too dangerous in practice to
3346 -- accept such a mis-spelled identifier which does not have this
3347 -- additional clue that confirms the incorrect spelling.
3349 if Token /= Tok_Identifier then
3350 if Start_Column > Scope.Table (Scope.Last).Ecol
3351 and then Is_Reserved_Identifier
3352 then
3353 Save_Scan_State (Scan_State); -- at reserved id
3354 Scan; -- possible reserved id
3356 if Token = Tok_Comma or else Token = Tok_Colon then
3357 Restore_Scan_State (Scan_State);
3358 Scan_Reserved_Identifier (Force_Msg => True);
3360 -- Note reserved identifier used as field name after all
3361 -- because not followed by colon or comma.
3363 else
3364 Restore_Scan_State (Scan_State);
3365 exit Component_Scan_Loop;
3366 end if;
3368 -- Non-identifier that definitely was not reserved id
3370 else
3371 exit Component_Scan_Loop;
3372 end if;
3373 end if;
3374 end loop Component_Scan_Loop;
3375 end if;
3377 if Token = Tok_Case then
3378 Set_Variant_Part (Component_List_Node, P_Variant_Part);
3380 -- Check for junk after variant part
3382 if Token = Tok_Identifier then
3383 Save_Scan_State (Scan_State);
3384 Scan; -- past identifier
3386 if Token = Tok_Colon then
3387 Restore_Scan_State (Scan_State);
3388 Error_Msg_SC ("component may not follow variant part");
3389 Discard_Junk_Node (P_Component_List);
3391 elsif Token = Tok_Case then
3392 Restore_Scan_State (Scan_State);
3393 Error_Msg_SC ("only one variant part allowed in a record");
3394 Discard_Junk_Node (P_Component_List);
3396 else
3397 Restore_Scan_State (Scan_State);
3398 end if;
3399 end if;
3400 end if;
3402 Set_Component_Items (Component_List_Node, Decls_List);
3403 return Component_List_Node;
3404 end P_Component_List;
3406 -------------------------
3407 -- 3.8 Component Item --
3408 -------------------------
3410 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
3412 -- COMPONENT_DECLARATION ::=
3413 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
3414 -- [:= DEFAULT_EXPRESSION]
3415 -- [ASPECT_SPECIFICATIONS];
3417 -- COMPONENT_DEFINITION ::=
3418 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
3420 -- Error recovery: cannot raise Error_Resync, if an error occurs,
3421 -- the scan is positioned past the following semicolon.
3423 -- Note: we do not yet allow representation clauses to appear as component
3424 -- items, do we need to add this capability sometime in the future ???
3426 procedure P_Component_Items (Decls : List_Id) is
3427 Aliased_Present : Boolean := False;
3428 CompDef_Node : Node_Id;
3429 Decl_Node : Node_Id;
3430 Scan_State : Saved_Scan_State;
3431 Not_Null_Present : Boolean := False;
3432 Num_Idents : Nat;
3433 Ident : Nat;
3434 Ident_Sloc : Source_Ptr;
3436 Idents : array (Int range 1 .. 4096) of Entity_Id;
3437 -- This array holds the list of defining identifiers. The upper bound
3438 -- of 4096 is intended to be essentially infinite, and we do not even
3439 -- bother to check for it being exceeded.
3441 begin
3442 if Token /= Tok_Identifier then
3443 Error_Msg_SC ("component declaration expected");
3444 Resync_Past_Semicolon;
3445 return;
3446 end if;
3448 Ident_Sloc := Token_Ptr;
3449 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3450 Num_Idents := 1;
3452 while Comma_Present loop
3453 Num_Idents := Num_Idents + 1;
3454 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3455 end loop;
3457 -- If there are multiple identifiers, we repeatedly scan the
3458 -- type and initialization expression information by resetting
3459 -- the scan pointer (so that we get completely separate trees
3460 -- for each occurrence).
3462 if Num_Idents > 1 then
3463 Save_Scan_State (Scan_State);
3464 end if;
3466 T_Colon;
3468 -- Loop through defining identifiers in list
3470 Ident := 1;
3471 Ident_Loop : loop
3473 -- The following block is present to catch Error_Resync
3474 -- which causes the parse to be reset past the semicolon
3476 begin
3477 Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
3478 Set_Defining_Identifier (Decl_Node, Idents (Ident));
3480 if Token = Tok_Constant then
3481 Error_Msg_SC ("constant components are not permitted");
3482 Scan;
3483 end if;
3485 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
3487 if Token_Name = Name_Aliased then
3488 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
3489 end if;
3491 if Token = Tok_Aliased then
3492 Aliased_Present := True;
3493 Scan; -- past ALIASED
3494 end if;
3496 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
3498 -- Ada 2005 (AI-230): Access Definition case
3500 if Token = Tok_Access then
3501 if Ada_Version < Ada_2005 then
3502 Error_Msg_SP
3503 ("generalized use of anonymous access types " &
3504 "is an Ada 2005 extension");
3505 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3506 end if;
3508 -- AI95-406 makes "aliased" legal (and useless) here, so the
3509 -- following code which used to be required is commented out.
3511 -- if Aliased_Present then
3512 -- Error_Msg_SP ("ALIASED not allowed here");
3513 -- end if;
3515 Set_Subtype_Indication (CompDef_Node, Empty);
3516 Set_Aliased_Present (CompDef_Node, False);
3517 Set_Access_Definition (CompDef_Node,
3518 P_Access_Definition (Not_Null_Present));
3519 else
3521 Set_Access_Definition (CompDef_Node, Empty);
3522 Set_Aliased_Present (CompDef_Node, Aliased_Present);
3523 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
3525 if Token = Tok_Array then
3526 Error_Msg_SC ("anonymous arrays not allowed as components");
3527 raise Error_Resync;
3528 end if;
3530 Set_Subtype_Indication (CompDef_Node,
3531 P_Subtype_Indication (Not_Null_Present));
3532 end if;
3534 Set_Component_Definition (Decl_Node, CompDef_Node);
3535 Set_Expression (Decl_Node, Init_Expr_Opt);
3537 if Ident > 1 then
3538 Set_Prev_Ids (Decl_Node, True);
3539 end if;
3541 if Ident < Num_Idents then
3542 Set_More_Ids (Decl_Node, True);
3543 end if;
3545 Append (Decl_Node, Decls);
3547 exception
3548 when Error_Resync =>
3549 if Token /= Tok_End then
3550 Resync_Past_Semicolon;
3551 end if;
3552 end;
3554 exit Ident_Loop when Ident = Num_Idents;
3555 Ident := Ident + 1;
3556 Restore_Scan_State (Scan_State);
3557 T_Colon;
3558 end loop Ident_Loop;
3560 P_Aspect_Specifications (Decl_Node);
3561 end P_Component_Items;
3563 --------------------------------
3564 -- 3.8 Component Declaration --
3565 --------------------------------
3567 -- Parsed by P_Component_Items (3.8)
3569 -------------------------
3570 -- 3.8.1 Variant Part --
3571 -------------------------
3573 -- VARIANT_PART ::=
3574 -- case discriminant_DIRECT_NAME is
3575 -- VARIANT
3576 -- {VARIANT}
3577 -- end case;
3579 -- The caller has checked that the initial token is CASE
3581 -- Error recovery: cannot raise Error_Resync
3583 function P_Variant_Part return Node_Id is
3584 Variant_Part_Node : Node_Id;
3585 Variants_List : List_Id;
3586 Case_Node : Node_Id;
3588 begin
3589 Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
3590 Push_Scope_Stack;
3591 Scope.Table (Scope.Last).Etyp := E_Case;
3592 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3593 Scope.Table (Scope.Last).Ecol := Start_Column;
3595 Scan; -- past CASE
3596 Case_Node := P_Expression;
3597 Set_Name (Variant_Part_Node, Case_Node);
3599 if Nkind (Case_Node) /= N_Identifier then
3600 Set_Name (Variant_Part_Node, Error);
3601 Error_Msg ("discriminant name expected", Sloc (Case_Node));
3603 elsif Paren_Count (Case_Node) /= 0 then
3604 Error_Msg
3605 ("|discriminant name may not be parenthesized",
3606 Sloc (Case_Node));
3607 Set_Paren_Count (Case_Node, 0);
3608 end if;
3610 TF_Is;
3611 Variants_List := New_List;
3612 P_Pragmas_Opt (Variants_List);
3614 -- Test missing variant
3616 if Token = Tok_End then
3617 Error_Msg_BC ("WHEN expected (must have at least one variant)");
3618 else
3619 Append (P_Variant, Variants_List);
3620 end if;
3622 -- Loop through variants, note that we allow if in place of when,
3623 -- this error will be detected and handled in P_Variant.
3625 loop
3626 P_Pragmas_Opt (Variants_List);
3628 if Token /= Tok_When
3629 and then Token /= Tok_If
3630 and then Token /= Tok_Others
3631 then
3632 exit when Check_End;
3633 end if;
3635 Append (P_Variant, Variants_List);
3636 end loop;
3638 Set_Variants (Variant_Part_Node, Variants_List);
3639 return Variant_Part_Node;
3640 end P_Variant_Part;
3642 --------------------
3643 -- 3.8.1 Variant --
3644 --------------------
3646 -- VARIANT ::=
3647 -- when DISCRETE_CHOICE_LIST =>
3648 -- COMPONENT_LIST
3650 -- Error recovery: cannot raise Error_Resync
3652 -- The initial token on entry is either WHEN, IF or OTHERS
3654 function P_Variant return Node_Id is
3655 Variant_Node : Node_Id;
3657 begin
3658 -- Special check to recover nicely from use of IF in place of WHEN
3660 if Token = Tok_If then
3661 T_When;
3662 Scan; -- past IF
3663 else
3664 T_When;
3665 end if;
3667 Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3668 Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3669 TF_Arrow;
3670 Set_Component_List (Variant_Node, P_Component_List);
3671 return Variant_Node;
3672 end P_Variant;
3674 ---------------------------------
3675 -- 3.8.1 Discrete Choice List --
3676 ---------------------------------
3678 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
3680 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
3682 -- Note: in Ada 83, the expression must be a simple expression
3684 -- Error recovery: cannot raise Error_Resync
3686 function P_Discrete_Choice_List return List_Id is
3687 Choices : List_Id;
3688 Expr_Node : Node_Id;
3689 Choice_Node : Node_Id;
3691 begin
3692 Choices := New_List;
3693 loop
3694 if Token = Tok_Others then
3695 Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
3696 Scan; -- past OTHERS
3698 else
3699 begin
3700 -- Scan out expression or range attribute
3702 Expr_Node := P_Expression_Or_Range_Attribute;
3703 Ignore (Tok_Right_Paren);
3705 if Token = Tok_Colon
3706 and then Nkind (Expr_Node) = N_Identifier
3707 then
3708 Error_Msg_SP ("label not permitted in this context");
3709 Scan; -- past colon
3711 -- Range attribute
3713 elsif Expr_Form = EF_Range_Attr then
3714 Append (Expr_Node, Choices);
3716 -- Explicit range
3718 elsif Token = Tok_Dot_Dot then
3719 Check_Simple_Expression (Expr_Node);
3720 Choice_Node := New_Node (N_Range, Token_Ptr);
3721 Set_Low_Bound (Choice_Node, Expr_Node);
3722 Scan; -- past ..
3723 Expr_Node := P_Expression_No_Right_Paren;
3724 Check_Simple_Expression (Expr_Node);
3725 Set_High_Bound (Choice_Node, Expr_Node);
3726 Append (Choice_Node, Choices);
3728 -- Simple name, must be subtype, so range allowed
3730 elsif Expr_Form = EF_Simple_Name then
3731 if Token = Tok_Range then
3732 Append (P_Subtype_Indication (Expr_Node), Choices);
3734 elsif Token in Token_Class_Consk then
3735 Error_Msg_SC
3736 ("the only constraint allowed here " &
3737 "is a range constraint");
3738 Discard_Junk_Node (P_Constraint_Opt);
3739 Append (Expr_Node, Choices);
3741 else
3742 Append (Expr_Node, Choices);
3743 end if;
3745 -- Expression
3747 else
3748 -- In Ada 2012 mode, the expression must be a simple
3749 -- expression. The reason for this restriction (i.e. going
3750 -- back to the Ada 83 rule) is to avoid ambiguities when set
3751 -- membership operations are allowed, consider the
3752 -- following:
3754 -- when A in 1 .. 10 | 12 =>
3756 -- This is ambiguous without parentheses, so we require one
3757 -- of the following two parenthesized forms to disambiguate:
3759 -- one of the following:
3761 -- when (A in 1 .. 10 | 12) =>
3762 -- when (A in 1 .. 10) | 12 =>
3764 -- To solve this, in Ada 2012 mode, we disallow the use of
3765 -- membership operations in expressions in choices.
3767 -- Technically in the grammar, the expression must match the
3768 -- grammar for restricted expression.
3770 if Ada_Version >= Ada_2012 then
3771 Check_Restricted_Expression (Expr_Node);
3773 -- In Ada 83 mode, the syntax required a simple expression
3775 else
3776 Check_Simple_Expression_In_Ada_83 (Expr_Node);
3777 end if;
3779 Append (Expr_Node, Choices);
3780 end if;
3782 exception
3783 when Error_Resync =>
3784 Resync_Choice;
3785 return Error_List;
3786 end;
3787 end if;
3789 if Token = Tok_Comma then
3790 Scan; -- past comma
3792 if Token = Tok_Vertical_Bar then
3793 Error_Msg_SP -- CODEFIX
3794 ("|extra "","" ignored");
3795 Scan; -- past |
3797 else
3798 Error_Msg_SP -- CODEFIX
3799 (""","" should be ""'|""");
3800 end if;
3802 else
3803 exit when Token /= Tok_Vertical_Bar;
3804 Scan; -- past |
3805 end if;
3807 end loop;
3809 return Choices;
3810 end P_Discrete_Choice_List;
3812 ----------------------------
3813 -- 3.8.1 Discrete Choice --
3814 ----------------------------
3816 -- Parsed by P_Discrete_Choice_List (3.8.1)
3818 ----------------------------------
3819 -- 3.9.1 Record Extension Part --
3820 ----------------------------------
3822 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3824 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3826 --------------------------------------
3827 -- 3.9.4 Interface Type Definition --
3828 --------------------------------------
3830 -- INTERFACE_TYPE_DEFINITION ::=
3831 -- [limited | task | protected | synchronized] interface
3832 -- [and INTERFACE_LIST]
3834 -- Error recovery: cannot raise Error_Resync
3836 function P_Interface_Type_Definition
3837 (Abstract_Present : Boolean) return Node_Id
3839 Typedef_Node : Node_Id;
3841 begin
3842 if Ada_Version < Ada_2005 then
3843 Error_Msg_SP ("abstract interface is an Ada 2005 extension");
3844 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3845 end if;
3847 if Abstract_Present then
3848 Error_Msg_SP
3849 ("ABSTRACT not allowed in interface type definition " &
3850 "(RM 3.9.4(2/2))");
3851 end if;
3853 Scan; -- past INTERFACE
3855 -- Ada 2005 (AI-345): In case of interfaces with a null list of
3856 -- interfaces we build a record_definition node.
3858 if Token = Tok_Semicolon or else Aspect_Specifications_Present then
3859 Typedef_Node := New_Node (N_Record_Definition, Token_Ptr);
3861 Set_Abstract_Present (Typedef_Node);
3862 Set_Tagged_Present (Typedef_Node);
3863 Set_Null_Present (Typedef_Node);
3864 Set_Interface_Present (Typedef_Node);
3866 -- Ada 2005 (AI-251): In case of not-synchronized interfaces that have
3867 -- a list of interfaces we build a derived_type_definition node. This
3868 -- simplifies the semantic analysis (and hence further maintenance)
3870 else
3871 if Token /= Tok_And then
3872 Error_Msg_AP ("AND expected");
3873 else
3874 Scan; -- past AND
3875 end if;
3877 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
3879 Set_Abstract_Present (Typedef_Node);
3880 Set_Interface_Present (Typedef_Node);
3881 Set_Subtype_Indication (Typedef_Node, P_Qualified_Simple_Name);
3883 Set_Record_Extension_Part (Typedef_Node,
3884 New_Node (N_Record_Definition, Token_Ptr));
3885 Set_Null_Present (Record_Extension_Part (Typedef_Node));
3887 if Token = Tok_And then
3888 Set_Interface_List (Typedef_Node, New_List);
3889 Scan; -- past AND
3891 loop
3892 Append (P_Qualified_Simple_Name,
3893 Interface_List (Typedef_Node));
3894 exit when Token /= Tok_And;
3895 Scan; -- past AND
3896 end loop;
3897 end if;
3898 end if;
3900 return Typedef_Node;
3901 end P_Interface_Type_Definition;
3903 ----------------------------------
3904 -- 3.10 Access Type Definition --
3905 ----------------------------------
3907 -- ACCESS_TYPE_DEFINITION ::=
3908 -- ACCESS_TO_OBJECT_DEFINITION
3909 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3911 -- ACCESS_TO_OBJECT_DEFINITION ::=
3912 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3914 -- GENERAL_ACCESS_MODIFIER ::= all | constant
3916 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3917 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3918 -- | [NULL_EXCLUSION] access [protected] function
3919 -- PARAMETER_AND_RESULT_PROFILE
3921 -- PARAMETER_PROFILE ::= [FORMAL_PART]
3923 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3925 -- Ada 2005 (AI-254): If Header_Already_Parsed then the caller has already
3926 -- parsed the null_exclusion part and has also removed the ACCESS token;
3927 -- otherwise the caller has just checked that the initial token is ACCESS
3929 -- Error recovery: can raise Error_Resync
3931 function P_Access_Type_Definition
3932 (Header_Already_Parsed : Boolean := False) return Node_Id
3934 Access_Loc : constant Source_Ptr := Token_Ptr;
3935 Prot_Flag : Boolean;
3936 Not_Null_Present : Boolean := False;
3937 Not_Null_Subtype : Boolean := False;
3938 Type_Def_Node : Node_Id;
3939 Result_Not_Null : Boolean;
3940 Result_Node : Node_Id;
3942 procedure Check_Junk_Subprogram_Name;
3943 -- Used in access to subprogram definition cases to check for an
3944 -- identifier or operator symbol that does not belong.
3946 --------------------------------
3947 -- Check_Junk_Subprogram_Name --
3948 --------------------------------
3950 procedure Check_Junk_Subprogram_Name is
3951 Saved_State : Saved_Scan_State;
3953 begin
3954 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
3955 Save_Scan_State (Saved_State);
3956 Scan; -- past possible junk subprogram name
3958 if Token = Tok_Left_Paren or else Token = Tok_Semicolon then
3959 Error_Msg_SP ("unexpected subprogram name ignored");
3960 return;
3962 else
3963 Restore_Scan_State (Saved_State);
3964 end if;
3965 end if;
3966 end Check_Junk_Subprogram_Name;
3968 -- Start of processing for P_Access_Type_Definition
3970 begin
3971 if not Header_Already_Parsed then
3973 -- NOT NULL ACCESS .. is a common form of access definition.
3974 -- ACCESS NOT NULL .. is certainly rare, but syntactically legal.
3975 -- NOT NULL ACCESS NOT NULL .. is rarer yet, and also legal.
3976 -- The last two cases are only meaningful if the following subtype
3977 -- indication denotes an access type (semantic check). The flag
3978 -- Not_Null_Subtype indicates that this second null exclusion is
3979 -- present in the access type definition.
3981 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
3982 Scan; -- past ACCESS
3983 Not_Null_Subtype := P_Null_Exclusion; -- Might also appear
3984 end if;
3986 if Token_Name = Name_Protected then
3987 Check_95_Keyword (Tok_Protected, Tok_Procedure);
3988 Check_95_Keyword (Tok_Protected, Tok_Function);
3989 end if;
3991 Prot_Flag := (Token = Tok_Protected);
3993 if Prot_Flag then
3994 Scan; -- past PROTECTED
3996 if Token /= Tok_Procedure and then Token /= Tok_Function then
3997 Error_Msg_SC -- CODEFIX
3998 ("FUNCTION or PROCEDURE expected");
3999 end if;
4000 end if;
4002 if Token = Tok_Procedure then
4003 if Ada_Version = Ada_83 then
4004 Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
4005 end if;
4007 Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
4008 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4009 Scan; -- past PROCEDURE
4010 Check_Junk_Subprogram_Name;
4011 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
4012 Set_Protected_Present (Type_Def_Node, Prot_Flag);
4014 elsif Token = Tok_Function then
4015 if Ada_Version = Ada_83 then
4016 Error_Msg_SC ("(Ada 83) access to function not allowed!");
4017 end if;
4019 Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
4020 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4021 Scan; -- past FUNCTION
4022 Check_Junk_Subprogram_Name;
4023 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
4024 Set_Protected_Present (Type_Def_Node, Prot_Flag);
4025 TF_Return;
4027 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
4029 -- Ada 2005 (AI-318-02)
4031 if Token = Tok_Access then
4032 if Ada_Version < Ada_2005 then
4033 Error_Msg_SC
4034 ("anonymous access result type is an Ada 2005 extension");
4035 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
4036 end if;
4038 Result_Node := P_Access_Definition (Result_Not_Null);
4040 else
4041 Result_Node := P_Subtype_Mark;
4042 No_Constraint;
4044 -- A null exclusion on the result type must be recorded in a flag
4045 -- distinct from the one used for the access-to-subprogram type's
4046 -- null exclusion.
4048 Set_Null_Exclusion_In_Return_Present
4049 (Type_Def_Node, Result_Not_Null);
4050 end if;
4052 Set_Result_Definition (Type_Def_Node, Result_Node);
4054 else
4055 Type_Def_Node :=
4056 New_Node (N_Access_To_Object_Definition, Access_Loc);
4057 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
4058 Set_Null_Excluding_Subtype (Type_Def_Node, Not_Null_Subtype);
4060 if Token = Tok_All or else Token = Tok_Constant then
4061 if Ada_Version = Ada_83 then
4062 Error_Msg_SC ("(Ada 83) access modifier not allowed!");
4063 end if;
4065 if Token = Tok_All then
4066 Set_All_Present (Type_Def_Node, True);
4068 else
4069 Set_Constant_Present (Type_Def_Node, True);
4070 end if;
4072 Scan; -- past ALL or CONSTANT
4073 end if;
4075 Set_Subtype_Indication (Type_Def_Node,
4076 P_Subtype_Indication (Not_Null_Present));
4077 end if;
4079 return Type_Def_Node;
4080 end P_Access_Type_Definition;
4082 ---------------------------------------
4083 -- 3.10 Access To Object Definition --
4084 ---------------------------------------
4086 -- Parsed by P_Access_Type_Definition (3.10)
4088 -----------------------------------
4089 -- 3.10 General Access Modifier --
4090 -----------------------------------
4092 -- Parsed by P_Access_Type_Definition (3.10)
4094 -------------------------------------------
4095 -- 3.10 Access To Subprogram Definition --
4096 -------------------------------------------
4098 -- Parsed by P_Access_Type_Definition (3.10)
4100 -----------------------------
4101 -- 3.10 Access Definition --
4102 -----------------------------
4104 -- ACCESS_DEFINITION ::=
4105 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4106 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
4108 -- ACCESS_TO_SUBPROGRAM_DEFINITION
4109 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
4110 -- | [NULL_EXCLUSION] access [protected] function
4111 -- PARAMETER_AND_RESULT_PROFILE
4113 -- The caller has parsed the null-exclusion part and it has also checked
4114 -- that the next token is ACCESS
4116 -- Error recovery: cannot raise Error_Resync
4118 function P_Access_Definition
4119 (Null_Exclusion_Present : Boolean) return Node_Id
4121 Def_Node : Node_Id;
4122 Subp_Node : Node_Id;
4124 begin
4125 Def_Node := New_Node (N_Access_Definition, Token_Ptr);
4126 Scan; -- past ACCESS
4128 -- Ada 2005 (AI-254): Access_To_Subprogram_Definition
4130 if Token = Tok_Protected
4131 or else Token = Tok_Procedure
4132 or else Token = Tok_Function
4133 then
4134 if Ada_Version < Ada_2005 then
4135 Error_Msg_SP ("access-to-subprogram is an Ada 2005 extension");
4136 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
4137 end if;
4139 Subp_Node := P_Access_Type_Definition (Header_Already_Parsed => True);
4140 Set_Null_Exclusion_Present (Subp_Node, Null_Exclusion_Present);
4141 Set_Access_To_Subprogram_Definition (Def_Node, Subp_Node);
4143 -- Ada 2005 (AI-231)
4144 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
4146 else
4147 Set_Null_Exclusion_Present (Def_Node, Null_Exclusion_Present);
4149 if Token = Tok_All then
4150 if Ada_Version < Ada_2005 then
4151 Error_Msg_SP
4152 ("ALL is not permitted for anonymous access types");
4153 end if;
4155 Scan; -- past ALL
4156 Set_All_Present (Def_Node);
4158 elsif Token = Tok_Constant then
4159 if Ada_Version < Ada_2005 then
4160 Error_Msg_SP ("access-to-constant is an Ada 2005 extension");
4161 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
4162 end if;
4164 Scan; -- past CONSTANT
4165 Set_Constant_Present (Def_Node);
4166 end if;
4168 Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
4169 No_Constraint;
4170 end if;
4172 return Def_Node;
4173 end P_Access_Definition;
4175 -----------------------------------------
4176 -- 3.10.1 Incomplete Type Declaration --
4177 -----------------------------------------
4179 -- Parsed by P_Type_Declaration (3.2.1)
4181 ----------------------------
4182 -- 3.11 Declarative Part --
4183 ----------------------------
4185 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
4187 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Items
4188 -- handles errors, and returns cleanly after an error has occurred)
4190 function P_Declarative_Part return List_Id is
4191 Decls : List_Id;
4192 Done : Boolean;
4194 begin
4195 -- Indicate no bad declarations detected yet. This will be reset by
4196 -- P_Declarative_Items if a bad declaration is discovered.
4198 Missing_Begin_Msg := No_Error_Msg;
4200 -- Get rid of active SIS entry from outer scope. This means we will
4201 -- miss some nested cases, but it doesn't seem worth the effort. See
4202 -- discussion in Par for further details
4204 SIS_Entry_Active := False;
4205 Decls := New_List;
4207 -- Loop to scan out the declarations
4209 loop
4210 P_Declarative_Items (Decls, Done, In_Spec => False);
4211 exit when Done;
4212 end loop;
4214 -- Get rid of active SIS entry which is left set only if we scanned a
4215 -- procedure declaration and have not found the body. We could give
4216 -- an error message, but that really would be usurping the role of
4217 -- semantic analysis (this really is a missing body case).
4219 SIS_Entry_Active := False;
4220 return Decls;
4221 end P_Declarative_Part;
4223 ----------------------------
4224 -- 3.11 Declarative Item --
4225 ----------------------------
4227 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
4229 -- Can return Error if a junk declaration is found, or Empty if no
4230 -- declaration is found (i.e. a token ending declarations, such as
4231 -- BEGIN or END is encountered).
4233 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
4234 -- then the scan is set past the next semicolon and Error is returned.
4236 procedure P_Declarative_Items
4237 (Decls : List_Id;
4238 Done : out Boolean;
4239 In_Spec : Boolean)
4241 Scan_State : Saved_Scan_State;
4243 begin
4244 if Style_Check then
4245 Style.Check_Indentation;
4246 end if;
4248 case Token is
4250 when Tok_Function =>
4251 Check_Bad_Layout;
4252 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4253 Done := False;
4255 when Tok_For =>
4256 Check_Bad_Layout;
4258 -- Check for loop (premature statement)
4260 Save_Scan_State (Scan_State);
4261 Scan; -- past FOR
4263 if Token = Tok_Identifier then
4264 Scan; -- past identifier
4266 if Token = Tok_In then
4267 Restore_Scan_State (Scan_State);
4268 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4269 return;
4270 end if;
4271 end if;
4273 -- Not a loop, so must be rep clause
4275 Restore_Scan_State (Scan_State);
4276 Append (P_Representation_Clause, Decls);
4277 Done := False;
4279 when Tok_Generic =>
4280 Check_Bad_Layout;
4281 Append (P_Generic, Decls);
4282 Done := False;
4284 when Tok_Identifier =>
4285 Check_Bad_Layout;
4287 -- Special check for misuse of overriding not in Ada 2005 mode
4289 if Token_Name = Name_Overriding
4290 and then not Next_Token_Is (Tok_Colon)
4291 then
4292 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
4293 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
4295 Token := Tok_Overriding;
4296 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4297 Done := False;
4299 -- Normal case, no overriding, or overriding followed by colon
4301 else
4302 P_Identifier_Declarations (Decls, Done, In_Spec);
4303 end if;
4305 -- Ada 2005: A subprogram declaration can start with "not" or
4306 -- "overriding". In older versions, "overriding" is handled
4307 -- like an identifier, with the appropriate messages.
4309 when Tok_Not =>
4310 Check_Bad_Layout;
4311 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4312 Done := False;
4314 when Tok_Overriding =>
4315 Check_Bad_Layout;
4316 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4317 Done := False;
4319 when Tok_Package =>
4320 Check_Bad_Layout;
4321 Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4322 Done := False;
4324 when Tok_Pragma =>
4325 Append (P_Pragma, Decls);
4326 Done := False;
4328 when Tok_Procedure =>
4329 Check_Bad_Layout;
4330 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
4331 Done := False;
4333 when Tok_Protected =>
4334 Check_Bad_Layout;
4335 Scan; -- past PROTECTED
4336 Append (P_Protected, Decls);
4337 Done := False;
4339 when Tok_Subtype =>
4340 Check_Bad_Layout;
4341 Append (P_Subtype_Declaration, Decls);
4342 Done := False;
4344 when Tok_Task =>
4345 Check_Bad_Layout;
4346 Scan; -- past TASK
4347 Append (P_Task, Decls);
4348 Done := False;
4350 when Tok_Type =>
4351 Check_Bad_Layout;
4352 Append (P_Type_Declaration, Decls);
4353 Done := False;
4355 when Tok_Use =>
4356 Check_Bad_Layout;
4357 Append (P_Use_Clause, Decls);
4358 Done := False;
4360 when Tok_With =>
4361 Check_Bad_Layout;
4363 if Aspect_Specifications_Present then
4365 -- If we are after a semicolon, complain that it was ignored.
4366 -- But we don't really ignore it, since we dump the aspects,
4367 -- so we make the error message a normal fatal message which
4368 -- will inhibit semantic analysis anyway).
4370 if Prev_Token = Tok_Semicolon then
4371 Error_Msg_SP -- CODEFIX
4372 ("extra "";"" ignored");
4374 -- If not just past semicolon, just complain that aspects are
4375 -- not allowed at this point.
4377 else
4378 Error_Msg_SC ("aspect specifications not allowed here");
4379 end if;
4381 declare
4382 Dummy_Node : constant Node_Id :=
4383 New_Node (N_Package_Specification, Token_Ptr);
4384 pragma Warnings (Off, Dummy_Node);
4385 -- Dummy node to attach aspect specifications to. We will
4386 -- then throw them away.
4388 begin
4389 P_Aspect_Specifications (Dummy_Node, Semicolon => True);
4390 end;
4392 -- Here if not aspect specifications case
4394 else
4395 Error_Msg_SC ("WITH can only appear in context clause");
4396 raise Error_Resync;
4397 end if;
4399 -- BEGIN terminates the scan of a sequence of declarations unless
4400 -- there is a missing subprogram body, see section on handling
4401 -- semicolon in place of IS. We only treat the begin as satisfying
4402 -- the subprogram declaration if it falls in the expected column
4403 -- or to its right.
4405 when Tok_Begin =>
4406 if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
4408 -- Here we have the case where a BEGIN is encountered during
4409 -- declarations in a declarative part, or at the outer level,
4410 -- and there is a subprogram declaration outstanding for which
4411 -- no body has been supplied. This is the case where we assume
4412 -- that the semicolon in the subprogram declaration should
4413 -- really have been is. The active SIS entry describes the
4414 -- subprogram declaration. On return the declaration has been
4415 -- modified to become a body.
4417 declare
4418 Specification_Node : Node_Id;
4419 Decl_Node : Node_Id;
4420 Body_Node : Node_Id;
4422 begin
4423 -- First issue the error message. If we had a missing
4424 -- semicolon in the declaration, then change the message
4425 -- to <missing "is">
4427 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
4428 Change_Error_Text -- Replace: "missing "";"" "
4429 (SIS_Missing_Semicolon_Message, "missing ""is""");
4431 -- Otherwise we saved the semicolon position, so complain
4433 else
4434 Error_Msg -- CODEFIX
4435 ("|"";"" should be IS", SIS_Semicolon_Sloc);
4436 end if;
4438 -- The next job is to fix up any declarations that occurred
4439 -- between the procedure header and the BEGIN. These got
4440 -- chained to the outer declarative region (immediately
4441 -- after the procedure declaration) and they should be
4442 -- chained to the subprogram itself, which is a body
4443 -- rather than a spec.
4445 Specification_Node := Specification (SIS_Declaration_Node);
4446 Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
4447 Body_Node := SIS_Declaration_Node;
4448 Set_Specification (Body_Node, Specification_Node);
4449 Set_Declarations (Body_Node, New_List);
4451 loop
4452 Decl_Node := Remove_Next (Body_Node);
4453 exit when Decl_Node = Empty;
4454 Append (Decl_Node, Declarations (Body_Node));
4455 end loop;
4457 -- Now make the scope table entry for the Begin-End and
4458 -- scan it out
4460 Push_Scope_Stack;
4461 Scope.Table (Scope.Last).Sloc := SIS_Sloc;
4462 Scope.Table (Scope.Last).Etyp := E_Name;
4463 Scope.Table (Scope.Last).Ecol := SIS_Ecol;
4464 Scope.Table (Scope.Last).Labl := SIS_Labl;
4465 Scope.Table (Scope.Last).Lreq := False;
4466 SIS_Entry_Active := False;
4467 Scan; -- past BEGIN
4468 Set_Handled_Statement_Sequence (Body_Node,
4469 P_Handled_Sequence_Of_Statements);
4470 End_Statements (Handled_Statement_Sequence (Body_Node));
4471 end;
4473 Done := False;
4475 else
4476 Done := True;
4477 end if;
4479 -- Normally an END terminates the scan for basic declarative items.
4480 -- The one exception is END RECORD, which is probably left over from
4481 -- some other junk.
4483 when Tok_End =>
4484 Save_Scan_State (Scan_State); -- at END
4485 Scan; -- past END
4487 if Token = Tok_Record then
4488 Error_Msg_SP ("no RECORD for this `end record`!");
4489 Scan; -- past RECORD
4490 TF_Semicolon;
4492 else
4493 Restore_Scan_State (Scan_State); -- to END
4494 Done := True;
4495 end if;
4497 -- The following tokens which can only be the start of a statement
4498 -- are considered to end a declarative part (i.e. we have a missing
4499 -- BEGIN situation). We are fairly conservative in making this
4500 -- judgment, because it is a real mess to go into statement mode
4501 -- prematurely in response to a junk declaration.
4503 when Tok_Abort |
4504 Tok_Accept |
4505 Tok_Declare |
4506 Tok_Delay |
4507 Tok_Exit |
4508 Tok_Goto |
4509 Tok_If |
4510 Tok_Loop |
4511 Tok_Null |
4512 Tok_Requeue |
4513 Tok_Select |
4514 Tok_While =>
4516 -- But before we decide that it's a statement, let's check for
4517 -- a reserved word misused as an identifier.
4519 if Is_Reserved_Identifier then
4520 Save_Scan_State (Scan_State);
4521 Scan; -- past the token
4523 -- If reserved identifier not followed by colon or comma, then
4524 -- this is most likely an assignment statement to the bad id.
4526 if Token /= Tok_Colon and then Token /= Tok_Comma then
4527 Restore_Scan_State (Scan_State);
4528 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4529 return;
4531 -- Otherwise we have a declaration of the bad id
4533 else
4534 Restore_Scan_State (Scan_State);
4535 Scan_Reserved_Identifier (Force_Msg => True);
4536 P_Identifier_Declarations (Decls, Done, In_Spec);
4537 end if;
4539 -- If not reserved identifier, then it's definitely a statement
4541 else
4542 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4543 return;
4544 end if;
4546 -- The token RETURN may well also signal a missing BEGIN situation,
4547 -- however, we never let it end the declarative part, because it may
4548 -- also be part of a half-baked function declaration.
4550 when Tok_Return =>
4551 Error_Msg_SC ("misplaced RETURN statement");
4552 raise Error_Resync;
4554 -- PRIVATE definitely terminates the declarations in a spec,
4555 -- and is an error in a body.
4557 when Tok_Private =>
4558 if In_Spec then
4559 Done := True;
4560 else
4561 Error_Msg_SC ("PRIVATE not allowed in body");
4562 Scan; -- past PRIVATE
4563 end if;
4565 -- An end of file definitely terminates the declarations
4567 when Tok_EOF =>
4568 Done := True;
4570 -- The remaining tokens do not end the scan, but cannot start a
4571 -- valid declaration, so we signal an error and resynchronize.
4572 -- But first check for misuse of a reserved identifier.
4574 when others =>
4576 -- Here we check for a reserved identifier
4578 if Is_Reserved_Identifier then
4579 Save_Scan_State (Scan_State);
4580 Scan; -- past the token
4582 if Token /= Tok_Colon and then Token /= Tok_Comma then
4583 Restore_Scan_State (Scan_State);
4584 Set_Declaration_Expected;
4585 raise Error_Resync;
4586 else
4587 Restore_Scan_State (Scan_State);
4588 Scan_Reserved_Identifier (Force_Msg => True);
4589 Check_Bad_Layout;
4590 P_Identifier_Declarations (Decls, Done, In_Spec);
4591 end if;
4593 else
4594 Set_Declaration_Expected;
4595 raise Error_Resync;
4596 end if;
4597 end case;
4599 -- To resynchronize after an error, we scan to the next semicolon and
4600 -- return with Done = False, indicating that there may still be more
4601 -- valid declarations to come.
4603 exception
4604 when Error_Resync =>
4605 Resync_Past_Semicolon;
4606 Done := False;
4607 end P_Declarative_Items;
4609 ----------------------------------
4610 -- 3.11 Basic Declarative Item --
4611 ----------------------------------
4613 -- BASIC_DECLARATIVE_ITEM ::=
4614 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
4616 -- Scan zero or more basic declarative items
4618 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
4619 -- the scan pointer is repositioned past the next semicolon, and the scan
4620 -- for declarative items continues.
4622 function P_Basic_Declarative_Items return List_Id is
4623 Decl : Node_Id;
4624 Decls : List_Id;
4625 Kind : Node_Kind;
4626 Done : Boolean;
4628 begin
4629 -- Indicate no bad declarations detected yet in the current context:
4630 -- visible or private declarations of a package spec.
4632 Missing_Begin_Msg := No_Error_Msg;
4634 -- Get rid of active SIS entry from outer scope. This means we will
4635 -- miss some nested cases, but it doesn't seem worth the effort. See
4636 -- discussion in Par for further details
4638 SIS_Entry_Active := False;
4640 -- Loop to scan out declarations
4642 Decls := New_List;
4644 loop
4645 P_Declarative_Items (Decls, Done, In_Spec => True);
4646 exit when Done;
4647 end loop;
4649 -- Get rid of active SIS entry. This is set only if we have scanned a
4650 -- procedure declaration and have not found the body. We could give
4651 -- an error message, but that really would be usurping the role of
4652 -- semantic analysis (this really is a case of a missing body).
4654 SIS_Entry_Active := False;
4656 -- Test for assorted illegal declarations not diagnosed elsewhere
4658 Decl := First (Decls);
4660 while Present (Decl) loop
4661 Kind := Nkind (Decl);
4663 -- Test for body scanned, not acceptable as basic decl item
4665 if Kind = N_Subprogram_Body or else
4666 Kind = N_Package_Body or else
4667 Kind = N_Task_Body or else
4668 Kind = N_Protected_Body
4669 then
4670 Error_Msg ("proper body not allowed in package spec", Sloc (Decl));
4672 -- Complete declaration of mangled subprogram body, for better
4673 -- recovery if analysis is attempted.
4675 if Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
4676 and then No (Handled_Statement_Sequence (Decl))
4677 then
4678 Set_Handled_Statement_Sequence (Decl,
4679 Make_Handled_Sequence_Of_Statements (Sloc (Decl),
4680 Statements => New_List));
4681 end if;
4683 -- Test for body stub scanned, not acceptable as basic decl item
4685 elsif Kind in N_Body_Stub then
4686 Error_Msg ("body stub not allowed in package spec", Sloc (Decl));
4688 elsif Kind = N_Assignment_Statement then
4689 Error_Msg
4690 ("assignment statement not allowed in package spec",
4691 Sloc (Decl));
4692 end if;
4694 Next (Decl);
4695 end loop;
4697 return Decls;
4698 end P_Basic_Declarative_Items;
4700 ----------------
4701 -- 3.11 Body --
4702 ----------------
4704 -- For proper body, see below
4705 -- For body stub, see 10.1.3
4707 -----------------------
4708 -- 3.11 Proper Body --
4709 -----------------------
4711 -- Subprogram body is parsed by P_Subprogram (6.1)
4712 -- Package body is parsed by P_Package (7.1)
4713 -- Task body is parsed by P_Task (9.1)
4714 -- Protected body is parsed by P_Protected (9.4)
4716 ------------------------------
4717 -- Set_Declaration_Expected --
4718 ------------------------------
4720 procedure Set_Declaration_Expected is
4721 begin
4722 Error_Msg_SC ("declaration expected");
4724 if Missing_Begin_Msg = No_Error_Msg then
4725 Missing_Begin_Msg := Get_Msg_Id;
4726 end if;
4727 end Set_Declaration_Expected;
4729 ----------------------
4730 -- Skip_Declaration --
4731 ----------------------
4733 procedure Skip_Declaration (S : List_Id) is
4734 Dummy_Done : Boolean;
4735 pragma Warnings (Off, Dummy_Done);
4736 begin
4737 P_Declarative_Items (S, Dummy_Done, False);
4738 end Skip_Declaration;
4740 -----------------------------------------
4741 -- Statement_When_Declaration_Expected --
4742 -----------------------------------------
4744 procedure Statement_When_Declaration_Expected
4745 (Decls : List_Id;
4746 Done : out Boolean;
4747 In_Spec : Boolean)
4749 begin
4750 -- Case of second occurrence of statement in one declaration sequence
4752 if Missing_Begin_Msg /= No_Error_Msg then
4754 -- In the procedure spec case, just ignore it, we only give one
4755 -- message for the first occurrence, since otherwise we may get
4756 -- horrible cascading if BODY was missing in the header line.
4758 if In_Spec then
4759 null;
4761 -- In the declarative part case, take a second statement as a sure
4762 -- sign that we really have a missing BEGIN, and end the declarative
4763 -- part now. Note that the caller will fix up the first message to
4764 -- say "missing BEGIN" so that's how the error will be signalled.
4766 else
4767 Done := True;
4768 return;
4769 end if;
4771 -- Case of first occurrence of unexpected statement
4773 else
4774 -- If we are in a package spec, then give message of statement
4775 -- not allowed in package spec. This message never gets changed.
4777 if In_Spec then
4778 Error_Msg_SC ("statement not allowed in package spec");
4780 -- If in declarative part, then we give the message complaining
4781 -- about finding a statement when a declaration is expected. This
4782 -- gets changed to a complaint about a missing BEGIN if we later
4783 -- find that no BEGIN is present.
4785 else
4786 Error_Msg_SC ("statement not allowed in declarative part");
4787 end if;
4789 -- Capture message Id. This is used for two purposes, first to
4790 -- stop multiple messages, see test above, and second, to allow
4791 -- the replacement of the message in the declarative part case.
4793 Missing_Begin_Msg := Get_Msg_Id;
4794 end if;
4796 -- In all cases except the case in which we decided to terminate the
4797 -- declaration sequence on a second error, we scan out the statement
4798 -- and append it to the list of declarations (note that the semantics
4799 -- can handle statements in a declaration list so if we proceed to
4800 -- call the semantic phase, all will be (reasonably) well.
4802 Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
4804 -- Done is set to False, since we want to continue the scan of
4805 -- declarations, hoping that this statement was a temporary glitch.
4806 -- If we indeed are now in the statement part (i.e. this was a missing
4807 -- BEGIN, then it's not terrible, we will simply keep calling this
4808 -- procedure to process the statements one by one, and then finally
4809 -- hit the missing BEGIN, which will clean up the error message.
4811 Done := False;
4812 end Statement_When_Declaration_Expected;
4814 end Ch3;