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 -- The identifier may be the name of a boolean aspect with a
109 -- defaulted True value. Further checks when analyzing aspect
112 elsif Token
= Tok_Comma
then
115 elsif Token
= Tok_Apostrophe
then
116 Scan
; -- past apostrophe
118 if Token
= Tok_Identifier
119 and then Token_Name
= Name_Class
123 if Token
= Tok_Arrow
then
130 Restore_Scan_State
(Scan_State
);
131 Error_Msg_SC
("|aspect specification is an Ada 2012 feature");
132 Error_Msg_SC
("\|unit must be compiled with -gnat2012 switch");
138 Restore_Scan_State
(Scan_State
);
140 end Aspect_Specifications_Present
;
142 -------------------------------
143 -- Get_Aspect_Specifications --
144 -------------------------------
146 function Get_Aspect_Specifications
147 (Semicolon
: Boolean := True) return List_Id
155 Aspects
:= Empty_List
;
157 -- Check if aspect specification present
159 if not Aspect_Specifications_Present
then
168 Aspects
:= Empty_List
;
173 if Token
/= Tok_Identifier
then
174 Error_Msg_SC
("aspect identifier expected");
177 Resync_Past_Semicolon
;
183 -- We have an identifier (which should be an aspect identifier)
185 A_Id
:= Get_Aspect_Id
(Token_Name
);
187 Make_Aspect_Specification
(Token_Ptr
,
188 Identifier
=> Token_Node
);
190 -- No valid aspect identifier present
192 if A_Id
= No_Aspect
then
193 Error_Msg_SC
("aspect identifier expected");
195 -- Check bad spelling
197 for J
in Aspect_Id_Exclude_No_Aspect
loop
198 if Is_Bad_Spelling_Of
(Token_Name
, Aspect_Names
(J
)) then
199 Error_Msg_Name_1
:= Aspect_Names
(J
);
200 Error_Msg_SC
-- CODEFIX
201 ("\possible misspelling of%");
206 Scan
; -- past incorrect identifier
208 if Token
= Tok_Apostrophe
then
210 Scan
; -- past presumably CLASS
213 if Token
= Tok_Arrow
then
215 Set_Expression
(Aspect
, P_Expression
);
218 elsif Token
= Tok_Comma
then
223 Resync_Past_Semicolon
;
232 Scan
; -- past identifier
234 -- Check for 'Class present
236 if Token
= Tok_Apostrophe
then
237 if not Class_Aspect_OK
(A_Id
) then
238 Error_Msg_Node_1
:= Identifier
(Aspect
);
239 Error_Msg_SC
("aspect& does not permit attribute here");
240 Scan
; -- past apostrophe
241 Scan
; -- past presumed CLASS
245 Scan
; -- past apostrophe
247 if Token
/= Tok_Identifier
248 or else Token_Name
/= Name_Class
250 Error_Msg_SC
("Class attribute expected here");
253 if Token
= Tok_Identifier
then
254 Scan
; -- past identifier not CLASS
259 Set_Class_Present
(Aspect
);
264 -- Test case of missing aspect definition
267 or else Token
= Tok_Semicolon
269 if Aspect_Argument
(A_Id
) /= Optional
then
270 Error_Msg_Node_1
:= Identifier
(Aspect
);
271 Error_Msg_AP
("aspect& requires an aspect definition");
275 elsif not Semicolon
and then Token
/= Tok_Arrow
then
276 if Aspect_Argument
(A_Id
) /= Optional
then
278 -- The name or expression may be there, but the arrow is
279 -- missing. Skip to the end of the declaration.
285 -- Here we have an aspect definition
288 if Token
= Tok_Arrow
then
295 if Aspect_Argument
(A_Id
) = Name
then
296 Set_Expression
(Aspect
, P_Name
);
298 Set_Expression
(Aspect
, P_Expression
);
302 -- If OK clause scanned, add it to the list
305 Append
(Aspect
, Aspects
);
308 if Token
= Tok_Comma
then
312 -- Recognize the case where a comma is missing between two
313 -- aspects, issue an error and proceed with next aspect.
315 elsif Token
= Tok_Identifier
316 and then Get_Aspect_Id
(Token_Name
) /= No_Aspect
319 Scan_State
: Saved_Scan_State
;
322 Save_Scan_State
(Scan_State
);
323 Scan
; -- past identifier
325 if Token
= Tok_Arrow
then
326 Restore_Scan_State
(Scan_State
);
327 Error_Msg_AP
-- CODEFIX
332 Restore_Scan_State
(Scan_State
);
336 -- Recognize the case where a semicolon was mistyped for a comma
337 -- between two aspects, issue an error and proceed with next
340 elsif Token
= Tok_Semicolon
then
342 Scan_State
: Saved_Scan_State
;
345 Save_Scan_State
(Scan_State
);
346 Scan
; -- past semicolon
348 if Token
= Tok_Identifier
349 and then Get_Aspect_Id
(Token_Name
) /= No_Aspect
351 Scan
; -- past identifier
353 if Token
= Tok_Arrow
then
354 Restore_Scan_State
(Scan_State
);
355 Error_Msg_SC
-- CODEFIX
356 ("|"";"" should be "",""");
357 Scan
; -- past semicolon
361 Restore_Scan_State
(Scan_State
);
365 Restore_Scan_State
(Scan_State
);
370 -- Must be terminator character
385 end Get_Aspect_Specifications
;
387 --------------------------------------------
388 -- 13.1 Representation Clause (also I.7) --
389 --------------------------------------------
391 -- REPRESENTATION_CLAUSE ::=
392 -- ATTRIBUTE_DEFINITION_CLAUSE
393 -- | ENUMERATION_REPRESENTATION_CLAUSE
394 -- | RECORD_REPRESENTATION_CLAUSE
397 -- ATTRIBUTE_DEFINITION_CLAUSE ::=
398 -- for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
399 -- | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
401 -- Note: in Ada 83, the expression must be a simple expression
403 -- AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
405 -- Note: in Ada 83, the expression must be a simple expression
407 -- ENUMERATION_REPRESENTATION_CLAUSE ::=
408 -- for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
410 -- ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
412 -- RECORD_REPRESENTATION_CLAUSE ::=
413 -- for first_subtype_LOCAL_NAME use
414 -- record [MOD_CLAUSE]
415 -- {COMPONENT_CLAUSE}
418 -- Note: for now we allow only a direct name as the local name in the
419 -- above constructs. This probably needs changing later on ???
421 -- The caller has checked that the initial token is FOR
423 -- Error recovery: cannot raise Error_Resync, if an error occurs,
424 -- the scan is repositioned past the next semicolon.
426 function P_Representation_Clause
return Node_Id
is
427 For_Loc
: Source_Ptr
;
429 Prefix_Node
: Node_Id
;
431 Identifier_Node
: Node_Id
;
432 Rep_Clause_Node
: Node_Id
;
434 Record_Items
: List_Id
;
437 For_Loc
:= Token_Ptr
;
440 -- Note that the name in a representation clause is always a simple
441 -- name, even in the attribute case, see AI-300 which made this so!
443 Identifier_Node
:= P_Identifier
(C_Use
);
445 -- Check case of qualified name to give good error message
447 if Token
= Tok_Dot
then
449 ("representation clause requires simple name!");
452 exit when Token
/= Tok_Dot
;
454 Discard_Junk_Node
(P_Identifier
);
458 -- Attribute Definition Clause
460 if Token
= Tok_Apostrophe
then
462 -- Allow local names of the form a'b'.... This enables
463 -- us to parse class-wide streams attributes correctly.
465 Name_Node
:= Identifier_Node
;
466 while Token
= Tok_Apostrophe
loop
468 Scan
; -- past apostrophe
470 Identifier_Node
:= Token_Node
;
471 Attr_Name
:= No_Name
;
473 if Token
= Tok_Identifier
then
474 Attr_Name
:= Token_Name
;
476 -- Note that the parser must complain in case of an internal
477 -- attribute name that comes from source since internal names
478 -- are meant to be used only by the compiler.
480 if not Is_Attribute_Name
(Attr_Name
)
481 and then (not Is_Internal_Attribute_Name
(Attr_Name
)
482 or else Comes_From_Source
(Token_Node
))
484 Signal_Bad_Attribute
;
488 Style
.Check_Attribute_Name
(False);
491 -- Here for case of attribute designator is not an identifier
494 if Token
= Tok_Delta
then
495 Attr_Name
:= Name_Delta
;
497 elsif Token
= Tok_Digits
then
498 Attr_Name
:= Name_Digits
;
500 elsif Token
= Tok_Access
then
501 Attr_Name
:= Name_Access
;
504 Error_Msg_AP
("attribute designator expected");
509 Style
.Check_Attribute_Name
(True);
513 -- We come here with an OK attribute scanned, and the
514 -- corresponding Attribute identifier node stored in Ident_Node.
516 Prefix_Node
:= Name_Node
;
517 Name_Node
:= New_Node
(N_Attribute_Reference
, Prev_Token_Ptr
);
518 Set_Prefix
(Name_Node
, Prefix_Node
);
519 Set_Attribute_Name
(Name_Node
, Attr_Name
);
523 Rep_Clause_Node
:= New_Node
(N_Attribute_Definition_Clause
, For_Loc
);
524 Set_Name
(Rep_Clause_Node
, Prefix_Node
);
525 Set_Chars
(Rep_Clause_Node
, Attr_Name
);
528 Expr_Node
:= P_Expression_No_Right_Paren
;
529 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
530 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
534 Rep_Clause_Node
:= Empty
;
536 -- AT follows USE (At Clause)
538 if Token
= Tok_At
then
540 Rep_Clause_Node
:= New_Node
(N_At_Clause
, For_Loc
);
541 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
542 Expr_Node
:= P_Expression_No_Right_Paren
;
543 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
544 Set_Expression
(Rep_Clause_Node
, Expr_Node
);
546 -- RECORD follows USE (Record Representation Clause)
548 elsif Token
= Tok_Record
then
549 Record_Items
:= P_Pragmas_Opt
;
551 New_Node
(N_Record_Representation_Clause
, For_Loc
);
552 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
555 Scope
.Table
(Scope
.Last
).Etyp
:= E_Record
;
556 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
557 Scope
.Table
(Scope
.Last
).Sloc
:= Token_Ptr
;
559 Record_Items
:= P_Pragmas_Opt
;
561 -- Possible Mod Clause
563 if Token
= Tok_At
then
564 Set_Mod_Clause
(Rep_Clause_Node
, P_Mod_Clause
);
565 Set_Pragmas_Before
(Mod_Clause
(Rep_Clause_Node
), Record_Items
);
566 Record_Items
:= P_Pragmas_Opt
;
569 if No
(Record_Items
) then
570 Record_Items
:= New_List
;
573 Set_Component_Clauses
(Rep_Clause_Node
, Record_Items
);
575 -- Loop through component clauses
578 if Token
not in Token_Class_Name
then
582 Append
(P_Component_Clause
, Record_Items
);
583 P_Pragmas_Opt
(Record_Items
);
586 -- Left paren follows USE (Enumeration Representation Clause)
588 elsif Token
= Tok_Left_Paren
then
590 New_Node
(N_Enumeration_Representation_Clause
, For_Loc
);
591 Set_Identifier
(Rep_Clause_Node
, Identifier_Node
);
592 Set_Array_Aggregate
(Rep_Clause_Node
, P_Aggregate
);
594 -- Some other token follows FOR (invalid representation clause)
597 Error_Msg_SC
("invalid representation clause");
603 return Rep_Clause_Node
;
607 Resync_Past_Semicolon
;
610 end P_Representation_Clause
;
612 ----------------------
613 -- 13.1 Local Name --
614 ----------------------
616 -- Local name is always parsed by its parent. In the case of its use in
617 -- pragmas, the check for a local name is handled in Par.Prag and allows
618 -- all the possible forms of local name. For the uses in chapter 13, we
619 -- currently only allow a direct name, but this should probably change???
621 ---------------------------
622 -- 13.1 At Clause (I.7) --
623 ---------------------------
625 -- Parsed by P_Representation_Clause (13.1)
627 ---------------------------------------
628 -- 13.3 Attribute Definition Clause --
629 ---------------------------------------
631 -- Parsed by P_Representation_Clause (13.1)
633 --------------------------------
634 -- 13.1 Aspect Specification --
635 --------------------------------
637 -- ASPECT_SPECIFICATION ::=
638 -- with ASPECT_MARK [=> ASPECT_DEFINITION] {,
639 -- ASPECT_MARK [=> ASPECT_DEFINITION] }
641 -- ASPECT_MARK ::= aspect_IDENTIFIER['Class]
643 -- ASPECT_DEFINITION ::= NAME | EXPRESSION
645 -- Error recovery: cannot raise Error_Resync
647 procedure P_Aspect_Specifications
649 Semicolon
: Boolean := True)
655 -- Aspect Specification is present
659 -- Here we have an aspect specification to scan, note that we don't
660 -- set the flag till later, because it may turn out that we have no
661 -- valid aspects in the list.
663 Aspects
:= Get_Aspect_Specifications
(Semicolon
);
665 -- Here if aspects present
667 if Is_Non_Empty_List
(Aspects
) then
669 -- If Decl is Empty, we just ignore the aspects (the caller in this
670 -- case has always issued an appropriate error message).
675 -- If Decl is Error, we ignore the aspects, and issue a message
677 elsif Decl
= Error
then
678 Error_Msg
("aspect specifications not allowed here", Ptr
);
680 -- Here aspects are allowed, and we store them
683 Set_Parent
(Aspects
, Decl
);
684 Set_Aspect_Specifications
(Decl
, Aspects
);
687 end P_Aspect_Specifications
;
689 ---------------------------------------------
690 -- 13.4 Enumeration Representation Clause --
691 ---------------------------------------------
693 -- Parsed by P_Representation_Clause (13.1)
695 ---------------------------------
696 -- 13.4 Enumeration Aggregate --
697 ---------------------------------
699 -- Parsed by P_Representation_Clause (13.1)
701 ------------------------------------------
702 -- 13.5.1 Record Representation Clause --
703 ------------------------------------------
705 -- Parsed by P_Representation_Clause (13.1)
707 ------------------------------
708 -- 13.5.1 Mod Clause (I.8) --
709 ------------------------------
711 -- MOD_CLAUSE ::= at mod static_EXPRESSION;
713 -- Note: in Ada 83, the expression must be a simple expression
715 -- The caller has checked that the initial Token is AT
717 -- Error recovery: cannot raise Error_Resync
719 -- Note: the caller is responsible for setting the Pragmas_Before field
721 function P_Mod_Clause
return Node_Id
is
726 Mod_Node
:= New_Node
(N_Mod_Clause
, Token_Ptr
);
729 Expr_Node
:= P_Expression_No_Right_Paren
;
730 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
731 Set_Expression
(Mod_Node
, Expr_Node
);
736 ------------------------------
737 -- 13.5.1 Component Clause --
738 ------------------------------
740 -- COMPONENT_CLAUSE ::=
741 -- COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
742 -- range FIRST_BIT .. LAST_BIT;
744 -- COMPONENT_CLAUSE_COMPONENT_NAME ::=
745 -- component_DIRECT_NAME
746 -- | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
747 -- | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
749 -- POSITION ::= static_EXPRESSION
751 -- Note: in Ada 83, the expression must be a simple expression
753 -- FIRST_BIT ::= static_SIMPLE_EXPRESSION
754 -- LAST_BIT ::= static_SIMPLE_EXPRESSION
756 -- Note: the AARM V2.0 grammar has an error at this point, it uses
757 -- EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
759 -- Error recovery: cannot raise Error_Resync
761 function P_Component_Clause
return Node_Id
is
762 Component_Node
: Node_Id
;
767 Component_Node
:= New_Node
(N_Component_Clause
, Token_Ptr
);
770 if Nkind
(Comp_Name
) = N_Identifier
771 or else Nkind
(Comp_Name
) = N_Attribute_Reference
773 Set_Component_Name
(Component_Node
, Comp_Name
);
776 ("component name must be direct name or attribute", Comp_Name
);
777 Set_Component_Name
(Component_Node
, Error
);
780 Set_Sloc
(Component_Node
, Token_Ptr
);
782 Expr_Node
:= P_Expression_No_Right_Paren
;
783 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
784 Set_Position
(Component_Node
, Expr_Node
);
786 Expr_Node
:= P_Expression_No_Right_Paren
;
787 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
788 Set_First_Bit
(Component_Node
, Expr_Node
);
790 Expr_Node
:= P_Expression_No_Right_Paren
;
791 Check_Simple_Expression_In_Ada_83
(Expr_Node
);
792 Set_Last_Bit
(Component_Node
, Expr_Node
);
794 return Component_Node
;
795 end P_Component_Clause
;
797 ----------------------
798 -- 13.5.1 Position --
799 ----------------------
801 -- Parsed by P_Component_Clause (13.5.1)
803 -----------------------
804 -- 13.5.1 First Bit --
805 -----------------------
807 -- Parsed by P_Component_Clause (13.5.1)
809 ----------------------
810 -- 13.5.1 Last Bit --
811 ----------------------
813 -- Parsed by P_Component_Clause (13.5.1)
815 --------------------------
816 -- 13.8 Code Statement --
817 --------------------------
819 -- CODE_STATEMENT ::= QUALIFIED_EXPRESSION
821 -- On entry the caller has scanned the SUBTYPE_MARK (passed in as the
822 -- single argument, and the scan points to the apostrophe.
824 -- Error recovery: can raise Error_Resync
826 function P_Code_Statement
(Subtype_Mark
: Node_Id
) return Node_Id
is
830 Scan
; -- past apostrophe
832 -- If left paren, then we have a possible code statement
834 if Token
= Tok_Left_Paren
then
835 Node1
:= New_Node
(N_Code_Statement
, Sloc
(Subtype_Mark
));
836 Set_Expression
(Node1
, P_Qualified_Expression
(Subtype_Mark
));
840 -- Otherwise we have an illegal range attribute. Note that P_Name
841 -- ensures that Token = Tok_Range is the only possibility left here.
844 Error_Msg_SC
("RANGE attribute illegal here!");
847 end P_Code_Statement
;