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
43 (Strict
: Boolean := Ada_Version
< Ada_2012
) return Boolean
45 Scan_State
: Saved_Scan_State
;
49 Save_Scan_State
(Scan_State
);
51 -- If we have a semicolon, test for semicolon followed by Aspect
52 -- Specifications, in which case we decide the semicolon is accidental.
54 if Token
= Tok_Semicolon
then
55 Scan
; -- past semicolon
57 -- The recursive test is set Strict, since we already have one
58 -- error (the unexpected semicolon), so we will ignore that semicolon
59 -- only if we absolutely definitely have an aspect specification
62 if Aspect_Specifications_Present
(Strict
=> True) then
63 Error_Msg_SP
("|extra "";"" ignored");
67 Restore_Scan_State
(Scan_State
);
72 -- Definitely must have WITH to consider aspect specs to be present
74 if Token
/= Tok_With
then
78 -- Have a WITH, see if it looks like an aspect specification
80 Save_Scan_State
(Scan_State
);
83 -- If no identifier, then consider that we definitely do not have an
84 -- aspect specification.
86 if Token
/= Tok_Identifier
then
89 -- This is where we pay attention to the Strict mode. Normally when we
90 -- are in Ada 2012 mode, Strict is False, and we consider that we have
91 -- an aspect specification if the identifier is an aspect name (even if
92 -- not followed by =>) or the identifier is not an aspect name but is
93 -- followed by =>. P_Aspect_Specifications will generate messages if the
94 -- aspect specification is ill-formed.
97 if Get_Aspect_Id
(Token_Name
) /= No_Aspect
then
100 Scan
; -- past identifier
101 Result
:= Token
= Tok_Arrow
;
104 -- If earlier than Ada 2012, check for valid aspect identifier followed
105 -- by an arrow, and consider that this is still an aspect specification
106 -- so we give an appropriate message.
109 if Get_Aspect_Id
(Token_Name
) = No_Aspect
then
113 Scan
; -- past aspect name
115 if Token
/= Tok_Arrow
then
119 Restore_Scan_State
(Scan_State
);
120 Error_Msg_SC
("|aspect specification is an Ada 2012 feature");
121 Error_Msg_SC
("\|unit must be compiled with -gnat2012 switch");
127 Restore_Scan_State
(Scan_State
);
129 end Aspect_Specifications_Present
;
131 --------------------------------------------
132 -- 13.1 Representation Clause (also I.7) --
133 --------------------------------------------
135 -- REPRESENTATION_CLAUSE ::=
136 -- ATTRIBUTE_DEFINITION_CLAUSE
137 -- | ENUMERATION_REPRESENTATION_CLAUSE
138 -- | RECORD_REPRESENTATION_CLAUSE
141 -- ATTRIBUTE_DEFINITION_CLAUSE ::=
142 -- for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
143 -- | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
145 -- Note: in Ada 83, the expression must be a simple expression
147 -- AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
149 -- Note: in Ada 83, the expression must be a simple expression
151 -- ENUMERATION_REPRESENTATION_CLAUSE ::=
152 -- for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
154 -- ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
156 -- RECORD_REPRESENTATION_CLAUSE ::=
157 -- for first_subtype_LOCAL_NAME use
158 -- record [MOD_CLAUSE]
159 -- {COMPONENT_CLAUSE}
162 -- Note: for now we allow only a direct name as the local name in the
163 -- above constructs. This probably needs changing later on ???
165 -- The caller has checked that the initial token is FOR
167 -- Error recovery: cannot raise Error_Resync, if an error occurs,
168 -- the scan is repositioned past the next semicolon.
170 function P_Representation_Clause
return Node_Id
is
171 For_Loc
: Source_Ptr
;
173 Prefix_Node
: Node_Id
;
175 Identifier_Node
: Node_Id
;
176 Rep_Clause_Node
: Node_Id
;
178 Record_Items
: List_Id
;
181 For_Loc
:= Token_Ptr
;
184 -- Note that the name in a representation clause is always a simple
185 -- name, even in the attribute case, see AI-300 which made this so!
187 Identifier_Node
:= P_Identifier
(C_Use
);
189 -- Check case of qualified name to give good error message
191 if Token
= Tok_Dot
then
193 ("representation clause requires simple name!");
196 exit when Token
/= Tok_Dot
;
198 Discard_Junk_Node
(P_Identifier
);
202 -- Attribute Definition Clause
204 if Token
= Tok_Apostrophe
then
206 -- Allow local names of the form a'b'.... This enables
207 -- us to parse class-wide streams attributes correctly.
209 Name_Node
:= Identifier_Node
;
210 while Token
= Tok_Apostrophe
loop
212 Scan
; -- past apostrophe
214 Identifier_Node
:= Token_Node
;
215 Attr_Name
:= No_Name
;
217 if Token
= Tok_Identifier
then
218 Attr_Name
:= Token_Name
;
220 if not Is_Attribute_Name
(Attr_Name
) then
221 Signal_Bad_Attribute
;
225 Style
.Check_Attribute_Name
(False);
228 -- Here for case of attribute designator is not an identifier
231 if Token
= Tok_Delta
then
232 Attr_Name
:= Name_Delta
;
234 elsif Token
= Tok_Digits
then
235 Attr_Name
:= Name_Digits
;
237 elsif Token
= Tok_Access
then
238 Attr_Name
:= Name_Access
;
241 Error_Msg_AP
("attribute designator expected");
246 Style
.Check_Attribute_Name
(True);
250 -- We come here with an OK attribute scanned, and the
251 -- corresponding Attribute identifier node stored in Ident_Node.
253 Prefix_Node
:= Name_Node
;
254 Name_Node
:= New_Node
(N_Attribute_Reference
, Prev_Token_Ptr
);
255 Set_Prefix
(Name_Node
, Prefix_Node
);
256 Set_Attribute_Name
(Name_Node
, Attr_Name
);
260 Rep_Clause_Node
:= New_Node
(N_Attribute_Definition_Clause
, For_Loc
);
261 Set_Name
(Rep_Clause_Node
, Prefix_Node
);
262 Set_Chars
(Rep_Clause_Node
, Attr_Name
);
265 Expr_Node
:= P_Expression_No_Right_Paren
;
266 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
267 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
271 Rep_Clause_Node
:= Empty
;
273 -- AT follows USE (At Clause)
275 if Token
= Tok_At
then
277 Rep_Clause_Node
:= New_Node
(N_At_Clause
, For_Loc
);
278 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
279 Expr_Node
:= P_Expression_No_Right_Paren
;
280 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
281 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
283 -- RECORD follows USE (Record Representation Clause)
285 elsif Token
= Tok_Record
then
286 Record_Items
:= P_Pragmas_Opt
;
288 New_Node
(N_Record_Representation_Clause
, For_Loc
);
289 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
292 Scope
.Table
(Scope
.Last
).Etyp
:= E_Record
;
293 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
294 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
296 Record_Items
:= P_Pragmas_Opt
;
298 -- Possible Mod Clause
300 if Token
= Tok_At
then
301 Set_Mod_Clause
(Rep_Clause_Node
, P_Mod_Clause
);
302 Set_Pragmas_Before
(Mod_Clause
(Rep_Clause_Node
), Record_Items
);
303 Record_Items
:= P_Pragmas_Opt
;
306 if No
(Record_Items
) then
307 Record_Items
:= New_List
;
310 Set_Component_Clauses
(Rep_Clause_Node
, Record_Items
);
312 -- Loop through component clauses
315 if Token
not in Token_Class_Name
then
319 Append
(P_Component_Clause
, Record_Items
);
320 P_Pragmas_Opt
(Record_Items
);
323 -- Left paren follows USE (Enumeration Representation Clause)
325 elsif Token
= Tok_Left_Paren
then
327 New_Node
(N_Enumeration_Representation_Clause
, For_Loc
);
328 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
329 Set_Array_Aggregate
(Rep_Clause_Node
, P_Aggregate
);
331 -- Some other token follows FOR (invalid representation clause)
334 Error_Msg_SC
("invalid representation clause");
340 return Rep_Clause_Node
;
344 Resync_Past_Semicolon
;
347 end P_Representation_Clause
;
349 ----------------------
350 -- 13.1 Local Name --
351 ----------------------
353 -- Local name is always parsed by its parent. In the case of its use in
354 -- pragmas, the check for a local name is handled in Par.Prag and allows
355 -- all the possible forms of local name. For the uses in chapter 13, we
356 -- currently only allow a direct name, but this should probably change???
358 ---------------------------
359 -- 13.1 At Clause (I.7) --
360 ---------------------------
362 -- Parsed by P_Representation_Clause (13.1)
364 ---------------------------------------
365 -- 13.3 Attribute Definition Clause --
366 ---------------------------------------
368 -- Parsed by P_Representation_Clause (13.1)
370 --------------------------------
371 -- 13.1 Aspect Specification --
372 --------------------------------
374 -- ASPECT_SPECIFICATION ::=
375 -- with ASPECT_MARK [=> ASPECT_DEFINITION] {.
376 -- ASPECT_MARK [=> ASPECT_DEFINITION] }
378 -- ASPECT_MARK ::= aspect_IDENTIFIER['Class]
380 -- ASPECT_DEFINITION ::= NAME | EXPRESSION
382 -- Error recovery: cannot raise Error_Resync
384 procedure P_Aspect_Specifications
(Decl
: Node_Id
) is
392 -- Check if aspect specification present
394 if not Aspect_Specifications_Present
then
399 -- Aspect Specification is present
404 -- Here we have an aspect specification to scan, note that we don;t
405 -- set the flag till later, because it may turn out that we have no
406 -- valid aspects in the list.
408 Aspects
:= Empty_List
;
412 if Token
/= Tok_Identifier
then
413 Error_Msg_SC
("aspect identifier expected");
414 Resync_Past_Semicolon
;
418 -- We have an identifier (which should be an aspect identifier)
420 A_Id
:= Get_Aspect_Id
(Token_Name
);
422 Make_Aspect_Specification
(Token_Ptr
,
423 Identifier
=> Token_Node
);
425 -- No valid aspect identifier present
427 if A_Id
= No_Aspect
then
428 Error_Msg_SC
("aspect identifier expected");
430 if Token
= Tok_Apostrophe
then
432 Scan
; -- past presumably CLASS
435 if Token
= Tok_Arrow
then
437 Set_Expression
(Aspect
, P_Expression
);
440 elsif Token
= Tok_Comma
then
444 Resync_Past_Semicolon
;
451 Scan
; -- past identifier
453 -- Check for 'Class present
455 if Token
= Tok_Apostrophe
then
456 if not Class_Aspect_OK
(A_Id
) then
457 Error_Msg_Node_1
:= Identifier
(Aspect
);
458 Error_Msg_SC
("aspect& does not permit attribute here");
459 Scan
; -- past apostrophe
460 Scan
; -- past presumed CLASS
464 Scan
; -- past apostrophe
466 if Token
/= Tok_Identifier
467 or else Token_Name
/= Name_Class
469 Error_Msg_SC
("Class attribute expected here");
472 if Token
= Tok_Identifier
then
473 Scan
; -- past identifier not CLASS
478 Set_Class_Present
(Aspect
);
483 -- Test case of missing aspect definition
485 if Token
= Tok_Comma
or else Token
= Tok_Semicolon
then
486 if Aspect_Argument
(A_Id
) /= Optional
then
487 Error_Msg_Node_1
:= Aspect
;
488 Error_Msg_AP
("aspect& requires an aspect definition");
492 -- Here we have an aspect definition
495 if Token
= Tok_Arrow
then
502 if Aspect_Argument
(A_Id
) = Name
then
503 Set_Expression
(Aspect
, P_Name
);
505 Set_Expression
(Aspect
, P_Expression
);
509 -- If OK clause scanned, add it to the list
512 Append
(Aspect
, Aspects
);
515 if Token
= Tok_Comma
then
524 -- If aspects scanned, store them
526 if Is_Non_Empty_List
(Aspects
) then
528 Error_Msg
("aspect specifications not allowed here", Ptr
);
530 Set_Parent
(Aspects
, Decl
);
531 Set_Aspect_Specifications
(Decl
, Aspects
);
534 end P_Aspect_Specifications
;
536 ---------------------------------------------
537 -- 13.4 Enumeration Representation Clause --
538 ---------------------------------------------
540 -- Parsed by P_Representation_Clause (13.1)
542 ---------------------------------
543 -- 13.4 Enumeration Aggregate --
544 ---------------------------------
546 -- Parsed by P_Representation_Clause (13.1)
548 ------------------------------------------
549 -- 13.5.1 Record Representation Clause --
550 ------------------------------------------
552 -- Parsed by P_Representation_Clause (13.1)
554 ------------------------------
555 -- 13.5.1 Mod Clause (I.8) --
556 ------------------------------
558 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
560 -- Note: in Ada 83, the expression must be a simple expression
562 -- The caller has checked that the initial Token is AT
564 -- Error recovery: cannot raise Error_Resync
566 -- Note: the caller is responsible for setting the Pragmas_Before field
568 function P_Mod_Clause
return Node_Id
is
573 Mod_Node
:= New_Node
(N_Mod_Clause
, Token_Ptr
);
576 Expr_Node
:= P_Expression_No_Right_Paren
;
577 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
578 Set_Expression
(Mod_Node
, Expr_Node
);
583 ------------------------------
584 -- 13.5.1 Component Clause --
585 ------------------------------
587 -- COMPONENT_CLAUSE ::=
588 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
589 -- range FIRST_BIT .. LAST_BIT;
591 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
592 -- component_DIRECT_NAME
593 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
594 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
596 -- POSITION ::= static_EXPRESSION
598 -- Note: in Ada 83, the expression must be a simple expression
600 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
601 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
603 -- Note: the AARM V2.0 grammar has an error at this point, it uses
604 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
606 -- Error recovery: cannot raise Error_Resync
608 function P_Component_Clause
return Node_Id
is
609 Component_Node
: Node_Id
;
614 Component_Node
:= New_Node
(N_Component_Clause
, Token_Ptr
);
617 if Nkind
(Comp_Name
) = N_Identifier
618 or else Nkind
(Comp_Name
) = N_Attribute_Reference
620 Set_Component_Name
(Component_Node
, Comp_Name
);
623 ("component name must be direct name or attribute", Comp_Name
);
624 Set_Component_Name
(Component_Node
, Error
);
627 Set_Sloc
(Component_Node
, Token_Ptr
);
629 Expr_Node
:= P_Expression_No_Right_Paren
;
630 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
631 Set_Position
(Component_Node
, Expr_Node
);
633 Expr_Node
:= P_Expression_No_Right_Paren
;
634 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
635 Set_First_Bit
(Component_Node
, Expr_Node
);
637 Expr_Node
:= P_Expression_No_Right_Paren
;
638 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
639 Set_Last_Bit
(Component_Node
, Expr_Node
);
641 return Component_Node
;
642 end P_Component_Clause
;
644 ----------------------
645 -- 13.5.1 Position --
646 ----------------------
648 -- Parsed by P_Component_Clause (13.5.1)
650 -----------------------
651 -- 13.5.1 First Bit --
652 -----------------------
654 -- Parsed by P_Component_Clause (13.5.1)
656 ----------------------
657 -- 13.5.1 Last Bit --
658 ----------------------
660 -- Parsed by P_Component_Clause (13.5.1)
662 --------------------------
663 -- 13.8 Code Statement --
664 --------------------------
666 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
668 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
669 -- single argument, and the scan points to the apostrophe.
671 -- Error recovery: can raise Error_Resync
673 function P_Code_Statement
(Subtype_Mark
: Node_Id
) return Node_Id
is
677 Scan
; -- past apostrophe
679 -- If left paren, then we have a possible code statement
681 if Token
= Tok_Left_Paren
then
682 Node1
:= New_Node
(N_Code_Statement
, Sloc
(Subtype_Mark
));
683 Set_Expression
(Node1
, P_Qualified_Expression
(Subtype_Mark
));
687 -- Otherwise we have an illegal range attribute. Note that P_Name
688 -- ensures that Token = Tok_Range is the only possibility left here.
690 else -- Token = Tok_Range
691 Error_Msg_SC
("RANGE attribute illegal here!");
695 end P_Code_Statement
;