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 -- Get_Aspect_Specifications --
137 -------------------------------
139 function Get_Aspect_Specifications
140 (Semicolon
: Boolean := True) return List_Id
148 Aspects
:= Empty_List
;
150 -- Check if aspect specification present
152 if not Aspect_Specifications_Present
then
161 Aspects
:= Empty_List
;
166 if Token
/= Tok_Identifier
then
167 Error_Msg_SC
("aspect identifier expected");
170 Resync_Past_Semicolon
;
176 -- We have an identifier (which should be an aspect identifier)
178 A_Id
:= Get_Aspect_Id
(Token_Name
);
180 Make_Aspect_Specification
(Token_Ptr
,
181 Identifier
=> Token_Node
);
183 -- No valid aspect identifier present
185 if A_Id
= No_Aspect
then
186 Error_Msg_SC
("aspect identifier expected");
188 -- Check bad spelling
190 for J
in Aspect_Id_Exclude_No_Aspect
loop
191 if Is_Bad_Spelling_Of
(Token_Name
, Aspect_Names
(J
)) then
192 Error_Msg_Name_1
:= Aspect_Names
(J
);
193 Error_Msg_SC
-- CODEFIX
194 ("\possible misspelling of%");
199 Scan
; -- past incorrect identifier
201 if Token
= Tok_Apostrophe
then
203 Scan
; -- past presumably CLASS
206 if Token
= Tok_Arrow
then
208 Set_Expression
(Aspect
, P_Expression
);
211 elsif Token
= Tok_Comma
then
216 Resync_Past_Semicolon
;
225 Scan
; -- past identifier
227 -- Check for 'Class present
229 if Token
= Tok_Apostrophe
then
230 if not Class_Aspect_OK
(A_Id
) then
231 Error_Msg_Node_1
:= Identifier
(Aspect
);
232 Error_Msg_SC
("aspect& does not permit attribute here");
233 Scan
; -- past apostrophe
234 Scan
; -- past presumed CLASS
238 Scan
; -- past apostrophe
240 if Token
/= Tok_Identifier
241 or else Token_Name
/= Name_Class
243 Error_Msg_SC
("Class attribute expected here");
246 if Token
= Tok_Identifier
then
247 Scan
; -- past identifier not CLASS
252 Set_Class_Present
(Aspect
);
257 -- Test case of missing aspect definition
260 or else Token
= Tok_Semicolon
262 if Aspect_Argument
(A_Id
) /= Optional
then
263 Error_Msg_Node_1
:= Identifier
(Aspect
);
264 Error_Msg_AP
("aspect& requires an aspect definition");
268 elsif not Semicolon
and then Token
/= Tok_Arrow
then
269 if Aspect_Argument
(A_Id
) /= Optional
then
271 -- The name or expression may be there, but the arrow is
272 -- missing. Skip to the end of the declaration.
278 -- Here we have an aspect definition
281 if Token
= Tok_Arrow
then
288 if Aspect_Argument
(A_Id
) = Name
then
289 Set_Expression
(Aspect
, P_Name
);
291 Set_Expression
(Aspect
, P_Expression
);
295 -- If OK clause scanned, add it to the list
298 Append
(Aspect
, Aspects
);
301 if Token
= Tok_Comma
then
305 -- Recognize the case where a comma is missing between two
306 -- aspects, issue an error and proceed with next aspect.
308 elsif Token
= Tok_Identifier
309 and then Get_Aspect_Id
(Token_Name
) /= No_Aspect
312 Scan_State
: Saved_Scan_State
;
315 Save_Scan_State
(Scan_State
);
316 Scan
; -- past identifier
318 if Token
= Tok_Arrow
then
319 Restore_Scan_State
(Scan_State
);
320 Error_Msg_AP
-- CODEFIX
325 Restore_Scan_State
(Scan_State
);
329 -- Recognize the case where a semicolon was mistyped for a comma
330 -- between two aspects, issue an error and proceed with next
333 elsif Token
= Tok_Semicolon
then
335 Scan_State
: Saved_Scan_State
;
338 Save_Scan_State
(Scan_State
);
339 Scan
; -- past semicolon
341 if Token
= Tok_Identifier
342 and then Get_Aspect_Id
(Token_Name
) /= No_Aspect
344 Scan
; -- past identifier
346 if Token
= Tok_Arrow
then
347 Restore_Scan_State
(Scan_State
);
348 Error_Msg_SC
-- CODEFIX
349 ("|"";"" should be "",""");
350 Scan
; -- past semicolon
354 Restore_Scan_State
(Scan_State
);
358 Restore_Scan_State
(Scan_State
);
363 -- Must be terminator character
378 end Get_Aspect_Specifications
;
380 --------------------------------------------
381 -- 13.1 Representation Clause (also I.7) --
382 --------------------------------------------
384 -- REPRESENTATION_CLAUSE ::=
385 -- ATTRIBUTE_DEFINITION_CLAUSE
386 -- | ENUMERATION_REPRESENTATION_CLAUSE
387 -- | RECORD_REPRESENTATION_CLAUSE
390 -- ATTRIBUTE_DEFINITION_CLAUSE ::=
391 -- for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
392 -- | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
394 -- Note: in Ada 83, the expression must be a simple expression
396 -- AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
398 -- Note: in Ada 83, the expression must be a simple expression
400 -- ENUMERATION_REPRESENTATION_CLAUSE ::=
401 -- for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
403 -- ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
405 -- RECORD_REPRESENTATION_CLAUSE ::=
406 -- for first_subtype_LOCAL_NAME use
407 -- record [MOD_CLAUSE]
408 -- {COMPONENT_CLAUSE}
411 -- Note: for now we allow only a direct name as the local name in the
412 -- above constructs. This probably needs changing later on ???
414 -- The caller has checked that the initial token is FOR
416 -- Error recovery: cannot raise Error_Resync, if an error occurs,
417 -- the scan is repositioned past the next semicolon.
419 function P_Representation_Clause
return Node_Id
is
420 For_Loc
: Source_Ptr
;
422 Prefix_Node
: Node_Id
;
424 Identifier_Node
: Node_Id
;
425 Rep_Clause_Node
: Node_Id
;
427 Record_Items
: List_Id
;
430 For_Loc
:= Token_Ptr
;
433 -- Note that the name in a representation clause is always a simple
434 -- name, even in the attribute case, see AI-300 which made this so!
436 Identifier_Node
:= P_Identifier
(C_Use
);
438 -- Check case of qualified name to give good error message
440 if Token
= Tok_Dot
then
442 ("representation clause requires simple name!");
445 exit when Token
/= Tok_Dot
;
447 Discard_Junk_Node
(P_Identifier
);
451 -- Attribute Definition Clause
453 if Token
= Tok_Apostrophe
then
455 -- Allow local names of the form a'b'.... This enables
456 -- us to parse class-wide streams attributes correctly.
458 Name_Node
:= Identifier_Node
;
459 while Token
= Tok_Apostrophe
loop
461 Scan
; -- past apostrophe
463 Identifier_Node
:= Token_Node
;
464 Attr_Name
:= No_Name
;
466 if Token
= Tok_Identifier
then
467 Attr_Name
:= Token_Name
;
469 -- Note that the parser must complain in case of an internal
470 -- attribute name that comes from source since internal names
471 -- are meant to be used only by the compiler.
473 if not Is_Attribute_Name
(Attr_Name
)
474 and then (not Is_Internal_Attribute_Name
(Attr_Name
)
475 or else Comes_From_Source
(Token_Node
))
477 Signal_Bad_Attribute
;
481 Style
.Check_Attribute_Name
(False);
484 -- Here for case of attribute designator is not an identifier
487 if Token
= Tok_Delta
then
488 Attr_Name
:= Name_Delta
;
490 elsif Token
= Tok_Digits
then
491 Attr_Name
:= Name_Digits
;
493 elsif Token
= Tok_Access
then
494 Attr_Name
:= Name_Access
;
497 Error_Msg_AP
("attribute designator expected");
502 Style
.Check_Attribute_Name
(True);
506 -- We come here with an OK attribute scanned, and the
507 -- corresponding Attribute identifier node stored in Ident_Node.
509 Prefix_Node
:= Name_Node
;
510 Name_Node
:= New_Node
(N_Attribute_Reference
, Prev_Token_Ptr
);
511 Set_Prefix
(Name_Node
, Prefix_Node
);
512 Set_Attribute_Name
(Name_Node
, Attr_Name
);
516 Rep_Clause_Node
:= New_Node
(N_Attribute_Definition_Clause
, For_Loc
);
517 Set_Name
(Rep_Clause_Node
, Prefix_Node
);
518 Set_Chars
(Rep_Clause_Node
, Attr_Name
);
521 Expr_Node
:= P_Expression_No_Right_Paren
;
522 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
523 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
527 Rep_Clause_Node
:= Empty
;
529 -- AT follows USE (At Clause)
531 if Token
= Tok_At
then
533 Rep_Clause_Node
:= New_Node
(N_At_Clause
, For_Loc
);
534 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
535 Expr_Node
:= P_Expression_No_Right_Paren
;
536 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
537 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
539 -- RECORD follows USE (Record Representation Clause)
541 elsif Token
= Tok_Record
then
542 Record_Items
:= P_Pragmas_Opt
;
544 New_Node
(N_Record_Representation_Clause
, For_Loc
);
545 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
548 Scope
.Table
(Scope
.Last
).Etyp
:= E_Record
;
549 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
550 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
552 Record_Items
:= P_Pragmas_Opt
;
554 -- Possible Mod Clause
556 if Token
= Tok_At
then
557 Set_Mod_Clause
(Rep_Clause_Node
, P_Mod_Clause
);
558 Set_Pragmas_Before
(Mod_Clause
(Rep_Clause_Node
), Record_Items
);
559 Record_Items
:= P_Pragmas_Opt
;
562 if No
(Record_Items
) then
563 Record_Items
:= New_List
;
566 Set_Component_Clauses
(Rep_Clause_Node
, Record_Items
);
568 -- Loop through component clauses
571 if Token
not in Token_Class_Name
then
575 Append
(P_Component_Clause
, Record_Items
);
576 P_Pragmas_Opt
(Record_Items
);
579 -- Left paren follows USE (Enumeration Representation Clause)
581 elsif Token
= Tok_Left_Paren
then
583 New_Node
(N_Enumeration_Representation_Clause
, For_Loc
);
584 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
585 Set_Array_Aggregate
(Rep_Clause_Node
, P_Aggregate
);
587 -- Some other token follows FOR (invalid representation clause)
590 Error_Msg_SC
("invalid representation clause");
596 return Rep_Clause_Node
;
600 Resync_Past_Semicolon
;
603 end P_Representation_Clause
;
605 ----------------------
606 -- 13.1 Local Name --
607 ----------------------
609 -- Local name is always parsed by its parent. In the case of its use in
610 -- pragmas, the check for a local name is handled in Par.Prag and allows
611 -- all the possible forms of local name. For the uses in chapter 13, we
612 -- currently only allow a direct name, but this should probably change???
614 ---------------------------
615 -- 13.1 At Clause (I.7) --
616 ---------------------------
618 -- Parsed by P_Representation_Clause (13.1)
620 ---------------------------------------
621 -- 13.3 Attribute Definition Clause --
622 ---------------------------------------
624 -- Parsed by P_Representation_Clause (13.1)
626 --------------------------------
627 -- 13.1 Aspect Specification --
628 --------------------------------
630 -- ASPECT_SPECIFICATION ::=
631 -- with ASPECT_MARK [=> ASPECT_DEFINITION] {,
632 -- ASPECT_MARK [=> ASPECT_DEFINITION] }
634 -- ASPECT_MARK ::= aspect_IDENTIFIER['Class]
636 -- ASPECT_DEFINITION ::= NAME | EXPRESSION
638 -- Error recovery: cannot raise Error_Resync
640 procedure P_Aspect_Specifications
642 Semicolon
: Boolean := True)
648 -- Aspect Specification is present
652 -- Here we have an aspect specification to scan, note that we don't
653 -- set the flag till later, because it may turn out that we have no
654 -- valid aspects in the list.
656 Aspects
:= Get_Aspect_Specifications
(Semicolon
);
658 -- Here if aspects present
660 if Is_Non_Empty_List
(Aspects
) then
662 -- If Decl is Empty, we just ignore the aspects (the caller in this
663 -- case has always issued an appropriate error message).
668 -- If Decl is Error, we ignore the aspects, and issue a message
670 elsif Decl
= Error
then
671 Error_Msg
("aspect specifications not allowed here", Ptr
);
673 -- Here aspects are allowed, and we store them
676 Set_Parent
(Aspects
, Decl
);
677 Set_Aspect_Specifications
(Decl
, Aspects
);
680 end P_Aspect_Specifications
;
682 ---------------------------------------------
683 -- 13.4 Enumeration Representation Clause --
684 ---------------------------------------------
686 -- Parsed by P_Representation_Clause (13.1)
688 ---------------------------------
689 -- 13.4 Enumeration Aggregate --
690 ---------------------------------
692 -- Parsed by P_Representation_Clause (13.1)
694 ------------------------------------------
695 -- 13.5.1 Record Representation Clause --
696 ------------------------------------------
698 -- Parsed by P_Representation_Clause (13.1)
700 ------------------------------
701 -- 13.5.1 Mod Clause (I.8) --
702 ------------------------------
704 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
706 -- Note: in Ada 83, the expression must be a simple expression
708 -- The caller has checked that the initial Token is AT
710 -- Error recovery: cannot raise Error_Resync
712 -- Note: the caller is responsible for setting the Pragmas_Before field
714 function P_Mod_Clause
return Node_Id
is
719 Mod_Node
:= New_Node
(N_Mod_Clause
, Token_Ptr
);
722 Expr_Node
:= P_Expression_No_Right_Paren
;
723 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
724 Set_Expression
(Mod_Node
, Expr_Node
);
729 ------------------------------
730 -- 13.5.1 Component Clause --
731 ------------------------------
733 -- COMPONENT_CLAUSE ::=
734 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
735 -- range FIRST_BIT .. LAST_BIT;
737 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
738 -- component_DIRECT_NAME
739 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
740 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
742 -- POSITION ::= static_EXPRESSION
744 -- Note: in Ada 83, the expression must be a simple expression
746 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
747 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
749 -- Note: the AARM V2.0 grammar has an error at this point, it uses
750 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
752 -- Error recovery: cannot raise Error_Resync
754 function P_Component_Clause
return Node_Id
is
755 Component_Node
: Node_Id
;
760 Component_Node
:= New_Node
(N_Component_Clause
, Token_Ptr
);
763 if Nkind
(Comp_Name
) = N_Identifier
764 or else Nkind
(Comp_Name
) = N_Attribute_Reference
766 Set_Component_Name
(Component_Node
, Comp_Name
);
769 ("component name must be direct name or attribute", Comp_Name
);
770 Set_Component_Name
(Component_Node
, Error
);
773 Set_Sloc
(Component_Node
, Token_Ptr
);
775 Expr_Node
:= P_Expression_No_Right_Paren
;
776 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
777 Set_Position
(Component_Node
, Expr_Node
);
779 Expr_Node
:= P_Expression_No_Right_Paren
;
780 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
781 Set_First_Bit
(Component_Node
, Expr_Node
);
783 Expr_Node
:= P_Expression_No_Right_Paren
;
784 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
785 Set_Last_Bit
(Component_Node
, Expr_Node
);
787 return Component_Node
;
788 end P_Component_Clause
;
790 ----------------------
791 -- 13.5.1 Position --
792 ----------------------
794 -- Parsed by P_Component_Clause (13.5.1)
796 -----------------------
797 -- 13.5.1 First Bit --
798 -----------------------
800 -- Parsed by P_Component_Clause (13.5.1)
802 ----------------------
803 -- 13.5.1 Last Bit --
804 ----------------------
806 -- Parsed by P_Component_Clause (13.5.1)
808 --------------------------
809 -- 13.8 Code Statement --
810 --------------------------
812 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
814 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
815 -- single argument, and the scan points to the apostrophe.
817 -- Error recovery: can raise Error_Resync
819 function P_Code_Statement
(Subtype_Mark
: Node_Id
) return Node_Id
is
823 Scan
; -- past apostrophe
825 -- If left paren, then we have a possible code statement
827 if Token
= Tok_Left_Paren
then
828 Node1
:= New_Node
(N_Code_Statement
, Sloc
(Subtype_Mark
));
829 Set_Expression
(Node1
, P_Qualified_Expression
(Subtype_Mark
));
833 -- Otherwise we have an illegal range attribute. Note that P_Name
834 -- ensures that Token = Tok_Range is the only possibility left here.
837 Error_Msg_SC
("RANGE attribute illegal here!");
840 end P_Code_Statement
;