1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 -- Scans out a pragma argument association. Identifier_Seen is true on
39 -- entry if a previous association had an identifier, and gets set True if
40 -- the scanned association has an identifier (this is used to check the
41 -- rule that no associations without identifiers can follow an association
42 -- which has an identifier). The result is returned in Association.
48 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT}
50 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT
52 -- An IDENTIFIER shall not be a reserved word
54 -- Error recovery: can raise Error_Resync (cannot return Error)
56 function P_Identifier
(C
: Id_Check
:= None
) return Node_Id
is
60 -- All set if we do indeed have an identifier
62 if Token
= Tok_Identifier
then
64 -- Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
65 -- OVERRIDING, and SYNCHRONIZED are new reserved words.
67 if Ada_Version
= Ada_95
68 and then Warn_On_Ada_2005_Compatibility
70 if Token_Name
= Name_Overriding
71 or else Token_Name
= Name_Synchronized
72 or else (Token_Name
= Name_Interface
73 and then Prev_Token
/= Tok_Pragma
)
75 Error_Msg_N
("& is a reserved word in Ada 2005?", Token_Node
);
79 Ident_Node
:= Token_Node
;
80 Scan
; -- past Identifier
83 -- If we have a reserved identifier, manufacture an identifier with
84 -- a corresponding name after posting an appropriate error message
86 elsif Is_Reserved_Identifier
(C
) then
87 Scan_Reserved_Identifier
(Force_Msg
=> False);
88 Ident_Node
:= Token_Node
;
89 Scan
; -- past the node
92 -- Otherwise we have junk that cannot be interpreted as an identifier
95 T_Identifier
; -- to give message
100 --------------------------
101 -- 2.3 Letter Or Digit --
102 --------------------------
104 -- Parsed by P_Identifier (2.3)
106 --------------------------
107 -- 2.4 Numeric Literal --
108 --------------------------
110 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
112 -- Numeric literal is returned by the scanner as either
113 -- Tok_Integer_Literal or Tok_Real_Literal
115 ----------------------------
116 -- 2.4.1 Decimal Literal --
117 ----------------------------
119 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
121 -- Handled by scanner as part of numeric lIteral handing (see 2.4)
127 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
129 -- Handled by scanner as part of numeric literal handling (see 2.4)
131 ---------------------
133 ---------------------
135 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL
137 -- Handled by scanner as part of numeric literal handling (see 2.4)
139 --------------------------
140 -- 2.4.2 Based Literal --
141 --------------------------
144 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
146 -- Handled by scanner as part of numeric literal handling (see 2.4)
154 -- Handled by scanner as part of numeric literal handling (see 2.4)
156 --------------------------
157 -- 2.4.2 Based Numeral --
158 --------------------------
161 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
163 -- Handled by scanner as part of numeric literal handling (see 2.4)
165 ---------------------------
166 -- 2.4.2 Extended Digit --
167 ---------------------------
169 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
171 -- Handled by scanner as part of numeric literal handling (see 2.4)
173 ----------------------------
174 -- 2.5 Character Literal --
175 ----------------------------
177 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
179 -- Handled by the scanner and returned as Tok_Char_Literal
181 -------------------------
182 -- 2.6 String Literal --
183 -------------------------
185 -- STRING LITERAL ::= "{STRING_ELEMENT}"
187 -- Handled by the scanner and returned as Tok_String_Literal
188 -- or if the string looks like an operator as Tok_Operator_Symbol.
190 -------------------------
191 -- 2.6 String Element --
192 -------------------------
194 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
196 -- A STRING_ELEMENT is either a pair of quotation marks ("),
197 -- or a single GRAPHIC_CHARACTER other than a quotation mark.
199 -- Handled by scanner as part of string literal handling (see 2.4)
205 -- A COMMENT starts with two adjacent hyphens and extends up to the
206 -- end of the line. A COMMENT may appear on any line of a program.
208 -- Handled by the scanner which simply skips past encountered comments
214 -- PRAGMA ::= pragma IDENTIFIER
215 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
217 -- The caller has checked that the initial token is PRAGMA
219 -- Error recovery: cannot raise Error_Resync
221 -- One special piece of processing is needed in this routine. As described
222 -- in the section on "Handling semicolon used in place of IS" in module
223 -- Parse, the parser detects the case of missing subprogram bodies to
224 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
225 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
226 -- recognize the use of these two pragmas in this context, otherwise it
227 -- will think there are missing bodies, and try to change ; to IS, when
228 -- in fact the bodies ARE present, supplied by these pragmas.
230 function P_Pragma
return Node_Id
is
232 Interface_Check_Required
: Boolean := False;
233 -- Set True if check of pragma INTERFACE is required
235 Import_Check_Required
: Boolean := False;
236 -- Set True if check of pragma IMPORT is required
238 Arg_Count
: Int
:= 0;
239 -- Number of argument associations processed
241 Identifier_Seen
: Boolean := False;
242 -- Set True if an identifier is encountered for a pragma argument. Used
243 -- to check that there are no more arguments without identifiers.
245 Pragma_Node
: Node_Id
;
246 Pragma_Name
: Name_Id
;
247 Semicolon_Loc
: Source_Ptr
;
248 Ident_Node
: Node_Id
;
249 Assoc_Node
: Node_Id
;
252 procedure Skip_Pragma_Semicolon
;
253 -- Skip past semicolon at end of pragma
255 ---------------------------
256 -- Skip_Pragma_Semicolon --
257 ---------------------------
259 procedure Skip_Pragma_Semicolon
is
261 if Token
/= Tok_Semicolon
then
263 Resync_Past_Semicolon
;
265 Scan
; -- past semicolon
267 end Skip_Pragma_Semicolon
;
269 -- Start of processing for P_Pragma
272 Pragma_Node
:= New_Node
(N_Pragma
, Token_Ptr
);
274 Pragma_Name
:= Token_Name
;
277 Style
.Check_Pragma_Name
;
280 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
281 -- allowed as a pragma name.
283 if Ada_Version
>= Ada_05
284 and then Token
= Tok_Interface
286 Pragma_Name
:= Name_Interface
;
287 Ident_Node
:= Token_Node
;
288 Scan
; -- past INTERFACE
290 Ident_Node
:= P_Identifier
;
291 Delete_Node
(Ident_Node
);
294 Set_Chars
(Pragma_Node
, Pragma_Name
);
296 -- See if special INTERFACE/IMPORT check is required
298 if SIS_Entry_Active
then
299 Interface_Check_Required
:= (Pragma_Name
= Name_Interface
);
300 Import_Check_Required
:= (Pragma_Name
= Name_Import
);
302 Interface_Check_Required
:= False;
303 Import_Check_Required
:= False;
306 -- Scan arguments. We assume that arguments are present if there is
307 -- a left paren, or if a semicolon is missing and there is another
308 -- token on the same line as the pragma name.
310 if Token
= Tok_Left_Paren
311 or else (Token
/= Tok_Semicolon
312 and then not Token_Is_At_Start_Of_Line
)
314 Set_Pragma_Argument_Associations
(Pragma_Node
, New_List
);
318 Arg_Count
:= Arg_Count
+ 1;
319 Scan_Pragma_Argument_Association
(Identifier_Seen
, Assoc_Node
);
322 and then (Interface_Check_Required
or else Import_Check_Required
)
324 -- Here is where we cancel the SIS active status if this pragma
325 -- supplies a body for the currently active subprogram spec.
327 if Nkind
(Expression
(Assoc_Node
)) in N_Direct_Name
328 and then Chars
(Expression
(Assoc_Node
)) = Chars
(SIS_Labl
)
330 SIS_Entry_Active
:= False;
334 Append
(Assoc_Node
, Pragma_Argument_Associations
(Pragma_Node
));
335 exit when Token
/= Tok_Comma
;
339 -- If we have := for pragma Debug, it is worth special casing
340 -- the error message (it is easy to think of pragma Debug as
341 -- taking a statement, and an assignment statement is the most
342 -- likely candidate for this error)
344 if Token
= Tok_Colon_Equal
and then Pragma_Name
= Name_Debug
then
345 Error_Msg_SC
("argument for pragma Debug must be procedure call");
348 -- Normal case, we expect a right paren here
355 Semicolon_Loc
:= Token_Ptr
;
357 -- Now we have two tasks left, we need to scan out the semicolon
358 -- following the pragma, and we have to call Par.Prag to process
359 -- the pragma. Normally we do them in this order, however, there
360 -- is one exception namely pragma Style_Checks where we like to
361 -- skip the semicolon after processing the pragma, since that way
362 -- the style checks for the scanning of the semicolon follow the
363 -- settings of the pragma.
365 -- You might think we could just unconditionally do things in
366 -- the opposite order, but there are other pragmas, notably the
367 -- case of pragma Source_File_Name, which assume the semicolon
368 -- is already scanned out.
370 if Chars
(Pragma_Node
) = Name_Style_Checks
then
371 Result
:= Par
.Prag
(Pragma_Node
, Semicolon_Loc
);
372 Skip_Pragma_Semicolon
;
375 Skip_Pragma_Semicolon
;
376 return Par
.Prag
(Pragma_Node
, Semicolon_Loc
);
381 Resync_Past_Semicolon
;
386 -- This routine is called if a pragma is encountered in an inappropriate
387 -- position, the pragma is scanned out and control returns to continue.
389 -- The caller has checked that the initial token is pragma
391 -- Error recovery: cannot raise Error_Resync
393 procedure P_Pragmas_Misplaced
is
395 while Token
= Tok_Pragma
loop
396 Error_Msg_SC
("pragma not allowed here");
397 Discard_Junk_Node
(P_Pragma
);
399 end P_Pragmas_Misplaced
;
401 -- This function is called to scan out an optional sequence of pragmas.
402 -- If no pragmas are found, then No_List is returned.
404 -- Error recovery: Cannot raise Error_Resync
406 function P_Pragmas_Opt
return List_Id
is
410 if Token
= Tok_Pragma
then
420 -- This procedure is called to scan out an optional sequence of pragmas.
421 -- Any pragmas found are appended to the list provided as an argument.
423 -- Error recovery: Cannot raise Error_Resync
425 procedure P_Pragmas_Opt
(List
: List_Id
) is
429 while Token
= Tok_Pragma
loop
432 if Chars
(P
) = Name_Assert
or else Chars
(P
) = Name_Debug
then
433 Error_Msg_Name_1
:= Chars
(P
);
435 ("pragma% must be in declaration/statement context", P
);
442 --------------------------------------
443 -- 2.8 Pragma_Argument Association --
444 --------------------------------------
446 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
447 -- [pragma_argument_IDENTIFIER =>] NAME
448 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
450 -- Error recovery: cannot raise Error_Resync
452 procedure Scan_Pragma_Argument_Association
453 (Identifier_Seen
: in out Boolean;
454 Association
: out Node_Id
)
456 Scan_State
: Saved_Scan_State
;
457 Identifier_Node
: Node_Id
;
460 Association
:= New_Node
(N_Pragma_Argument_Association
, Token_Ptr
);
461 Set_Chars
(Association
, No_Name
);
463 if Token
= Tok_Identifier
then
464 Identifier_Node
:= Token_Node
;
465 Save_Scan_State
(Scan_State
); -- at Identifier
466 Scan
; -- past Identifier
468 if Token
= Tok_Arrow
then
469 Identifier_Seen
:= True;
471 Set_Chars
(Association
, Chars
(Identifier_Node
));
472 Delete_Node
(Identifier_Node
);
474 -- Case of argument with no identifier
477 Restore_Scan_State
(Scan_State
); -- to Identifier
479 if Identifier_Seen
then
481 ("|pragma argument identifier required here (RM 2.8(4))");
486 Set_Expression
(Association
, P_Expression
);
487 end Scan_Pragma_Argument_Association
;