File : par-ch2.adb


   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-2015, 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 
  26 pragma Style_Checks (All_Checks);
  27 --  Turn off subprogram body ordering check. Subprograms are in order
  28 --  by RM section rather than alphabetical
  29 
  30 separate (Par)
  31 package 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       Interface_Check_Required : Boolean := False;
 228       --  Set True if check of pragma INTERFACE is required
 229 
 230       Import_Check_Required : Boolean := False;
 231       --  Set True if check of pragma IMPORT is required
 232 
 233       Arg_Count : Nat := 0;
 234       --  Number of argument associations processed
 235 
 236       Identifier_Seen : Boolean := False;
 237       --  Set True if an identifier is encountered for a pragma argument. Used
 238       --  to check that there are no more arguments without identifiers.
 239 
 240       Prag_Node     : Node_Id;
 241       Prag_Name     : Name_Id;
 242       Semicolon_Loc : Source_Ptr;
 243       Ident_Node    : Node_Id;
 244       Assoc_Node    : Node_Id;
 245       Result        : Node_Id;
 246 
 247       procedure Skip_Pragma_Semicolon;
 248       --  Skip past semicolon at end of pragma
 249 
 250       ---------------------------
 251       -- Skip_Pragma_Semicolon --
 252       ---------------------------
 253 
 254       procedure Skip_Pragma_Semicolon is
 255       begin
 256          --  If skipping the pragma, ignore a missing semicolon
 257 
 258          if Token /= Tok_Semicolon and then Skipping then
 259             null;
 260 
 261          --  Otherwise demand a semicolon
 262 
 263          else
 264             T_Semicolon;
 265          end if;
 266       end Skip_Pragma_Semicolon;
 267 
 268    --  Start of processing for P_Pragma
 269 
 270    begin
 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 Ada_Version >= Ada_2005
 283         and then Token = Tok_Interface
 284       then
 285          Prag_Name  := Name_Interface;
 286          Ident_Node := Make_Identifier (Token_Ptr, Name_Interface);
 287          Scan; -- past INTERFACE
 288       else
 289          Ident_Node := P_Identifier;
 290       end if;
 291 
 292       Set_Pragma_Identifier (Prag_Node, Ident_Node);
 293 
 294       --  See if special INTERFACE/IMPORT check is required
 295 
 296       if SIS_Entry_Active then
 297          Interface_Check_Required := (Prag_Name = Name_Interface);
 298          Import_Check_Required    := (Prag_Name = Name_Import);
 299       else
 300          Interface_Check_Required := False;
 301          Import_Check_Required    := False;
 302       end if;
 303 
 304       --  Set global to indicate if we are within a Depends pragma
 305 
 306       if Chars (Ident_Node) = Name_Depends then
 307          Inside_Depends := True;
 308       end if;
 309 
 310       --  Scan arguments. We assume that arguments are present if there is
 311       --  a left paren, or if a semicolon is missing and there is another
 312       --  token on the same line as the pragma name.
 313 
 314       if Token = Tok_Left_Paren
 315         or else (Token /= Tok_Semicolon
 316                   and then not Token_Is_At_Start_Of_Line)
 317       then
 318          Set_Pragma_Argument_Associations (Prag_Node, New_List);
 319          T_Left_Paren;
 320 
 321          loop
 322             Arg_Count := Arg_Count + 1;
 323 
 324             Scan_Pragma_Argument_Association
 325               (Identifier_Seen   => Identifier_Seen,
 326                Association       => Assoc_Node,
 327                Reserved_Words_OK =>
 328                  Nam_In (Prag_Name, Name_Restriction_Warnings,
 329                                     Name_Restrictions));
 330 
 331             if Arg_Count = 2
 332               and then (Interface_Check_Required or else Import_Check_Required)
 333             then
 334                --  Here is where we cancel the SIS active status if this pragma
 335                --  supplies a body for the currently active subprogram spec.
 336 
 337                if Nkind (Expression (Assoc_Node)) in N_Direct_Name
 338                  and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
 339                then
 340                   SIS_Entry_Active := False;
 341                end if;
 342             end if;
 343 
 344             Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
 345             exit when Token /= Tok_Comma;
 346             Scan; -- past comma
 347          end loop;
 348 
 349          --  If we have := for pragma Debug, it is worth special casing the
 350          --  error message (it is easy to think of pragma Debug as taking a
 351          --  statement, and an assignment statement is the most likely
 352          --  candidate for this error)
 353 
 354          if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
 355             Error_Msg_SC ("argument for pragma Debug must be procedure call");
 356             Resync_To_Semicolon;
 357 
 358          --  Normal case, we expect a right paren here
 359 
 360          else
 361             T_Right_Paren;
 362          end if;
 363       end if;
 364 
 365       Semicolon_Loc := Token_Ptr;
 366 
 367       --  Cancel indication of being within Depends pragm. Can be done
 368       --  unconditionally, since quicker than doing a test.
 369 
 370       Inside_Depends := False;
 371 
 372       --  Now we have two tasks left, we need to scan out the semicolon
 373       --  following the pragma, and we have to call Par.Prag to process
 374       --  the pragma. Normally we do them in this order, however, there
 375       --  is one exception namely pragma Style_Checks where we like to
 376       --  skip the semicolon after processing the pragma, since that way
 377       --  the style checks for the scanning of the semicolon follow the
 378       --  settings of the pragma.
 379 
 380       --  You might think we could just unconditionally do things in
 381       --  the opposite order, but there are other pragmas, notably the
 382       --  case of pragma Source_File_Name, which assume the semicolon
 383       --  is already scanned out.
 384 
 385       if Prag_Name = Name_Style_Checks then
 386          Result := Par.Prag (Prag_Node, Semicolon_Loc);
 387          Skip_Pragma_Semicolon;
 388          return Result;
 389       else
 390          Skip_Pragma_Semicolon;
 391          return Par.Prag (Prag_Node, Semicolon_Loc);
 392       end if;
 393 
 394    exception
 395       when Error_Resync =>
 396          Resync_Past_Semicolon;
 397          return Error;
 398 
 399    end P_Pragma;
 400 
 401    --  This routine is called if a pragma is encountered in an inappropriate
 402    --  position, the pragma is scanned out and control returns to continue.
 403 
 404    --  The caller has checked that the initial token is pragma
 405 
 406    --  Error recovery: cannot raise Error_Resync
 407 
 408    procedure P_Pragmas_Misplaced is
 409    begin
 410       while Token = Tok_Pragma loop
 411          Error_Msg_SC ("pragma not allowed here");
 412          Discard_Junk_Node (P_Pragma (Skipping => True));
 413       end loop;
 414    end P_Pragmas_Misplaced;
 415 
 416    --  This function is called to scan out an optional sequence of pragmas.
 417    --  If no pragmas are found, then No_List is returned.
 418 
 419    --  Error recovery: Cannot raise Error_Resync
 420 
 421    function P_Pragmas_Opt return List_Id is
 422       L : List_Id;
 423 
 424    begin
 425       if Token = Tok_Pragma then
 426          L := New_List;
 427          P_Pragmas_Opt (L);
 428          return L;
 429 
 430       else
 431          return No_List;
 432       end if;
 433    end P_Pragmas_Opt;
 434 
 435    --  This procedure is called to scan out an optional sequence of pragmas.
 436    --  Any pragmas found are appended to the list provided as an argument.
 437 
 438    --  Error recovery: Cannot raise Error_Resync
 439 
 440    procedure P_Pragmas_Opt (List : List_Id) is
 441       P     : Node_Id;
 442 
 443    begin
 444       while Token = Tok_Pragma loop
 445          P := P_Pragma;
 446 
 447          if Nkind (P) /= N_Error
 448            and then Nam_In (Pragma_Name (P), Name_Assert, Name_Debug)
 449          then
 450             Error_Msg_Name_1 := Pragma_Name (P);
 451             Error_Msg_N
 452               ("pragma% must be in declaration/statement context", P);
 453          else
 454             Append (P, List);
 455          end if;
 456       end loop;
 457    end P_Pragmas_Opt;
 458 
 459    --------------------------------------
 460    -- 2.8  Pragma_Argument Association --
 461    --------------------------------------
 462 
 463    --  PRAGMA_ARGUMENT_ASSOCIATION ::=
 464    --    [pragma_argument_IDENTIFIER =>] NAME
 465    --  | [pragma_argument_IDENTIFIER =>] EXPRESSION
 466 
 467    --  In Ada 2012, there are two more possibilities:
 468 
 469    --  PRAGMA_ARGUMENT_ASSOCIATION ::=
 470    --    [pragma_argument_ASPECT_MARK =>] NAME
 471    --  | [pragma_argument_ASPECT_MARK =>] EXPRESSION
 472 
 473    --  where the interesting allowed cases (which do not fit the syntax of the
 474    --  first alternative above) are
 475 
 476    --  ASPECT_MARK ::=
 477    --    Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class
 478 
 479    --  We allow this special usage in all Ada modes, but it would be a pain to
 480    --  allow these aspects to pervade the pragma syntax, and the representation
 481    --  of pragma nodes internally. So what we do is to replace these
 482    --  ASPECT_MARK forms with identifiers whose name is one of the special
 483    --  internal names _Pre, _Post, _Invariant, or _Type_Invariant.
 484 
 485    --  Error recovery: cannot raise Error_Resync
 486 
 487    procedure Scan_Pragma_Argument_Association
 488      (Identifier_Seen   : in out Boolean;
 489       Association       : out Node_Id;
 490       Reserved_Words_OK : Boolean := False)
 491    is
 492       function P_Expression_Or_Reserved_Word return Node_Id;
 493       --  Parse an expression or, if the token denotes one of the following
 494       --  reserved words, construct an identifier with proper Chars field.
 495       --    Access
 496       --    Delta
 497       --    Digits
 498       --    Mod
 499       --    Range
 500 
 501       -----------------------------------
 502       -- P_Expression_Or_Reserved_Word --
 503       -----------------------------------
 504 
 505       function P_Expression_Or_Reserved_Word return Node_Id is
 506          Word    : Node_Id;
 507          Word_Id : Name_Id;
 508 
 509       begin
 510          Word_Id := No_Name;
 511 
 512          if Token = Tok_Access then
 513             Word_Id := Name_Access;
 514             Scan; -- past ACCESS
 515 
 516          elsif Token = Tok_Delta then
 517             Word_Id := Name_Delta;
 518             Scan; -- past DELTA
 519 
 520          elsif Token = Tok_Digits then
 521             Word_Id := Name_Digits;
 522             Scan; -- past DIGITS
 523 
 524          elsif Token = Tok_Mod then
 525             Word_Id := Name_Mod;
 526             Scan; -- past MOD
 527 
 528          elsif Token = Tok_Range then
 529             Word_Id := Name_Range;
 530             Scan; -- post RANGE
 531          end if;
 532 
 533          if Word_Id = No_Name then
 534             return P_Expression;
 535          else
 536             Word := New_Node (N_Identifier, Token_Ptr);
 537             Set_Chars (Word, Word_Id);
 538             return Word;
 539          end if;
 540       end P_Expression_Or_Reserved_Word;
 541 
 542       --  Local variables
 543 
 544       Expression_Node : Node_Id;
 545       Identifier_Node : Node_Id;
 546       Identifier_OK   : Boolean;
 547       Scan_State      : Saved_Scan_State;
 548 
 549    --  Start of processing for Scan_Pragma_Argument_Association
 550 
 551    begin
 552       Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
 553       Set_Chars (Association, No_Name);
 554       Identifier_OK := False;
 555 
 556       --  Argument starts with identifier
 557 
 558       if Token = Tok_Identifier then
 559          Identifier_Node := Token_Node;
 560          Save_Scan_State (Scan_State); -- at Identifier
 561          Scan; -- past Identifier
 562 
 563          if Token = Tok_Arrow then
 564             Scan; -- past arrow
 565             Identifier_OK := True;
 566 
 567          --  Case of one of the special aspect forms
 568 
 569          elsif Token = Tok_Apostrophe then
 570             Scan; -- past apostrophe
 571 
 572             --  We have apostrophe, so check for identifier'Class
 573 
 574             if Token /= Tok_Identifier or else Token_Name /= Name_Class then
 575                null;
 576 
 577             --  We have identifier'Class, check for arrow
 578 
 579             else
 580                Scan; -- Past Class
 581 
 582                if Token /= Tok_Arrow then
 583                   null;
 584 
 585                --  Here we have scanned identifier'Class =>
 586 
 587                else
 588                   Identifier_OK := True;
 589                   Scan; -- past arrow
 590 
 591                   case Chars (Identifier_Node) is
 592                      when Name_Pre =>
 593                         Set_Chars (Identifier_Node, Name_uPre);
 594 
 595                      when Name_Post =>
 596                         Set_Chars (Identifier_Node, Name_uPost);
 597 
 598                      when Name_Type_Invariant =>
 599                         Set_Chars (Identifier_Node, Name_uType_Invariant);
 600 
 601                      when Name_Invariant =>
 602                         Set_Chars (Identifier_Node, Name_uInvariant);
 603 
 604                      --  If it is X'Class => for some invalid X, we will give
 605                      --  an error, and forget that 'Class was present, which
 606                      --  will give better error recovery. We could do a spell
 607                      --  check here, but it seems too much work.
 608 
 609                      when others =>
 610                         Error_Msg_SC ("invalid aspect id for pragma");
 611                   end case;
 612                end if;
 613             end if;
 614          end if;
 615 
 616          --  Identifier was present
 617 
 618          if Identifier_OK then
 619             Set_Chars (Association, Chars (Identifier_Node));
 620             Identifier_Seen := True;
 621 
 622          --  Identifier not present after all
 623 
 624          else
 625             Restore_Scan_State (Scan_State); -- to Identifier
 626          end if;
 627       end if;
 628 
 629       --  Diagnose error of "positional" argument for pragma appearing after
 630       --  a "named" argument (quotes here are because that's not quite accurate
 631       --  Ada RM terminology).
 632 
 633       --  Since older GNAT versions did not generate this error, disable this
 634       --  message in Relaxed_RM_Semantics mode to help legacy code using e.g.
 635       --  codepeer.
 636 
 637       if Identifier_Seen
 638         and not Identifier_OK
 639         and not Relaxed_RM_Semantics
 640       then
 641          Error_Msg_SC ("|pragma argument identifier required here");
 642          Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
 643       end if;
 644 
 645       if Identifier_OK then
 646 
 647          --  Certain pragmas such as Restriction_Warnings and Restrictions
 648          --  allow reserved words to appear as expressions when checking for
 649          --  prohibited uses of attributes.
 650 
 651          if Reserved_Words_OK
 652            and then Chars (Identifier_Node) = Name_No_Use_Of_Attribute
 653          then
 654             Expression_Node := P_Expression_Or_Reserved_Word;
 655          else
 656             Expression_Node := P_Expression;
 657          end if;
 658       else
 659          Expression_Node := P_Expression_If_OK;
 660       end if;
 661 
 662       Set_Expression (Association, Expression_Node);
 663    end Scan_Pragma_Argument_Association;
 664 
 665 end Ch2;