| 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-2025, 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 |
| 66 |
(C : Id_Check := None; |
| 67 |
Force_Msg : Boolean := False) |
| 68 |
return Node_Id |
| 69 |
is |
| 70 |
Ident_Node : Node_Id; |
| 71 |
|
| 72 |
begin |
| 73 |
-- All set if we do indeed have an identifier |
| 74 |
|
| 75 |
if Token = Tok_Identifier then |
| 76 |
Check_Future_Keyword; |
| 77 |
|
| 78 |
-- If we have a reserved identifier, manufacture an identifier with |
| 79 |
-- a corresponding name after posting an appropriate error message |
| 80 |
|
| 81 |
elsif Is_Reserved_Identifier (C) then |
| 82 |
Scan_Reserved_Identifier (Force_Msg => Force_Msg); |
| 83 |
|
| 84 |
-- Otherwise we have junk that cannot be interpreted as an identifier |
| 85 |
|
| 86 |
else |
| 87 |
T_Identifier; -- to give message |
| 88 |
raise Error_Resync; |
| 89 |
end if; |
| 90 |
|
| 91 |
if Style_Check then |
| 92 |
Style.Check_Defining_Identifier_Casing; |
| 93 |
end if; |
| 94 |
|
| 95 |
Ident_Node := Token_Node; |
| 96 |
Scan; -- past the identifier |
| 97 |
|
| 98 |
return Ident_Node; |
| 99 |
end P_Identifier; |
| 100 |
|
| 101 |
-------------------------- |
| 102 |
-- 2.3 Letter Or Digit -- |
| 103 |
-------------------------- |
| 104 |
|
| 105 |
-- Parsed by P_Identifier (2.3) |
| 106 |
|
| 107 |
-------------------------- |
| 108 |
-- 2.4 Numeric Literal -- |
| 109 |
-------------------------- |
| 110 |
|
| 111 |
-- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL |
| 112 |
|
| 113 |
-- Numeric literal is returned by the scanner as either |
| 114 |
-- Tok_Integer_Literal or Tok_Real_Literal |
| 115 |
|
| 116 |
---------------------------- |
| 117 |
-- 2.4.1 Decimal Literal -- |
| 118 |
---------------------------- |
| 119 |
|
| 120 |
-- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT] |
| 121 |
|
| 122 |
-- Handled by scanner as part of numeric literal handing (see 2.4) |
| 123 |
|
| 124 |
-------------------- |
| 125 |
-- 2.4.1 Numeral -- |
| 126 |
-------------------- |
| 127 |
|
| 128 |
-- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT} |
| 129 |
|
| 130 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 131 |
|
| 132 |
--------------------- |
| 133 |
-- 2.4.1 Exponent -- |
| 134 |
--------------------- |
| 135 |
|
| 136 |
-- EXPONENT ::= E [+] NUMERAL | E - NUMERAL |
| 137 |
|
| 138 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 139 |
|
| 140 |
-------------------------- |
| 141 |
-- 2.4.2 Based Literal -- |
| 142 |
-------------------------- |
| 143 |
|
| 144 |
-- BASED_LITERAL ::= |
| 145 |
-- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT] |
| 146 |
|
| 147 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 148 |
|
| 149 |
----------------- |
| 150 |
-- 2.4.2 Base -- |
| 151 |
----------------- |
| 152 |
|
| 153 |
-- BASE ::= NUMERAL |
| 154 |
|
| 155 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 156 |
|
| 157 |
-------------------------- |
| 158 |
-- 2.4.2 Based Numeral -- |
| 159 |
-------------------------- |
| 160 |
|
| 161 |
-- BASED_NUMERAL ::= |
| 162 |
-- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT} |
| 163 |
|
| 164 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 165 |
|
| 166 |
--------------------------- |
| 167 |
-- 2.4.2 Extended Digit -- |
| 168 |
--------------------------- |
| 169 |
|
| 170 |
-- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F |
| 171 |
|
| 172 |
-- Handled by scanner as part of numeric literal handling (see 2.4) |
| 173 |
|
| 174 |
---------------------------- |
| 175 |
-- 2.5 Character Literal -- |
| 176 |
---------------------------- |
| 177 |
|
| 178 |
-- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER ' |
| 179 |
|
| 180 |
-- Handled by the scanner and returned as Tok_Char_Literal |
| 181 |
|
| 182 |
------------------------- |
| 183 |
-- 2.6 String Literal -- |
| 184 |
------------------------- |
| 185 |
|
| 186 |
-- STRING LITERAL ::= "{STRING_ELEMENT}" |
| 187 |
|
| 188 |
-- Handled by the scanner and returned as Tok_String_Literal |
| 189 |
-- or if the string looks like an operator as Tok_Operator_Symbol. |
| 190 |
|
| 191 |
------------------------- |
| 192 |
-- 2.6 String Element -- |
| 193 |
------------------------- |
| 194 |
|
| 195 |
-- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER |
| 196 |
|
| 197 |
-- A STRING_ELEMENT is either a pair of quotation marks ("), |
| 198 |
-- or a single GRAPHIC_CHARACTER other than a quotation mark. |
| 199 |
|
| 200 |
-- Handled by scanner as part of string literal handling (see 2.4) |
| 201 |
|
| 202 |
--------------------------------------- |
| 203 |
-- 2.6 Interpolated String Literal -- |
| 204 |
--------------------------------------- |
| 205 |
|
| 206 |
-- INTERPOLATED_STRING_LITERAL ::= |
| 207 |
-- 'f' "{INTERPOLATED_STRING_ELEMENT}" { |
| 208 |
-- "{INTERPOLATED_STRING_ELEMENT}" } |
| 209 |
|
| 210 |
-- INTERPOLATED_STRING_ELEMENT ::= |
| 211 |
-- ESCAPED_CHARACTER | INTERPOLATED_EXPRESSION |
| 212 |
-- | non_quotation_mark_non_left_brace_GRAPHIC_CHARACTER |
| 213 |
|
| 214 |
-- ESCAPED_CHARACTER ::= '\GRAPHIC_CHARACTER' |
| 215 |
|
| 216 |
-- INTERPOLATED_EXPRESSION ::= '{' EXPRESSION '}' |
| 217 |
|
| 218 |
-- Interpolated string element and escaped character rules are handled by |
| 219 |
-- scanner as part of string literal handling. |
| 220 |
|
| 221 |
----------------------------------- |
| 222 |
-- P_Interpolated_String_Literal -- |
| 223 |
----------------------------------- |
| 224 |
|
| 225 |
function P_Interpolated_String_Literal return Node_Id is |
| 226 |
Elements_List : constant List_Id := New_List; |
| 227 |
Saved_State : constant Boolean := Inside_Interpolated_String_Literal; |
| 228 |
String_Node : Node_Id; |
| 229 |
|
| 230 |
begin |
| 231 |
String_Node := New_Node (N_Interpolated_String_Literal, Token_Ptr); |
| 232 |
Inside_Interpolated_String_Literal := True; |
| 233 |
|
| 234 |
Scan; -- past 'f' |
| 235 |
|
| 236 |
if Token /= Tok_String_Literal then |
| 237 |
Error_Msg_SC ("string literal expected"); |
| 238 |
|
| 239 |
else |
| 240 |
Set_Is_Interpolated_String_Literal (Token_Node); |
| 241 |
Append_To (Elements_List, Token_Node); |
| 242 |
Scan; -- past string_literal |
| 243 |
|
| 244 |
while Token in Tok_Left_Curly_Bracket | Tok_String_Literal loop |
| 245 |
|
| 246 |
-- Interpolated expression |
| 247 |
|
| 248 |
if Token = Tok_Left_Curly_Bracket then |
| 249 |
declare |
| 250 |
Saved_In_Expr : constant Boolean := |
| 251 |
Inside_Interpolated_String_Expression; |
| 252 |
|
| 253 |
begin |
| 254 |
Scan; -- past '{' |
| 255 |
Inside_Interpolated_String_Expression := True; |
| 256 |
Append_To (Elements_List, P_Expression); |
| 257 |
Inside_Interpolated_String_Expression := Saved_In_Expr; |
| 258 |
T_Right_Curly_Bracket; |
| 259 |
end; |
| 260 |
else |
| 261 |
if Prev_Token /= Tok_Right_Curly_Bracket then |
| 262 |
Error_Msg_SC ("unexpected string literal"); |
| 263 |
end if; |
| 264 |
|
| 265 |
Set_Is_Interpolated_String_Literal (Token_Node); |
| 266 |
Append_To (Elements_List, Token_Node); |
| 267 |
Scan; -- past string_literal |
| 268 |
end if; |
| 269 |
end loop; |
| 270 |
end if; |
| 271 |
|
| 272 |
Inside_Interpolated_String_Literal := Saved_State; |
| 273 |
Set_Expressions (String_Node, Elements_List); |
| 274 |
|
| 275 |
return String_Node; |
| 276 |
end P_Interpolated_String_Literal; |
| 277 |
|
| 278 |
------------------ |
| 279 |
-- 2.7 Comment -- |
| 280 |
------------------ |
| 281 |
|
| 282 |
-- A COMMENT starts with two adjacent hyphens and extends up to the |
| 283 |
-- end of the line. A COMMENT may appear on any line of a program. |
| 284 |
|
| 285 |
-- Handled by the scanner which simply skips past encountered comments |
| 286 |
|
| 287 |
----------------- |
| 288 |
-- 2.8 Pragma -- |
| 289 |
----------------- |
| 290 |
|
| 291 |
-- PRAGMA ::= pragma IDENTIFIER |
| 292 |
-- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})]; |
| 293 |
|
| 294 |
-- The caller has checked that the initial token is PRAGMA |
| 295 |
|
| 296 |
-- Error recovery: cannot raise Error_Resync |
| 297 |
|
| 298 |
-- One special piece of processing is needed in this routine. As described |
| 299 |
-- in the section on "Handling semicolon used in place of IS" in module |
| 300 |
-- Parse, the parser detects the case of missing subprogram bodies to |
| 301 |
-- allow recovery from this syntactic error. Pragma INTERFACE (and, for |
| 302 |
-- Ada 95, pragma IMPORT) can appear in place of the body. The parser must |
| 303 |
-- recognize the use of these two pragmas in this context, otherwise it |
| 304 |
-- will think there are missing bodies, and try to change ; to IS, when |
| 305 |
-- in fact the bodies ARE present, supplied by these pragmas. |
| 306 |
|
| 307 |
function P_Pragma (Skipping : Boolean := False) return Node_Id is |
| 308 |
procedure Skip_Pragma_Semicolon; |
| 309 |
-- Skip past semicolon at end of pragma |
| 310 |
|
| 311 |
--------------------------- |
| 312 |
-- Skip_Pragma_Semicolon -- |
| 313 |
--------------------------- |
| 314 |
|
| 315 |
procedure Skip_Pragma_Semicolon is |
| 316 |
begin |
| 317 |
-- If skipping the pragma, ignore a missing semicolon |
| 318 |
|
| 319 |
if Token /= Tok_Semicolon and then Skipping then |
| 320 |
null; |
| 321 |
|
| 322 |
-- Otherwise demand a semicolon |
| 323 |
|
| 324 |
else |
| 325 |
T_Semicolon; |
| 326 |
end if; |
| 327 |
end Skip_Pragma_Semicolon; |
| 328 |
|
| 329 |
-- Local variables |
| 330 |
|
| 331 |
Import_Check_Required : Boolean := False; |
| 332 |
-- Set True if check of pragma IMPORT or INTERFACE is required |
| 333 |
|
| 334 |
Arg_Count : Nat := 0; |
| 335 |
-- Number of argument associations processed |
| 336 |
|
| 337 |
Identifier_Seen : Boolean := False; |
| 338 |
-- Set True if an identifier is encountered for a pragma argument. Used |
| 339 |
-- to check that there are no more arguments without identifiers. |
| 340 |
|
| 341 |
Assoc_Node : Node_Id; |
| 342 |
Ident_Node : Node_Id; |
| 343 |
Prag_Name : Name_Id; |
| 344 |
Prag_Node : Node_Id; |
| 345 |
Result : Node_Id; |
| 346 |
Semicolon_Loc : Source_Ptr; |
| 347 |
|
| 348 |
-- Start of processing for P_Pragma |
| 349 |
|
| 350 |
begin |
| 351 |
Inside_Pragma := True; |
| 352 |
Prag_Node := New_Node (N_Pragma, Token_Ptr); |
| 353 |
Scan; -- past PRAGMA |
| 354 |
Prag_Name := Token_Name; |
| 355 |
|
| 356 |
if Style_Check then |
| 357 |
Style.Check_Pragma_Name; |
| 358 |
end if; |
| 359 |
|
| 360 |
-- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is |
| 361 |
-- allowed as a pragma name. |
| 362 |
|
| 363 |
if Is_Reserved_Keyword (Token) then |
| 364 |
Prag_Name := Keyword_Name (Token); |
| 365 |
Ident_Node := Make_Identifier (Token_Ptr, Prag_Name); |
| 366 |
Scan; -- past the keyword |
| 367 |
else |
| 368 |
Ident_Node := P_Identifier; |
| 369 |
end if; |
| 370 |
|
| 371 |
Set_Pragma_Identifier (Prag_Node, Ident_Node); |
| 372 |
|
| 373 |
-- See if special INTERFACE/IMPORT check is required |
| 374 |
|
| 375 |
if SIS_Entry_Active then |
| 376 |
Import_Check_Required := |
| 377 |
Prag_Name = Name_Import or else Prag_Name = Name_Interface; |
| 378 |
else |
| 379 |
Import_Check_Required := False; |
| 380 |
end if; |
| 381 |
|
| 382 |
-- Set global to indicate if we are within a Depends pragma |
| 383 |
|
| 384 |
if Chars (Ident_Node) = Name_Depends |
| 385 |
or else Chars (Ident_Node) = Name_Refined_Depends |
| 386 |
then |
| 387 |
Inside_Depends := True; |
| 388 |
end if; |
| 389 |
|
| 390 |
-- Scan arguments. We assume that arguments are present if there is |
| 391 |
-- a left paren, or if a semicolon is missing and there is another |
| 392 |
-- token on the same line as the pragma name. |
| 393 |
|
| 394 |
if Token = Tok_Left_Paren |
| 395 |
or else (Token /= Tok_Semicolon |
| 396 |
and then not Token_Is_At_Start_Of_Line) |
| 397 |
then |
| 398 |
Set_Pragma_Argument_Associations (Prag_Node, New_List); |
| 399 |
T_Left_Paren; |
| 400 |
|
| 401 |
loop |
| 402 |
Arg_Count := Arg_Count + 1; |
| 403 |
|
| 404 |
Scan_Pragma_Argument_Association |
| 405 |
(Identifier_Seen => Identifier_Seen, |
| 406 |
Association => Assoc_Node, |
| 407 |
Reserved_Words_OK => |
| 408 |
Prag_Name in Name_Restriction_Warnings | Name_Restrictions); |
| 409 |
|
| 410 |
if Arg_Count = 2 and then Import_Check_Required then |
| 411 |
-- Here is where we cancel the SIS active status if this pragma |
| 412 |
-- supplies a body for the currently active subprogram spec. |
| 413 |
|
| 414 |
if Nkind (Expression (Assoc_Node)) in N_Direct_Name |
| 415 |
and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl) |
| 416 |
then |
| 417 |
SIS_Entry_Active := False; |
| 418 |
end if; |
| 419 |
end if; |
| 420 |
|
| 421 |
Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node)); |
| 422 |
exit when Token /= Tok_Comma; |
| 423 |
Scan; -- past comma |
| 424 |
end loop; |
| 425 |
|
| 426 |
-- If we have := for pragma Debug, it is worth special casing the |
| 427 |
-- error message (it is easy to think of pragma Debug as taking a |
| 428 |
-- statement, and an assignment statement is the most likely |
| 429 |
-- candidate for this error) |
| 430 |
|
| 431 |
if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then |
| 432 |
Error_Msg_SC ("argument for pragma Debug must be procedure call"); |
| 433 |
Resync_To_Semicolon; |
| 434 |
|
| 435 |
-- Normal case, we expect a right paren here |
| 436 |
|
| 437 |
else |
| 438 |
T_Right_Paren; |
| 439 |
end if; |
| 440 |
end if; |
| 441 |
|
| 442 |
Semicolon_Loc := Token_Ptr; |
| 443 |
|
| 444 |
-- Cancel indication of being within a pragma or in particular a Depends |
| 445 |
-- pragma. |
| 446 |
|
| 447 |
Inside_Depends := False; |
| 448 |
Inside_Pragma := False; |
| 449 |
|
| 450 |
-- Now we have two tasks left, we need to scan out the semicolon |
| 451 |
-- following the pragma, and we have to call Par.Prag to process |
| 452 |
-- the pragma. Normally we do them in this order, however, there |
| 453 |
-- is one exception namely pragma Style_Checks where we like to |
| 454 |
-- skip the semicolon after processing the pragma, since that way |
| 455 |
-- the style checks for the scanning of the semicolon follow the |
| 456 |
-- settings of the pragma. |
| 457 |
|
| 458 |
-- You might think we could just unconditionally do things in |
| 459 |
-- the opposite order, but there are other pragmas, notably the |
| 460 |
-- case of pragma Source_File_Name, which assume the semicolon |
| 461 |
-- is already scanned out. |
| 462 |
|
| 463 |
if Prag_Name = Name_Style_Checks then |
| 464 |
Result := Par.Prag (Prag_Node, Semicolon_Loc); |
| 465 |
Skip_Pragma_Semicolon; |
| 466 |
return Result; |
| 467 |
else |
| 468 |
Skip_Pragma_Semicolon; |
| 469 |
return Par.Prag (Prag_Node, Semicolon_Loc); |
| 470 |
end if; |
| 471 |
|
| 472 |
exception |
| 473 |
when Error_Resync => |
| 474 |
Resync_Past_Semicolon; |
| 475 |
Inside_Depends := False; |
| 476 |
Inside_Pragma := False; |
| 477 |
return Error; |
| 478 |
end P_Pragma; |
| 479 |
|
| 480 |
-- This routine is called if a pragma is encountered in an inappropriate |
| 481 |
-- position, the pragma is scanned out and control returns to continue. |
| 482 |
|
| 483 |
-- The caller has checked that the initial token is pragma |
| 484 |
|
| 485 |
-- Error recovery: cannot raise Error_Resync |
| 486 |
|
| 487 |
procedure P_Pragmas_Misplaced is |
| 488 |
begin |
| 489 |
while Token = Tok_Pragma loop |
| 490 |
Error_Msg_SC ("pragma not allowed here"); |
| 491 |
Discard_Junk_Node (P_Pragma (Skipping => True)); |
| 492 |
end loop; |
| 493 |
end P_Pragmas_Misplaced; |
| 494 |
|
| 495 |
-- This function is called to scan out an optional sequence of pragmas. |
| 496 |
-- If no pragmas are found, then No_List is returned. |
| 497 |
|
| 498 |
-- Error recovery: Cannot raise Error_Resync |
| 499 |
|
| 500 |
function P_Pragmas_Opt return List_Id is |
| 501 |
L : List_Id; |
| 502 |
|
| 503 |
begin |
| 504 |
if Token = Tok_Pragma then |
| 505 |
L := New_List; |
| 506 |
P_Pragmas_Opt (L); |
| 507 |
return L; |
| 508 |
|
| 509 |
else |
| 510 |
return No_List; |
| 511 |
end if; |
| 512 |
end P_Pragmas_Opt; |
| 513 |
|
| 514 |
-- This procedure is called to scan out an optional sequence of pragmas. |
| 515 |
-- Any pragmas found are appended to the list provided as an argument. |
| 516 |
|
| 517 |
-- Error recovery: Cannot raise Error_Resync |
| 518 |
|
| 519 |
procedure P_Pragmas_Opt (List : List_Id) is |
| 520 |
P : Node_Id; |
| 521 |
|
| 522 |
begin |
| 523 |
while Token = Tok_Pragma loop |
| 524 |
P := P_Pragma; |
| 525 |
|
| 526 |
if Nkind (P) /= N_Error |
| 527 |
and then Pragma_Name_Unmapped (P) in Name_Assert | Name_Debug |
| 528 |
then |
| 529 |
Error_Msg_Name_1 := Pragma_Name_Unmapped (P); |
| 530 |
Error_Msg_N |
| 531 |
("pragma% must be in declaration/statement context", P); |
| 532 |
else |
| 533 |
Append (P, List); |
| 534 |
end if; |
| 535 |
end loop; |
| 536 |
end P_Pragmas_Opt; |
| 537 |
|
| 538 |
-------------------------------------- |
| 539 |
-- 2.8 Pragma_Argument Association -- |
| 540 |
-------------------------------------- |
| 541 |
|
| 542 |
-- PRAGMA_ARGUMENT_ASSOCIATION ::= |
| 543 |
-- [pragma_argument_IDENTIFIER =>] NAME |
| 544 |
-- | [pragma_argument_IDENTIFIER =>] EXPRESSION |
| 545 |
|
| 546 |
-- In Ada 2012, there are two more possibilities: |
| 547 |
|
| 548 |
-- PRAGMA_ARGUMENT_ASSOCIATION ::= |
| 549 |
-- [pragma_argument_ASPECT_MARK =>] NAME |
| 550 |
-- | [pragma_argument_ASPECT_MARK =>] EXPRESSION |
| 551 |
|
| 552 |
-- where the interesting allowed cases (which do not fit the syntax of the |
| 553 |
-- first alternative above) are |
| 554 |
|
| 555 |
-- ASPECT_MARK ::= |
| 556 |
-- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class |
| 557 |
|
| 558 |
-- We allow this special usage in all Ada modes, but it would be a pain to |
| 559 |
-- allow these aspects to pervade the pragma syntax, and the representation |
| 560 |
-- of pragma nodes internally. So what we do is to replace these |
| 561 |
-- ASPECT_MARK forms with identifiers whose name is one of the special |
| 562 |
-- internal names _Pre, _Post, _Invariant, or _Type_Invariant. |
| 563 |
|
| 564 |
-- Error recovery: cannot raise Error_Resync |
| 565 |
|
| 566 |
procedure Scan_Pragma_Argument_Association |
| 567 |
(Identifier_Seen : in out Boolean; |
| 568 |
Association : out Node_Id; |
| 569 |
Reserved_Words_OK : Boolean := False) |
| 570 |
is |
| 571 |
function P_Expression_Or_Reserved_Word return Node_Id; |
| 572 |
-- Parse an expression or, if the token is one of the following reserved |
| 573 |
-- words, construct an identifier with proper Chars field. |
| 574 |
-- Access |
| 575 |
-- Delta |
| 576 |
-- Digits |
| 577 |
-- Mod |
| 578 |
-- Range |
| 579 |
|
| 580 |
----------------------------------- |
| 581 |
-- P_Expression_Or_Reserved_Word -- |
| 582 |
----------------------------------- |
| 583 |
|
| 584 |
function P_Expression_Or_Reserved_Word return Node_Id is |
| 585 |
Word : Node_Id; |
| 586 |
Word_Id : Name_Id; |
| 587 |
|
| 588 |
begin |
| 589 |
Word_Id := No_Name; |
| 590 |
|
| 591 |
if Token = Tok_Access then |
| 592 |
Word_Id := Name_Access; |
| 593 |
Scan; -- past ACCESS |
| 594 |
|
| 595 |
elsif Token = Tok_Delta then |
| 596 |
Word_Id := Name_Delta; |
| 597 |
Scan; -- past DELTA |
| 598 |
|
| 599 |
elsif Token = Tok_Digits then |
| 600 |
Word_Id := Name_Digits; |
| 601 |
Scan; -- past DIGITS |
| 602 |
|
| 603 |
elsif Token = Tok_Mod then |
| 604 |
Word_Id := Name_Mod; |
| 605 |
Scan; -- past MOD |
| 606 |
|
| 607 |
elsif Token = Tok_Range then |
| 608 |
Word_Id := Name_Range; |
| 609 |
Scan; -- post RANGE |
| 610 |
end if; |
| 611 |
|
| 612 |
if Word_Id = No_Name then |
| 613 |
return P_Expression; |
| 614 |
else |
| 615 |
Word := New_Node (N_Identifier, Token_Ptr); |
| 616 |
Set_Chars (Word, Word_Id); |
| 617 |
return Word; |
| 618 |
end if; |
| 619 |
end P_Expression_Or_Reserved_Word; |
| 620 |
|
| 621 |
-- Local variables |
| 622 |
|
| 623 |
Expression_Node : Node_Id; |
| 624 |
Identifier_Node : Node_Id; |
| 625 |
Identifier_OK : Boolean; |
| 626 |
Scan_State : Saved_Scan_State; |
| 627 |
|
| 628 |
-- Start of processing for Scan_Pragma_Argument_Association |
| 629 |
|
| 630 |
begin |
| 631 |
Association := New_Node (N_Pragma_Argument_Association, Token_Ptr); |
| 632 |
Set_Chars (Association, No_Name); |
| 633 |
Identifier_OK := False; |
| 634 |
|
| 635 |
-- Argument starts with identifier |
| 636 |
|
| 637 |
if Token = Tok_Identifier then |
| 638 |
Identifier_Node := Token_Node; |
| 639 |
Save_Scan_State (Scan_State); -- at Identifier |
| 640 |
Scan; -- past Identifier |
| 641 |
|
| 642 |
if Token = Tok_Arrow then |
| 643 |
Scan; -- past arrow |
| 644 |
Identifier_OK := True; |
| 645 |
|
| 646 |
-- Case of one of the special aspect forms |
| 647 |
|
| 648 |
elsif Token = Tok_Apostrophe then |
| 649 |
Scan; -- past apostrophe |
| 650 |
|
| 651 |
-- We have apostrophe, so check for identifier'Class |
| 652 |
|
| 653 |
if Token /= Tok_Identifier or else Token_Name /= Name_Class then |
| 654 |
null; |
| 655 |
|
| 656 |
-- We have identifier'Class, check for arrow |
| 657 |
|
| 658 |
else |
| 659 |
Scan; -- Past Class |
| 660 |
|
| 661 |
if Token /= Tok_Arrow then |
| 662 |
null; |
| 663 |
|
| 664 |
-- Here we have scanned identifier'Class => |
| 665 |
|
| 666 |
else |
| 667 |
Identifier_OK := True; |
| 668 |
Scan; -- past arrow |
| 669 |
|
| 670 |
case Chars (Identifier_Node) is |
| 671 |
when Name_Pre => |
| 672 |
Set_Chars (Identifier_Node, Name_uPre); |
| 673 |
|
| 674 |
when Name_Post => |
| 675 |
Set_Chars (Identifier_Node, Name_uPost); |
| 676 |
|
| 677 |
when Name_Type_Invariant => |
| 678 |
Set_Chars (Identifier_Node, Name_uType_Invariant); |
| 679 |
|
| 680 |
when Name_Invariant => |
| 681 |
Set_Chars (Identifier_Node, Name_uInvariant); |
| 682 |
|
| 683 |
-- If it is X'Class => for some invalid X, we will give |
| 684 |
-- an error, and forget that 'Class was present, which |
| 685 |
-- will give better error recovery. We could do a spell |
| 686 |
-- check here, but it seems too much work. |
| 687 |
|
| 688 |
when others => |
| 689 |
Error_Msg_SC ("invalid aspect id for pragma"); |
| 690 |
end case; |
| 691 |
end if; |
| 692 |
end if; |
| 693 |
end if; |
| 694 |
|
| 695 |
-- Identifier was present |
| 696 |
|
| 697 |
if Identifier_OK then |
| 698 |
Set_Chars (Association, Chars (Identifier_Node)); |
| 699 |
Identifier_Seen := True; |
| 700 |
|
| 701 |
-- Identifier not present after all |
| 702 |
|
| 703 |
else |
| 704 |
Restore_Scan_State (Scan_State); -- to Identifier |
| 705 |
end if; |
| 706 |
end if; |
| 707 |
|
| 708 |
-- Diagnose error of "positional" argument for pragma appearing after |
| 709 |
-- a "named" argument (quotes here are because that's not quite accurate |
| 710 |
-- Ada RM terminology). |
| 711 |
|
| 712 |
-- Since older GNAT versions did not generate this error, disable this |
| 713 |
-- message in Relaxed_RM_Semantics mode to help legacy code using e.g. |
| 714 |
-- codepeer. |
| 715 |
|
| 716 |
if Identifier_Seen |
| 717 |
and not Identifier_OK |
| 718 |
and not Relaxed_RM_Semantics |
| 719 |
then |
| 720 |
Error_Msg_SC ("|pragma argument identifier required here"); |
| 721 |
Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))"); |
| 722 |
end if; |
| 723 |
|
| 724 |
if Identifier_OK then |
| 725 |
|
| 726 |
-- Certain pragmas such as Restriction_Warnings and Restrictions |
| 727 |
-- allow reserved words to appear as expressions when checking for |
| 728 |
-- prohibited uses of attributes. |
| 729 |
|
| 730 |
if Reserved_Words_OK |
| 731 |
and then Chars (Identifier_Node) = Name_No_Use_Of_Attribute |
| 732 |
then |
| 733 |
Expression_Node := P_Expression_Or_Reserved_Word; |
| 734 |
else |
| 735 |
Expression_Node := P_Expression; |
| 736 |
end if; |
| 737 |
else |
| 738 |
Expression_Node := P_Expression_If_OK; |
| 739 |
end if; |
| 740 |
|
| 741 |
Set_Expression (Association, Expression_Node); |
| 742 |
end Scan_Pragma_Argument_Association; |
| 743 |
|
| 744 |
end Ch2; |