compiler: don't generate stubs for ambiguous direct interface methods
[official-gcc.git] / gcc / ada / par-ch2.adb
blob060bb410978e5c7cbde7bba19a1aab836b3d85e4
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-2022, 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.7 Comment --
204 ------------------
206 -- A COMMENT starts with two adjacent hyphens and extends up to the
207 -- end of the line. A COMMENT may appear on any line of a program.
209 -- Handled by the scanner which simply skips past encountered comments
211 -----------------
212 -- 2.8 Pragma --
213 -----------------
215 -- PRAGMA ::= pragma IDENTIFIER
216 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
218 -- The caller has checked that the initial token is PRAGMA
220 -- Error recovery: cannot raise Error_Resync
222 -- One special piece of processing is needed in this routine. As described
223 -- in the section on "Handling semicolon used in place of IS" in module
224 -- Parse, the parser detects the case of missing subprogram bodies to
225 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
226 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
227 -- recognize the use of these two pragmas in this context, otherwise it
228 -- will think there are missing bodies, and try to change ; to IS, when
229 -- in fact the bodies ARE present, supplied by these pragmas.
231 function P_Pragma (Skipping : Boolean := False) return Node_Id is
232 procedure Skip_Pragma_Semicolon;
233 -- Skip past semicolon at end of pragma
235 ---------------------------
236 -- Skip_Pragma_Semicolon --
237 ---------------------------
239 procedure Skip_Pragma_Semicolon is
240 begin
241 -- If skipping the pragma, ignore a missing semicolon
243 if Token /= Tok_Semicolon and then Skipping then
244 null;
246 -- Otherwise demand a semicolon
248 else
249 T_Semicolon;
250 end if;
251 end Skip_Pragma_Semicolon;
253 -- Local variables
255 Import_Check_Required : Boolean := False;
256 -- Set True if check of pragma IMPORT or INTERFACE is required
258 Arg_Count : Nat := 0;
259 -- Number of argument associations processed
261 Identifier_Seen : Boolean := False;
262 -- Set True if an identifier is encountered for a pragma argument. Used
263 -- to check that there are no more arguments without identifiers.
265 Assoc_Node : Node_Id;
266 Ident_Node : Node_Id;
267 Prag_Name : Name_Id;
268 Prag_Node : Node_Id;
269 Result : Node_Id;
270 Semicolon_Loc : Source_Ptr;
272 -- Start of processing for P_Pragma
274 begin
275 Inside_Pragma := True;
276 Prag_Node := New_Node (N_Pragma, Token_Ptr);
277 Scan; -- past PRAGMA
278 Prag_Name := Token_Name;
280 if Style_Check then
281 Style.Check_Pragma_Name;
282 end if;
284 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
285 -- allowed as a pragma name.
287 if Is_Reserved_Keyword (Token) then
288 Prag_Name := Keyword_Name (Token);
289 Ident_Node := Make_Identifier (Token_Ptr, Prag_Name);
290 Scan; -- past the keyword
291 else
292 Ident_Node := P_Identifier;
293 end if;
295 Set_Pragma_Identifier (Prag_Node, Ident_Node);
297 -- See if special INTERFACE/IMPORT check is required
299 if SIS_Entry_Active then
300 Import_Check_Required :=
301 (Prag_Name = Name_Import) or else (Prag_Name = Name_Interface);
302 else
303 Import_Check_Required := False;
304 end if;
306 -- Set global to indicate if we are within a Depends pragma
308 if Chars (Ident_Node) = Name_Depends
309 or else Chars (Ident_Node) = Name_Refined_Depends
310 then
311 Inside_Depends := True;
312 end if;
314 -- Scan arguments. We assume that arguments are present if there is
315 -- a left paren, or if a semicolon is missing and there is another
316 -- token on the same line as the pragma name.
318 if Token = Tok_Left_Paren
319 or else (Token /= Tok_Semicolon
320 and then not Token_Is_At_Start_Of_Line)
321 then
322 Set_Pragma_Argument_Associations (Prag_Node, New_List);
323 T_Left_Paren;
325 loop
326 Arg_Count := Arg_Count + 1;
328 Scan_Pragma_Argument_Association
329 (Identifier_Seen => Identifier_Seen,
330 Association => Assoc_Node,
331 Reserved_Words_OK =>
332 Prag_Name in Name_Restriction_Warnings | Name_Restrictions);
334 if Arg_Count = 2 and then Import_Check_Required then
335 -- Here is where we cancel the SIS active status if this pragma
336 -- supplies a body for the currently active subprogram spec.
338 if Nkind (Expression (Assoc_Node)) in N_Direct_Name
339 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
340 then
341 SIS_Entry_Active := False;
342 end if;
343 end if;
345 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
346 exit when Token /= Tok_Comma;
347 Scan; -- past comma
348 end loop;
350 -- If we have := for pragma Debug, it is worth special casing the
351 -- error message (it is easy to think of pragma Debug as taking a
352 -- statement, and an assignment statement is the most likely
353 -- candidate for this error)
355 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
356 Error_Msg_SC ("argument for pragma Debug must be procedure call");
357 Resync_To_Semicolon;
359 -- Normal case, we expect a right paren here
361 else
362 T_Right_Paren;
363 end if;
364 end if;
366 Semicolon_Loc := Token_Ptr;
368 -- Cancel indication of being within a pragma or in particular a Depends
369 -- pragma.
371 Inside_Depends := False;
372 Inside_Pragma := False;
374 -- Now we have two tasks left, we need to scan out the semicolon
375 -- following the pragma, and we have to call Par.Prag to process
376 -- the pragma. Normally we do them in this order, however, there
377 -- is one exception namely pragma Style_Checks where we like to
378 -- skip the semicolon after processing the pragma, since that way
379 -- the style checks for the scanning of the semicolon follow the
380 -- settings of the pragma.
382 -- You might think we could just unconditionally do things in
383 -- the opposite order, but there are other pragmas, notably the
384 -- case of pragma Source_File_Name, which assume the semicolon
385 -- is already scanned out.
387 if Prag_Name = Name_Style_Checks then
388 Result := Par.Prag (Prag_Node, Semicolon_Loc);
389 Skip_Pragma_Semicolon;
390 return Result;
391 else
392 Skip_Pragma_Semicolon;
393 return Par.Prag (Prag_Node, Semicolon_Loc);
394 end if;
396 exception
397 when Error_Resync =>
398 Resync_Past_Semicolon;
399 Inside_Depends := False;
400 Inside_Pragma := False;
401 return Error;
402 end P_Pragma;
404 -- This routine is called if a pragma is encountered in an inappropriate
405 -- position, the pragma is scanned out and control returns to continue.
407 -- The caller has checked that the initial token is pragma
409 -- Error recovery: cannot raise Error_Resync
411 procedure P_Pragmas_Misplaced is
412 begin
413 while Token = Tok_Pragma loop
414 Error_Msg_SC ("pragma not allowed here");
415 Discard_Junk_Node (P_Pragma (Skipping => True));
416 end loop;
417 end P_Pragmas_Misplaced;
419 -- This function is called to scan out an optional sequence of pragmas.
420 -- If no pragmas are found, then No_List is returned.
422 -- Error recovery: Cannot raise Error_Resync
424 function P_Pragmas_Opt return List_Id is
425 L : List_Id;
427 begin
428 if Token = Tok_Pragma then
429 L := New_List;
430 P_Pragmas_Opt (L);
431 return L;
433 else
434 return No_List;
435 end if;
436 end P_Pragmas_Opt;
438 -- This procedure is called to scan out an optional sequence of pragmas.
439 -- Any pragmas found are appended to the list provided as an argument.
441 -- Error recovery: Cannot raise Error_Resync
443 procedure P_Pragmas_Opt (List : List_Id) is
444 P : Node_Id;
446 begin
447 while Token = Tok_Pragma loop
448 P := P_Pragma;
450 if Nkind (P) /= N_Error
451 and then Pragma_Name_Unmapped (P) in Name_Assert | Name_Debug
452 then
453 Error_Msg_Name_1 := Pragma_Name_Unmapped (P);
454 Error_Msg_N
455 ("pragma% must be in declaration/statement context", P);
456 else
457 Append (P, List);
458 end if;
459 end loop;
460 end P_Pragmas_Opt;
462 --------------------------------------
463 -- 2.8 Pragma_Argument Association --
464 --------------------------------------
466 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
467 -- [pragma_argument_IDENTIFIER =>] NAME
468 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
470 -- In Ada 2012, there are two more possibilities:
472 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
473 -- [pragma_argument_ASPECT_MARK =>] NAME
474 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION
476 -- where the interesting allowed cases (which do not fit the syntax of the
477 -- first alternative above) are
479 -- ASPECT_MARK ::=
480 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class
482 -- We allow this special usage in all Ada modes, but it would be a pain to
483 -- allow these aspects to pervade the pragma syntax, and the representation
484 -- of pragma nodes internally. So what we do is to replace these
485 -- ASPECT_MARK forms with identifiers whose name is one of the special
486 -- internal names _Pre, _Post, _Invariant, or _Type_Invariant.
488 -- Error recovery: cannot raise Error_Resync
490 procedure Scan_Pragma_Argument_Association
491 (Identifier_Seen : in out Boolean;
492 Association : out Node_Id;
493 Reserved_Words_OK : Boolean := False)
495 function P_Expression_Or_Reserved_Word return Node_Id;
496 -- Parse an expression or, if the token is one of the following reserved
497 -- words, construct an identifier with proper Chars field.
498 -- Access
499 -- Delta
500 -- Digits
501 -- Mod
502 -- Range
504 -----------------------------------
505 -- P_Expression_Or_Reserved_Word --
506 -----------------------------------
508 function P_Expression_Or_Reserved_Word return Node_Id is
509 Word : Node_Id;
510 Word_Id : Name_Id;
512 begin
513 Word_Id := No_Name;
515 if Token = Tok_Access then
516 Word_Id := Name_Access;
517 Scan; -- past ACCESS
519 elsif Token = Tok_Delta then
520 Word_Id := Name_Delta;
521 Scan; -- past DELTA
523 elsif Token = Tok_Digits then
524 Word_Id := Name_Digits;
525 Scan; -- past DIGITS
527 elsif Token = Tok_Mod then
528 Word_Id := Name_Mod;
529 Scan; -- past MOD
531 elsif Token = Tok_Range then
532 Word_Id := Name_Range;
533 Scan; -- post RANGE
534 end if;
536 if Word_Id = No_Name then
537 return P_Expression;
538 else
539 Word := New_Node (N_Identifier, Token_Ptr);
540 Set_Chars (Word, Word_Id);
541 return Word;
542 end if;
543 end P_Expression_Or_Reserved_Word;
545 -- Local variables
547 Expression_Node : Node_Id;
548 Identifier_Node : Node_Id;
549 Identifier_OK : Boolean;
550 Scan_State : Saved_Scan_State;
552 -- Start of processing for Scan_Pragma_Argument_Association
554 begin
555 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
556 Set_Chars (Association, No_Name);
557 Identifier_OK := False;
559 -- Argument starts with identifier
561 if Token = Tok_Identifier then
562 Identifier_Node := Token_Node;
563 Save_Scan_State (Scan_State); -- at Identifier
564 Scan; -- past Identifier
566 if Token = Tok_Arrow then
567 Scan; -- past arrow
568 Identifier_OK := True;
570 -- Case of one of the special aspect forms
572 elsif Token = Tok_Apostrophe then
573 Scan; -- past apostrophe
575 -- We have apostrophe, so check for identifier'Class
577 if Token /= Tok_Identifier or else Token_Name /= Name_Class then
578 null;
580 -- We have identifier'Class, check for arrow
582 else
583 Scan; -- Past Class
585 if Token /= Tok_Arrow then
586 null;
588 -- Here we have scanned identifier'Class =>
590 else
591 Identifier_OK := True;
592 Scan; -- past arrow
594 case Chars (Identifier_Node) is
595 when Name_Pre =>
596 Set_Chars (Identifier_Node, Name_uPre);
598 when Name_Post =>
599 Set_Chars (Identifier_Node, Name_uPost);
601 when Name_Type_Invariant =>
602 Set_Chars (Identifier_Node, Name_uType_Invariant);
604 when Name_Invariant =>
605 Set_Chars (Identifier_Node, Name_uInvariant);
607 -- If it is X'Class => for some invalid X, we will give
608 -- an error, and forget that 'Class was present, which
609 -- will give better error recovery. We could do a spell
610 -- check here, but it seems too much work.
612 when others =>
613 Error_Msg_SC ("invalid aspect id for pragma");
614 end case;
615 end if;
616 end if;
617 end if;
619 -- Identifier was present
621 if Identifier_OK then
622 Set_Chars (Association, Chars (Identifier_Node));
623 Identifier_Seen := True;
625 -- Identifier not present after all
627 else
628 Restore_Scan_State (Scan_State); -- to Identifier
629 end if;
630 end if;
632 -- Diagnose error of "positional" argument for pragma appearing after
633 -- a "named" argument (quotes here are because that's not quite accurate
634 -- Ada RM terminology).
636 -- Since older GNAT versions did not generate this error, disable this
637 -- message in Relaxed_RM_Semantics mode to help legacy code using e.g.
638 -- codepeer.
640 if Identifier_Seen
641 and not Identifier_OK
642 and not Relaxed_RM_Semantics
643 then
644 Error_Msg_SC ("|pragma argument identifier required here");
645 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
646 end if;
648 if Identifier_OK then
650 -- Certain pragmas such as Restriction_Warnings and Restrictions
651 -- allow reserved words to appear as expressions when checking for
652 -- prohibited uses of attributes.
654 if Reserved_Words_OK
655 and then Chars (Identifier_Node) = Name_No_Use_Of_Attribute
656 then
657 Expression_Node := P_Expression_Or_Reserved_Word;
658 else
659 Expression_Node := P_Expression;
660 end if;
661 else
662 Expression_Node := P_Expression_If_OK;
663 end if;
665 Set_Expression (Association, Expression_Node);
666 end Scan_Pragma_Argument_Association;
668 end Ch2;