1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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
517 or else (not Semicolon and then Token /= Tok_Arrow)
519 if Aspect_Argument (A_Id) /= Optional then
520 Error_Msg_Node_1 := Aspect;
521 Error_Msg_AP ("aspect& requires an aspect definition");
525 -- Here we have an aspect definition
528 if Token = Tok_Arrow then
535 if Aspect_Argument (A_Id) = Name then
536 Set_Expression (Aspect, P_Name);
538 Set_Expression (Aspect, P_Expression);
542 -- If OK clause scanned, add it to the list
545 Append (Aspect, Aspects);
548 if Token = Tok_Comma then
551 -- Must be terminator character
563 -- Here if aspects present
565 if Is_Non_Empty_List (Aspects) then
567 -- If Decl is Empty, we just ignore the aspects (the caller in this
568 -- case has always issued an appropriate error message).
573 -- If Decl is Error, we ignore the aspects, and issue a message
575 elsif Decl = Error then
576 Error_Msg ("aspect specifications not allowed here", Ptr);
578 -- Here aspects are allowed, and we store them
581 Set_Parent (Aspects, Decl);
582 Set_Aspect_Specifications (Decl, Aspects);
585 end P_Aspect_Specifications;
587 ---------------------------------------------
588 -- 13.4 Enumeration Representation Clause --
589 ---------------------------------------------
591 -- Parsed by P_Representation_Clause (13.1)
593 ---------------------------------
594 -- 13.4 Enumeration Aggregate --
595 ---------------------------------
597 -- Parsed by P_Representation_Clause (13.1)
599 ------------------------------------------
600 -- 13.5.1 Record Representation Clause --
601 ------------------------------------------
603 -- Parsed by P_Representation_Clause (13.1)
605 ------------------------------
606 -- 13.5.1 Mod Clause (I.8) --
607 ------------------------------
609 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
611 -- Note: in Ada 83, the expression must be a simple expression
613 -- The caller has checked that the initial Token is AT
615 -- Error recovery: cannot raise Error_Resync
617 -- Note: the caller is responsible for setting the Pragmas_Before field
619 function P_Mod_Clause return Node_Id is
624 Mod_Node := New_Node (N_Mod_Clause, Token_Ptr);
627 Expr_Node := P_Expression_No_Right_Paren;
628 Check_Simple_Expression_In_Ada_83 (Expr_Node);
629 Set_Expression (Mod_Node, Expr_Node);
634 ------------------------------
635 -- 13.5.1 Component Clause --
636 ------------------------------
638 -- COMPONENT_CLAUSE ::=
639 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
640 -- range FIRST_BIT .. LAST_BIT;
642 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
643 -- component_DIRECT_NAME
644 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
645 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
647 -- POSITION ::= static_EXPRESSION
649 -- Note: in Ada 83, the expression must be a simple expression
651 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
652 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
654 -- Note: the AARM V2.0 grammar has an error at this point, it uses
655 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
657 -- Error recovery: cannot raise Error_Resync
659 function P_Component_Clause return Node_Id is
660 Component_Node : Node_Id;
665 Component_Node := New_Node (N_Component_Clause, Token_Ptr);
668 if Nkind (Comp_Name) = N_Identifier
669 or else Nkind (Comp_Name) = N_Attribute_Reference
671 Set_Component_Name (Component_Node, Comp_Name);
674 ("component name must be direct name or attribute", Comp_Name);
675 Set_Component_Name (Component_Node, Error);
678 Set_Sloc (Component_Node, Token_Ptr);
680 Expr_Node := P_Expression_No_Right_Paren;
681 Check_Simple_Expression_In_Ada_83 (Expr_Node);
682 Set_Position (Component_Node, Expr_Node);
684 Expr_Node := P_Expression_No_Right_Paren;
685 Check_Simple_Expression_In_Ada_83 (Expr_Node);
686 Set_First_Bit (Component_Node, Expr_Node);
688 Expr_Node := P_Expression_No_Right_Paren;
689 Check_Simple_Expression_In_Ada_83 (Expr_Node);
690 Set_Last_Bit (Component_Node, Expr_Node);
692 return Component_Node;
693 end P_Component_Clause;
695 ----------------------
696 -- 13.5.1 Position --
697 ----------------------
699 -- Parsed by P_Component_Clause (13.5.1)
701 -----------------------
702 -- 13.5.1 First Bit --
703 -----------------------
705 -- Parsed by P_Component_Clause (13.5.1)
707 ----------------------
708 -- 13.5.1 Last Bit --
709 ----------------------
711 -- Parsed by P_Component_Clause (13.5.1)
713 --------------------------
714 -- 13.8 Code Statement --
715 --------------------------
717 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
719 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
720 -- single argument, and the scan points to the apostrophe.
722 -- Error recovery: can raise Error_Resync
724 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id is
728 Scan; -- past apostrophe
730 -- If left paren, then we have a possible code statement
732 if Token = Tok_Left_Paren then
733 Node1 := New_Node (N_Code_Statement, Sloc (Subtype_Mark));
734 Set_Expression (Node1, P_Qualified_Expression (Subtype_Mark));
738 -- Otherwise we have an illegal range attribute. Note that P_Name
739 -- ensures that Token = Tok_Range is the only possibility left here.
741 else -- Token = Tok_Range
742 Error_Msg_SC ("RANGE attribute illegal here!");
746 end P_Code_Statement;