2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / par-ch3.adb
blobc2ec59be9dc2c5c640e1bd049a72b4d6f9492796
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-2008, 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 package body Ch3 is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
40 function P_Component_List return Node_Id;
41 function P_Defining_Character_Literal return Node_Id;
42 function P_Delta_Constraint return Node_Id;
43 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id;
44 function P_Digits_Constraint return Node_Id;
45 function P_Discriminant_Association return Node_Id;
46 function P_Enumeration_Literal_Specification return Node_Id;
47 function P_Enumeration_Type_Definition return Node_Id;
48 function P_Fixed_Point_Definition return Node_Id;
49 function P_Floating_Point_Definition return Node_Id;
50 function P_Index_Or_Discriminant_Constraint return Node_Id;
51 function P_Real_Range_Specification_Opt return Node_Id;
52 function P_Subtype_Declaration return Node_Id;
53 function P_Type_Declaration return Node_Id;
54 function P_Modular_Type_Definition return Node_Id;
55 function P_Variant return Node_Id;
56 function P_Variant_Part return Node_Id;
58 procedure P_Declarative_Items
59 (Decls : List_Id;
60 Done : out Boolean;
61 In_Spec : Boolean);
62 -- Scans out a single declarative item, or, in the case of a declaration
63 -- with a list of identifiers, a list of declarations, one for each of the
64 -- identifiers in the list. The declaration or declarations scanned are
65 -- appended to the given list. Done indicates whether or not there may be
66 -- additional declarative items to scan. If Done is True, then a decision
67 -- has been made that there are no more items to scan. If Done is False,
68 -- then there may be additional declarations to scan. In_Spec is true if
69 -- we are scanning a package declaration, and is used to generate an
70 -- appropriate message if a statement is encountered in such a context.
72 procedure P_Identifier_Declarations
73 (Decls : List_Id;
74 Done : out Boolean;
75 In_Spec : Boolean);
76 -- Scans out a set of declarations for an identifier or list of
77 -- identifiers, and appends them to the given list. The parameters have
78 -- the same significance as for P_Declarative_Items.
80 procedure Statement_When_Declaration_Expected
81 (Decls : List_Id;
82 Done : out Boolean;
83 In_Spec : Boolean);
84 -- Called when a statement is found at a point where a declaration was
85 -- expected. The parameters are as described for P_Declarative_Items.
87 procedure Set_Declaration_Expected;
88 -- Posts a "declaration expected" error messages at the start of the
89 -- current token, and if this is the first such message issued, saves
90 -- the message id in Missing_Begin_Msg, for possible later replacement.
92 -------------------
93 -- Init_Expr_Opt --
94 -------------------
96 function Init_Expr_Opt (P : Boolean := False) return Node_Id is
97 begin
98 -- For colon, assume it means := unless it is at the end of
99 -- a line, in which case guess that it means a semicolon.
101 if Token = Tok_Colon then
102 if Token_Is_At_End_Of_Line then
103 T_Semicolon;
104 return Empty;
105 end if;
107 -- Here if := or something that we will take as equivalent
109 elsif Token = Tok_Colon_Equal
110 or else Token = Tok_Equal
111 or else Token = Tok_Is
112 then
113 null;
115 -- Another possibility. If we have a literal followed by a semicolon,
116 -- we assume that we have a missing colon-equal.
118 elsif Token in Token_Class_Literal then
119 declare
120 Scan_State : Saved_Scan_State;
122 begin
123 Save_Scan_State (Scan_State);
124 Scan; -- past literal or identifier
126 if Token = Tok_Semicolon then
127 Restore_Scan_State (Scan_State);
128 else
129 Restore_Scan_State (Scan_State);
130 return Empty;
131 end if;
132 end;
134 -- Otherwise we definitely have no initialization expression
136 else
137 return Empty;
138 end if;
140 -- Merge here if we have an initialization expression
142 T_Colon_Equal;
144 if P then
145 return P_Expression;
146 else
147 return P_Expression_No_Right_Paren;
148 end if;
149 end Init_Expr_Opt;
151 ----------------------------
152 -- 3.1 Basic Declaration --
153 ----------------------------
155 -- Parsed by P_Basic_Declarative_Items (3.9)
157 ------------------------------
158 -- 3.1 Defining Identifier --
159 ------------------------------
161 -- DEFINING_IDENTIFIER ::= IDENTIFIER
163 -- Error recovery: can raise Error_Resync
165 function P_Defining_Identifier (C : Id_Check := None) return Node_Id is
166 Ident_Node : Node_Id;
168 begin
169 -- Scan out the identifier. Note that this code is essentially identical
170 -- to P_Identifier, except that in the call to Scan_Reserved_Identifier
171 -- we set Force_Msg to True, since we want at least one message for each
172 -- separate declaration (but not use) of a reserved identifier.
174 if Token = Tok_Identifier then
176 -- Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
177 -- OVERRIDING, and SYNCHRONIZED are new reserved words. Note that
178 -- in the case where these keywords are misused in Ada 95 mode,
179 -- this routine will generally not be called at all.
181 if Ada_Version = Ada_95
182 and then Warn_On_Ada_2005_Compatibility
183 then
184 if Token_Name = Name_Overriding
185 or else Token_Name = Name_Synchronized
186 or else (Token_Name = Name_Interface
187 and then Prev_Token /= Tok_Pragma)
188 then
189 Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
190 end if;
191 end if;
193 -- If we have a reserved identifier, manufacture an identifier with
194 -- a corresponding name after posting an appropriate error message
196 elsif Is_Reserved_Identifier (C) then
197 Scan_Reserved_Identifier (Force_Msg => True);
199 -- Otherwise we have junk that cannot be interpreted as an identifier
201 else
202 T_Identifier; -- to give message
203 raise Error_Resync;
204 end if;
206 Ident_Node := Token_Node;
207 Scan; -- past the reserved identifier
209 if Ident_Node /= Error then
210 Change_Identifier_To_Defining_Identifier (Ident_Node);
211 end if;
213 return Ident_Node;
214 end P_Defining_Identifier;
216 -----------------------------
217 -- 3.2.1 Type Declaration --
218 -----------------------------
220 -- TYPE_DECLARATION ::=
221 -- FULL_TYPE_DECLARATION
222 -- | INCOMPLETE_TYPE_DECLARATION
223 -- | PRIVATE_TYPE_DECLARATION
224 -- | PRIVATE_EXTENSION_DECLARATION
226 -- FULL_TYPE_DECLARATION ::=
227 -- type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART] is TYPE_DEFINITION;
228 -- | CONCURRENT_TYPE_DECLARATION
230 -- INCOMPLETE_TYPE_DECLARATION ::=
231 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] [is tagged];
233 -- PRIVATE_TYPE_DECLARATION ::=
234 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART]
235 -- is [abstract] [tagged] [limited] private;
237 -- PRIVATE_EXTENSION_DECLARATION ::=
238 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
239 -- [abstract] [limited | synchronized]
240 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
241 -- with private;
243 -- TYPE_DEFINITION ::=
244 -- ENUMERATION_TYPE_DEFINITION | INTEGER_TYPE_DEFINITION
245 -- | REAL_TYPE_DEFINITION | ARRAY_TYPE_DEFINITION
246 -- | RECORD_TYPE_DEFINITION | ACCESS_TYPE_DEFINITION
247 -- | DERIVED_TYPE_DEFINITION | INTERFACE_TYPE_DEFINITION
249 -- INTEGER_TYPE_DEFINITION ::=
250 -- SIGNED_INTEGER_TYPE_DEFINITION
251 -- MODULAR_TYPE_DEFINITION
253 -- INTERFACE_TYPE_DEFINITION ::=
254 -- [limited | task | protected | synchronized ] interface
255 -- [and INTERFACE_LIST]
257 -- Error recovery: can raise Error_Resync
259 -- Note: The processing for full type declaration, incomplete type
260 -- declaration, private type declaration and type definition is
261 -- included in this function. The processing for concurrent type
262 -- declarations is NOT here, but rather in chapter 9 (i.e. this
263 -- function handles only declarations starting with TYPE).
265 function P_Type_Declaration return Node_Id is
266 Abstract_Present : Boolean := False;
267 Abstract_Loc : Source_Ptr := No_Location;
268 Decl_Node : Node_Id;
269 Discr_List : List_Id;
270 Discr_Sloc : Source_Ptr;
271 End_Labl : Node_Id;
272 Ident_Node : Node_Id;
273 Is_Derived_Iface : Boolean := False;
274 Type_Loc : Source_Ptr;
275 Type_Start_Col : Column_Number;
276 Unknown_Dis : Boolean;
278 Typedef_Node : Node_Id;
279 -- Normally holds type definition, except in the case of a private
280 -- extension declaration, in which case it holds the declaration itself
282 begin
283 Type_Loc := Token_Ptr;
284 Type_Start_Col := Start_Column;
286 -- If we have TYPE, then proceed ahead and scan identifier
288 if Token = Tok_Type then
289 Type_Token_Location := Type_Loc;
290 Scan; -- past TYPE
291 Ident_Node := P_Defining_Identifier (C_Is);
293 -- Otherwise this is an error case, and we may already have converted
294 -- the current token to a defining identifier, so don't do it again!
296 else
297 T_Type;
299 if Token = Tok_Identifier
300 and then Nkind (Token_Node) = N_Defining_Identifier
301 then
302 Ident_Node := Token_Node;
303 Scan; -- past defining identifier
304 else
305 Ident_Node := P_Defining_Identifier (C_Is);
306 end if;
307 end if;
309 Discr_Sloc := Token_Ptr;
311 if P_Unknown_Discriminant_Part_Opt then
312 Unknown_Dis := True;
313 Discr_List := No_List;
314 else
315 Unknown_Dis := False;
316 Discr_List := P_Known_Discriminant_Part_Opt;
317 end if;
319 -- Incomplete type declaration. We complete the processing for this
320 -- case here and return the resulting incomplete type declaration node
322 if Token = Tok_Semicolon then
323 Scan; -- past ;
324 Decl_Node := New_Node (N_Incomplete_Type_Declaration, Type_Loc);
325 Set_Defining_Identifier (Decl_Node, Ident_Node);
326 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
327 Set_Discriminant_Specifications (Decl_Node, Discr_List);
328 return Decl_Node;
330 else
331 Decl_Node := Empty;
332 end if;
334 -- Full type declaration or private type declaration, must have IS
336 if Token = Tok_Equal then
337 TF_Is;
338 Scan; -- past = used in place of IS
340 elsif Token = Tok_Renames then
341 Error_Msg_SC ("RENAMES should be IS");
342 Scan; -- past RENAMES used in place of IS
344 else
345 TF_Is;
346 end if;
348 -- First an error check, if we have two identifiers in a row, a likely
349 -- possibility is that the first of the identifiers is an incorrectly
350 -- spelled keyword.
352 if Token = Tok_Identifier then
353 declare
354 SS : Saved_Scan_State;
355 I2 : Boolean;
357 begin
358 Save_Scan_State (SS);
359 Scan; -- past initial identifier
360 I2 := (Token = Tok_Identifier);
361 Restore_Scan_State (SS);
363 if I2
364 and then
365 (Bad_Spelling_Of (Tok_Abstract) or else
366 Bad_Spelling_Of (Tok_Access) or else
367 Bad_Spelling_Of (Tok_Aliased) or else
368 Bad_Spelling_Of (Tok_Constant))
369 then
370 null;
371 end if;
372 end;
373 end if;
375 -- Check for misuse of Ada 95 keyword abstract in Ada 83 mode
377 if Token_Name = Name_Abstract then
378 Check_95_Keyword (Tok_Abstract, Tok_Tagged);
379 Check_95_Keyword (Tok_Abstract, Tok_New);
380 end if;
382 -- Check cases of misuse of ABSTRACT
384 if Token = Tok_Abstract then
385 Abstract_Present := True;
386 Abstract_Loc := Token_Ptr;
387 Scan; -- past ABSTRACT
389 -- Ada 2005 (AI-419): AARM 3.4 (2/2)
391 if (Ada_Version < Ada_05 and then Token = Tok_Limited)
392 or else Token = Tok_Private
393 or else Token = Tok_Record
394 or else Token = Tok_Null
395 then
396 Error_Msg_AP ("TAGGED expected");
397 end if;
398 end if;
400 -- Check for misuse of Ada 95 keyword Tagged
402 if Token_Name = Name_Tagged then
403 Check_95_Keyword (Tok_Tagged, Tok_Private);
404 Check_95_Keyword (Tok_Tagged, Tok_Limited);
405 Check_95_Keyword (Tok_Tagged, Tok_Record);
406 end if;
408 -- Special check for misuse of Aliased
410 if Token = Tok_Aliased or else Token_Name = Name_Aliased then
411 Error_Msg_SC ("ALIASED not allowed in type definition");
412 Scan; -- past ALIASED
413 end if;
415 -- The following processing deals with either a private type declaration
416 -- or a full type declaration. In the private type case, we build the
417 -- N_Private_Type_Declaration node, setting its Tagged_Present and
418 -- Limited_Present flags, on encountering the Private keyword, and
419 -- leave Typedef_Node set to Empty. For the full type declaration
420 -- case, Typedef_Node gets set to the type definition.
422 Typedef_Node := Empty;
424 -- Switch on token following the IS. The loop normally runs once. It
425 -- only runs more than once if an error is detected, to try again after
426 -- detecting and fixing up the error.
428 loop
429 case Token is
431 when Tok_Access |
432 Tok_Not => -- Ada 2005 (AI-231)
433 Typedef_Node := P_Access_Type_Definition;
434 TF_Semicolon;
435 exit;
437 when Tok_Array =>
438 Typedef_Node := P_Array_Type_Definition;
439 TF_Semicolon;
440 exit;
442 when Tok_Delta =>
443 Typedef_Node := P_Fixed_Point_Definition;
444 TF_Semicolon;
445 exit;
447 when Tok_Digits =>
448 Typedef_Node := P_Floating_Point_Definition;
449 TF_Semicolon;
450 exit;
452 when Tok_In =>
453 Ignore (Tok_In);
455 when Tok_Integer_Literal =>
456 T_Range;
457 Typedef_Node := P_Signed_Integer_Type_Definition;
458 TF_Semicolon;
459 exit;
461 when Tok_Null =>
462 Typedef_Node := P_Record_Definition;
463 TF_Semicolon;
464 exit;
466 when Tok_Left_Paren =>
467 Typedef_Node := P_Enumeration_Type_Definition;
469 End_Labl :=
470 Make_Identifier (Token_Ptr,
471 Chars => Chars (Ident_Node));
472 Set_Comes_From_Source (End_Labl, False);
474 Set_End_Label (Typedef_Node, End_Labl);
475 TF_Semicolon;
476 exit;
478 when Tok_Mod =>
479 Typedef_Node := P_Modular_Type_Definition;
480 TF_Semicolon;
481 exit;
483 when Tok_New =>
484 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
486 if Nkind (Typedef_Node) = N_Derived_Type_Definition
487 and then Present (Record_Extension_Part (Typedef_Node))
488 then
489 End_Labl :=
490 Make_Identifier (Token_Ptr,
491 Chars => Chars (Ident_Node));
492 Set_Comes_From_Source (End_Labl, False);
494 Set_End_Label
495 (Record_Extension_Part (Typedef_Node), End_Labl);
496 end if;
498 TF_Semicolon;
499 exit;
501 when Tok_Range =>
502 Typedef_Node := P_Signed_Integer_Type_Definition;
503 TF_Semicolon;
504 exit;
506 when Tok_Record =>
507 Typedef_Node := P_Record_Definition;
509 End_Labl :=
510 Make_Identifier (Token_Ptr,
511 Chars => Chars (Ident_Node));
512 Set_Comes_From_Source (End_Labl, False);
514 Set_End_Label (Typedef_Node, End_Labl);
515 TF_Semicolon;
516 exit;
518 when Tok_Tagged =>
519 Scan; -- past TAGGED
521 -- Ada 2005 (AI-326): If the words IS TAGGED appear, the type
522 -- is a tagged incomplete type.
524 if Ada_Version >= Ada_05
525 and then Token = Tok_Semicolon
526 then
527 Scan; -- past ;
529 Decl_Node :=
530 New_Node (N_Incomplete_Type_Declaration, Type_Loc);
531 Set_Defining_Identifier (Decl_Node, Ident_Node);
532 Set_Tagged_Present (Decl_Node);
533 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
534 Set_Discriminant_Specifications (Decl_Node, Discr_List);
536 return Decl_Node;
537 end if;
539 if Token = Tok_Abstract then
540 Error_Msg_SC ("ABSTRACT must come before TAGGED");
541 Abstract_Present := True;
542 Abstract_Loc := Token_Ptr;
543 Scan; -- past ABSTRACT
544 end if;
546 if Token = Tok_Limited then
547 Scan; -- past LIMITED
549 -- TAGGED LIMITED PRIVATE case
551 if Token = Tok_Private then
552 Decl_Node :=
553 New_Node (N_Private_Type_Declaration, Type_Loc);
554 Set_Tagged_Present (Decl_Node, True);
555 Set_Limited_Present (Decl_Node, True);
556 Scan; -- past PRIVATE
558 -- TAGGED LIMITED RECORD
560 else
561 Typedef_Node := P_Record_Definition;
562 Set_Tagged_Present (Typedef_Node, True);
563 Set_Limited_Present (Typedef_Node, True);
565 End_Labl :=
566 Make_Identifier (Token_Ptr,
567 Chars => Chars (Ident_Node));
568 Set_Comes_From_Source (End_Labl, False);
570 Set_End_Label (Typedef_Node, End_Labl);
571 end if;
573 else
574 -- TAGGED PRIVATE
576 if Token = Tok_Private then
577 Decl_Node :=
578 New_Node (N_Private_Type_Declaration, Type_Loc);
579 Set_Tagged_Present (Decl_Node, True);
580 Scan; -- past PRIVATE
582 -- TAGGED RECORD
584 else
585 Typedef_Node := P_Record_Definition;
586 Set_Tagged_Present (Typedef_Node, True);
588 End_Labl :=
589 Make_Identifier (Token_Ptr,
590 Chars => Chars (Ident_Node));
591 Set_Comes_From_Source (End_Labl, False);
593 Set_End_Label (Typedef_Node, End_Labl);
594 end if;
595 end if;
597 TF_Semicolon;
598 exit;
600 when Tok_Limited =>
601 Scan; -- past LIMITED
603 loop
604 if Token = Tok_Tagged then
605 Error_Msg_SC ("TAGGED must come before LIMITED");
606 Scan; -- past TAGGED
608 elsif Token = Tok_Abstract then
609 Error_Msg_SC ("ABSTRACT must come before LIMITED");
610 Scan; -- past ABSTRACT
612 else
613 exit;
614 end if;
615 end loop;
617 -- LIMITED RECORD or LIMITED NULL RECORD
619 if Token = Tok_Record or else Token = Tok_Null then
620 if Ada_Version = Ada_83 then
621 Error_Msg_SP
622 ("(Ada 83) limited record declaration not allowed!");
624 -- In Ada2005, "abstract limited" can appear before "new",
625 -- but it cannot be part of an untagged record declaration.
627 elsif Abstract_Present
628 and then Prev_Token /= Tok_Tagged
629 then
630 Error_Msg_SP ("TAGGED expected");
631 end if;
633 Typedef_Node := P_Record_Definition;
634 Set_Limited_Present (Typedef_Node, True);
636 -- Ada 2005 (AI-251): LIMITED INTERFACE
638 -- If we are compiling in Ada 83 or Ada 95 mode, "interface"
639 -- is not a reserved word but we force its analysis to
640 -- generate the corresponding usage error.
642 elsif Token = Tok_Interface
643 or else (Token = Tok_Identifier
644 and then Chars (Token_Node) = Name_Interface)
645 then
646 Typedef_Node :=
647 P_Interface_Type_Definition (Abstract_Present);
648 Abstract_Present := True;
649 Set_Limited_Present (Typedef_Node);
651 if Nkind (Typedef_Node) = N_Derived_Type_Definition then
652 Is_Derived_Iface := True;
653 end if;
655 -- Ada 2005 (AI-419): LIMITED NEW
657 elsif Token = Tok_New then
658 if Ada_Version < Ada_05 then
659 Error_Msg_SP
660 ("LIMITED in derived type is an Ada 2005 extension");
661 Error_Msg_SP
662 ("\unit must be compiled with -gnat05 switch");
663 end if;
665 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
666 Set_Limited_Present (Typedef_Node);
668 if Nkind (Typedef_Node) = N_Derived_Type_Definition
669 and then Present (Record_Extension_Part (Typedef_Node))
670 then
671 End_Labl :=
672 Make_Identifier (Token_Ptr,
673 Chars => Chars (Ident_Node));
674 Set_Comes_From_Source (End_Labl, False);
676 Set_End_Label
677 (Record_Extension_Part (Typedef_Node), End_Labl);
678 end if;
680 -- LIMITED PRIVATE is the only remaining possibility here
682 else
683 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
684 Set_Limited_Present (Decl_Node, True);
685 T_Private; -- past PRIVATE (or complain if not there!)
686 end if;
688 TF_Semicolon;
689 exit;
691 -- Here we have an identifier after the IS, which is certainly
692 -- wrong and which might be one of several different mistakes.
694 when Tok_Identifier =>
696 -- First case, if identifier is on same line, then probably we
697 -- have something like "type X is Integer .." and the best
698 -- diagnosis is a missing NEW. Note: the missing new message
699 -- will be posted by P_Derived_Type_Def_Or_Private_Ext_Decl.
701 if not Token_Is_At_Start_Of_Line then
702 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
703 TF_Semicolon;
705 -- If the identifier is at the start of the line, and is in the
706 -- same column as the type declaration itself then we consider
707 -- that we had a missing type definition on the previous line
709 elsif Start_Column <= Type_Start_Col then
710 Error_Msg_AP ("type definition expected");
711 Typedef_Node := Error;
713 -- If the identifier is at the start of the line, and is in
714 -- a column to the right of the type declaration line, then we
715 -- may have something like:
717 -- type x is
718 -- r : integer
720 -- and the best diagnosis is a missing record keyword
722 else
723 Typedef_Node := P_Record_Definition;
724 TF_Semicolon;
725 end if;
727 exit;
729 -- Ada 2005 (AI-251): INTERFACE
731 when Tok_Interface =>
732 Typedef_Node := P_Interface_Type_Definition (Abstract_Present);
733 Abstract_Present := True;
734 TF_Semicolon;
735 exit;
737 when Tok_Private =>
738 Decl_Node := New_Node (N_Private_Type_Declaration, Type_Loc);
739 Scan; -- past PRIVATE
740 TF_Semicolon;
741 exit;
743 -- Ada 2005 (AI-345): Protected, synchronized or task interface
744 -- or Ada 2005 (AI-443): Synchronized private extension.
746 when Tok_Protected |
747 Tok_Synchronized |
748 Tok_Task =>
750 declare
751 Saved_Token : constant Token_Type := Token;
753 begin
754 Scan; -- past TASK, PROTECTED or SYNCHRONIZED
756 -- Synchronized private extension
758 if Token = Tok_New then
759 Typedef_Node := P_Derived_Type_Def_Or_Private_Ext_Decl;
761 if Saved_Token = Tok_Synchronized then
762 Set_Synchronized_Present (Typedef_Node);
763 else
764 Error_Msg_SC ("invalid kind of private extension");
765 end if;
767 -- Interface
769 else
770 if Token /= Tok_Interface then
771 Error_Msg_SC ("NEW or INTERFACE expected");
772 end if;
774 Typedef_Node :=
775 P_Interface_Type_Definition (Abstract_Present);
776 Abstract_Present := True;
778 case Saved_Token is
779 when Tok_Task =>
780 Set_Task_Present (Typedef_Node);
782 when Tok_Protected =>
783 Set_Protected_Present (Typedef_Node);
785 when Tok_Synchronized =>
786 Set_Synchronized_Present (Typedef_Node);
788 when others =>
789 pragma Assert (False);
790 null;
791 end case;
792 end if;
793 end;
795 TF_Semicolon;
796 exit;
798 -- Anything else is an error
800 when others =>
801 if Bad_Spelling_Of (Tok_Access)
802 or else
803 Bad_Spelling_Of (Tok_Array)
804 or else
805 Bad_Spelling_Of (Tok_Delta)
806 or else
807 Bad_Spelling_Of (Tok_Digits)
808 or else
809 Bad_Spelling_Of (Tok_Limited)
810 or else
811 Bad_Spelling_Of (Tok_Private)
812 or else
813 Bad_Spelling_Of (Tok_Range)
814 or else
815 Bad_Spelling_Of (Tok_Record)
816 or else
817 Bad_Spelling_Of (Tok_Tagged)
818 then
819 null;
821 else
822 Error_Msg_AP ("type definition expected");
823 raise Error_Resync;
824 end if;
826 end case;
827 end loop;
829 -- For the private type declaration case, the private type declaration
830 -- node has been built, with the Tagged_Present and Limited_Present
831 -- flags set as needed, and Typedef_Node is left set to Empty.
833 if No (Typedef_Node) then
834 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
835 Set_Abstract_Present (Decl_Node, Abstract_Present);
837 -- For a private extension declaration, Typedef_Node contains the
838 -- N_Private_Extension_Declaration node, which we now complete. Note
839 -- that the private extension declaration, unlike a full type
840 -- declaration, does permit unknown discriminants.
842 elsif Nkind (Typedef_Node) = N_Private_Extension_Declaration then
843 Decl_Node := Typedef_Node;
844 Set_Sloc (Decl_Node, Type_Loc);
845 Set_Unknown_Discriminants_Present (Decl_Node, Unknown_Dis);
846 Set_Abstract_Present (Typedef_Node, Abstract_Present);
848 -- In the full type declaration case, Typedef_Node has the type
849 -- definition and here is where we build the full type declaration
850 -- node. This is also where we check for improper use of an unknown
851 -- discriminant part (not allowed for full type declaration).
853 else
854 if Nkind (Typedef_Node) = N_Record_Definition
855 or else (Nkind (Typedef_Node) = N_Derived_Type_Definition
856 and then Present (Record_Extension_Part (Typedef_Node)))
857 or else Is_Derived_Iface
858 then
859 Set_Abstract_Present (Typedef_Node, Abstract_Present);
861 elsif Abstract_Present then
862 Error_Msg ("ABSTRACT not allowed here, ignored", Abstract_Loc);
863 end if;
865 Decl_Node := New_Node (N_Full_Type_Declaration, Type_Loc);
866 Set_Type_Definition (Decl_Node, Typedef_Node);
868 if Unknown_Dis then
869 Error_Msg
870 ("Full type declaration cannot have unknown discriminants",
871 Discr_Sloc);
872 end if;
873 end if;
875 -- Remaining processing is common for all three cases
877 Set_Defining_Identifier (Decl_Node, Ident_Node);
878 Set_Discriminant_Specifications (Decl_Node, Discr_List);
879 return Decl_Node;
880 end P_Type_Declaration;
882 ----------------------------------
883 -- 3.2.1 Full Type Declaration --
884 ----------------------------------
886 -- Parsed by P_Type_Declaration (3.2.1)
888 ----------------------------
889 -- 3.2.1 Type Definition --
890 ----------------------------
892 -- Parsed by P_Type_Declaration (3.2.1)
894 --------------------------------
895 -- 3.2.2 Subtype Declaration --
896 --------------------------------
898 -- SUBTYPE_DECLARATION ::=
899 -- subtype DEFINING_IDENTIFIER is [NULL_EXCLUSION] SUBTYPE_INDICATION;
901 -- The caller has checked that the initial token is SUBTYPE
903 -- Error recovery: can raise Error_Resync
905 function P_Subtype_Declaration return Node_Id is
906 Decl_Node : Node_Id;
907 Not_Null_Present : Boolean := False;
909 begin
910 Decl_Node := New_Node (N_Subtype_Declaration, Token_Ptr);
911 Scan; -- past SUBTYPE
912 Set_Defining_Identifier (Decl_Node, P_Defining_Identifier (C_Is));
913 TF_Is;
915 if Token = Tok_New then
916 Error_Msg_SC ("NEW ignored (only allowed in type declaration)");
917 Scan; -- past NEW
918 end if;
920 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
921 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
923 Set_Subtype_Indication
924 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
925 TF_Semicolon;
926 return Decl_Node;
927 end P_Subtype_Declaration;
929 -------------------------------
930 -- 3.2.2 Subtype Indication --
931 -------------------------------
933 -- SUBTYPE_INDICATION ::=
934 -- [not null] SUBTYPE_MARK [CONSTRAINT]
936 -- Error recovery: can raise Error_Resync
938 function P_Null_Exclusion
939 (Allow_Anonymous_In_95 : Boolean := False) return Boolean
941 Not_Loc : constant Source_Ptr := Token_Ptr;
942 -- Source position of "not", if present
944 begin
945 if Token /= Tok_Not then
946 return False;
948 else
949 Scan; -- past NOT
951 if Token = Tok_Null then
952 Scan; -- past NULL
954 -- Ada 2005 (AI-441, AI-447): null_exclusion is illegal in Ada 95,
955 -- except in the case of anonymous access types.
957 -- Allow_Anonymous_In_95 will be True if we're parsing a formal
958 -- parameter or discriminant, which are the only places where
959 -- anonymous access types occur in Ada 95. "Formal : not null
960 -- access ..." is legal in Ada 95, whereas "Formal : not null
961 -- Named_Access_Type" is not.
963 if Ada_Version >= Ada_05
964 or else (Ada_Version >= Ada_95
965 and then Allow_Anonymous_In_95
966 and then Token = Tok_Access)
967 then
968 null; -- OK
970 else
971 Error_Msg
972 ("`NOT NULL` access type is an Ada 2005 extension", Not_Loc);
973 Error_Msg
974 ("\unit should be compiled with -gnat05 switch", Not_Loc);
975 end if;
977 else
978 Error_Msg_SP ("NULL expected");
979 end if;
981 if Token = Tok_New then
982 Error_Msg ("`NOT NULL` comes after NEW, not before", Not_Loc);
983 end if;
985 return True;
986 end if;
987 end P_Null_Exclusion;
989 function P_Subtype_Indication
990 (Not_Null_Present : Boolean := False) return Node_Id
992 Type_Node : Node_Id;
994 begin
995 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
996 Type_Node := P_Subtype_Mark;
997 return P_Subtype_Indication (Type_Node, Not_Null_Present);
999 else
1000 -- Check for error of using record definition and treat it nicely,
1001 -- otherwise things are really messed up, so resynchronize.
1003 if Token = Tok_Record then
1004 Error_Msg_SC ("anonymous record definitions are not permitted");
1005 Discard_Junk_Node (P_Record_Definition);
1006 return Error;
1008 else
1009 Error_Msg_AP ("subtype indication expected");
1010 raise Error_Resync;
1011 end if;
1012 end if;
1013 end P_Subtype_Indication;
1015 -- The following function is identical except that it is called with
1016 -- the subtype mark already scanned out, and it scans out the constraint
1018 -- Error recovery: can raise Error_Resync
1020 function P_Subtype_Indication
1021 (Subtype_Mark : Node_Id;
1022 Not_Null_Present : Boolean := False) return Node_Id
1024 Indic_Node : Node_Id;
1025 Constr_Node : Node_Id;
1027 begin
1028 Constr_Node := P_Constraint_Opt;
1030 if No (Constr_Node) then
1031 return Subtype_Mark;
1032 else
1033 if Not_Null_Present then
1034 Error_Msg_SP ("`NOT NULL` not allowed if constraint given");
1035 end if;
1037 Indic_Node := New_Node (N_Subtype_Indication, Sloc (Subtype_Mark));
1038 Set_Subtype_Mark (Indic_Node, Check_Subtype_Mark (Subtype_Mark));
1039 Set_Constraint (Indic_Node, Constr_Node);
1040 return Indic_Node;
1041 end if;
1042 end P_Subtype_Indication;
1044 -------------------------
1045 -- 3.2.2 Subtype Mark --
1046 -------------------------
1048 -- SUBTYPE_MARK ::= subtype_NAME;
1050 -- Note: The subtype mark which appears after an IN or NOT IN
1051 -- operator is parsed by P_Range_Or_Subtype_Mark (3.5)
1053 -- Error recovery: cannot raise Error_Resync
1055 function P_Subtype_Mark return Node_Id is
1056 begin
1057 return P_Subtype_Mark_Resync;
1058 exception
1059 when Error_Resync =>
1060 return Error;
1061 end P_Subtype_Mark;
1063 -- This routine differs from P_Subtype_Mark in that it insists that an
1064 -- identifier be present, and if it is not, it raises Error_Resync.
1066 -- Error recovery: can raise Error_Resync
1068 function P_Subtype_Mark_Resync return Node_Id is
1069 Type_Node : Node_Id;
1071 begin
1072 if Token = Tok_Access then
1073 Error_Msg_SC ("anonymous access type definition not allowed here");
1074 Scan; -- past ACCESS
1075 end if;
1077 if Token = Tok_Array then
1078 Error_Msg_SC ("anonymous array definition not allowed here");
1079 Discard_Junk_Node (P_Array_Type_Definition);
1080 return Error;
1082 else
1083 Type_Node := P_Qualified_Simple_Name_Resync;
1085 -- Check for a subtype mark attribute. The only valid possibilities
1086 -- are 'CLASS and 'BASE. Anything else is a definite error. We may
1087 -- as well catch it here.
1089 if Token = Tok_Apostrophe then
1090 return P_Subtype_Mark_Attribute (Type_Node);
1091 else
1092 return Type_Node;
1093 end if;
1094 end if;
1095 end P_Subtype_Mark_Resync;
1097 -- The following function is called to scan out a subtype mark attribute.
1098 -- The caller has already scanned out the subtype mark, which is passed in
1099 -- as the argument, and has checked that the current token is apostrophe.
1101 -- Only a special subclass of attributes, called type attributes
1102 -- (see Snames package) are allowed in this syntactic position.
1104 -- Note: if the apostrophe is followed by other than an identifier, then
1105 -- the input expression is returned unchanged, and the scan pointer is
1106 -- left pointing to the apostrophe.
1108 -- Error recovery: can raise Error_Resync
1110 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id is
1111 Attr_Node : Node_Id := Empty;
1112 Scan_State : Saved_Scan_State;
1113 Prefix : Node_Id;
1115 begin
1116 Prefix := Check_Subtype_Mark (Type_Node);
1118 if Prefix = Error then
1119 raise Error_Resync;
1120 end if;
1122 -- Loop through attributes appearing (more than one can appear as for
1123 -- for example in X'Base'Class). We are at an apostrophe on entry to
1124 -- this loop, and it runs once for each attribute parsed, with
1125 -- Prefix being the current possible prefix if it is an attribute.
1127 loop
1128 Save_Scan_State (Scan_State); -- at Apostrophe
1129 Scan; -- past apostrophe
1131 if Token /= Tok_Identifier then
1132 Restore_Scan_State (Scan_State); -- to apostrophe
1133 return Prefix; -- no attribute after all
1135 elsif not Is_Type_Attribute_Name (Token_Name) then
1136 Error_Msg_N
1137 ("attribute & may not be used in a subtype mark", Token_Node);
1138 raise Error_Resync;
1140 else
1141 Attr_Node :=
1142 Make_Attribute_Reference (Prev_Token_Ptr,
1143 Prefix => Prefix,
1144 Attribute_Name => Token_Name);
1145 Scan; -- past type attribute identifier
1146 end if;
1148 exit when Token /= Tok_Apostrophe;
1149 Prefix := Attr_Node;
1150 end loop;
1152 -- Fall through here after scanning type attribute
1154 return Attr_Node;
1155 end P_Subtype_Mark_Attribute;
1157 -----------------------
1158 -- 3.2.2 Constraint --
1159 -----------------------
1161 -- CONSTRAINT ::= SCALAR_CONSTRAINT | COMPOSITE_CONSTRAINT
1163 -- SCALAR_CONSTRAINT ::=
1164 -- RANGE_CONSTRAINT | DIGITS_CONSTRAINT | DELTA_CONSTRAINT
1166 -- COMPOSITE_CONSTRAINT ::=
1167 -- INDEX_CONSTRAINT | DISCRIMINANT_CONSTRAINT
1169 -- If no constraint is present, this function returns Empty
1171 -- Error recovery: can raise Error_Resync
1173 function P_Constraint_Opt return Node_Id is
1174 begin
1175 if Token = Tok_Range
1176 or else Bad_Spelling_Of (Tok_Range)
1177 then
1178 return P_Range_Constraint;
1180 elsif Token = Tok_Digits
1181 or else Bad_Spelling_Of (Tok_Digits)
1182 then
1183 return P_Digits_Constraint;
1185 elsif Token = Tok_Delta
1186 or else Bad_Spelling_Of (Tok_Delta)
1187 then
1188 return P_Delta_Constraint;
1190 elsif Token = Tok_Left_Paren then
1191 return P_Index_Or_Discriminant_Constraint;
1193 elsif Token = Tok_In then
1194 Ignore (Tok_In);
1195 return P_Constraint_Opt;
1197 else
1198 return Empty;
1199 end if;
1200 end P_Constraint_Opt;
1202 ------------------------------
1203 -- 3.2.2 Scalar Constraint --
1204 ------------------------------
1206 -- Parsed by P_Constraint_Opt (3.2.2)
1208 ---------------------------------
1209 -- 3.2.2 Composite Constraint --
1210 ---------------------------------
1212 -- Parsed by P_Constraint_Opt (3.2.2)
1214 --------------------------------------------------------
1215 -- 3.3 Identifier Declarations (Also 7.4, 8.5, 11.1) --
1216 --------------------------------------------------------
1218 -- This routine scans out a declaration starting with an identifier:
1220 -- OBJECT_DECLARATION ::=
1221 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1222 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1223 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1224 -- ACCESS_DEFINITION [:= EXPRESSION];
1225 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1226 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1228 -- NUMBER_DECLARATION ::=
1229 -- DEFINING_IDENTIFIER_LIST : constant ::= static_EXPRESSION;
1231 -- OBJECT_RENAMING_DECLARATION ::=
1232 -- DEFINING_IDENTIFIER :
1233 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1234 -- | DEFINING_IDENTIFIER :
1235 -- ACCESS_DEFINITION renames object_NAME;
1237 -- EXCEPTION_RENAMING_DECLARATION ::=
1238 -- DEFINING_IDENTIFIER : exception renames exception_NAME;
1240 -- EXCEPTION_DECLARATION ::=
1241 -- DEFINING_IDENTIFIER_LIST : exception;
1243 -- Note that the ALIASED indication in an object declaration is
1244 -- marked by a flag in the parent node.
1246 -- The caller has checked that the initial token is an identifier
1248 -- The value returned is a list of declarations, one for each identifier
1249 -- in the list (as described in Sinfo, we always split up multiple
1250 -- declarations into the equivalent sequence of single declarations
1251 -- using the More_Ids and Prev_Ids flags to preserve the source).
1253 -- If the identifier turns out to be a probable statement rather than
1254 -- an identifier, then the scan is left pointing to the identifier and
1255 -- No_List is returned.
1257 -- Error recovery: can raise Error_Resync
1259 procedure P_Identifier_Declarations
1260 (Decls : List_Id;
1261 Done : out Boolean;
1262 In_Spec : Boolean)
1264 Acc_Node : Node_Id;
1265 Decl_Node : Node_Id;
1266 Type_Node : Node_Id;
1267 Ident_Sloc : Source_Ptr;
1268 Scan_State : Saved_Scan_State;
1269 List_OK : Boolean := True;
1270 Ident : Nat;
1271 Init_Expr : Node_Id;
1272 Init_Loc : Source_Ptr;
1273 Con_Loc : Source_Ptr;
1274 Not_Null_Present : Boolean := False;
1276 Idents : array (Int range 1 .. 4096) of Entity_Id;
1277 -- Used to save identifiers in the identifier list. The upper bound
1278 -- of 4096 is expected to be infinite in practice, and we do not even
1279 -- bother to check if this upper bound is exceeded.
1281 Num_Idents : Nat := 1;
1282 -- Number of identifiers stored in Idents
1284 procedure No_List;
1285 -- This procedure is called in renames cases to make sure that we do
1286 -- not have more than one identifier. If we do have more than one
1287 -- then an error message is issued (and the declaration is split into
1288 -- multiple declarations)
1290 function Token_Is_Renames return Boolean;
1291 -- Checks if current token is RENAMES, and if so, scans past it and
1292 -- returns True, otherwise returns False. Includes checking for some
1293 -- common error cases.
1295 -------------
1296 -- No_List --
1297 -------------
1299 procedure No_List is
1300 begin
1301 if Num_Idents > 1 then
1302 Error_Msg ("identifier list not allowed for RENAMES",
1303 Sloc (Idents (2)));
1304 end if;
1306 List_OK := False;
1307 end No_List;
1309 ----------------------
1310 -- Token_Is_Renames --
1311 ----------------------
1313 function Token_Is_Renames return Boolean is
1314 At_Colon : Saved_Scan_State;
1316 begin
1317 if Token = Tok_Colon then
1318 Save_Scan_State (At_Colon);
1319 Scan; -- past colon
1320 Check_Misspelling_Of (Tok_Renames);
1322 if Token = Tok_Renames then
1323 Error_Msg_SP ("|extra "":"" ignored");
1324 Scan; -- past RENAMES
1325 return True;
1326 else
1327 Restore_Scan_State (At_Colon);
1328 return False;
1329 end if;
1331 else
1332 Check_Misspelling_Of (Tok_Renames);
1334 if Token = Tok_Renames then
1335 Scan; -- past RENAMES
1336 return True;
1337 else
1338 return False;
1339 end if;
1340 end if;
1341 end Token_Is_Renames;
1343 -- Start of processing for P_Identifier_Declarations
1345 begin
1346 Ident_Sloc := Token_Ptr;
1347 Save_Scan_State (Scan_State); -- at first identifier
1348 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1350 -- If we have a colon after the identifier, then we can assume that
1351 -- this is in fact a valid identifier declaration and can steam ahead.
1353 if Token = Tok_Colon then
1354 Scan; -- past colon
1356 -- If we have a comma, then scan out the list of identifiers
1358 elsif Token = Tok_Comma then
1360 while Comma_Present loop
1361 Num_Idents := Num_Idents + 1;
1362 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1363 end loop;
1365 Save_Scan_State (Scan_State); -- at colon
1366 T_Colon;
1368 -- If we have identifier followed by := then we assume that what is
1369 -- really meant is an assignment statement. The assignment statement
1370 -- is scanned out and added to the list of declarations. An exception
1371 -- occurs if the := is followed by the keyword constant, in which case
1372 -- we assume it was meant to be a colon.
1374 elsif Token = Tok_Colon_Equal then
1375 Scan; -- past :=
1377 if Token = Tok_Constant then
1378 Error_Msg_SP ("colon expected");
1380 else
1381 Restore_Scan_State (Scan_State);
1382 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
1383 return;
1384 end if;
1386 -- If we have an IS keyword, then assume the TYPE keyword was missing
1388 elsif Token = Tok_Is then
1389 Restore_Scan_State (Scan_State);
1390 Append_To (Decls, P_Type_Declaration);
1391 Done := False;
1392 return;
1394 -- Otherwise we have an error situation
1396 else
1397 Restore_Scan_State (Scan_State);
1399 -- First case is possible misuse of PROTECTED in Ada 83 mode. If
1400 -- so, fix the keyword and return to scan the protected declaration.
1402 if Token_Name = Name_Protected then
1403 Check_95_Keyword (Tok_Protected, Tok_Identifier);
1404 Check_95_Keyword (Tok_Protected, Tok_Type);
1405 Check_95_Keyword (Tok_Protected, Tok_Body);
1407 if Token = Tok_Protected then
1408 Done := False;
1409 return;
1410 end if;
1412 -- Check misspelling possibilities. If so, correct the misspelling
1413 -- and return to scan out the resulting declaration.
1415 elsif Bad_Spelling_Of (Tok_Function)
1416 or else Bad_Spelling_Of (Tok_Procedure)
1417 or else Bad_Spelling_Of (Tok_Package)
1418 or else Bad_Spelling_Of (Tok_Pragma)
1419 or else Bad_Spelling_Of (Tok_Protected)
1420 or else Bad_Spelling_Of (Tok_Generic)
1421 or else Bad_Spelling_Of (Tok_Subtype)
1422 or else Bad_Spelling_Of (Tok_Type)
1423 or else Bad_Spelling_Of (Tok_Task)
1424 or else Bad_Spelling_Of (Tok_Use)
1425 or else Bad_Spelling_Of (Tok_For)
1426 then
1427 Done := False;
1428 return;
1430 -- Otherwise we definitely have an ordinary identifier with a junk
1431 -- token after it. Just complain that we expect a declaration, and
1432 -- skip to a semicolon
1434 else
1435 Set_Declaration_Expected;
1436 Resync_Past_Semicolon;
1437 Done := False;
1438 return;
1439 end if;
1440 end if;
1442 -- Come here with an identifier list and colon scanned out. We now
1443 -- build the nodes for the declarative items. One node is built for
1444 -- each identifier in the list, with the type information being
1445 -- repeated by rescanning the appropriate section of source.
1447 -- First an error check, if we have two identifiers in a row, a likely
1448 -- possibility is that the first of the identifiers is an incorrectly
1449 -- spelled keyword.
1451 if Token = Tok_Identifier then
1452 declare
1453 SS : Saved_Scan_State;
1454 I2 : Boolean;
1456 begin
1457 Save_Scan_State (SS);
1458 Scan; -- past initial identifier
1459 I2 := (Token = Tok_Identifier);
1460 Restore_Scan_State (SS);
1462 if I2
1463 and then
1464 (Bad_Spelling_Of (Tok_Access) or else
1465 Bad_Spelling_Of (Tok_Aliased) or else
1466 Bad_Spelling_Of (Tok_Constant))
1467 then
1468 null;
1469 end if;
1470 end;
1471 end if;
1473 -- Loop through identifiers
1475 Ident := 1;
1476 Ident_Loop : loop
1478 -- Check for some cases of misused Ada 95 keywords
1480 if Token_Name = Name_Aliased then
1481 Check_95_Keyword (Tok_Aliased, Tok_Array);
1482 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1483 Check_95_Keyword (Tok_Aliased, Tok_Constant);
1484 end if;
1486 -- Constant cases
1488 if Token = Tok_Constant then
1489 Con_Loc := Token_Ptr;
1490 Scan; -- past CONSTANT
1492 -- Number declaration, initialization required
1494 Init_Expr := Init_Expr_Opt;
1496 if Present (Init_Expr) then
1497 if Not_Null_Present then
1498 Error_Msg_SP
1499 ("`NOT NULL` not allowed in numeric expression");
1500 end if;
1502 Decl_Node := New_Node (N_Number_Declaration, Ident_Sloc);
1503 Set_Expression (Decl_Node, Init_Expr);
1505 -- Constant object declaration
1507 else
1508 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1509 Set_Constant_Present (Decl_Node, True);
1511 if Token_Name = Name_Aliased then
1512 Check_95_Keyword (Tok_Aliased, Tok_Array);
1513 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
1514 end if;
1516 if Token = Tok_Aliased then
1517 Error_Msg_SC ("ALIASED should be before CONSTANT");
1518 Scan; -- past ALIASED
1519 Set_Aliased_Present (Decl_Node, True);
1520 end if;
1522 if Token = Tok_Array then
1523 Set_Object_Definition
1524 (Decl_Node, P_Array_Type_Definition);
1526 else
1527 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1528 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1530 if Token = Tok_Access then
1531 if Ada_Version < Ada_05 then
1532 Error_Msg_SP
1533 ("generalized use of anonymous access types " &
1534 "is an Ada 2005 extension");
1535 Error_Msg_SP
1536 ("\unit must be compiled with -gnat05 switch");
1537 end if;
1539 Set_Object_Definition
1540 (Decl_Node, P_Access_Definition (Not_Null_Present));
1541 else
1542 Set_Object_Definition
1543 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1544 end if;
1545 end if;
1547 if Token = Tok_Renames then
1548 Error_Msg
1549 ("CONSTANT not permitted in renaming declaration",
1550 Con_Loc);
1551 Scan; -- Past renames
1552 Discard_Junk_Node (P_Name);
1553 end if;
1554 end if;
1556 -- Exception cases
1558 elsif Token = Tok_Exception then
1559 Scan; -- past EXCEPTION
1561 if Token_Is_Renames then
1562 No_List;
1563 Decl_Node :=
1564 New_Node (N_Exception_Renaming_Declaration, Ident_Sloc);
1565 Set_Name (Decl_Node, P_Qualified_Simple_Name_Resync);
1566 No_Constraint;
1567 else
1568 Decl_Node := New_Node (N_Exception_Declaration, Prev_Token_Ptr);
1569 end if;
1571 -- Aliased case (note that an object definition is required)
1573 elsif Token = Tok_Aliased then
1574 Scan; -- past ALIASED
1575 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1576 Set_Aliased_Present (Decl_Node, True);
1578 if Token = Tok_Constant then
1579 Scan; -- past CONSTANT
1580 Set_Constant_Present (Decl_Node, True);
1581 end if;
1583 if Token = Tok_Array then
1584 Set_Object_Definition
1585 (Decl_Node, P_Array_Type_Definition);
1587 else
1588 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1589 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1591 -- Access definition (AI-406) or subtype indication
1593 if Token = Tok_Access then
1594 if Ada_Version < Ada_05 then
1595 Error_Msg_SP
1596 ("generalized use of anonymous access types " &
1597 "is an Ada 2005 extension");
1598 Error_Msg_SP
1599 ("\unit must be compiled with -gnat05 switch");
1600 end if;
1602 Set_Object_Definition
1603 (Decl_Node, P_Access_Definition (Not_Null_Present));
1604 else
1605 Set_Object_Definition
1606 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1607 end if;
1608 end if;
1610 -- Array case
1612 elsif Token = Tok_Array then
1613 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1614 Set_Object_Definition (Decl_Node, P_Array_Type_Definition);
1616 -- Ada 2005 (AI-254, AI-406)
1618 elsif Token = Tok_Not then
1620 -- OBJECT_DECLARATION ::=
1621 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1622 -- [NULL_EXCLUSION] SUBTYPE_INDICATION [:= EXPRESSION];
1623 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1624 -- ACCESS_DEFINITION [:= EXPRESSION];
1626 -- OBJECT_RENAMING_DECLARATION ::=
1627 -- DEFINING_IDENTIFIER :
1628 -- [NULL_EXCLUSION] SUBTYPE_MARK renames object_NAME;
1629 -- | DEFINING_IDENTIFIER :
1630 -- ACCESS_DEFINITION renames object_NAME;
1632 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/423)
1634 if Token = Tok_Access then
1635 if Ada_Version < Ada_05 then
1636 Error_Msg_SP
1637 ("generalized use of anonymous access types " &
1638 "is an Ada 2005 extension");
1639 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1640 end if;
1642 Acc_Node := P_Access_Definition (Not_Null_Present);
1644 if Token /= Tok_Renames then
1645 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1646 Set_Object_Definition (Decl_Node, Acc_Node);
1648 else
1649 Scan; -- past renames
1650 No_List;
1651 Decl_Node :=
1652 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1653 Set_Access_Definition (Decl_Node, Acc_Node);
1654 Set_Name (Decl_Node, P_Name);
1655 end if;
1657 else
1658 Type_Node := P_Subtype_Mark;
1660 -- Object renaming declaration
1662 if Token_Is_Renames then
1663 if Ada_Version < Ada_05 then
1664 Error_Msg_SP
1665 ("`NOT NULL` not allowed in object renaming");
1666 raise Error_Resync;
1668 -- Ada 2005 (AI-423): Object renaming declaration with
1669 -- a null exclusion.
1671 else
1672 No_List;
1673 Decl_Node :=
1674 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1675 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1676 Set_Subtype_Mark (Decl_Node, Type_Node);
1677 Set_Name (Decl_Node, P_Name);
1678 end if;
1680 -- Object declaration
1682 else
1683 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1684 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1685 Set_Object_Definition
1686 (Decl_Node,
1687 P_Subtype_Indication (Type_Node, Not_Null_Present));
1689 -- RENAMES at this point means that we had the combination
1690 -- of a constraint on the Type_Node and renames, which is
1691 -- illegal
1693 if Token_Is_Renames then
1694 Error_Msg_N ("constraint not allowed in object renaming "
1695 & "declaration",
1696 Constraint (Object_Definition (Decl_Node)));
1697 raise Error_Resync;
1698 end if;
1699 end if;
1700 end if;
1702 -- Ada 2005 (AI-230): Access Definition case
1704 elsif Token = Tok_Access then
1705 if Ada_Version < Ada_05 then
1706 Error_Msg_SP
1707 ("generalized use of anonymous access types " &
1708 "is an Ada 2005 extension");
1709 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1710 end if;
1712 Acc_Node := P_Access_Definition (Null_Exclusion_Present => False);
1714 -- Object declaration with access definition, or renaming
1716 if Token /= Tok_Renames then
1717 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1718 Set_Object_Definition (Decl_Node, Acc_Node);
1720 else
1721 Scan; -- past renames
1722 No_List;
1723 Decl_Node :=
1724 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1725 Set_Access_Definition (Decl_Node, Acc_Node);
1726 Set_Name (Decl_Node, P_Name);
1727 end if;
1729 -- Subtype indication case
1731 else
1732 Type_Node := P_Subtype_Mark;
1734 -- Object renaming declaration
1736 if Token_Is_Renames then
1737 No_List;
1738 Decl_Node :=
1739 New_Node (N_Object_Renaming_Declaration, Ident_Sloc);
1740 Set_Subtype_Mark (Decl_Node, Type_Node);
1741 Set_Name (Decl_Node, P_Name);
1743 -- Object declaration
1745 else
1746 Decl_Node := New_Node (N_Object_Declaration, Ident_Sloc);
1747 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1748 Set_Object_Definition
1749 (Decl_Node,
1750 P_Subtype_Indication (Type_Node, Not_Null_Present));
1752 -- RENAMES at this point means that we had the combination of
1753 -- a constraint on the Type_Node and renames, which is illegal
1755 if Token_Is_Renames then
1756 Error_Msg_N
1757 ("constraint not allowed in object renaming declaration",
1758 Constraint (Object_Definition (Decl_Node)));
1759 raise Error_Resync;
1760 end if;
1761 end if;
1762 end if;
1764 -- Scan out initialization, allowed only for object declaration
1766 Init_Loc := Token_Ptr;
1767 Init_Expr := Init_Expr_Opt;
1769 if Present (Init_Expr) then
1770 if Nkind (Decl_Node) = N_Object_Declaration then
1771 Set_Expression (Decl_Node, Init_Expr);
1772 Set_Has_Init_Expression (Decl_Node);
1773 else
1774 Error_Msg ("initialization not allowed here", Init_Loc);
1775 end if;
1776 end if;
1778 TF_Semicolon;
1779 Set_Defining_Identifier (Decl_Node, Idents (Ident));
1781 if List_OK then
1782 if Ident < Num_Idents then
1783 Set_More_Ids (Decl_Node, True);
1784 end if;
1786 if Ident > 1 then
1787 Set_Prev_Ids (Decl_Node, True);
1788 end if;
1789 end if;
1791 Append (Decl_Node, Decls);
1792 exit Ident_Loop when Ident = Num_Idents;
1793 Restore_Scan_State (Scan_State);
1794 T_Colon;
1795 Ident := Ident + 1;
1796 end loop Ident_Loop;
1798 Done := False;
1799 end P_Identifier_Declarations;
1801 -------------------------------
1802 -- 3.3.1 Object Declaration --
1803 -------------------------------
1805 -- OBJECT DECLARATION ::=
1806 -- DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1807 -- SUBTYPE_INDICATION [:= EXPRESSION];
1808 -- | DEFINING_IDENTIFIER_LIST : [aliased] [constant]
1809 -- ARRAY_TYPE_DEFINITION [:= EXPRESSION];
1810 -- | SINGLE_TASK_DECLARATION
1811 -- | SINGLE_PROTECTED_DECLARATION
1813 -- Cases starting with TASK are parsed by P_Task (9.1)
1814 -- Cases starting with PROTECTED are parsed by P_Protected (9.4)
1815 -- All other cases are parsed by P_Identifier_Declarations (3.3)
1817 -------------------------------------
1818 -- 3.3.1 Defining Identifier List --
1819 -------------------------------------
1821 -- DEFINING_IDENTIFIER_LIST ::=
1822 -- DEFINING_IDENTIFIER {, DEFINING_IDENTIFIER}
1824 -- Always parsed by the construct in which it appears. See special
1825 -- section on "Handling of Defining Identifier Lists" in this unit.
1827 -------------------------------
1828 -- 3.3.2 Number Declaration --
1829 -------------------------------
1831 -- Parsed by P_Identifier_Declarations (3.3)
1833 -------------------------------------------------------------------------
1834 -- 3.4 Derived Type Definition or Private Extension Declaration (7.3) --
1835 -------------------------------------------------------------------------
1837 -- DERIVED_TYPE_DEFINITION ::=
1838 -- [abstract] [limited] new [NULL_EXCLUSION] parent_SUBTYPE_INDICATION
1839 -- [[and INTERFACE_LIST] RECORD_EXTENSION_PART]
1841 -- PRIVATE_EXTENSION_DECLARATION ::=
1842 -- type DEFINING_IDENTIFIER [DISCRIMINANT_PART] is
1843 -- [abstract] [limited | synchronized]
1844 -- new ancestor_SUBTYPE_INDICATION [and INTERFACE_LIST]
1845 -- with private;
1847 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
1849 -- The caller has already scanned out the part up to the NEW, and Token
1850 -- either contains Tok_New (or ought to, if it doesn't this procedure
1851 -- will post an appropriate "NEW expected" message).
1853 -- Note: the caller is responsible for filling in the Sloc field of
1854 -- the returned node in the private extension declaration case as
1855 -- well as the stuff relating to the discriminant part.
1857 -- Error recovery: can raise Error_Resync;
1859 function P_Derived_Type_Def_Or_Private_Ext_Decl return Node_Id is
1860 Typedef_Node : Node_Id;
1861 Typedecl_Node : Node_Id;
1862 Not_Null_Present : Boolean := False;
1864 begin
1865 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
1867 if Ada_Version < Ada_05
1868 and then Token = Tok_Identifier
1869 and then Token_Name = Name_Interface
1870 then
1871 Error_Msg_SP
1872 ("abstract interface is an Ada 2005 extension");
1873 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1874 else
1875 T_New;
1876 end if;
1878 if Token = Tok_Abstract then
1879 Error_Msg_SC ("ABSTRACT must come before NEW, not after");
1880 Scan;
1881 end if;
1883 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1884 Set_Null_Exclusion_Present (Typedef_Node, Not_Null_Present);
1885 Set_Subtype_Indication (Typedef_Node,
1886 P_Subtype_Indication (Not_Null_Present));
1888 -- Ada 2005 (AI-251): Deal with interfaces
1890 if Token = Tok_And then
1891 Scan; -- past AND
1893 if Ada_Version < Ada_05 then
1894 Error_Msg_SP
1895 ("abstract interface is an Ada 2005 extension");
1896 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1897 end if;
1899 Set_Interface_List (Typedef_Node, New_List);
1901 loop
1902 Append (P_Qualified_Simple_Name, Interface_List (Typedef_Node));
1903 exit when Token /= Tok_And;
1904 Scan; -- past AND
1905 end loop;
1907 if Token /= Tok_With then
1908 Error_Msg_SC ("WITH expected");
1909 raise Error_Resync;
1910 end if;
1911 end if;
1913 -- Deal with record extension, note that we assume that a WITH is
1914 -- missing in the case of "type X is new Y record ..." or in the
1915 -- case of "type X is new Y null record".
1917 if Token = Tok_With
1918 or else Token = Tok_Record
1919 or else Token = Tok_Null
1920 then
1921 T_With; -- past WITH or give error message
1923 if Token = Tok_Limited then
1924 Error_Msg_SC
1925 ("LIMITED keyword not allowed in private extension");
1926 Scan; -- ignore LIMITED
1927 end if;
1929 -- Private extension declaration
1931 if Token = Tok_Private then
1932 Scan; -- past PRIVATE
1934 -- Throw away the type definition node and build the type
1935 -- declaration node. Note the caller must set the Sloc,
1936 -- Discriminant_Specifications, Unknown_Discriminants_Present,
1937 -- and Defined_Identifier fields in the returned node.
1939 Typedecl_Node :=
1940 Make_Private_Extension_Declaration (No_Location,
1941 Defining_Identifier => Empty,
1942 Subtype_Indication => Subtype_Indication (Typedef_Node),
1943 Abstract_Present => Abstract_Present (Typedef_Node),
1944 Interface_List => Interface_List (Typedef_Node));
1946 return Typedecl_Node;
1948 -- Derived type definition with record extension part
1950 else
1951 Set_Record_Extension_Part (Typedef_Node, P_Record_Definition);
1952 return Typedef_Node;
1953 end if;
1955 -- Derived type definition with no record extension part
1957 else
1958 return Typedef_Node;
1959 end if;
1960 end P_Derived_Type_Def_Or_Private_Ext_Decl;
1962 ---------------------------
1963 -- 3.5 Range Constraint --
1964 ---------------------------
1966 -- RANGE_CONSTRAINT ::= range RANGE
1968 -- The caller has checked that the initial token is RANGE
1970 -- Error recovery: cannot raise Error_Resync
1972 function P_Range_Constraint return Node_Id is
1973 Range_Node : Node_Id;
1975 begin
1976 Range_Node := New_Node (N_Range_Constraint, Token_Ptr);
1977 Scan; -- past RANGE
1978 Set_Range_Expression (Range_Node, P_Range);
1979 return Range_Node;
1980 end P_Range_Constraint;
1982 ----------------
1983 -- 3.5 Range --
1984 ----------------
1986 -- RANGE ::=
1987 -- RANGE_ATTRIBUTE_REFERENCE | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
1989 -- Note: the range that appears in a membership test is parsed by
1990 -- P_Range_Or_Subtype_Mark (3.5).
1992 -- Error recovery: cannot raise Error_Resync
1994 function P_Range return Node_Id is
1995 Expr_Node : Node_Id;
1996 Range_Node : Node_Id;
1998 begin
1999 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2001 if Expr_Form = EF_Range_Attr then
2002 return Expr_Node;
2004 elsif Token = Tok_Dot_Dot then
2005 Range_Node := New_Node (N_Range, Token_Ptr);
2006 Set_Low_Bound (Range_Node, Expr_Node);
2007 Scan; -- past ..
2008 Expr_Node := P_Expression;
2009 Check_Simple_Expression (Expr_Node);
2010 Set_High_Bound (Range_Node, Expr_Node);
2011 return Range_Node;
2013 -- Anything else is an error
2015 else
2016 T_Dot_Dot; -- force missing .. message
2017 return Error;
2018 end if;
2019 end P_Range;
2021 ----------------------------------
2022 -- 3.5 P_Range_Or_Subtype_Mark --
2023 ----------------------------------
2025 -- RANGE ::=
2026 -- RANGE_ATTRIBUTE_REFERENCE
2027 -- | SIMPLE_EXPRESSION .. SIMPLE_EXPRESSION
2029 -- This routine scans out the range or subtype mark that forms the right
2030 -- operand of a membership test (it is not used in any other contexts, and
2031 -- error messages are specialized with this knowledge in mind).
2033 -- Note: as documented in the Sinfo interface, although the syntax only
2034 -- allows a subtype mark, we in fact allow any simple expression to be
2035 -- returned from this routine. The semantics is responsible for issuing
2036 -- an appropriate message complaining if the argument is not a name.
2037 -- This simplifies the coding and error recovery processing in the
2038 -- parser, and in any case it is preferable not to consider this a
2039 -- syntax error and to continue with the semantic analysis.
2041 -- Error recovery: cannot raise Error_Resync
2043 function P_Range_Or_Subtype_Mark return Node_Id is
2044 Expr_Node : Node_Id;
2045 Range_Node : Node_Id;
2046 Save_Loc : Source_Ptr;
2048 -- Start of processing for P_Range_Or_Subtype_Mark
2050 begin
2051 -- Save location of possible junk parentheses
2053 Save_Loc := Token_Ptr;
2055 -- Scan out either a simple expression or a range (this accepts more
2056 -- than is legal here, but as explained above, we like to allow more
2057 -- with a proper diagnostic.
2059 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2061 -- Range attribute
2063 if Expr_Form = EF_Range_Attr then
2064 return Expr_Node;
2066 -- Simple_Expression .. Simple_Expression
2068 elsif Token = Tok_Dot_Dot then
2069 Check_Simple_Expression (Expr_Node);
2070 Range_Node := New_Node (N_Range, Token_Ptr);
2071 Set_Low_Bound (Range_Node, Expr_Node);
2072 Scan; -- past ..
2073 Set_High_Bound (Range_Node, P_Simple_Expression);
2074 return Range_Node;
2076 -- Case of subtype mark (optionally qualified simple name or an
2077 -- attribute whose prefix is an optionally qualified simple name)
2079 elsif Expr_Form = EF_Simple_Name
2080 or else Nkind (Expr_Node) = N_Attribute_Reference
2081 then
2082 -- Check for error of range constraint after a subtype mark
2084 if Token = Tok_Range then
2085 Error_Msg_SC ("range constraint not allowed in membership test");
2086 Scan; -- past RANGE
2087 raise Error_Resync;
2089 -- Check for error of DIGITS or DELTA after a subtype mark
2091 elsif Token = Tok_Digits or else Token = Tok_Delta then
2092 Error_Msg_SC
2093 ("accuracy definition not allowed in membership test");
2094 Scan; -- past DIGITS or DELTA
2095 raise Error_Resync;
2097 -- Attribute reference, may or may not be OK, but in any case we
2098 -- will scan it out
2100 elsif Token = Tok_Apostrophe then
2101 return P_Subtype_Mark_Attribute (Expr_Node);
2103 -- OK case of simple name, just return it
2105 else
2106 return Expr_Node;
2107 end if;
2109 -- Here we have some kind of error situation. Check for junk parens
2110 -- then return what we have, caller will deal with other errors.
2112 else
2113 if Nkind (Expr_Node) in N_Subexpr
2114 and then Paren_Count (Expr_Node) /= 0
2115 then
2116 Error_Msg ("|parentheses not allowed for subtype mark", Save_Loc);
2117 Set_Paren_Count (Expr_Node, 0);
2118 end if;
2120 return Expr_Node;
2121 end if;
2122 end P_Range_Or_Subtype_Mark;
2124 ----------------------------------------
2125 -- 3.5.1 Enumeration Type Definition --
2126 ----------------------------------------
2128 -- ENUMERATION_TYPE_DEFINITION ::=
2129 -- (ENUMERATION_LITERAL_SPECIFICATION
2130 -- {, ENUMERATION_LITERAL_SPECIFICATION})
2132 -- The caller has already scanned out the TYPE keyword
2134 -- Error recovery: can raise Error_Resync;
2136 function P_Enumeration_Type_Definition return Node_Id is
2137 Typedef_Node : Node_Id;
2139 begin
2140 Typedef_Node := New_Node (N_Enumeration_Type_Definition, Token_Ptr);
2141 Set_Literals (Typedef_Node, New_List);
2143 T_Left_Paren;
2145 loop
2146 Append (P_Enumeration_Literal_Specification, Literals (Typedef_Node));
2147 exit when not Comma_Present;
2148 end loop;
2150 T_Right_Paren;
2151 return Typedef_Node;
2152 end P_Enumeration_Type_Definition;
2154 ----------------------------------------------
2155 -- 3.5.1 Enumeration Literal Specification --
2156 ----------------------------------------------
2158 -- ENUMERATION_LITERAL_SPECIFICATION ::=
2159 -- DEFINING_IDENTIFIER | DEFINING_CHARACTER_LITERAL
2161 -- Error recovery: can raise Error_Resync
2163 function P_Enumeration_Literal_Specification return Node_Id is
2164 begin
2165 if Token = Tok_Char_Literal then
2166 return P_Defining_Character_Literal;
2167 else
2168 return P_Defining_Identifier (C_Comma_Right_Paren);
2169 end if;
2170 end P_Enumeration_Literal_Specification;
2172 ---------------------------------------
2173 -- 3.5.1 Defining_Character_Literal --
2174 ---------------------------------------
2176 -- DEFINING_CHARACTER_LITERAL ::= CHARACTER_LITERAL
2178 -- Error recovery: cannot raise Error_Resync
2180 -- The caller has checked that the current token is a character literal
2182 function P_Defining_Character_Literal return Node_Id is
2183 Literal_Node : Node_Id;
2185 begin
2186 Literal_Node := Token_Node;
2187 Change_Character_Literal_To_Defining_Character_Literal (Literal_Node);
2188 Scan; -- past character literal
2189 return Literal_Node;
2190 end P_Defining_Character_Literal;
2192 ------------------------------------
2193 -- 3.5.4 Integer Type Definition --
2194 ------------------------------------
2196 -- Parsed by P_Type_Declaration (3.2.1)
2198 -------------------------------------------
2199 -- 3.5.4 Signed Integer Type Definition --
2200 -------------------------------------------
2202 -- SIGNED_INTEGER_TYPE_DEFINITION ::=
2203 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2205 -- Normally the initial token on entry is RANGE, but in some
2206 -- error conditions, the range token was missing and control is
2207 -- passed with Token pointing to first token of the first expression.
2209 -- Error recovery: cannot raise Error_Resync
2211 function P_Signed_Integer_Type_Definition return Node_Id is
2212 Typedef_Node : Node_Id;
2213 Expr_Node : Node_Id;
2215 begin
2216 Typedef_Node := New_Node (N_Signed_Integer_Type_Definition, Token_Ptr);
2218 if Token = Tok_Range then
2219 Scan; -- past RANGE
2220 end if;
2222 Expr_Node := P_Expression;
2223 Check_Simple_Expression (Expr_Node);
2224 Set_Low_Bound (Typedef_Node, Expr_Node);
2225 T_Dot_Dot;
2226 Expr_Node := P_Expression;
2227 Check_Simple_Expression (Expr_Node);
2228 Set_High_Bound (Typedef_Node, Expr_Node);
2229 return Typedef_Node;
2230 end P_Signed_Integer_Type_Definition;
2232 ------------------------------------
2233 -- 3.5.4 Modular Type Definition --
2234 ------------------------------------
2236 -- MODULAR_TYPE_DEFINITION ::= mod static_EXPRESSION
2238 -- The caller has checked that the initial token is MOD
2240 -- Error recovery: cannot raise Error_Resync
2242 function P_Modular_Type_Definition return Node_Id is
2243 Typedef_Node : Node_Id;
2245 begin
2246 if Ada_Version = Ada_83 then
2247 Error_Msg_SC ("(Ada 83): modular types not allowed");
2248 end if;
2250 Typedef_Node := New_Node (N_Modular_Type_Definition, Token_Ptr);
2251 Scan; -- past MOD
2252 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2254 -- Handle mod L..R cleanly
2256 if Token = Tok_Dot_Dot then
2257 Error_Msg_SC ("range not allowed for modular type");
2258 Scan; -- past ..
2259 Set_Expression (Typedef_Node, P_Expression_No_Right_Paren);
2260 end if;
2262 return Typedef_Node;
2263 end P_Modular_Type_Definition;
2265 ---------------------------------
2266 -- 3.5.6 Real Type Definition --
2267 ---------------------------------
2269 -- Parsed by P_Type_Declaration (3.2.1)
2271 --------------------------------------
2272 -- 3.5.7 Floating Point Definition --
2273 --------------------------------------
2275 -- FLOATING_POINT_DEFINITION ::=
2276 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2278 -- Note: In Ada-83, the EXPRESSION must be a SIMPLE_EXPRESSION
2280 -- The caller has checked that the initial token is DIGITS
2282 -- Error recovery: cannot raise Error_Resync
2284 function P_Floating_Point_Definition return Node_Id is
2285 Digits_Loc : constant Source_Ptr := Token_Ptr;
2286 Def_Node : Node_Id;
2287 Expr_Node : Node_Id;
2289 begin
2290 Scan; -- past DIGITS
2291 Expr_Node := P_Expression_No_Right_Paren;
2292 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2294 -- Handle decimal fixed-point defn with DIGITS/DELTA in wrong order
2296 if Token = Tok_Delta then
2297 Error_Msg_SC ("|DELTA must come before DIGITS");
2298 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Digits_Loc);
2299 Scan; -- past DELTA
2300 Set_Delta_Expression (Def_Node, P_Expression_No_Right_Paren);
2302 -- OK floating-point definition
2304 else
2305 Def_Node := New_Node (N_Floating_Point_Definition, Digits_Loc);
2306 end if;
2308 Set_Digits_Expression (Def_Node, Expr_Node);
2309 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2310 return Def_Node;
2311 end P_Floating_Point_Definition;
2313 -------------------------------------
2314 -- 3.5.7 Real Range Specification --
2315 -------------------------------------
2317 -- REAL_RANGE_SPECIFICATION ::=
2318 -- range static_SIMPLE_EXPRESSION .. static_SIMPLE_EXPRESSION
2320 -- Error recovery: cannot raise Error_Resync
2322 function P_Real_Range_Specification_Opt return Node_Id is
2323 Specification_Node : Node_Id;
2324 Expr_Node : Node_Id;
2326 begin
2327 if Token = Tok_Range then
2328 Specification_Node :=
2329 New_Node (N_Real_Range_Specification, Token_Ptr);
2330 Scan; -- past RANGE
2331 Expr_Node := P_Expression_No_Right_Paren;
2332 Check_Simple_Expression (Expr_Node);
2333 Set_Low_Bound (Specification_Node, Expr_Node);
2334 T_Dot_Dot;
2335 Expr_Node := P_Expression_No_Right_Paren;
2336 Check_Simple_Expression (Expr_Node);
2337 Set_High_Bound (Specification_Node, Expr_Node);
2338 return Specification_Node;
2339 else
2340 return Empty;
2341 end if;
2342 end P_Real_Range_Specification_Opt;
2344 -----------------------------------
2345 -- 3.5.9 Fixed Point Definition --
2346 -----------------------------------
2348 -- FIXED_POINT_DEFINITION ::=
2349 -- ORDINARY_FIXED_POINT_DEFINITION | DECIMAL_FIXED_POINT_DEFINITION
2351 -- ORDINARY_FIXED_POINT_DEFINITION ::=
2352 -- delta static_EXPRESSION REAL_RANGE_SPECIFICATION
2354 -- DECIMAL_FIXED_POINT_DEFINITION ::=
2355 -- delta static_EXPRESSION
2356 -- digits static_EXPRESSION [REAL_RANGE_SPECIFICATION]
2358 -- The caller has checked that the initial token is DELTA
2360 -- Error recovery: cannot raise Error_Resync
2362 function P_Fixed_Point_Definition return Node_Id is
2363 Delta_Node : Node_Id;
2364 Delta_Loc : Source_Ptr;
2365 Def_Node : Node_Id;
2366 Expr_Node : Node_Id;
2368 begin
2369 Delta_Loc := Token_Ptr;
2370 Scan; -- past DELTA
2371 Delta_Node := P_Expression_No_Right_Paren;
2372 Check_Simple_Expression_In_Ada_83 (Delta_Node);
2374 if Token = Tok_Digits then
2375 if Ada_Version = Ada_83 then
2376 Error_Msg_SC ("(Ada 83) decimal fixed type not allowed!");
2377 end if;
2379 Def_Node := New_Node (N_Decimal_Fixed_Point_Definition, Delta_Loc);
2380 Scan; -- past DIGITS
2381 Expr_Node := P_Expression_No_Right_Paren;
2382 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2383 Set_Digits_Expression (Def_Node, Expr_Node);
2385 else
2386 Def_Node := New_Node (N_Ordinary_Fixed_Point_Definition, Delta_Loc);
2388 -- Range is required in ordinary fixed point case
2390 if Token /= Tok_Range then
2391 Error_Msg_AP ("range must be given for fixed-point type");
2392 T_Range;
2393 end if;
2394 end if;
2396 Set_Delta_Expression (Def_Node, Delta_Node);
2397 Set_Real_Range_Specification (Def_Node, P_Real_Range_Specification_Opt);
2398 return Def_Node;
2399 end P_Fixed_Point_Definition;
2401 --------------------------------------------
2402 -- 3.5.9 Ordinary Fixed Point Definition --
2403 --------------------------------------------
2405 -- Parsed by P_Fixed_Point_Definition (3.5.9)
2407 -------------------------------------------
2408 -- 3.5.9 Decimal Fixed Point Definition --
2409 -------------------------------------------
2411 -- Parsed by P_Decimal_Point_Definition (3.5.9)
2413 ------------------------------
2414 -- 3.5.9 Digits Constraint --
2415 ------------------------------
2417 -- DIGITS_CONSTRAINT ::=
2418 -- digits static_EXPRESSION [RANGE_CONSTRAINT]
2420 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2422 -- The caller has checked that the initial token is DIGITS
2424 function P_Digits_Constraint return Node_Id is
2425 Constraint_Node : Node_Id;
2426 Expr_Node : Node_Id;
2428 begin
2429 Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
2430 Scan; -- past DIGITS
2431 Expr_Node := P_Expression;
2432 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2433 Set_Digits_Expression (Constraint_Node, Expr_Node);
2435 if Token = Tok_Range then
2436 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2437 end if;
2439 return Constraint_Node;
2440 end P_Digits_Constraint;
2442 -----------------------------
2443 -- 3.5.9 Delta Constraint --
2444 -----------------------------
2446 -- DELTA CONSTRAINT ::= DELTA STATIC_EXPRESSION [RANGE_CONSTRAINT]
2448 -- Note: this is an obsolescent feature in Ada 95 (I.3)
2450 -- Note: in Ada 83, the EXPRESSION must be a SIMPLE_EXPRESSION
2452 -- The caller has checked that the initial token is DELTA
2454 -- Error recovery: cannot raise Error_Resync
2456 function P_Delta_Constraint return Node_Id is
2457 Constraint_Node : Node_Id;
2458 Expr_Node : Node_Id;
2460 begin
2461 Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
2462 Scan; -- past DELTA
2463 Expr_Node := P_Expression;
2464 Check_Simple_Expression_In_Ada_83 (Expr_Node);
2465 Set_Delta_Expression (Constraint_Node, Expr_Node);
2467 if Token = Tok_Range then
2468 Set_Range_Constraint (Constraint_Node, P_Range_Constraint);
2469 end if;
2471 return Constraint_Node;
2472 end P_Delta_Constraint;
2474 --------------------------------
2475 -- 3.6 Array Type Definition --
2476 --------------------------------
2478 -- ARRAY_TYPE_DEFINITION ::=
2479 -- UNCONSTRAINED_ARRAY_DEFINITION | CONSTRAINED_ARRAY_DEFINITION
2481 -- UNCONSTRAINED_ARRAY_DEFINITION ::=
2482 -- array (INDEX_SUBTYPE_DEFINITION {, INDEX_SUBTYPE_DEFINITION}) of
2483 -- COMPONENT_DEFINITION
2485 -- INDEX_SUBTYPE_DEFINITION ::= SUBTYPE_MARK range <>
2487 -- CONSTRAINED_ARRAY_DEFINITION ::=
2488 -- array (DISCRETE_SUBTYPE_DEFINITION {, DISCRETE_SUBTYPE_DEFINITION}) of
2489 -- COMPONENT_DEFINITION
2491 -- DISCRETE_SUBTYPE_DEFINITION ::=
2492 -- DISCRETE_SUBTYPE_INDICATION | RANGE
2494 -- COMPONENT_DEFINITION ::=
2495 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
2497 -- The caller has checked that the initial token is ARRAY
2499 -- Error recovery: can raise Error_Resync
2501 function P_Array_Type_Definition return Node_Id is
2502 Array_Loc : Source_Ptr;
2503 CompDef_Node : Node_Id;
2504 Def_Node : Node_Id;
2505 Not_Null_Present : Boolean := False;
2506 Subs_List : List_Id;
2507 Scan_State : Saved_Scan_State;
2508 Aliased_Present : Boolean := False;
2510 begin
2511 Array_Loc := Token_Ptr;
2512 Scan; -- past ARRAY
2513 Subs_List := New_List;
2514 T_Left_Paren;
2516 -- It's quite tricky to disentangle these two possibilities, so we do
2517 -- a prescan to determine which case we have and then reset the scan.
2518 -- The prescan skips past possible subtype mark tokens.
2520 Save_Scan_State (Scan_State); -- just after paren
2522 while Token in Token_Class_Desig or else
2523 Token = Tok_Dot or else
2524 Token = Tok_Apostrophe -- because of 'BASE, 'CLASS
2525 loop
2526 Scan;
2527 end loop;
2529 -- If we end up on RANGE <> then we have the unconstrained case. We
2530 -- will also allow the RANGE to be omitted, just to improve error
2531 -- handling for a case like array (integer <>) of integer;
2533 Scan; -- past possible RANGE or <>
2535 if (Prev_Token = Tok_Range and then Token = Tok_Box) or else
2536 Prev_Token = Tok_Box
2537 then
2538 Def_Node := New_Node (N_Unconstrained_Array_Definition, Array_Loc);
2539 Restore_Scan_State (Scan_State); -- to first subtype mark
2541 loop
2542 Append (P_Subtype_Mark_Resync, Subs_List);
2543 T_Range;
2544 T_Box;
2545 exit when Token = Tok_Right_Paren or else Token = Tok_Of;
2546 T_Comma;
2547 end loop;
2549 Set_Subtype_Marks (Def_Node, Subs_List);
2551 else
2552 Def_Node := New_Node (N_Constrained_Array_Definition, Array_Loc);
2553 Restore_Scan_State (Scan_State); -- to first discrete range
2555 loop
2556 Append (P_Discrete_Subtype_Definition, Subs_List);
2557 exit when not Comma_Present;
2558 end loop;
2560 Set_Discrete_Subtype_Definitions (Def_Node, Subs_List);
2561 end if;
2563 T_Right_Paren;
2564 T_Of;
2566 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
2568 if Token_Name = Name_Aliased then
2569 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
2570 end if;
2572 if Token = Tok_Aliased then
2573 Aliased_Present := True;
2574 Scan; -- past ALIASED
2575 end if;
2577 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
2579 -- Ada 2005 (AI-230): Access Definition case
2581 if Token = Tok_Access then
2582 if Ada_Version < Ada_05 then
2583 Error_Msg_SP
2584 ("generalized use of anonymous access types " &
2585 "is an Ada 2005 extension");
2586 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
2587 end if;
2589 if Aliased_Present then
2590 Error_Msg_SP ("ALIASED not allowed here");
2591 end if;
2593 Set_Subtype_Indication (CompDef_Node, Empty);
2594 Set_Aliased_Present (CompDef_Node, False);
2595 Set_Access_Definition (CompDef_Node,
2596 P_Access_Definition (Not_Null_Present));
2597 else
2599 Set_Access_Definition (CompDef_Node, Empty);
2600 Set_Aliased_Present (CompDef_Node, Aliased_Present);
2601 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
2602 Set_Subtype_Indication (CompDef_Node,
2603 P_Subtype_Indication (Not_Null_Present));
2604 end if;
2606 Set_Component_Definition (Def_Node, CompDef_Node);
2608 return Def_Node;
2609 end P_Array_Type_Definition;
2611 -----------------------------------------
2612 -- 3.6 Unconstrained Array Definition --
2613 -----------------------------------------
2615 -- Parsed by P_Array_Type_Definition (3.6)
2617 ---------------------------------------
2618 -- 3.6 Constrained Array Definition --
2619 ---------------------------------------
2621 -- Parsed by P_Array_Type_Definition (3.6)
2623 --------------------------------------
2624 -- 3.6 Discrete Subtype Definition --
2625 --------------------------------------
2627 -- DISCRETE_SUBTYPE_DEFINITION ::=
2628 -- discrete_SUBTYPE_INDICATION | RANGE
2630 -- Note: the discrete subtype definition appearing in a constrained
2631 -- array definition is parsed by P_Array_Type_Definition (3.6)
2633 -- Error recovery: cannot raise Error_Resync
2635 function P_Discrete_Subtype_Definition return Node_Id is
2636 begin
2637 -- The syntax of a discrete subtype definition is identical to that
2638 -- of a discrete range, so we simply share the same parsing code.
2640 return P_Discrete_Range;
2641 end P_Discrete_Subtype_Definition;
2643 -------------------------------
2644 -- 3.6 Component Definition --
2645 -------------------------------
2647 -- For the array case, parsed by P_Array_Type_Definition (3.6)
2648 -- For the record case, parsed by P_Component_Declaration (3.8)
2650 -----------------------------
2651 -- 3.6.1 Index Constraint --
2652 -----------------------------
2654 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2656 ---------------------------
2657 -- 3.6.1 Discrete Range --
2658 ---------------------------
2660 -- DISCRETE_RANGE ::= discrete_SUBTYPE_INDICATION | RANGE
2662 -- The possible forms for a discrete range are:
2664 -- Subtype_Mark (SUBTYPE_INDICATION, 3.2.2)
2665 -- Subtype_Mark range Range (SUBTYPE_INDICATION, 3.2.2)
2666 -- Range_Attribute (RANGE, 3.5)
2667 -- Simple_Expression .. Simple_Expression (RANGE, 3.5)
2669 -- Error recovery: cannot raise Error_Resync
2671 function P_Discrete_Range return Node_Id is
2672 Expr_Node : Node_Id;
2673 Range_Node : Node_Id;
2675 begin
2676 Expr_Node := P_Simple_Expression_Or_Range_Attribute;
2678 if Expr_Form = EF_Range_Attr then
2679 return Expr_Node;
2681 elsif Token = Tok_Range then
2682 if Expr_Form /= EF_Simple_Name then
2683 Error_Msg_SC ("range must be preceded by subtype mark");
2684 end if;
2686 return P_Subtype_Indication (Expr_Node);
2688 -- Check Expression .. Expression case
2690 elsif Token = Tok_Dot_Dot then
2691 Range_Node := New_Node (N_Range, Token_Ptr);
2692 Set_Low_Bound (Range_Node, Expr_Node);
2693 Scan; -- past ..
2694 Expr_Node := P_Expression;
2695 Check_Simple_Expression (Expr_Node);
2696 Set_High_Bound (Range_Node, Expr_Node);
2697 return Range_Node;
2699 -- Otherwise we must have a subtype mark
2701 elsif Expr_Form = EF_Simple_Name then
2702 return Expr_Node;
2704 -- If incorrect, complain that we expect ..
2706 else
2707 T_Dot_Dot;
2708 return Expr_Node;
2709 end if;
2710 end P_Discrete_Range;
2712 ----------------------------
2713 -- 3.7 Discriminant Part --
2714 ----------------------------
2716 -- DISCRIMINANT_PART ::=
2717 -- UNKNOWN_DISCRIMINANT_PART
2718 -- | KNOWN_DISCRIMINANT_PART
2720 -- A discriminant part is parsed by P_Known_Discriminant_Part_Opt (3.7)
2721 -- or P_Unknown_Discriminant_Part (3.7), since we know which we want.
2723 ------------------------------------
2724 -- 3.7 Unknown Discriminant Part --
2725 ------------------------------------
2727 -- UNKNOWN_DISCRIMINANT_PART ::= (<>)
2729 -- If no unknown discriminant part is present, then False is returned,
2730 -- otherwise the unknown discriminant is scanned out and True is returned.
2732 -- Error recovery: cannot raise Error_Resync
2734 function P_Unknown_Discriminant_Part_Opt return Boolean is
2735 Scan_State : Saved_Scan_State;
2737 begin
2738 -- If <> right now, then this is missing left paren
2740 if Token = Tok_Box then
2741 U_Left_Paren;
2743 -- If not <> or left paren, then definitely no box
2745 elsif Token /= Tok_Left_Paren then
2746 return False;
2748 -- Left paren, so might be a box after it
2750 else
2751 Save_Scan_State (Scan_State);
2752 Scan; -- past the left paren
2754 if Token /= Tok_Box then
2755 Restore_Scan_State (Scan_State);
2756 return False;
2757 end if;
2758 end if;
2760 -- We are now pointing to the box
2762 if Ada_Version = Ada_83 then
2763 Error_Msg_SC ("(Ada 83) unknown discriminant not allowed!");
2764 end if;
2766 Scan; -- past the box
2767 U_Right_Paren; -- must be followed by right paren
2768 return True;
2769 end P_Unknown_Discriminant_Part_Opt;
2771 ----------------------------------
2772 -- 3.7 Known Discriminant Part --
2773 ----------------------------------
2775 -- KNOWN_DISCRIMINANT_PART ::=
2776 -- (DISCRIMINANT_SPECIFICATION {; DISCRIMINANT_SPECIFICATION})
2778 -- DISCRIMINANT_SPECIFICATION ::=
2779 -- DEFINING_IDENTIFIER_LIST : [NULL_EXCLUSION] SUBTYPE_MARK
2780 -- [:= DEFAULT_EXPRESSION]
2781 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
2782 -- [:= DEFAULT_EXPRESSION]
2784 -- If no known discriminant part is present, then No_List is returned
2786 -- Error recovery: cannot raise Error_Resync
2788 function P_Known_Discriminant_Part_Opt return List_Id is
2789 Specification_Node : Node_Id;
2790 Specification_List : List_Id;
2791 Ident_Sloc : Source_Ptr;
2792 Scan_State : Saved_Scan_State;
2793 Num_Idents : Nat;
2794 Not_Null_Present : Boolean;
2795 Ident : Nat;
2797 Idents : array (Int range 1 .. 4096) of Entity_Id;
2798 -- This array holds the list of defining identifiers. The upper bound
2799 -- of 4096 is intended to be essentially infinite, and we do not even
2800 -- bother to check for it being exceeded.
2802 begin
2803 if Token = Tok_Left_Paren then
2804 Specification_List := New_List;
2805 Scan; -- past (
2806 P_Pragmas_Misplaced;
2808 Specification_Loop : loop
2810 Ident_Sloc := Token_Ptr;
2811 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
2812 Num_Idents := 1;
2814 while Comma_Present loop
2815 Num_Idents := Num_Idents + 1;
2816 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
2817 end loop;
2819 -- If there are multiple identifiers, we repeatedly scan the
2820 -- type and initialization expression information by resetting
2821 -- the scan pointer (so that we get completely separate trees
2822 -- for each occurrence).
2824 if Num_Idents > 1 then
2825 Save_Scan_State (Scan_State);
2826 end if;
2828 T_Colon;
2830 -- Loop through defining identifiers in list
2832 Ident := 1;
2833 Ident_Loop : loop
2834 Specification_Node :=
2835 New_Node (N_Discriminant_Specification, Ident_Sloc);
2836 Set_Defining_Identifier (Specification_Node, Idents (Ident));
2837 Not_Null_Present := -- Ada 2005 (AI-231, AI-447)
2838 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
2840 if Token = Tok_Access then
2841 if Ada_Version = Ada_83 then
2842 Error_Msg_SC
2843 ("(Ada 83) access discriminant not allowed!");
2844 end if;
2846 Set_Discriminant_Type
2847 (Specification_Node,
2848 P_Access_Definition (Not_Null_Present));
2849 else
2851 Set_Discriminant_Type
2852 (Specification_Node, P_Subtype_Mark);
2853 No_Constraint;
2854 Set_Null_Exclusion_Present -- Ada 2005 (AI-231)
2855 (Specification_Node, Not_Null_Present);
2856 end if;
2858 Set_Expression
2859 (Specification_Node, Init_Expr_Opt (True));
2861 if Ident > 1 then
2862 Set_Prev_Ids (Specification_Node, True);
2863 end if;
2865 if Ident < Num_Idents then
2866 Set_More_Ids (Specification_Node, True);
2867 end if;
2869 Append (Specification_Node, Specification_List);
2870 exit Ident_Loop when Ident = Num_Idents;
2871 Ident := Ident + 1;
2872 Restore_Scan_State (Scan_State);
2873 T_Colon;
2874 end loop Ident_Loop;
2876 exit Specification_Loop when Token /= Tok_Semicolon;
2877 Scan; -- past ;
2878 P_Pragmas_Misplaced;
2879 end loop Specification_Loop;
2881 T_Right_Paren;
2882 return Specification_List;
2884 else
2885 return No_List;
2886 end if;
2887 end P_Known_Discriminant_Part_Opt;
2889 -------------------------------------
2890 -- 3.7 Discriminant Specification --
2891 -------------------------------------
2893 -- Parsed by P_Known_Discriminant_Part_Opt (3.7)
2895 -----------------------------
2896 -- 3.7 Default Expression --
2897 -----------------------------
2899 -- Always parsed (simply as an Expression) by the parent construct
2901 ------------------------------------
2902 -- 3.7.1 Discriminant Constraint --
2903 ------------------------------------
2905 -- Parsed by P_Index_Or_Discriminant_Constraint (3.7.1)
2907 --------------------------------------------------------
2908 -- 3.7.1 Index or Discriminant Constraint (also 3.6) --
2909 --------------------------------------------------------
2911 -- DISCRIMINANT_CONSTRAINT ::=
2912 -- (DISCRIMINANT_ASSOCIATION {, DISCRIMINANT_ASSOCIATION})
2914 -- DISCRIMINANT_ASSOCIATION ::=
2915 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
2916 -- EXPRESSION
2918 -- This routine parses either an index or a discriminant constraint. As
2919 -- is clear from the above grammar, it is often possible to clearly
2920 -- determine which of the two possibilities we have, but there are
2921 -- cases (those in which we have a series of expressions of the same
2922 -- syntactic form as subtype indications), where we cannot tell. Since
2923 -- this means that in any case the semantic phase has to distinguish
2924 -- between the two, there is not much point in the parser trying to
2925 -- distinguish even those cases where the difference is clear. In any
2926 -- case, if we have a situation like:
2928 -- (A => 123, 235 .. 500)
2930 -- it is not clear which of the two items is the wrong one, better to
2931 -- let the semantic phase give a clear message. Consequently, this
2932 -- routine in general returns a list of items which can be either
2933 -- discrete ranges or discriminant associations.
2935 -- The caller has checked that the initial token is a left paren
2937 -- Error recovery: can raise Error_Resync
2939 function P_Index_Or_Discriminant_Constraint return Node_Id is
2940 Scan_State : Saved_Scan_State;
2941 Constr_Node : Node_Id;
2942 Constr_List : List_Id;
2943 Expr_Node : Node_Id;
2944 Result_Node : Node_Id;
2946 begin
2947 Result_Node := New_Node (N_Index_Or_Discriminant_Constraint, Token_Ptr);
2948 Scan; -- past (
2949 Constr_List := New_List;
2950 Set_Constraints (Result_Node, Constr_List);
2952 -- The two syntactic forms are a little mixed up, so what we are doing
2953 -- here is looking at the first entry to determine which case we have
2955 -- A discriminant constraint is a list of discriminant associations,
2956 -- which have one of the following possible forms:
2958 -- Expression
2959 -- Id => Expression
2960 -- Id | Id | .. | Id => Expression
2962 -- An index constraint is a list of discrete ranges which have one
2963 -- of the following possible forms:
2965 -- Subtype_Mark
2966 -- Subtype_Mark range Range
2967 -- Range_Attribute
2968 -- Simple_Expression .. Simple_Expression
2970 -- Loop through discriminants in list
2972 loop
2973 -- Check cases of Id => Expression or Id | Id => Expression
2975 if Token = Tok_Identifier then
2976 Save_Scan_State (Scan_State); -- at Id
2977 Scan; -- past Id
2979 if Token = Tok_Arrow or else Token = Tok_Vertical_Bar then
2980 Restore_Scan_State (Scan_State); -- to Id
2981 Append (P_Discriminant_Association, Constr_List);
2982 goto Loop_Continue;
2983 else
2984 Restore_Scan_State (Scan_State); -- to Id
2985 end if;
2986 end if;
2988 -- Otherwise scan out an expression and see what we have got
2990 Expr_Node := P_Expression_Or_Range_Attribute;
2992 if Expr_Form = EF_Range_Attr then
2993 Append (Expr_Node, Constr_List);
2995 elsif Token = Tok_Range then
2996 if Expr_Form /= EF_Simple_Name then
2997 Error_Msg_SC ("subtype mark required before RANGE");
2998 end if;
3000 Append (P_Subtype_Indication (Expr_Node), Constr_List);
3001 goto Loop_Continue;
3003 -- Check Simple_Expression .. Simple_Expression case
3005 elsif Token = Tok_Dot_Dot then
3006 Check_Simple_Expression (Expr_Node);
3007 Constr_Node := New_Node (N_Range, Token_Ptr);
3008 Set_Low_Bound (Constr_Node, Expr_Node);
3009 Scan; -- past ..
3010 Expr_Node := P_Expression;
3011 Check_Simple_Expression (Expr_Node);
3012 Set_High_Bound (Constr_Node, Expr_Node);
3013 Append (Constr_Node, Constr_List);
3014 goto Loop_Continue;
3016 -- Case of an expression which could be either form
3018 else
3019 Append (Expr_Node, Constr_List);
3020 goto Loop_Continue;
3021 end if;
3023 -- Here with a single entry scanned
3025 <<Loop_Continue>>
3026 exit when not Comma_Present;
3028 end loop;
3030 T_Right_Paren;
3031 return Result_Node;
3032 end P_Index_Or_Discriminant_Constraint;
3034 -------------------------------------
3035 -- 3.7.1 Discriminant Association --
3036 -------------------------------------
3038 -- DISCRIMINANT_ASSOCIATION ::=
3039 -- [discriminant_SELECTOR_NAME {| discriminant_SELECTOR_NAME} =>]
3040 -- EXPRESSION
3042 -- This routine is used only when the name list is present and the caller
3043 -- has already checked this (by scanning ahead and repositioning the
3044 -- scan).
3046 -- Error_Recovery: cannot raise Error_Resync;
3048 function P_Discriminant_Association return Node_Id is
3049 Discr_Node : Node_Id;
3050 Names_List : List_Id;
3051 Ident_Sloc : Source_Ptr;
3053 begin
3054 Ident_Sloc := Token_Ptr;
3055 Names_List := New_List;
3057 loop
3058 Append (P_Identifier (C_Vertical_Bar_Arrow), Names_List);
3059 exit when Token /= Tok_Vertical_Bar;
3060 Scan; -- past |
3061 end loop;
3063 Discr_Node := New_Node (N_Discriminant_Association, Ident_Sloc);
3064 Set_Selector_Names (Discr_Node, Names_List);
3065 TF_Arrow;
3066 Set_Expression (Discr_Node, P_Expression);
3067 return Discr_Node;
3068 end P_Discriminant_Association;
3070 ---------------------------------
3071 -- 3.8 Record Type Definition --
3072 ---------------------------------
3074 -- RECORD_TYPE_DEFINITION ::=
3075 -- [[abstract] tagged] [limited] RECORD_DEFINITION
3077 -- There is no node in the tree for a record type definition. Instead
3078 -- a record definition node appears, with possible Abstract_Present,
3079 -- Tagged_Present, and Limited_Present flags set appropriately.
3081 ----------------------------
3082 -- 3.8 Record Definition --
3083 ----------------------------
3085 -- RECORD_DEFINITION ::=
3086 -- record
3087 -- COMPONENT_LIST
3088 -- end record
3089 -- | null record
3091 -- Note: in the case where a record definition node is used to represent
3092 -- a record type definition, the caller sets the Tagged_Present and
3093 -- Limited_Present flags in the resulting N_Record_Definition node as
3094 -- required.
3096 -- Note that the RECORD token at the start may be missing in certain
3097 -- error situations, so this function is expected to post the error
3099 -- Error recovery: can raise Error_Resync
3101 function P_Record_Definition return Node_Id is
3102 Rec_Node : Node_Id;
3104 begin
3105 Rec_Node := New_Node (N_Record_Definition, Token_Ptr);
3107 -- Null record case
3109 if Token = Tok_Null then
3110 Scan; -- past NULL
3111 T_Record;
3112 Set_Null_Present (Rec_Node, True);
3114 -- Catch incomplete declaration to prevent cascaded errors, see
3115 -- ACATS B393002 for an example.
3117 elsif Token = Tok_Semicolon then
3118 Error_Msg_AP ("missing record definition");
3120 -- Case starting with RECORD keyword. Build scope stack entry. For the
3121 -- column, we use the first non-blank character on the line, to deal
3122 -- with situations such as:
3124 -- type X is record
3125 -- ...
3126 -- end record;
3128 -- which is not official RM indentation, but is not uncommon usage, and
3129 -- in particular is standard GNAT coding style, so handle it nicely.
3131 else
3132 Push_Scope_Stack;
3133 Scope.Table (Scope.Last).Etyp := E_Record;
3134 Scope.Table (Scope.Last).Ecol := Start_Column;
3135 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3136 Scope.Table (Scope.Last).Labl := Error;
3137 Scope.Table (Scope.Last).Junk := (Token /= Tok_Record);
3139 T_Record;
3141 Set_Component_List (Rec_Node, P_Component_List);
3143 loop
3144 exit when Check_End;
3145 Discard_Junk_Node (P_Component_List);
3146 end loop;
3147 end if;
3149 return Rec_Node;
3150 end P_Record_Definition;
3152 -------------------------
3153 -- 3.8 Component List --
3154 -------------------------
3156 -- COMPONENT_LIST ::=
3157 -- COMPONENT_ITEM {COMPONENT_ITEM}
3158 -- | {COMPONENT_ITEM} VARIANT_PART
3159 -- | null;
3161 -- Error recovery: cannot raise Error_Resync
3163 function P_Component_List return Node_Id is
3164 Component_List_Node : Node_Id;
3165 Decls_List : List_Id;
3166 Scan_State : Saved_Scan_State;
3168 begin
3169 Component_List_Node := New_Node (N_Component_List, Token_Ptr);
3170 Decls_List := New_List;
3172 if Token = Tok_Null then
3173 Scan; -- past NULL
3174 TF_Semicolon;
3175 P_Pragmas_Opt (Decls_List);
3176 Set_Null_Present (Component_List_Node, True);
3177 return Component_List_Node;
3179 else
3180 P_Pragmas_Opt (Decls_List);
3182 if Token /= Tok_Case then
3183 Component_Scan_Loop : loop
3184 P_Component_Items (Decls_List);
3185 P_Pragmas_Opt (Decls_List);
3187 exit Component_Scan_Loop when Token = Tok_End
3188 or else Token = Tok_Case
3189 or else Token = Tok_When;
3191 -- We are done if we do not have an identifier. However, if
3192 -- we have a misspelled reserved identifier that is in a column
3193 -- to the right of the record definition, we will treat it as
3194 -- an identifier. It turns out to be too dangerous in practice
3195 -- to accept such a mis-spelled identifier which does not have
3196 -- this additional clue that confirms the incorrect spelling.
3198 if Token /= Tok_Identifier then
3199 if Start_Column > Scope.Table (Scope.Last).Ecol
3200 and then Is_Reserved_Identifier
3201 then
3202 Save_Scan_State (Scan_State); -- at reserved id
3203 Scan; -- possible reserved id
3205 if Token = Tok_Comma or else Token = Tok_Colon then
3206 Restore_Scan_State (Scan_State);
3207 Scan_Reserved_Identifier (Force_Msg => True);
3209 -- Note reserved identifier used as field name after
3210 -- all because not followed by colon or comma
3212 else
3213 Restore_Scan_State (Scan_State);
3214 exit Component_Scan_Loop;
3215 end if;
3217 -- Non-identifier that definitely was not reserved id
3219 else
3220 exit Component_Scan_Loop;
3221 end if;
3222 end if;
3223 end loop Component_Scan_Loop;
3224 end if;
3226 if Token = Tok_Case then
3227 Set_Variant_Part (Component_List_Node, P_Variant_Part);
3229 -- Check for junk after variant part
3231 if Token = Tok_Identifier then
3232 Save_Scan_State (Scan_State);
3233 Scan; -- past identifier
3235 if Token = Tok_Colon then
3236 Restore_Scan_State (Scan_State);
3237 Error_Msg_SC ("component may not follow variant part");
3238 Discard_Junk_Node (P_Component_List);
3240 elsif Token = Tok_Case then
3241 Restore_Scan_State (Scan_State);
3242 Error_Msg_SC ("only one variant part allowed in a record");
3243 Discard_Junk_Node (P_Component_List);
3245 else
3246 Restore_Scan_State (Scan_State);
3247 end if;
3248 end if;
3249 end if;
3250 end if;
3252 Set_Component_Items (Component_List_Node, Decls_List);
3253 return Component_List_Node;
3254 end P_Component_List;
3256 -------------------------
3257 -- 3.8 Component Item --
3258 -------------------------
3260 -- COMPONENT_ITEM ::= COMPONENT_DECLARATION | REPRESENTATION_CLAUSE
3262 -- COMPONENT_DECLARATION ::=
3263 -- DEFINING_IDENTIFIER_LIST : COMPONENT_DEFINITION
3264 -- [:= DEFAULT_EXPRESSION];
3266 -- COMPONENT_DEFINITION ::=
3267 -- [aliased] [NULL_EXCLUSION] SUBTYPE_INDICATION | ACCESS_DEFINITION
3269 -- Error recovery: cannot raise Error_Resync, if an error occurs,
3270 -- the scan is positioned past the following semicolon.
3272 -- Note: we do not yet allow representation clauses to appear as component
3273 -- items, do we need to add this capability sometime in the future ???
3275 procedure P_Component_Items (Decls : List_Id) is
3276 Aliased_Present : Boolean := False;
3277 CompDef_Node : Node_Id;
3278 Decl_Node : Node_Id;
3279 Scan_State : Saved_Scan_State;
3280 Not_Null_Present : Boolean := False;
3281 Num_Idents : Nat;
3282 Ident : Nat;
3283 Ident_Sloc : Source_Ptr;
3285 Idents : array (Int range 1 .. 4096) of Entity_Id;
3286 -- This array holds the list of defining identifiers. The upper bound
3287 -- of 4096 is intended to be essentially infinite, and we do not even
3288 -- bother to check for it being exceeded.
3290 begin
3291 if Token /= Tok_Identifier then
3292 Error_Msg_SC ("component declaration expected");
3293 Resync_Past_Semicolon;
3294 return;
3295 end if;
3297 Ident_Sloc := Token_Ptr;
3298 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
3299 Num_Idents := 1;
3301 while Comma_Present loop
3302 Num_Idents := Num_Idents + 1;
3303 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
3304 end loop;
3306 -- If there are multiple identifiers, we repeatedly scan the
3307 -- type and initialization expression information by resetting
3308 -- the scan pointer (so that we get completely separate trees
3309 -- for each occurrence).
3311 if Num_Idents > 1 then
3312 Save_Scan_State (Scan_State);
3313 end if;
3315 T_Colon;
3317 -- Loop through defining identifiers in list
3319 Ident := 1;
3320 Ident_Loop : loop
3322 -- The following block is present to catch Error_Resync
3323 -- which causes the parse to be reset past the semicolon
3325 begin
3326 Decl_Node := New_Node (N_Component_Declaration, Ident_Sloc);
3327 Set_Defining_Identifier (Decl_Node, Idents (Ident));
3329 if Token = Tok_Constant then
3330 Error_Msg_SC ("constant components are not permitted");
3331 Scan;
3332 end if;
3334 CompDef_Node := New_Node (N_Component_Definition, Token_Ptr);
3336 if Token_Name = Name_Aliased then
3337 Check_95_Keyword (Tok_Aliased, Tok_Identifier);
3338 end if;
3340 if Token = Tok_Aliased then
3341 Aliased_Present := True;
3342 Scan; -- past ALIASED
3343 end if;
3345 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231/AI-254)
3347 -- Ada 2005 (AI-230): Access Definition case
3349 if Token = Tok_Access then
3350 if Ada_Version < Ada_05 then
3351 Error_Msg_SP
3352 ("generalized use of anonymous access types " &
3353 "is an Ada 2005 extension");
3354 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3355 end if;
3357 if Aliased_Present then
3358 Error_Msg_SP ("ALIASED not allowed here");
3359 end if;
3361 Set_Subtype_Indication (CompDef_Node, Empty);
3362 Set_Aliased_Present (CompDef_Node, False);
3363 Set_Access_Definition (CompDef_Node,
3364 P_Access_Definition (Not_Null_Present));
3365 else
3367 Set_Access_Definition (CompDef_Node, Empty);
3368 Set_Aliased_Present (CompDef_Node, Aliased_Present);
3369 Set_Null_Exclusion_Present (CompDef_Node, Not_Null_Present);
3371 if Token = Tok_Array then
3372 Error_Msg_SC
3373 ("anonymous arrays not allowed as components");
3374 raise Error_Resync;
3375 end if;
3377 Set_Subtype_Indication (CompDef_Node,
3378 P_Subtype_Indication (Not_Null_Present));
3379 end if;
3381 Set_Component_Definition (Decl_Node, CompDef_Node);
3382 Set_Expression (Decl_Node, Init_Expr_Opt);
3384 if Ident > 1 then
3385 Set_Prev_Ids (Decl_Node, True);
3386 end if;
3388 if Ident < Num_Idents then
3389 Set_More_Ids (Decl_Node, True);
3390 end if;
3392 Append (Decl_Node, Decls);
3394 exception
3395 when Error_Resync =>
3396 if Token /= Tok_End then
3397 Resync_Past_Semicolon;
3398 end if;
3399 end;
3401 exit Ident_Loop when Ident = Num_Idents;
3402 Ident := Ident + 1;
3403 Restore_Scan_State (Scan_State);
3404 T_Colon;
3406 end loop Ident_Loop;
3408 TF_Semicolon;
3409 end P_Component_Items;
3411 --------------------------------
3412 -- 3.8 Component Declaration --
3413 --------------------------------
3415 -- Parsed by P_Component_Items (3.8)
3417 -------------------------
3418 -- 3.8.1 Variant Part --
3419 -------------------------
3421 -- VARIANT_PART ::=
3422 -- case discriminant_DIRECT_NAME is
3423 -- VARIANT
3424 -- {VARIANT}
3425 -- end case;
3427 -- The caller has checked that the initial token is CASE
3429 -- Error recovery: cannot raise Error_Resync
3431 function P_Variant_Part return Node_Id is
3432 Variant_Part_Node : Node_Id;
3433 Variants_List : List_Id;
3434 Case_Node : Node_Id;
3436 begin
3437 Variant_Part_Node := New_Node (N_Variant_Part, Token_Ptr);
3438 Push_Scope_Stack;
3439 Scope.Table (Scope.Last).Etyp := E_Case;
3440 Scope.Table (Scope.Last).Sloc := Token_Ptr;
3441 Scope.Table (Scope.Last).Ecol := Start_Column;
3443 Scan; -- past CASE
3444 Case_Node := P_Expression;
3445 Set_Name (Variant_Part_Node, Case_Node);
3447 if Nkind (Case_Node) /= N_Identifier then
3448 Set_Name (Variant_Part_Node, Error);
3449 Error_Msg ("discriminant name expected", Sloc (Case_Node));
3451 elsif Paren_Count (Case_Node) /= 0 then
3452 Error_Msg ("|discriminant name may not be parenthesized",
3453 Sloc (Case_Node));
3454 Set_Paren_Count (Case_Node, 0);
3455 end if;
3457 TF_Is;
3458 Variants_List := New_List;
3459 P_Pragmas_Opt (Variants_List);
3461 -- Test missing variant
3463 if Token = Tok_End then
3464 Error_Msg_BC ("WHEN expected (must have at least one variant)");
3465 else
3466 Append (P_Variant, Variants_List);
3467 end if;
3469 -- Loop through variants, note that we allow if in place of when,
3470 -- this error will be detected and handled in P_Variant.
3472 loop
3473 P_Pragmas_Opt (Variants_List);
3475 if Token /= Tok_When
3476 and then Token /= Tok_If
3477 and then Token /= Tok_Others
3478 then
3479 exit when Check_End;
3480 end if;
3482 Append (P_Variant, Variants_List);
3483 end loop;
3485 Set_Variants (Variant_Part_Node, Variants_List);
3486 return Variant_Part_Node;
3487 end P_Variant_Part;
3489 --------------------
3490 -- 3.8.1 Variant --
3491 --------------------
3493 -- VARIANT ::=
3494 -- when DISCRETE_CHOICE_LIST =>
3495 -- COMPONENT_LIST
3497 -- Error recovery: cannot raise Error_Resync
3499 -- The initial token on entry is either WHEN, IF or OTHERS
3501 function P_Variant return Node_Id is
3502 Variant_Node : Node_Id;
3504 begin
3505 -- Special check to recover nicely from use of IF in place of WHEN
3507 if Token = Tok_If then
3508 T_When;
3509 Scan; -- past IF
3510 else
3511 T_When;
3512 end if;
3514 Variant_Node := New_Node (N_Variant, Prev_Token_Ptr);
3515 Set_Discrete_Choices (Variant_Node, P_Discrete_Choice_List);
3516 TF_Arrow;
3517 Set_Component_List (Variant_Node, P_Component_List);
3518 return Variant_Node;
3519 end P_Variant;
3521 ---------------------------------
3522 -- 3.8.1 Discrete Choice List --
3523 ---------------------------------
3525 -- DISCRETE_CHOICE_LIST ::= DISCRETE_CHOICE {| DISCRETE_CHOICE}
3527 -- DISCRETE_CHOICE ::= EXPRESSION | DISCRETE_RANGE | others
3529 -- Note: in Ada 83, the expression must be a simple expression
3531 -- Error recovery: cannot raise Error_Resync
3533 function P_Discrete_Choice_List return List_Id is
3534 Choices : List_Id;
3535 Expr_Node : Node_Id;
3536 Choice_Node : Node_Id;
3538 begin
3539 Choices := New_List;
3541 loop
3542 if Token = Tok_Others then
3543 Append (New_Node (N_Others_Choice, Token_Ptr), Choices);
3544 Scan; -- past OTHERS
3546 else
3547 begin
3548 Expr_Node := P_Expression_Or_Range_Attribute;
3549 Ignore (Tok_Right_Paren);
3551 if Token = Tok_Colon
3552 and then Nkind (Expr_Node) = N_Identifier
3553 then
3554 Error_Msg_SP ("label not permitted in this context");
3555 Scan; -- past colon
3557 elsif Expr_Form = EF_Range_Attr then
3558 Append (Expr_Node, Choices);
3560 elsif Token = Tok_Dot_Dot then
3561 Check_Simple_Expression (Expr_Node);
3562 Choice_Node := New_Node (N_Range, Token_Ptr);
3563 Set_Low_Bound (Choice_Node, Expr_Node);
3564 Scan; -- past ..
3565 Expr_Node := P_Expression_No_Right_Paren;
3566 Check_Simple_Expression (Expr_Node);
3567 Set_High_Bound (Choice_Node, Expr_Node);
3568 Append (Choice_Node, Choices);
3570 elsif Expr_Form = EF_Simple_Name then
3571 if Token = Tok_Range then
3572 Append (P_Subtype_Indication (Expr_Node), Choices);
3574 elsif Token in Token_Class_Consk then
3575 Error_Msg_SC
3576 ("the only constraint allowed here " &
3577 "is a range constraint");
3578 Discard_Junk_Node (P_Constraint_Opt);
3579 Append (Expr_Node, Choices);
3581 else
3582 Append (Expr_Node, Choices);
3583 end if;
3585 else
3586 Check_Simple_Expression_In_Ada_83 (Expr_Node);
3587 Append (Expr_Node, Choices);
3588 end if;
3590 exception
3591 when Error_Resync =>
3592 Resync_Choice;
3593 return Error_List;
3594 end;
3595 end if;
3597 if Token = Tok_Comma then
3598 Error_Msg_SC (""","" should be ""'|""");
3599 else
3600 exit when Token /= Tok_Vertical_Bar;
3601 end if;
3603 Scan; -- past | or comma
3604 end loop;
3606 return Choices;
3607 end P_Discrete_Choice_List;
3609 ----------------------------
3610 -- 3.8.1 Discrete Choice --
3611 ----------------------------
3613 -- Parsed by P_Discrete_Choice_List (3.8.1)
3615 ----------------------------------
3616 -- 3.9.1 Record Extension Part --
3617 ----------------------------------
3619 -- RECORD_EXTENSION_PART ::= with RECORD_DEFINITION
3621 -- Parsed by P_Derived_Type_Def_Or_Private_Ext_Decl (3.4)
3623 --------------------------------------
3624 -- 3.9.4 Interface Type Definition --
3625 --------------------------------------
3627 -- INTERFACE_TYPE_DEFINITION ::=
3628 -- [limited | task | protected | synchronized] interface
3629 -- [and INTERFACE_LIST]
3631 -- Error recovery: cannot raise Error_Resync
3633 function P_Interface_Type_Definition
3634 (Abstract_Present : Boolean) return Node_Id
3636 Typedef_Node : Node_Id;
3638 begin
3639 if Ada_Version < Ada_05 then
3640 Error_Msg_SP ("abstract interface is an Ada 2005 extension");
3641 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
3642 end if;
3644 if Abstract_Present then
3645 Error_Msg_SP ("ABSTRACT not allowed in interface type definition " &
3646 "(RM 3.9.4(2/2))");
3647 end if;
3649 Scan; -- past INTERFACE
3651 -- Ada 2005 (AI-345): In case of interfaces with a null list of
3652 -- interfaces we build a record_definition node.
3654 if Token = Tok_Semicolon then
3655 Typedef_Node := New_Node (N_Record_Definition, Token_Ptr);
3657 Set_Abstract_Present (Typedef_Node);
3658 Set_Tagged_Present (Typedef_Node);
3659 Set_Null_Present (Typedef_Node);
3660 Set_Interface_Present (Typedef_Node);
3662 -- Ada 2005 (AI-251): In case of not-synchronized interfaces that have
3663 -- a list of interfaces we build a derived_type_definition node. This
3664 -- simplifies the semantic analysis (and hence further maintenance)
3666 else
3667 if Token /= Tok_And then
3668 Error_Msg_AP ("AND expected");
3669 else
3670 Scan; -- past AND
3671 end if;
3673 Typedef_Node := New_Node (N_Derived_Type_Definition, Token_Ptr);
3675 Set_Abstract_Present (Typedef_Node);
3676 Set_Interface_Present (Typedef_Node);
3677 Set_Subtype_Indication (Typedef_Node, P_Qualified_Simple_Name);
3679 Set_Record_Extension_Part (Typedef_Node,
3680 New_Node (N_Record_Definition, Token_Ptr));
3681 Set_Null_Present (Record_Extension_Part (Typedef_Node));
3683 if Token = Tok_And then
3684 Set_Interface_List (Typedef_Node, New_List);
3685 Scan; -- past AND
3687 loop
3688 Append (P_Qualified_Simple_Name,
3689 Interface_List (Typedef_Node));
3690 exit when Token /= Tok_And;
3691 Scan; -- past AND
3692 end loop;
3693 end if;
3694 end if;
3696 return Typedef_Node;
3697 end P_Interface_Type_Definition;
3699 ----------------------------------
3700 -- 3.10 Access Type Definition --
3701 ----------------------------------
3703 -- ACCESS_TYPE_DEFINITION ::=
3704 -- ACCESS_TO_OBJECT_DEFINITION
3705 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3707 -- ACCESS_TO_OBJECT_DEFINITION ::=
3708 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_INDICATION
3710 -- GENERAL_ACCESS_MODIFIER ::= all | constant
3712 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3713 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3714 -- | [NULL_EXCLUSION] access [protected] function
3715 -- PARAMETER_AND_RESULT_PROFILE
3717 -- PARAMETER_PROFILE ::= [FORMAL_PART]
3719 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] RETURN SUBTYPE_MARK
3721 -- Ada 2005 (AI-254): If Header_Already_Parsed then the caller has already
3722 -- parsed the null_exclusion part and has also removed the ACCESS token;
3723 -- otherwise the caller has just checked that the initial token is ACCESS
3725 -- Error recovery: can raise Error_Resync
3727 function P_Access_Type_Definition
3728 (Header_Already_Parsed : Boolean := False) return Node_Id
3730 Access_Loc : constant Source_Ptr := Token_Ptr;
3731 Prot_Flag : Boolean;
3732 Not_Null_Present : Boolean := False;
3733 Type_Def_Node : Node_Id;
3734 Result_Not_Null : Boolean;
3735 Result_Node : Node_Id;
3737 procedure Check_Junk_Subprogram_Name;
3738 -- Used in access to subprogram definition cases to check for an
3739 -- identifier or operator symbol that does not belong.
3741 --------------------------------
3742 -- Check_Junk_Subprogram_Name --
3743 --------------------------------
3745 procedure Check_Junk_Subprogram_Name is
3746 Saved_State : Saved_Scan_State;
3748 begin
3749 if Token = Tok_Identifier or else Token = Tok_Operator_Symbol then
3750 Save_Scan_State (Saved_State);
3751 Scan; -- past possible junk subprogram name
3753 if Token = Tok_Left_Paren or else Token = Tok_Semicolon then
3754 Error_Msg_SP ("unexpected subprogram name ignored");
3755 return;
3757 else
3758 Restore_Scan_State (Saved_State);
3759 end if;
3760 end if;
3761 end Check_Junk_Subprogram_Name;
3763 -- Start of processing for P_Access_Type_Definition
3765 begin
3766 if not Header_Already_Parsed then
3767 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
3768 Scan; -- past ACCESS
3769 end if;
3771 if Token_Name = Name_Protected then
3772 Check_95_Keyword (Tok_Protected, Tok_Procedure);
3773 Check_95_Keyword (Tok_Protected, Tok_Function);
3774 end if;
3776 Prot_Flag := (Token = Tok_Protected);
3778 if Prot_Flag then
3779 Scan; -- past PROTECTED
3781 if Token /= Tok_Procedure and then Token /= Tok_Function then
3782 Error_Msg_SC ("FUNCTION or PROCEDURE expected");
3783 end if;
3784 end if;
3786 if Token = Tok_Procedure then
3787 if Ada_Version = Ada_83 then
3788 Error_Msg_SC ("(Ada 83) access to procedure not allowed!");
3789 end if;
3791 Type_Def_Node := New_Node (N_Access_Procedure_Definition, Access_Loc);
3792 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3793 Scan; -- past PROCEDURE
3794 Check_Junk_Subprogram_Name;
3795 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3796 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3798 elsif Token = Tok_Function then
3799 if Ada_Version = Ada_83 then
3800 Error_Msg_SC ("(Ada 83) access to function not allowed!");
3801 end if;
3803 Type_Def_Node := New_Node (N_Access_Function_Definition, Access_Loc);
3804 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3805 Scan; -- past FUNCTION
3806 Check_Junk_Subprogram_Name;
3807 Set_Parameter_Specifications (Type_Def_Node, P_Parameter_Profile);
3808 Set_Protected_Present (Type_Def_Node, Prot_Flag);
3809 TF_Return;
3811 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
3813 -- Ada 2005 (AI-318-02)
3815 if Token = Tok_Access then
3816 if Ada_Version < Ada_05 then
3817 Error_Msg_SC
3818 ("anonymous access result type is an Ada 2005 extension");
3819 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
3820 end if;
3822 Result_Node := P_Access_Definition (Result_Not_Null);
3824 else
3825 Result_Node := P_Subtype_Mark;
3826 No_Constraint;
3827 end if;
3829 -- Note: A null exclusion given on the result type needs to
3830 -- be coded by a distinct flag, since Null_Exclusion_Present
3831 -- on an access-to-function type pertains to a null exclusion
3832 -- on the access type itself (as set above). ???
3833 -- Set_Null_Exclusion_Present??? (Type_Def_Node, Result_Not_Null);
3835 Set_Result_Definition (Type_Def_Node, Result_Node);
3837 else
3838 Type_Def_Node :=
3839 New_Node (N_Access_To_Object_Definition, Access_Loc);
3840 Set_Null_Exclusion_Present (Type_Def_Node, Not_Null_Present);
3842 if Token = Tok_All or else Token = Tok_Constant then
3843 if Ada_Version = Ada_83 then
3844 Error_Msg_SC ("(Ada 83) access modifier not allowed!");
3845 end if;
3847 if Token = Tok_All then
3848 Set_All_Present (Type_Def_Node, True);
3850 else
3851 Set_Constant_Present (Type_Def_Node, True);
3852 end if;
3854 Scan; -- past ALL or CONSTANT
3855 end if;
3857 Set_Subtype_Indication (Type_Def_Node,
3858 P_Subtype_Indication (Not_Null_Present));
3859 end if;
3861 return Type_Def_Node;
3862 end P_Access_Type_Definition;
3864 ---------------------------------------
3865 -- 3.10 Access To Object Definition --
3866 ---------------------------------------
3868 -- Parsed by P_Access_Type_Definition (3.10)
3870 -----------------------------------
3871 -- 3.10 General Access Modifier --
3872 -----------------------------------
3874 -- Parsed by P_Access_Type_Definition (3.10)
3876 -------------------------------------------
3877 -- 3.10 Access To Subprogram Definition --
3878 -------------------------------------------
3880 -- Parsed by P_Access_Type_Definition (3.10)
3882 -----------------------------
3883 -- 3.10 Access Definition --
3884 -----------------------------
3886 -- ACCESS_DEFINITION ::=
3887 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
3888 -- | ACCESS_TO_SUBPROGRAM_DEFINITION
3890 -- ACCESS_TO_SUBPROGRAM_DEFINITION
3891 -- [NULL_EXCLUSION] access [protected] procedure PARAMETER_PROFILE
3892 -- | [NULL_EXCLUSION] access [protected] function
3893 -- PARAMETER_AND_RESULT_PROFILE
3895 -- The caller has parsed the null-exclusion part and it has also checked
3896 -- that the next token is ACCESS
3898 -- Error recovery: cannot raise Error_Resync
3900 function P_Access_Definition
3901 (Null_Exclusion_Present : Boolean) return Node_Id
3903 Def_Node : Node_Id;
3904 Subp_Node : Node_Id;
3906 begin
3907 Def_Node := New_Node (N_Access_Definition, Token_Ptr);
3908 Scan; -- past ACCESS
3910 -- Ada 2005 (AI-254): Access_To_Subprogram_Definition
3912 if Token = Tok_Protected
3913 or else Token = Tok_Procedure
3914 or else Token = Tok_Function
3915 then
3916 if Ada_Version < Ada_05 then
3917 Error_Msg_SP ("access-to-subprogram is an Ada 2005 extension");
3918 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
3919 end if;
3921 Subp_Node := P_Access_Type_Definition (Header_Already_Parsed => True);
3922 Set_Null_Exclusion_Present (Subp_Node, Null_Exclusion_Present);
3923 Set_Access_To_Subprogram_Definition (Def_Node, Subp_Node);
3925 -- Ada 2005 (AI-231)
3926 -- [NULL_EXCLUSION] access [GENERAL_ACCESS_MODIFIER] SUBTYPE_MARK
3928 else
3929 Set_Null_Exclusion_Present (Def_Node, Null_Exclusion_Present);
3931 if Token = Tok_All then
3932 if Ada_Version < Ada_05 then
3933 Error_Msg_SP
3934 ("ALL is not permitted for anonymous access types");
3935 end if;
3937 Scan; -- past ALL
3938 Set_All_Present (Def_Node);
3940 elsif Token = Tok_Constant then
3941 if Ada_Version < Ada_05 then
3942 Error_Msg_SP ("access-to-constant is an Ada 2005 extension");
3943 Error_Msg_SP ("\unit should be compiled with -gnat05 switch");
3944 end if;
3946 Scan; -- past CONSTANT
3947 Set_Constant_Present (Def_Node);
3948 end if;
3950 Set_Subtype_Mark (Def_Node, P_Subtype_Mark);
3951 No_Constraint;
3952 end if;
3954 return Def_Node;
3955 end P_Access_Definition;
3957 -----------------------------------------
3958 -- 3.10.1 Incomplete Type Declaration --
3959 -----------------------------------------
3961 -- Parsed by P_Type_Declaration (3.2.1)
3963 ----------------------------
3964 -- 3.11 Declarative Part --
3965 ----------------------------
3967 -- DECLARATIVE_PART ::= {DECLARATIVE_ITEM}
3969 -- Error recovery: cannot raise Error_Resync (because P_Declarative_Items
3970 -- handles errors, and returns cleanly after an error has occurred)
3972 function P_Declarative_Part return List_Id is
3973 Decls : List_Id;
3974 Done : Boolean;
3976 begin
3977 -- Indicate no bad declarations detected yet. This will be reset by
3978 -- P_Declarative_Items if a bad declaration is discovered.
3980 Missing_Begin_Msg := No_Error_Msg;
3982 -- Get rid of active SIS entry from outer scope. This means we will
3983 -- miss some nested cases, but it doesn't seem worth the effort. See
3984 -- discussion in Par for further details
3986 SIS_Entry_Active := False;
3987 Decls := New_List;
3989 -- Loop to scan out the declarations
3991 loop
3992 P_Declarative_Items (Decls, Done, In_Spec => False);
3993 exit when Done;
3994 end loop;
3996 -- Get rid of active SIS entry which is left set only if we scanned a
3997 -- procedure declaration and have not found the body. We could give
3998 -- an error message, but that really would be usurping the role of
3999 -- semantic analysis (this really is a missing body case).
4001 SIS_Entry_Active := False;
4002 return Decls;
4003 end P_Declarative_Part;
4005 ----------------------------
4006 -- 3.11 Declarative Item --
4007 ----------------------------
4009 -- DECLARATIVE_ITEM ::= BASIC_DECLARATIVE_ITEM | BODY
4011 -- Can return Error if a junk declaration is found, or Empty if no
4012 -- declaration is found (i.e. a token ending declarations, such as
4013 -- BEGIN or END is encountered).
4015 -- Error recovery: cannot raise Error_Resync. If an error resync occurs,
4016 -- then the scan is set past the next semicolon and Error is returned.
4018 procedure P_Declarative_Items
4019 (Decls : List_Id;
4020 Done : out Boolean;
4021 In_Spec : Boolean)
4023 Scan_State : Saved_Scan_State;
4025 begin
4026 if Style_Check then
4027 Style.Check_Indentation;
4028 end if;
4030 case Token is
4032 when Tok_Function =>
4033 Check_Bad_Layout;
4034 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4035 Done := False;
4037 when Tok_For =>
4038 Check_Bad_Layout;
4040 -- Check for loop (premature statement)
4042 Save_Scan_State (Scan_State);
4043 Scan; -- past FOR
4045 if Token = Tok_Identifier then
4046 Scan; -- past identifier
4048 if Token = Tok_In then
4049 Restore_Scan_State (Scan_State);
4050 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4051 return;
4052 end if;
4053 end if;
4055 -- Not a loop, so must be rep clause
4057 Restore_Scan_State (Scan_State);
4058 Append (P_Representation_Clause, Decls);
4059 Done := False;
4061 when Tok_Generic =>
4062 Check_Bad_Layout;
4063 Append (P_Generic, Decls);
4064 Done := False;
4066 when Tok_Identifier =>
4067 Check_Bad_Layout;
4069 -- Special check for misuse of overriding not in Ada 2005 mode
4071 if Token_Name = Name_Overriding
4072 and then not Next_Token_Is (Tok_Colon)
4073 then
4074 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
4075 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
4077 Token := Tok_Overriding;
4078 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4079 Done := False;
4081 -- Normal case, no overriding, or overriding followed by colon
4083 else
4084 P_Identifier_Declarations (Decls, Done, In_Spec);
4085 end if;
4087 -- Ada2005: A subprogram declaration can start with "not" or
4088 -- "overriding". In older versions, "overriding" is handled
4089 -- like an identifier, with the appropriate messages.
4091 when Tok_Not =>
4092 Check_Bad_Layout;
4093 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4094 Done := False;
4096 when Tok_Overriding =>
4097 Check_Bad_Layout;
4098 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4099 Done := False;
4101 when Tok_Package =>
4102 Check_Bad_Layout;
4103 Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4104 Done := False;
4106 when Tok_Pragma =>
4107 Append (P_Pragma, Decls);
4108 Done := False;
4110 when Tok_Procedure =>
4111 Check_Bad_Layout;
4112 Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
4113 Done := False;
4115 when Tok_Protected =>
4116 Check_Bad_Layout;
4117 Scan; -- past PROTECTED
4118 Append (P_Protected, Decls);
4119 Done := False;
4121 when Tok_Subtype =>
4122 Check_Bad_Layout;
4123 Append (P_Subtype_Declaration, Decls);
4124 Done := False;
4126 when Tok_Task =>
4127 Check_Bad_Layout;
4128 Scan; -- past TASK
4129 Append (P_Task, Decls);
4130 Done := False;
4132 when Tok_Type =>
4133 Check_Bad_Layout;
4134 Append (P_Type_Declaration, Decls);
4135 Done := False;
4137 when Tok_Use =>
4138 Check_Bad_Layout;
4139 Append (P_Use_Clause, Decls);
4140 Done := False;
4142 when Tok_With =>
4143 Check_Bad_Layout;
4144 Error_Msg_SC ("WITH can only appear in context clause");
4145 raise Error_Resync;
4147 -- BEGIN terminates the scan of a sequence of declarations unless
4148 -- there is a missing subprogram body, see section on handling
4149 -- semicolon in place of IS. We only treat the begin as satisfying
4150 -- the subprogram declaration if it falls in the expected column
4151 -- or to its right.
4153 when Tok_Begin =>
4154 if SIS_Entry_Active and then Start_Column >= SIS_Ecol then
4156 -- Here we have the case where a BEGIN is encountered during
4157 -- declarations in a declarative part, or at the outer level,
4158 -- and there is a subprogram declaration outstanding for which
4159 -- no body has been supplied. This is the case where we assume
4160 -- that the semicolon in the subprogram declaration should
4161 -- really have been is. The active SIS entry describes the
4162 -- subprogram declaration. On return the declaration has been
4163 -- modified to become a body.
4165 declare
4166 Specification_Node : Node_Id;
4167 Decl_Node : Node_Id;
4168 Body_Node : Node_Id;
4170 begin
4171 -- First issue the error message. If we had a missing
4172 -- semicolon in the declaration, then change the message
4173 -- to <missing "is">
4175 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
4176 Change_Error_Text -- Replace: "missing "";"" "
4177 (SIS_Missing_Semicolon_Message, "missing ""is""");
4179 -- Otherwise we saved the semicolon position, so complain
4181 else
4182 Error_Msg ("|"";"" should be IS", SIS_Semicolon_Sloc);
4183 end if;
4185 -- The next job is to fix up any declarations that occurred
4186 -- between the procedure header and the BEGIN. These got
4187 -- chained to the outer declarative region (immediately
4188 -- after the procedure declaration) and they should be
4189 -- chained to the subprogram itself, which is a body
4190 -- rather than a spec.
4192 Specification_Node := Specification (SIS_Declaration_Node);
4193 Change_Node (SIS_Declaration_Node, N_Subprogram_Body);
4194 Body_Node := SIS_Declaration_Node;
4195 Set_Specification (Body_Node, Specification_Node);
4196 Set_Declarations (Body_Node, New_List);
4198 loop
4199 Decl_Node := Remove_Next (Body_Node);
4200 exit when Decl_Node = Empty;
4201 Append (Decl_Node, Declarations (Body_Node));
4202 end loop;
4204 -- Now make the scope table entry for the Begin-End and
4205 -- scan it out
4207 Push_Scope_Stack;
4208 Scope.Table (Scope.Last).Sloc := SIS_Sloc;
4209 Scope.Table (Scope.Last).Etyp := E_Name;
4210 Scope.Table (Scope.Last).Ecol := SIS_Ecol;
4211 Scope.Table (Scope.Last).Labl := SIS_Labl;
4212 Scope.Table (Scope.Last).Lreq := False;
4213 SIS_Entry_Active := False;
4214 Scan; -- past BEGIN
4215 Set_Handled_Statement_Sequence (Body_Node,
4216 P_Handled_Sequence_Of_Statements);
4217 End_Statements (Handled_Statement_Sequence (Body_Node));
4218 end;
4220 Done := False;
4222 else
4223 Done := True;
4224 end if;
4226 -- Normally an END terminates the scan for basic declarative
4227 -- items. The one exception is END RECORD, which is probably
4228 -- left over from some other junk.
4230 when Tok_End =>
4231 Save_Scan_State (Scan_State); -- at END
4232 Scan; -- past END
4234 if Token = Tok_Record then
4235 Error_Msg_SP ("no RECORD for this `end record`!");
4236 Scan; -- past RECORD
4237 TF_Semicolon;
4239 else
4240 Restore_Scan_State (Scan_State); -- to END
4241 Done := True;
4242 end if;
4244 -- The following tokens which can only be the start of a statement
4245 -- are considered to end a declarative part (i.e. we have a missing
4246 -- BEGIN situation). We are fairly conservative in making this
4247 -- judgment, because it is a real mess to go into statement mode
4248 -- prematurely in response to a junk declaration.
4250 when Tok_Abort |
4251 Tok_Accept |
4252 Tok_Declare |
4253 Tok_Delay |
4254 Tok_Exit |
4255 Tok_Goto |
4256 Tok_If |
4257 Tok_Loop |
4258 Tok_Null |
4259 Tok_Requeue |
4260 Tok_Select |
4261 Tok_While =>
4263 -- But before we decide that it's a statement, let's check for
4264 -- a reserved word misused as an identifier.
4266 if Is_Reserved_Identifier then
4267 Save_Scan_State (Scan_State);
4268 Scan; -- past the token
4270 -- If reserved identifier not followed by colon or comma, then
4271 -- this is most likely an assignment statement to the bad id.
4273 if Token /= Tok_Colon and then Token /= Tok_Comma then
4274 Restore_Scan_State (Scan_State);
4275 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4276 return;
4278 -- Otherwise we have a declaration of the bad id
4280 else
4281 Restore_Scan_State (Scan_State);
4282 Scan_Reserved_Identifier (Force_Msg => True);
4283 P_Identifier_Declarations (Decls, Done, In_Spec);
4284 end if;
4286 -- If not reserved identifier, then it's definitely a statement
4288 else
4289 Statement_When_Declaration_Expected (Decls, Done, In_Spec);
4290 return;
4291 end if;
4293 -- The token RETURN may well also signal a missing BEGIN situation,
4294 -- however, we never let it end the declarative part, because it may
4295 -- also be part of a half-baked function declaration.
4297 when Tok_Return =>
4298 Error_Msg_SC ("misplaced RETURN statement");
4299 raise Error_Resync;
4301 -- PRIVATE definitely terminates the declarations in a spec,
4302 -- and is an error in a body.
4304 when Tok_Private =>
4305 if In_Spec then
4306 Done := True;
4307 else
4308 Error_Msg_SC ("PRIVATE not allowed in body");
4309 Scan; -- past PRIVATE
4310 end if;
4312 -- An end of file definitely terminates the declarations!
4314 when Tok_EOF =>
4315 Done := True;
4317 -- The remaining tokens do not end the scan, but cannot start a
4318 -- valid declaration, so we signal an error and resynchronize.
4319 -- But first check for misuse of a reserved identifier.
4321 when others =>
4323 -- Here we check for a reserved identifier
4325 if Is_Reserved_Identifier then
4326 Save_Scan_State (Scan_State);
4327 Scan; -- past the token
4329 if Token /= Tok_Colon and then Token /= Tok_Comma then
4330 Restore_Scan_State (Scan_State);
4331 Set_Declaration_Expected;
4332 raise Error_Resync;
4333 else
4334 Restore_Scan_State (Scan_State);
4335 Scan_Reserved_Identifier (Force_Msg => True);
4336 Check_Bad_Layout;
4337 P_Identifier_Declarations (Decls, Done, In_Spec);
4338 end if;
4340 else
4341 Set_Declaration_Expected;
4342 raise Error_Resync;
4343 end if;
4344 end case;
4346 -- To resynchronize after an error, we scan to the next semicolon and
4347 -- return with Done = False, indicating that there may still be more
4348 -- valid declarations to come.
4350 exception
4351 when Error_Resync =>
4352 Resync_Past_Semicolon;
4353 Done := False;
4354 end P_Declarative_Items;
4356 ----------------------------------
4357 -- 3.11 Basic Declarative Item --
4358 ----------------------------------
4360 -- BASIC_DECLARATIVE_ITEM ::=
4361 -- BASIC_DECLARATION | REPRESENTATION_CLAUSE | USE_CLAUSE
4363 -- Scan zero or more basic declarative items
4365 -- Error recovery: cannot raise Error_Resync. If an error is detected, then
4366 -- the scan pointer is repositioned past the next semicolon, and the scan
4367 -- for declarative items continues.
4369 function P_Basic_Declarative_Items return List_Id is
4370 Decl : Node_Id;
4371 Decls : List_Id;
4372 Kind : Node_Kind;
4373 Done : Boolean;
4375 begin
4376 -- Indicate no bad declarations detected yet in the current context:
4377 -- visible or private declarations of a package spec.
4379 Missing_Begin_Msg := No_Error_Msg;
4381 -- Get rid of active SIS entry from outer scope. This means we will
4382 -- miss some nested cases, but it doesn't seem worth the effort. See
4383 -- discussion in Par for further details
4385 SIS_Entry_Active := False;
4387 -- Loop to scan out declarations
4389 Decls := New_List;
4391 loop
4392 P_Declarative_Items (Decls, Done, In_Spec => True);
4393 exit when Done;
4394 end loop;
4396 -- Get rid of active SIS entry. This is set only if we have scanned a
4397 -- procedure declaration and have not found the body. We could give
4398 -- an error message, but that really would be usurping the role of
4399 -- semantic analysis (this really is a case of a missing body).
4401 SIS_Entry_Active := False;
4403 -- Test for assorted illegal declarations not diagnosed elsewhere
4405 Decl := First (Decls);
4407 while Present (Decl) loop
4408 Kind := Nkind (Decl);
4410 -- Test for body scanned, not acceptable as basic decl item
4412 if Kind = N_Subprogram_Body or else
4413 Kind = N_Package_Body or else
4414 Kind = N_Task_Body or else
4415 Kind = N_Protected_Body
4416 then
4417 Error_Msg
4418 ("proper body not allowed in package spec", Sloc (Decl));
4420 -- Test for body stub scanned, not acceptable as basic decl item
4422 elsif Kind in N_Body_Stub then
4423 Error_Msg
4424 ("body stub not allowed in package spec", Sloc (Decl));
4426 elsif Kind = N_Assignment_Statement then
4427 Error_Msg
4428 ("assignment statement not allowed in package spec",
4429 Sloc (Decl));
4430 end if;
4432 Next (Decl);
4433 end loop;
4435 return Decls;
4436 end P_Basic_Declarative_Items;
4438 ----------------
4439 -- 3.11 Body --
4440 ----------------
4442 -- For proper body, see below
4443 -- For body stub, see 10.1.3
4445 -----------------------
4446 -- 3.11 Proper Body --
4447 -----------------------
4449 -- Subprogram body is parsed by P_Subprogram (6.1)
4450 -- Package body is parsed by P_Package (7.1)
4451 -- Task body is parsed by P_Task (9.1)
4452 -- Protected body is parsed by P_Protected (9.4)
4454 ------------------------------
4455 -- Set_Declaration_Expected --
4456 ------------------------------
4458 procedure Set_Declaration_Expected is
4459 begin
4460 Error_Msg_SC ("declaration expected");
4462 if Missing_Begin_Msg = No_Error_Msg then
4463 Missing_Begin_Msg := Get_Msg_Id;
4464 end if;
4465 end Set_Declaration_Expected;
4467 ----------------------
4468 -- Skip_Declaration --
4469 ----------------------
4471 procedure Skip_Declaration (S : List_Id) is
4472 Dummy_Done : Boolean;
4473 pragma Warnings (Off, Dummy_Done);
4474 begin
4475 P_Declarative_Items (S, Dummy_Done, False);
4476 end Skip_Declaration;
4478 -----------------------------------------
4479 -- Statement_When_Declaration_Expected --
4480 -----------------------------------------
4482 procedure Statement_When_Declaration_Expected
4483 (Decls : List_Id;
4484 Done : out Boolean;
4485 In_Spec : Boolean)
4487 begin
4488 -- Case of second occurrence of statement in one declaration sequence
4490 if Missing_Begin_Msg /= No_Error_Msg then
4492 -- In the procedure spec case, just ignore it, we only give one
4493 -- message for the first occurrence, since otherwise we may get
4494 -- horrible cascading if BODY was missing in the header line.
4496 if In_Spec then
4497 null;
4499 -- In the declarative part case, take a second statement as a sure
4500 -- sign that we really have a missing BEGIN, and end the declarative
4501 -- part now. Note that the caller will fix up the first message to
4502 -- say "missing BEGIN" so that's how the error will be signalled.
4504 else
4505 Done := True;
4506 return;
4507 end if;
4509 -- Case of first occurrence of unexpected statement
4511 else
4512 -- If we are in a package spec, then give message of statement
4513 -- not allowed in package spec. This message never gets changed.
4515 if In_Spec then
4516 Error_Msg_SC ("statement not allowed in package spec");
4518 -- If in declarative part, then we give the message complaining
4519 -- about finding a statement when a declaration is expected. This
4520 -- gets changed to a complaint about a missing BEGIN if we later
4521 -- find that no BEGIN is present.
4523 else
4524 Error_Msg_SC ("statement not allowed in declarative part");
4525 end if;
4527 -- Capture message Id. This is used for two purposes, first to
4528 -- stop multiple messages, see test above, and second, to allow
4529 -- the replacement of the message in the declarative part case.
4531 Missing_Begin_Msg := Get_Msg_Id;
4532 end if;
4534 -- In all cases except the case in which we decided to terminate the
4535 -- declaration sequence on a second error, we scan out the statement
4536 -- and append it to the list of declarations (note that the semantics
4537 -- can handle statements in a declaration list so if we proceed to
4538 -- call the semantic phase, all will be (reasonably) well!
4540 Append_List_To (Decls, P_Sequence_Of_Statements (SS_Unco));
4542 -- Done is set to False, since we want to continue the scan of
4543 -- declarations, hoping that this statement was a temporary glitch.
4544 -- If we indeed are now in the statement part (i.e. this was a missing
4545 -- BEGIN, then it's not terrible, we will simply keep calling this
4546 -- procedure to process the statements one by one, and then finally
4547 -- hit the missing BEGIN, which will clean up the error message.
4549 Done := False;
4550 end Statement_When_Declaration_Expected;
4552 end Ch3;