1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
33 -- Local functions, used only in this chapter
35 function P_Component_Clause return Node_Id;
36 function P_Mod_Clause return Node_Id;
38 -----------------------------------
39 -- Aspect_Specifications_Present --
40 -----------------------------------
42 function Aspect_Specifications_Present
43 (Strict : Boolean := Ada_Version < Ada_2012) return Boolean
45 Scan_State : Saved_Scan_State;
49 -- Definitely must have WITH to consider aspect specs to be present
51 -- Note that this means that if we have a semicolon, we immediately
52 -- return False. There is a case in which this is not optimal, namely
55 -- type R is new Integer;
58 -- where the semicolon is redundant, but scanning forward for it would
59 -- be too expensive. Instead we pick up the aspect specifications later
60 -- as a bogus declaration, and diagnose the semicolon at that point.
62 if Token /= Tok_With then
66 -- Have a WITH, see if it looks like an aspect specification
68 Save_Scan_State (Scan_State);
71 -- If no identifier, then consider that we definitely do not have an
72 -- aspect specification.
74 if Token /= Tok_Identifier then
77 -- This is where we pay attention to the Strict mode. Normally when we
78 -- are in Ada 2012 mode, Strict is False, and we consider that we have
79 -- an aspect specification if the identifier is an aspect name (even if
80 -- not followed by =>) or the identifier is not an aspect name but is
81 -- followed by =>. P_Aspect_Specifications will generate messages if the
82 -- aspect specification is ill-formed.
85 if Get_Aspect_Id (Token_Name) /= No_Aspect then
88 Scan; -- past identifier
89 Result := Token = Tok_Arrow;
92 -- If earlier than Ada 2012, check for valid aspect identifier (possibly
93 -- completed with 'CLASS) followed by an arrow, and consider that this
94 -- is still an aspect specification so we give an appropriate message.
97 if Get_Aspect_Id (Token_Name) = No_Aspect then
101 Scan; -- past aspect name
105 if Token = Tok_Arrow then
108 elsif Token = Tok_Apostrophe then
109 Scan; -- past apostrophe
111 if Token = Tok_Identifier
112 and then Token_Name = Name_Class
116 if Token = Tok_Arrow then
123 Restore_Scan_State (Scan_State);
124 Error_Msg_SC ("|aspect specification is an Ada 2012 feature");
125 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
131 Restore_Scan_State (Scan_State);
133 end Aspect_Specifications_Present;
135 --------------------------------------------
136 -- 13.1 Representation Clause (also I.7) --
137 --------------------------------------------
139 -- REPRESENTATION_CLAUSE ::=
140 -- ATTRIBUTE_DEFINITION_CLAUSE
141 -- | ENUMERATION_REPRESENTATION_CLAUSE
142 -- | RECORD_REPRESENTATION_CLAUSE
145 -- ATTRIBUTE_DEFINITION_CLAUSE ::=
146 -- for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
147 -- | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
149 -- Note: in Ada 83, the expression must be a simple expression
151 -- AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
153 -- Note: in Ada 83, the expression must be a simple expression
155 -- ENUMERATION_REPRESENTATION_CLAUSE ::=
156 -- for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
158 -- ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
160 -- RECORD_REPRESENTATION_CLAUSE ::=
161 -- for first_subtype_LOCAL_NAME use
162 -- record [MOD_CLAUSE]
163 -- {COMPONENT_CLAUSE}
166 -- Note: for now we allow only a direct name as the local name in the
167 -- above constructs. This probably needs changing later on ???
169 -- The caller has checked that the initial token is FOR
171 -- Error recovery: cannot raise Error_Resync, if an error occurs,
172 -- the scan is repositioned past the next semicolon.
174 function P_Representation_Clause return Node_Id is
175 For_Loc : Source_Ptr;
177 Prefix_Node : Node_Id;
179 Identifier_Node : Node_Id;
180 Rep_Clause_Node : Node_Id;
182 Record_Items : List_Id;
185 For_Loc := Token_Ptr;
188 -- Note that the name in a representation clause is always a simple
189 -- name, even in the attribute case, see AI-300 which made this so!
191 Identifier_Node := P_Identifier (C_Use);
193 -- Check case of qualified name to give good error message
195 if Token = Tok_Dot then
197 ("representation clause requires simple name!");
200 exit when Token /= Tok_Dot;
202 Discard_Junk_Node (P_Identifier);
206 -- Attribute Definition Clause
208 if Token = Tok_Apostrophe then
210 -- Allow local names of the form a'b'.... This enables
211 -- us to parse class-wide streams attributes correctly.
213 Name_Node := Identifier_Node;
214 while Token = Tok_Apostrophe loop
216 Scan; -- past apostrophe
218 Identifier_Node := Token_Node;
219 Attr_Name := No_Name;
221 if Token = Tok_Identifier then
222 Attr_Name := Token_Name;
224 if not Is_Attribute_Name (Attr_Name) then
225 Signal_Bad_Attribute;
229 Style.Check_Attribute_Name (False);
232 -- Here for case of attribute designator is not an identifier
235 if Token = Tok_Delta then
236 Attr_Name := Name_Delta;
238 elsif Token = Tok_Digits then
239 Attr_Name := Name_Digits;
241 elsif Token = Tok_Access then
242 Attr_Name := Name_Access;
245 Error_Msg_AP ("attribute designator expected");
250 Style.Check_Attribute_Name (True);
254 -- We come here with an OK attribute scanned, and the
255 -- corresponding Attribute identifier node stored in Ident_Node.
257 Prefix_Node := Name_Node;
258 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
259 Set_Prefix (Name_Node, Prefix_Node);
260 Set_Attribute_Name (Name_Node, Attr_Name);
264 Rep_Clause_Node := New_Node (N_Attribute_Definition_Clause, For_Loc);
265 Set_Name (Rep_Clause_Node, Prefix_Node);
266 Set_Chars (Rep_Clause_Node, Attr_Name);
269 Expr_Node := P_Expression_No_Right_Paren;
270 Check_Simple_Expression_In_Ada_83 (Expr_Node);
271 Set_Expression (Rep_Clause_Node, Expr_Node);
275 Rep_Clause_Node := Empty;
277 -- AT follows USE (At Clause)
279 if Token = Tok_At then
281 Rep_Clause_Node := New_Node (N_At_Clause, For_Loc);
282 Set_Identifier (Rep_Clause_Node, Identifier_Node);
283 Expr_Node := P_Expression_No_Right_Paren;
284 Check_Simple_Expression_In_Ada_83 (Expr_Node);
285 Set_Expression (Rep_Clause_Node, Expr_Node);
287 -- RECORD follows USE (Record Representation Clause)
289 elsif Token = Tok_Record then
290 Record_Items := P_Pragmas_Opt;
292 New_Node (N_Record_Representation_Clause, For_Loc);
293 Set_Identifier (Rep_Clause_Node, Identifier_Node);
296 Scope.Table (Scope.Last).Etyp := E_Record;
297 Scope.Table (Scope.Last).Ecol := Start_Column;
298 Scope.Table (Scope.Last).Sloc := Token_Ptr;
300 Record_Items := P_Pragmas_Opt;
302 -- Possible Mod Clause
304 if Token = Tok_At then
305 Set_Mod_Clause (Rep_Clause_Node, P_Mod_Clause);
306 Set_Pragmas_Before (Mod_Clause (Rep_Clause_Node), Record_Items);
307 Record_Items := P_Pragmas_Opt;
310 if No (Record_Items) then
311 Record_Items := New_List;
314 Set_Component_Clauses (Rep_Clause_Node, Record_Items);
316 -- Loop through component clauses
319 if Token not in Token_Class_Name then
323 Append (P_Component_Clause, Record_Items);
324 P_Pragmas_Opt (Record_Items);
327 -- Left paren follows USE (Enumeration Representation Clause)
329 elsif Token = Tok_Left_Paren then
331 New_Node (N_Enumeration_Representation_Clause, For_Loc);
332 Set_Identifier (Rep_Clause_Node, Identifier_Node);
333 Set_Array_Aggregate (Rep_Clause_Node, P_Aggregate);
335 -- Some other token follows FOR (invalid representation clause)
338 Error_Msg_SC ("invalid representation clause");
344 return Rep_Clause_Node;
348 Resync_Past_Semicolon;
351 end P_Representation_Clause;
353 ----------------------
354 -- 13.1 Local Name --
355 ----------------------
357 -- Local name is always parsed by its parent. In the case of its use in
358 -- pragmas, the check for a local name is handled in Par.Prag and allows
359 -- all the possible forms of local name. For the uses in chapter 13, we
360 -- currently only allow a direct name, but this should probably change???
362 ---------------------------
363 -- 13.1 At Clause (I.7) --
364 ---------------------------
366 -- Parsed by P_Representation_Clause (13.1)
368 ---------------------------------------
369 -- 13.3 Attribute Definition Clause --
370 ---------------------------------------
372 -- Parsed by P_Representation_Clause (13.1)
374 --------------------------------
375 -- 13.1 Aspect Specification --
376 --------------------------------
378 -- ASPECT_SPECIFICATION ::=
379 -- with ASPECT_MARK [=> ASPECT_DEFINITION] {,
380 -- ASPECT_MARK [=> ASPECT_DEFINITION] }
382 -- ASPECT_MARK ::= aspect_IDENTIFIER['Class]
384 -- ASPECT_DEFINITION ::= NAME | EXPRESSION
386 -- Error recovery: cannot raise Error_Resync
388 procedure P_Aspect_Specifications
390 Semicolon : Boolean := True)
399 -- Check if aspect specification present
401 if not Aspect_Specifications_Present then
409 -- Aspect Specification is present
414 -- Here we have an aspect specification to scan, note that we don't
415 -- set the flag till later, because it may turn out that we have no
416 -- valid aspects in the list.
418 Aspects := Empty_List;
422 if Token /= Tok_Identifier then
423 Error_Msg_SC ("aspect identifier expected");
426 Resync_Past_Semicolon;
432 -- We have an identifier (which should be an aspect identifier)
434 A_Id := Get_Aspect_Id (Token_Name);
436 Make_Aspect_Specification (Token_Ptr,
437 Identifier => Token_Node);
439 -- No valid aspect identifier present
441 if A_Id = No_Aspect then
442 Error_Msg_SC ("aspect identifier expected");
444 -- Check bad spelling
446 for J in Aspect_Id loop
447 if Is_Bad_Spelling_Of (Token_Name, Aspect_Names (J)) then
448 Error_Msg_Name_1 := Aspect_Names (J);
449 Error_Msg_SC -- CODEFIX
450 ("\possible misspelling of%");
455 Scan; -- past incorrect identifier
457 if Token = Tok_Apostrophe then
459 Scan; -- past presumably CLASS
462 if Token = Tok_Arrow then
464 Set_Expression (Aspect, P_Expression);
467 elsif Token = Tok_Comma then
472 Resync_Past_Semicolon;
481 Scan; -- past identifier
483 -- Check for 'Class present
485 if Token = Tok_Apostrophe then
486 if not Class_Aspect_OK (A_Id) then
487 Error_Msg_Node_1 := Identifier (Aspect);
488 Error_Msg_SC ("aspect& does not permit attribute here");
489 Scan; -- past apostrophe
490 Scan; -- past presumed CLASS
494 Scan; -- past apostrophe
496 if Token /= Tok_Identifier
497 or else Token_Name /= Name_Class
499 Error_Msg_SC ("Class attribute expected here");
502 if Token = Tok_Identifier then
503 Scan; -- past identifier not CLASS
508 Set_Class_Present (Aspect);
513 -- Test case of missing aspect definition
516 or else Token = Tok_Semicolon
518 if Aspect_Argument (A_Id) /= Optional then
519 Error_Msg_Node_1 := Identifier (Aspect);
520 Error_Msg_AP ("aspect& requires an aspect definition");
524 elsif not Semicolon and then Token /= Tok_Arrow then
525 if Aspect_Argument (A_Id) /= Optional then
527 -- The name or expression may be there, but the arrow is
528 -- missing. Skip to the end of the declaration.
534 -- Here we have an aspect definition
537 if Token = Tok_Arrow then
544 if Aspect_Argument (A_Id) = Name then
545 Set_Expression (Aspect, P_Name);
547 Set_Expression (Aspect, P_Expression);
551 -- If OK clause scanned, add it to the list
554 Append (Aspect, Aspects);
557 if Token = Tok_Comma then
561 -- Recognize the case where a comma is missing between two
562 -- aspects, issue an error and proceed with next aspect.
564 elsif Token = Tok_Identifier
565 and then Get_Aspect_Id (Token_Name) /= No_Aspect
568 Scan_State : Saved_Scan_State;
571 Save_Scan_State (Scan_State);
572 Scan; -- past identifier
574 if Token = Tok_Arrow then
575 Restore_Scan_State (Scan_State);
576 Error_Msg_AP -- CODEFIX
581 Restore_Scan_State (Scan_State);
585 -- Recognize the case where a semicolon was mistyped for a comma
586 -- between two aspects, issue an error and proceed with next
589 elsif Token = Tok_Semicolon then
591 Scan_State : Saved_Scan_State;
594 Save_Scan_State (Scan_State);
595 Scan; -- past semicolon
597 if Token = Tok_Identifier
598 and then Get_Aspect_Id (Token_Name) /= No_Aspect
600 Scan; -- past identifier
602 if Token = Tok_Arrow then
603 Restore_Scan_State (Scan_State);
604 Error_Msg_SC -- CODEFIX
605 ("|"";"" should be "",""");
606 Scan; -- past semicolon
610 Restore_Scan_State (Scan_State);
614 Restore_Scan_State (Scan_State);
619 -- Must be terminator character
632 -- Here if aspects present
634 if Is_Non_Empty_List (Aspects) then
636 -- If Decl is Empty, we just ignore the aspects (the caller in this
637 -- case has always issued an appropriate error message).
642 -- If Decl is Error, we ignore the aspects, and issue a message
644 elsif Decl = Error then
645 Error_Msg ("aspect specifications not allowed here", Ptr);
647 -- Here aspects are allowed, and we store them
650 Set_Parent (Aspects, Decl);
651 Set_Aspect_Specifications (Decl, Aspects);
654 end P_Aspect_Specifications;
656 ---------------------------------------------
657 -- 13.4 Enumeration Representation Clause --
658 ---------------------------------------------
660 -- Parsed by P_Representation_Clause (13.1)
662 ---------------------------------
663 -- 13.4 Enumeration Aggregate --
664 ---------------------------------
666 -- Parsed by P_Representation_Clause (13.1)
668 ------------------------------------------
669 -- 13.5.1 Record Representation Clause --
670 ------------------------------------------
672 -- Parsed by P_Representation_Clause (13.1)
674 ------------------------------
675 -- 13.5.1 Mod Clause (I.8) --
676 ------------------------------
678 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
680 -- Note: in Ada 83, the expression must be a simple expression
682 -- The caller has checked that the initial Token is AT
684 -- Error recovery: cannot raise Error_Resync
686 -- Note: the caller is responsible for setting the Pragmas_Before field
688 function P_Mod_Clause return Node_Id is
693 Mod_Node := New_Node (N_Mod_Clause, Token_Ptr);
696 Expr_Node := P_Expression_No_Right_Paren;
697 Check_Simple_Expression_In_Ada_83 (Expr_Node);
698 Set_Expression (Mod_Node, Expr_Node);
703 ------------------------------
704 -- 13.5.1 Component Clause --
705 ------------------------------
707 -- COMPONENT_CLAUSE ::=
708 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
709 -- range FIRST_BIT .. LAST_BIT;
711 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
712 -- component_DIRECT_NAME
713 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
714 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
716 -- POSITION ::= static_EXPRESSION
718 -- Note: in Ada 83, the expression must be a simple expression
720 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
721 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
723 -- Note: the AARM V2.0 grammar has an error at this point, it uses
724 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
726 -- Error recovery: cannot raise Error_Resync
728 function P_Component_Clause return Node_Id is
729 Component_Node : Node_Id;
734 Component_Node := New_Node (N_Component_Clause, Token_Ptr);
737 if Nkind (Comp_Name) = N_Identifier
738 or else Nkind (Comp_Name) = N_Attribute_Reference
740 Set_Component_Name (Component_Node, Comp_Name);
743 ("component name must be direct name or attribute", Comp_Name);
744 Set_Component_Name (Component_Node, Error);
747 Set_Sloc (Component_Node, Token_Ptr);
749 Expr_Node := P_Expression_No_Right_Paren;
750 Check_Simple_Expression_In_Ada_83 (Expr_Node);
751 Set_Position (Component_Node, Expr_Node);
753 Expr_Node := P_Expression_No_Right_Paren;
754 Check_Simple_Expression_In_Ada_83 (Expr_Node);
755 Set_First_Bit (Component_Node, Expr_Node);
757 Expr_Node := P_Expression_No_Right_Paren;
758 Check_Simple_Expression_In_Ada_83 (Expr_Node);
759 Set_Last_Bit (Component_Node, Expr_Node);
761 return Component_Node;
762 end P_Component_Clause;
764 ----------------------
765 -- 13.5.1 Position --
766 ----------------------
768 -- Parsed by P_Component_Clause (13.5.1)
770 -----------------------
771 -- 13.5.1 First Bit --
772 -----------------------
774 -- Parsed by P_Component_Clause (13.5.1)
776 ----------------------
777 -- 13.5.1 Last Bit --
778 ----------------------
780 -- Parsed by P_Component_Clause (13.5.1)
782 --------------------------
783 -- 13.8 Code Statement --
784 --------------------------
786 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
788 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
789 -- single argument, and the scan points to the apostrophe.
791 -- Error recovery: can raise Error_Resync
793 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id is
797 Scan; -- past apostrophe
799 -- If left paren, then we have a possible code statement
801 if Token = Tok_Left_Paren then
802 Node1 := New_Node (N_Code_Statement, Sloc (Subtype_Mark));
803 Set_Expression (Node1, P_Qualified_Expression (Subtype_Mark));
807 -- Otherwise we have an illegal range attribute. Note that P_Name
808 -- ensures that Token = Tok_Range is the only possibility left here.
810 else -- Token = Tok_Range
811 Error_Msg_SC ("RANGE attribute illegal here!");
815 end P_Code_Statement;