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-2020, 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 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. 46 -- 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. 52 53 --------------------- 54 -- 2.3 Identifier -- 55 --------------------- 56 57 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT} 58 59 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT 60 61 -- An IDENTIFIER shall not be a reserved word 62 63 -- Error recovery: can raise Error_Resync (cannot return Error) 64 65 function P_Identifier (C : Id_Check := None) return Node_Id is 66 Ident_Node : Node_Id; 67 68 begin 69 -- All set if we do indeed have an identifier 70 71 -- Code duplication, see Par_Ch3.P_Defining_Identifier??? 72 73 if Token = Tok_Identifier then 74 Check_Future_Keyword; 75 Ident_Node := Token_Node; 76 Scan; -- past Identifier 77 return Ident_Node; 78 79 -- If we have a reserved identifier, manufacture an identifier with 80 -- a corresponding name after posting an appropriate error message 81 82 elsif Is_Reserved_Identifier (C) then 83 Scan_Reserved_Identifier (Force_Msg => False); 84 Ident_Node := Token_Node; 85 Scan; -- past the node 86 return Ident_Node; 87 88 -- Otherwise we have junk that cannot be interpreted as an identifier 89 90 else 91 T_Identifier; -- to give message 92 raise Error_Resync; 93 end if; 94 end P_Identifier; 95 96 -------------------------- 97 -- 2.3 Letter Or Digit -- 98 -------------------------- 99 100 -- Parsed by P_Identifier (2.3) 101 102 -------------------------- 103 -- 2.4 Numeric Literal -- 104 -------------------------- 105 106 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL 107 108 -- Numeric literal is returned by the scanner as either 109 -- Tok_Integer_Literal or Tok_Real_Literal 110 111 ---------------------------- 112 -- 2.4.1 Decimal Literal -- 113 ---------------------------- 114 115 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT] 116 117 -- Handled by scanner as part of numeric literal handing (see 2.4) 118 119 -------------------- 120 -- 2.4.1 Numeral -- 121 -------------------- 122 123 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT} 124 125 -- Handled by scanner as part of numeric literal handling (see 2.4) 126 127 --------------------- 128 -- 2.4.1 Exponent -- 129 --------------------- 130 131 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL 132 133 -- Handled by scanner as part of numeric literal handling (see 2.4) 134 135 -------------------------- 136 -- 2.4.2 Based Literal -- 137 -------------------------- 138 139 -- BASED_LITERAL ::= 140 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT] 141 142 -- Handled by scanner as part of numeric literal handling (see 2.4) 143 144 ----------------- 145 -- 2.4.2 Base -- 146 ----------------- 147 148 -- BASE ::= NUMERAL 149 150 -- Handled by scanner as part of numeric literal handling (see 2.4) 151 152 -------------------------- 153 -- 2.4.2 Based Numeral -- 154 -------------------------- 155 156 -- BASED_NUMERAL ::= 157 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT} 158 159 -- Handled by scanner as part of numeric literal handling (see 2.4) 160 161 --------------------------- 162 -- 2.4.2 Extended Digit -- 163 --------------------------- 164 165 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F 166 167 -- Handled by scanner as part of numeric literal handling (see 2.4) 168 169 ---------------------------- 170 -- 2.5 Character Literal -- 171 ---------------------------- 172 173 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER ' 174 175 -- Handled by the scanner and returned as Tok_Char_Literal 176 177 ------------------------- 178 -- 2.6 String Literal -- 179 ------------------------- 180 181 -- STRING LITERAL ::= "{STRING_ELEMENT}" 182 183 -- Handled by the scanner and returned as Tok_String_Literal 184 -- or if the string looks like an operator as Tok_Operator_Symbol. 185 186 ------------------------- 187 -- 2.6 String Element -- 188 ------------------------- 189 190 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER 191 192 -- A STRING_ELEMENT is either a pair of quotation marks ("), 193 -- or a single GRAPHIC_CHARACTER other than a quotation mark. 194 195 -- Handled by scanner as part of string literal handling (see 2.4) 196 197 ------------------ 198 -- 2.7 Comment -- 199 ------------------ 200 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. 203 204 -- Handled by the scanner which simply skips past encountered comments 205 206 ----------------- 207 -- 2.8 Pragma -- 208 ----------------- 209 210 -- PRAGMA ::= pragma IDENTIFIER 211 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})]; 212 213 -- The caller has checked that the initial token is PRAGMA 214 215 -- Error recovery: cannot raise Error_Resync 216 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. 225 226 function P_Pragma (Skipping : Boolean := False) return Node_Id is 227 procedure Skip_Pragma_Semicolon; 228 -- Skip past semicolon at end of pragma 229 230 --------------------------- 231 -- Skip_Pragma_Semicolon -- 232 --------------------------- 233 234 procedure Skip_Pragma_Semicolon is 235 begin 236 -- If skipping the pragma, ignore a missing semicolon 237 238 if Token /= Tok_Semicolon and then Skipping then 239 null; 240 241 -- Otherwise demand a semicolon 242 243 else 244 T_Semicolon; 245 end if; 246 end Skip_Pragma_Semicolon; 247 248 -- Local variables 249 250 Import_Check_Required : Boolean := False; 251 -- Set True if check of pragma IMPORT or INTERFACE is required 252 253 Arg_Count : Nat := 0; 254 -- Number of argument associations processed 255 256 Identifier_Seen : Boolean := False; 257 -- Set True if an identifier is encountered for a pragma argument. Used 258 -- to check that there are no more arguments without identifiers. 259 260 Assoc_Node : Node_Id; 261 Ident_Node : Node_Id; 262 Prag_Name : Name_Id; 263 Prag_Node : Node_Id; 264 Result : Node_Id; 265 Semicolon_Loc : Source_Ptr; 266 267 -- Start of processing for P_Pragma 268 269 begin 270 Inside_Pragma := True; 271 Prag_Node := New_Node (N_Pragma, Token_Ptr); 272 Scan; -- past PRAGMA 273 Prag_Name := Token_Name; 274 275 if Style_Check then 276 Style.Check_Pragma_Name; 277 end if; 278 279 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is 280 -- allowed as a pragma name. 281 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 286 else 287 Ident_Node := P_Identifier; 288 end if; 289 290 Set_Pragma_Identifier (Prag_Node, Ident_Node); 291 292 -- See if special INTERFACE/IMPORT check is required 293 294 if SIS_Entry_Active then 295 Import_Check_Required := 296 (Prag_Name = Name_Import) or else (Prag_Name = Name_Interface); 297 else 298 Import_Check_Required := False; 299 end if; 300 301 -- Set global to indicate if we are within a Depends pragma 302 303 if Chars (Ident_Node) = Name_Depends 304 or else Chars (Ident_Node) = Name_Refined_Depends 305 then 306 Inside_Depends := True; 307 end if; 308 309 -- Scan arguments. We assume that arguments are present if there is 310 -- a left paren, or if a semicolon is missing and there is another 311 -- token on the same line as the pragma name. 312 313 if Token = Tok_Left_Paren 314 or else (Token /= Tok_Semicolon 315 and then not Token_Is_At_Start_Of_Line) 316 then 317 Set_Pragma_Argument_Associations (Prag_Node, New_List); 318 T_Left_Paren; 319 320 loop 321 Arg_Count := Arg_Count + 1; 322 323 Scan_Pragma_Argument_Association 324 (Identifier_Seen => Identifier_Seen, 325 Association => Assoc_Node, 326 Reserved_Words_OK => 327 Prag_Name in Name_Restriction_Warnings | Name_Restrictions); 328 329 if Arg_Count = 2 and then Import_Check_Required then 330 -- Here is where we cancel the SIS active status if this pragma 331 -- supplies a body for the currently active subprogram spec. 332 333 if Nkind (Expression (Assoc_Node)) in N_Direct_Name 334 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl) 335 then 336 SIS_Entry_Active := False; 337 end if; 338 end if; 339 340 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node)); 341 exit when Token /= Tok_Comma; 342 Scan; -- past comma 343 end loop; 344 345 -- If we have := for pragma Debug, it is worth special casing the 346 -- error message (it is easy to think of pragma Debug as taking a 347 -- statement, and an assignment statement is the most likely 348 -- candidate for this error) 349 350 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then 351 Error_Msg_SC ("argument for pragma Debug must be procedure call"); 352 Resync_To_Semicolon; 353 354 -- Normal case, we expect a right paren here 355 356 else 357 T_Right_Paren; 358 end if; 359 end if; 360 361 Semicolon_Loc := Token_Ptr; 362 363 -- Cancel indication of being within a pragma or in particular a Depends 364 -- pragma. 365 366 Inside_Depends := False; 367 Inside_Pragma := False; 368 369 -- Now we have two tasks left, we need to scan out the semicolon 370 -- following the pragma, and we have to call Par.Prag to process 371 -- the pragma. Normally we do them in this order, however, there 372 -- is one exception namely pragma Style_Checks where we like to 373 -- skip the semicolon after processing the pragma, since that way 374 -- the style checks for the scanning of the semicolon follow the 375 -- settings of the pragma. 376 377 -- You might think we could just unconditionally do things in 378 -- the opposite order, but there are other pragmas, notably the 379 -- case of pragma Source_File_Name, which assume the semicolon 380 -- is already scanned out. 381 382 if Prag_Name = Name_Style_Checks then 383 Result := Par.Prag (Prag_Node, Semicolon_Loc); 384 Skip_Pragma_Semicolon; 385 return Result; 386 else 387 Skip_Pragma_Semicolon; 388 return Par.Prag (Prag_Node, Semicolon_Loc); 389 end if; 390 391 exception 392 when Error_Resync => 393 Resync_Past_Semicolon; 394 Inside_Depends := False; 395 Inside_Pragma := False; 396 return Error; 397 end P_Pragma; 398 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. 401 402 -- The caller has checked that the initial token is pragma 403 404 -- Error recovery: cannot raise Error_Resync 405 406 procedure P_Pragmas_Misplaced is 407 begin 408 while Token = Tok_Pragma loop 409 Error_Msg_SC ("pragma not allowed here"); 410 Discard_Junk_Node (P_Pragma (Skipping => True)); 411 end loop; 412 end P_Pragmas_Misplaced; 413 414 -- This function is called to scan out an optional sequence of pragmas. 415 -- If no pragmas are found, then No_List is returned. 416 417 -- Error recovery: Cannot raise Error_Resync 418 419 function P_Pragmas_Opt return List_Id is 420 L : List_Id; 421 422 begin 423 if Token = Tok_Pragma then 424 L := New_List; 425 P_Pragmas_Opt (L); 426 return L; 427 428 else 429 return No_List; 430 end if; 431 end P_Pragmas_Opt; 432 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. 435 436 -- Error recovery: Cannot raise Error_Resync 437 438 procedure P_Pragmas_Opt (List : List_Id) is 439 P : Node_Id; 440 441 begin 442 while Token = Tok_Pragma loop 443 P := P_Pragma; 444 445 if Nkind (P) /= N_Error 446 and then Pragma_Name_Unmapped (P) in Name_Assert | Name_Debug 447 then 448 Error_Msg_Name_1 := Pragma_Name_Unmapped (P); 449 Error_Msg_N 450 ("pragma% must be in declaration/statement context", P); 451 else 452 Append (P, List); 453 end if; 454 end loop; 455 end P_Pragmas_Opt; 456 457 -------------------------------------- 458 -- 2.8 Pragma_Argument Association -- 459 -------------------------------------- 460 461 -- PRAGMA_ARGUMENT_ASSOCIATION ::= 462 -- [pragma_argument_IDENTIFIER =>] NAME 463 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION 464 465 -- In Ada 2012, there are two more possibilities: 466 467 -- PRAGMA_ARGUMENT_ASSOCIATION ::= 468 -- [pragma_argument_ASPECT_MARK =>] NAME 469 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION 470 471 -- where the interesting allowed cases (which do not fit the syntax of the 472 -- first alternative above) are 473 474 -- ASPECT_MARK ::= 475 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class 476 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. 482 483 -- Error recovery: cannot raise Error_Resync 484 485 procedure Scan_Pragma_Argument_Association 486 (Identifier_Seen : in out Boolean; 487 Association : out Node_Id; 488 Reserved_Words_OK : Boolean := False) 489 is 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. 493 -- Access 494 -- Delta 495 -- Digits 496 -- Mod 497 -- Range 498 499 ----------------------------------- 500 -- P_Expression_Or_Reserved_Word -- 501 ----------------------------------- 502 503 function P_Expression_Or_Reserved_Word return Node_Id is 504 Word : Node_Id; 505 Word_Id : Name_Id; 506 507 begin 508 Word_Id := No_Name; 509 510 if Token = Tok_Access then 511 Word_Id := Name_Access; 512 Scan; -- past ACCESS 513 514 elsif Token = Tok_Delta then 515 Word_Id := Name_Delta; 516 Scan; -- past DELTA 517 518 elsif Token = Tok_Digits then 519 Word_Id := Name_Digits; 520 Scan; -- past DIGITS 521 522 elsif Token = Tok_Mod then 523 Word_Id := Name_Mod; 524 Scan; -- past MOD 525 526 elsif Token = Tok_Range then 527 Word_Id := Name_Range; 528 Scan; -- post RANGE 529 end if; 530 531 if Word_Id = No_Name then 532 return P_Expression; 533 else 534 Word := New_Node (N_Identifier, Token_Ptr); 535 Set_Chars (Word, Word_Id); 536 return Word; 537 end if; 538 end P_Expression_Or_Reserved_Word; 539 540 -- Local variables 541 542 Expression_Node : Node_Id; 543 Identifier_Node : Node_Id; 544 Identifier_OK : Boolean; 545 Scan_State : Saved_Scan_State; 546 547 -- Start of processing for Scan_Pragma_Argument_Association 548 549 begin 550 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr); 551 Set_Chars (Association, No_Name); 552 Identifier_OK := False; 553 554 -- Argument starts with identifier 555 556 if Token = Tok_Identifier then 557 Identifier_Node := Token_Node; 558 Save_Scan_State (Scan_State); -- at Identifier 559 Scan; -- past Identifier 560 561 if Token = Tok_Arrow then 562 Scan; -- past arrow 563 Identifier_OK := True; 564 565 -- Case of one of the special aspect forms 566 567 elsif Token = Tok_Apostrophe then 568 Scan; -- past apostrophe 569 570 -- We have apostrophe, so check for identifier'Class 571 572 if Token /= Tok_Identifier or else Token_Name /= Name_Class then 573 null; 574 575 -- We have identifier'Class, check for arrow 576 577 else 578 Scan; -- Past Class 579 580 if Token /= Tok_Arrow then 581 null; 582 583 -- Here we have scanned identifier'Class => 584 585 else 586 Identifier_OK := True; 587 Scan; -- past arrow 588 589 case Chars (Identifier_Node) is 590 when Name_Pre => 591 Set_Chars (Identifier_Node, Name_uPre); 592 593 when Name_Post => 594 Set_Chars (Identifier_Node, Name_uPost); 595 596 when Name_Type_Invariant => 597 Set_Chars (Identifier_Node, Name_uType_Invariant); 598 599 when Name_Invariant => 600 Set_Chars (Identifier_Node, Name_uInvariant); 601 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. 606 607 when others => 608 Error_Msg_SC ("invalid aspect id for pragma"); 609 end case; 610 end if; 611 end if; 612 end if; 613 614 -- Identifier was present 615 616 if Identifier_OK then 617 Set_Chars (Association, Chars (Identifier_Node)); 618 Identifier_Seen := True; 619 620 -- Identifier not present after all 621 622 else 623 Restore_Scan_State (Scan_State); -- to Identifier 624 end if; 625 end if; 626 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). 630 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. 633 -- codepeer. 634 635 if Identifier_Seen 636 and not Identifier_OK 637 and not Relaxed_RM_Semantics 638 then 639 Error_Msg_SC ("|pragma argument identifier required here"); 640 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))"); 641 end if; 642 643 if Identifier_OK then 644 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. 648 649 if Reserved_Words_OK 650 and then Chars (Identifier_Node) = Name_No_Use_Of_Attribute 651 then 652 Expression_Node := P_Expression_Or_Reserved_Word; 653 else 654 Expression_Node := P_Expression; 655 end if; 656 else 657 Expression_Node := P_Expression_If_OK; 658 end if; 659 660 Set_Expression (Association, Expression_Node); 661 end Scan_Pragma_Argument_Association; 662 663end Ch2; 664