1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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 procedure Scan_Pragma_Argument_Association
36 (Identifier_Seen
: in out Boolean;
37 Association
: out Node_Id
;
38 Reserved_Words_OK
: Boolean := False);
39 -- Scans out a pragma argument association. Identifier_Seen is True on
40 -- entry if a previous association had an identifier, and gets set True
41 -- if the scanned association has an identifier (this is used to check the
42 -- rule that no associations without identifiers can follow an association
43 -- which has an identifier). The result is returned in Association. Flag
44 -- For_Pragma_Restrictions should be set when arguments are being parsed
45 -- for pragma Restrictions.
47 -- Note: We allow attribute forms Pre'Class, Post'Class, Invariant'Class,
48 -- Type_Invariant'Class in place of a pragma argument identifier. Rather
49 -- than handle this case specially, we replace such references with
50 -- one of the special internal identifiers _Pre, _Post, _Invariant, or
51 -- _Type_Invariant, and this procedure is where this replacement occurs.
57 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT}
59 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT
61 -- An IDENTIFIER shall not be a reserved word
63 -- Error recovery: can raise Error_Resync (cannot return Error)
65 function P_Identifier
(C
: Id_Check
:= None
) return Node_Id
is
69 -- All set if we do indeed have an identifier
71 -- Code duplication, see Par_Ch3.P_Defining_Identifier???
73 if Token
= Tok_Identifier
then
75 Ident_Node
:= Token_Node
;
76 Scan
; -- past Identifier
79 -- If we have a reserved identifier, manufacture an identifier with
80 -- a corresponding name after posting an appropriate error message
82 elsif Is_Reserved_Identifier
(C
) then
83 Scan_Reserved_Identifier
(Force_Msg
=> False);
84 Ident_Node
:= Token_Node
;
85 Scan
; -- past the node
88 -- Otherwise we have junk that cannot be interpreted as an identifier
91 T_Identifier
; -- to give message
96 --------------------------
97 -- 2.3 Letter Or Digit --
98 --------------------------
100 -- Parsed by P_Identifier (2.3)
102 --------------------------
103 -- 2.4 Numeric Literal --
104 --------------------------
106 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
108 -- Numeric literal is returned by the scanner as either
109 -- Tok_Integer_Literal or Tok_Real_Literal
111 ----------------------------
112 -- 2.4.1 Decimal Literal --
113 ----------------------------
115 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
117 -- Handled by scanner as part of numeric literal handing (see 2.4)
123 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
125 -- Handled by scanner as part of numeric literal handling (see 2.4)
127 ---------------------
129 ---------------------
131 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL
133 -- Handled by scanner as part of numeric literal handling (see 2.4)
135 --------------------------
136 -- 2.4.2 Based Literal --
137 --------------------------
140 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
142 -- Handled by scanner as part of numeric literal handling (see 2.4)
150 -- Handled by scanner as part of numeric literal handling (see 2.4)
152 --------------------------
153 -- 2.4.2 Based Numeral --
154 --------------------------
157 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
159 -- Handled by scanner as part of numeric literal handling (see 2.4)
161 ---------------------------
162 -- 2.4.2 Extended Digit --
163 ---------------------------
165 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
167 -- Handled by scanner as part of numeric literal handling (see 2.4)
169 ----------------------------
170 -- 2.5 Character Literal --
171 ----------------------------
173 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
175 -- Handled by the scanner and returned as Tok_Char_Literal
177 -------------------------
178 -- 2.6 String Literal --
179 -------------------------
181 -- STRING LITERAL ::= "{STRING_ELEMENT}"
183 -- Handled by the scanner and returned as Tok_String_Literal
184 -- or if the string looks like an operator as Tok_Operator_Symbol.
186 -------------------------
187 -- 2.6 String Element --
188 -------------------------
190 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
192 -- A STRING_ELEMENT is either a pair of quotation marks ("),
193 -- or a single GRAPHIC_CHARACTER other than a quotation mark.
195 -- Handled by scanner as part of string literal handling (see 2.4)
201 -- A COMMENT starts with two adjacent hyphens and extends up to the
202 -- end of the line. A COMMENT may appear on any line of a program.
204 -- Handled by the scanner which simply skips past encountered comments
210 -- PRAGMA ::= pragma IDENTIFIER
211 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
213 -- The caller has checked that the initial token is PRAGMA
215 -- Error recovery: cannot raise Error_Resync
217 -- One special piece of processing is needed in this routine. As described
218 -- in the section on "Handling semicolon used in place of IS" in module
219 -- Parse, the parser detects the case of missing subprogram bodies to
220 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
221 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
222 -- recognize the use of these two pragmas in this context, otherwise it
223 -- will think there are missing bodies, and try to change ; to IS, when
224 -- in fact the bodies ARE present, supplied by these pragmas.
226 function P_Pragma
(Skipping
: Boolean := False) return Node_Id
is
227 Interface_Check_Required
: Boolean := False;
228 -- Set True if check of pragma INTERFACE is required
230 Import_Check_Required
: Boolean := False;
231 -- Set True if check of pragma IMPORT is required
233 Arg_Count
: Nat
:= 0;
234 -- Number of argument associations processed
236 Identifier_Seen
: Boolean := False;
237 -- Set True if an identifier is encountered for a pragma argument. Used
238 -- to check that there are no more arguments without identifiers.
242 Semicolon_Loc
: Source_Ptr
;
243 Ident_Node
: Node_Id
;
244 Assoc_Node
: Node_Id
;
247 procedure Skip_Pragma_Semicolon
;
248 -- Skip past semicolon at end of pragma
250 ---------------------------
251 -- Skip_Pragma_Semicolon --
252 ---------------------------
254 procedure Skip_Pragma_Semicolon
is
256 -- If skipping the pragma, ignore a missing semicolon
258 if Token
/= Tok_Semicolon
and then Skipping
then
261 -- Otherwise demand a semicolon
266 end Skip_Pragma_Semicolon
;
268 -- Start of processing for P_Pragma
271 Prag_Node
:= New_Node
(N_Pragma
, Token_Ptr
);
273 Prag_Name
:= Token_Name
;
276 Style
.Check_Pragma_Name
;
279 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
280 -- allowed as a pragma name.
282 if Is_Reserved_Keyword
(Token
) then
283 Prag_Name
:= Keyword_Name
(Token
);
284 Ident_Node
:= Make_Identifier
(Token_Ptr
, Prag_Name
);
285 Scan
; -- past the keyword
287 Ident_Node
:= P_Identifier
;
290 Set_Pragma_Identifier
(Prag_Node
, Ident_Node
);
292 -- See if special INTERFACE/IMPORT check is required
294 if SIS_Entry_Active
then
295 Interface_Check_Required
:= (Prag_Name
= Name_Interface
);
296 Import_Check_Required
:= (Prag_Name
= Name_Import
);
298 Interface_Check_Required
:= False;
299 Import_Check_Required
:= False;
302 -- Set global to indicate if we are within a Depends pragma
304 if Chars
(Ident_Node
) = Name_Depends
then
305 Inside_Depends
:= True;
308 -- Scan arguments. We assume that arguments are present if there is
309 -- a left paren, or if a semicolon is missing and there is another
310 -- token on the same line as the pragma name.
312 if Token
= Tok_Left_Paren
313 or else (Token
/= Tok_Semicolon
314 and then not Token_Is_At_Start_Of_Line
)
316 Set_Pragma_Argument_Associations
(Prag_Node
, New_List
);
320 Arg_Count
:= Arg_Count
+ 1;
322 Scan_Pragma_Argument_Association
323 (Identifier_Seen
=> Identifier_Seen
,
324 Association
=> Assoc_Node
,
326 Nam_In
(Prag_Name
, Name_Restriction_Warnings
,
330 and then (Interface_Check_Required
or else Import_Check_Required
)
332 -- Here is where we cancel the SIS active status if this pragma
333 -- supplies a body for the currently active subprogram spec.
335 if Nkind
(Expression
(Assoc_Node
)) in N_Direct_Name
336 and then Chars
(Expression
(Assoc_Node
)) = Chars
(SIS_Labl
)
338 SIS_Entry_Active
:= False;
342 Append
(Assoc_Node
, Pragma_Argument_Associations
(Prag_Node
));
343 exit when Token
/= Tok_Comma
;
347 -- If we have := for pragma Debug, it is worth special casing the
348 -- error message (it is easy to think of pragma Debug as taking a
349 -- statement, and an assignment statement is the most likely
350 -- candidate for this error)
352 if Token
= Tok_Colon_Equal
and then Prag_Name
= Name_Debug
then
353 Error_Msg_SC
("argument for pragma Debug must be procedure call");
356 -- Normal case, we expect a right paren here
363 Semicolon_Loc
:= Token_Ptr
;
365 -- Cancel indication of being within Depends pragm. Can be done
366 -- unconditionally, since quicker than doing a test.
368 Inside_Depends
:= False;
370 -- Now we have two tasks left, we need to scan out the semicolon
371 -- following the pragma, and we have to call Par.Prag to process
372 -- the pragma. Normally we do them in this order, however, there
373 -- is one exception namely pragma Style_Checks where we like to
374 -- skip the semicolon after processing the pragma, since that way
375 -- the style checks for the scanning of the semicolon follow the
376 -- settings of the pragma.
378 -- You might think we could just unconditionally do things in
379 -- the opposite order, but there are other pragmas, notably the
380 -- case of pragma Source_File_Name, which assume the semicolon
381 -- is already scanned out.
383 if Prag_Name
= Name_Style_Checks
then
384 Result
:= Par
.Prag
(Prag_Node
, Semicolon_Loc
);
385 Skip_Pragma_Semicolon
;
388 Skip_Pragma_Semicolon
;
389 return Par
.Prag
(Prag_Node
, Semicolon_Loc
);
394 Resync_Past_Semicolon
;
399 -- This routine is called if a pragma is encountered in an inappropriate
400 -- position, the pragma is scanned out and control returns to continue.
402 -- The caller has checked that the initial token is pragma
404 -- Error recovery: cannot raise Error_Resync
406 procedure P_Pragmas_Misplaced
is
408 while Token
= Tok_Pragma
loop
409 Error_Msg_SC
("pragma not allowed here");
410 Discard_Junk_Node
(P_Pragma
(Skipping
=> True));
412 end P_Pragmas_Misplaced
;
414 -- This function is called to scan out an optional sequence of pragmas.
415 -- If no pragmas are found, then No_List is returned.
417 -- Error recovery: Cannot raise Error_Resync
419 function P_Pragmas_Opt
return List_Id
is
423 if Token
= Tok_Pragma
then
433 -- This procedure is called to scan out an optional sequence of pragmas.
434 -- Any pragmas found are appended to the list provided as an argument.
436 -- Error recovery: Cannot raise Error_Resync
438 procedure P_Pragmas_Opt
(List
: List_Id
) is
442 while Token
= Tok_Pragma
loop
445 if Nkind
(P
) /= N_Error
446 and then Nam_In
(Pragma_Name_Unmapped
(P
), Name_Assert
, Name_Debug
)
448 Error_Msg_Name_1
:= Pragma_Name_Unmapped
(P
);
450 ("pragma% must be in declaration/statement context", P
);
457 --------------------------------------
458 -- 2.8 Pragma_Argument Association --
459 --------------------------------------
461 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
462 -- [pragma_argument_IDENTIFIER =>] NAME
463 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
465 -- In Ada 2012, there are two more possibilities:
467 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
468 -- [pragma_argument_ASPECT_MARK =>] NAME
469 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION
471 -- where the interesting allowed cases (which do not fit the syntax of the
472 -- first alternative above) are
475 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class
477 -- We allow this special usage in all Ada modes, but it would be a pain to
478 -- allow these aspects to pervade the pragma syntax, and the representation
479 -- of pragma nodes internally. So what we do is to replace these
480 -- ASPECT_MARK forms with identifiers whose name is one of the special
481 -- internal names _Pre, _Post, _Invariant, or _Type_Invariant.
483 -- Error recovery: cannot raise Error_Resync
485 procedure Scan_Pragma_Argument_Association
486 (Identifier_Seen
: in out Boolean;
487 Association
: out Node_Id
;
488 Reserved_Words_OK
: Boolean := False)
490 function P_Expression_Or_Reserved_Word
return Node_Id
;
491 -- Parse an expression or, if the token is one of the following reserved
492 -- words, construct an identifier with proper Chars field.
499 -----------------------------------
500 -- P_Expression_Or_Reserved_Word --
501 -----------------------------------
503 function P_Expression_Or_Reserved_Word
return Node_Id
is
510 if Token
= Tok_Access
then
511 Word_Id
:= Name_Access
;
514 elsif Token
= Tok_Delta
then
515 Word_Id
:= Name_Delta
;
518 elsif Token
= Tok_Digits
then
519 Word_Id
:= Name_Digits
;
522 elsif Token
= Tok_Mod
then
526 elsif Token
= Tok_Range
then
527 Word_Id
:= Name_Range
;
531 if Word_Id
= No_Name
then
534 Word
:= New_Node
(N_Identifier
, Token_Ptr
);
535 Set_Chars
(Word
, Word_Id
);
538 end P_Expression_Or_Reserved_Word
;
542 Expression_Node
: Node_Id
;
543 Identifier_Node
: Node_Id
;
544 Identifier_OK
: Boolean;
545 Scan_State
: Saved_Scan_State
;
547 -- Start of processing for Scan_Pragma_Argument_Association
550 Association
:= New_Node
(N_Pragma_Argument_Association
, Token_Ptr
);
551 Set_Chars
(Association
, No_Name
);
552 Identifier_OK
:= False;
554 -- Argument starts with identifier
556 if Token
= Tok_Identifier
then
557 Identifier_Node
:= Token_Node
;
558 Save_Scan_State
(Scan_State
); -- at Identifier
559 Scan
; -- past Identifier
561 if Token
= Tok_Arrow
then
563 Identifier_OK
:= True;
565 -- Case of one of the special aspect forms
567 elsif Token
= Tok_Apostrophe
then
568 Scan
; -- past apostrophe
570 -- We have apostrophe, so check for identifier'Class
572 if Token
/= Tok_Identifier
or else Token_Name
/= Name_Class
then
575 -- We have identifier'Class, check for arrow
580 if Token
/= Tok_Arrow
then
583 -- Here we have scanned identifier'Class =>
586 Identifier_OK
:= True;
589 case Chars
(Identifier_Node
) is
591 Set_Chars
(Identifier_Node
, Name_uPre
);
594 Set_Chars
(Identifier_Node
, Name_uPost
);
596 when Name_Type_Invariant
=>
597 Set_Chars
(Identifier_Node
, Name_uType_Invariant
);
599 when Name_Invariant
=>
600 Set_Chars
(Identifier_Node
, Name_uInvariant
);
602 -- If it is X'Class => for some invalid X, we will give
603 -- an error, and forget that 'Class was present, which
604 -- will give better error recovery. We could do a spell
605 -- check here, but it seems too much work.
608 Error_Msg_SC
("invalid aspect id for pragma");
614 -- Identifier was present
616 if Identifier_OK
then
617 Set_Chars
(Association
, Chars
(Identifier_Node
));
618 Identifier_Seen
:= True;
620 -- Identifier not present after all
623 Restore_Scan_State
(Scan_State
); -- to Identifier
627 -- Diagnose error of "positional" argument for pragma appearing after
628 -- a "named" argument (quotes here are because that's not quite accurate
629 -- Ada RM terminology).
631 -- Since older GNAT versions did not generate this error, disable this
632 -- message in Relaxed_RM_Semantics mode to help legacy code using e.g.
636 and not Identifier_OK
637 and not Relaxed_RM_Semantics
639 Error_Msg_SC
("|pragma argument identifier required here");
640 Error_Msg_SC
("\since previous argument had identifier (RM 2.8(4))");
643 if Identifier_OK
then
645 -- Certain pragmas such as Restriction_Warnings and Restrictions
646 -- allow reserved words to appear as expressions when checking for
647 -- prohibited uses of attributes.
650 and then Chars
(Identifier_Node
) = Name_No_Use_Of_Attribute
652 Expression_Node
:= P_Expression_Or_Reserved_Word
;
654 Expression_Node
:= P_Expression
;
657 Expression_Node
:= P_Expression_If_OK
;
660 Set_Expression
(Association
, Expression_Node
);
661 end Scan_Pragma_Argument_Association
;