Debian Codegraph @

gcc/ada/sem_type.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ T Y P E --
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 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Alloc;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Nlists; use Nlists;
35 with Errout; use Errout;
36 with Lib; use Lib;
37 with Namet; use Namet;
38 with Opt; use Opt;
39 with Output; use Output;
40 with Sem; use Sem;
41 with Sem_Aux; use Sem_Aux;
42 with Sem_Ch6; use Sem_Ch6;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch12; use Sem_Ch12;
45 with Sem_Disp; use Sem_Disp;
46 with Sem_Dist; use Sem_Dist;
47 with Sem_Util; use Sem_Util;
48 with Stand; use Stand;
49 with Sinfo; use Sinfo;
50 with Sinfo.Nodes; use Sinfo.Nodes;
51 with Sinfo.Utils; use Sinfo.Utils;
52 with Snames; use Snames;
53 with Table;
54 with Treepr; use Treepr;
55 with Uintp; use Uintp;
56
57 with GNAT.HTable; use GNAT.HTable;
58
59 package body Sem_Type is
60
61 ---------------------
62 -- Data Structures --
63 ---------------------
64
65 -- The following data structures establish a mapping between nodes and
66 -- their interpretations. An overloaded node has an entry in Interp_Map,
67 -- which in turn contains a pointer into the All_Interp array. The
68 -- interpretations of a given node are contiguous in All_Interp. Each set
69 -- of interpretations is terminated with the marker No_Interp.
70
71 -- Interp_Map All_Interp
72
73 -- +-----+ +--------+
74 -- | | --->|interp1 |
75 -- |_____| | |interp2 |
76 -- |index|---------| |nointerp|
77 -- |-----| | |
78 -- | | | |
79 -- +-----+ +--------+
80
81 -- This scheme does not currently reclaim interpretations. In principle,
82 -- after a unit is compiled, all overloadings have been resolved, and the
83 -- candidate interpretations should be deleted. This should be easier
84 -- now than with the previous scheme???
85
86 package All_Interp is new Table.Table (
87 Table_Component_Type => Interp,
88 Table_Index_Type => Interp_Index,
89 Table_Low_Bound => 0,
90 Table_Initial => Alloc.All_Interp_Initial,
91 Table_Increment => Alloc.All_Interp_Increment,
92 Table_Name => "All_Interp");
93
94 Header_Max : constant := 3079;
95 -- The number of hash buckets; an arbitrary prime number
96
97 subtype Header_Num is Integer range 0 .. Header_Max - 1;
98
99 function Hash (N : Node_Id) return Header_Num;
100 -- A trivial hashing function for nodes, used to insert an overloaded
101 -- node into the Interp_Map table.
102
103 package Interp_Map is new Simple_HTable
104 (Header_Num => Header_Num,
105 Element => Interp_Index,
106 No_Element => -1,
107 Key => Node_Id,
108 Hash => Hash,
109 Equal => "=");
110
111 Last_Overloaded : Node_Id := Empty;
112 -- Overloaded node after initializing a new collection of interpretations
113
114 -------------------------------------
115 -- Handling of Overload Resolution --
116 -------------------------------------
117
118 -- Overload resolution uses two passes over the syntax tree of a complete
119 -- context. In the first, bottom-up pass, the types of actuals in calls
120 -- are used to resolve possibly overloaded subprogram and operator names.
121 -- In the second top-down pass, the type of the context (for example the
122 -- condition in a while statement) is used to resolve a possibly ambiguous
123 -- call, and the unique subprogram name in turn imposes a specific context
124 -- on each of its actuals.
125
126 -- Most expressions are in fact unambiguous, and the bottom-up pass is
127 -- sufficient to resolve most everything. To simplify the common case,
128 -- names and expressions carry a flag Is_Overloaded to indicate whether
129 -- they have more than one interpretation. If the flag is off, then each
130 -- name has already a unique meaning and type, and the bottom-up pass is
131 -- sufficient (and much simpler).
132
133 --------------------------
134 -- Operator Overloading --
135 --------------------------
136
137 -- The visibility of operators is handled differently from that of other
138 -- entities. We do not introduce explicit versions of primitive operators
139 -- for each type definition. As a result, there is only one entity
140 -- corresponding to predefined addition on all numeric types, etc. The
141 -- back end resolves predefined operators according to their type. The
142 -- visibility of primitive operations then reduces to the visibility of the
143 -- resulting type: (a + b) is a legal interpretation of some primitive
144 -- operator + if the type of the result (which must also be the type of a
145 -- and b) is directly visible (either immediately visible or use-visible).
146
147 -- User-defined operators are treated like other functions, but the
148 -- visibility of these user-defined operations must be special-cased
149 -- to determine whether they hide or are hidden by predefined operators.
150 -- The form P."+" (x, y) requires additional handling.
151
152 -- Concatenation is treated more conventionally: for every one-dimensional
153 -- array type we introduce a explicit concatenation operator. This is
154 -- necessary to handle the case of (element & element => array) which
155 -- cannot be handled conveniently if there is no explicit instance of
156 -- resulting type of the operation.
157
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
161
162 procedure All_Overloads;
163 pragma Warnings (Off, All_Overloads);
164 -- Debugging procedure: list full contents of Overloads table
165
166 function Binary_Op_Interp_Has_Abstract_Op
167 (N : Node_Id;
168 E : Entity_Id) return Entity_Id;
169 -- Given the node and entity of a binary operator, determine whether the
170 -- actuals of E contain an abstract interpretation with regards to the
171 -- types of their corresponding formals. Return the abstract operation or
172 -- Empty.
173
174 function Function_Interp_Has_Abstract_Op
175 (N : Node_Id;
176 E : Entity_Id) return Entity_Id;
177 -- Given the node and entity of a function call, determine whether the
178 -- actuals of E contain an abstract interpretation with regards to the
179 -- types of their corresponding formals. Return the abstract operation or
180 -- Empty.
181
182 function Has_Abstract_Op
183 (N : Node_Id;
184 Typ : Entity_Id) return Entity_Id;
185 -- Subsidiary routine to Binary_Op_Interp_Has_Abstract_Op and Function_
186 -- Interp_Has_Abstract_Op. Determine whether an overloaded node has an
187 -- abstract interpretation which yields type Typ.
188
189 procedure New_Interps (N : Node_Id);
190 -- Initialize collection of interpretations for the given node, which is
191 -- either an overloaded entity, or an operation whose arguments have
192 -- multiple interpretations. Interpretations can be added to only one
193 -- node at a time.
194
195 --------------------
196 -- Add_One_Interp --
197 --------------------
198
199 procedure Add_One_Interp
200 (N : Node_Id;
201 E : Entity_Id;
202 T : Entity_Id;
203 Opnd_Typ : Entity_Id := Empty)
204 is
205 Vis_Type : Entity_Id;
206
207 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id);
208 -- Add one interpretation to an overloaded node. Add a new entry if
209 -- not hidden by previous one, and remove previous one if hidden by
210 -- new one.
211
212 function Is_Universal_Operation (Op : Entity_Id) return Boolean;
213 -- True if the entity is a predefined operator and the operands have
214 -- a universal Interpretation.
215
216 ---------------
217 -- Add_Entry --
218 ---------------
219
220 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id) is
221 Abstr_Op : Entity_Id := Empty;
222 I : Interp_Index;
223 It : Interp;
224
225 -- Start of processing for Add_Entry
226
227 begin
228 -- Find out whether the new entry references interpretations that
229 -- are abstract or disabled by abstract operators.
230
231 if Ada_Version >= Ada_2005 then
232 if Nkind (N) in N_Binary_Op then
233 Abstr_Op := Binary_Op_Interp_Has_Abstract_Op (N, Name);
234 elsif Nkind (N) = N_Function_Call
235 and then Ekind (Name) = E_Function
236 then
237 Abstr_Op := Function_Interp_Has_Abstract_Op (N, Name);
238 end if;
239 end if;
240
241 Get_First_Interp (N, I, It);
242 while Present (It.Nam) loop
243
244 -- Avoid making duplicate entries in overloads
245
246 if Name = It.Nam
247 and then Base_Type (It.Typ) = Base_Type (T)
248 then
249 return;
250
251 -- A user-defined subprogram hides another declared at an outer
252 -- level, or one that is use-visible. So return if previous
253 -- definition hides new one (which is either in an outer
254 -- scope, or use-visible). Note that for functions use-visible
255 -- is the same as potentially use-visible. If new one hides
256 -- previous one, replace entry in table of interpretations.
257 -- If this is a universal operation, retain the operator in case
258 -- preference rule applies.
259
260 elsif ((Ekind (Name) in E_Function | E_Procedure
261 and then Ekind (Name) = Ekind (It.Nam))
262 or else (Ekind (Name) = E_Operator
263 and then Ekind (It.Nam) = E_Function))
264 and then Is_Immediately_Visible (It.Nam)
265 and then Type_Conformant (Name, It.Nam)
266 and then Base_Type (It.Typ) = Base_Type (T)
267 then
268 if Is_Universal_Operation (Name) then
269 exit;
270
271 -- If node is an operator symbol, we have no actuals with
272 -- which to check hiding, and this is done in full in the
273 -- caller (Analyze_Subprogram_Renaming) so we include the
274 -- predefined operator in any case.
275
276 elsif Nkind (N) = N_Operator_Symbol
277 or else
278 (Nkind (N) = N_Expanded_Name
279 and then Nkind (Selector_Name (N)) = N_Operator_Symbol)
280 then
281 exit;
282
283 elsif not In_Open_Scopes (Scope (Name))
284 or else Scope_Depth (Scope (Name)) <=
285 Scope_Depth (Scope (It.Nam))
286 then
287 -- If ambiguity within instance, and entity is not an
288 -- implicit operation, save for later disambiguation.
289
290 if Scope (Name) = Scope (It.Nam)
291 and then not Is_Inherited_Operation (Name)
292 and then In_Instance
293 then
294 exit;
295 else
296 return;
297 end if;
298
299 else
300 All_Interp.Table (I).Nam := Name;
301 return;
302 end if;
303
304 -- Otherwise keep going
305
306 else
307 Get_Next_Interp (I, It);
308 end if;
309 end loop;
310
311 All_Interp.Table (All_Interp.Last) := (Name, Typ, Opnd_Typ, Abstr_Op);
312 All_Interp.Append (No_Interp);
313 end Add_Entry;
314
315 ----------------------------
316 -- Is_Universal_Operation --
317 ----------------------------
318
319 function Is_Universal_Operation (Op : Entity_Id) return Boolean is
320 Arg : Node_Id;
321
322 begin
323 if Ekind (Op) /= E_Operator then
324 return False;
325
326 elsif Nkind (N) in N_Binary_Op then
327 if Present (Universal_Interpretation (Left_Opnd (N)))
328 and then Present (Universal_Interpretation (Right_Opnd (N)))
329 then
330 return True;
331 elsif Nkind (N) in N_Op_Eq | N_Op_Ne
332 and then
333 (Is_Anonymous_Access_Type (Etype (Left_Opnd (N)))
334 or else Is_Anonymous_Access_Type (Etype (Right_Opnd (N))))
335 then
336 return True;
337 else
338 return False;
339 end if;
340
341 elsif Nkind (N) in N_Unary_Op then
342 return Present (Universal_Interpretation (Right_Opnd (N)));
343
344 elsif Nkind (N) = N_Function_Call then
345 Arg := First_Actual (N);
346 while Present (Arg) loop
347 if No (Universal_Interpretation (Arg)) then
348 return False;
349 end if;
350
351 Next_Actual (Arg);
352 end loop;
353
354 return True;
355
356 else
357 return False;
358 end if;
359 end Is_Universal_Operation;
360
361 -- Start of processing for Add_One_Interp
362
363 begin
364 -- If the interpretation is a predefined operator, verify that it is
365 -- visible, or that the entity has already been resolved (case of an
366 -- instantiation node that refers to a predefined operation, or an
367 -- internally generated operator node, or an operator given as an
368 -- expanded name). If the operator is a comparison or equality, then
369 -- it is the type of the operand that is relevant here.
370
371 if Ekind (E) = E_Operator then
372 if Present (Opnd_Typ) then
373 Vis_Type := Opnd_Typ;
374 else
375 Vis_Type := Base_Type (T);
376 end if;
377
378 if Nkind (N) = N_Expanded_Name
379 or else (Nkind (N) in N_Op and then E = Entity (N))
380 or else Is_Visible_Operator (N, Vis_Type)
381 then
382 null;
383
384 -- Save type for subsequent error message, in case no other
385 -- interpretation is found.
386
387 else
388 Candidate_Type := Vis_Type;
389 return;
390 end if;
391
392 -- In an instance, an abstract non-dispatching operation cannot be a
393 -- candidate interpretation, because it could not have been one in the
394 -- generic (it may be a spurious overloading in the instance).
395
396 elsif In_Instance
397 and then Is_Overloadable (E)
398 and then Is_Abstract_Subprogram (E)
399 and then not Is_Dispatching_Operation (E)
400 then
401 return;
402
403 -- An inherited interface operation that is implemented by some derived
404 -- type does not participate in overload resolution, only the
405 -- implementation operation does.
406
407 elsif Is_Hidden (E)
408 and then Is_Subprogram (E)
409 and then Present (Interface_Alias (E))
410 then
411 -- Ada 2005 (AI-251): If this primitive operation corresponds with
412 -- an immediate ancestor interface there is no need to add it to the
413 -- list of interpretations. The corresponding aliased primitive is
414 -- also in this list of primitive operations and will be used instead
415 -- because otherwise we have a dummy ambiguity between the two
416 -- subprograms which are in fact the same.
417
418 if not Is_Ancestor
419 (Find_Dispatching_Type (Interface_Alias (E)),
420 Find_Dispatching_Type (E))
421 then
422 Add_One_Interp (N, Interface_Alias (E), T);
423
424 -- Otherwise this is the first interpretation, N has type Any_Type
425 -- and we must place the new type on the node.
426
427 else
428 Set_Etype (N, T);
429 end if;
430
431 return;
432
433 -- Calling stubs for an RACW operation never participate in resolution,
434 -- they are executed only through dispatching calls.
435
436 elsif Is_RACW_Stub_Type_Operation (E) then
437 return;
438 end if;
439
440 -- If this is the first interpretation of N, N has type Any_Type.
441 -- In that case place the new type on the node. If one interpretation
442 -- already exists, indicate that the node is overloaded, and store
443 -- both the previous and the new interpretation in All_Interp. If
444 -- this is a later interpretation, just add it to the set.
445
446 if Etype (N) = Any_Type then
447 if Is_Type (E) then
448 Set_Etype (N, T);
449
450 else
451 -- Record both the operator or subprogram name, and its type
452
453 if Nkind (N) in N_Op or else Is_Entity_Name (N) then
454 Set_Entity (N, E);
455 end if;
456
457 Set_Etype (N, T);
458 end if;
459
460 -- Either there is no current interpretation in the table for any
461 -- node or the interpretation that is present is for a different
462 -- node. In both cases add a new interpretation to the table.
463
464 elsif No (Last_Overloaded)
465 or else
466 (Last_Overloaded /= N
467 and then not Is_Overloaded (N))
468 then
469 New_Interps (N);
470
471 if (Nkind (N) in N_Op or else Is_Entity_Name (N))
472 and then Present (Entity (N))
473 then
474 Add_Entry (Entity (N), Etype (N));
475
476 elsif Nkind (N) in N_Subprogram_Call
477 and then Is_Entity_Name (Name (N))
478 then
479 Add_Entry (Entity (Name (N)), Etype (N));
480
481 -- If this is an indirect call there will be no name associated
482 -- with the previous entry. To make diagnostics clearer, save
483 -- Subprogram_Type of first interpretation, so that the error will
484 -- point to the anonymous access to subprogram, not to the result
485 -- type of the call itself.
486
487 elsif (Nkind (N)) = N_Function_Call
488 and then Nkind (Name (N)) = N_Explicit_Dereference
489 and then Is_Overloaded (Name (N))
490 then
491 declare
492 It : Interp;
493
494 Itn : Interp_Index;
495 pragma Warnings (Off, Itn);
496
497 begin
498 Get_First_Interp (Name (N), Itn, It);
499 Add_Entry (It.Nam, Etype (N));
500 end;
501
502 else
503 -- Overloaded prefix in indexed or selected component, or call
504 -- whose name is an expression or another call.
505
506 Add_Entry (Etype (N), Etype (N));
507 end if;
508
509 Add_Entry (E, T);
510
511 else
512 Add_Entry (E, T);
513 end if;
514 end Add_One_Interp;
515
516 -------------------
517 -- All_Overloads --
518 -------------------
519
520 procedure All_Overloads is
521 begin
522 for J in All_Interp.First .. All_Interp.Last loop
523
524 if Present (All_Interp.Table (J).Nam) then
525 Write_Entity_Info (All_Interp.Table (J). Nam, " ");
526 else
527 Write_Str ("No Interp");
528 Write_Eol;
529 end if;
530
531 Write_Str ("=================");
532 Write_Eol;
533 end loop;
534 end All_Overloads;
535
536 --------------------------------------
537 -- Binary_Op_Interp_Has_Abstract_Op --
538 --------------------------------------
539
540 function Binary_Op_Interp_Has_Abstract_Op
541 (N : Node_Id;
542 E : Entity_Id) return Entity_Id
543 is
544 Abstr_Op : Entity_Id;
545 E_Left : constant Node_Id := First_Formal (E);
546 E_Right : constant Node_Id := Next_Formal (E_Left);
547
548 begin
549 Abstr_Op := Has_Abstract_Op (Left_Opnd (N), Etype (E_Left));
550 if Present (Abstr_Op) then
551 return Abstr_Op;
552 end if;
553
554 return Has_Abstract_Op (Right_Opnd (N), Etype (E_Right));
555 end Binary_Op_Interp_Has_Abstract_Op;
556
557 ---------------------
558 -- Collect_Interps --
559 ---------------------
560
561 procedure Collect_Interps (N : Node_Id) is
562 Ent : constant Entity_Id := Entity (N);
563 H : Entity_Id;
564 First_Interp : Interp_Index;
565
566 function Within_Instance (E : Entity_Id) return Boolean;
567 -- Within an instance there can be spurious ambiguities between a local
568 -- entity and one declared outside of the instance. This can only happen
569 -- for subprograms, because otherwise the local entity hides the outer
570 -- one. For an overloadable entity, this predicate determines whether it
571 -- is a candidate within the instance, or must be ignored.
572
573 ---------------------
574 -- Within_Instance --
575 ---------------------
576
577 function Within_Instance (E : Entity_Id) return Boolean is
578 Inst : Entity_Id;
579 Scop : Entity_Id;
580
581 begin
582 if not In_Instance then
583 return False;
584 end if;
585
586 Inst := Current_Scope;
587 while Present (Inst) and then not Is_Generic_Instance (Inst) loop
588 Inst := Scope (Inst);
589 end loop;
590
591 Scop := Scope (E);
592 while Present (Scop) and then Scop /= Standard_Standard loop
593 if Scop = Inst then
594 return True;
595 end if;
596
597 Scop := Scope (Scop);
598 end loop;
599
600 return False;
601 end Within_Instance;
602
603 -- Start of processing for Collect_Interps
604
605 begin
606 New_Interps (N);
607
608 -- Unconditionally add the entity that was initially matched
609
610 First_Interp := All_Interp.Last;
611 Add_One_Interp (N, Ent, Etype (N));
612
613 -- For expanded name, pick up all additional entities from the
614 -- same scope, since these are obviously also visible. Note that
615 -- these are not necessarily contiguous on the homonym chain.
616
617 if Nkind (N) = N_Expanded_Name then
618 H := Homonym (Ent);
619 while Present (H) loop
620 if Scope (H) = Scope (Entity (N)) then
621 Add_One_Interp (N, H, Etype (H));
622 end if;
623
624 H := Homonym (H);
625 end loop;
626
627 -- Case of direct name
628
629 else
630 -- First, search the homonym chain for directly visible entities
631
632 H := Current_Entity (Ent);
633 while Present (H) loop
634 exit when
635 not Is_Overloadable (H)
636 and then Is_Immediately_Visible (H);
637
638 if Is_Immediately_Visible (H) and then H /= Ent then
639
640 -- Only add interpretation if not hidden by an inner
641 -- immediately visible one.
642
643 for J in First_Interp .. All_Interp.Last - 1 loop
644
645 -- Current homograph is not hidden. Add to overloads
646
647 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
648 exit;
649
650 -- Homograph is hidden, unless it is a predefined operator
651
652 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
653
654 -- A homograph in the same scope can occur within an
655 -- instantiation, the resulting ambiguity has to be
656 -- resolved later. The homographs may both be local
657 -- functions or actuals, or may be declared at different
658 -- levels within the instance. The renaming of an actual
659 -- within the instance must not be included.
660
661 if Within_Instance (H)
662 and then H /= Renamed_Entity (Ent)
663 and then not Is_Inherited_Operation (H)
664 then
665 All_Interp.Table (All_Interp.Last) :=
666 (H, Etype (H), Empty, Empty);
667 All_Interp.Append (No_Interp);
668 goto Next_Homograph;
669
670 elsif Scope (H) /= Standard_Standard then
671 goto Next_Homograph;
672 end if;
673 end if;
674 end loop;
675
676 -- On exit, we know that current homograph is not hidden
677
678 Add_One_Interp (N, H, Etype (H));
679
680 if Debug_Flag_E then
681 Write_Str ("Add overloaded interpretation ");
682 Write_Int (Int (H));
683 Write_Eol;
684 end if;
685 end if;
686
687 <<Next_Homograph>>
688 H := Homonym (H);
689 end loop;
690
691 -- Scan list of homographs for use-visible entities only
692
693 H := Current_Entity (Ent);
694
695 while Present (H) loop
696 if Is_Potentially_Use_Visible (H)
697 and then H /= Ent
698 and then Is_Overloadable (H)
699 then
700 for J in First_Interp .. All_Interp.Last - 1 loop
701
702 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
703 exit;
704
705 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
706 goto Next_Use_Homograph;
707 end if;
708 end loop;
709
710 Add_One_Interp (N, H, Etype (H));
711 end if;
712
713 <<Next_Use_Homograph>>
714 H := Homonym (H);
715 end loop;
716 end if;
717
718 if All_Interp.Last = First_Interp + 1 then
719
720 -- The final interpretation is in fact not overloaded. Note that the
721 -- unique legal interpretation may or may not be the original one,
722 -- so we need to update N's entity and etype now, because once N
723 -- is marked as not overloaded it is also expected to carry the
724 -- proper interpretation.
725
726 Set_Is_Overloaded (N, False);
727 Set_Entity (N, All_Interp.Table (First_Interp).Nam);
728 Set_Etype (N, All_Interp.Table (First_Interp).Typ);
729 end if;
730 end Collect_Interps;
731
732 ------------
733 -- Covers --
734 ------------
735
736 function Covers (T1, T2 : Entity_Id) return Boolean is
737 BT1 : Entity_Id;
738 BT2 : Entity_Id;
739
740 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean;
741 -- In an instance the proper view may not always be correct for
742 -- private types, but private and full view are compatible. This
743 -- removes spurious errors from nested instantiations that involve,
744 -- among other things, types derived from private types.
745
746 function Real_Actual (T : Entity_Id) return Entity_Id;
747 -- If an actual in an inner instance is the formal of an enclosing
748 -- generic, the actual in the enclosing instance is the one that can
749 -- create an accidental ambiguity, and the check on compatibility of
750 -- generic actual types must use this enclosing actual.
751
752 ----------------------
753 -- Full_View_Covers --
754 ----------------------
755
756 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean is
757 begin
758 if Present (Full_View (Typ1))
759 and then Covers (Full_View (Typ1), Typ2)
760 then
761 return True;
762
763 elsif Present (Underlying_Full_View (Typ1))
764 and then Covers (Underlying_Full_View (Typ1), Typ2)
765 then
766 return True;
767
768 else
769 return False;
770 end if;
771 end Full_View_Covers;
772
773 -----------------
774 -- Real_Actual --
775 -----------------
776
777 function Real_Actual (T : Entity_Id) return Entity_Id is
778 Par : constant Node_Id := Parent (T);
779 RA : Entity_Id;
780
781 begin
782 -- Retrieve parent subtype from subtype declaration for actual
783
784 if Nkind (Par) = N_Subtype_Declaration
785 and then not Comes_From_Source (Par)
786 and then Is_Entity_Name (Subtype_Indication (Par))
787 then
788 RA := Entity (Subtype_Indication (Par));
789
790 if Is_Generic_Actual_Type (RA) then
791 return RA;
792 end if;
793 end if;
794
795 -- Otherwise actual is not the actual of an enclosing instance
796
797 return T;
798 end Real_Actual;
799
800 -- Start of processing for Covers
801
802 begin
803 -- If either operand is missing, then this is an error, but ignore it
804 -- and pretend we have a cover if errors already detected since this may
805 -- simply mean we have malformed trees or a semantic error upstream.
806
807 if No (T1) or else No (T2) then
808 if Total_Errors_Detected /= 0 then
809 return True;
810 else
811 raise Program_Error;
812 end if;
813 end if;
814
815 -- Trivial case: same types are always compatible
816
817 if T1 = T2 then
818 return True;
819 end if;
820
821 -- First check for Standard_Void_Type, which is special. Subsequent
822 -- processing in this routine assumes T1 and T2 are bona fide types;
823 -- Standard_Void_Type is a special entity that has some, but not all,
824 -- properties of types.
825
826 if T1 = Standard_Void_Type or else T2 = Standard_Void_Type then
827 return False;
828 end if;
829
830 BT1 := Base_Type (T1);
831 BT2 := Base_Type (T2);
832
833 -- Handle underlying view of records with unknown discriminants
834 -- using the original entity that motivated the construction of
835 -- this underlying record view (see Build_Derived_Private_Type).
836
837 if Is_Underlying_Record_View (BT1) then
838 BT1 := Underlying_Record_View (BT1);
839 end if;
840
841 if Is_Underlying_Record_View (BT2) then
842 BT2 := Underlying_Record_View (BT2);
843 end if;
844
845 -- Simplest case: types that have the same base type and are not generic
846 -- actuals are compatible. Generic actuals belong to their class but are
847 -- not compatible with other types of their class, and in particular
848 -- with other generic actuals. They are however compatible with their
849 -- own subtypes, and itypes with the same base are compatible as well.
850 -- Similarly, constrained subtypes obtained from expressions of an
851 -- unconstrained nominal type are compatible with the base type (may
852 -- lead to spurious ambiguities in obscure cases ???)
853
854 -- Generic actuals require special treatment to avoid spurious ambi-
855 -- guities in an instance, when two formal types are instantiated with
856 -- the same actual, so that different subprograms end up with the same
857 -- signature in the instance. If a generic actual is the actual of an
858 -- enclosing instance, it is that actual that we must compare: generic
859 -- actuals are only incompatible if they appear in the same instance.
860
861 if BT1 = BT2
862 or else BT1 = T2
863 or else BT2 = T1
864 then
865 if not Is_Generic_Actual_Type (T1)
866 or else
867 not Is_Generic_Actual_Type (T2)
868 then
869 return True;
870
871 -- Both T1 and T2 are generic actual types
872
873 else
874 declare
875 RT1 : constant Entity_Id := Real_Actual (T1);
876 RT2 : constant Entity_Id := Real_Actual (T2);
877 begin
878 return RT1 = RT2
879 or else Is_Itype (T1)
880 or else Is_Itype (T2)
881 or else Is_Constr_Subt_For_U_Nominal (T1)
882 or else Is_Constr_Subt_For_U_Nominal (T2)
883 or else Scope (RT1) /= Scope (RT2);
884 end;
885 end if;
886
887 -- This test may seem to be redundant with the above one, but it catches
888 -- peculiar cases where a private type declared in a package is used in
889 -- a generic construct declared in another package, and the body of the
890 -- former package contains an instantiation of the generic construct on
891 -- an object whose type is a subtype of the private type; in this case,
892 -- the subtype is not private but the type is private in the instance.
893
894 elsif Is_Subtype_Of (T1 => T2, T2 => T1) then
895 return True;
896
897 -- Literals are compatible with types in a given "class"
898
899 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
900 or else (T2 = Universal_Real and then Is_Real_Type (T1))
901 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
902 or else (T2 = Universal_Access and then Is_Access_Type (T1))
903 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
904 or else (T2 = Any_Character and then Is_Character_Type (T1))
905 or else (T2 = Any_String and then Is_String_Type (T1))
906 then
907 return True;
908
909 -- The context may be class wide, and a class-wide type is compatible
910 -- with any member of the class.
911
912 elsif Is_Class_Wide_Type (T1)
913 and then Is_Ancestor (Root_Type (T1), T2)
914 then
915 return True;
916
917 elsif Is_Class_Wide_Type (T1)
918 and then Is_Class_Wide_Type (T2)
919 and then Base_Type (Etype (T1)) = Base_Type (Etype (T2))
920 then
921 return True;
922
923 -- Ada 2005 (AI-345): A class-wide abstract interface type covers a
924 -- task_type or protected_type that implements the interface.
925
926 elsif Ada_Version >= Ada_2005
927 and then Is_Concurrent_Type (T2)
928 and then Is_Class_Wide_Type (T1)
929 and then Is_Interface (Etype (T1))
930 and then Interface_Present_In_Ancestor
931 (Typ => BT2, Iface => Etype (T1))
932 then
933 return True;
934
935 -- Ada 2005 (AI-251): A class-wide abstract interface type T1 covers an
936 -- object T2 implementing T1.
937
938 elsif Ada_Version >= Ada_2005
939 and then Is_Tagged_Type (T2)
940 and then Is_Class_Wide_Type (T1)
941 and then Is_Interface (Etype (T1))
942 then
943 if Interface_Present_In_Ancestor (Typ => T2,
944 Iface => Etype (T1))
945 then
946 return True;
947 end if;
948
949 declare
950 E : Entity_Id;
951 Elmt : Elmt_Id;
952
953 begin
954 if Is_Concurrent_Type (BT2) then
955 E := Corresponding_Record_Type (BT2);
956 else
957 E := BT2;
958 end if;
959
960 -- Ada 2005 (AI-251): A class-wide abstract interface type T1
961 -- covers an object T2 that implements a direct derivation of T1.
962 -- Note: test for presence of E is defense against previous error.
963
964 if No (E) then
965 Check_Error_Detected;
966
967 -- Here we have a corresponding record type
968
969 elsif Present (Interfaces (E)) then
970 Elmt := First_Elmt (Interfaces (E));
971 while Present (Elmt) loop
972 if Is_Ancestor (Etype (T1), Node (Elmt)) then
973 return True;
974 else
975 Next_Elmt (Elmt);
976 end if;
977 end loop;
978 end if;
979
980 -- We should also check the case in which T1 is an ancestor of
981 -- some implemented interface???
982
983 return False;
984 end;
985
986 -- In a dispatching call, the formal is of some specific type, and the
987 -- actual is of the corresponding class-wide type, including a subtype
988 -- of the class-wide type.
989
990 elsif Is_Class_Wide_Type (T2)
991 and then
992 (Class_Wide_Type (T1) = Class_Wide_Type (T2)
993 or else Base_Type (Root_Type (T2)) = BT1)
994 then
995 return True;
996
997 -- Some contexts require a class of types rather than a specific type.
998 -- For example, conditions require any boolean type, fixed point
999 -- attributes require some real type, etc. The built-in types Any_XXX
1000 -- represent these classes.
1001
1002 elsif (T1 = Any_Integer and then Is_Integer_Type (T2))
1003 or else (T1 = Any_Boolean and then Is_Boolean_Type (T2))
1004 or else (T1 = Any_Real and then Is_Real_Type (T2))
1005 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
1006 or else (T1 = Any_Discrete and then Is_Discrete_Type (T2))
1007 then
1008 return True;
1009
1010 -- An aggregate is compatible with an array or record type
1011
1012 elsif T2 = Any_Composite and then Is_Aggregate_Type (T1) then
1013 return True;
1014
1015 -- In Ada 2022, an aggregate is compatible with the type that
1016 -- has the corresponding aspect.
1017
1018 elsif Ada_Version >= Ada_2022
1019 and then T2 = Any_Composite
1020 and then Has_Aspect (T1, Aspect_Aggregate)
1021 then
1022 return True;
1023
1024 -- If the expected type is an anonymous access, the designated type must
1025 -- cover that of the expression. Use the base type for this check: even
1026 -- though access subtypes are rare in sources, they are generated for
1027 -- actuals in instantiations.
1028
1029 elsif Ekind (BT1) = E_Anonymous_Access_Type
1030 and then Is_Access_Type (T2)
1031 and then Covers (Designated_Type (T1), Designated_Type (T2))
1032 then
1033 return True;
1034
1035 -- Ada 2012 (AI05-0149): Allow an anonymous access type in the context
1036 -- of a named general access type. An implicit conversion will be
1037 -- applied. For the resolution, the designated types must match if
1038 -- untagged; further, if the designated type is tagged, the designated
1039 -- type of the anonymous access type shall be covered by the designated
1040 -- type of the named access type.
1041
1042 elsif Ada_Version >= Ada_2012
1043 and then Ekind (BT1) = E_General_Access_Type
1044 and then Ekind (BT2) = E_Anonymous_Access_Type
1045 and then Covers (Designated_Type (T1), Designated_Type (T2))
1046 and then Is_Class_Wide_Type (Designated_Type (T1)) >=
1047 Is_Class_Wide_Type (Designated_Type (T2))
1048 then
1049 return True;
1050
1051 -- An Access_To_Subprogram is compatible with itself, or with an
1052 -- anonymous type created for an attribute reference Access.
1053
1054 elsif Ekind (BT1) in E_Access_Subprogram_Type
1055 | E_Access_Protected_Subprogram_Type
1056 and then Is_Access_Type (T2)
1057 and then (not Comes_From_Source (T1)
1058 or else not Comes_From_Source (T2))
1059 and then (Is_Overloadable (Designated_Type (T2))
1060 or else Ekind (Designated_Type (T2)) = E_Subprogram_Type)
1061 and then Type_Conformant (Designated_Type (T1), Designated_Type (T2))
1062 and then Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
1063 then
1064 return True;
1065
1066 -- Ada 2005 (AI-254): An Anonymous_Access_To_Subprogram is compatible
1067 -- with itself, or with an anonymous type created for an attribute
1068 -- reference Access.
1069
1070 elsif Ekind (BT1) in E_Anonymous_Access_Subprogram_Type
1071 | E_Anonymous_Access_Protected_Subprogram_Type
1072 and then Is_Access_Type (T2)
1073 and then (not Comes_From_Source (T1)
1074 or else not Comes_From_Source (T2))
1075 and then (Is_Overloadable (Designated_Type (T2))
1076 or else Ekind (Designated_Type (T2)) = E_Subprogram_Type)
1077 and then Type_Conformant (Designated_Type (T1), Designated_Type (T2))
1078 and then Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
1079 then
1080 return True;
1081
1082 -- The context can be a remote access type, and the expression the
1083 -- corresponding source type declared in a categorized package, or
1084 -- vice versa.
1085
1086 elsif Is_Record_Type (T1)
1087 and then (Is_Remote_Call_Interface (T1) or else Is_Remote_Types (T1))
1088 and then Present (Corresponding_Remote_Type (T1))
1089 then
1090 return Covers (Corresponding_Remote_Type (T1), T2);
1091
1092 -- and conversely.
1093
1094 elsif Is_Record_Type (T2)
1095 and then (Is_Remote_Call_Interface (T2) or else Is_Remote_Types (T2))
1096 and then Present (Corresponding_Remote_Type (T2))
1097 then
1098 return Covers (Corresponding_Remote_Type (T2), T1);
1099
1100 -- Synchronized types are represented at run time by their corresponding
1101 -- record type. During expansion one is replaced with the other, but
1102 -- they are compatible views of the same type.
1103
1104 elsif Is_Record_Type (T1)
1105 and then Is_Concurrent_Type (T2)
1106 and then Present (Corresponding_Record_Type (T2))
1107 then
1108 return Covers (T1, Corresponding_Record_Type (T2));
1109
1110 elsif Is_Concurrent_Type (T1)
1111 and then Present (Corresponding_Record_Type (T1))
1112 and then Is_Record_Type (T2)
1113 then
1114 return Covers (Corresponding_Record_Type (T1), T2);
1115
1116 -- During analysis, an attribute reference 'Access has a special type
1117 -- kind: Access_Attribute_Type, to be replaced eventually with the type
1118 -- imposed by context.
1119
1120 elsif Ekind (T2) = E_Access_Attribute_Type
1121 and then Ekind (BT1) in E_General_Access_Type | E_Access_Type
1122 and then Covers (Designated_Type (T1), Designated_Type (T2))
1123 then
1124 -- If the target type is a RACW type while the source is an access
1125 -- attribute type, we are building a RACW that may be exported.
1126
1127 if Is_Remote_Access_To_Class_Wide_Type (BT1) then
1128 Set_Has_RACW (Current_Sem_Unit);
1129 end if;
1130
1131 return True;
1132
1133 -- Ditto for allocators, which eventually resolve to the context type
1134
1135 elsif Ekind (T2) = E_Allocator_Type and then Is_Access_Type (T1) then
1136 return Covers (Designated_Type (T1), Designated_Type (T2))
1137 or else
1138 (From_Limited_With (Designated_Type (T1))
1139 and then Covers (Designated_Type (T2), Designated_Type (T1)));
1140
1141 -- A boolean operation on integer literals is compatible with modular
1142 -- context.
1143
1144 elsif T2 = Any_Modular and then Is_Modular_Integer_Type (T1) then
1145 return True;
1146
1147 -- The actual type may be the result of a previous error
1148
1149 elsif BT2 = Any_Type then
1150 return True;
1151
1152 -- A Raise_Expressions is legal in any expression context
1153
1154 elsif BT2 = Raise_Type then
1155 return True;
1156
1157 -- A packed array type covers its corresponding non-packed type. This is
1158 -- not legitimate Ada, but allows the omission of a number of otherwise
1159 -- useless unchecked conversions, and since this can only arise in
1160 -- (known correct) expanded code, no harm is done.
1161
1162 elsif Is_Packed_Array (T2)
1163 and then T1 = Packed_Array_Impl_Type (T2)
1164 then
1165 return True;
1166
1167 -- Similarly an array type covers its corresponding packed array type
1168
1169 elsif Is_Packed_Array (T1)
1170 and then T2 = Packed_Array_Impl_Type (T1)
1171 then
1172 return True;
1173
1174 -- With types exported from instantiations, check whether a partial and
1175 -- a full view match. Verify that types are legal, to prevent cascaded
1176 -- errors.
1177
1178 elsif Is_Private_Type (T1)
1179 and then Is_Type (T2)
1180 and then Is_Generic_Actual_Type (T2)
1181 and then Full_View_Covers (T1, T2)
1182 then
1183 return True;
1184
1185 elsif Is_Private_Type (T2)
1186 and then Is_Type (T1)
1187 and then Is_Generic_Actual_Type (T1)
1188 and then Full_View_Covers (T2, T1)
1189 then
1190 return True;
1191
1192 -- In the expansion of inlined bodies, types are compatible if they
1193 -- are structurally equivalent.
1194
1195 elsif In_Inlined_Body
1196 and then (Underlying_Type (T1) = Underlying_Type (T2)
1197 or else
1198 (Is_Access_Type (T1)
1199 and then Is_Access_Type (T2)
1200 and then Designated_Type (T1) = Designated_Type (T2))
1201 or else
1202 (T1 = Universal_Access
1203 and then Is_Access_Type (Underlying_Type (T2)))
1204 or else
1205 (T2 = Any_Composite
1206 and then Is_Composite_Type (Underlying_Type (T1))))
1207 then
1208 return True;
1209
1210 -- Ada 2005 (AI-50217): Additional branches to make the shadow entity
1211 -- obtained through a limited_with compatible with its real entity.
1212
1213 elsif From_Limited_With (T1) then
1214
1215 -- If the expected type is the nonlimited view of a type, the
1216 -- expression may have the limited view. If that one in turn is
1217 -- incomplete, get full view if available.
1218
1219 return Has_Non_Limited_View (T1)
1220 and then Covers (Get_Full_View (Non_Limited_View (T1)), T2);
1221
1222 elsif From_Limited_With (T2) then
1223
1224 -- If units in the context have Limited_With clauses on each other,
1225 -- either type might have a limited view. Checks performed elsewhere
1226 -- verify that the context type is the nonlimited view.
1227
1228 return Has_Non_Limited_View (T2)
1229 and then Covers (T1, Get_Full_View (Non_Limited_View (T2)));
1230
1231 -- Coverage for incomplete types
1232
1233 elsif Ekind (T1) = E_Incomplete_Type
1234 and then Present (Full_View (T1))
1235 then
1236 return Covers (Full_View (T1), T2);
1237
1238 elsif Ekind (T2) = E_Incomplete_Type
1239 and then Present (Full_View (T2))
1240 then
1241 return Covers (T1, Full_View (T2));
1242
1243 -- Ada 2005 (AI-412): Coverage for regular incomplete subtypes
1244
1245 elsif Ekind (T1) = E_Incomplete_Subtype then
1246 return Covers (Full_View (Etype (T1)), T2);
1247
1248 elsif Ekind (T2) = E_Incomplete_Subtype then
1249 return Covers (T1, Full_View (Etype (T2)));
1250
1251 -- Ada 2005 (AI-423): Coverage of formal anonymous access types
1252 -- and actual anonymous access types in the context of generic
1253 -- instantiations. We have the following situation:
1254
1255 -- generic
1256 -- type Formal is private;
1257 -- Formal_Obj : access Formal; -- T1
1258 -- package G is ...
1259
1260 -- package P is
1261 -- type Actual is ...
1262 -- Actual_Obj : access Actual; -- T2
1263 -- package Instance is new G (Formal => Actual,
1264 -- Formal_Obj => Actual_Obj);
1265
1266 elsif Ada_Version >= Ada_2005
1267 and then Is_Anonymous_Access_Type (T1)
1268 and then Is_Anonymous_Access_Type (T2)
1269 and then Is_Generic_Type (Directly_Designated_Type (T1))
1270 and then Get_Instance_Of (Directly_Designated_Type (T1)) =
1271 Directly_Designated_Type (T2)
1272 then
1273 return True;
1274
1275 -- Otherwise, types are not compatible
1276
1277 else
1278 return False;
1279 end if;
1280 end Covers;
1281
1282 ------------------
1283 -- Disambiguate --
1284 ------------------
1285
1286 function Disambiguate
1287 (N : Node_Id;
1288 I1, I2 : Interp_Index;
1289 Typ : Entity_Id) return Interp
1290 is
1291 I : Interp_Index;
1292 It : Interp;
1293 It1, It2 : Interp;
1294 Nam1, Nam2 : Entity_Id;
1295 Predef_Subp : Entity_Id;
1296 User_Subp : Entity_Id;
1297
1298 function Inherited_From_Actual (S : Entity_Id) return Boolean;
1299 -- Determine whether one of the candidates is an operation inherited by
1300 -- a type that is derived from an actual in an instantiation.
1301
1302 function In_Same_Declaration_List
1303 (Typ : Entity_Id;
1304 Op_Decl : Entity_Id) return Boolean;
1305 -- AI05-0020: a spurious ambiguity may arise when equality on anonymous
1306 -- access types is declared on the partial view of a designated type, so
1307 -- that the type declaration and equality are not in the same list of
1308 -- declarations. This AI gives a preference rule for the user-defined
1309 -- operation. Same rule applies for arithmetic operations on private
1310 -- types completed with fixed-point types: the predefined operation is
1311 -- hidden; this is already handled properly in GNAT.
1312
1313 function Is_Actual_Subprogram (S : Entity_Id) return Boolean;
1314 -- Determine whether a subprogram is an actual in an enclosing instance.
1315 -- An overloading between such a subprogram and one declared outside the
1316 -- instance is resolved in favor of the first, because it resolved in
1317 -- the generic. Within the instance the actual is represented by a
1318 -- constructed subprogram renaming.
1319
1320 function Is_Ambiguous_Boolean_Operator (I : Interp) return Boolean;
1321 -- Determine whether I corresponds to an "ambiguous" boolean operator.
1322 -- Such an interpretation is used to record the ambiguity of operands
1323 -- diagnosed during the analysis of comparison and equality operations.
1324 -- See Find_Comparison_Equality_Types in Sem_Ch4 for the rationale.
1325
1326 function Matches (Op : Node_Id; Func_Id : Entity_Id) return Boolean;
1327 -- Determine whether function Func_Id is an exact match for binary or
1328 -- unary operator Op.
1329
1330 function Operand_Type return Entity_Id;
1331 -- Determine type of operand for an equality operation, to apply Ada
1332 -- 2005 rules to equality on anonymous access types.
1333
1334 function Standard_Operator return Boolean;
1335 -- Check whether subprogram is predefined operator declared in Standard.
1336 -- It may given by an operator name, or by an expanded name whose prefix
1337 -- is Standard.
1338
1339 function Remove_Conversions_And_Abstract_Operations return Interp;
1340 -- Last chance for pathological cases involving comparisons on literals,
1341 -- and user overloadings of the same operator. Such pathologies have
1342 -- been removed from the ACVC, but still appear in two DEC tests, with
1343 -- the following notable quote from Ben Brosgol:
1344 --
1345 -- [Note: I disclaim all credit/responsibility/blame for coming up with
1346 -- this example; Robert Dewar brought it to our attention, since it is
1347 -- apparently found in the ACVC 1.5. I did not attempt to find the
1348 -- reason in the Reference Manual that makes the example legal, since I
1349 -- was too nauseated by it to want to pursue it further.]
1350 --
1351 -- Accordingly, this is not a fully recursive solution, but it handles
1352 -- DEC tests c460vsa, c460vsb. It also handles ai00136a, which pushes
1353 -- pathology in the other direction with calls whose multiple overloaded
1354 -- actuals make them truly unresolvable.
1355
1356 -- The new rules concerning abstract operations create additional need
1357 -- for special handling of expressions with universal operands, see
1358 -- comments to Has_Abstract_Interpretation below.
1359
1360 function Is_User_Defined_Anonymous_Access_Equality
1361 (User_Subp, Predef_Subp : Entity_Id) return Boolean;
1362 -- Check for Ada 2005, AI-020: If the context involves an anonymous
1363 -- access operand, recognize a user-defined equality (User_Subp) with
1364 -- the proper signature, declared in the same declarative list as the
1365 -- type and not hiding a predefined equality Predef_Subp.
1366
1367 ---------------------------
1368 -- Inherited_From_Actual --
1369 ---------------------------
1370
1371 function Inherited_From_Actual (S : Entity_Id) return Boolean is
1372 Par : constant Node_Id := Parent (S);
1373 begin
1374 if Nkind (Par) /= N_Full_Type_Declaration
1375 or else Nkind (Type_Definition (Par)) /= N_Derived_Type_Definition
1376 then
1377 return False;
1378 else
1379 return Is_Entity_Name (Subtype_Indication (Type_Definition (Par)))
1380 and then
1381 Is_Generic_Actual_Type (
1382 Entity (Subtype_Indication (Type_Definition (Par))));
1383 end if;
1384 end Inherited_From_Actual;
1385
1386 ------------------------------
1387 -- In_Same_Declaration_List --
1388 ------------------------------
1389
1390 function In_Same_Declaration_List
1391 (Typ : Entity_Id;
1392 Op_Decl : Entity_Id) return Boolean
1393 is
1394 Scop : constant Entity_Id := Scope (Typ);
1395
1396 begin
1397 return In_Same_List (Parent (Typ), Op_Decl)
1398 or else
1399 (Is_Package_Or_Generic_Package (Scop)
1400 and then List_Containing (Op_Decl) =
1401 Visible_Declarations (Parent (Scop))
1402 and then List_Containing (Parent (Typ)) =
1403 Private_Declarations (Parent (Scop)));
1404 end In_Same_Declaration_List;
1405
1406 --------------------------
1407 -- Is_Actual_Subprogram --
1408 --------------------------
1409
1410 function Is_Actual_Subprogram (S : Entity_Id) return Boolean is
1411 begin
1412 return In_Open_Scopes (Scope (S))
1413 and then Nkind (Unit_Declaration_Node (S)) =
1414 N_Subprogram_Renaming_Declaration
1415
1416 -- Determine if the renaming came from source or was generated as a
1417 -- a result of generic expansion since the actual is represented by
1418 -- a constructed subprogram renaming.
1419
1420 and then not Comes_From_Source (Unit_Declaration_Node (S))
1421
1422 and then
1423 (Is_Generic_Instance (Scope (S))
1424 or else Is_Wrapper_Package (Scope (S)));
1425 end Is_Actual_Subprogram;
1426
1427 -----------------------------------
1428 -- Is_Ambiguous_Boolean_Operator --
1429 -----------------------------------
1430
1431 function Is_Ambiguous_Boolean_Operator (I : Interp) return Boolean is
1432 begin
1433 return Ekind (I.Nam) = E_Operator
1434 and then I.Typ = Standard_Boolean
1435 and then I.Opnd_Typ = Any_Type;
1436 end Is_Ambiguous_Boolean_Operator;
1437
1438 -------------
1439 -- Matches --
1440 -------------
1441
1442 function Matches (Op : Node_Id; Func_Id : Entity_Id) return Boolean is
1443 function Matching_Types
1444 (Opnd_Typ : Entity_Id;
1445 Formal_Typ : Entity_Id) return Boolean;
1446 -- Determine whether operand type Opnd_Typ and formal parameter type
1447 -- Formal_Typ are either the same or compatible.
1448
1449 --------------------
1450 -- Matching_Types --
1451 --------------------
1452
1453 function Matching_Types
1454 (Opnd_Typ : Entity_Id;
1455 Formal_Typ : Entity_Id) return Boolean
1456 is
1457 begin
1458 -- A direct match
1459
1460 if Opnd_Typ = Formal_Typ then
1461 return True;
1462
1463 -- Any integer type matches universal integer
1464
1465 elsif Opnd_Typ = Universal_Integer
1466 and then Is_Integer_Type (Formal_Typ)
1467 then
1468 return True;
1469
1470 -- Any floating point type matches universal real
1471
1472 elsif Opnd_Typ = Universal_Real
1473 and then Is_Floating_Point_Type (Formal_Typ)
1474 then
1475 return True;
1476
1477 -- The type of the formal parameter maps a generic actual type to
1478 -- a generic formal type. If the operand type is the type being
1479 -- mapped in an instance, then this is a match.
1480
1481 elsif Is_Generic_Actual_Type (Formal_Typ)
1482 and then Etype (Formal_Typ) = Opnd_Typ
1483 then
1484 return True;
1485
1486 -- Formal_Typ is a private view, or Opnd_Typ and Formal_Typ are
1487 -- compatible only on a base-type basis.
1488
1489 else
1490 return False;
1491 end if;
1492 end Matching_Types;
1493
1494 -- Local variables
1495
1496 F1 : constant Entity_Id := First_Formal (Func_Id);
1497 F1_Typ : constant Entity_Id := Etype (F1);
1498 F2 : constant Entity_Id := Next_Formal (F1);
1499 F2_Typ : constant Entity_Id := Etype (F2);
1500 Lop_Typ : constant Entity_Id := Etype (Left_Opnd (Op));
1501 Rop_Typ : constant Entity_Id := Etype (Right_Opnd (Op));
1502
1503 -- Start of processing for Matches
1504
1505 begin
1506 if Lop_Typ = F1_Typ then
1507 return Matching_Types (Rop_Typ, F2_Typ);
1508
1509 elsif Rop_Typ = F2_Typ then
1510 return Matching_Types (Lop_Typ, F1_Typ);
1511
1512 -- Otherwise this is not a good match because each operand-formal
1513 -- pair is compatible only on base-type basis, which is not specific
1514 -- enough.
1515
1516 else
1517 return False;
1518 end if;
1519 end Matches;
1520
1521 ------------------
1522 -- Operand_Type --
1523 ------------------
1524
1525 function Operand_Type return Entity_Id is
1526 Opnd : Node_Id;
1527
1528 begin
1529 if Nkind (N) = N_Function_Call then
1530 Opnd := First_Actual (N);
1531 else
1532 Opnd := Left_Opnd (N);
1533 end if;
1534
1535 return Etype (Opnd);
1536 end Operand_Type;
1537
1538 ------------------------------------------------
1539 -- Remove_Conversions_And_Abstract_Operations --
1540 ------------------------------------------------
1541
1542 function Remove_Conversions_And_Abstract_Operations return Interp is
1543 I : Interp_Index;
1544 It : Interp;
1545 It1 : Interp;
1546 F1 : Entity_Id;
1547 Act1 : Node_Id;
1548 Act2 : Node_Id;
1549
1550 function Has_Abstract_Interpretation (N : Node_Id) return Boolean;
1551 -- If an operation has universal operands, the universal operation
1552 -- is present among its interpretations. If there is an abstract
1553 -- interpretation for the operator, with a numeric result, this
1554 -- interpretation was already removed in sem_ch4, but the universal
1555 -- one is still visible. We must rescan the list of operators and
1556 -- remove the universal interpretation to resolve the ambiguity.
1557
1558 function Is_Numeric_Only_Type (T : Entity_Id) return Boolean;
1559 -- Return True if T is a numeric type and not Any_Type
1560
1561 ---------------------------------
1562 -- Has_Abstract_Interpretation --
1563 ---------------------------------
1564
1565 function Has_Abstract_Interpretation (N : Node_Id) return Boolean is
1566 E : Entity_Id;
1567
1568 begin
1569 if Nkind (N) not in N_Op
1570 or else Ada_Version < Ada_2005
1571 or else not Is_Overloaded (N)
1572 or else No (Universal_Interpretation (N))
1573 then
1574 return False;
1575
1576 else
1577 E := Get_Name_Entity_Id (Chars (N));
1578 while Present (E) loop
1579 if Is_Overloadable (E)
1580 and then Is_Abstract_Subprogram (E)
1581 and then Is_Numeric_Only_Type (Etype (E))
1582 then
1583 return True;
1584 else
1585 E := Homonym (E);
1586 end if;
1587 end loop;
1588
1589 -- Finally, if an operand of the binary operator is itself
1590 -- an operator, recurse to see whether its own abstract
1591 -- interpretation is responsible for the spurious ambiguity.
1592
1593 if Nkind (N) in N_Binary_Op then
1594 return Has_Abstract_Interpretation (Left_Opnd (N))
1595 or else Has_Abstract_Interpretation (Right_Opnd (N));
1596
1597 elsif Nkind (N) in N_Unary_Op then
1598 return Has_Abstract_Interpretation (Right_Opnd (N));
1599
1600 else
1601 return False;
1602 end if;
1603 end if;
1604 end Has_Abstract_Interpretation;
1605
1606 --------------------------
1607 -- Is_Numeric_Only_Type --
1608 --------------------------
1609
1610 function Is_Numeric_Only_Type (T : Entity_Id) return Boolean is
1611 begin
1612 return Is_Numeric_Type (T) and then T /= Any_Type;
1613 end Is_Numeric_Only_Type;
1614
1615 -- Start of processing for Remove_Conversions_And_Abstract_Operations
1616
1617 begin
1618 It1 := No_Interp;
1619
1620 Get_First_Interp (N, I, It);
1621 while Present (It.Typ) loop
1622 if not Is_Overloadable (It.Nam) then
1623 return No_Interp;
1624 end if;
1625
1626 F1 := First_Formal (It.Nam);
1627
1628 if No (F1) then
1629 return It1;
1630
1631 else
1632 if Nkind (N) in N_Subprogram_Call then
1633 Act1 := First_Actual (N);
1634
1635 if Present (Act1) then
1636 Act2 := Next_Actual (Act1);
1637 else
1638 Act2 := Empty;
1639 end if;
1640
1641 elsif Nkind (N) in N_Unary_Op then
1642 Act1 := Right_Opnd (N);
1643 Act2 := Empty;
1644
1645 elsif Nkind (N) in N_Binary_Op then
1646 Act1 := Left_Opnd (N);
1647 Act2 := Right_Opnd (N);
1648
1649 -- Use the type of the second formal, so as to include
1650 -- exponentiation, where the exponent may be ambiguous and
1651 -- the result non-universal.
1652
1653 Next_Formal (F1);
1654
1655 else
1656 return It1;
1657 end if;
1658
1659 if Nkind (Act1) in N_Op
1660 and then Is_Overloaded (Act1)
1661 and then
1662 (Nkind (Act1) in N_Unary_Op
1663 or else Nkind (Left_Opnd (Act1)) in
1664 N_Integer_Literal | N_Real_Literal)
1665 and then Nkind (Right_Opnd (Act1)) in
1666 N_Integer_Literal | N_Real_Literal
1667 and then Has_Compatible_Type (Act1, Standard_Boolean)
1668 and then Etype (F1) = Standard_Boolean
1669 then
1670 -- If the two candidates are the original ones, the
1671 -- ambiguity is real. Otherwise keep the original, further
1672 -- calls to Disambiguate will take care of others in the
1673 -- list of candidates.
1674
1675 if It1 /= No_Interp then
1676 if It = Disambiguate.It1
1677 or else It = Disambiguate.It2
1678 then
1679 if It1 = Disambiguate.It1
1680 or else It1 = Disambiguate.It2
1681 then
1682 return No_Interp;
1683 else
1684 It1 := It;
1685 end if;
1686 end if;
1687
1688 elsif Present (Act2)
1689 and then Nkind (Act2) in N_Op
1690 and then Is_Overloaded (Act2)
1691 and then Nkind (Right_Opnd (Act2)) in
1692 N_Integer_Literal | N_Real_Literal
1693 and then Has_Compatible_Type (Act2, Standard_Boolean)
1694 then
1695 -- The preference rule on the first actual is not
1696 -- sufficient to disambiguate.
1697
1698 goto Next_Interp;
1699
1700 else
1701 It1 := It;
1702 end if;
1703
1704 elsif Is_Numeric_Only_Type (Etype (F1))
1705 and then Has_Abstract_Interpretation (Act1)
1706 then
1707 -- Current interpretation is not the right one because it
1708 -- expects a numeric operand. Examine all the others.
1709
1710 declare
1711 I : Interp_Index;
1712 It : Interp;
1713
1714 begin
1715 Get_First_Interp (N, I, It);
1716 while Present (It.Typ) loop
1717 if not Is_Numeric_Only_Type
1718 (Etype (First_Formal (It.Nam)))
1719 then
1720 if No (Act2)
1721 or else not
1722 Is_Numeric_Only_Type
1723 (Etype (Next_Formal (First_Formal (It.Nam))))
1724 or else not Has_Abstract_Interpretation (Act2)
1725 then
1726 return It;
1727 end if;
1728 end if;
1729
1730 Get_Next_Interp (I, It);
1731 end loop;
1732
1733 return No_Interp;
1734 end;
1735
1736 elsif Is_Numeric_Only_Type (Etype (F1))
1737 and then Present (Act2)
1738 and then Has_Abstract_Interpretation (Act2)
1739 then
1740 -- Current interpretation is not the right one because it
1741 -- expects a numeric operand. Examine all the others.
1742
1743 declare
1744 I : Interp_Index;
1745 It : Interp;
1746
1747 begin
1748 Get_First_Interp (N, I, It);
1749 while Present (It.Typ) loop
1750 if not Is_Numeric_Only_Type
1751 (Etype (Next_Formal (First_Formal (It.Nam))))
1752 then
1753 if not Is_Numeric_Only_Type
1754 (Etype (First_Formal (It.Nam)))
1755 or else not Has_Abstract_Interpretation (Act1)
1756 then
1757 return It;
1758 end if;
1759 end if;
1760
1761 Get_Next_Interp (I, It);
1762 end loop;
1763
1764 return No_Interp;
1765 end;
1766 end if;
1767 end if;
1768
1769 <<Next_Interp>>
1770 Get_Next_Interp (I, It);
1771 end loop;
1772
1773 return It1;
1774 end Remove_Conversions_And_Abstract_Operations;
1775
1776 -----------------------
1777 -- Standard_Operator --
1778 -----------------------
1779
1780 function Standard_Operator return Boolean is
1781 Nam : Node_Id;
1782
1783 begin
1784 if Nkind (N) in N_Op then
1785 return True;
1786
1787 elsif Nkind (N) = N_Function_Call then
1788 Nam := Name (N);
1789
1790 if Nkind (Nam) /= N_Expanded_Name then
1791 return True;
1792 else
1793 return Entity (Prefix (Nam)) = Standard_Standard;
1794 end if;
1795 else
1796 return False;
1797 end if;
1798 end Standard_Operator;
1799
1800 -----------------------------------------------
1801 -- Is_User_Defined_Anonymous_Access_Equality --
1802 -----------------------------------------------
1803
1804 function Is_User_Defined_Anonymous_Access_Equality
1805 (User_Subp, Predef_Subp : Entity_Id) return Boolean is
1806 begin
1807 return Present (User_Subp)
1808
1809 -- Check for Ada 2005 and use of anonymous access
1810
1811 and then Ada_Version >= Ada_2005
1812 and then Etype (User_Subp) = Standard_Boolean
1813 and then Is_Anonymous_Access_Type (Operand_Type)
1814
1815 -- This check is only relevant if User_Subp is visible and not in
1816 -- an instance
1817
1818 and then (In_Open_Scopes (Scope (User_Subp))
1819 or else Is_Potentially_Use_Visible (User_Subp))
1820 and then not In_Instance
1821 and then not Hides_Op (User_Subp, Predef_Subp)
1822
1823 -- Is User_Subp declared in the same declarative list as the type?
1824
1825 and then
1826 In_Same_Declaration_List
1827 (Designated_Type (Operand_Type),
1828 Unit_Declaration_Node (User_Subp));
1829 end Is_User_Defined_Anonymous_Access_Equality;
1830
1831 -- Start of processing for Disambiguate
1832
1833 begin
1834 -- Recover the two legal interpretations
1835
1836 Get_First_Interp (N, I, It);
1837 while I /= I1 loop
1838 Get_Next_Interp (I, It);
1839 end loop;
1840
1841 It1 := It;
1842 Nam1 := It.Nam;
1843
1844 -- Return immediately if either corresponds to a recorded ambiguity
1845
1846 if Is_Ambiguous_Boolean_Operator (It1) then
1847 return It1;
1848 end if;
1849
1850 while I /= I2 loop
1851 Get_Next_Interp (I, It);
1852 end loop;
1853
1854 It2 := It;
1855 Nam2 := It.Nam;
1856
1857 -- See above
1858
1859 if Is_Ambiguous_Boolean_Operator (It2) then
1860 return It2;
1861 end if;
1862
1863 -- Check whether one of the entities is an Ada 2005/2012/2022 and we
1864 -- are operating in an earlier mode, in which case we discard the Ada
1865 -- 2005/2012/2022 entity, so that we get proper Ada 95 overload
1866 -- resolution.
1867
1868 if Ada_Version < Ada_2005 then
1869 if Is_Ada_2005_Only (Nam1)
1870 or else Is_Ada_2012_Only (Nam1)
1871 or else Is_Ada_2022_Only (Nam1)
1872 then
1873 return It2;
1874
1875 elsif Is_Ada_2005_Only (Nam2)
1876 or else Is_Ada_2012_Only (Nam2)
1877 or else Is_Ada_2022_Only (Nam2)
1878 then
1879 return It1;
1880 end if;
1881
1882 -- Check whether one of the entities is an Ada 2012/2022 entity and we
1883 -- are operating in Ada 2005 mode, in which case we discard the Ada 2012
1884 -- Ada 2022 entity, so that we get proper Ada 2005 overload resolution.
1885
1886 elsif Ada_Version = Ada_2005 then
1887 if Is_Ada_2012_Only (Nam1) or else Is_Ada_2022_Only (Nam1) then
1888 return It2;
1889 elsif Is_Ada_2012_Only (Nam2) or else Is_Ada_2022_Only (Nam2) then
1890 return It1;
1891 end if;
1892
1893 -- Ditto for Ada 2012 vs Ada 2022.
1894
1895 elsif Ada_Version = Ada_2012 then
1896 if Is_Ada_2022_Only (Nam1) then
1897 return It2;
1898 elsif Is_Ada_2022_Only (Nam2) then
1899 return It1;
1900 end if;
1901 end if;
1902
1903 -- If the context is universal, the predefined operator is preferred.
1904 -- This includes bounds in numeric type declarations, and expressions
1905 -- in type conversions. If no interpretation yields a universal type,
1906 -- then we must check whether the user-defined entity hides the prede-
1907 -- fined one.
1908
1909 if Chars (Nam1) in Any_Operator_Name and then Standard_Operator then
1910 if Typ = Universal_Integer
1911 or else Typ = Universal_Real
1912 or else Typ = Any_Integer
1913 or else Typ = Any_Discrete
1914 or else Typ = Any_Real
1915 or else Typ = Any_Type
1916 then
1917 -- Find an interpretation that yields the universal type, or else
1918 -- a predefined operator that yields a predefined numeric type.
1919
1920 declare
1921 Candidate : Interp := No_Interp;
1922
1923 begin
1924 Get_First_Interp (N, I, It);
1925 while Present (It.Typ) loop
1926 if Is_Universal_Numeric_Type (It.Typ)
1927 and then (Typ = Any_Type or else Covers (Typ, It.Typ))
1928 then
1929 return It;
1930
1931 elsif Is_Numeric_Type (It.Typ)
1932 and then Scope (It.Typ) = Standard_Standard
1933 and then Scope (It.Nam) = Standard_Standard
1934 and then Covers (Typ, It.Typ)
1935 then
1936 Candidate := It;
1937 end if;
1938
1939 Get_Next_Interp (I, It);
1940 end loop;
1941
1942 if Candidate /= No_Interp then
1943 return Candidate;
1944 end if;
1945 end;
1946
1947 elsif Chars (Nam1) /= Name_Op_Not
1948 and then (Typ = Standard_Boolean or else Typ = Any_Boolean)
1949 then
1950 -- Equality or comparison operation. Choose predefined operator if
1951 -- arguments are universal. The node may be an operator, name, or
1952 -- a function call, so unpack arguments accordingly.
1953
1954 declare
1955 Arg1, Arg2 : Node_Id;
1956
1957 begin
1958 if Nkind (N) in N_Op then
1959 Arg1 := Left_Opnd (N);
1960 Arg2 := Right_Opnd (N);
1961
1962 elsif Is_Entity_Name (N) then
1963 Arg1 := First_Entity (Entity (N));
1964 Arg2 := Next_Entity (Arg1);
1965
1966 else
1967 Arg1 := First_Actual (N);
1968 Arg2 := Next_Actual (Arg1);
1969 end if;
1970
1971 if Present (Arg2) then
1972 if Ekind (Nam1) = E_Operator then
1973 Predef_Subp := Nam1;
1974 User_Subp := Nam2;
1975 elsif Ekind (Nam2) = E_Operator then
1976 Predef_Subp := Nam2;
1977 User_Subp := Nam1;
1978 else
1979 Predef_Subp := Empty;
1980 User_Subp := Empty;
1981 end if;
1982
1983 -- Take into account universal interpretation as well as
1984 -- universal_access equality, as long as AI05-0020 does not
1985 -- trigger.
1986
1987 if (Present (Universal_Interpretation (Arg1))
1988 and then Universal_Interpretation (Arg2) =
1989 Universal_Interpretation (Arg1))
1990 or else
1991 (Nkind (N) in N_Op_Eq | N_Op_Ne
1992 and then (Is_Anonymous_Access_Type (Etype (Arg1))
1993 or else
1994 Is_Anonymous_Access_Type (Etype (Arg2)))
1995 and then not
1996 Is_User_Defined_Anonymous_Access_Equality
1997 (User_Subp, Predef_Subp))
1998 then
1999 Get_First_Interp (N, I, It);
2000 while Scope (It.Nam) /= Standard_Standard loop
2001 Get_Next_Interp (I, It);
2002 end loop;
2003
2004 return It;
2005 end if;
2006 end if;
2007 end;
2008 end if;
2009 end if;
2010
2011 -- If no universal interpretation, check whether user-defined operator
2012 -- hides predefined one, as well as other special cases. If the node
2013 -- is a range, then one or both bounds are ambiguous. Each will have
2014 -- to be disambiguated w.r.t. the context type. The type of the range
2015 -- itself is imposed by the context, so we can return either legal
2016 -- interpretation.
2017
2018 if Ekind (Nam1) = E_Operator then
2019 Predef_Subp := Nam1;
2020 User_Subp := Nam2;
2021
2022 elsif Ekind (Nam2) = E_Operator then
2023 Predef_Subp := Nam2;
2024 User_Subp := Nam1;
2025
2026 elsif Nkind (N) = N_Range then
2027 return It1;
2028
2029 -- Implement AI05-105: A renaming declaration with an access
2030 -- definition must resolve to an anonymous access type. This
2031 -- is a resolution rule and can be used to disambiguate.
2032
2033 elsif Nkind (Parent (N)) = N_Object_Renaming_Declaration
2034 and then Present (Access_Definition (Parent (N)))
2035 then
2036 if Is_Anonymous_Access_Type (It1.Typ) then
2037 if Ekind (It2.Typ) = Ekind (It1.Typ) then
2038
2039 -- True ambiguity
2040
2041 return No_Interp;
2042
2043 else
2044 return It1;
2045 end if;
2046
2047 elsif Is_Anonymous_Access_Type (It2.Typ) then
2048 return It2;
2049
2050 -- No legal interpretation
2051
2052 else
2053 return No_Interp;
2054 end if;
2055
2056 -- Two access attribute types may have been created for an expression
2057 -- with an implicit dereference, which is automatically overloaded.
2058 -- If both access attribute types designate the same object type,
2059 -- disambiguation if any will take place elsewhere, so keep any one of
2060 -- the interpretations.
2061
2062 elsif Ekind (It1.Typ) = E_Access_Attribute_Type
2063 and then Ekind (It2.Typ) = E_Access_Attribute_Type
2064 and then Designated_Type (It1.Typ) = Designated_Type (It2.Typ)
2065 then
2066 return It1;
2067
2068 -- If two user defined-subprograms are visible, it is a true ambiguity,
2069 -- unless one of them is an entry and the context is a conditional or
2070 -- timed entry call, or unless we are within an instance and this is
2071 -- results from two formals types with the same actual.
2072
2073 else
2074 if Nkind (N) = N_Procedure_Call_Statement
2075 and then Nkind (Parent (N)) = N_Entry_Call_Alternative
2076 and then N = Entry_Call_Statement (Parent (N))
2077 then
2078 if Ekind (Nam2) = E_Entry then
2079 return It2;
2080 elsif Ekind (Nam1) = E_Entry then
2081 return It1;
2082 else
2083 return No_Interp;
2084 end if;
2085
2086 -- If the ambiguity occurs within an instance, it is due to several
2087 -- formal types with the same actual. Look for an exact match between
2088 -- the types of the formals of the overloadable entities, and the
2089 -- actuals in the call, to recover the unambiguous match in the
2090 -- original generic.
2091
2092 -- The ambiguity can also be due to an overloading between a formal
2093 -- subprogram and a subprogram declared outside the generic. If the
2094 -- node is overloaded, it did not resolve to the global entity in
2095 -- the generic, and we choose the formal subprogram.
2096
2097 -- Finally, the ambiguity can be between an explicit subprogram and
2098 -- one inherited (with different defaults) from an actual. In this
2099 -- case the resolution was to the explicit declaration in the
2100 -- generic, and remains so in the instance.
2101
2102 -- The same sort of disambiguation needed for calls is also required
2103 -- for the name given in a subprogram renaming, and that case is
2104 -- handled here as well. We test Comes_From_Source to exclude this
2105 -- treatment for implicit renamings created for formal subprograms.
2106
2107 elsif In_Instance and then not In_Generic_Actual (N) then
2108 if Nkind (N) in N_Subprogram_Call
2109 or else
2110 (Nkind (N) in N_Has_Entity
2111 and then
2112 Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
2113 and then Comes_From_Source (Parent (N)))
2114 then
2115 declare
2116 Actual : Node_Id;
2117 Formal : Entity_Id;
2118 Renam : Entity_Id := Empty;
2119 Is_Act1 : constant Boolean := Is_Actual_Subprogram (Nam1);
2120 Is_Act2 : constant Boolean := Is_Actual_Subprogram (Nam2);
2121
2122 begin
2123 if Is_Act1 and then not Is_Act2 then
2124 return It1;
2125
2126 elsif Is_Act2 and then not Is_Act1 then
2127 return It2;
2128
2129 elsif Inherited_From_Actual (Nam1)
2130 and then Comes_From_Source (Nam2)
2131 then
2132 return It2;
2133
2134 elsif Inherited_From_Actual (Nam2)
2135 and then Comes_From_Source (Nam1)
2136 then
2137 return It1;
2138 end if;
2139
2140 -- In the case of a renamed subprogram, pick up the entity
2141 -- of the renaming declaration so we can traverse its
2142 -- formal parameters.
2143
2144 if Nkind (N) in N_Has_Entity then
2145 Renam := Defining_Unit_Name (Specification (Parent (N)));
2146 end if;
2147
2148 if Present (Renam) then
2149 Actual := First_Formal (Renam);
2150 else
2151 Actual := First_Actual (N);
2152 end if;
2153
2154 Formal := First_Formal (Nam1);
2155 while Present (Actual) loop
2156 if Etype (Actual) /= Etype (Formal) then
2157 return It2;
2158 end if;
2159
2160 if Present (Renam) then
2161 Next_Formal (Actual);
2162 else
2163 Next_Actual (Actual);
2164 end if;
2165
2166 Next_Formal (Formal);
2167 end loop;
2168
2169 return It1;
2170 end;
2171
2172 elsif Nkind (N) in N_Binary_Op then
2173 if Matches (N, Nam1) then
2174 return It1;
2175 else
2176 return It2;
2177 end if;
2178
2179 elsif Nkind (N) in N_Unary_Op then
2180 if Etype (Right_Opnd (N)) = Etype (First_Formal (Nam1)) then
2181 return It1;
2182 else
2183 return It2;
2184 end if;
2185
2186 else
2187 return Remove_Conversions_And_Abstract_Operations;
2188 end if;
2189 else
2190 return Remove_Conversions_And_Abstract_Operations;
2191 end if;
2192 end if;
2193
2194 -- An implicit concatenation operator on a string type cannot be
2195 -- disambiguated from the predefined concatenation. This can only
2196 -- happen with concatenation of string literals.
2197
2198 if Chars (User_Subp) = Name_Op_Concat
2199 and then Ekind (User_Subp) = E_Operator
2200 and then Is_String_Type (Etype (First_Formal (User_Subp)))
2201 then
2202 return No_Interp;
2203
2204 -- If the user-defined operator matches the signature of the operator,
2205 -- and is declared in an open scope, or in the scope of the resulting
2206 -- type, or given by an expanded name that names its scope, it hides
2207 -- the predefined operator for the type. But exponentiation has to be
2208 -- special-cased because the latter operator does not have a symmetric
2209 -- signature, and may not be hidden by the explicit one.
2210
2211 elsif Hides_Op (User_Subp, Predef_Subp)
2212 or else (Nkind (N) = N_Function_Call
2213 and then Nkind (Name (N)) = N_Expanded_Name
2214 and then (Chars (Predef_Subp) /= Name_Op_Expon
2215 or else Hides_Op (User_Subp, Predef_Subp))
2216 and then Scope (User_Subp) = Entity (Prefix (Name (N))))
2217 then
2218 if It1.Nam = User_Subp then
2219 return It1;
2220 else
2221 return It2;
2222 end if;
2223
2224 -- Otherwise, the predefined operator has precedence, or if the user-
2225 -- defined operation is directly visible we have a true ambiguity.
2226
2227 -- If this is a fixed-point multiplication and division in Ada 83 mode,
2228 -- exclude the universal_fixed operator, which often causes ambiguities
2229 -- in legacy code.
2230
2231 -- Ditto in Ada 2012, where an ambiguity may arise for an operation
2232 -- on a partial view that is completed with a fixed point type. See
2233 -- AI05-0020 and AI05-0209. The ambiguity is resolved in favor of the
2234 -- user-defined type and subprogram, so that a client of the package
2235 -- has the same resolution as the body of the package.
2236
2237 else
2238 if (In_Open_Scopes (Scope (User_Subp))
2239 or else Is_Potentially_Use_Visible (User_Subp))
2240 and then not In_Instance
2241 then
2242 if Is_Fixed_Point_Type (Typ)
2243 and then Chars (Nam1) in Name_Op_Multiply | Name_Op_Divide
2244 and then
2245 (Ada_Version = Ada_83
2246 or else (Ada_Version >= Ada_2012
2247 and then In_Same_Declaration_List
2248 (First_Subtype (Typ),
2249 Unit_Declaration_Node (User_Subp))))
2250 then
2251 if It2.Nam = Predef_Subp then
2252 return It1;
2253 else
2254 return It2;
2255 end if;
2256
2257 -- Check for AI05-020
2258
2259 elsif Chars (Nam1) in Name_Op_Eq | Name_Op_Ne
2260 and then Is_User_Defined_Anonymous_Access_Equality
2261 (User_Subp, Predef_Subp)
2262 then
2263 if It2.Nam = Predef_Subp then
2264 return It1;
2265 else
2266 return It2;
2267 end if;
2268
2269 -- RM 8.4(10): an immediately visible operator hides a use-visible
2270 -- user-defined operation that is a homograph. This disambiguation
2271 -- cannot take place earlier because visibility of the predefined
2272 -- operator can only be established when operand types are known.
2273
2274 elsif Ekind (User_Subp) = E_Function
2275 and then Ekind (Predef_Subp) = E_Operator
2276 and then Operator_Matches_Spec (Predef_Subp, User_Subp)
2277 and then Nkind (N) in N_Op
2278 and then not Is_Overloaded (Right_Opnd (N))
2279 and then
2280 Is_Immediately_Visible (Base_Type (Etype (Right_Opnd (N))))
2281 and then Is_Potentially_Use_Visible (User_Subp)
2282 then
2283 if It1.Nam = Predef_Subp then
2284 return It1;
2285 else
2286 return It2;
2287 end if;
2288
2289 else
2290 return Remove_Conversions_And_Abstract_Operations;
2291 end if;
2292
2293 elsif It1.Nam = Predef_Subp then
2294 return It1;
2295
2296 else
2297 return It2;
2298 end if;
2299 end if;
2300 end Disambiguate;
2301
2302 -------------------------
2303 -- Entity_Matches_Spec --
2304 -------------------------
2305
2306 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean is
2307 begin
2308 -- For the simple case of same kinds, type conformance is required, but
2309 -- a parameterless function can also rename a literal.
2310
2311 if Ekind (Old_S) = Ekind (New_S)
2312 or else (Ekind (New_S) = E_Function
2313 and then Ekind (Old_S) = E_Enumeration_Literal)
2314 then
2315 return Type_Conformant (New_S, Old_S);
2316
2317 -- Likewise for a procedure and an entry
2318
2319 elsif Ekind (New_S) = E_Procedure and then Is_Entry (Old_S) then
2320 return Type_Conformant (New_S, Old_S);
2321
2322 -- For a user-defined operator, use the dedicated predicate
2323
2324 elsif Ekind (New_S) = E_Function and then Ekind (Old_S) = E_Operator then
2325 return Operator_Matches_Spec (Old_S, New_S);
2326
2327 else
2328 return False;
2329 end if;
2330 end Entity_Matches_Spec;
2331
2332 ----------------------
2333 -- Find_Unique_Type --
2334 ----------------------
2335
2336 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id is
2337 T : constant Entity_Id := Specific_Type (Etype (L), Etype (R));
2338
2339 begin
2340 if T = Any_Type then
2341 if Is_User_Defined_Literal (L, Etype (R)) then
2342 return Etype (R);
2343 elsif Is_User_Defined_Literal (R, Etype (L)) then
2344 return Etype (L);
2345 end if;
2346 end if;
2347
2348 return T;
2349 end Find_Unique_Type;
2350
2351 -------------------------------------
2352 -- Function_Interp_Has_Abstract_Op --
2353 -------------------------------------
2354
2355 function Function_Interp_Has_Abstract_Op
2356 (N : Node_Id;
2357 E : Entity_Id) return Entity_Id
2358 is
2359 Abstr_Op : Entity_Id;
2360 Act : Node_Id;
2361 Act_Parm : Node_Id;
2362 Form_Parm : Node_Id;
2363
2364 begin
2365 if Is_Overloaded (N) then
2366 -- Move through the formals and actuals of the call to
2367 -- determine if an abstract interpretation exists.
2368
2369 Act_Parm := First_Actual (N);
2370 Form_Parm := First_Formal (E);
2371 while Present (Act_Parm) and then Present (Form_Parm) loop
2372 Act := Act_Parm;
2373
2374 -- Extract the actual from a parameter association
2375
2376 if Nkind (Act) = N_Parameter_Association then
2377 Act := Explicit_Actual_Parameter (Act);
2378 end if;
2379
2380 -- Use the actual and the type of its correponding formal to test
2381 -- for an abstract interpretation and return it when found.
2382
2383 Abstr_Op := Has_Abstract_Op (Act, Etype (Form_Parm));
2384
2385 if Present (Abstr_Op) then
2386 return Abstr_Op;
2387 end if;
2388
2389 Next_Actual (Act_Parm);
2390 Next_Formal (Form_Parm);
2391 end loop;
2392 end if;
2393
2394 -- Otherwise, return empty
2395
2396 return Empty;
2397 end Function_Interp_Has_Abstract_Op;
2398
2399 ----------------------
2400 -- Get_First_Interp --
2401 ----------------------
2402
2403 procedure Get_First_Interp
2404 (N : Node_Id;
2405 I : out Interp_Index;
2406 It : out Interp)
2407 is
2408 Int_Ind : Interp_Index;
2409 O_N : Node_Id;
2410
2411 begin
2412 -- If a selected component is overloaded because the selector has
2413 -- multiple interpretations, the node is a call to a protected
2414 -- operation or an indirect call. Retrieve the interpretation from
2415 -- the selector name. The selected component may be overloaded as well
2416 -- if the prefix is overloaded. That case is unchanged.
2417
2418 if Nkind (N) = N_Selected_Component
2419 and then Is_Overloaded (Selector_Name (N))
2420 then
2421 O_N := Selector_Name (N);
2422 else
2423 O_N := N;
2424 end if;
2425
2426 Int_Ind := Interp_Map.Get (O_N);
2427
2428 -- Procedure should never be called if the node has no interpretations
2429
2430 if Int_Ind < 0 then
2431 raise Program_Error;
2432 end if;
2433
2434 I := Int_Ind;
2435 It := All_Interp.Table (Int_Ind);
2436 end Get_First_Interp;
2437
2438 ---------------------
2439 -- Get_Next_Interp --
2440 ---------------------
2441
2442 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp) is
2443 begin
2444 I := I + 1;
2445 It := All_Interp.Table (I);
2446 end Get_Next_Interp;
2447
2448 -------------------------
2449 -- Has_Compatible_Type --
2450 -------------------------
2451
2452 function Has_Compatible_Type (N : Node_Id; Typ : Entity_Id) return Boolean
2453 is
2454 I : Interp_Index;
2455 It : Interp;
2456
2457 begin
2458 if N = Error then
2459 return False;
2460 end if;
2461
2462 if Nkind (N) = N_Subtype_Indication or else not Is_Overloaded (N) then
2463 if Covers (Typ, Etype (N))
2464
2465 -- Ada 2005 (AI-345): The context may be a synchronized interface.
2466 -- If the type is already frozen, use the corresponding_record to
2467 -- check whether it is a proper descendant.
2468
2469 or else
2470 (Is_Record_Type (Typ)
2471 and then Is_Concurrent_Type (Etype (N))
2472 and then Present (Corresponding_Record_Type (Etype (N)))
2473 and then Covers (Typ, Corresponding_Record_Type (Etype (N))))
2474
2475 or else
2476 (Is_Concurrent_Type (Typ)
2477 and then Is_Record_Type (Etype (N))
2478 and then Present (Corresponding_Record_Type (Typ))
2479 and then Covers (Corresponding_Record_Type (Typ), Etype (N)))
2480
2481 or else Is_User_Defined_Literal (N, Typ)
2482
2483 then
2484 return True;
2485 end if;
2486
2487 -- Overloaded case
2488
2489 else
2490 Get_First_Interp (N, I, It);
2491 while Present (It.Typ) loop
2492 if Covers (Typ, It.Typ)
2493
2494 -- Ada 2005 (AI-345)
2495
2496 or else
2497 (Is_Record_Type (Typ)
2498 and then Is_Concurrent_Type (It.Typ)
2499 and then Present (Corresponding_Record_Type (Etype (It.Typ)))
2500 and then
2501 Covers (Typ, Corresponding_Record_Type (Etype (It.Typ))))
2502
2503 or else
2504 (Is_Concurrent_Type (Typ)
2505 and then Is_Record_Type (It.Typ)
2506 and then Present (Corresponding_Record_Type (Typ))
2507 and then
2508 Covers (Corresponding_Record_Type (Typ), Etype (It.Typ)))
2509
2510 then
2511 return True;
2512 end if;
2513
2514 Get_Next_Interp (I, It);
2515 end loop;
2516 end if;
2517
2518 return False;
2519 end Has_Compatible_Type;
2520
2521 ---------------------
2522 -- Has_Abstract_Op --
2523 ---------------------
2524
2525 function Has_Abstract_Op
2526 (N : Node_Id;
2527 Typ : Entity_Id) return Entity_Id
2528 is
2529 I : Interp_Index;
2530 It : Interp;
2531
2532 begin
2533 if Is_Overloaded (N) then
2534 Get_First_Interp (N, I, It);
2535 while Present (It.Nam) loop
2536 if Present (It.Abstract_Op)
2537 and then Etype (It.Abstract_Op) = Typ
2538 then
2539 return It.Abstract_Op;
2540 end if;
2541
2542 Get_Next_Interp (I, It);
2543 end loop;
2544 end if;
2545
2546 return Empty;
2547 end Has_Abstract_Op;
2548
2549 ----------
2550 -- Hash --
2551 ----------
2552
2553 function Hash (N : Node_Id) return Header_Num is
2554 begin
2555 return Header_Num (N mod Header_Max);
2556 end Hash;
2557
2558 --------------
2559 -- Hides_Op --
2560 --------------
2561
2562 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean is
2563 Btyp : constant Entity_Id := Base_Type (Etype (First_Formal (F)));
2564 begin
2565 return Operator_Matches_Spec (Op, F)
2566 and then (In_Open_Scopes (Scope (F))
2567 or else Scope (F) = Scope (Btyp)
2568 or else (not In_Open_Scopes (Scope (Btyp))
2569 and then not In_Use (Btyp)
2570 and then not In_Use (Scope (Btyp))));
2571 end Hides_Op;
2572
2573 ------------------------
2574 -- Init_Interp_Tables --
2575 ------------------------
2576
2577 procedure Init_Interp_Tables is
2578 begin
2579 All_Interp.Init;
2580 Interp_Map.Reset;
2581 end Init_Interp_Tables;
2582
2583 -----------------------------------
2584 -- Interface_Present_In_Ancestor --
2585 -----------------------------------
2586
2587 function Interface_Present_In_Ancestor
2588 (Typ : Entity_Id;
2589 Iface : Entity_Id) return Boolean
2590 is
2591 Target_Typ : Entity_Id;
2592 Iface_Typ : Entity_Id;
2593
2594 function Iface_Present_In_Ancestor (Typ : Entity_Id) return Boolean;
2595 -- Returns True if Typ or some ancestor of Typ implements Iface
2596
2597 -------------------------------
2598 -- Iface_Present_In_Ancestor --
2599 -------------------------------
2600
2601 function Iface_Present_In_Ancestor (Typ : Entity_Id) return Boolean is
2602 E : Entity_Id;
2603 AI : Entity_Id;
2604 Elmt : Elmt_Id;
2605
2606 begin
2607 if Typ = Iface_Typ then
2608 return True;
2609 end if;
2610
2611 -- Handle private types
2612
2613 if Present (Full_View (Typ))
2614 and then not Is_Concurrent_Type (Full_View (Typ))
2615 then
2616 E := Full_View (Typ);
2617 else
2618 E := Typ;
2619 end if;
2620
2621 loop
2622 if Is_Record_Type (E)
2623 and then Present (Interfaces (E))
2624 then
2625 Elmt := First_Elmt (Interfaces (E));
2626 while Present (Elmt) loop
2627 AI := Node (Elmt);
2628
2629 if AI = Iface_Typ or else Is_Ancestor (Iface_Typ, AI) then
2630 return True;
2631 end if;
2632
2633 Next_Elmt (Elmt);
2634 end loop;
2635 end if;
2636
2637 exit when Etype (E) = E
2638
2639 -- Handle private types
2640
2641 or else (Present (Full_View (Etype (E)))
2642 and then Full_View (Etype (E)) = E);
2643
2644 -- Check if the current type is a direct derivation of the
2645 -- interface
2646
2647 if Etype (E) = Iface_Typ then
2648 return True;
2649 end if;
2650
2651 -- Climb to the immediate ancestor handling private types
2652
2653 if Present (Full_View (Etype (E))) then
2654 E := Full_View (Etype (E));
2655 else
2656 E := Etype (E);
2657 end if;
2658 end loop;
2659
2660 return False;
2661 end Iface_Present_In_Ancestor;
2662
2663 -- Start of processing for Interface_Present_In_Ancestor
2664
2665 begin
2666 -- Iface might be a class-wide subtype, so we have to apply Base_Type
2667
2668 if Is_Class_Wide_Type (Iface) then
2669 Iface_Typ := Etype (Base_Type (Iface));
2670 else
2671 Iface_Typ := Iface;
2672 end if;
2673
2674 -- Handle subtypes
2675
2676 Iface_Typ := Base_Type (Iface_Typ);
2677
2678 if Is_Access_Type (Typ) then
2679 Target_Typ := Etype (Directly_Designated_Type (Typ));
2680 else
2681 Target_Typ := Typ;
2682 end if;
2683
2684 if Is_Concurrent_Record_Type (Target_Typ) then
2685 Target_Typ := Corresponding_Concurrent_Type (Target_Typ);
2686 end if;
2687
2688 Target_Typ := Base_Type (Target_Typ);
2689
2690 -- In case of concurrent types we can't use the Corresponding Record_Typ
2691 -- to look for the interface because it is built by the expander (and
2692 -- hence it is not always available). For this reason we traverse the
2693 -- list of interfaces (available in the parent of the concurrent type).
2694
2695 if Is_Concurrent_Type (Target_Typ) then
2696 declare
2697 AI : Node_Id;
2698
2699 begin
2700 AI := First (Interface_List (Parent (Target_Typ)));
2701
2702 -- The progenitor itself may be a subtype of an interface type
2703
2704 while Present (AI) loop
2705 if Etype (AI) = Iface_Typ
2706 or else Base_Type (Etype (AI)) = Iface_Typ
2707 then
2708 return True;
2709
2710 elsif Present (Interfaces (Etype (AI)))
2711 and then Iface_Present_In_Ancestor (Etype (AI))
2712 then
2713 return True;
2714 end if;
2715
2716 Next (AI);
2717 end loop;
2718 end;
2719
2720 return False;
2721 end if;
2722
2723 if Is_Class_Wide_Type (Target_Typ) then
2724 Target_Typ := Etype (Target_Typ);
2725 end if;
2726
2727 if Ekind (Target_Typ) = E_Incomplete_Type then
2728
2729 -- We must have either a full view or a nonlimited view of the type
2730 -- to locate the list of ancestors.
2731
2732 if Present (Full_View (Target_Typ)) then
2733 Target_Typ := Full_View (Target_Typ);
2734 else
2735 -- In a spec expression or in an expression function, the use of
2736 -- an incomplete type is legal; legality of the conversion will be
2737 -- checked at freeze point of related entity.
2738
2739 if In_Spec_Expression then
2740 return True;
2741
2742 else
2743 pragma Assert (Present (Non_Limited_View (Target_Typ)));
2744 Target_Typ := Non_Limited_View (Target_Typ);
2745 end if;
2746 end if;
2747
2748 -- Protect the front end against previously detected errors
2749
2750 if Ekind (Target_Typ) = E_Incomplete_Type then
2751 return False;
2752 end if;
2753 end if;
2754
2755 return Iface_Present_In_Ancestor (Target_Typ);
2756 end Interface_Present_In_Ancestor;
2757
2758 ---------------------
2759 -- Intersect_Types --
2760 ---------------------
2761
2762 function Intersect_Types (L, R : Node_Id) return Entity_Id is
2763 Index : Interp_Index;
2764 It : Interp;
2765 Typ : Entity_Id;
2766
2767 function Check_Right_Argument (T : Entity_Id) return Entity_Id;
2768 -- Find interpretation of right arg that has type compatible with T
2769
2770 --------------------------
2771 -- Check_Right_Argument --
2772 --------------------------
2773
2774 function Check_Right_Argument (T : Entity_Id) return Entity_Id is
2775 Index : Interp_Index;
2776 It : Interp;
2777 T2 : Entity_Id;
2778
2779 begin
2780 if not Is_Overloaded (R) then
2781 return Specific_Type (T, Etype (R));
2782
2783 else
2784 Get_First_Interp (R, Index, It);
2785 loop
2786 T2 := Specific_Type (T, It.Typ);
2787
2788 if T2 /= Any_Type then
2789 return T2;
2790 end if;
2791
2792 Get_Next_Interp (Index, It);
2793 exit when No (It.Typ);
2794 end loop;
2795
2796 return Any_Type;
2797 end if;
2798 end Check_Right_Argument;
2799
2800 -- Start of processing for Intersect_Types
2801
2802 begin
2803 if Etype (L) = Any_Type or else Etype (R) = Any_Type then
2804 return Any_Type;
2805 end if;
2806
2807 if not Is_Overloaded (L) then
2808 Typ := Check_Right_Argument (Etype (L));
2809
2810 else
2811 Typ := Any_Type;
2812 Get_First_Interp (L, Index, It);
2813 while Present (It.Typ) loop
2814 Typ := Check_Right_Argument (It.Typ);
2815 exit when Typ /= Any_Type;
2816 Get_Next_Interp (Index, It);
2817 end loop;
2818
2819 end if;
2820
2821 -- If Typ is Any_Type, it means no compatible pair of types was found
2822
2823 if Typ = Any_Type then
2824 if Nkind (Parent (L)) in N_Op then
2825 Error_Msg_N ("incompatible types for operator", Parent (L));
2826
2827 elsif Nkind (Parent (L)) = N_Range then
2828 Error_Msg_N ("incompatible types given in constraint", Parent (L));
2829
2830 -- Ada 2005 (AI-251): Complete the error notification
2831
2832 elsif Is_Class_Wide_Type (Etype (R))
2833 and then Is_Interface (Etype (Class_Wide_Type (Etype (R))))
2834 then
2835 Error_Msg_NE ("(Ada 2005) does not implement interface }",
2836 L, Etype (Class_Wide_Type (Etype (R))));
2837
2838 -- Specialize message if one operand is a limited view, a priori
2839 -- unrelated to all other types.
2840
2841 elsif From_Limited_With (Etype (R)) then
2842 Error_Msg_NE ("limited view of& not compatible with context",
2843 R, Etype (R));
2844
2845 elsif From_Limited_With (Etype (L)) then
2846 Error_Msg_NE ("limited view of& not compatible with context",
2847 L, Etype (L));
2848 else
2849 Error_Msg_N ("incompatible types", Parent (L));
2850 end if;
2851 end if;
2852
2853 return Typ;
2854 end Intersect_Types;
2855
2856 -----------------------
2857 -- In_Generic_Actual --
2858 -----------------------
2859
2860 function In_Generic_Actual (Exp : Node_Id) return Boolean is
2861 Par : constant Node_Id := Parent (Exp);
2862
2863 begin
2864 if No (Par) then
2865 return False;
2866
2867 elsif Nkind (Par) in N_Declaration then
2868 return
2869 Nkind (Par) = N_Object_Declaration
2870 and then Present (Corresponding_Generic_Association (Par));
2871
2872 elsif Nkind (Par) = N_Object_Renaming_Declaration then
2873 return Present (Corresponding_Generic_Association (Par));
2874
2875 elsif Nkind (Par) in N_Statement_Other_Than_Procedure_Call then
2876 return False;
2877
2878 else
2879 return In_Generic_Actual (Par);
2880 end if;
2881 end In_Generic_Actual;
2882
2883 -----------------
2884 -- Is_Ancestor --
2885 -----------------
2886
2887 function Is_Ancestor
2888 (T1 : Entity_Id;
2889 T2 : Entity_Id;
2890 Use_Full_View : Boolean := False) return Boolean
2891 is
2892 BT1 : Entity_Id;
2893 BT2 : Entity_Id;
2894 Par : Entity_Id;
2895
2896 begin
2897 BT1 := Base_Type (T1);
2898 BT2 := Base_Type (T2);
2899
2900 -- Handle underlying view of records with unknown discriminants using
2901 -- the original entity that motivated the construction of this
2902 -- underlying record view (see Build_Derived_Private_Type).
2903
2904 if Is_Underlying_Record_View (BT1) then
2905 BT1 := Underlying_Record_View (BT1);
2906 end if;
2907
2908 if Is_Underlying_Record_View (BT2) then
2909 BT2 := Underlying_Record_View (BT2);
2910 end if;
2911
2912 if BT1 = BT2 then
2913 return True;
2914
2915 -- The predicate must look past privacy
2916
2917 elsif Is_Private_Type (T1)
2918 and then Present (Full_View (T1))
2919 and then BT2 = Base_Type (Full_View (T1))
2920 then
2921 return True;
2922
2923 elsif Is_Private_Type (T2)
2924 and then Present (Full_View (T2))
2925 and then BT1 = Base_Type (Full_View (T2))
2926 then
2927 return True;
2928
2929 else
2930 -- Obtain the parent of the base type of T2 (use the full view if
2931 -- allowed).
2932
2933 if Use_Full_View
2934 and then Is_Private_Type (BT2)
2935 and then Present (Full_View (BT2))
2936 then
2937 -- No climbing needed if its full view is the root type
2938
2939 if Full_View (BT2) = Root_Type (Full_View (BT2)) then
2940 return False;
2941 end if;
2942
2943 Par := Etype (Full_View (BT2));
2944
2945 else
2946 Par := Etype (BT2);
2947 end if;
2948
2949 loop
2950 -- If there was a error on the type declaration, do not recurse
2951
2952 if Error_Posted (Par) then
2953 return False;
2954
2955 elsif BT1 = Base_Type (Par)
2956 or else (Is_Private_Type (T1)
2957 and then Present (Full_View (T1))
2958 and then Base_Type (Par) = Base_Type (Full_View (T1)))
2959 then
2960 return True;
2961
2962 elsif Is_Private_Type (Par)
2963 and then Present (Full_View (Par))
2964 and then Full_View (Par) = BT1
2965 then
2966 return True;
2967
2968 -- Root type found
2969
2970 elsif Par = Root_Type (Par) then
2971 return False;
2972
2973 -- Continue climbing
2974
2975 else
2976 -- Use the full-view of private types (if allowed). Guard
2977 -- against infinite loops when full view has same type as
2978 -- parent, as can happen with interface extensions.
2979
2980 if Use_Full_View
2981 and then Is_Private_Type (Par)
2982 and then Present (Full_View (Par))
2983 and then Par /= Etype (Full_View (Par))
2984 then
2985 Par := Etype (Full_View (Par));
2986 else
2987 Par := Etype (Par);
2988 end if;
2989 end if;
2990 end loop;
2991 end if;
2992 end Is_Ancestor;
2993
2994 --------------------
2995 -- Is_Progenitor --
2996 --------------------
2997
2998 function Is_Progenitor
2999 (Iface : Entity_Id;
3000 Typ : Entity_Id) return Boolean
3001 is
3002 begin
3003 return Implements_Interface (Typ, Iface, Exclude_Parents => True);
3004 end Is_Progenitor;
3005
3006 -------------------
3007 -- Is_Subtype_Of --
3008 -------------------
3009
3010 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
3011 S : Entity_Id;
3012
3013 begin
3014 S := Ancestor_Subtype (T1);
3015 while Present (S) loop
3016 if S = T2 then
3017 return True;
3018 else
3019 S := Ancestor_Subtype (S);
3020 end if;
3021 end loop;
3022
3023 return False;
3024 end Is_Subtype_Of;
3025
3026 -------------------------
3027 -- Is_Visible_Operator --
3028 -------------------------
3029
3030 function Is_Visible_Operator (N : Node_Id; Typ : Entity_Id) return Boolean
3031 is
3032 begin
3033 -- The predefined operators of the universal types are always visible
3034
3035 if Typ in Universal_Integer | Universal_Real | Universal_Access then
3036 return True;
3037
3038 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
3039 -- anonymous access types in universal_access equality operators.
3040
3041 elsif Is_Anonymous_Access_Type (Typ) then
3042 return Ada_Version >= Ada_2005;
3043
3044 -- Similar reasoning for special types used for composite types before
3045 -- type resolution is done.
3046
3047 elsif Typ = Any_Composite or else Typ = Any_String then
3048 return True;
3049
3050 -- Within an instance, the predefined operators of the formal types are
3051 -- visible and, for the other types, the generic package declaration has
3052 -- already been successfully analyzed. Likewise for an inlined body.
3053
3054 elsif In_Instance or else In_Inlined_Body then
3055 return True;
3056
3057 -- If the operation is given in functional notation and the prefix is an
3058 -- expanded name, then the operator is visible if the prefix is the scope
3059 -- of the type, or System if the type is declared in an extension of it.
3060
3061 elsif Nkind (N) = N_Function_Call
3062 and then Nkind (Name (N)) = N_Expanded_Name
3063 then
3064 declare
3065 Pref : constant Entity_Id := Entity (Prefix (Name (N)));
3066 Scop : constant Entity_Id := Scope (Typ);
3067
3068 begin
3069 return Pref = Scop
3070 or else (Present (System_Aux_Id)
3071 and then Scop = System_Aux_Id
3072 and then Pref = Scope (Scop));
3073 end;
3074
3075 -- Otherwise the operator is visible when the type is visible
3076
3077 else
3078 return Is_Potentially_Use_Visible (Typ)
3079 or else In_Use (Typ)
3080 or else (In_Use (Scope (Typ)) and then not Is_Hidden (Typ))
3081 or else In_Open_Scopes (Scope (Typ));
3082 end if;
3083 end Is_Visible_Operator;
3084
3085 ------------------
3086 -- List_Interps --
3087 ------------------
3088
3089 procedure List_Interps (Nam : Node_Id; Err : Node_Id) is
3090 Index : Interp_Index;
3091 It : Interp;
3092
3093 begin
3094 Get_First_Interp (Nam, Index, It);
3095 while Present (It.Nam) loop
3096 if Scope (It.Nam) = Standard_Standard
3097 and then Scope (It.Typ) /= Standard_Standard
3098 then
3099 Error_Msg_Sloc := Sloc (Parent (It.Typ));
3100 Error_Msg_NE ("\\& (inherited) declared#!", Err, It.Nam);
3101
3102 else
3103 Error_Msg_Sloc := Sloc (It.Nam);
3104 Error_Msg_NE ("\\& declared#!", Err, It.Nam);
3105 end if;
3106
3107 Get_Next_Interp (Index, It);
3108 end loop;
3109 end List_Interps;
3110
3111 -----------------
3112 -- New_Interps --
3113 -----------------
3114
3115 procedure New_Interps (N : Node_Id) is
3116 begin
3117 All_Interp.Append (No_Interp);
3118
3119 -- Add or rewrite the existing node
3120 Last_Overloaded := N;
3121 Interp_Map.Set (N, All_Interp.Last);
3122 Set_Is_Overloaded (N, True);
3123 end New_Interps;
3124
3125 ---------------------------
3126 -- Operator_Matches_Spec --
3127 ---------------------------
3128
3129 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean is
3130 New_First_F : constant Entity_Id := First_Formal (New_S);
3131 Op_Name : constant Name_Id := Chars (Op);
3132 T : constant Entity_Id := Etype (New_S);
3133 New_F : Entity_Id;
3134 Num : Nat;
3135 Old_F : Entity_Id;
3136 T1 : Entity_Id;
3137 T2 : Entity_Id;
3138
3139 begin
3140 -- To verify that a predefined operator matches a given signature, do a
3141 -- case analysis of the operator classes. Function can have one or two
3142 -- formals and must have the proper result type.
3143
3144 New_F := New_First_F;
3145 Old_F := First_Formal (Op);
3146 Num := 0;
3147 while Present (New_F) and then Present (Old_F) loop
3148 Num := Num + 1;
3149 Next_Formal (New_F);
3150 Next_Formal (Old_F);
3151 end loop;
3152
3153 -- Definite mismatch if different number of parameters
3154
3155 if Present (Old_F) or else Present (New_F) then
3156 return False;
3157
3158 -- Unary operators
3159
3160 elsif Num = 1 then
3161 T1 := Etype (New_First_F);
3162
3163 if Op_Name in Name_Op_Subtract | Name_Op_Add | Name_Op_Abs then
3164 return Base_Type (T1) = Base_Type (T)
3165 and then Is_Numeric_Type (T);
3166
3167 elsif Op_Name = Name_Op_Not then
3168 return Base_Type (T1) = Base_Type (T)
3169 and then Valid_Boolean_Arg (Base_Type (T));
3170
3171 else
3172 return False;
3173 end if;
3174
3175 -- Binary operators
3176
3177 else
3178 T1 := Etype (New_First_F);
3179 T2 := Etype (Next_Formal (New_First_F));
3180
3181 if Op_Name in Name_Op_And | Name_Op_Or | Name_Op_Xor then
3182 return Base_Type (T1) = Base_Type (T2)
3183 and then Base_Type (T1) = Base_Type (T)
3184 and then Valid_Boolean_Arg (Base_Type (T));
3185
3186 elsif Op_Name in Name_Op_Eq | Name_Op_Ne then
3187 return Base_Type (T1) = Base_Type (T2)
3188 and then Valid_Equality_Arg (T1)
3189 and then Is_Boolean_Type (T);
3190
3191 elsif Op_Name in Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge
3192 then
3193 return Base_Type (T1) = Base_Type (T2)
3194 and then Valid_Comparison_Arg (T1)
3195 and then Is_Boolean_Type (T);
3196
3197 elsif Op_Name in Name_Op_Add | Name_Op_Subtract then
3198 return Base_Type (T1) = Base_Type (T2)
3199 and then Base_Type (T1) = Base_Type (T)
3200 and then Is_Numeric_Type (T);
3201
3202 -- For division and multiplication, a user-defined function does not
3203 -- match the predefined universal_fixed operation, except in Ada 83.
3204
3205 elsif Op_Name = Name_Op_Divide then
3206 return (Base_Type (T1) = Base_Type (T2)
3207 and then Base_Type (T1) = Base_Type (T)
3208 and then Is_Numeric_Type (T)
3209 and then (not Is_Fixed_Point_Type (T)
3210 or else Ada_Version = Ada_83))
3211
3212 -- Mixed_Mode operations on fixed-point types
3213
3214 or else (Base_Type (T1) = Base_Type (T)
3215 and then Base_Type (T2) = Base_Type (Standard_Integer)
3216 and then Is_Fixed_Point_Type (T))
3217
3218 -- A user defined operator can also match (and hide) a mixed
3219 -- operation on universal literals.
3220
3221 or else (Is_Integer_Type (T2)
3222 and then Is_Floating_Point_Type (T1)
3223 and then Base_Type (T1) = Base_Type (T));
3224
3225 elsif Op_Name = Name_Op_Multiply then
3226 return (Base_Type (T1) = Base_Type (T2)
3227 and then Base_Type (T1) = Base_Type (T)
3228 and then Is_Numeric_Type (T)
3229 and then (not Is_Fixed_Point_Type (T)
3230 or else Ada_Version = Ada_83))
3231
3232 -- Mixed_Mode operations on fixed-point types
3233
3234 or else (Base_Type (T1) = Base_Type (T)
3235 and then Base_Type (T2) = Base_Type (Standard_Integer)
3236 and then Is_Fixed_Point_Type (T))
3237
3238 or else (Base_Type (T2) = Base_Type (T)
3239 and then Base_Type (T1) = Base_Type (Standard_Integer)
3240 and then Is_Fixed_Point_Type (T))
3241
3242 or else (Is_Integer_Type (T2)
3243 and then Is_Floating_Point_Type (T1)
3244 and then Base_Type (T1) = Base_Type (T))
3245
3246 or else (Is_Integer_Type (T1)
3247 and then Is_Floating_Point_Type (T2)
3248 and then Base_Type (T2) = Base_Type (T));
3249
3250 elsif Op_Name in Name_Op_Mod | Name_Op_Rem then
3251 return Base_Type (T1) = Base_Type (T2)
3252 and then Base_Type (T1) = Base_Type (T)
3253 and then Is_Integer_Type (T);
3254
3255 elsif Op_Name = Name_Op_Expon then
3256 return Base_Type (T1) = Base_Type (T)
3257 and then Is_Numeric_Type (T)
3258 and then Base_Type (T2) = Base_Type (Standard_Integer);
3259
3260 elsif Op_Name = Name_Op_Concat then
3261 return Is_Array_Type (T)
3262 and then Base_Type (T) = Base_Type (Etype (Op))
3263 and then (Base_Type (T1) = Base_Type (T)
3264 or else
3265 Base_Type (T1) = Base_Type (Component_Type (T)))
3266 and then (Base_Type (T2) = Base_Type (T)
3267 or else
3268 Base_Type (T2) = Base_Type (Component_Type (T)));
3269
3270 else
3271 return False;
3272 end if;
3273 end if;
3274 end Operator_Matches_Spec;
3275
3276 -------------------
3277 -- Remove_Interp --
3278 -------------------
3279
3280 procedure Remove_Interp (I : in out Interp_Index) is
3281 II : Interp_Index;
3282
3283 begin
3284 -- Find end of interp list and copy downward to erase the discarded one
3285
3286 II := I + 1;
3287 while Present (All_Interp.Table (II).Typ) loop
3288 II := II + 1;
3289 end loop;
3290
3291 for J in I + 1 .. II loop
3292 All_Interp.Table (J - 1) := All_Interp.Table (J);
3293 end loop;
3294
3295 -- Back up interp index to insure that iterator will pick up next
3296 -- available interpretation.
3297
3298 I := I - 1;
3299 end Remove_Interp;
3300
3301 ------------------
3302 -- Save_Interps --
3303 ------------------
3304
3305 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id) is
3306 Old_Ind : Interp_Index;
3307 O_N : Node_Id;
3308
3309 begin
3310 if Is_Overloaded (Old_N) then
3311 Set_Is_Overloaded (New_N);
3312
3313 if Nkind (Old_N) = N_Selected_Component
3314 and then Is_Overloaded (Selector_Name (Old_N))
3315 then
3316 O_N := Selector_Name (Old_N);
3317 else
3318 O_N := Old_N;
3319 end if;
3320
3321 Old_Ind := Interp_Map.Get (O_N);
3322 pragma Assert (Old_Ind >= 0);
3323
3324 New_Interps (New_N);
3325 Interp_Map.Set (New_N, Old_Ind);
3326 end if;
3327 end Save_Interps;
3328
3329 -------------------
3330 -- Specific_Type --
3331 -------------------
3332
3333 function Specific_Type (Typ_1, Typ_2 : Entity_Id) return Entity_Id is
3334 T1 : constant Entity_Id := Available_View (Typ_1);
3335 T2 : constant Entity_Id := Available_View (Typ_2);
3336 B1 : constant Entity_Id := Base_Type (T1);
3337 B2 : constant Entity_Id := Base_Type (T2);
3338
3339 function Is_Remote_Access (T : Entity_Id) return Boolean;
3340 -- Check whether T is the equivalent type of a remote access type.
3341 -- If distribution is enabled, T is a legal context for Null.
3342
3343 ----------------------
3344 -- Is_Remote_Access --
3345 ----------------------
3346
3347 function Is_Remote_Access (T : Entity_Id) return Boolean is
3348 begin
3349 return Is_Record_Type (T)
3350 and then (Is_Remote_Call_Interface (T)
3351 or else Is_Remote_Types (T))
3352 and then Present (Corresponding_Remote_Type (T))
3353 and then Is_Access_Type (Corresponding_Remote_Type (T));
3354 end Is_Remote_Access;
3355
3356 -- Start of processing for Specific_Type
3357
3358 begin
3359 if T1 = Any_Type or else T2 = Any_Type then
3360 return Any_Type;
3361 end if;
3362
3363 if B1 = B2 then
3364 return B1;
3365
3366 elsif (T1 = Universal_Integer and then Is_Integer_Type (T2))
3367 or else (T1 = Universal_Real and then Is_Real_Type (T2))
3368 or else (T1 = Universal_Fixed and then Is_Fixed_Point_Type (T2))
3369 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
3370 or else (T1 = Any_Modular and then Is_Modular_Integer_Type (T2))
3371 or else (T1 = Any_Character and then Is_Character_Type (T2))
3372 or else (T1 = Any_String and then Is_String_Type (T2))
3373 or else (T1 = Any_Composite and then Is_Aggregate_Type (T2))
3374 or else (Ada_Version >= Ada_2022
3375 and then T1 = Any_Composite
3376 and then Has_Aspect (T2, Aspect_Aggregate))
3377 then
3378 return B2;
3379
3380 elsif (T1 = Universal_Access
3381 or else Ekind (T1) in E_Allocator_Type | E_Access_Attribute_Type)
3382 and then (Is_Access_Type (T2) or else Is_Remote_Access (T2))
3383 then
3384 return B2;
3385
3386 elsif T1 = Raise_Type then
3387 return B2;
3388
3389 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
3390 or else (T2 = Universal_Real and then Is_Real_Type (T1))
3391 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
3392 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
3393 or else (T2 = Any_Modular and then Is_Modular_Integer_Type (T1))
3394 or else (T2 = Any_Character and then Is_Character_Type (T1))
3395 or else (T2 = Any_String and then Is_String_Type (T1))
3396 or else (T2 = Any_Composite and then Is_Aggregate_Type (T1))
3397 or else (Ada_Version >= Ada_2022
3398 and then T2 = Any_Composite
3399 and then Has_Aspect (T1, Aspect_Aggregate))
3400 then
3401 return B1;
3402
3403 elsif (T2 = Universal_Access
3404 or else Ekind (T2) in E_Allocator_Type | E_Access_Attribute_Type)
3405 and then (Is_Access_Type (T1) or else Is_Remote_Access (T1))
3406 then
3407 return B1;
3408
3409 elsif T2 = Raise_Type then
3410 return B1;
3411
3412 -- Ada 2005 (AI-251): T1 and T2 are class-wide types, and T2 is an
3413 -- interface, return T1, and vice versa.
3414
3415 elsif Is_Class_Wide_Type (T1)
3416 and then Is_Class_Wide_Type (T2)
3417 and then Is_Interface (Etype (T2))
3418 then
3419 return B1;
3420
3421 elsif Is_Class_Wide_Type (T2)
3422 and then Is_Class_Wide_Type (T1)
3423 and then Is_Interface (Etype (T1))
3424 then
3425 return B2;
3426
3427 -- Ada 2005 (AI-251): T1 is a concrete type that implements the
3428 -- class-wide interface T2, return T1, and vice versa.
3429
3430 elsif Is_Tagged_Type (T1)
3431 and then Is_Class_Wide_Type (T2)
3432 and then Is_Interface (Etype (T2))
3433 and then Interface_Present_In_Ancestor (Typ => T1,
3434 Iface => Etype (T2))
3435 then
3436 return B1;
3437
3438 elsif Is_Tagged_Type (T2)
3439 and then Is_Class_Wide_Type (T1)
3440 and then Is_Interface (Etype (T1))
3441 and then Interface_Present_In_Ancestor (Typ => T2,
3442 Iface => Etype (T1))
3443 then
3444 return B2;
3445
3446 elsif Is_Class_Wide_Type (T1)
3447 and then Is_Ancestor (Root_Type (T1), T2)
3448 then
3449 return B1;
3450
3451 elsif Is_Class_Wide_Type (T2)
3452 and then Is_Ancestor (Root_Type (T2), T1)
3453 then
3454 return B2;
3455
3456 elsif Is_Access_Type (T1)
3457 and then Is_Access_Type (T2)
3458 and then Is_Class_Wide_Type (Designated_Type (T1))
3459 and then not Is_Class_Wide_Type (Designated_Type (T2))
3460 and then
3461 Is_Ancestor (Root_Type (Designated_Type (T1)), Designated_Type (T2))
3462 then
3463 return T1;
3464
3465 elsif Is_Access_Type (T1)
3466 and then Is_Access_Type (T2)
3467 and then Is_Class_Wide_Type (Designated_Type (T2))
3468 and then not Is_Class_Wide_Type (Designated_Type (T1))
3469 and then
3470 Is_Ancestor (Root_Type (Designated_Type (T2)), Designated_Type (T1))
3471 then
3472 return T2;
3473
3474 elsif Ekind (B1) in E_Access_Subprogram_Type
3475 | E_Access_Protected_Subprogram_Type
3476 and then Ekind (Designated_Type (B1)) /= E_Subprogram_Type
3477 and then Is_Access_Type (T2)
3478 then
3479 return T2;
3480
3481 elsif Ekind (B2) in E_Access_Subprogram_Type
3482 | E_Access_Protected_Subprogram_Type
3483 and then Ekind (Designated_Type (B2)) /= E_Subprogram_Type
3484 and then Is_Access_Type (T1)
3485 then
3486 return T1;
3487
3488 -- Ada 2005 (AI-230): Support the following operators:
3489
3490 -- function "=" (L, R : universal_access) return Boolean;
3491 -- function "/=" (L, R : universal_access) return Boolean;
3492
3493 -- Pool-specific access types (E_Access_Type) are not covered by these
3494 -- operators because of the legality rule of 4.5.2(9.2): "The operands
3495 -- of the equality operators for universal_access shall be convertible
3496 -- to one another (see 4.6)". For example, considering the type decla-
3497 -- ration "type P is access Integer" and an anonymous access to Integer,
3498 -- P is convertible to "access Integer" by 4.6 (24.11-24.15), but there
3499 -- is no rule in 4.6 that allows "access Integer" to be converted to P.
3500 -- Note that this does not preclude one operand to be a pool-specific
3501 -- access type, as a previous version of this code enforced.
3502
3503 elsif Is_Anonymous_Access_Type (T1)
3504 and then Is_Access_Type (T2)
3505 and then Ada_Version >= Ada_2005
3506 then
3507 return T1;
3508
3509 elsif Is_Anonymous_Access_Type (T2)
3510 and then Is_Access_Type (T1)
3511 and then Ada_Version >= Ada_2005
3512 then
3513 return T2;
3514
3515 -- With types exported from instantiation, also check private views the
3516 -- same way as Covers
3517
3518 elsif Is_Private_Type (T1) and then Is_Generic_Actual_Type (T2) then
3519 if Present (Full_View (T1)) then
3520 return Specific_Type (Full_View (T1), T2);
3521
3522 elsif Present (Underlying_Full_View (T1)) then
3523 return Specific_Type (Underlying_Full_View (T1), T2);
3524 end if;
3525
3526 elsif Is_Private_Type (T2) and then Is_Generic_Actual_Type (T1) then
3527 if Present (Full_View (T2)) then
3528 return Specific_Type (T1, Full_View (T2));
3529
3530 elsif Present (Underlying_Full_View (T2)) then
3531 return Specific_Type (T1, Underlying_Full_View (T2));
3532 end if;
3533 end if;
3534
3535 -- If none of the above cases applies, types are not compatible
3536
3537 return Any_Type;
3538 end Specific_Type;
3539
3540 ---------------------
3541 -- Set_Abstract_Op --
3542 ---------------------
3543
3544 procedure Set_Abstract_Op (I : Interp_Index; V : Entity_Id) is
3545 begin
3546 All_Interp.Table (I).Abstract_Op := V;
3547 end Set_Abstract_Op;
3548
3549 -----------------------
3550 -- Valid_Boolean_Arg --
3551 -----------------------
3552
3553 -- In addition to booleans and arrays of booleans, we must include
3554 -- aggregates as valid boolean arguments, because in the first pass of
3555 -- resolution their components are not examined. If it turns out not to be
3556 -- an aggregate of booleans, this will be diagnosed in Resolve.
3557 -- Any_Composite must be checked for prior to the array type checks because
3558 -- Any_Composite does not have any associated indexes.
3559
3560 function Valid_Boolean_Arg (T : Entity_Id) return Boolean is
3561 begin
3562 if Is_Boolean_Type (T)
3563 or else Is_Modular_Integer_Type (T)
3564 or else T = Universal_Integer
3565 or else T = Any_Composite
3566 or else T = Raise_Type
3567 then
3568 return True;
3569
3570 elsif Is_Array_Type (T)
3571 and then Number_Dimensions (T) = 1
3572 and then Is_Boolean_Type (Component_Type (T))
3573 and then
3574 ((not Is_Private_Composite (T) and then not Is_Limited_Composite (T))
3575 or else In_Instance
3576 or else Available_Full_View_Of_Component (T))
3577 then
3578 return True;
3579
3580 else
3581 return False;
3582 end if;
3583 end Valid_Boolean_Arg;
3584
3585 --------------------------
3586 -- Valid_Comparison_Arg --
3587 --------------------------
3588
3589 -- See above for the reason why aggregates and strings are included
3590
3591 function Valid_Comparison_Arg (T : Entity_Id) return Boolean is
3592 begin
3593 if Is_Discrete_Type (T) or else Is_Real_Type (T) then
3594 return True;
3595
3596 elsif T = Any_Composite or else T = Any_String then
3597 return True;
3598
3599 elsif Is_Array_Type (T)
3600 and then Number_Dimensions (T) = 1
3601 and then Is_Discrete_Type (Component_Type (T))
3602 and then (not Is_Private_Composite (T) or else In_Instance)
3603 and then (not Is_Limited_Composite (T) or else In_Instance)
3604 then
3605 return True;
3606
3607 elsif Is_Array_Type (T)
3608 and then Number_Dimensions (T) = 1
3609 and then Is_Discrete_Type (Component_Type (T))
3610 and then Available_Full_View_Of_Component (T)
3611 then
3612 return True;
3613
3614 elsif Is_String_Type (T) then
3615 return True;
3616
3617 else
3618 return False;
3619 end if;
3620 end Valid_Comparison_Arg;
3621
3622 ------------------------
3623 -- Valid_Equality_Arg --
3624 ------------------------
3625
3626 -- Same reasoning as above but implicit because of the nonlimited test
3627
3628 function Valid_Equality_Arg (T : Entity_Id) return Boolean is
3629 begin
3630 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
3631 -- anonymous access types in universal_access equality operators.
3632
3633 if Is_Anonymous_Access_Type (T) then
3634 return Ada_Version >= Ada_2005;
3635
3636 elsif Is_Incomplete_Type (T) then
3637 return False;
3638
3639 elsif not Is_Limited_Type (T) then
3640 return True;
3641
3642 elsif Is_Array_Type (T)
3643 and then not Is_Limited_Type (Component_Type (T))
3644 and then Available_Full_View_Of_Component (T)
3645 then
3646 return True;
3647
3648 else
3649 return False;
3650 end if;
3651 end Valid_Equality_Arg;
3652
3653 ------------------
3654 -- Write_Interp --
3655 ------------------
3656
3657 procedure Write_Interp (It : Interp) is
3658 begin
3659 Write_Str ("Nam: ");
3660 Print_Tree_Node (It.Nam);
3661 Write_Str ("Typ: ");
3662 Print_Tree_Node (It.Typ);
3663 Write_Str ("Abstract_Op: ");
3664 Print_Tree_Node (It.Abstract_Op);
3665 end Write_Interp;
3666
3667 ---------------------
3668 -- Write_Overloads --
3669 ---------------------
3670
3671 procedure Write_Overloads (N : Node_Id) is
3672 I : Interp_Index;
3673 It : Interp;
3674 Nam : Entity_Id;
3675
3676 begin
3677 Write_Str ("Overloads: ");
3678 Print_Node_Briefly (N);
3679
3680 if not Is_Overloaded (N) then
3681 if Is_Entity_Name (N) then
3682 Write_Line ("Non-overloaded entity ");
3683 Write_Entity_Info (Entity (N), " ");
3684 end if;
3685
3686 elsif Nkind (N) not in N_Has_Entity then
3687 Get_First_Interp (N, I, It);
3688 while Present (It.Nam) loop
3689 Write_Int (Int (It.Typ));
3690 Write_Str (" ");
3691 Write_Name (Chars (It.Typ));
3692 Write_Eol;
3693 Get_Next_Interp (I, It);
3694 end loop;
3695
3696 else
3697 Get_First_Interp (N, I, It);
3698 Write_Line ("Overloaded entity ");
3699 Write_Line (" Name Type Abstract Op");
3700 Write_Line ("===============================================");
3701 Nam := It.Nam;
3702
3703 while Present (Nam) loop
3704 Write_Int (Int (Nam));
3705 Write_Str (" ");
3706 Write_Name (Chars (Nam));
3707 Write_Str (" ");
3708 Write_Int (Int (It.Typ));
3709 Write_Str (" ");
3710 Write_Name (Chars (It.Typ));
3711
3712 if Present (It.Abstract_Op) then
3713 Write_Str (" ");
3714 Write_Int (Int (It.Abstract_Op));
3715 Write_Str (" ");
3716 Write_Name (Chars (It.Abstract_Op));
3717 end if;
3718
3719 Write_Eol;
3720 Get_Next_Interp (I, It);
3721 Nam := It.Nam;
3722 end loop;
3723 end if;
3724 end Write_Overloads;
3725
3726 end Sem_Type;