1------------------------------------------------------------------------------ 2-- -- 3-- GNAT COMPILER COMPONENTS -- 4-- -- 5-- P A R . C H 2 -- 6-- -- 7-- B o d y -- 8-- -- 9-- Copyright (C) 1992-2013, Free Software Foundation, Inc. -- 10-- -- 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. -- 20-- -- 21-- GNAT was originally developed by the GNAT team at New York University. -- 22-- Extensive contributions were provided by Ada Core Technologies Inc. -- 23-- -- 24------------------------------------------------------------------------------ 25 26pragma Style_Checks (All_Checks); 27-- Turn off subprogram body ordering check. Subprograms are in order 28-- by RM section rather than alphabetical 29 30separate (Par) 31package body Ch2 is 32 33 -- Local functions, used only in this chapter 34 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. 43 -- 44 -- Note: We allow attribute forms Pre'Class, Post'Class, Invariant'Class, 45 -- Type_Invariant'Class in place of a pragma argument identifier. Rather 46 -- than handle this case specially, we replace such references with 47 -- one of the special internal identifiers _Pre, _Post, _Invariant, or 48 -- _Type_Invariant, and this procedure is where this replacement occurs. 49 50 --------------------- 51 -- 2.3 Identifier -- 52 --------------------- 53 54 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT} 55 56 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT 57 58 -- An IDENTIFIER shall not be a reserved word 59 60 -- Error recovery: can raise Error_Resync (cannot return Error) 61 62 function P_Identifier (C : Id_Check := None) return Node_Id is 63 Ident_Node : Node_Id; 64 65 begin 66 -- All set if we do indeed have an identifier 67 68 -- Code duplication, see Par_Ch3.P_Defining_Identifier??? 69 70 if Token = Tok_Identifier then 71 Check_Future_Keyword; 72 Ident_Node := Token_Node; 73 Scan; -- past Identifier 74 return Ident_Node; 75 76 -- If we have a reserved identifier, manufacture an identifier with 77 -- a corresponding name after posting an appropriate error message 78 79 elsif Is_Reserved_Identifier (C) then 80 Scan_Reserved_Identifier (Force_Msg => False); 81 Ident_Node := Token_Node; 82 Scan; -- past the node 83 return Ident_Node; 84 85 -- Otherwise we have junk that cannot be interpreted as an identifier 86 87 else 88 T_Identifier; -- to give message 89 raise Error_Resync; 90 end if; 91 end P_Identifier; 92 93 -------------------------- 94 -- 2.3 Letter Or Digit -- 95 -------------------------- 96 97 -- Parsed by P_Identifier (2.3) 98 99 -------------------------- 100 -- 2.4 Numeric Literal -- 101 -------------------------- 102 103 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL 104 105 -- Numeric literal is returned by the scanner as either 106 -- Tok_Integer_Literal or Tok_Real_Literal 107 108 ---------------------------- 109 -- 2.4.1 Decimal Literal -- 110 ---------------------------- 111 112 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT] 113 114 -- Handled by scanner as part of numeric literal handing (see 2.4) 115 116 -------------------- 117 -- 2.4.1 Numeral -- 118 -------------------- 119 120 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT} 121 122 -- Handled by scanner as part of numeric literal handling (see 2.4) 123 124 --------------------- 125 -- 2.4.1 Exponent -- 126 --------------------- 127 128 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL 129 130 -- Handled by scanner as part of numeric literal handling (see 2.4) 131 132 -------------------------- 133 -- 2.4.2 Based Literal -- 134 -------------------------- 135 136 -- BASED_LITERAL ::= 137 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT] 138 139 -- Handled by scanner as part of numeric literal handling (see 2.4) 140 141 ----------------- 142 -- 2.4.2 Base -- 143 ----------------- 144 145 -- BASE ::= NUMERAL 146 147 -- Handled by scanner as part of numeric literal handling (see 2.4) 148 149 -------------------------- 150 -- 2.4.2 Based Numeral -- 151 -------------------------- 152 153 -- BASED_NUMERAL ::= 154 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT} 155 156 -- Handled by scanner as part of numeric literal handling (see 2.4) 157 158 --------------------------- 159 -- 2.4.2 Extended Digit -- 160 --------------------------- 161 162 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F 163 164 -- Handled by scanner as part of numeric literal handling (see 2.4) 165 166 ---------------------------- 167 -- 2.5 Character Literal -- 168 ---------------------------- 169 170 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER ' 171 172 -- Handled by the scanner and returned as Tok_Char_Literal 173 174 ------------------------- 175 -- 2.6 String Literal -- 176 ------------------------- 177 178 -- STRING LITERAL ::= "{STRING_ELEMENT}" 179 180 -- Handled by the scanner and returned as Tok_String_Literal 181 -- or if the string looks like an operator as Tok_Operator_Symbol. 182 183 ------------------------- 184 -- 2.6 String Element -- 185 ------------------------- 186 187 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER 188 189 -- A STRING_ELEMENT is either a pair of quotation marks ("), 190 -- or a single GRAPHIC_CHARACTER other than a quotation mark. 191 192 -- Handled by scanner as part of string literal handling (see 2.4) 193 194 ------------------ 195 -- 2.7 Comment -- 196 ------------------ 197 198 -- A COMMENT starts with two adjacent hyphens and extends up to the 199 -- end of the line. A COMMENT may appear on any line of a program. 200 201 -- Handled by the scanner which simply skips past encountered comments 202 203 ----------------- 204 -- 2.8 Pragma -- 205 ----------------- 206 207 -- PRAGMA ::= pragma IDENTIFIER 208 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})]; 209 210 -- The caller has checked that the initial token is PRAGMA 211 212 -- Error recovery: cannot raise Error_Resync 213 214 -- One special piece of processing is needed in this routine. As described 215 -- in the section on "Handling semicolon used in place of IS" in module 216 -- Parse, the parser detects the case of missing subprogram bodies to 217 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for 218 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must 219 -- recognize the use of these two pragmas in this context, otherwise it 220 -- will think there are missing bodies, and try to change ; to IS, when 221 -- in fact the bodies ARE present, supplied by these pragmas. 222 223 function P_Pragma (Skipping : Boolean := False) return Node_Id is 224 Interface_Check_Required : Boolean := False; 225 -- Set True if check of pragma INTERFACE is required 226 227 Import_Check_Required : Boolean := False; 228 -- Set True if check of pragma IMPORT is required 229 230 Arg_Count : Int := 0; 231 -- Number of argument associations processed 232 233 Identifier_Seen : Boolean := False; 234 -- Set True if an identifier is encountered for a pragma argument. Used 235 -- to check that there are no more arguments without identifiers. 236 237 Prag_Node : Node_Id; 238 Prag_Name : Name_Id; 239 Semicolon_Loc : Source_Ptr; 240 Ident_Node : Node_Id; 241 Assoc_Node : Node_Id; 242 Result : Node_Id; 243 244 procedure Skip_Pragma_Semicolon; 245 -- Skip past semicolon at end of pragma 246 247 --------------------------- 248 -- Skip_Pragma_Semicolon -- 249 --------------------------- 250 251 procedure Skip_Pragma_Semicolon is 252 begin 253 -- If skipping the pragma, ignore a missing semicolon 254 255 if Token /= Tok_Semicolon and then Skipping then 256 null; 257 258 -- Otherwise demand a semicolon 259 260 else 261 T_Semicolon; 262 end if; 263 end Skip_Pragma_Semicolon; 264 265 -- Start of processing for P_Pragma 266 267 begin 268 Prag_Node := New_Node (N_Pragma, Token_Ptr); 269 Scan; -- past PRAGMA 270 Prag_Name := Token_Name; 271 272 if Style_Check then 273 Style.Check_Pragma_Name; 274 end if; 275 276 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is 277 -- allowed as a pragma name. 278 279 if Ada_Version >= Ada_2005 280 and then Token = Tok_Interface 281 then 282 Prag_Name := Name_Interface; 283 Ident_Node := Make_Identifier (Token_Ptr, Name_Interface); 284 Scan; -- past INTERFACE 285 else 286 Ident_Node := P_Identifier; 287 end if; 288 289 Set_Pragma_Identifier (Prag_Node, Ident_Node); 290 291 -- See if special INTERFACE/IMPORT check is required 292 293 if SIS_Entry_Active then 294 Interface_Check_Required := (Prag_Name = Name_Interface); 295 Import_Check_Required := (Prag_Name = Name_Import); 296 else 297 Interface_Check_Required := False; 298 Import_Check_Required := False; 299 end if; 300 301 -- Scan arguments. We assume that arguments are present if there is 302 -- a left paren, or if a semicolon is missing and there is another 303 -- token on the same line as the pragma name. 304 305 if Token = Tok_Left_Paren 306 or else (Token /= Tok_Semicolon 307 and then not Token_Is_At_Start_Of_Line) 308 then 309 Set_Pragma_Argument_Associations (Prag_Node, New_List); 310 T_Left_Paren; 311 312 loop 313 Arg_Count := Arg_Count + 1; 314 Scan_Pragma_Argument_Association (Identifier_Seen, Assoc_Node); 315 316 if Arg_Count = 2 317 and then (Interface_Check_Required or else Import_Check_Required) 318 then 319 -- Here is where we cancel the SIS active status if this pragma 320 -- supplies a body for the currently active subprogram spec. 321 322 if Nkind (Expression (Assoc_Node)) in N_Direct_Name 323 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl) 324 then 325 SIS_Entry_Active := False; 326 end if; 327 end if; 328 329 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node)); 330 exit when Token /= Tok_Comma; 331 Scan; -- past comma 332 end loop; 333 334 -- If we have := for pragma Debug, it is worth special casing the 335 -- error message (it is easy to think of pragma Debug as taking a 336 -- statement, and an assignment statement is the most likely 337 -- candidate for this error) 338 339 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then 340 Error_Msg_SC ("argument for pragma Debug must be procedure call"); 341 Resync_To_Semicolon; 342 343 -- Normal case, we expect a right paren here 344 345 else 346 T_Right_Paren; 347 end if; 348 end if; 349 350 Semicolon_Loc := Token_Ptr; 351 352 -- Now we have two tasks left, we need to scan out the semicolon 353 -- following the pragma, and we have to call Par.Prag to process 354 -- the pragma. Normally we do them in this order, however, there 355 -- is one exception namely pragma Style_Checks where we like to 356 -- skip the semicolon after processing the pragma, since that way 357 -- the style checks for the scanning of the semicolon follow the 358 -- settings of the pragma. 359 360 -- You might think we could just unconditionally do things in 361 -- the opposite order, but there are other pragmas, notably the 362 -- case of pragma Source_File_Name, which assume the semicolon 363 -- is already scanned out. 364 365 if Prag_Name = Name_Style_Checks then 366 Result := Par.Prag (Prag_Node, Semicolon_Loc); 367 Skip_Pragma_Semicolon; 368 return Result; 369 else 370 Skip_Pragma_Semicolon; 371 return Par.Prag (Prag_Node, Semicolon_Loc); 372 end if; 373 374 exception 375 when Error_Resync => 376 Resync_Past_Semicolon; 377 return Error; 378 379 end P_Pragma; 380 381 -- This routine is called if a pragma is encountered in an inappropriate 382 -- position, the pragma is scanned out and control returns to continue. 383 384 -- The caller has checked that the initial token is pragma 385 386 -- Error recovery: cannot raise Error_Resync 387 388 procedure P_Pragmas_Misplaced is 389 begin 390 while Token = Tok_Pragma loop 391 Error_Msg_SC ("pragma not allowed here"); 392 Discard_Junk_Node (P_Pragma (Skipping => True)); 393 end loop; 394 end P_Pragmas_Misplaced; 395 396 -- This function is called to scan out an optional sequence of pragmas. 397 -- If no pragmas are found, then No_List is returned. 398 399 -- Error recovery: Cannot raise Error_Resync 400 401 function P_Pragmas_Opt return List_Id is 402 L : List_Id; 403 404 begin 405 if Token = Tok_Pragma then 406 L := New_List; 407 P_Pragmas_Opt (L); 408 return L; 409 410 else 411 return No_List; 412 end if; 413 end P_Pragmas_Opt; 414 415 -- This procedure is called to scan out an optional sequence of pragmas. 416 -- Any pragmas found are appended to the list provided as an argument. 417 418 -- Error recovery: Cannot raise Error_Resync 419 420 procedure P_Pragmas_Opt (List : List_Id) is 421 P : Node_Id; 422 423 begin 424 while Token = Tok_Pragma loop 425 P := P_Pragma; 426 427 if Nkind (P) /= N_Error 428 and then Nam_In (Pragma_Name (P), Name_Assert, Name_Debug) 429 then 430 Error_Msg_Name_1 := Pragma_Name (P); 431 Error_Msg_N 432 ("pragma% must be in declaration/statement context", P); 433 else 434 Append (P, List); 435 end if; 436 end loop; 437 end P_Pragmas_Opt; 438 439 -------------------------------------- 440 -- 2.8 Pragma_Argument Association -- 441 -------------------------------------- 442 443 -- PRAGMA_ARGUMENT_ASSOCIATION ::= 444 -- [pragma_argument_IDENTIFIER =>] NAME 445 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION 446 447 -- In Ada 2012, there are two more possibilities: 448 449 -- PRAGMA_ARGUMENT_ASSOCIATION ::= 450 -- [pragma_argument_ASPECT_MARK =>] NAME 451 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION 452 453 -- where the interesting allowed cases (which do not fit the syntax of the 454 -- first alternative above) are 455 456 -- ASPECT_MARK ::= 457 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class 458 459 -- We allow this special usage in all Ada modes, but it would be a pain to 460 -- allow these aspects to pervade the pragma syntax, and the representation 461 -- of pragma nodes internally. So what we do is to replace these 462 -- ASPECT_MARK forms with identifiers whose name is one of the special 463 -- internal names _Pre, _Post, _Invariant, or _Type_Invariant. 464 465 -- Error recovery: cannot raise Error_Resync 466 467 procedure Scan_Pragma_Argument_Association 468 (Identifier_Seen : in out Boolean; 469 Association : out Node_Id) 470 is 471 Scan_State : Saved_Scan_State; 472 Identifier_Node : Node_Id; 473 Id_Present : Boolean; 474 475 begin 476 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr); 477 Set_Chars (Association, No_Name); 478 Id_Present := False; 479 480 -- Argument starts with identifier 481 482 if Token = Tok_Identifier then 483 Identifier_Node := Token_Node; 484 Save_Scan_State (Scan_State); -- at Identifier 485 Scan; -- past Identifier 486 487 if Token = Tok_Arrow then 488 Scan; -- past arrow 489 Id_Present := True; 490 491 -- Case of one of the special aspect forms 492 493 elsif Token = Tok_Apostrophe then 494 Scan; -- past apostrophe 495 496 -- We have apostrophe, so check for identifier'Class 497 498 if Token /= Tok_Identifier or else Token_Name /= Name_Class then 499 null; 500 501 -- We have identifier'Class, check for arrow 502 503 else 504 Scan; -- Past Class 505 506 if Token /= Tok_Arrow then 507 null; 508 509 -- Here we have scanned identifier'Class => 510 511 else 512 Id_Present := True; 513 Scan; -- past arrow 514 515 case Chars (Identifier_Node) is 516 when Name_Pre => 517 Set_Chars (Identifier_Node, Name_uPre); 518 519 when Name_Post => 520 Set_Chars (Identifier_Node, Name_uPost); 521 522 when Name_Type_Invariant => 523 Set_Chars (Identifier_Node, Name_uType_Invariant); 524 525 when Name_Invariant => 526 Set_Chars (Identifier_Node, Name_uInvariant); 527 528 -- If it is X'Class => for some invalid X, we will give 529 -- an error, and forget that 'Class was present, which 530 -- will give better error recovery. We could do a spell 531 -- check here, but it seems too much work. 532 533 when others => 534 Error_Msg_SC ("invalid aspect id for pragma"); 535 end case; 536 end if; 537 end if; 538 end if; 539 540 -- Identifier was present 541 542 if Id_Present then 543 Set_Chars (Association, Chars (Identifier_Node)); 544 Identifier_Seen := True; 545 546 -- Identifier not present after all 547 548 else 549 Restore_Scan_State (Scan_State); -- to Identifier 550 end if; 551 end if; 552 553 -- Diagnose error of "positional" argument for pragma appearing after 554 -- a "named" argument (quotes here are because that's not quite accurate 555 -- Ada RM terminology). 556 557 -- Since older GNAT versions did not generate this error, disable this 558 -- message in Relaxed_RM_Semantics mode to help legacy code using e.g. 559 -- codepeer. 560 561 if Identifier_Seen and not Id_Present and not Relaxed_RM_Semantics then 562 Error_Msg_SC ("|pragma argument identifier required here"); 563 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))"); 564 end if; 565 566 if Id_Present then 567 Set_Expression (Association, P_Expression); 568 else 569 Set_Expression (Association, P_Expression_If_OK); 570 end if; 571 end Scan_Pragma_Argument_Association; 572 573end Ch2; 574