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