1------------------------------------------------------------------------------ 2-- -- 3-- GNAT COMPILER COMPONENTS -- 4-- -- 5-- P A R . U T I L -- 6-- -- 7-- B o d y -- 8-- -- 9-- Copyright (C) 1992-2012, 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 26with Csets; use Csets; 27with Namet.Sp; use Namet.Sp; 28with Stylesw; use Stylesw; 29with Uintp; use Uintp; 30with Warnsw; use Warnsw; 31 32with GNAT.Spelling_Checker; use GNAT.Spelling_Checker; 33 34separate (Par) 35package body Util is 36 37 --------------------- 38 -- Bad_Spelling_Of -- 39 --------------------- 40 41 function Bad_Spelling_Of (T : Token_Type) return Boolean is 42 Tname : constant String := Token_Type'Image (T); 43 -- Characters of token name 44 45 S : String (1 .. Tname'Last - 4); 46 -- Characters of token name folded to lower case, omitting TOK_ at start 47 48 M1 : String (1 .. 42) := "incorrect spelling of keyword ************"; 49 M2 : String (1 .. 44) := "illegal abbreviation of keyword ************"; 50 -- Buffers used to construct error message 51 52 P1 : constant := 30; 53 P2 : constant := 32; 54 -- Starting subscripts in M1, M2 for keyword name 55 56 SL : constant Natural := S'Length; 57 -- Length of expected token name excluding TOK_ at start 58 59 begin 60 if Token /= Tok_Identifier then 61 return False; 62 end if; 63 64 for J in S'Range loop 65 S (J) := Fold_Lower (Tname (J + 4)); 66 end loop; 67 68 Get_Name_String (Token_Name); 69 70 -- A special check for case of PROGRAM used for PROCEDURE 71 72 if T = Tok_Procedure 73 and then Name_Len = 7 74 and then Name_Buffer (1 .. 7) = "program" 75 then 76 Error_Msg_SC -- CODEFIX 77 ("PROCEDURE expected"); 78 Token := T; 79 return True; 80 81 -- A special check for an illegal abbreviation 82 83 elsif Name_Len < S'Length 84 and then Name_Len >= 4 85 and then Name_Buffer (1 .. Name_Len) = S (1 .. Name_Len) 86 then 87 for J in 1 .. S'Last loop 88 M2 (P2 + J - 1) := Fold_Upper (S (J)); 89 end loop; 90 91 Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last)); 92 Token := T; 93 return True; 94 end if; 95 96 -- Now we go into the full circuit to check for a misspelling 97 98 -- Never consider something a misspelling if either the actual or 99 -- expected string is less than 3 characters (before this check we 100 -- used to consider i to be a misspelled if in some cases!) 101 102 if SL < 3 or else Name_Len < 3 then 103 return False; 104 105 -- Special case: prefix matches, i.e. the leading characters of the 106 -- token that we have exactly match the required keyword. If there 107 -- are at least two characters left over, assume that we have a case 108 -- of two keywords joined together which should not be joined. 109 110 elsif Name_Len > SL + 1 111 and then S = Name_Buffer (1 .. SL) 112 then 113 Scan_Ptr := Token_Ptr + S'Length; 114 Error_Msg_S ("|missing space"); 115 Token := T; 116 return True; 117 end if; 118 119 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then 120 for J in 1 .. S'Last loop 121 M1 (P1 + J - 1) := Fold_Upper (S (J)); 122 end loop; 123 124 Error_Msg_SC -- CODFIX 125 (M1 (1 .. P1 - 1 + S'Last)); 126 Token := T; 127 return True; 128 129 else 130 return False; 131 end if; 132 end Bad_Spelling_Of; 133 134 ---------------------- 135 -- Check_95_Keyword -- 136 ---------------------- 137 138 -- On entry, the caller has checked that current token is an identifier 139 -- whose name matches the name of the 95 keyword New_Tok. 140 141 procedure Check_95_Keyword (Token_95, Next : Token_Type) is 142 Scan_State : Saved_Scan_State; 143 144 begin 145 Save_Scan_State (Scan_State); -- at identifier/keyword 146 Scan; -- past identifier/keyword 147 148 if Token = Next then 149 Restore_Scan_State (Scan_State); -- to identifier 150 Error_Msg_Name_1 := Token_Name; 151 Error_Msg_SC ("(Ada 83) keyword* cannot be used!"); 152 Token := Token_95; 153 else 154 Restore_Scan_State (Scan_State); -- to identifier 155 end if; 156 end Check_95_Keyword; 157 158 ---------------------- 159 -- Check_Bad_Layout -- 160 ---------------------- 161 162 procedure Check_Bad_Layout is 163 begin 164 if RM_Column_Check and then Token_Is_At_Start_Of_Line 165 and then Start_Column <= Scope.Table (Scope.Last).Ecol 166 then 167 Error_Msg_BC -- CODEFIX 168 ("(style) incorrect layout"); 169 end if; 170 end Check_Bad_Layout; 171 172 -------------------------- 173 -- Check_Future_Keyword -- 174 -------------------------- 175 176 procedure Check_Future_Keyword is 177 begin 178 -- Ada 2005 (AI-284): Compiling in Ada 95 mode we warn that INTERFACE, 179 -- OVERRIDING, and SYNCHRONIZED are new reserved words. 180 181 if Ada_Version = Ada_95 182 and then Warn_On_Ada_2005_Compatibility 183 then 184 if Token_Name = Name_Overriding 185 or else Token_Name = Name_Synchronized 186 or else (Token_Name = Name_Interface 187 and then Prev_Token /= Tok_Pragma) 188 then 189 Error_Msg_N ("& is a reserved word in Ada 2005?y?", Token_Node); 190 end if; 191 end if; 192 193 -- Similarly, warn about Ada 2012 reserved words 194 195 if Ada_Version in Ada_95 .. Ada_2005 196 and then Warn_On_Ada_2012_Compatibility 197 then 198 if Token_Name = Name_Some then 199 Error_Msg_N ("& is a reserved word in Ada 2012?y?", Token_Node); 200 end if; 201 end if; 202 203 -- Note: we deliberately do not emit these warnings when operating in 204 -- Ada 83 mode because in that case we assume the user is building 205 -- legacy code anyway and is not interested in updating Ada versions. 206 207 end Check_Future_Keyword; 208 209 -------------------------- 210 -- Check_Misspelling_Of -- 211 -------------------------- 212 213 procedure Check_Misspelling_Of (T : Token_Type) is 214 begin 215 if Bad_Spelling_Of (T) then 216 null; 217 end if; 218 end Check_Misspelling_Of; 219 220 ----------------------------- 221 -- Check_Simple_Expression -- 222 ----------------------------- 223 224 procedure Check_Simple_Expression (E : Node_Id) is 225 begin 226 if Expr_Form = EF_Non_Simple then 227 Error_Msg_N ("this expression must be parenthesized", E); 228 end if; 229 end Check_Simple_Expression; 230 231 --------------------------------------- 232 -- Check_Simple_Expression_In_Ada_83 -- 233 --------------------------------------- 234 235 procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id) is 236 begin 237 if Expr_Form = EF_Non_Simple then 238 if Ada_Version = Ada_83 then 239 Error_Msg_N ("(Ada 83) this expression must be parenthesized!", E); 240 end if; 241 end if; 242 end Check_Simple_Expression_In_Ada_83; 243 244 ------------------------ 245 -- Check_Subtype_Mark -- 246 ------------------------ 247 248 function Check_Subtype_Mark (Mark : Node_Id) return Node_Id is 249 begin 250 if Nkind (Mark) = N_Identifier 251 or else Nkind (Mark) = N_Selected_Component 252 or else (Nkind (Mark) = N_Attribute_Reference 253 and then Is_Type_Attribute_Name (Attribute_Name (Mark))) 254 or else Mark = Error 255 then 256 return Mark; 257 else 258 Error_Msg ("subtype mark expected", Sloc (Mark)); 259 return Error; 260 end if; 261 end Check_Subtype_Mark; 262 263 ------------------- 264 -- Comma_Present -- 265 ------------------- 266 267 function Comma_Present return Boolean is 268 Scan_State : Saved_Scan_State; 269 Paren_Count : Nat; 270 271 begin 272 -- First check, if a comma is present, then a comma is present! 273 274 if Token = Tok_Comma then 275 T_Comma; 276 return True; 277 278 -- If we have a right paren, then that is taken as ending the list 279 -- i.e. no comma is present. 280 281 elsif Token = Tok_Right_Paren then 282 return False; 283 284 -- If pragmas, then get rid of them and make a recursive call 285 -- to process what follows these pragmas. 286 287 elsif Token = Tok_Pragma then 288 P_Pragmas_Misplaced; 289 return Comma_Present; 290 291 -- At this stage we have an error, and the goal is to decide on whether 292 -- or not we should diagnose an error and report a (non-existent) 293 -- comma as being present, or simply to report no comma is present 294 295 -- If we are a semicolon, then the question is whether we have a missing 296 -- right paren, or whether the semicolon should have been a comma. To 297 -- guess the right answer, we scan ahead keeping track of the paren 298 -- level, looking for a clue that helps us make the right decision. 299 300 -- This approach is highly accurate in the single error case, and does 301 -- not make bad mistakes in the multiple error case (indeed we can't 302 -- really make a very bad decision at this point in any case). 303 304 elsif Token = Tok_Semicolon then 305 Save_Scan_State (Scan_State); 306 Scan; -- past semicolon 307 308 -- Check for being followed by identifier => which almost certainly 309 -- means we are still in a parameter list and the comma should have 310 -- been a semicolon (such a sequence could not follow a semicolon) 311 312 if Token = Tok_Identifier then 313 Scan; 314 315 if Token = Tok_Arrow then 316 goto Assume_Comma; 317 end if; 318 end if; 319 320 -- If that test didn't work, loop ahead looking for a comma or 321 -- semicolon at the same parenthesis level. Always remember that 322 -- we can't go badly wrong in an error situation like this! 323 324 Paren_Count := 0; 325 326 -- Here is the look ahead loop, Paren_Count tells us whether the 327 -- token we are looking at is at the same paren level as the 328 -- suspicious semicolon that we are trying to figure out. 329 330 loop 331 332 -- If we hit another semicolon or an end of file, and we have 333 -- not seen a right paren or another comma on the way, then 334 -- probably the semicolon did end the list. Indeed that is 335 -- certainly the only single error correction possible here. 336 337 if Token = Tok_Semicolon or else Token = Tok_EOF then 338 Restore_Scan_State (Scan_State); 339 return False; 340 341 -- A comma at the same paren level as the semicolon is a strong 342 -- indicator that the semicolon should have been a comma, indeed 343 -- again this is the only possible single error correction. 344 345 elsif Token = Tok_Comma then 346 exit when Paren_Count = 0; 347 348 -- A left paren just bumps the paren count 349 350 elsif Token = Tok_Left_Paren then 351 Paren_Count := Paren_Count + 1; 352 353 -- A right paren that is at the same paren level as the semicolon 354 -- also means that the only possible single error correction is 355 -- to assume that the semicolon should have been a comma. If we 356 -- are not at the same paren level, then adjust the paren level. 357 358 elsif Token = Tok_Right_Paren then 359 exit when Paren_Count = 0; 360 Paren_Count := Paren_Count - 1; 361 end if; 362 363 -- Keep going, we haven't made a decision yet 364 365 Scan; 366 end loop; 367 368 -- If we fall through the loop, it means that we found a terminating 369 -- right paren or another comma. In either case it is reasonable to 370 -- assume that the semicolon was really intended to be a comma. Also 371 -- come here for the identifier arrow case. 372 373 <<Assume_Comma>> 374 Restore_Scan_State (Scan_State); 375 Error_Msg_SC -- CODEFIX 376 ("|"";"" should be "","""); 377 Scan; -- past the semicolon 378 return True; 379 380 -- If we are not at semicolon or a right paren, then we base the 381 -- decision on whether or not the next token can be part of an 382 -- expression. If not, then decide that no comma is present (the 383 -- caller will eventually generate a missing right parent message) 384 385 elsif Token in Token_Class_Eterm then 386 return False; 387 388 -- Otherwise we assume a comma is present, even if none is present, 389 -- since the next token must be part of an expression, so if we were 390 -- at the end of the list, then there is more than one error present. 391 392 else 393 T_Comma; -- to give error 394 return True; 395 end if; 396 end Comma_Present; 397 398 ----------------------- 399 -- Discard_Junk_List -- 400 ----------------------- 401 402 procedure Discard_Junk_List (L : List_Id) is 403 pragma Warnings (Off, L); 404 begin 405 null; 406 end Discard_Junk_List; 407 408 ----------------------- 409 -- Discard_Junk_Node -- 410 ----------------------- 411 412 procedure Discard_Junk_Node (N : Node_Id) is 413 pragma Warnings (Off, N); 414 begin 415 null; 416 end Discard_Junk_Node; 417 418 ------------ 419 -- Ignore -- 420 ------------ 421 422 procedure Ignore (T : Token_Type) is 423 begin 424 while Token = T loop 425 if T = Tok_Comma then 426 Error_Msg_SC -- CODEFIX 427 ("|extra "","" ignored"); 428 429 elsif T = Tok_Left_Paren then 430 Error_Msg_SC -- CODEFIX 431 ("|extra ""("" ignored"); 432 433 elsif T = Tok_Right_Paren then 434 Error_Msg_SC -- CODEFIX 435 ("|extra "")"" ignored"); 436 437 elsif T = Tok_Semicolon then 438 Error_Msg_SC -- CODEFIX 439 ("|extra "";"" ignored"); 440 441 elsif T = Tok_Colon then 442 Error_Msg_SC -- CODEFIX 443 ("|extra "":"" ignored"); 444 445 else 446 declare 447 Tname : constant String := Token_Type'Image (Token); 448 begin 449 Error_Msg_SC ("|extra " & Tname (5 .. Tname'Last) & "ignored"); 450 end; 451 end if; 452 453 Scan; -- Scan past ignored token 454 end loop; 455 end Ignore; 456 457 ---------------------------- 458 -- Is_Reserved_Identifier -- 459 ---------------------------- 460 461 function Is_Reserved_Identifier (C : Id_Check := None) return Boolean is 462 begin 463 if not Is_Reserved_Keyword (Token) then 464 return False; 465 466 else 467 declare 468 Ident_Casing : constant Casing_Type := 469 Identifier_Casing (Current_Source_File); 470 Key_Casing : constant Casing_Type := 471 Keyword_Casing (Current_Source_File); 472 473 begin 474 -- If the casing of identifiers and keywords is different in 475 -- this source file, and the casing of this token matches the 476 -- keyword casing, then we return False, since it is pretty 477 -- clearly intended to be a keyword. 478 479 if Ident_Casing = Unknown 480 or else Key_Casing = Unknown 481 or else Ident_Casing = Key_Casing 482 or else Determine_Token_Casing /= Key_Casing 483 then 484 return True; 485 486 -- Here we have a keyword written clearly with keyword casing. 487 -- In default mode, we would not be willing to consider this as 488 -- a reserved identifier, but if C is set, we may still accept it 489 490 elsif C /= None then 491 declare 492 Scan_State : Saved_Scan_State; 493 OK_Next_Tok : Boolean; 494 495 begin 496 Save_Scan_State (Scan_State); 497 Scan; 498 499 if Token_Is_At_Start_Of_Line then 500 return False; 501 end if; 502 503 case C is 504 when None => 505 raise Program_Error; 506 507 when C_Comma_Right_Paren => 508 OK_Next_Tok := 509 Token = Tok_Comma or else Token = Tok_Right_Paren; 510 511 when C_Comma_Colon => 512 OK_Next_Tok := 513 Token = Tok_Comma or else Token = Tok_Colon; 514 515 when C_Do => 516 OK_Next_Tok := 517 Token = Tok_Do; 518 519 when C_Dot => 520 OK_Next_Tok := 521 Token = Tok_Dot; 522 523 when C_Greater_Greater => 524 OK_Next_Tok := 525 Token = Tok_Greater_Greater; 526 527 when C_In => 528 OK_Next_Tok := 529 Token = Tok_In; 530 531 when C_Is => 532 OK_Next_Tok := 533 Token = Tok_Is; 534 535 when C_Left_Paren_Semicolon => 536 OK_Next_Tok := 537 Token = Tok_Left_Paren or else Token = Tok_Semicolon; 538 539 when C_Use => 540 OK_Next_Tok := 541 Token = Tok_Use; 542 543 when C_Vertical_Bar_Arrow => 544 OK_Next_Tok := 545 Token = Tok_Vertical_Bar or else Token = Tok_Arrow; 546 end case; 547 548 Restore_Scan_State (Scan_State); 549 550 if OK_Next_Tok then 551 return True; 552 end if; 553 end; 554 end if; 555 end; 556 end if; 557 558 -- If we fall through it is not a reserved identifier 559 560 return False; 561 end Is_Reserved_Identifier; 562 563 ---------------------- 564 -- Merge_Identifier -- 565 ---------------------- 566 567 procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type) is 568 begin 569 if Token /= Tok_Identifier then 570 return; 571 end if; 572 573 declare 574 S : Saved_Scan_State; 575 T : Token_Type; 576 577 begin 578 Save_Scan_State (S); 579 Scan; 580 T := Token; 581 Restore_Scan_State (S); 582 583 if T /= Nxt then 584 return; 585 end if; 586 end; 587 588 -- Check exactly one space between identifiers 589 590 if Source (Token_Ptr - 1) /= ' ' 591 or else Int (Token_Ptr) /= 592 Int (Prev_Token_Ptr) + Length_Of_Name (Chars (Prev)) + 1 593 then 594 return; 595 end if; 596 597 -- Do the merge 598 599 Get_Name_String (Chars (Token_Node)); 600 601 declare 602 Buf : constant String (1 .. Name_Len) := 603 Name_Buffer (1 .. Name_Len); 604 605 begin 606 Get_Name_String (Chars (Prev)); 607 Add_Char_To_Name_Buffer ('_'); 608 Add_Str_To_Name_Buffer (Buf); 609 Set_Chars (Prev, Name_Find); 610 end; 611 612 Error_Msg_Node_1 := Prev; 613 Error_Msg_SC ("unexpected identifier, possibly & was meant here"); 614 Scan; 615 end Merge_Identifier; 616 617 ------------------- 618 -- Next_Token_Is -- 619 ------------------- 620 621 function Next_Token_Is (Tok : Token_Type) return Boolean is 622 Scan_State : Saved_Scan_State; 623 Result : Boolean; 624 begin 625 Save_Scan_State (Scan_State); 626 Scan; 627 Result := (Token = Tok); 628 Restore_Scan_State (Scan_State); 629 return Result; 630 end Next_Token_Is; 631 632 ------------------- 633 -- No_Constraint -- 634 ------------------- 635 636 procedure No_Constraint is 637 begin 638 -- If we have a token that could start a constraint on the same line 639 -- then cnsider this an illegal constraint. It seems unlikely it could 640 -- be anything else if it is on the same line. 641 642 if Token in Token_Class_Consk then 643 Error_Msg_SC ("constraint not allowed here"); 644 Discard_Junk_Node (P_Constraint_Opt); 645 end if; 646 end No_Constraint; 647 648 --------------------- 649 -- Pop_Scope_Stack -- 650 --------------------- 651 652 procedure Pop_Scope_Stack is 653 begin 654 pragma Assert (Scope.Last > 0); 655 Scope.Decrement_Last; 656 657 if Debug_Flag_P then 658 Error_Msg_Uint_1 := UI_From_Int (Scope.Last); 659 Error_Msg_SC ("decrement scope stack ptr, new value = ^!"); 660 end if; 661 end Pop_Scope_Stack; 662 663 ---------------------- 664 -- Push_Scope_Stack -- 665 ---------------------- 666 667 procedure Push_Scope_Stack is 668 begin 669 Scope.Increment_Last; 670 671 if Style_Check_Max_Nesting_Level 672 and then Scope.Last = Style_Max_Nesting_Level + 1 673 then 674 Error_Msg 675 ("(style) maximum nesting level exceeded", 676 First_Non_Blank_Location); 677 end if; 678 679 Scope.Table (Scope.Last).Junk := False; 680 Scope.Table (Scope.Last).Node := Empty; 681 682 if Debug_Flag_P then 683 Error_Msg_Uint_1 := UI_From_Int (Scope.Last); 684 Error_Msg_SC ("increment scope stack ptr, new value = ^!"); 685 end if; 686 end Push_Scope_Stack; 687 688 ---------------------- 689 -- Separate_Present -- 690 ---------------------- 691 692 function Separate_Present return Boolean is 693 Scan_State : Saved_Scan_State; 694 695 begin 696 if Token = Tok_Separate then 697 return True; 698 699 elsif Token /= Tok_Identifier then 700 return False; 701 702 else 703 Save_Scan_State (Scan_State); 704 Scan; -- past identifier 705 706 if Token = Tok_Semicolon then 707 Restore_Scan_State (Scan_State); 708 return Bad_Spelling_Of (Tok_Separate); 709 710 else 711 Restore_Scan_State (Scan_State); 712 return False; 713 end if; 714 end if; 715 end Separate_Present; 716 717 -------------------------- 718 -- Signal_Bad_Attribute -- 719 -------------------------- 720 721 procedure Signal_Bad_Attribute is 722 begin 723 Bad_Attribute (Token_Node, Token_Name, Warn => False); 724 end Signal_Bad_Attribute; 725 726 ----------------------------- 727 -- Token_Is_At_End_Of_Line -- 728 ----------------------------- 729 730 function Token_Is_At_End_Of_Line return Boolean is 731 S : Source_Ptr; 732 733 begin 734 -- Skip past blanks and horizontal tabs 735 736 S := Scan_Ptr; 737 while Source (S) = ' ' or else Source (S) = ASCII.HT loop 738 S := S + 1; 739 end loop; 740 741 -- We are at end of line if at a control character (CR/LF/VT/FF/EOF) 742 -- or if we are at the start of an end of line comment sequence. 743 744 return Source (S) < ' ' 745 or else (Source (S) = '-' and then Source (S + 1) = '-'); 746 end Token_Is_At_End_Of_Line; 747 748 ------------------------------- 749 -- Token_Is_At_Start_Of_Line -- 750 ------------------------------- 751 752 function Token_Is_At_Start_Of_Line return Boolean is 753 begin 754 return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF); 755 end Token_Is_At_Start_Of_Line; 756 757 ----------------------------------- 758 -- Warn_If_Standard_Redefinition -- 759 ----------------------------------- 760 761 procedure Warn_If_Standard_Redefinition (N : Node_Id) is 762 begin 763 if Warn_On_Standard_Redefinition then 764 declare 765 C : constant Entity_Id := Current_Entity (N); 766 begin 767 if Present (C) and then Sloc (C) = Standard_Location then 768 Error_Msg_N ("redefinition of entity& in Standard?K?", N); 769 end if; 770 end; 771 end if; 772 end Warn_If_Standard_Redefinition; 773 774end Util; 775