c++: contracts fixes
[official-gcc.git] / gcc / ada / sem_type.adb
blob718c29754c1a0d2b627b6e6da9ffd73135f162ed
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-2022, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 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;
57 with GNAT.HTable; use GNAT.HTable;
59 package body Sem_Type is
61 ---------------------
62 -- Data Structures --
63 ---------------------
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.
71 -- Interp_Map All_Interp
73 -- +-----+ +--------+
74 -- | | --->|interp1 |
75 -- |_____| | |interp2 |
76 -- |index|---------| |nointerp|
77 -- |-----| | |
78 -- | | | |
79 -- +-----+ +--------+
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???
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");
94 Header_Max : constant := 3079;
95 -- The number of hash buckets; an arbitrary prime number
97 subtype Header_Num is Integer range 0 .. Header_Max - 1;
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.
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 => "=");
111 Last_Overloaded : Node_Id := Empty;
112 -- Overloaded node after initializing a new collection of intepretation
114 -------------------------------------
115 -- Handling of Overload Resolution --
116 -------------------------------------
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.
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).
133 --------------------------
134 -- Operator Overloading --
135 --------------------------
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).
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.
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.
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 procedure All_Overloads;
163 pragma Warnings (Off, All_Overloads);
164 -- Debugging procedure: list full contents of Overloads table
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.
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.
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.
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.
195 --------------------
196 -- Add_One_Interp --
197 --------------------
199 procedure Add_One_Interp
200 (N : Node_Id;
201 E : Entity_Id;
202 T : Entity_Id;
203 Opnd_Type : Entity_Id := Empty)
205 Vis_Type : Entity_Id;
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.
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.
216 ---------------
217 -- Add_Entry --
218 ---------------
220 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id) is
221 Abstr_Op : Entity_Id := Empty;
222 I : Interp_Index;
223 It : Interp;
225 -- Start of processing for Add_Entry
227 begin
228 -- Find out whether the new entry references interpretations that
229 -- are abstract or disabled by abstract operators.
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;
241 Get_First_Interp (N, I, It);
242 while Present (It.Nam) loop
244 -- Avoid making duplicate entries in overloads
246 if Name = It.Nam
247 and then Base_Type (It.Typ) = Base_Type (T)
248 then
249 return;
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.
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;
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.
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;
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.
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;
299 else
300 All_Interp.Table (I).Nam := Name;
301 return;
302 end if;
304 -- Otherwise keep going
306 else
307 Get_Next_Interp (I, It);
308 end if;
309 end loop;
311 All_Interp.Table (All_Interp.Last) := (Name, Typ, Abstr_Op);
312 All_Interp.Append (No_Interp);
313 end Add_Entry;
315 ----------------------------
316 -- Is_Universal_Operation --
317 ----------------------------
319 function Is_Universal_Operation (Op : Entity_Id) return Boolean is
320 Arg : Node_Id;
322 begin
323 if Ekind (Op) /= E_Operator then
324 return False;
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;
341 elsif Nkind (N) in N_Unary_Op then
342 return Present (Universal_Interpretation (Right_Opnd (N)));
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;
351 Next_Actual (Arg);
352 end loop;
354 return True;
356 else
357 return False;
358 end if;
359 end Is_Universal_Operation;
361 -- Start of processing for Add_One_Interp
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.
371 if Ekind (E) = E_Operator then
372 if Present (Opnd_Type) then
373 Vis_Type := Opnd_Type;
374 else
375 Vis_Type := Base_Type (T);
376 end if;
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;
384 -- Save type for subsequent error message, in case no other
385 -- interpretation is found.
387 else
388 Candidate_Type := Vis_Type;
389 return;
390 end if;
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).
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;
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.
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.
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);
424 -- Otherwise this is the first interpretation, N has type Any_Type
425 -- and we must place the new type on the node.
427 else
428 Set_Etype (N, T);
429 end if;
431 return;
433 -- Calling stubs for an RACW operation never participate in resolution,
434 -- they are executed only through dispatching calls.
436 elsif Is_RACW_Stub_Type_Operation (E) then
437 return;
438 end if;
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.
446 if Etype (N) = Any_Type then
447 if Is_Type (E) then
448 Set_Etype (N, T);
450 else
451 -- Record both the operator or subprogram name, and its type
453 if Nkind (N) in N_Op or else Is_Entity_Name (N) then
454 Set_Entity (N, E);
455 end if;
457 Set_Etype (N, T);
458 end if;
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.
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);
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));
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));
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.
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;
494 Itn : Interp_Index;
495 pragma Warnings (Off, Itn);
497 begin
498 Get_First_Interp (Name (N), Itn, It);
499 Add_Entry (It.Nam, Etype (N));
500 end;
502 else
503 -- Overloaded prefix in indexed or selected component, or call
504 -- whose name is an expression or another call.
506 Add_Entry (Etype (N), Etype (N));
507 end if;
509 Add_Entry (E, T);
511 else
512 Add_Entry (E, T);
513 end if;
514 end Add_One_Interp;
516 -------------------
517 -- All_Overloads --
518 -------------------
520 procedure All_Overloads is
521 begin
522 for J in All_Interp.First .. All_Interp.Last loop
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;
531 Write_Str ("=================");
532 Write_Eol;
533 end loop;
534 end All_Overloads;
536 --------------------------------------
537 -- Binary_Op_Interp_Has_Abstract_Op --
538 --------------------------------------
540 function Binary_Op_Interp_Has_Abstract_Op
541 (N : Node_Id;
542 E : Entity_Id) return Entity_Id
544 Abstr_Op : Entity_Id;
545 E_Left : constant Node_Id := First_Formal (E);
546 E_Right : constant Node_Id := Next_Formal (E_Left);
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;
554 return Has_Abstract_Op (Right_Opnd (N), Etype (E_Right));
555 end Binary_Op_Interp_Has_Abstract_Op;
557 ---------------------
558 -- Collect_Interps --
559 ---------------------
561 procedure Collect_Interps (N : Node_Id) is
562 Ent : constant Entity_Id := Entity (N);
563 H : Entity_Id;
564 First_Interp : Interp_Index;
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.
573 ---------------------
574 -- Within_Instance --
575 ---------------------
577 function Within_Instance (E : Entity_Id) return Boolean is
578 Inst : Entity_Id;
579 Scop : Entity_Id;
581 begin
582 if not In_Instance then
583 return False;
584 end if;
586 Inst := Current_Scope;
587 while Present (Inst) and then not Is_Generic_Instance (Inst) loop
588 Inst := Scope (Inst);
589 end loop;
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;
597 Scop := Scope (Scop);
598 end loop;
600 return False;
601 end Within_Instance;
603 -- Start of processing for Collect_Interps
605 begin
606 New_Interps (N);
608 -- Unconditionally add the entity that was initially matched
610 First_Interp := All_Interp.Last;
611 Add_One_Interp (N, Ent, Etype (N));
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.
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;
624 H := Homonym (H);
625 end loop;
627 -- Case of direct name
629 else
630 -- First, search the homonym chain for directly visible entities
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);
638 if Is_Immediately_Visible (H) and then H /= Ent then
640 -- Only add interpretation if not hidden by an inner
641 -- immediately visible one.
643 for J in First_Interp .. All_Interp.Last - 1 loop
645 -- Current homograph is not hidden. Add to overloads
647 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
648 exit;
650 -- Homograph is hidden, unless it is a predefined operator
652 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
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.
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);
667 All_Interp.Append (No_Interp);
668 goto Next_Homograph;
670 elsif Scope (H) /= Standard_Standard then
671 goto Next_Homograph;
672 end if;
673 end if;
674 end loop;
676 -- On exit, we know that current homograph is not hidden
678 Add_One_Interp (N, H, Etype (H));
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;
687 <<Next_Homograph>>
688 H := Homonym (H);
689 end loop;
691 -- Scan list of homographs for use-visible entities only
693 H := Current_Entity (Ent);
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
702 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
703 exit;
705 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
706 goto Next_Use_Homograph;
707 end if;
708 end loop;
710 Add_One_Interp (N, H, Etype (H));
711 end if;
713 <<Next_Use_Homograph>>
714 H := Homonym (H);
715 end loop;
716 end if;
718 if All_Interp.Last = First_Interp + 1 then
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.
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;
732 ------------
733 -- Covers --
734 ------------
736 function Covers (T1, T2 : Entity_Id) return Boolean is
737 BT1 : Entity_Id;
738 BT2 : Entity_Id;
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.
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.
752 ----------------------
753 -- Full_View_Covers --
754 ----------------------
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;
763 elsif Present (Underlying_Full_View (Typ1))
764 and then Covers (Underlying_Full_View (Typ1), Typ2)
765 then
766 return True;
768 else
769 return False;
770 end if;
771 end Full_View_Covers;
773 -----------------
774 -- Real_Actual --
775 -----------------
777 function Real_Actual (T : Entity_Id) return Entity_Id is
778 Par : constant Node_Id := Parent (T);
779 RA : Entity_Id;
781 begin
782 -- Retrieve parent subtype from subtype declaration for actual
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));
790 if Is_Generic_Actual_Type (RA) then
791 return RA;
792 end if;
793 end if;
795 -- Otherwise actual is not the actual of an enclosing instance
797 return T;
798 end Real_Actual;
800 -- Start of processing for Covers
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.
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;
815 -- Trivial case: same types are always compatible
817 if T1 = T2 then
818 return True;
819 end if;
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.
826 if T1 = Standard_Void_Type or else T2 = Standard_Void_Type then
827 return False;
828 end if;
830 BT1 := Base_Type (T1);
831 BT2 := Base_Type (T2);
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).
837 if Is_Underlying_Record_View (BT1) then
838 BT1 := Underlying_Record_View (BT1);
839 end if;
841 if Is_Underlying_Record_View (BT2) then
842 BT2 := Underlying_Record_View (BT2);
843 end if;
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 ???)
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.
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;
871 -- Both T1 and T2 are generic actual types
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;
887 -- Literals are compatible with types in a given "class"
889 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
890 or else (T2 = Universal_Real and then Is_Real_Type (T1))
891 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
892 or else (T2 = Universal_Access and then Is_Access_Type (T1))
893 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
894 or else (T2 = Any_Character and then Is_Character_Type (T1))
895 or else (T2 = Any_String and then Is_String_Type (T1))
896 then
897 return True;
899 -- The context may be class wide, and a class-wide type is compatible
900 -- with any member of the class.
902 elsif Is_Class_Wide_Type (T1)
903 and then Is_Ancestor (Root_Type (T1), T2)
904 then
905 return True;
907 elsif Is_Class_Wide_Type (T1)
908 and then Is_Class_Wide_Type (T2)
909 and then Base_Type (Etype (T1)) = Base_Type (Etype (T2))
910 then
911 return True;
913 -- Ada 2005 (AI-345): A class-wide abstract interface type covers a
914 -- task_type or protected_type that implements the interface.
916 elsif Ada_Version >= Ada_2005
917 and then Is_Concurrent_Type (T2)
918 and then Is_Class_Wide_Type (T1)
919 and then Is_Interface (Etype (T1))
920 and then Interface_Present_In_Ancestor
921 (Typ => BT2, Iface => Etype (T1))
922 then
923 return True;
925 -- Ada 2005 (AI-251): A class-wide abstract interface type T1 covers an
926 -- object T2 implementing T1.
928 elsif Ada_Version >= Ada_2005
929 and then Is_Tagged_Type (T2)
930 and then Is_Class_Wide_Type (T1)
931 and then Is_Interface (Etype (T1))
932 then
933 if Interface_Present_In_Ancestor (Typ => T2,
934 Iface => Etype (T1))
935 then
936 return True;
937 end if;
939 declare
940 E : Entity_Id;
941 Elmt : Elmt_Id;
943 begin
944 if Is_Concurrent_Type (BT2) then
945 E := Corresponding_Record_Type (BT2);
946 else
947 E := BT2;
948 end if;
950 -- Ada 2005 (AI-251): A class-wide abstract interface type T1
951 -- covers an object T2 that implements a direct derivation of T1.
952 -- Note: test for presence of E is defense against previous error.
954 if No (E) then
955 Check_Error_Detected;
957 -- Here we have a corresponding record type
959 elsif Present (Interfaces (E)) then
960 Elmt := First_Elmt (Interfaces (E));
961 while Present (Elmt) loop
962 if Is_Ancestor (Etype (T1), Node (Elmt)) then
963 return True;
964 else
965 Next_Elmt (Elmt);
966 end if;
967 end loop;
968 end if;
970 -- We should also check the case in which T1 is an ancestor of
971 -- some implemented interface???
973 return False;
974 end;
976 -- In a dispatching call, the formal is of some specific type, and the
977 -- actual is of the corresponding class-wide type, including a subtype
978 -- of the class-wide type.
980 elsif Is_Class_Wide_Type (T2)
981 and then
982 (Class_Wide_Type (T1) = Class_Wide_Type (T2)
983 or else Base_Type (Root_Type (T2)) = BT1)
984 then
985 return True;
987 -- Some contexts require a class of types rather than a specific type.
988 -- For example, conditions require any boolean type, fixed point
989 -- attributes require some real type, etc. The built-in types Any_XXX
990 -- represent these classes.
992 elsif (T1 = Any_Integer and then Is_Integer_Type (T2))
993 or else (T1 = Any_Boolean and then Is_Boolean_Type (T2))
994 or else (T1 = Any_Real and then Is_Real_Type (T2))
995 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
996 or else (T1 = Any_Discrete and then Is_Discrete_Type (T2))
997 then
998 return True;
1000 -- An aggregate is compatible with an array or record type
1002 elsif T2 = Any_Composite and then Is_Aggregate_Type (T1) then
1003 return True;
1005 -- In Ada_2022, an aggregate is compatible with the type that
1006 -- as the corresponding aspect.
1008 elsif Ada_Version >= Ada_2022
1009 and then T2 = Any_Composite
1010 and then Has_Aspect (T1, Aspect_Aggregate)
1011 then
1012 return True;
1014 -- If the expected type is an anonymous access, the designated type must
1015 -- cover that of the expression. Use the base type for this check: even
1016 -- though access subtypes are rare in sources, they are generated for
1017 -- actuals in instantiations.
1019 elsif Ekind (BT1) = E_Anonymous_Access_Type
1020 and then Is_Access_Type (T2)
1021 and then Covers (Designated_Type (T1), Designated_Type (T2))
1022 then
1023 return True;
1025 -- Ada 2012 (AI05-0149): Allow an anonymous access type in the context
1026 -- of a named general access type. An implicit conversion will be
1027 -- applied. For the resolution, the designated types must match if
1028 -- untagged; further, if the designated type is tagged, the designated
1029 -- type of the anonymous access type shall be covered by the designated
1030 -- type of the named access type.
1032 elsif Ada_Version >= Ada_2012
1033 and then Ekind (BT1) = E_General_Access_Type
1034 and then Ekind (BT2) = E_Anonymous_Access_Type
1035 and then Covers (Designated_Type (T1), Designated_Type (T2))
1036 and then (Is_Class_Wide_Type (Designated_Type (T1)) >=
1037 Is_Class_Wide_Type (Designated_Type (T2)))
1038 then
1039 return True;
1041 -- An Access_To_Subprogram is compatible with itself, or with an
1042 -- anonymous type created for an attribute reference Access.
1044 elsif Ekind (BT1) in E_Access_Subprogram_Type
1045 | E_Access_Protected_Subprogram_Type
1046 and then Is_Access_Type (T2)
1047 and then (not Comes_From_Source (T1)
1048 or else not Comes_From_Source (T2))
1049 and then (Is_Overloadable (Designated_Type (T2))
1050 or else Ekind (Designated_Type (T2)) = E_Subprogram_Type)
1051 and then Type_Conformant (Designated_Type (T1), Designated_Type (T2))
1052 and then Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
1053 then
1054 return True;
1056 -- Ada 2005 (AI-254): An Anonymous_Access_To_Subprogram is compatible
1057 -- with itself, or with an anonymous type created for an attribute
1058 -- reference Access.
1060 elsif Ekind (BT1) in E_Anonymous_Access_Subprogram_Type
1061 | E_Anonymous_Access_Protected_Subprogram_Type
1062 and then Is_Access_Type (T2)
1063 and then (not Comes_From_Source (T1)
1064 or else not Comes_From_Source (T2))
1065 and then (Is_Overloadable (Designated_Type (T2))
1066 or else Ekind (Designated_Type (T2)) = E_Subprogram_Type)
1067 and then Type_Conformant (Designated_Type (T1), Designated_Type (T2))
1068 and then Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
1069 then
1070 return True;
1072 -- The context can be a remote access type, and the expression the
1073 -- corresponding source type declared in a categorized package, or
1074 -- vice versa.
1076 elsif Is_Record_Type (T1)
1077 and then (Is_Remote_Call_Interface (T1) or else Is_Remote_Types (T1))
1078 and then Present (Corresponding_Remote_Type (T1))
1079 then
1080 return Covers (Corresponding_Remote_Type (T1), T2);
1082 -- and conversely.
1084 elsif Is_Record_Type (T2)
1085 and then (Is_Remote_Call_Interface (T2) or else Is_Remote_Types (T2))
1086 and then Present (Corresponding_Remote_Type (T2))
1087 then
1088 return Covers (Corresponding_Remote_Type (T2), T1);
1090 -- Synchronized types are represented at run time by their corresponding
1091 -- record type. During expansion one is replaced with the other, but
1092 -- they are compatible views of the same type.
1094 elsif Is_Record_Type (T1)
1095 and then Is_Concurrent_Type (T2)
1096 and then Present (Corresponding_Record_Type (T2))
1097 then
1098 return Covers (T1, Corresponding_Record_Type (T2));
1100 elsif Is_Concurrent_Type (T1)
1101 and then Present (Corresponding_Record_Type (T1))
1102 and then Is_Record_Type (T2)
1103 then
1104 return Covers (Corresponding_Record_Type (T1), T2);
1106 -- During analysis, an attribute reference 'Access has a special type
1107 -- kind: Access_Attribute_Type, to be replaced eventually with the type
1108 -- imposed by context.
1110 elsif Ekind (T2) = E_Access_Attribute_Type
1111 and then Ekind (BT1) in E_General_Access_Type | E_Access_Type
1112 and then Covers (Designated_Type (T1), Designated_Type (T2))
1113 then
1114 -- If the target type is a RACW type while the source is an access
1115 -- attribute type, we are building a RACW that may be exported.
1117 if Is_Remote_Access_To_Class_Wide_Type (BT1) then
1118 Set_Has_RACW (Current_Sem_Unit);
1119 end if;
1121 return True;
1123 -- Ditto for allocators, which eventually resolve to the context type
1125 elsif Ekind (T2) = E_Allocator_Type and then Is_Access_Type (T1) then
1126 return Covers (Designated_Type (T1), Designated_Type (T2))
1127 or else
1128 (From_Limited_With (Designated_Type (T1))
1129 and then Covers (Designated_Type (T2), Designated_Type (T1)));
1131 -- A boolean operation on integer literals is compatible with modular
1132 -- context.
1134 elsif T2 = Any_Modular and then Is_Modular_Integer_Type (T1) then
1135 return True;
1137 -- The actual type may be the result of a previous error
1139 elsif BT2 = Any_Type then
1140 return True;
1142 -- A Raise_Expressions is legal in any expression context
1144 elsif BT2 = Raise_Type then
1145 return True;
1147 -- A packed array type covers its corresponding non-packed type. This is
1148 -- not legitimate Ada, but allows the omission of a number of otherwise
1149 -- useless unchecked conversions, and since this can only arise in
1150 -- (known correct) expanded code, no harm is done.
1152 elsif Is_Packed_Array (T2)
1153 and then T1 = Packed_Array_Impl_Type (T2)
1154 then
1155 return True;
1157 -- Similarly an array type covers its corresponding packed array type
1159 elsif Is_Packed_Array (T1)
1160 and then T2 = Packed_Array_Impl_Type (T1)
1161 then
1162 return True;
1164 -- In instances, or with types exported from instantiations, check
1165 -- whether a partial and a full view match. Verify that types are
1166 -- legal, to prevent cascaded errors.
1168 elsif Is_Private_Type (T1)
1169 and then (In_Instance
1170 or else (Is_Type (T2) and then Is_Generic_Actual_Type (T2)))
1171 and then Full_View_Covers (T1, T2)
1172 then
1173 return True;
1175 elsif Is_Private_Type (T2)
1176 and then (In_Instance
1177 or else (Is_Type (T1) and then Is_Generic_Actual_Type (T1)))
1178 and then Full_View_Covers (T2, T1)
1179 then
1180 return True;
1182 -- In the expansion of inlined bodies, types are compatible if they
1183 -- are structurally equivalent.
1185 elsif In_Inlined_Body
1186 and then (Underlying_Type (T1) = Underlying_Type (T2)
1187 or else
1188 (Is_Access_Type (T1)
1189 and then Is_Access_Type (T2)
1190 and then Designated_Type (T1) = Designated_Type (T2))
1191 or else
1192 (T1 = Universal_Access
1193 and then Is_Access_Type (Underlying_Type (T2)))
1194 or else
1195 (T2 = Any_Composite
1196 and then Is_Composite_Type (Underlying_Type (T1))))
1197 then
1198 return True;
1200 -- Ada 2005 (AI-50217): Additional branches to make the shadow entity
1201 -- obtained through a limited_with compatible with its real entity.
1203 elsif From_Limited_With (T1) then
1205 -- If the expected type is the nonlimited view of a type, the
1206 -- expression may have the limited view. If that one in turn is
1207 -- incomplete, get full view if available.
1209 return Has_Non_Limited_View (T1)
1210 and then Covers (Get_Full_View (Non_Limited_View (T1)), T2);
1212 elsif From_Limited_With (T2) then
1214 -- If units in the context have Limited_With clauses on each other,
1215 -- either type might have a limited view. Checks performed elsewhere
1216 -- verify that the context type is the nonlimited view.
1218 return Has_Non_Limited_View (T2)
1219 and then Covers (T1, Get_Full_View (Non_Limited_View (T2)));
1221 -- Ada 2005 (AI-412): Coverage for regular incomplete subtypes
1223 elsif Ekind (T1) = E_Incomplete_Subtype then
1224 return Covers (Full_View (Etype (T1)), T2);
1226 elsif Ekind (T2) = E_Incomplete_Subtype then
1227 return Covers (T1, Full_View (Etype (T2)));
1229 -- Ada 2005 (AI-423): Coverage of formal anonymous access types
1230 -- and actual anonymous access types in the context of generic
1231 -- instantiations. We have the following situation:
1233 -- generic
1234 -- type Formal is private;
1235 -- Formal_Obj : access Formal; -- T1
1236 -- package G is ...
1238 -- package P is
1239 -- type Actual is ...
1240 -- Actual_Obj : access Actual; -- T2
1241 -- package Instance is new G (Formal => Actual,
1242 -- Formal_Obj => Actual_Obj);
1244 elsif Ada_Version >= Ada_2005
1245 and then Is_Anonymous_Access_Type (T1)
1246 and then Is_Anonymous_Access_Type (T2)
1247 and then Is_Generic_Type (Directly_Designated_Type (T1))
1248 and then Get_Instance_Of (Directly_Designated_Type (T1)) =
1249 Directly_Designated_Type (T2)
1250 then
1251 return True;
1253 -- Otherwise, types are not compatible
1255 else
1256 return False;
1257 end if;
1258 end Covers;
1260 ------------------
1261 -- Disambiguate --
1262 ------------------
1264 function Disambiguate
1265 (N : Node_Id;
1266 I1, I2 : Interp_Index;
1267 Typ : Entity_Id) return Interp
1269 I : Interp_Index;
1270 It : Interp;
1271 It1, It2 : Interp;
1272 Nam1, Nam2 : Entity_Id;
1273 Predef_Subp : Entity_Id;
1274 User_Subp : Entity_Id;
1276 function Inherited_From_Actual (S : Entity_Id) return Boolean;
1277 -- Determine whether one of the candidates is an operation inherited by
1278 -- a type that is derived from an actual in an instantiation.
1280 function In_Same_Declaration_List
1281 (Typ : Entity_Id;
1282 Op_Decl : Entity_Id) return Boolean;
1283 -- AI05-0020: a spurious ambiguity may arise when equality on anonymous
1284 -- access types is declared on the partial view of a designated type, so
1285 -- that the type declaration and equality are not in the same list of
1286 -- declarations. This AI gives a preference rule for the user-defined
1287 -- operation. Same rule applies for arithmetic operations on private
1288 -- types completed with fixed-point types: the predefined operation is
1289 -- hidden; this is already handled properly in GNAT.
1291 function Is_Actual_Subprogram (S : Entity_Id) return Boolean;
1292 -- Determine whether a subprogram is an actual in an enclosing instance.
1293 -- An overloading between such a subprogram and one declared outside the
1294 -- instance is resolved in favor of the first, because it resolved in
1295 -- the generic. Within the instance the actual is represented by a
1296 -- constructed subprogram renaming.
1298 function Matches (Op : Node_Id; Func_Id : Entity_Id) return Boolean;
1299 -- Determine whether function Func_Id is an exact match for binary or
1300 -- unary operator Op.
1302 function Operand_Type return Entity_Id;
1303 -- Determine type of operand for an equality operation, to apply Ada
1304 -- 2005 rules to equality on anonymous access types.
1306 function Standard_Operator return Boolean;
1307 -- Check whether subprogram is predefined operator declared in Standard.
1308 -- It may given by an operator name, or by an expanded name whose prefix
1309 -- is Standard.
1311 function Remove_Conversions_And_Abstract_Operations return Interp;
1312 -- Last chance for pathological cases involving comparisons on literals,
1313 -- and user overloadings of the same operator. Such pathologies have
1314 -- been removed from the ACVC, but still appear in two DEC tests, with
1315 -- the following notable quote from Ben Brosgol:
1317 -- [Note: I disclaim all credit/responsibility/blame for coming up with
1318 -- this example; Robert Dewar brought it to our attention, since it is
1319 -- apparently found in the ACVC 1.5. I did not attempt to find the
1320 -- reason in the Reference Manual that makes the example legal, since I
1321 -- was too nauseated by it to want to pursue it further.]
1323 -- Accordingly, this is not a fully recursive solution, but it handles
1324 -- DEC tests c460vsa, c460vsb. It also handles ai00136a, which pushes
1325 -- pathology in the other direction with calls whose multiple overloaded
1326 -- actuals make them truly unresolvable.
1328 -- The new rules concerning abstract operations create additional need
1329 -- for special handling of expressions with universal operands, see
1330 -- comments to Has_Abstract_Interpretation below.
1332 function Is_User_Defined_Anonymous_Access_Equality
1333 (User_Subp, Predef_Subp : Entity_Id) return Boolean;
1334 -- Check for Ada 2005, AI-020: If the context involves an anonymous
1335 -- access operand, recognize a user-defined equality (User_Subp) with
1336 -- the proper signature, declared in the same declarative list as the
1337 -- type and not hiding a predefined equality Predef_Subp.
1339 ---------------------------
1340 -- Inherited_From_Actual --
1341 ---------------------------
1343 function Inherited_From_Actual (S : Entity_Id) return Boolean is
1344 Par : constant Node_Id := Parent (S);
1345 begin
1346 if Nkind (Par) /= N_Full_Type_Declaration
1347 or else Nkind (Type_Definition (Par)) /= N_Derived_Type_Definition
1348 then
1349 return False;
1350 else
1351 return Is_Entity_Name (Subtype_Indication (Type_Definition (Par)))
1352 and then
1353 Is_Generic_Actual_Type (
1354 Entity (Subtype_Indication (Type_Definition (Par))));
1355 end if;
1356 end Inherited_From_Actual;
1358 ------------------------------
1359 -- In_Same_Declaration_List --
1360 ------------------------------
1362 function In_Same_Declaration_List
1363 (Typ : Entity_Id;
1364 Op_Decl : Entity_Id) return Boolean
1366 Scop : constant Entity_Id := Scope (Typ);
1368 begin
1369 return In_Same_List (Parent (Typ), Op_Decl)
1370 or else
1371 (Is_Package_Or_Generic_Package (Scop)
1372 and then List_Containing (Op_Decl) =
1373 Visible_Declarations (Parent (Scop))
1374 and then List_Containing (Parent (Typ)) =
1375 Private_Declarations (Parent (Scop)));
1376 end In_Same_Declaration_List;
1378 --------------------------
1379 -- Is_Actual_Subprogram --
1380 --------------------------
1382 function Is_Actual_Subprogram (S : Entity_Id) return Boolean is
1383 begin
1384 return In_Open_Scopes (Scope (S))
1385 and then Nkind (Unit_Declaration_Node (S)) =
1386 N_Subprogram_Renaming_Declaration
1388 -- Determine if the renaming came from source or was generated as a
1389 -- a result of generic expansion since the actual is represented by
1390 -- a constructed subprogram renaming.
1392 and then not Comes_From_Source (Unit_Declaration_Node (S))
1394 and then
1395 (Is_Generic_Instance (Scope (S))
1396 or else Is_Wrapper_Package (Scope (S)));
1397 end Is_Actual_Subprogram;
1399 -------------
1400 -- Matches --
1401 -------------
1403 function Matches (Op : Node_Id; Func_Id : Entity_Id) return Boolean is
1404 function Matching_Types
1405 (Opnd_Typ : Entity_Id;
1406 Formal_Typ : Entity_Id) return Boolean;
1407 -- Determine whether operand type Opnd_Typ and formal parameter type
1408 -- Formal_Typ are either the same or compatible.
1410 --------------------
1411 -- Matching_Types --
1412 --------------------
1414 function Matching_Types
1415 (Opnd_Typ : Entity_Id;
1416 Formal_Typ : Entity_Id) return Boolean
1418 begin
1419 -- A direct match
1421 if Opnd_Typ = Formal_Typ then
1422 return True;
1424 -- Any integer type matches universal integer
1426 elsif Opnd_Typ = Universal_Integer
1427 and then Is_Integer_Type (Formal_Typ)
1428 then
1429 return True;
1431 -- Any floating point type matches universal real
1433 elsif Opnd_Typ = Universal_Real
1434 and then Is_Floating_Point_Type (Formal_Typ)
1435 then
1436 return True;
1438 -- The type of the formal parameter maps a generic actual type to
1439 -- a generic formal type. If the operand type is the type being
1440 -- mapped in an instance, then this is a match.
1442 elsif Is_Generic_Actual_Type (Formal_Typ)
1443 and then Etype (Formal_Typ) = Opnd_Typ
1444 then
1445 return True;
1447 -- Formal_Typ is a private view, or Opnd_Typ and Formal_Typ are
1448 -- compatible only on a base-type basis.
1450 else
1451 return False;
1452 end if;
1453 end Matching_Types;
1455 -- Local variables
1457 F1 : constant Entity_Id := First_Formal (Func_Id);
1458 F1_Typ : constant Entity_Id := Etype (F1);
1459 F2 : constant Entity_Id := Next_Formal (F1);
1460 F2_Typ : constant Entity_Id := Etype (F2);
1461 Lop_Typ : constant Entity_Id := Etype (Left_Opnd (Op));
1462 Rop_Typ : constant Entity_Id := Etype (Right_Opnd (Op));
1464 -- Start of processing for Matches
1466 begin
1467 if Lop_Typ = F1_Typ then
1468 return Matching_Types (Rop_Typ, F2_Typ);
1470 elsif Rop_Typ = F2_Typ then
1471 return Matching_Types (Lop_Typ, F1_Typ);
1473 -- Otherwise this is not a good match because each operand-formal
1474 -- pair is compatible only on base-type basis, which is not specific
1475 -- enough.
1477 else
1478 return False;
1479 end if;
1480 end Matches;
1482 ------------------
1483 -- Operand_Type --
1484 ------------------
1486 function Operand_Type return Entity_Id is
1487 Opnd : Node_Id;
1489 begin
1490 if Nkind (N) = N_Function_Call then
1491 Opnd := First_Actual (N);
1492 else
1493 Opnd := Left_Opnd (N);
1494 end if;
1496 return Etype (Opnd);
1497 end Operand_Type;
1499 ------------------------------------------------
1500 -- Remove_Conversions_And_Abstract_Operations --
1501 ------------------------------------------------
1503 function Remove_Conversions_And_Abstract_Operations return Interp is
1504 I : Interp_Index;
1505 It : Interp;
1506 It1 : Interp;
1507 F1 : Entity_Id;
1508 Act1 : Node_Id;
1509 Act2 : Node_Id;
1511 function Has_Abstract_Interpretation (N : Node_Id) return Boolean;
1512 -- If an operation has universal operands, the universal operation
1513 -- is present among its interpretations. If there is an abstract
1514 -- interpretation for the operator, with a numeric result, this
1515 -- interpretation was already removed in sem_ch4, but the universal
1516 -- one is still visible. We must rescan the list of operators and
1517 -- remove the universal interpretation to resolve the ambiguity.
1519 function Is_Numeric_Only_Type (T : Entity_Id) return Boolean;
1520 -- Return True if T is a numeric type and not Any_Type
1522 ---------------------------------
1523 -- Has_Abstract_Interpretation --
1524 ---------------------------------
1526 function Has_Abstract_Interpretation (N : Node_Id) return Boolean is
1527 E : Entity_Id;
1529 begin
1530 if Nkind (N) not in N_Op
1531 or else Ada_Version < Ada_2005
1532 or else not Is_Overloaded (N)
1533 or else No (Universal_Interpretation (N))
1534 then
1535 return False;
1537 else
1538 E := Get_Name_Entity_Id (Chars (N));
1539 while Present (E) loop
1540 if Is_Overloadable (E)
1541 and then Is_Abstract_Subprogram (E)
1542 and then Is_Numeric_Only_Type (Etype (E))
1543 then
1544 return True;
1545 else
1546 E := Homonym (E);
1547 end if;
1548 end loop;
1550 -- Finally, if an operand of the binary operator is itself
1551 -- an operator, recurse to see whether its own abstract
1552 -- interpretation is responsible for the spurious ambiguity.
1554 if Nkind (N) in N_Binary_Op then
1555 return Has_Abstract_Interpretation (Left_Opnd (N))
1556 or else Has_Abstract_Interpretation (Right_Opnd (N));
1558 elsif Nkind (N) in N_Unary_Op then
1559 return Has_Abstract_Interpretation (Right_Opnd (N));
1561 else
1562 return False;
1563 end if;
1564 end if;
1565 end Has_Abstract_Interpretation;
1567 --------------------------
1568 -- Is_Numeric_Only_Type --
1569 --------------------------
1571 function Is_Numeric_Only_Type (T : Entity_Id) return Boolean is
1572 begin
1573 return Is_Numeric_Type (T) and then T /= Any_Type;
1574 end Is_Numeric_Only_Type;
1576 -- Start of processing for Remove_Conversions_And_Abstract_Operations
1578 begin
1579 It1 := No_Interp;
1581 Get_First_Interp (N, I, It);
1582 while Present (It.Typ) loop
1583 if not Is_Overloadable (It.Nam) then
1584 return No_Interp;
1585 end if;
1587 F1 := First_Formal (It.Nam);
1589 if No (F1) then
1590 return It1;
1592 else
1593 if Nkind (N) in N_Subprogram_Call then
1594 Act1 := First_Actual (N);
1596 if Present (Act1) then
1597 Act2 := Next_Actual (Act1);
1598 else
1599 Act2 := Empty;
1600 end if;
1602 elsif Nkind (N) in N_Unary_Op then
1603 Act1 := Right_Opnd (N);
1604 Act2 := Empty;
1606 elsif Nkind (N) in N_Binary_Op then
1607 Act1 := Left_Opnd (N);
1608 Act2 := Right_Opnd (N);
1610 -- Use the type of the second formal, so as to include
1611 -- exponentiation, where the exponent may be ambiguous and
1612 -- the result non-universal.
1614 Next_Formal (F1);
1616 else
1617 return It1;
1618 end if;
1620 if Nkind (Act1) in N_Op
1621 and then Is_Overloaded (Act1)
1622 and then
1623 (Nkind (Act1) in N_Unary_Op
1624 or else Nkind (Left_Opnd (Act1)) in
1625 N_Integer_Literal | N_Real_Literal)
1626 and then Nkind (Right_Opnd (Act1)) in
1627 N_Integer_Literal | N_Real_Literal
1628 and then Has_Compatible_Type (Act1, Standard_Boolean)
1629 and then Etype (F1) = Standard_Boolean
1630 then
1631 -- If the two candidates are the original ones, the
1632 -- ambiguity is real. Otherwise keep the original, further
1633 -- calls to Disambiguate will take care of others in the
1634 -- list of candidates.
1636 if It1 /= No_Interp then
1637 if It = Disambiguate.It1
1638 or else It = Disambiguate.It2
1639 then
1640 if It1 = Disambiguate.It1
1641 or else It1 = Disambiguate.It2
1642 then
1643 return No_Interp;
1644 else
1645 It1 := It;
1646 end if;
1647 end if;
1649 elsif Present (Act2)
1650 and then Nkind (Act2) in N_Op
1651 and then Is_Overloaded (Act2)
1652 and then Nkind (Right_Opnd (Act2)) in
1653 N_Integer_Literal | N_Real_Literal
1654 and then Has_Compatible_Type (Act2, Standard_Boolean)
1655 then
1656 -- The preference rule on the first actual is not
1657 -- sufficient to disambiguate.
1659 goto Next_Interp;
1661 else
1662 It1 := It;
1663 end if;
1665 elsif Is_Numeric_Only_Type (Etype (F1))
1666 and then Has_Abstract_Interpretation (Act1)
1667 then
1668 -- Current interpretation is not the right one because it
1669 -- expects a numeric operand. Examine all the others.
1671 declare
1672 I : Interp_Index;
1673 It : Interp;
1675 begin
1676 Get_First_Interp (N, I, It);
1677 while Present (It.Typ) loop
1678 if not Is_Numeric_Only_Type
1679 (Etype (First_Formal (It.Nam)))
1680 then
1681 if No (Act2)
1682 or else not
1683 Is_Numeric_Only_Type
1684 (Etype (Next_Formal (First_Formal (It.Nam))))
1685 or else not Has_Abstract_Interpretation (Act2)
1686 then
1687 return It;
1688 end if;
1689 end if;
1691 Get_Next_Interp (I, It);
1692 end loop;
1694 return No_Interp;
1695 end;
1697 elsif Is_Numeric_Only_Type (Etype (F1))
1698 and then Present (Act2)
1699 and then Has_Abstract_Interpretation (Act2)
1700 then
1701 -- Current interpretation is not the right one because it
1702 -- expects a numeric operand. Examine all the others.
1704 declare
1705 I : Interp_Index;
1706 It : Interp;
1708 begin
1709 Get_First_Interp (N, I, It);
1710 while Present (It.Typ) loop
1711 if not Is_Numeric_Only_Type
1712 (Etype (Next_Formal (First_Formal (It.Nam))))
1713 then
1714 if not Is_Numeric_Only_Type
1715 (Etype (First_Formal (It.Nam)))
1716 or else not Has_Abstract_Interpretation (Act1)
1717 then
1718 return It;
1719 end if;
1720 end if;
1722 Get_Next_Interp (I, It);
1723 end loop;
1725 return No_Interp;
1726 end;
1727 end if;
1728 end if;
1730 <<Next_Interp>>
1731 Get_Next_Interp (I, It);
1732 end loop;
1734 return It1;
1735 end Remove_Conversions_And_Abstract_Operations;
1737 -----------------------
1738 -- Standard_Operator --
1739 -----------------------
1741 function Standard_Operator return Boolean is
1742 Nam : Node_Id;
1744 begin
1745 if Nkind (N) in N_Op then
1746 return True;
1748 elsif Nkind (N) = N_Function_Call then
1749 Nam := Name (N);
1751 if Nkind (Nam) /= N_Expanded_Name then
1752 return True;
1753 else
1754 return Entity (Prefix (Nam)) = Standard_Standard;
1755 end if;
1756 else
1757 return False;
1758 end if;
1759 end Standard_Operator;
1761 -----------------------------------------------
1762 -- Is_User_Defined_Anonymous_Access_Equality --
1763 -----------------------------------------------
1765 function Is_User_Defined_Anonymous_Access_Equality
1766 (User_Subp, Predef_Subp : Entity_Id) return Boolean is
1767 begin
1768 return Present (User_Subp)
1770 -- Check for Ada 2005 and use of anonymous access
1772 and then Ada_Version >= Ada_2005
1773 and then Etype (User_Subp) = Standard_Boolean
1774 and then Is_Anonymous_Access_Type (Operand_Type)
1776 -- This check is only relevant if User_Subp is visible and not in
1777 -- an instance
1779 and then (In_Open_Scopes (Scope (User_Subp))
1780 or else Is_Potentially_Use_Visible (User_Subp))
1781 and then not In_Instance
1782 and then not Hides_Op (User_Subp, Predef_Subp)
1784 -- Is User_Subp declared in the same declarative list as the type?
1786 and then
1787 In_Same_Declaration_List
1788 (Designated_Type (Operand_Type),
1789 Unit_Declaration_Node (User_Subp));
1790 end Is_User_Defined_Anonymous_Access_Equality;
1792 -- Start of processing for Disambiguate
1794 begin
1795 -- Recover the two legal interpretations
1797 Get_First_Interp (N, I, It);
1798 while I /= I1 loop
1799 Get_Next_Interp (I, It);
1800 end loop;
1802 It1 := It;
1803 Nam1 := It.Nam;
1805 while I /= I2 loop
1806 Get_Next_Interp (I, It);
1807 end loop;
1809 It2 := It;
1810 Nam2 := It.Nam;
1812 -- Check whether one of the entities is an Ada 2005/2012/2022 and we
1813 -- are operating in an earlier mode, in which case we discard the Ada
1814 -- 2005/2012/2022 entity, so that we get proper Ada 95 overload
1815 -- resolution.
1817 if Ada_Version < Ada_2005 then
1818 if Is_Ada_2005_Only (Nam1)
1819 or else Is_Ada_2012_Only (Nam1)
1820 or else Is_Ada_2022_Only (Nam1)
1821 then
1822 return It2;
1824 elsif Is_Ada_2005_Only (Nam2)
1825 or else Is_Ada_2012_Only (Nam2)
1826 or else Is_Ada_2022_Only (Nam2)
1827 then
1828 return It1;
1829 end if;
1831 -- Check whether one of the entities is an Ada 2012/2022 entity and we
1832 -- are operating in Ada 2005 mode, in which case we discard the Ada 2012
1833 -- Ada 2022 entity, so that we get proper Ada 2005 overload resolution.
1835 elsif Ada_Version = Ada_2005 then
1836 if Is_Ada_2012_Only (Nam1) or else Is_Ada_2022_Only (Nam1) then
1837 return It2;
1838 elsif Is_Ada_2012_Only (Nam2) or else Is_Ada_2022_Only (Nam2) then
1839 return It1;
1840 end if;
1842 -- Ditto for Ada 2012 vs Ada 2022.
1844 elsif Ada_Version = Ada_2012 then
1845 if Is_Ada_2022_Only (Nam1) then
1846 return It2;
1847 elsif Is_Ada_2022_Only (Nam2) then
1848 return It1;
1849 end if;
1850 end if;
1852 -- If the context is universal, the predefined operator is preferred.
1853 -- This includes bounds in numeric type declarations, and expressions
1854 -- in type conversions. If no interpretation yields a universal type,
1855 -- then we must check whether the user-defined entity hides the prede-
1856 -- fined one.
1858 if Chars (Nam1) in Any_Operator_Name and then Standard_Operator then
1859 if Typ = Universal_Integer
1860 or else Typ = Universal_Real
1861 or else Typ = Any_Integer
1862 or else Typ = Any_Discrete
1863 or else Typ = Any_Real
1864 or else Typ = Any_Type
1865 then
1866 -- Find an interpretation that yields the universal type, or else
1867 -- a predefined operator that yields a predefined numeric type.
1869 declare
1870 Candidate : Interp := No_Interp;
1872 begin
1873 Get_First_Interp (N, I, It);
1874 while Present (It.Typ) loop
1875 if Is_Universal_Numeric_Type (It.Typ)
1876 and then (Typ = Any_Type or else Covers (Typ, It.Typ))
1877 then
1878 return It;
1880 elsif Is_Numeric_Type (It.Typ)
1881 and then Scope (It.Typ) = Standard_Standard
1882 and then Scope (It.Nam) = Standard_Standard
1883 and then Covers (Typ, It.Typ)
1884 then
1885 Candidate := It;
1886 end if;
1888 Get_Next_Interp (I, It);
1889 end loop;
1891 if Candidate /= No_Interp then
1892 return Candidate;
1893 end if;
1894 end;
1896 elsif Chars (Nam1) /= Name_Op_Not
1897 and then (Typ = Standard_Boolean or else Typ = Any_Boolean)
1898 then
1899 -- Equality or comparison operation. Choose predefined operator if
1900 -- arguments are universal. The node may be an operator, name, or
1901 -- a function call, so unpack arguments accordingly.
1903 declare
1904 Arg1, Arg2 : Node_Id;
1906 begin
1907 if Nkind (N) in N_Op then
1908 Arg1 := Left_Opnd (N);
1909 Arg2 := Right_Opnd (N);
1911 elsif Is_Entity_Name (N) then
1912 Arg1 := First_Entity (Entity (N));
1913 Arg2 := Next_Entity (Arg1);
1915 else
1916 Arg1 := First_Actual (N);
1917 Arg2 := Next_Actual (Arg1);
1918 end if;
1920 if Present (Arg2) then
1921 if Ekind (Nam1) = E_Operator then
1922 Predef_Subp := Nam1;
1923 User_Subp := Nam2;
1924 elsif Ekind (Nam2) = E_Operator then
1925 Predef_Subp := Nam2;
1926 User_Subp := Nam1;
1927 else
1928 Predef_Subp := Empty;
1929 User_Subp := Empty;
1930 end if;
1932 -- Take into account universal interpretation as well as
1933 -- universal_access equality, as long as AI05-0020 does not
1934 -- trigger.
1936 if (Present (Universal_Interpretation (Arg1))
1937 and then Universal_Interpretation (Arg2) =
1938 Universal_Interpretation (Arg1))
1939 or else
1940 (Nkind (N) in N_Op_Eq | N_Op_Ne
1941 and then (Is_Anonymous_Access_Type (Etype (Arg1))
1942 or else
1943 Is_Anonymous_Access_Type (Etype (Arg2)))
1944 and then not
1945 Is_User_Defined_Anonymous_Access_Equality
1946 (User_Subp, Predef_Subp))
1947 then
1948 Get_First_Interp (N, I, It);
1949 while Scope (It.Nam) /= Standard_Standard loop
1950 Get_Next_Interp (I, It);
1951 end loop;
1953 return It;
1954 end if;
1955 end if;
1956 end;
1957 end if;
1958 end if;
1960 -- If no universal interpretation, check whether user-defined operator
1961 -- hides predefined one, as well as other special cases. If the node
1962 -- is a range, then one or both bounds are ambiguous. Each will have
1963 -- to be disambiguated w.r.t. the context type. The type of the range
1964 -- itself is imposed by the context, so we can return either legal
1965 -- interpretation.
1967 if Ekind (Nam1) = E_Operator then
1968 Predef_Subp := Nam1;
1969 User_Subp := Nam2;
1971 elsif Ekind (Nam2) = E_Operator then
1972 Predef_Subp := Nam2;
1973 User_Subp := Nam1;
1975 elsif Nkind (N) = N_Range then
1976 return It1;
1978 -- Implement AI05-105: A renaming declaration with an access
1979 -- definition must resolve to an anonymous access type. This
1980 -- is a resolution rule and can be used to disambiguate.
1982 elsif Nkind (Parent (N)) = N_Object_Renaming_Declaration
1983 and then Present (Access_Definition (Parent (N)))
1984 then
1985 if Is_Anonymous_Access_Type (It1.Typ) then
1986 if Ekind (It2.Typ) = Ekind (It1.Typ) then
1988 -- True ambiguity
1990 return No_Interp;
1992 else
1993 return It1;
1994 end if;
1996 elsif Is_Anonymous_Access_Type (It2.Typ) then
1997 return It2;
1999 -- No legal interpretation
2001 else
2002 return No_Interp;
2003 end if;
2005 -- Two access attribute types may have been created for an expression
2006 -- with an implicit dereference, which is automatically overloaded.
2007 -- If both access attribute types designate the same object type,
2008 -- disambiguation if any will take place elsewhere, so keep any one of
2009 -- the interpretations.
2011 elsif Ekind (It1.Typ) = E_Access_Attribute_Type
2012 and then Ekind (It2.Typ) = E_Access_Attribute_Type
2013 and then Designated_Type (It1.Typ) = Designated_Type (It2.Typ)
2014 then
2015 return It1;
2017 -- If two user defined-subprograms are visible, it is a true ambiguity,
2018 -- unless one of them is an entry and the context is a conditional or
2019 -- timed entry call, or unless we are within an instance and this is
2020 -- results from two formals types with the same actual.
2022 else
2023 if Nkind (N) = N_Procedure_Call_Statement
2024 and then Nkind (Parent (N)) = N_Entry_Call_Alternative
2025 and then N = Entry_Call_Statement (Parent (N))
2026 then
2027 if Ekind (Nam2) = E_Entry then
2028 return It2;
2029 elsif Ekind (Nam1) = E_Entry then
2030 return It1;
2031 else
2032 return No_Interp;
2033 end if;
2035 -- If the ambiguity occurs within an instance, it is due to several
2036 -- formal types with the same actual. Look for an exact match between
2037 -- the types of the formals of the overloadable entities, and the
2038 -- actuals in the call, to recover the unambiguous match in the
2039 -- original generic.
2041 -- The ambiguity can also be due to an overloading between a formal
2042 -- subprogram and a subprogram declared outside the generic. If the
2043 -- node is overloaded, it did not resolve to the global entity in
2044 -- the generic, and we choose the formal subprogram.
2046 -- Finally, the ambiguity can be between an explicit subprogram and
2047 -- one inherited (with different defaults) from an actual. In this
2048 -- case the resolution was to the explicit declaration in the
2049 -- generic, and remains so in the instance.
2051 -- The same sort of disambiguation needed for calls is also required
2052 -- for the name given in a subprogram renaming, and that case is
2053 -- handled here as well. We test Comes_From_Source to exclude this
2054 -- treatment for implicit renamings created for formal subprograms.
2056 elsif In_Instance and then not In_Generic_Actual (N) then
2057 if Nkind (N) in N_Subprogram_Call
2058 or else
2059 (Nkind (N) in N_Has_Entity
2060 and then
2061 Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
2062 and then Comes_From_Source (Parent (N)))
2063 then
2064 declare
2065 Actual : Node_Id;
2066 Formal : Entity_Id;
2067 Renam : Entity_Id := Empty;
2068 Is_Act1 : constant Boolean := Is_Actual_Subprogram (Nam1);
2069 Is_Act2 : constant Boolean := Is_Actual_Subprogram (Nam2);
2071 begin
2072 if Is_Act1 and then not Is_Act2 then
2073 return It1;
2075 elsif Is_Act2 and then not Is_Act1 then
2076 return It2;
2078 elsif Inherited_From_Actual (Nam1)
2079 and then Comes_From_Source (Nam2)
2080 then
2081 return It2;
2083 elsif Inherited_From_Actual (Nam2)
2084 and then Comes_From_Source (Nam1)
2085 then
2086 return It1;
2087 end if;
2089 -- In the case of a renamed subprogram, pick up the entity
2090 -- of the renaming declaration so we can traverse its
2091 -- formal parameters.
2093 if Nkind (N) in N_Has_Entity then
2094 Renam := Defining_Unit_Name (Specification (Parent (N)));
2095 end if;
2097 if Present (Renam) then
2098 Actual := First_Formal (Renam);
2099 else
2100 Actual := First_Actual (N);
2101 end if;
2103 Formal := First_Formal (Nam1);
2104 while Present (Actual) loop
2105 if Etype (Actual) /= Etype (Formal) then
2106 return It2;
2107 end if;
2109 if Present (Renam) then
2110 Next_Formal (Actual);
2111 else
2112 Next_Actual (Actual);
2113 end if;
2115 Next_Formal (Formal);
2116 end loop;
2118 return It1;
2119 end;
2121 elsif Nkind (N) in N_Binary_Op then
2122 if Matches (N, Nam1) then
2123 return It1;
2124 else
2125 return It2;
2126 end if;
2128 elsif Nkind (N) in N_Unary_Op then
2129 if Etype (Right_Opnd (N)) = Etype (First_Formal (Nam1)) then
2130 return It1;
2131 else
2132 return It2;
2133 end if;
2135 else
2136 return Remove_Conversions_And_Abstract_Operations;
2137 end if;
2138 else
2139 return Remove_Conversions_And_Abstract_Operations;
2140 end if;
2141 end if;
2143 -- An implicit concatenation operator on a string type cannot be
2144 -- disambiguated from the predefined concatenation. This can only
2145 -- happen with concatenation of string literals.
2147 if Chars (User_Subp) = Name_Op_Concat
2148 and then Ekind (User_Subp) = E_Operator
2149 and then Is_String_Type (Etype (First_Formal (User_Subp)))
2150 then
2151 return No_Interp;
2153 -- If the user-defined operator matches the signature of the operator,
2154 -- and is declared in an open scope, or in the scope of the resulting
2155 -- type, or given by an expanded name that names its scope, it hides
2156 -- the predefined operator for the type. But exponentiation has to be
2157 -- special-cased because the latter operator does not have a symmetric
2158 -- signature, and may not be hidden by the explicit one.
2160 elsif Hides_Op (User_Subp, Predef_Subp)
2161 or else (Nkind (N) = N_Function_Call
2162 and then Nkind (Name (N)) = N_Expanded_Name
2163 and then (Chars (Predef_Subp) /= Name_Op_Expon
2164 or else Hides_Op (User_Subp, Predef_Subp))
2165 and then Scope (User_Subp) = Entity (Prefix (Name (N))))
2166 then
2167 if It1.Nam = User_Subp then
2168 return It1;
2169 else
2170 return It2;
2171 end if;
2173 -- Otherwise, the predefined operator has precedence, or if the user-
2174 -- defined operation is directly visible we have a true ambiguity.
2176 -- If this is a fixed-point multiplication and division in Ada 83 mode,
2177 -- exclude the universal_fixed operator, which often causes ambiguities
2178 -- in legacy code.
2180 -- Ditto in Ada 2012, where an ambiguity may arise for an operation
2181 -- on a partial view that is completed with a fixed point type. See
2182 -- AI05-0020 and AI05-0209. The ambiguity is resolved in favor of the
2183 -- user-defined type and subprogram, so that a client of the package
2184 -- has the same resolution as the body of the package.
2186 else
2187 if (In_Open_Scopes (Scope (User_Subp))
2188 or else Is_Potentially_Use_Visible (User_Subp))
2189 and then not In_Instance
2190 then
2191 if Is_Fixed_Point_Type (Typ)
2192 and then Chars (Nam1) in Name_Op_Multiply | Name_Op_Divide
2193 and then
2194 (Ada_Version = Ada_83
2195 or else (Ada_Version >= Ada_2012
2196 and then In_Same_Declaration_List
2197 (First_Subtype (Typ),
2198 Unit_Declaration_Node (User_Subp))))
2199 then
2200 if It2.Nam = Predef_Subp then
2201 return It1;
2202 else
2203 return It2;
2204 end if;
2206 -- Check for AI05-020
2208 elsif Chars (Nam1) in Name_Op_Eq | Name_Op_Ne
2209 and then Is_User_Defined_Anonymous_Access_Equality
2210 (User_Subp, Predef_Subp)
2211 then
2212 if It2.Nam = Predef_Subp then
2213 return It1;
2214 else
2215 return It2;
2216 end if;
2218 -- RM 8.4(10): an immediately visible operator hides a use-visible
2219 -- user-defined operation that is a homograph. This disambiguation
2220 -- cannot take place earlier because visibility of the predefined
2221 -- operator can only be established when operand types are known.
2223 elsif Ekind (User_Subp) = E_Function
2224 and then Ekind (Predef_Subp) = E_Operator
2225 and then Operator_Matches_Spec (Predef_Subp, User_Subp)
2226 and then Nkind (N) in N_Op
2227 and then not Is_Overloaded (Right_Opnd (N))
2228 and then
2229 Is_Immediately_Visible (Base_Type (Etype (Right_Opnd (N))))
2230 and then Is_Potentially_Use_Visible (User_Subp)
2231 then
2232 if It2.Nam = Predef_Subp then
2233 return It1;
2234 else
2235 return It2;
2236 end if;
2238 else
2239 return Remove_Conversions_And_Abstract_Operations;
2240 end if;
2242 elsif It1.Nam = Predef_Subp then
2243 return It1;
2245 else
2246 return It2;
2247 end if;
2248 end if;
2249 end Disambiguate;
2251 -------------------------
2252 -- Entity_Matches_Spec --
2253 -------------------------
2255 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean is
2256 begin
2257 -- For the simple case of same kinds, type conformance is required, but
2258 -- a parameterless function can also rename a literal.
2260 if Ekind (Old_S) = Ekind (New_S)
2261 or else (Ekind (New_S) = E_Function
2262 and then Ekind (Old_S) = E_Enumeration_Literal)
2263 then
2264 return Type_Conformant (New_S, Old_S);
2266 -- Likewise for a procedure and an entry
2268 elsif Ekind (New_S) = E_Procedure and then Is_Entry (Old_S) then
2269 return Type_Conformant (New_S, Old_S);
2271 -- For a user-defined operator, use the dedicated predicate
2273 elsif Ekind (New_S) = E_Function and then Ekind (Old_S) = E_Operator then
2274 return Operator_Matches_Spec (Old_S, New_S);
2276 else
2277 return False;
2278 end if;
2279 end Entity_Matches_Spec;
2281 ----------------------
2282 -- Find_Unique_Type --
2283 ----------------------
2285 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id is
2286 T : constant Entity_Id := Specific_Type (Etype (L), Etype (R));
2288 begin
2289 if T = Any_Type then
2290 if Is_User_Defined_Literal (L, Etype (R)) then
2291 return Etype (R);
2292 elsif Is_User_Defined_Literal (R, Etype (L)) then
2293 return Etype (L);
2294 end if;
2295 end if;
2297 return T;
2298 end Find_Unique_Type;
2300 -------------------------------------
2301 -- Function_Interp_Has_Abstract_Op --
2302 -------------------------------------
2304 function Function_Interp_Has_Abstract_Op
2305 (N : Node_Id;
2306 E : Entity_Id) return Entity_Id
2308 Abstr_Op : Entity_Id;
2309 Act : Node_Id;
2310 Act_Parm : Node_Id;
2311 Form_Parm : Node_Id;
2313 begin
2314 if Is_Overloaded (N) then
2315 -- Move through the formals and actuals of the call to
2316 -- determine if an abstract interpretation exists.
2318 Act_Parm := First_Actual (N);
2319 Form_Parm := First_Formal (E);
2320 while Present (Act_Parm) and then Present (Form_Parm) loop
2321 Act := Act_Parm;
2323 -- Extract the actual from a parameter association
2325 if Nkind (Act) = N_Parameter_Association then
2326 Act := Explicit_Actual_Parameter (Act);
2327 end if;
2329 -- Use the actual and the type of its correponding formal to test
2330 -- for an abstract interpretation and return it when found.
2332 Abstr_Op := Has_Abstract_Op (Act, Etype (Form_Parm));
2334 if Present (Abstr_Op) then
2335 return Abstr_Op;
2336 end if;
2338 Next_Actual (Act_Parm);
2339 Next_Formal (Form_Parm);
2340 end loop;
2341 end if;
2343 -- Otherwise, return empty
2345 return Empty;
2346 end Function_Interp_Has_Abstract_Op;
2348 ----------------------
2349 -- Get_First_Interp --
2350 ----------------------
2352 procedure Get_First_Interp
2353 (N : Node_Id;
2354 I : out Interp_Index;
2355 It : out Interp)
2357 Int_Ind : Interp_Index;
2358 O_N : Node_Id;
2360 begin
2361 -- If a selected component is overloaded because the selector has
2362 -- multiple interpretations, the node is a call to a protected
2363 -- operation or an indirect call. Retrieve the interpretation from
2364 -- the selector name. The selected component may be overloaded as well
2365 -- if the prefix is overloaded. That case is unchanged.
2367 if Nkind (N) = N_Selected_Component
2368 and then Is_Overloaded (Selector_Name (N))
2369 then
2370 O_N := Selector_Name (N);
2371 else
2372 O_N := N;
2373 end if;
2375 Int_Ind := Interp_Map.Get (O_N);
2377 -- Procedure should never be called if the node has no interpretations
2379 if Int_Ind < 0 then
2380 raise Program_Error;
2381 end if;
2383 I := Int_Ind;
2384 It := All_Interp.Table (Int_Ind);
2385 end Get_First_Interp;
2387 ---------------------
2388 -- Get_Next_Interp --
2389 ---------------------
2391 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp) is
2392 begin
2393 I := I + 1;
2394 It := All_Interp.Table (I);
2395 end Get_Next_Interp;
2397 -------------------------
2398 -- Has_Compatible_Type --
2399 -------------------------
2401 function Has_Compatible_Type (N : Node_Id; Typ : Entity_Id) return Boolean
2403 I : Interp_Index;
2404 It : Interp;
2406 begin
2407 if N = Error then
2408 return False;
2409 end if;
2411 if Nkind (N) = N_Subtype_Indication or else not Is_Overloaded (N) then
2412 if Covers (Typ, Etype (N))
2414 -- Ada 2005 (AI-345): The context may be a synchronized interface.
2415 -- If the type is already frozen, use the corresponding_record to
2416 -- check whether it is a proper descendant.
2418 or else
2419 (Is_Record_Type (Typ)
2420 and then Is_Concurrent_Type (Etype (N))
2421 and then Present (Corresponding_Record_Type (Etype (N)))
2422 and then Covers (Typ, Corresponding_Record_Type (Etype (N))))
2424 or else
2425 (Is_Concurrent_Type (Typ)
2426 and then Is_Record_Type (Etype (N))
2427 and then Present (Corresponding_Record_Type (Typ))
2428 and then Covers (Corresponding_Record_Type (Typ), Etype (N)))
2430 or else Is_User_Defined_Literal (N, Typ)
2432 then
2433 return True;
2434 end if;
2436 -- Overloaded case
2438 else
2439 Get_First_Interp (N, I, It);
2440 while Present (It.Typ) loop
2441 if Covers (Typ, It.Typ)
2443 -- Ada 2005 (AI-345)
2445 or else
2446 (Is_Record_Type (Typ)
2447 and then Is_Concurrent_Type (It.Typ)
2448 and then Present (Corresponding_Record_Type (Etype (It.Typ)))
2449 and then
2450 Covers (Typ, Corresponding_Record_Type (Etype (It.Typ))))
2452 or else
2453 (Is_Concurrent_Type (Typ)
2454 and then Is_Record_Type (It.Typ)
2455 and then Present (Corresponding_Record_Type (Typ))
2456 and then
2457 Covers (Corresponding_Record_Type (Typ), Etype (It.Typ)))
2459 then
2460 return True;
2461 end if;
2463 Get_Next_Interp (I, It);
2464 end loop;
2465 end if;
2467 return False;
2468 end Has_Compatible_Type;
2470 ---------------------
2471 -- Has_Abstract_Op --
2472 ---------------------
2474 function Has_Abstract_Op
2475 (N : Node_Id;
2476 Typ : Entity_Id) return Entity_Id
2478 I : Interp_Index;
2479 It : Interp;
2481 begin
2482 if Is_Overloaded (N) then
2483 Get_First_Interp (N, I, It);
2484 while Present (It.Nam) loop
2485 if Present (It.Abstract_Op)
2486 and then Etype (It.Abstract_Op) = Typ
2487 then
2488 return It.Abstract_Op;
2489 end if;
2491 Get_Next_Interp (I, It);
2492 end loop;
2493 end if;
2495 return Empty;
2496 end Has_Abstract_Op;
2498 ----------
2499 -- Hash --
2500 ----------
2502 function Hash (N : Node_Id) return Header_Num is
2503 begin
2504 return Header_Num (N mod Header_Max);
2505 end Hash;
2507 --------------
2508 -- Hides_Op --
2509 --------------
2511 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean is
2512 Btyp : constant Entity_Id := Base_Type (Etype (First_Formal (F)));
2513 begin
2514 return Operator_Matches_Spec (Op, F)
2515 and then (In_Open_Scopes (Scope (F))
2516 or else Scope (F) = Scope (Btyp)
2517 or else (not In_Open_Scopes (Scope (Btyp))
2518 and then not In_Use (Btyp)
2519 and then not In_Use (Scope (Btyp))));
2520 end Hides_Op;
2522 ------------------------
2523 -- Init_Interp_Tables --
2524 ------------------------
2526 procedure Init_Interp_Tables is
2527 begin
2528 All_Interp.Init;
2529 Interp_Map.Reset;
2530 end Init_Interp_Tables;
2532 -----------------------------------
2533 -- Interface_Present_In_Ancestor --
2534 -----------------------------------
2536 function Interface_Present_In_Ancestor
2537 (Typ : Entity_Id;
2538 Iface : Entity_Id) return Boolean
2540 Target_Typ : Entity_Id;
2541 Iface_Typ : Entity_Id;
2543 function Iface_Present_In_Ancestor (Typ : Entity_Id) return Boolean;
2544 -- Returns True if Typ or some ancestor of Typ implements Iface
2546 -------------------------------
2547 -- Iface_Present_In_Ancestor --
2548 -------------------------------
2550 function Iface_Present_In_Ancestor (Typ : Entity_Id) return Boolean is
2551 E : Entity_Id;
2552 AI : Entity_Id;
2553 Elmt : Elmt_Id;
2555 begin
2556 if Typ = Iface_Typ then
2557 return True;
2558 end if;
2560 -- Handle private types
2562 if Present (Full_View (Typ))
2563 and then not Is_Concurrent_Type (Full_View (Typ))
2564 then
2565 E := Full_View (Typ);
2566 else
2567 E := Typ;
2568 end if;
2570 loop
2571 if Present (Interfaces (E))
2572 and then not Is_Empty_Elmt_List (Interfaces (E))
2573 then
2574 Elmt := First_Elmt (Interfaces (E));
2575 while Present (Elmt) loop
2576 AI := Node (Elmt);
2578 if AI = Iface_Typ or else Is_Ancestor (Iface_Typ, AI) then
2579 return True;
2580 end if;
2582 Next_Elmt (Elmt);
2583 end loop;
2584 end if;
2586 exit when Etype (E) = E
2588 -- Handle private types
2590 or else (Present (Full_View (Etype (E)))
2591 and then Full_View (Etype (E)) = E);
2593 -- Check if the current type is a direct derivation of the
2594 -- interface
2596 if Etype (E) = Iface_Typ then
2597 return True;
2598 end if;
2600 -- Climb to the immediate ancestor handling private types
2602 if Present (Full_View (Etype (E))) then
2603 E := Full_View (Etype (E));
2604 else
2605 E := Etype (E);
2606 end if;
2607 end loop;
2609 return False;
2610 end Iface_Present_In_Ancestor;
2612 -- Start of processing for Interface_Present_In_Ancestor
2614 begin
2615 -- Iface might be a class-wide subtype, so we have to apply Base_Type
2617 if Is_Class_Wide_Type (Iface) then
2618 Iface_Typ := Etype (Base_Type (Iface));
2619 else
2620 Iface_Typ := Iface;
2621 end if;
2623 -- Handle subtypes
2625 Iface_Typ := Base_Type (Iface_Typ);
2627 if Is_Access_Type (Typ) then
2628 Target_Typ := Etype (Directly_Designated_Type (Typ));
2629 else
2630 Target_Typ := Typ;
2631 end if;
2633 if Is_Concurrent_Record_Type (Target_Typ) then
2634 Target_Typ := Corresponding_Concurrent_Type (Target_Typ);
2635 end if;
2637 Target_Typ := Base_Type (Target_Typ);
2639 -- In case of concurrent types we can't use the Corresponding Record_Typ
2640 -- to look for the interface because it is built by the expander (and
2641 -- hence it is not always available). For this reason we traverse the
2642 -- list of interfaces (available in the parent of the concurrent type)
2644 if Is_Concurrent_Type (Target_Typ) then
2645 if Present (Interface_List (Parent (Target_Typ))) then
2646 declare
2647 AI : Node_Id;
2649 begin
2650 AI := First (Interface_List (Parent (Target_Typ)));
2652 -- The progenitor itself may be a subtype of an interface type.
2654 while Present (AI) loop
2655 if Etype (AI) = Iface_Typ
2656 or else Base_Type (Etype (AI)) = Iface_Typ
2657 then
2658 return True;
2660 elsif Present (Interfaces (Etype (AI)))
2661 and then Iface_Present_In_Ancestor (Etype (AI))
2662 then
2663 return True;
2664 end if;
2666 Next (AI);
2667 end loop;
2668 end;
2669 end if;
2671 return False;
2672 end if;
2674 if Is_Class_Wide_Type (Target_Typ) then
2675 Target_Typ := Etype (Target_Typ);
2676 end if;
2678 if Ekind (Target_Typ) = E_Incomplete_Type then
2680 -- We must have either a full view or a nonlimited view of the type
2681 -- to locate the list of ancestors.
2683 if Present (Full_View (Target_Typ)) then
2684 Target_Typ := Full_View (Target_Typ);
2685 else
2686 -- In a spec expression or in an expression function, the use of
2687 -- an incomplete type is legal; legality of the conversion will be
2688 -- checked at freeze point of related entity.
2690 if In_Spec_Expression then
2691 return True;
2693 else
2694 pragma Assert (Present (Non_Limited_View (Target_Typ)));
2695 Target_Typ := Non_Limited_View (Target_Typ);
2696 end if;
2697 end if;
2699 -- Protect the front end against previously detected errors
2701 if Ekind (Target_Typ) = E_Incomplete_Type then
2702 return False;
2703 end if;
2704 end if;
2706 return Iface_Present_In_Ancestor (Target_Typ);
2707 end Interface_Present_In_Ancestor;
2709 ---------------------
2710 -- Intersect_Types --
2711 ---------------------
2713 function Intersect_Types (L, R : Node_Id) return Entity_Id is
2714 Index : Interp_Index;
2715 It : Interp;
2716 Typ : Entity_Id;
2718 function Check_Right_Argument (T : Entity_Id) return Entity_Id;
2719 -- Find interpretation of right arg that has type compatible with T
2721 --------------------------
2722 -- Check_Right_Argument --
2723 --------------------------
2725 function Check_Right_Argument (T : Entity_Id) return Entity_Id is
2726 Index : Interp_Index;
2727 It : Interp;
2728 T2 : Entity_Id;
2730 begin
2731 if not Is_Overloaded (R) then
2732 return Specific_Type (T, Etype (R));
2734 else
2735 Get_First_Interp (R, Index, It);
2736 loop
2737 T2 := Specific_Type (T, It.Typ);
2739 if T2 /= Any_Type then
2740 return T2;
2741 end if;
2743 Get_Next_Interp (Index, It);
2744 exit when No (It.Typ);
2745 end loop;
2747 return Any_Type;
2748 end if;
2749 end Check_Right_Argument;
2751 -- Start of processing for Intersect_Types
2753 begin
2754 if Etype (L) = Any_Type or else Etype (R) = Any_Type then
2755 return Any_Type;
2756 end if;
2758 if not Is_Overloaded (L) then
2759 Typ := Check_Right_Argument (Etype (L));
2761 else
2762 Typ := Any_Type;
2763 Get_First_Interp (L, Index, It);
2764 while Present (It.Typ) loop
2765 Typ := Check_Right_Argument (It.Typ);
2766 exit when Typ /= Any_Type;
2767 Get_Next_Interp (Index, It);
2768 end loop;
2770 end if;
2772 -- If Typ is Any_Type, it means no compatible pair of types was found
2774 if Typ = Any_Type then
2775 if Nkind (Parent (L)) in N_Op then
2776 Error_Msg_N ("incompatible types for operator", Parent (L));
2778 elsif Nkind (Parent (L)) = N_Range then
2779 Error_Msg_N ("incompatible types given in constraint", Parent (L));
2781 -- Ada 2005 (AI-251): Complete the error notification
2783 elsif Is_Class_Wide_Type (Etype (R))
2784 and then Is_Interface (Etype (Class_Wide_Type (Etype (R))))
2785 then
2786 Error_Msg_NE ("(Ada 2005) does not implement interface }",
2787 L, Etype (Class_Wide_Type (Etype (R))));
2789 -- Specialize message if one operand is a limited view, a priori
2790 -- unrelated to all other types.
2792 elsif From_Limited_With (Etype (R)) then
2793 Error_Msg_NE ("limited view of& not compatible with context",
2794 R, Etype (R));
2796 elsif From_Limited_With (Etype (L)) then
2797 Error_Msg_NE ("limited view of& not compatible with context",
2798 L, Etype (L));
2799 else
2800 Error_Msg_N ("incompatible types", Parent (L));
2801 end if;
2802 end if;
2804 return Typ;
2805 end Intersect_Types;
2807 -----------------------
2808 -- In_Generic_Actual --
2809 -----------------------
2811 function In_Generic_Actual (Exp : Node_Id) return Boolean is
2812 Par : constant Node_Id := Parent (Exp);
2814 begin
2815 if No (Par) then
2816 return False;
2818 elsif Nkind (Par) in N_Declaration then
2819 return
2820 Nkind (Par) = N_Object_Declaration
2821 and then Present (Corresponding_Generic_Association (Par));
2823 elsif Nkind (Par) = N_Object_Renaming_Declaration then
2824 return Present (Corresponding_Generic_Association (Par));
2826 elsif Nkind (Par) in N_Statement_Other_Than_Procedure_Call then
2827 return False;
2829 else
2830 return In_Generic_Actual (Par);
2831 end if;
2832 end In_Generic_Actual;
2834 -----------------
2835 -- Is_Ancestor --
2836 -----------------
2838 function Is_Ancestor
2839 (T1 : Entity_Id;
2840 T2 : Entity_Id;
2841 Use_Full_View : Boolean := False) return Boolean
2843 BT1 : Entity_Id;
2844 BT2 : Entity_Id;
2845 Par : Entity_Id;
2847 begin
2848 BT1 := Base_Type (T1);
2849 BT2 := Base_Type (T2);
2851 -- Handle underlying view of records with unknown discriminants using
2852 -- the original entity that motivated the construction of this
2853 -- underlying record view (see Build_Derived_Private_Type).
2855 if Is_Underlying_Record_View (BT1) then
2856 BT1 := Underlying_Record_View (BT1);
2857 end if;
2859 if Is_Underlying_Record_View (BT2) then
2860 BT2 := Underlying_Record_View (BT2);
2861 end if;
2863 if BT1 = BT2 then
2864 return True;
2866 -- The predicate must look past privacy
2868 elsif Is_Private_Type (T1)
2869 and then Present (Full_View (T1))
2870 and then BT2 = Base_Type (Full_View (T1))
2871 then
2872 return True;
2874 elsif Is_Private_Type (T2)
2875 and then Present (Full_View (T2))
2876 and then BT1 = Base_Type (Full_View (T2))
2877 then
2878 return True;
2880 else
2881 -- Obtain the parent of the base type of T2 (use the full view if
2882 -- allowed).
2884 if Use_Full_View
2885 and then Is_Private_Type (BT2)
2886 and then Present (Full_View (BT2))
2887 then
2888 -- No climbing needed if its full view is the root type
2890 if Full_View (BT2) = Root_Type (Full_View (BT2)) then
2891 return False;
2892 end if;
2894 Par := Etype (Full_View (BT2));
2896 else
2897 Par := Etype (BT2);
2898 end if;
2900 loop
2901 -- If there was a error on the type declaration, do not recurse
2903 if Error_Posted (Par) then
2904 return False;
2906 elsif BT1 = Base_Type (Par)
2907 or else (Is_Private_Type (T1)
2908 and then Present (Full_View (T1))
2909 and then Base_Type (Par) = Base_Type (Full_View (T1)))
2910 then
2911 return True;
2913 elsif Is_Private_Type (Par)
2914 and then Present (Full_View (Par))
2915 and then Full_View (Par) = BT1
2916 then
2917 return True;
2919 -- Root type found
2921 elsif Par = Root_Type (Par) then
2922 return False;
2924 -- Continue climbing
2926 else
2927 -- Use the full-view of private types (if allowed). Guard
2928 -- against infinite loops when full view has same type as
2929 -- parent, as can happen with interface extensions.
2931 if Use_Full_View
2932 and then Is_Private_Type (Par)
2933 and then Present (Full_View (Par))
2934 and then Par /= Etype (Full_View (Par))
2935 then
2936 Par := Etype (Full_View (Par));
2937 else
2938 Par := Etype (Par);
2939 end if;
2940 end if;
2941 end loop;
2942 end if;
2943 end Is_Ancestor;
2945 --------------------
2946 -- Is_Progenitor --
2947 --------------------
2949 function Is_Progenitor
2950 (Iface : Entity_Id;
2951 Typ : Entity_Id) return Boolean
2953 begin
2954 return Implements_Interface (Typ, Iface, Exclude_Parents => True);
2955 end Is_Progenitor;
2957 -------------------
2958 -- Is_Subtype_Of --
2959 -------------------
2961 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
2962 S : Entity_Id;
2964 begin
2965 S := Ancestor_Subtype (T1);
2966 while Present (S) loop
2967 if S = T2 then
2968 return True;
2969 else
2970 S := Ancestor_Subtype (S);
2971 end if;
2972 end loop;
2974 return False;
2975 end Is_Subtype_Of;
2977 -------------------------
2978 -- Is_Visible_Operator --
2979 -------------------------
2981 function Is_Visible_Operator (N : Node_Id; Typ : Entity_Id) return Boolean
2983 begin
2984 -- The predefined operators of the universal types are always visible
2986 if Typ in Universal_Integer | Universal_Real | Universal_Access then
2987 return True;
2989 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
2990 -- anonymous access types in universal_access equality operators.
2992 elsif Is_Anonymous_Access_Type (Typ) then
2993 return Ada_Version >= Ada_2005;
2995 -- Similar reasoning for special types used for composite types before
2996 -- type resolution is done.
2998 elsif Typ = Any_Composite or else Typ = Any_String then
2999 return True;
3001 -- Within an instance, the predefined operators of the formal types are
3002 -- visible and, for the other types, the generic package declaration has
3003 -- already been successfully analyzed. Likewise for an inlined body.
3005 elsif In_Instance or else In_Inlined_Body then
3006 return True;
3008 -- If the operation is given in functional notation and the prefix is an
3009 -- expanded name, then the operator is visible if the prefix is the scope
3010 -- of the type, or System if the type is declared in an extension of it.
3012 elsif Nkind (N) = N_Function_Call
3013 and then Nkind (Name (N)) = N_Expanded_Name
3014 then
3015 declare
3016 Pref : constant Entity_Id := Entity (Prefix (Name (N)));
3017 Scop : constant Entity_Id := Scope (Typ);
3019 begin
3020 return Pref = Scop
3021 or else (Present (System_Aux_Id)
3022 and then Scop = System_Aux_Id
3023 and then Pref = Scope (Scop));
3024 end;
3026 -- Otherwise the operator is visible when the type is visible
3028 else
3029 return Is_Potentially_Use_Visible (Typ)
3030 or else In_Use (Typ)
3031 or else (In_Use (Scope (Typ)) and then not Is_Hidden (Typ))
3032 or else In_Open_Scopes (Scope (Typ));
3033 end if;
3034 end Is_Visible_Operator;
3036 ------------------
3037 -- List_Interps --
3038 ------------------
3040 procedure List_Interps (Nam : Node_Id; Err : Node_Id) is
3041 Index : Interp_Index;
3042 It : Interp;
3044 begin
3045 Get_First_Interp (Nam, Index, It);
3046 while Present (It.Nam) loop
3047 if Scope (It.Nam) = Standard_Standard
3048 and then Scope (It.Typ) /= Standard_Standard
3049 then
3050 Error_Msg_Sloc := Sloc (Parent (It.Typ));
3051 Error_Msg_NE ("\\& (inherited) declared#!", Err, It.Nam);
3053 else
3054 Error_Msg_Sloc := Sloc (It.Nam);
3055 Error_Msg_NE ("\\& declared#!", Err, It.Nam);
3056 end if;
3058 Get_Next_Interp (Index, It);
3059 end loop;
3060 end List_Interps;
3062 -----------------
3063 -- New_Interps --
3064 -----------------
3066 procedure New_Interps (N : Node_Id) is
3067 begin
3068 All_Interp.Append (No_Interp);
3070 -- Add or rewrite the existing node
3071 Last_Overloaded := N;
3072 Interp_Map.Set (N, All_Interp.Last);
3073 Set_Is_Overloaded (N, True);
3074 end New_Interps;
3076 ---------------------------
3077 -- Operator_Matches_Spec --
3078 ---------------------------
3080 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean is
3081 New_First_F : constant Entity_Id := First_Formal (New_S);
3082 Op_Name : constant Name_Id := Chars (Op);
3083 T : constant Entity_Id := Etype (New_S);
3084 New_F : Entity_Id;
3085 Num : Nat;
3086 Old_F : Entity_Id;
3087 T1 : Entity_Id;
3088 T2 : Entity_Id;
3090 begin
3091 -- To verify that a predefined operator matches a given signature, do a
3092 -- case analysis of the operator classes. Function can have one or two
3093 -- formals and must have the proper result type.
3095 New_F := New_First_F;
3096 Old_F := First_Formal (Op);
3097 Num := 0;
3098 while Present (New_F) and then Present (Old_F) loop
3099 Num := Num + 1;
3100 Next_Formal (New_F);
3101 Next_Formal (Old_F);
3102 end loop;
3104 -- Definite mismatch if different number of parameters
3106 if Present (Old_F) or else Present (New_F) then
3107 return False;
3109 -- Unary operators
3111 elsif Num = 1 then
3112 T1 := Etype (New_First_F);
3114 if Op_Name in Name_Op_Subtract | Name_Op_Add | Name_Op_Abs then
3115 return Base_Type (T1) = Base_Type (T)
3116 and then Is_Numeric_Type (T);
3118 elsif Op_Name = Name_Op_Not then
3119 return Base_Type (T1) = Base_Type (T)
3120 and then Valid_Boolean_Arg (Base_Type (T));
3122 else
3123 return False;
3124 end if;
3126 -- Binary operators
3128 else
3129 T1 := Etype (New_First_F);
3130 T2 := Etype (Next_Formal (New_First_F));
3132 if Op_Name in Name_Op_And | Name_Op_Or | Name_Op_Xor then
3133 return Base_Type (T1) = Base_Type (T2)
3134 and then Base_Type (T1) = Base_Type (T)
3135 and then Valid_Boolean_Arg (Base_Type (T));
3137 elsif Op_Name in Name_Op_Eq | Name_Op_Ne then
3138 return Base_Type (T1) = Base_Type (T2)
3139 and then Valid_Equality_Arg (T1)
3140 and then Is_Boolean_Type (T);
3142 elsif Op_Name in Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge
3143 then
3144 return Base_Type (T1) = Base_Type (T2)
3145 and then Valid_Comparison_Arg (T1)
3146 and then Is_Boolean_Type (T);
3148 elsif Op_Name in Name_Op_Add | Name_Op_Subtract then
3149 return Base_Type (T1) = Base_Type (T2)
3150 and then Base_Type (T1) = Base_Type (T)
3151 and then Is_Numeric_Type (T);
3153 -- For division and multiplication, a user-defined function does not
3154 -- match the predefined universal_fixed operation, except in Ada 83.
3156 elsif Op_Name = Name_Op_Divide then
3157 return (Base_Type (T1) = Base_Type (T2)
3158 and then Base_Type (T1) = Base_Type (T)
3159 and then Is_Numeric_Type (T)
3160 and then (not Is_Fixed_Point_Type (T)
3161 or else Ada_Version = Ada_83))
3163 -- Mixed_Mode operations on fixed-point types
3165 or else (Base_Type (T1) = Base_Type (T)
3166 and then Base_Type (T2) = Base_Type (Standard_Integer)
3167 and then Is_Fixed_Point_Type (T))
3169 -- A user defined operator can also match (and hide) a mixed
3170 -- operation on universal literals.
3172 or else (Is_Integer_Type (T2)
3173 and then Is_Floating_Point_Type (T1)
3174 and then Base_Type (T1) = Base_Type (T));
3176 elsif Op_Name = Name_Op_Multiply then
3177 return (Base_Type (T1) = Base_Type (T2)
3178 and then Base_Type (T1) = Base_Type (T)
3179 and then Is_Numeric_Type (T)
3180 and then (not Is_Fixed_Point_Type (T)
3181 or else Ada_Version = Ada_83))
3183 -- Mixed_Mode operations on fixed-point types
3185 or else (Base_Type (T1) = Base_Type (T)
3186 and then Base_Type (T2) = Base_Type (Standard_Integer)
3187 and then Is_Fixed_Point_Type (T))
3189 or else (Base_Type (T2) = Base_Type (T)
3190 and then Base_Type (T1) = Base_Type (Standard_Integer)
3191 and then Is_Fixed_Point_Type (T))
3193 or else (Is_Integer_Type (T2)
3194 and then Is_Floating_Point_Type (T1)
3195 and then Base_Type (T1) = Base_Type (T))
3197 or else (Is_Integer_Type (T1)
3198 and then Is_Floating_Point_Type (T2)
3199 and then Base_Type (T2) = Base_Type (T));
3201 elsif Op_Name in Name_Op_Mod | Name_Op_Rem then
3202 return Base_Type (T1) = Base_Type (T2)
3203 and then Base_Type (T1) = Base_Type (T)
3204 and then Is_Integer_Type (T);
3206 elsif Op_Name = Name_Op_Expon then
3207 return Base_Type (T1) = Base_Type (T)
3208 and then Is_Numeric_Type (T)
3209 and then Base_Type (T2) = Base_Type (Standard_Integer);
3211 elsif Op_Name = Name_Op_Concat then
3212 return Is_Array_Type (T)
3213 and then (Base_Type (T) = Base_Type (Etype (Op)))
3214 and then (Base_Type (T1) = Base_Type (T)
3215 or else
3216 Base_Type (T1) = Base_Type (Component_Type (T)))
3217 and then (Base_Type (T2) = Base_Type (T)
3218 or else
3219 Base_Type (T2) = Base_Type (Component_Type (T)));
3221 else
3222 return False;
3223 end if;
3224 end if;
3225 end Operator_Matches_Spec;
3227 -------------------
3228 -- Remove_Interp --
3229 -------------------
3231 procedure Remove_Interp (I : in out Interp_Index) is
3232 II : Interp_Index;
3234 begin
3235 -- Find end of interp list and copy downward to erase the discarded one
3237 II := I + 1;
3238 while Present (All_Interp.Table (II).Typ) loop
3239 II := II + 1;
3240 end loop;
3242 for J in I + 1 .. II loop
3243 All_Interp.Table (J - 1) := All_Interp.Table (J);
3244 end loop;
3246 -- Back up interp index to insure that iterator will pick up next
3247 -- available interpretation.
3249 I := I - 1;
3250 end Remove_Interp;
3252 ------------------
3253 -- Save_Interps --
3254 ------------------
3256 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id) is
3257 Old_Ind : Interp_Index;
3258 O_N : Node_Id;
3260 begin
3261 if Is_Overloaded (Old_N) then
3262 Set_Is_Overloaded (New_N);
3264 if Nkind (Old_N) = N_Selected_Component
3265 and then Is_Overloaded (Selector_Name (Old_N))
3266 then
3267 O_N := Selector_Name (Old_N);
3268 else
3269 O_N := Old_N;
3270 end if;
3272 Old_Ind := Interp_Map.Get (O_N);
3273 pragma Assert (Old_Ind >= 0);
3275 New_Interps (New_N);
3276 Interp_Map.Set (New_N, Old_Ind);
3277 end if;
3278 end Save_Interps;
3280 -------------------
3281 -- Specific_Type --
3282 -------------------
3284 function Specific_Type (Typ_1, Typ_2 : Entity_Id) return Entity_Id is
3285 T1 : constant Entity_Id := Available_View (Typ_1);
3286 T2 : constant Entity_Id := Available_View (Typ_2);
3287 B1 : constant Entity_Id := Base_Type (T1);
3288 B2 : constant Entity_Id := Base_Type (T2);
3290 function Is_Remote_Access (T : Entity_Id) return Boolean;
3291 -- Check whether T is the equivalent type of a remote access type.
3292 -- If distribution is enabled, T is a legal context for Null.
3294 ----------------------
3295 -- Is_Remote_Access --
3296 ----------------------
3298 function Is_Remote_Access (T : Entity_Id) return Boolean is
3299 begin
3300 return Is_Record_Type (T)
3301 and then (Is_Remote_Call_Interface (T)
3302 or else Is_Remote_Types (T))
3303 and then Present (Corresponding_Remote_Type (T))
3304 and then Is_Access_Type (Corresponding_Remote_Type (T));
3305 end Is_Remote_Access;
3307 -- Start of processing for Specific_Type
3309 begin
3310 if T1 = Any_Type or else T2 = Any_Type then
3311 return Any_Type;
3312 end if;
3314 if B1 = B2 then
3315 return B1;
3317 elsif (T1 = Universal_Integer and then Is_Integer_Type (T2))
3318 or else (T1 = Universal_Real and then Is_Real_Type (T2))
3319 or else (T1 = Universal_Fixed and then Is_Fixed_Point_Type (T2))
3320 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
3321 or else (T1 = Any_Modular and then Is_Modular_Integer_Type (T2))
3322 or else (T1 = Any_Character and then Is_Character_Type (T2))
3323 or else (T1 = Any_String and then Is_String_Type (T2))
3324 or else (T1 = Any_Composite and then Is_Aggregate_Type (T2))
3325 then
3326 return B2;
3328 elsif (T1 = Universal_Access
3329 or else Ekind (T1) in E_Allocator_Type | E_Access_Attribute_Type)
3330 and then (Is_Access_Type (T2) or else Is_Remote_Access (T2))
3331 then
3332 return B2;
3334 elsif T1 = Raise_Type then
3335 return B2;
3337 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
3338 or else (T2 = Universal_Real and then Is_Real_Type (T1))
3339 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
3340 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
3341 or else (T2 = Any_Modular and then Is_Modular_Integer_Type (T1))
3342 or else (T2 = Any_Character and then Is_Character_Type (T1))
3343 or else (T2 = Any_String and then Is_String_Type (T1))
3344 or else (T2 = Any_Composite and then Is_Aggregate_Type (T1))
3345 then
3346 return B1;
3348 elsif (T2 = Universal_Access
3349 or else Ekind (T2) in E_Allocator_Type | E_Access_Attribute_Type)
3350 and then (Is_Access_Type (T1) or else Is_Remote_Access (T1))
3351 then
3352 return B1;
3354 elsif T2 = Raise_Type then
3355 return B1;
3357 -- Ada 2005 (AI-251): T1 and T2 are class-wide types, and T2 is an
3358 -- interface, return T1, and vice versa.
3360 elsif Is_Class_Wide_Type (T1)
3361 and then Is_Class_Wide_Type (T2)
3362 and then Is_Interface (Etype (T2))
3363 then
3364 return B1;
3366 elsif Is_Class_Wide_Type (T2)
3367 and then Is_Class_Wide_Type (T1)
3368 and then Is_Interface (Etype (T1))
3369 then
3370 return B2;
3372 -- Ada 2005 (AI-251): T1 is a concrete type that implements the
3373 -- class-wide interface T2, return T1, and vice versa.
3375 elsif Is_Tagged_Type (T1)
3376 and then Is_Class_Wide_Type (T2)
3377 and then Is_Interface (Etype (T2))
3378 and then Interface_Present_In_Ancestor (Typ => T1,
3379 Iface => Etype (T2))
3380 then
3381 return B1;
3383 elsif Is_Tagged_Type (T2)
3384 and then Is_Class_Wide_Type (T1)
3385 and then Is_Interface (Etype (T1))
3386 and then Interface_Present_In_Ancestor (Typ => T2,
3387 Iface => Etype (T1))
3388 then
3389 return B2;
3391 elsif Is_Class_Wide_Type (T1)
3392 and then Is_Ancestor (Root_Type (T1), T2)
3393 then
3394 return B1;
3396 elsif Is_Class_Wide_Type (T2)
3397 and then Is_Ancestor (Root_Type (T2), T1)
3398 then
3399 return B2;
3401 elsif Is_Access_Type (T1)
3402 and then Is_Access_Type (T2)
3403 and then Is_Class_Wide_Type (Designated_Type (T1))
3404 and then not Is_Class_Wide_Type (Designated_Type (T2))
3405 and then
3406 Is_Ancestor (Root_Type (Designated_Type (T1)), Designated_Type (T2))
3407 then
3408 return T1;
3410 elsif Is_Access_Type (T1)
3411 and then Is_Access_Type (T2)
3412 and then Is_Class_Wide_Type (Designated_Type (T2))
3413 and then not Is_Class_Wide_Type (Designated_Type (T1))
3414 and then
3415 Is_Ancestor (Root_Type (Designated_Type (T2)), Designated_Type (T1))
3416 then
3417 return T2;
3419 elsif Ekind (B1) in E_Access_Subprogram_Type
3420 | E_Access_Protected_Subprogram_Type
3421 and then Ekind (Designated_Type (B1)) /= E_Subprogram_Type
3422 and then Is_Access_Type (T2)
3423 then
3424 return T2;
3426 elsif Ekind (B2) in E_Access_Subprogram_Type
3427 | E_Access_Protected_Subprogram_Type
3428 and then Ekind (Designated_Type (B2)) /= E_Subprogram_Type
3429 and then Is_Access_Type (T1)
3430 then
3431 return T1;
3433 -- Ada 2005 (AI-230): Support the following operators:
3435 -- function "=" (L, R : universal_access) return Boolean;
3436 -- function "/=" (L, R : universal_access) return Boolean;
3438 -- Pool-specific access types (E_Access_Type) are not covered by these
3439 -- operators because of the legality rule of 4.5.2(9.2): "The operands
3440 -- of the equality operators for universal_access shall be convertible
3441 -- to one another (see 4.6)". For example, considering the type decla-
3442 -- ration "type P is access Integer" and an anonymous access to Integer,
3443 -- P is convertible to "access Integer" by 4.6 (24.11-24.15), but there
3444 -- is no rule in 4.6 that allows "access Integer" to be converted to P.
3445 -- Note that this does not preclude one operand to be a pool-specific
3446 -- access type, as a previous version of this code enforced.
3448 elsif Is_Anonymous_Access_Type (T1)
3449 and then Is_Access_Type (T2)
3450 and then Ada_Version >= Ada_2005
3451 then
3452 return T1;
3454 elsif Is_Anonymous_Access_Type (T2)
3455 and then Is_Access_Type (T1)
3456 and then Ada_Version >= Ada_2005
3457 then
3458 return T2;
3460 -- In instances, also check private views the same way as Covers
3462 elsif Is_Private_Type (T1) and then In_Instance then
3463 if Present (Full_View (T1)) then
3464 return Specific_Type (Full_View (T1), T2);
3466 elsif Present (Underlying_Full_View (T1)) then
3467 return Specific_Type (Underlying_Full_View (T1), T2);
3468 end if;
3470 elsif Is_Private_Type (T2) and then In_Instance then
3471 if Present (Full_View (T2)) then
3472 return Specific_Type (T1, Full_View (T2));
3474 elsif Present (Underlying_Full_View (T2)) then
3475 return Specific_Type (T1, Underlying_Full_View (T2));
3476 end if;
3477 end if;
3479 -- If none of the above cases applies, types are not compatible
3481 return Any_Type;
3482 end Specific_Type;
3484 ---------------------
3485 -- Set_Abstract_Op --
3486 ---------------------
3488 procedure Set_Abstract_Op (I : Interp_Index; V : Entity_Id) is
3489 begin
3490 All_Interp.Table (I).Abstract_Op := V;
3491 end Set_Abstract_Op;
3493 -----------------------
3494 -- Valid_Boolean_Arg --
3495 -----------------------
3497 -- In addition to booleans and arrays of booleans, we must include
3498 -- aggregates as valid boolean arguments, because in the first pass of
3499 -- resolution their components are not examined. If it turns out not to be
3500 -- an aggregate of booleans, this will be diagnosed in Resolve.
3501 -- Any_Composite must be checked for prior to the array type checks because
3502 -- Any_Composite does not have any associated indexes.
3504 function Valid_Boolean_Arg (T : Entity_Id) return Boolean is
3505 begin
3506 if Is_Boolean_Type (T)
3507 or else Is_Modular_Integer_Type (T)
3508 or else T = Universal_Integer
3509 or else T = Any_Composite
3510 or else T = Raise_Type
3511 then
3512 return True;
3514 elsif Is_Array_Type (T)
3515 and then Number_Dimensions (T) = 1
3516 and then Is_Boolean_Type (Component_Type (T))
3517 and then
3518 ((not Is_Private_Composite (T) and then not Is_Limited_Composite (T))
3519 or else In_Instance
3520 or else Available_Full_View_Of_Component (T))
3521 then
3522 return True;
3524 else
3525 return False;
3526 end if;
3527 end Valid_Boolean_Arg;
3529 --------------------------
3530 -- Valid_Comparison_Arg --
3531 --------------------------
3533 -- See above for the reason why aggregates and strings are included
3535 function Valid_Comparison_Arg (T : Entity_Id) return Boolean is
3536 begin
3537 if Is_Discrete_Type (T) or else Is_Real_Type (T) then
3538 return True;
3540 elsif T = Any_Composite or else T = Any_String then
3541 return True;
3543 elsif Is_Array_Type (T)
3544 and then Number_Dimensions (T) = 1
3545 and then Is_Discrete_Type (Component_Type (T))
3546 and then (not Is_Private_Composite (T) or else In_Instance)
3547 and then (not Is_Limited_Composite (T) or else In_Instance)
3548 then
3549 return True;
3551 elsif Is_Array_Type (T)
3552 and then Number_Dimensions (T) = 1
3553 and then Is_Discrete_Type (Component_Type (T))
3554 and then Available_Full_View_Of_Component (T)
3555 then
3556 return True;
3558 elsif Is_String_Type (T) then
3559 return True;
3561 else
3562 return False;
3563 end if;
3564 end Valid_Comparison_Arg;
3566 ------------------------
3567 -- Valid_Equality_Arg --
3568 ------------------------
3570 -- Same reasoning as above but implicit because of the nonlimited test
3572 function Valid_Equality_Arg (T : Entity_Id) return Boolean is
3573 begin
3574 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
3575 -- anonymous access types in universal_access equality operators.
3577 if Is_Anonymous_Access_Type (T) then
3578 return Ada_Version >= Ada_2005;
3580 elsif not Is_Limited_Type (T) then
3581 return True;
3583 elsif Is_Array_Type (T)
3584 and then not Is_Limited_Type (Component_Type (T))
3585 and then Available_Full_View_Of_Component (T)
3586 then
3587 return True;
3589 else
3590 return False;
3591 end if;
3592 end Valid_Equality_Arg;
3594 ------------------
3595 -- Write_Interp --
3596 ------------------
3598 procedure Write_Interp (It : Interp) is
3599 begin
3600 Write_Str ("Nam: ");
3601 Print_Tree_Node (It.Nam);
3602 Write_Str ("Typ: ");
3603 Print_Tree_Node (It.Typ);
3604 Write_Str ("Abstract_Op: ");
3605 Print_Tree_Node (It.Abstract_Op);
3606 end Write_Interp;
3608 ---------------------
3609 -- Write_Overloads --
3610 ---------------------
3612 procedure Write_Overloads (N : Node_Id) is
3613 I : Interp_Index;
3614 It : Interp;
3615 Nam : Entity_Id;
3617 begin
3618 Write_Str ("Overloads: ");
3619 Print_Node_Briefly (N);
3621 if not Is_Overloaded (N) then
3622 if Is_Entity_Name (N) then
3623 Write_Line ("Non-overloaded entity ");
3624 Write_Entity_Info (Entity (N), " ");
3625 end if;
3627 elsif Nkind (N) not in N_Has_Entity then
3628 Get_First_Interp (N, I, It);
3629 while Present (It.Nam) loop
3630 Write_Int (Int (It.Typ));
3631 Write_Str (" ");
3632 Write_Name (Chars (It.Typ));
3633 Write_Eol;
3634 Get_Next_Interp (I, It);
3635 end loop;
3637 else
3638 Get_First_Interp (N, I, It);
3639 Write_Line ("Overloaded entity ");
3640 Write_Line (" Name Type Abstract Op");
3641 Write_Line ("===============================================");
3642 Nam := It.Nam;
3644 while Present (Nam) loop
3645 Write_Int (Int (Nam));
3646 Write_Str (" ");
3647 Write_Name (Chars (Nam));
3648 Write_Str (" ");
3649 Write_Int (Int (It.Typ));
3650 Write_Str (" ");
3651 Write_Name (Chars (It.Typ));
3653 if Present (It.Abstract_Op) then
3654 Write_Str (" ");
3655 Write_Int (Int (It.Abstract_Op));
3656 Write_Str (" ");
3657 Write_Name (Chars (It.Abstract_Op));
3658 end if;
3660 Write_Eol;
3661 Get_Next_Interp (I, It);
3662 Nam := It.Nam;
3663 end loop;
3664 end if;
3665 end Write_Overloads;
3667 end Sem_Type;