1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 -- Code duplication, see Par_Ch3.P_Defining_Identifier???
64 if Token = Tok_Identifier then
66 -- Shouldn't the warnings below be emitted when in Ada 83 mode???
68 -- Ada 2005 (AI-284): If compiling in Ada 95 mode, we warn that
69 -- INTERFACE, OVERRIDING, and SYNCHRONIZED are new reserved words.
71 if Ada_Version = Ada_95
72 and then Warn_On_Ada_2005_Compatibility
74 if Token_Name = Name_Overriding
75 or else Token_Name = Name_Synchronized
76 or else (Token_Name = Name_Interface
77 and then Prev_Token /= Tok_Pragma)
79 Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
83 -- Similarly, warn about Ada 2012 reserved words
85 if Ada_Version in Ada_95 .. Ada_2005
86 and then Warn_On_Ada_2012_Compatibility
88 if Token_Name = Name_Some then
89 Error_Msg_N ("& is a reserved word in Ada 2012?", Token_Node);
93 Ident_Node := Token_Node;
94 Scan; -- past Identifier
97 -- If we have a reserved identifier, manufacture an identifier with
98 -- a corresponding name after posting an appropriate error message
100 elsif Is_Reserved_Identifier (C) then
101 Scan_Reserved_Identifier (Force_Msg => False);
102 Ident_Node := Token_Node;
103 Scan; -- past the node
106 -- Otherwise we have junk that cannot be interpreted as an identifier
109 T_Identifier; -- to give message
114 --------------------------
115 -- 2.3 Letter Or Digit --
116 --------------------------
118 -- Parsed by P_Identifier (2.3)
120 --------------------------
121 -- 2.4 Numeric Literal --
122 --------------------------
124 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
126 -- Numeric literal is returned by the scanner as either
127 -- Tok_Integer_Literal or Tok_Real_Literal
129 ----------------------------
130 -- 2.4.1 Decimal Literal --
131 ----------------------------
133 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
135 -- Handled by scanner as part of numeric literal handing (see 2.4)
141 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
143 -- Handled by scanner as part of numeric literal handling (see 2.4)
145 ---------------------
147 ---------------------
149 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL
151 -- Handled by scanner as part of numeric literal handling (see 2.4)
153 --------------------------
154 -- 2.4.2 Based Literal --
155 --------------------------
158 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
160 -- Handled by scanner as part of numeric literal handling (see 2.4)
168 -- Handled by scanner as part of numeric literal handling (see 2.4)
170 --------------------------
171 -- 2.4.2 Based Numeral --
172 --------------------------
175 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
177 -- Handled by scanner as part of numeric literal handling (see 2.4)
179 ---------------------------
180 -- 2.4.2 Extended Digit --
181 ---------------------------
183 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
185 -- Handled by scanner as part of numeric literal handling (see 2.4)
187 ----------------------------
188 -- 2.5 Character Literal --
189 ----------------------------
191 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
193 -- Handled by the scanner and returned as Tok_Char_Literal
195 -------------------------
196 -- 2.6 String Literal --
197 -------------------------
199 -- STRING LITERAL ::= "{STRING_ELEMENT}"
201 -- Handled by the scanner and returned as Tok_String_Literal
202 -- or if the string looks like an operator as Tok_Operator_Symbol.
204 -------------------------
205 -- 2.6 String Element --
206 -------------------------
208 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
210 -- A STRING_ELEMENT is either a pair of quotation marks ("),
211 -- or a single GRAPHIC_CHARACTER other than a quotation mark.
213 -- Handled by scanner as part of string literal handling (see 2.4)
219 -- A COMMENT starts with two adjacent hyphens and extends up to the
220 -- end of the line. A COMMENT may appear on any line of a program.
222 -- Handled by the scanner which simply skips past encountered comments
228 -- PRAGMA ::= pragma IDENTIFIER
229 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
231 -- The caller has checked that the initial token is PRAGMA
233 -- Error recovery: cannot raise Error_Resync
235 -- One special piece of processing is needed in this routine. As described
236 -- in the section on "Handling semicolon used in place of IS" in module
237 -- Parse, the parser detects the case of missing subprogram bodies to
238 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
239 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
240 -- recognize the use of these two pragmas in this context, otherwise it
241 -- will think there are missing bodies, and try to change ; to IS, when
242 -- in fact the bodies ARE present, supplied by these pragmas.
244 function P_Pragma (Skipping : Boolean := False) return Node_Id is
245 Interface_Check_Required : Boolean := False;
246 -- Set True if check of pragma INTERFACE is required
248 Import_Check_Required : Boolean := False;
249 -- Set True if check of pragma IMPORT is required
251 Arg_Count : Int := 0;
252 -- Number of argument associations processed
254 Identifier_Seen : Boolean := False;
255 -- Set True if an identifier is encountered for a pragma argument. Used
256 -- to check that there are no more arguments without identifiers.
260 Semicolon_Loc : Source_Ptr;
261 Ident_Node : Node_Id;
262 Assoc_Node : Node_Id;
265 procedure Skip_Pragma_Semicolon;
266 -- Skip past semicolon at end of pragma
268 ---------------------------
269 -- Skip_Pragma_Semicolon --
270 ---------------------------
272 procedure Skip_Pragma_Semicolon is
274 if Token /= Tok_Semicolon then
276 -- If skipping the pragma, ignore a missing semicolon
281 -- Otherwise demand a semicolon
287 -- Scan past semicolon if present
292 end Skip_Pragma_Semicolon;
294 -- Start of processing for P_Pragma
297 Prag_Node := New_Node (N_Pragma, Token_Ptr);
299 Prag_Name := Token_Name;
302 Style.Check_Pragma_Name;
305 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
306 -- allowed as a pragma name.
308 if Ada_Version >= Ada_2005
309 and then Token = Tok_Interface
311 Prag_Name := Name_Interface;
312 Ident_Node := Make_Identifier (Token_Ptr, Name_Interface);
313 Scan; -- past INTERFACE
315 Ident_Node := P_Identifier;
318 Set_Pragma_Identifier (Prag_Node, Ident_Node);
320 -- See if special INTERFACE/IMPORT check is required
322 if SIS_Entry_Active then
323 Interface_Check_Required := (Prag_Name = Name_Interface);
324 Import_Check_Required := (Prag_Name = Name_Import);
326 Interface_Check_Required := False;
327 Import_Check_Required := False;
330 -- Scan arguments. We assume that arguments are present if there is
331 -- a left paren, or if a semicolon is missing and there is another
332 -- token on the same line as the pragma name.
334 if Token = Tok_Left_Paren
335 or else (Token /= Tok_Semicolon
336 and then not Token_Is_At_Start_Of_Line)
338 Set_Pragma_Argument_Associations (Prag_Node, New_List);
342 Arg_Count := Arg_Count + 1;
343 Scan_Pragma_Argument_Association (Identifier_Seen, Assoc_Node);
346 and then (Interface_Check_Required or else Import_Check_Required)
348 -- Here is where we cancel the SIS active status if this pragma
349 -- supplies a body for the currently active subprogram spec.
351 if Nkind (Expression (Assoc_Node)) in N_Direct_Name
352 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
354 SIS_Entry_Active := False;
358 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
359 exit when Token /= Tok_Comma;
363 -- If we have := for pragma Debug, it is worth special casing the
364 -- error message (it is easy to think of pragma Debug as taking a
365 -- statement, and an assignment statement is the most likely
366 -- candidate for this error)
368 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
369 Error_Msg_SC ("argument for pragma Debug must be procedure call");
372 -- Normal case, we expect a right paren here
379 Semicolon_Loc := Token_Ptr;
381 -- Now we have two tasks left, we need to scan out the semicolon
382 -- following the pragma, and we have to call Par.Prag to process
383 -- the pragma. Normally we do them in this order, however, there
384 -- is one exception namely pragma Style_Checks where we like to
385 -- skip the semicolon after processing the pragma, since that way
386 -- the style checks for the scanning of the semicolon follow the
387 -- settings of the pragma.
389 -- You might think we could just unconditionally do things in
390 -- the opposite order, but there are other pragmas, notably the
391 -- case of pragma Source_File_Name, which assume the semicolon
392 -- is already scanned out.
394 if Prag_Name = Name_Style_Checks then
395 Result := Par.Prag (Prag_Node, Semicolon_Loc);
396 Skip_Pragma_Semicolon;
399 Skip_Pragma_Semicolon;
400 return Par.Prag (Prag_Node, Semicolon_Loc);
405 Resync_Past_Semicolon;
410 -- This routine is called if a pragma is encountered in an inappropriate
411 -- position, the pragma is scanned out and control returns to continue.
413 -- The caller has checked that the initial token is pragma
415 -- Error recovery: cannot raise Error_Resync
417 procedure P_Pragmas_Misplaced is
419 while Token = Tok_Pragma loop
420 Error_Msg_SC ("pragma not allowed here");
421 Discard_Junk_Node (P_Pragma (Skipping => True));
423 end P_Pragmas_Misplaced;
425 -- This function is called to scan out an optional sequence of pragmas.
426 -- If no pragmas are found, then No_List is returned.
428 -- Error recovery: Cannot raise Error_Resync
430 function P_Pragmas_Opt return List_Id is
434 if Token = Tok_Pragma then
444 -- This procedure is called to scan out an optional sequence of pragmas.
445 -- Any pragmas found are appended to the list provided as an argument.
447 -- Error recovery: Cannot raise Error_Resync
449 procedure P_Pragmas_Opt (List : List_Id) is
453 while Token = Tok_Pragma loop
456 if Nkind (P) /= N_Error
457 and then (Pragma_Name (P) = Name_Assert
459 Pragma_Name (P) = Name_Debug)
461 Error_Msg_Name_1 := Pragma_Name (P);
463 ("pragma% must be in declaration/statement context", P);
470 --------------------------------------
471 -- 2.8 Pragma_Argument Association --
472 --------------------------------------
474 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
475 -- [pragma_argument_IDENTIFIER =>] NAME
476 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
478 -- Error recovery: cannot raise Error_Resync
480 procedure Scan_Pragma_Argument_Association
481 (Identifier_Seen : in out Boolean;
482 Association : out Node_Id)
484 Scan_State : Saved_Scan_State;
485 Identifier_Node : Node_Id;
486 Id_Present : Boolean;
489 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
490 Set_Chars (Association, No_Name);
492 -- Argument starts with identifier
494 if Token = Tok_Identifier then
495 Identifier_Node := Token_Node;
496 Save_Scan_State (Scan_State); -- at Identifier
497 Scan; -- past Identifier
499 if Token = Tok_Arrow then
500 Identifier_Seen := True;
502 Set_Chars (Association, Chars (Identifier_Node));
505 -- Case of argument with no identifier
508 Restore_Scan_State (Scan_State); -- to Identifier
512 -- Argument does not start with identifier
518 -- Diagnose error of "positional" argument for pragma appearing after
519 -- a "named" argument (quotes here are because that's not quite accurate
520 -- Ada RM terminology).
522 -- Since older GNAT versions did not generate this error, disable this
523 -- message in codepeer mode to help legacy code using codepeer.
525 if Identifier_Seen and not Id_Present and not CodePeer_Mode then
526 Error_Msg_SC ("|pragma argument identifier required here");
527 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
531 Set_Expression (Association, P_Expression);
533 Set_Expression (Association, P_Expression_If_OK);
535 end Scan_Pragma_Argument_Association;