ada: Rename Is_Constr_Subt_For_UN_Aliased flag
[official-gcc.git] / gcc / ada / par-ch2.adb
blobaf92f5ac3536e30b1c0ab38a23b29f24bd022383
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 Saved_State : constant Boolean := Inside_Interpolated_String_Literal;
229 String_Node : Node_Id;
231 begin
232 String_Node := New_Node (N_Interpolated_String_Literal, Token_Ptr);
233 Inside_Interpolated_String_Literal := True;
235 Scan; -- past 'f'
237 if Token /= Tok_String_Literal then
238 Error_Msg_SC ("string literal expected");
240 else
241 Append_To (Elements_List, Token_Node);
242 Scan; -- past string_literal
244 while Token in Tok_Left_Curly_Bracket | Tok_String_Literal loop
246 -- Interpolated expression
248 if Token = Tok_Left_Curly_Bracket then
249 declare
250 Saved_In_Expr : constant Boolean :=
251 Inside_Interpolated_String_Expression;
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_String_Literal then
262 NL_Node := New_Node (N_String_Literal, Token_Ptr);
263 Set_Has_Wide_Character (NL_Node, False);
264 Set_Has_Wide_Wide_Character (NL_Node, False);
266 Start_String;
267 Store_String_Char (Get_Char_Code (ASCII.LF));
268 Set_Strval (NL_Node, End_String);
269 Append_To (Elements_List, NL_Node);
270 end if;
272 Append_To (Elements_List, Token_Node);
273 Scan; -- past string_literal
274 end if;
275 end loop;
276 end if;
278 Inside_Interpolated_String_Literal := Saved_State;
279 Set_Expressions (String_Node, Elements_List);
281 return String_Node;
282 end P_Interpolated_String_Literal;
284 ------------------
285 -- 2.7 Comment --
286 ------------------
288 -- A COMMENT starts with two adjacent hyphens and extends up to the
289 -- end of the line. A COMMENT may appear on any line of a program.
291 -- Handled by the scanner which simply skips past encountered comments
293 -----------------
294 -- 2.8 Pragma --
295 -----------------
297 -- PRAGMA ::= pragma IDENTIFIER
298 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
300 -- The caller has checked that the initial token is PRAGMA
302 -- Error recovery: cannot raise Error_Resync
304 -- One special piece of processing is needed in this routine. As described
305 -- in the section on "Handling semicolon used in place of IS" in module
306 -- Parse, the parser detects the case of missing subprogram bodies to
307 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
308 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
309 -- recognize the use of these two pragmas in this context, otherwise it
310 -- will think there are missing bodies, and try to change ; to IS, when
311 -- in fact the bodies ARE present, supplied by these pragmas.
313 function P_Pragma (Skipping : Boolean := False) return Node_Id is
314 procedure Skip_Pragma_Semicolon;
315 -- Skip past semicolon at end of pragma
317 ---------------------------
318 -- Skip_Pragma_Semicolon --
319 ---------------------------
321 procedure Skip_Pragma_Semicolon is
322 begin
323 -- If skipping the pragma, ignore a missing semicolon
325 if Token /= Tok_Semicolon and then Skipping then
326 null;
328 -- Otherwise demand a semicolon
330 else
331 T_Semicolon;
332 end if;
333 end Skip_Pragma_Semicolon;
335 -- Local variables
337 Import_Check_Required : Boolean := False;
338 -- Set True if check of pragma IMPORT or INTERFACE is required
340 Arg_Count : Nat := 0;
341 -- Number of argument associations processed
343 Identifier_Seen : Boolean := False;
344 -- Set True if an identifier is encountered for a pragma argument. Used
345 -- to check that there are no more arguments without identifiers.
347 Assoc_Node : Node_Id;
348 Ident_Node : Node_Id;
349 Prag_Name : Name_Id;
350 Prag_Node : Node_Id;
351 Result : Node_Id;
352 Semicolon_Loc : Source_Ptr;
354 -- Start of processing for P_Pragma
356 begin
357 Inside_Pragma := True;
358 Prag_Node := New_Node (N_Pragma, Token_Ptr);
359 Scan; -- past PRAGMA
360 Prag_Name := Token_Name;
362 if Style_Check then
363 Style.Check_Pragma_Name;
364 end if;
366 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
367 -- allowed as a pragma name.
369 if Is_Reserved_Keyword (Token) then
370 Prag_Name := Keyword_Name (Token);
371 Ident_Node := Make_Identifier (Token_Ptr, Prag_Name);
372 Scan; -- past the keyword
373 else
374 Ident_Node := P_Identifier;
375 end if;
377 Set_Pragma_Identifier (Prag_Node, Ident_Node);
379 -- See if special INTERFACE/IMPORT check is required
381 if SIS_Entry_Active then
382 Import_Check_Required :=
383 Prag_Name = Name_Import or else Prag_Name = Name_Interface;
384 else
385 Import_Check_Required := False;
386 end if;
388 -- Set global to indicate if we are within a Depends pragma
390 if Chars (Ident_Node) = Name_Depends
391 or else Chars (Ident_Node) = Name_Refined_Depends
392 then
393 Inside_Depends := True;
394 end if;
396 -- Scan arguments. We assume that arguments are present if there is
397 -- a left paren, or if a semicolon is missing and there is another
398 -- token on the same line as the pragma name.
400 if Token = Tok_Left_Paren
401 or else (Token /= Tok_Semicolon
402 and then not Token_Is_At_Start_Of_Line)
403 then
404 Set_Pragma_Argument_Associations (Prag_Node, New_List);
405 T_Left_Paren;
407 loop
408 Arg_Count := Arg_Count + 1;
410 Scan_Pragma_Argument_Association
411 (Identifier_Seen => Identifier_Seen,
412 Association => Assoc_Node,
413 Reserved_Words_OK =>
414 Prag_Name in Name_Restriction_Warnings | Name_Restrictions);
416 if Arg_Count = 2 and then Import_Check_Required then
417 -- Here is where we cancel the SIS active status if this pragma
418 -- supplies a body for the currently active subprogram spec.
420 if Nkind (Expression (Assoc_Node)) in N_Direct_Name
421 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
422 then
423 SIS_Entry_Active := False;
424 end if;
425 end if;
427 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
428 exit when Token /= Tok_Comma;
429 Scan; -- past comma
430 end loop;
432 -- If we have := for pragma Debug, it is worth special casing the
433 -- error message (it is easy to think of pragma Debug as taking a
434 -- statement, and an assignment statement is the most likely
435 -- candidate for this error)
437 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
438 Error_Msg_SC ("argument for pragma Debug must be procedure call");
439 Resync_To_Semicolon;
441 -- Normal case, we expect a right paren here
443 else
444 T_Right_Paren;
445 end if;
446 end if;
448 Semicolon_Loc := Token_Ptr;
450 -- Cancel indication of being within a pragma or in particular a Depends
451 -- pragma.
453 Inside_Depends := False;
454 Inside_Pragma := False;
456 -- Now we have two tasks left, we need to scan out the semicolon
457 -- following the pragma, and we have to call Par.Prag to process
458 -- the pragma. Normally we do them in this order, however, there
459 -- is one exception namely pragma Style_Checks where we like to
460 -- skip the semicolon after processing the pragma, since that way
461 -- the style checks for the scanning of the semicolon follow the
462 -- settings of the pragma.
464 -- You might think we could just unconditionally do things in
465 -- the opposite order, but there are other pragmas, notably the
466 -- case of pragma Source_File_Name, which assume the semicolon
467 -- is already scanned out.
469 if Prag_Name = Name_Style_Checks then
470 Result := Par.Prag (Prag_Node, Semicolon_Loc);
471 Skip_Pragma_Semicolon;
472 return Result;
473 else
474 Skip_Pragma_Semicolon;
475 return Par.Prag (Prag_Node, Semicolon_Loc);
476 end if;
478 exception
479 when Error_Resync =>
480 Resync_Past_Semicolon;
481 Inside_Depends := False;
482 Inside_Pragma := False;
483 return Error;
484 end P_Pragma;
486 -- This routine is called if a pragma is encountered in an inappropriate
487 -- position, the pragma is scanned out and control returns to continue.
489 -- The caller has checked that the initial token is pragma
491 -- Error recovery: cannot raise Error_Resync
493 procedure P_Pragmas_Misplaced is
494 begin
495 while Token = Tok_Pragma loop
496 Error_Msg_SC ("pragma not allowed here");
497 Discard_Junk_Node (P_Pragma (Skipping => True));
498 end loop;
499 end P_Pragmas_Misplaced;
501 -- This function is called to scan out an optional sequence of pragmas.
502 -- If no pragmas are found, then No_List is returned.
504 -- Error recovery: Cannot raise Error_Resync
506 function P_Pragmas_Opt return List_Id is
507 L : List_Id;
509 begin
510 if Token = Tok_Pragma then
511 L := New_List;
512 P_Pragmas_Opt (L);
513 return L;
515 else
516 return No_List;
517 end if;
518 end P_Pragmas_Opt;
520 -- This procedure is called to scan out an optional sequence of pragmas.
521 -- Any pragmas found are appended to the list provided as an argument.
523 -- Error recovery: Cannot raise Error_Resync
525 procedure P_Pragmas_Opt (List : List_Id) is
526 P : Node_Id;
528 begin
529 while Token = Tok_Pragma loop
530 P := P_Pragma;
532 if Nkind (P) /= N_Error
533 and then Pragma_Name_Unmapped (P) in Name_Assert | Name_Debug
534 then
535 Error_Msg_Name_1 := Pragma_Name_Unmapped (P);
536 Error_Msg_N
537 ("pragma% must be in declaration/statement context", P);
538 else
539 Append (P, List);
540 end if;
541 end loop;
542 end P_Pragmas_Opt;
544 --------------------------------------
545 -- 2.8 Pragma_Argument Association --
546 --------------------------------------
548 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
549 -- [pragma_argument_IDENTIFIER =>] NAME
550 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
552 -- In Ada 2012, there are two more possibilities:
554 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
555 -- [pragma_argument_ASPECT_MARK =>] NAME
556 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION
558 -- where the interesting allowed cases (which do not fit the syntax of the
559 -- first alternative above) are
561 -- ASPECT_MARK ::=
562 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class
564 -- We allow this special usage in all Ada modes, but it would be a pain to
565 -- allow these aspects to pervade the pragma syntax, and the representation
566 -- of pragma nodes internally. So what we do is to replace these
567 -- ASPECT_MARK forms with identifiers whose name is one of the special
568 -- internal names _Pre, _Post, _Invariant, or _Type_Invariant.
570 -- Error recovery: cannot raise Error_Resync
572 procedure Scan_Pragma_Argument_Association
573 (Identifier_Seen : in out Boolean;
574 Association : out Node_Id;
575 Reserved_Words_OK : Boolean := False)
577 function P_Expression_Or_Reserved_Word return Node_Id;
578 -- Parse an expression or, if the token is one of the following reserved
579 -- words, construct an identifier with proper Chars field.
580 -- Access
581 -- Delta
582 -- Digits
583 -- Mod
584 -- Range
586 -----------------------------------
587 -- P_Expression_Or_Reserved_Word --
588 -----------------------------------
590 function P_Expression_Or_Reserved_Word return Node_Id is
591 Word : Node_Id;
592 Word_Id : Name_Id;
594 begin
595 Word_Id := No_Name;
597 if Token = Tok_Access then
598 Word_Id := Name_Access;
599 Scan; -- past ACCESS
601 elsif Token = Tok_Delta then
602 Word_Id := Name_Delta;
603 Scan; -- past DELTA
605 elsif Token = Tok_Digits then
606 Word_Id := Name_Digits;
607 Scan; -- past DIGITS
609 elsif Token = Tok_Mod then
610 Word_Id := Name_Mod;
611 Scan; -- past MOD
613 elsif Token = Tok_Range then
614 Word_Id := Name_Range;
615 Scan; -- post RANGE
616 end if;
618 if Word_Id = No_Name then
619 return P_Expression;
620 else
621 Word := New_Node (N_Identifier, Token_Ptr);
622 Set_Chars (Word, Word_Id);
623 return Word;
624 end if;
625 end P_Expression_Or_Reserved_Word;
627 -- Local variables
629 Expression_Node : Node_Id;
630 Identifier_Node : Node_Id;
631 Identifier_OK : Boolean;
632 Scan_State : Saved_Scan_State;
634 -- Start of processing for Scan_Pragma_Argument_Association
636 begin
637 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
638 Set_Chars (Association, No_Name);
639 Identifier_OK := False;
641 -- Argument starts with identifier
643 if Token = Tok_Identifier then
644 Identifier_Node := Token_Node;
645 Save_Scan_State (Scan_State); -- at Identifier
646 Scan; -- past Identifier
648 if Token = Tok_Arrow then
649 Scan; -- past arrow
650 Identifier_OK := True;
652 -- Case of one of the special aspect forms
654 elsif Token = Tok_Apostrophe then
655 Scan; -- past apostrophe
657 -- We have apostrophe, so check for identifier'Class
659 if Token /= Tok_Identifier or else Token_Name /= Name_Class then
660 null;
662 -- We have identifier'Class, check for arrow
664 else
665 Scan; -- Past Class
667 if Token /= Tok_Arrow then
668 null;
670 -- Here we have scanned identifier'Class =>
672 else
673 Identifier_OK := True;
674 Scan; -- past arrow
676 case Chars (Identifier_Node) is
677 when Name_Pre =>
678 Set_Chars (Identifier_Node, Name_uPre);
680 when Name_Post =>
681 Set_Chars (Identifier_Node, Name_uPost);
683 when Name_Type_Invariant =>
684 Set_Chars (Identifier_Node, Name_uType_Invariant);
686 when Name_Invariant =>
687 Set_Chars (Identifier_Node, Name_uInvariant);
689 -- If it is X'Class => for some invalid X, we will give
690 -- an error, and forget that 'Class was present, which
691 -- will give better error recovery. We could do a spell
692 -- check here, but it seems too much work.
694 when others =>
695 Error_Msg_SC ("invalid aspect id for pragma");
696 end case;
697 end if;
698 end if;
699 end if;
701 -- Identifier was present
703 if Identifier_OK then
704 Set_Chars (Association, Chars (Identifier_Node));
705 Identifier_Seen := True;
707 -- Identifier not present after all
709 else
710 Restore_Scan_State (Scan_State); -- to Identifier
711 end if;
712 end if;
714 -- Diagnose error of "positional" argument for pragma appearing after
715 -- a "named" argument (quotes here are because that's not quite accurate
716 -- Ada RM terminology).
718 -- Since older GNAT versions did not generate this error, disable this
719 -- message in Relaxed_RM_Semantics mode to help legacy code using e.g.
720 -- codepeer.
722 if Identifier_Seen
723 and not Identifier_OK
724 and not Relaxed_RM_Semantics
725 then
726 Error_Msg_SC ("|pragma argument identifier required here");
727 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
728 end if;
730 if Identifier_OK then
732 -- Certain pragmas such as Restriction_Warnings and Restrictions
733 -- allow reserved words to appear as expressions when checking for
734 -- prohibited uses of attributes.
736 if Reserved_Words_OK
737 and then Chars (Identifier_Node) = Name_No_Use_Of_Attribute
738 then
739 Expression_Node := P_Expression_Or_Reserved_Word;
740 else
741 Expression_Node := P_Expression;
742 end if;
743 else
744 Expression_Node := P_Expression_If_OK;
745 end if;
747 Set_Expression (Association, Expression_Node);
748 end Scan_Pragma_Argument_Association;
750 end Ch2;