1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 pragma Style_Checks (All_Checks);
28 -- Turn off subprogram body ordering check. Subprograms are in order
29 -- by RM section rather than alphabetical
34 -- Local functions, used only in this chapter
36 procedure Scan_Pragma_Argument_Association
37 (Identifier_Seen : in out Boolean;
38 Association : out Node_Id);
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 if
41 -- 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.
49 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT}
51 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT
53 -- An IDENTIFIER shall not be a reserved word
55 -- Error recovery: can raise Error_Resync (cannot return Error)
57 function P_Identifier (C : Id_Check := None) return Node_Id is
61 -- All set if we do indeed have an identifier
63 if Token = Tok_Identifier then
65 -- Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
66 -- OVERRIDING, and SYNCHRONIZED are new reserved words.
68 if Ada_Version = Ada_95
69 and then Warn_On_Ada_2005_Compatibility
71 if Token_Name = Name_Overriding
72 or else Token_Name = Name_Synchronized
73 or else (Token_Name = Name_Interface
74 and then Prev_Token /= Tok_Pragma)
76 Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
80 Ident_Node := Token_Node;
81 Scan; -- past Identifier
84 -- If we have a reserved identifier, manufacture an identifier with
85 -- a corresponding name after posting an appropriate error message
87 elsif Is_Reserved_Identifier (C) then
88 Scan_Reserved_Identifier (Force_Msg => False);
89 Ident_Node := Token_Node;
90 Scan; -- past the node
93 -- Otherwise we have junk that cannot be interpreted as an identifier
96 T_Identifier; -- to give message
101 --------------------------
102 -- 2.3 Letter Or Digit --
103 --------------------------
105 -- Parsed by P_Identifier (2.3)
107 --------------------------
108 -- 2.4 Numeric Literal --
109 --------------------------
111 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
113 -- Numeric literal is returned by the scanner as either
114 -- Tok_Integer_Literal or Tok_Real_Literal
116 ----------------------------
117 -- 2.4.1 Decimal Literal --
118 ----------------------------
120 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
122 -- Handled by scanner as part of numeric lIteral handing (see 2.4)
128 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
130 -- Handled by scanner as part of numeric literal handling (see 2.4)
132 ---------------------
134 ---------------------
136 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL
138 -- Handled by scanner as part of numeric literal handling (see 2.4)
140 --------------------------
141 -- 2.4.2 Based Literal --
142 --------------------------
145 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
147 -- Handled by scanner as part of numeric literal handling (see 2.4)
155 -- Handled by scanner as part of numeric literal handling (see 2.4)
157 --------------------------
158 -- 2.4.2 Based Numeral --
159 --------------------------
162 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
164 -- Handled by scanner as part of numeric literal handling (see 2.4)
166 ---------------------------
167 -- 2.4.2 Extended Digit --
168 ---------------------------
170 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
172 -- Handled by scanner as part of numeric literal handling (see 2.4)
174 ----------------------------
175 -- 2.5 Character Literal --
176 ----------------------------
178 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
180 -- Handled by the scanner and returned as Tok_Character_Literal
182 -------------------------
183 -- 2.6 String Literal --
184 -------------------------
186 -- STRING LITERAL ::= "{STRING_ELEMENT}"
188 -- Handled by the scanner and returned as Tok_Character_Literal
189 -- or if the string looks like an operator as Tok_Operator_Symbol.
191 -------------------------
192 -- 2.6 String Element --
193 -------------------------
195 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
197 -- A STRING_ELEMENT is either a pair of quotation marks ("),
198 -- or a single GRAPHIC_CHARACTER other than a quotation mark.
200 -- Handled by scanner as part of string literal handling (see 2.4)
206 -- A COMMENT starts with two adjacent hyphens and extends up to the
207 -- end of the line. A COMMENT may appear on any line of a program.
209 -- Handled by the scanner which simply skips past encountered comments
215 -- PRAGMA ::= pragma IDENTIFIER
216 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
218 -- The caller has checked that the initial token is PRAGMA
220 -- Error recovery: cannot raise Error_Resync
222 -- One special piece of processing is needed in this routine. As described
223 -- in the section on "Handling semicolon used in place of IS" in module
224 -- Parse, the parser detects the case of missing subprogram bodies to
225 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
226 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
227 -- recognize the use of these two pragmas in this context, otherwise it
228 -- will think there are missing bodies, and try to change ; to IS, when
229 -- in fact the bodies ARE present, supplied by these pragmas.
231 function P_Pragma return Node_Id is
233 Interface_Check_Required : Boolean := False;
234 -- Set True if check of pragma INTERFACE is required
236 Import_Check_Required : Boolean := False;
237 -- Set True if check of pragma IMPORT is required
239 Arg_Count : Int := 0;
240 -- Number of argument associations processed
242 Identifier_Seen : Boolean := False;
243 -- Set True if an identifier is encountered for a pragma argument. Used
244 -- to check that there are no more arguments without identifiers.
246 Pragma_Node : Node_Id;
247 Pragma_Name : Name_Id;
248 Semicolon_Loc : Source_Ptr;
249 Ident_Node : Node_Id;
250 Assoc_Node : Node_Id;
253 procedure Skip_Pragma_Semicolon;
254 -- Skip past semicolon at end of pragma
256 ---------------------------
257 -- Skip_Pragma_Semicolon --
258 ---------------------------
260 procedure Skip_Pragma_Semicolon is
262 if Token /= Tok_Semicolon then
264 Resync_Past_Semicolon;
266 Scan; -- past semicolon
268 end Skip_Pragma_Semicolon;
270 -- Start of processing for P_Pragma
273 Pragma_Node := New_Node (N_Pragma, Token_Ptr);
275 Pragma_Name := Token_Name;
278 Style.Check_Pragma_Name;
281 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
282 -- allowed as a pragma name.
284 if Ada_Version >= Ada_05
285 and then Token = Tok_Interface
287 Pragma_Name := Name_Interface;
288 Ident_Node := Token_Node;
289 Scan; -- past INTERFACE
291 Ident_Node := P_Identifier;
292 Delete_Node (Ident_Node);
295 Set_Chars (Pragma_Node, Pragma_Name);
297 -- See if special INTERFACE/IMPORT check is required
299 if SIS_Entry_Active then
300 Interface_Check_Required := (Pragma_Name = Name_Interface);
301 Import_Check_Required := (Pragma_Name = Name_Import);
303 Interface_Check_Required := False;
304 Import_Check_Required := False;
307 -- Scan arguments. We assume that arguments are present if there is
308 -- a left paren, or if a semicolon is missing and there is another
309 -- token on the same line as the pragma name.
311 if Token = Tok_Left_Paren
312 or else (Token /= Tok_Semicolon
313 and then not Token_Is_At_Start_Of_Line)
315 Set_Pragma_Argument_Associations (Pragma_Node, New_List);
319 Arg_Count := Arg_Count + 1;
320 Scan_Pragma_Argument_Association (Identifier_Seen, Assoc_Node);
323 and then (Interface_Check_Required or else Import_Check_Required)
325 -- Here is where we cancel the SIS active status if this pragma
326 -- supplies a body for the currently active subprogram spec.
328 if Nkind (Expression (Assoc_Node)) in N_Direct_Name
329 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
331 SIS_Entry_Active := False;
335 Append (Assoc_Node, Pragma_Argument_Associations (Pragma_Node));
336 exit when Token /= Tok_Comma;
340 -- If we have := for pragma Debug, it is worth special casing
341 -- the error message (it is easy to think of pragma Debug as
342 -- taking a statement, and an assignment statement is the most
343 -- likely candidate for this error)
345 if Token = Tok_Colon_Equal and then Pragma_Name = Name_Debug then
346 Error_Msg_SC ("argument for pragma Debug must be procedure call");
349 -- Normal case, we expect a right paren here
356 Semicolon_Loc := Token_Ptr;
358 -- Now we have two tasks left, we need to scan out the semicolon
359 -- following the pragma, and we have to call Par.Prag to process
360 -- the pragma. Normally we do them in this order, however, there
361 -- is one exception namely pragma Style_Checks where we like to
362 -- skip the semicolon after processing the pragma, since that way
363 -- the style checks for the scanning of the semicolon follow the
364 -- settings of the pragma.
366 -- You might think we could just unconditionally do things in
367 -- the opposite order, but there are other pragmas, notably the
368 -- case of pragma Source_File_Name, which assume the semicolon
369 -- is already scanned out.
371 if Chars (Pragma_Node) = Name_Style_Checks then
372 Result := Par.Prag (Pragma_Node, Semicolon_Loc);
373 Skip_Pragma_Semicolon;
376 Skip_Pragma_Semicolon;
377 return Par.Prag (Pragma_Node, Semicolon_Loc);
382 Resync_Past_Semicolon;
387 -- This routine is called if a pragma is encountered in an inappropriate
388 -- position, the pragma is scanned out and control returns to continue.
390 -- The caller has checked that the initial token is pragma
392 -- Error recovery: cannot raise Error_Resync
394 procedure P_Pragmas_Misplaced is
396 while Token = Tok_Pragma loop
397 Error_Msg_SC ("pragma not allowed here");
398 Discard_Junk_Node (P_Pragma);
400 end P_Pragmas_Misplaced;
402 -- This function is called to scan out an optional sequence of pragmas.
403 -- If no pragmas are found, then No_List is returned.
405 -- Error recovery: Cannot raise Error_Resync
407 function P_Pragmas_Opt return List_Id is
411 if Token = Tok_Pragma then
421 -- This procedure is called to scan out an optional sequence of pragmas.
422 -- Any pragmas found are appended to the list provided as an argument.
424 -- Error recovery: Cannot raise Error_Resync
426 procedure P_Pragmas_Opt (List : List_Id) is
430 while Token = Tok_Pragma loop
433 if Chars (P) = Name_Assert or else Chars (P) = Name_Debug then
434 Error_Msg_Name_1 := Chars (P);
436 ("pragma% must be in declaration/statement context", P);
443 --------------------------------------
444 -- 2.8 Pragma_Argument Association --
445 --------------------------------------
447 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
448 -- [pragma_argument_IDENTIFIER =>] NAME
449 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
451 -- Error recovery: cannot raise Error_Resync
453 procedure Scan_Pragma_Argument_Association
454 (Identifier_Seen : in out Boolean;
455 Association : out Node_Id)
457 Scan_State : Saved_Scan_State;
458 Identifier_Node : Node_Id;
461 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
462 Set_Chars (Association, No_Name);
464 if Token = Tok_Identifier then
465 Identifier_Node := Token_Node;
466 Save_Scan_State (Scan_State); -- at Identifier
467 Scan; -- past Identifier
469 if Token = Tok_Arrow then
470 Identifier_Seen := True;
472 Set_Chars (Association, Chars (Identifier_Node));
473 Delete_Node (Identifier_Node);
475 -- Case of argument with no identifier
478 Restore_Scan_State (Scan_State); -- to Identifier
480 if Identifier_Seen then
482 ("|pragma argument identifier required here ('R'M' 2.8(4))");
487 Set_Expression (Association, P_Expression);
488 end Scan_Pragma_Argument_Association;