1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 return Boolean is
43 Scan_State : Saved_Scan_State;
47 Save_Scan_State (Scan_State);
49 -- If we have a semicolon, test for semicolon followed by Aspect
50 -- Specifications, in which case we decide the semicolon is accidental.
52 if Token = Tok_Semicolon then
53 Scan; -- past semicolon
55 if Aspect_Specifications_Present then
56 Error_Msg_SP ("|extra "";"" ignored");
60 Restore_Scan_State (Scan_State);
65 -- Definitely must have WITH to consider aspect specs to be present
67 if Token /= Tok_With then
71 -- Have a WITH, see if it looks like an aspect specification
73 Save_Scan_State (Scan_State);
76 -- If no identifier, then consider that we definitely do not have an
77 -- aspect specification.
79 if Token /= Tok_Identifier then
82 -- In Ada 2012 mode, we are less strict, and we consider that we have
83 -- an aspect specification if the identifier is an aspect name (even if
84 -- not followed by =>) or the identifier is not an aspect name but is
85 -- followed by =>. P_Aspect_Specifications will generate messages if the
86 -- aspect specification is ill-formed.
88 elsif Ada_Version >= Ada_2012 then
89 if Get_Aspect_Id (Token_Name) /= No_Aspect then
92 Scan; -- past identifier
93 Result := Token = Tok_Arrow;
96 -- If earlier than Ada 2012, check for valid aspect identifier followed
97 -- by an arrow, and consider that this is still an aspect specification
98 -- so we give an appropriate message.
101 if Get_Aspect_Id (Token_Name) = No_Aspect then
105 Scan; -- past aspect name
107 if Token /= Tok_Arrow then
111 Restore_Scan_State (Scan_State);
112 Error_Msg_SC ("|aspect specification is an Ada 2012 feature");
113 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
119 Restore_Scan_State (Scan_State);
121 end Aspect_Specifications_Present;
123 --------------------------------------------
124 -- 13.1 Representation Clause (also I.7) --
125 --------------------------------------------
127 -- REPRESENTATION_CLAUSE ::=
128 -- ATTRIBUTE_DEFINITION_CLAUSE
129 -- | ENUMERATION_REPRESENTATION_CLAUSE
130 -- | RECORD_REPRESENTATION_CLAUSE
133 -- ATTRIBUTE_DEFINITION_CLAUSE ::=
134 -- for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
135 -- | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
137 -- Note: in Ada 83, the expression must be a simple expression
139 -- AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
141 -- Note: in Ada 83, the expression must be a simple expression
143 -- ENUMERATION_REPRESENTATION_CLAUSE ::=
144 -- for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
146 -- ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
148 -- RECORD_REPRESENTATION_CLAUSE ::=
149 -- for first_subtype_LOCAL_NAME use
150 -- record [MOD_CLAUSE]
151 -- {COMPONENT_CLAUSE}
154 -- Note: for now we allow only a direct name as the local name in the
155 -- above constructs. This probably needs changing later on ???
157 -- The caller has checked that the initial token is FOR
159 -- Error recovery: cannot raise Error_Resync, if an error occurs,
160 -- the scan is repositioned past the next semicolon.
162 function P_Representation_Clause return Node_Id is
163 For_Loc : Source_Ptr;
165 Prefix_Node : Node_Id;
167 Identifier_Node : Node_Id;
168 Rep_Clause_Node : Node_Id;
170 Record_Items : List_Id;
173 For_Loc := Token_Ptr;
176 -- Note that the name in a representation clause is always a simple
177 -- name, even in the attribute case, see AI-300 which made this so!
179 Identifier_Node := P_Identifier (C_Use);
181 -- Check case of qualified name to give good error message
183 if Token = Tok_Dot then
185 ("representation clause requires simple name!");
188 exit when Token /= Tok_Dot;
190 Discard_Junk_Node (P_Identifier);
194 -- Attribute Definition Clause
196 if Token = Tok_Apostrophe then
198 -- Allow local names of the form a'b'.... This enables
199 -- us to parse class-wide streams attributes correctly.
201 Name_Node := Identifier_Node;
202 while Token = Tok_Apostrophe loop
204 Scan; -- past apostrophe
206 Identifier_Node := Token_Node;
207 Attr_Name := No_Name;
209 if Token = Tok_Identifier then
210 Attr_Name := Token_Name;
212 if not Is_Attribute_Name (Attr_Name) then
213 Signal_Bad_Attribute;
217 Style.Check_Attribute_Name (False);
220 -- Here for case of attribute designator is not an identifier
223 if Token = Tok_Delta then
224 Attr_Name := Name_Delta;
226 elsif Token = Tok_Digits then
227 Attr_Name := Name_Digits;
229 elsif Token = Tok_Access then
230 Attr_Name := Name_Access;
233 Error_Msg_AP ("attribute designator expected");
238 Style.Check_Attribute_Name (True);
242 -- We come here with an OK attribute scanned, and the
243 -- corresponding Attribute identifier node stored in Ident_Node.
245 Prefix_Node := Name_Node;
246 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
247 Set_Prefix (Name_Node, Prefix_Node);
248 Set_Attribute_Name (Name_Node, Attr_Name);
252 Rep_Clause_Node := New_Node (N_Attribute_Definition_Clause, For_Loc);
253 Set_Name (Rep_Clause_Node, Prefix_Node);
254 Set_Chars (Rep_Clause_Node, Attr_Name);
257 Expr_Node := P_Expression_No_Right_Paren;
258 Check_Simple_Expression_In_Ada_83 (Expr_Node);
259 Set_Expression (Rep_Clause_Node, Expr_Node);
263 Rep_Clause_Node := Empty;
265 -- AT follows USE (At Clause)
267 if Token = Tok_At then
269 Rep_Clause_Node := New_Node (N_At_Clause, For_Loc);
270 Set_Identifier (Rep_Clause_Node, Identifier_Node);
271 Expr_Node := P_Expression_No_Right_Paren;
272 Check_Simple_Expression_In_Ada_83 (Expr_Node);
273 Set_Expression (Rep_Clause_Node, Expr_Node);
275 -- RECORD follows USE (Record Representation Clause)
277 elsif Token = Tok_Record then
278 Record_Items := P_Pragmas_Opt;
280 New_Node (N_Record_Representation_Clause, For_Loc);
281 Set_Identifier (Rep_Clause_Node, Identifier_Node);
284 Scope.Table (Scope.Last).Etyp := E_Record;
285 Scope.Table (Scope.Last).Ecol := Start_Column;
286 Scope.Table (Scope.Last).Sloc := Token_Ptr;
288 Record_Items := P_Pragmas_Opt;
290 -- Possible Mod Clause
292 if Token = Tok_At then
293 Set_Mod_Clause (Rep_Clause_Node, P_Mod_Clause);
294 Set_Pragmas_Before (Mod_Clause (Rep_Clause_Node), Record_Items);
295 Record_Items := P_Pragmas_Opt;
298 if No (Record_Items) then
299 Record_Items := New_List;
302 Set_Component_Clauses (Rep_Clause_Node, Record_Items);
304 -- Loop through component clauses
307 if Token not in Token_Class_Name then
311 Append (P_Component_Clause, Record_Items);
312 P_Pragmas_Opt (Record_Items);
315 -- Left paren follows USE (Enumeration Representation Clause)
317 elsif Token = Tok_Left_Paren then
319 New_Node (N_Enumeration_Representation_Clause, For_Loc);
320 Set_Identifier (Rep_Clause_Node, Identifier_Node);
321 Set_Array_Aggregate (Rep_Clause_Node, P_Aggregate);
323 -- Some other token follows FOR (invalid representation clause)
326 Error_Msg_SC ("invalid representation clause");
332 return Rep_Clause_Node;
336 Resync_Past_Semicolon;
339 end P_Representation_Clause;
341 ----------------------
342 -- 13.1 Local Name --
343 ----------------------
345 -- Local name is always parsed by its parent. In the case of its use in
346 -- pragmas, the check for a local name is handled in Par.Prag and allows
347 -- all the possible forms of local name. For the uses in chapter 13, we
348 -- currently only allow a direct name, but this should probably change???
350 ---------------------------
351 -- 13.1 At Clause (I.7) --
352 ---------------------------
354 -- Parsed by P_Representation_Clause (13.1)
356 ---------------------------------------
357 -- 13.3 Attribute Definition Clause --
358 ---------------------------------------
360 -- Parsed by P_Representation_Clause (13.1)
362 ------------------------------
363 -- 13.1 Aspect Specifation --
364 ------------------------------
366 -- ASPECT_SPECIFICATION ::=
367 -- with ASPECT_MARK [=> ASPECT_DEFINITION] {.
368 -- ASPECT_MARK [=> ASPECT_DEFINITION] }
370 -- ASPECT_MARK ::= aspect_IDENTIFIER['Class]
372 -- ASPECT_DEFINITION ::= NAME | EXPRESSION
374 -- Error recovery: cannot raise Error_Resync
376 procedure P_Aspect_Specifications (Decl : Node_Id) is
384 -- Check if aspect specification present
386 if not Aspect_Specifications_Present then
391 -- Aspect Specification is present
396 -- Here we have an aspect specification to scan, note that we don;t
397 -- set the flag till later, because it may turn out that we have no
398 -- valid aspects in the list.
400 Aspects := Empty_List;
404 if Token /= Tok_Identifier then
405 Error_Msg_SC ("aspect identifier expected");
406 Resync_Past_Semicolon;
410 -- We have an identifier (which should be an aspect identifier)
412 A_Id := Get_Aspect_Id (Token_Name);
414 Make_Aspect_Specification (Token_Ptr,
415 Identifier => Token_Node);
417 -- No valid aspect identifier present
419 if A_Id = No_Aspect then
420 Error_Msg_SC ("aspect identifier expected");
422 if Token = Tok_Apostrophe then
424 Scan; -- past presumably CLASS
427 if Token = Tok_Arrow then
429 Set_Expression (Aspect, P_Expression);
432 elsif Token = Tok_Comma then
436 Resync_Past_Semicolon;
443 Scan; -- past identifier
445 -- Check for 'Class present
447 if Token = Tok_Apostrophe then
448 if not Class_Aspect_OK (A_Id) then
449 Error_Msg_Node_1 := Identifier (Aspect);
450 Error_Msg_SC ("aspect& does not permit attribute here");
451 Scan; -- past apostophe
452 Scan; -- past presumed CLASS
456 Scan; -- past apostrophe
458 if Token /= Tok_Identifier
459 or else Token_Name /= Name_Class
461 Error_Msg_SC ("Class attribute expected here");
464 if Token = Tok_Identifier then
465 Scan; -- past identifier not CLASS
470 Set_Class_Present (Aspect);
475 -- Test case of missing aspect definition
477 if Token = Tok_Comma or else Token = Tok_Semicolon then
478 if Aspect_Argument (A_Id) /= Optional then
479 Error_Msg_Node_1 := Aspect;
480 Error_Msg_AP ("aspect& requires an aspect definition");
484 -- Here we have an aspect definition
487 if Token = Tok_Arrow then
494 if Aspect_Argument (A_Id) = Name then
495 Set_Expression (Aspect, P_Name);
497 Set_Expression (Aspect, P_Expression);
501 -- If OK clause scanned, add it to the list
504 Append (Aspect, Aspects);
507 if Token = Tok_Comma then
516 -- If aspects scanned, store them
518 if Is_Non_Empty_List (Aspects) then
520 Error_Msg ("aspect specifications not allowed here", Ptr);
522 Set_Parent (Aspects, Decl);
523 Set_Aspect_Specifications (Decl, Aspects);
526 end P_Aspect_Specifications;
528 ---------------------------------------------
529 -- 13.4 Enumeration Representation Clause --
530 ---------------------------------------------
532 -- Parsed by P_Representation_Clause (13.1)
534 ---------------------------------
535 -- 13.4 Enumeration Aggregate --
536 ---------------------------------
538 -- Parsed by P_Representation_Clause (13.1)
540 ------------------------------------------
541 -- 13.5.1 Record Representation Clause --
542 ------------------------------------------
544 -- Parsed by P_Representation_Clause (13.1)
546 ------------------------------
547 -- 13.5.1 Mod Clause (I.8) --
548 ------------------------------
550 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
552 -- Note: in Ada 83, the expression must be a simple expression
554 -- The caller has checked that the initial Token is AT
556 -- Error recovery: cannot raise Error_Resync
558 -- Note: the caller is responsible for setting the Pragmas_Before field
560 function P_Mod_Clause return Node_Id is
565 Mod_Node := New_Node (N_Mod_Clause, Token_Ptr);
568 Expr_Node := P_Expression_No_Right_Paren;
569 Check_Simple_Expression_In_Ada_83 (Expr_Node);
570 Set_Expression (Mod_Node, Expr_Node);
575 ------------------------------
576 -- 13.5.1 Component Clause --
577 ------------------------------
579 -- COMPONENT_CLAUSE ::=
580 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
581 -- range FIRST_BIT .. LAST_BIT;
583 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
584 -- component_DIRECT_NAME
585 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
586 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
588 -- POSITION ::= static_EXPRESSION
590 -- Note: in Ada 83, the expression must be a simple expression
592 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
593 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
595 -- Note: the AARM V2.0 grammar has an error at this point, it uses
596 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
598 -- Error recovery: cannot raise Error_Resync
600 function P_Component_Clause return Node_Id is
601 Component_Node : Node_Id;
606 Component_Node := New_Node (N_Component_Clause, Token_Ptr);
609 if Nkind (Comp_Name) = N_Identifier
610 or else Nkind (Comp_Name) = N_Attribute_Reference
612 Set_Component_Name (Component_Node, Comp_Name);
615 ("component name must be direct name or attribute", Comp_Name);
616 Set_Component_Name (Component_Node, Error);
619 Set_Sloc (Component_Node, Token_Ptr);
621 Expr_Node := P_Expression_No_Right_Paren;
622 Check_Simple_Expression_In_Ada_83 (Expr_Node);
623 Set_Position (Component_Node, Expr_Node);
625 Expr_Node := P_Expression_No_Right_Paren;
626 Check_Simple_Expression_In_Ada_83 (Expr_Node);
627 Set_First_Bit (Component_Node, Expr_Node);
629 Expr_Node := P_Expression_No_Right_Paren;
630 Check_Simple_Expression_In_Ada_83 (Expr_Node);
631 Set_Last_Bit (Component_Node, Expr_Node);
633 return Component_Node;
634 end P_Component_Clause;
636 ----------------------
637 -- 13.5.1 Position --
638 ----------------------
640 -- Parsed by P_Component_Clause (13.5.1)
642 -----------------------
643 -- 13.5.1 First Bit --
644 -----------------------
646 -- Parsed by P_Component_Clause (13.5.1)
648 ----------------------
649 -- 13.5.1 Last Bit --
650 ----------------------
652 -- Parsed by P_Component_Clause (13.5.1)
654 --------------------------
655 -- 13.8 Code Statement --
656 --------------------------
658 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
660 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
661 -- single argument, and the scan points to the apostrophe.
663 -- Error recovery: can raise Error_Resync
665 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id is
669 Scan; -- past apostrophe
671 -- If left paren, then we have a possible code statement
673 if Token = Tok_Left_Paren then
674 Node1 := New_Node (N_Code_Statement, Sloc (Subtype_Mark));
675 Set_Expression (Node1, P_Qualified_Expression (Subtype_Mark));
679 -- Otherwise we have an illegal range attribute. Note that P_Name
680 -- ensures that Token = Tok_Range is the only possibility left here.
682 else -- Token = Tok_Range
683 Error_Msg_SC ("RANGE attribute illegal here!");
687 end P_Code_Statement;