1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
34 with Output; use Output;
36 with Sem_Ch6; use Sem_Ch6;
37 with Sem_Ch8; use Sem_Ch8;
38 with Sem_Util; use Sem_Util;
39 with Stand; use Stand;
40 with Sinfo; use Sinfo;
41 with Snames; use Snames;
43 with Uintp; use Uintp;
45 package body Sem_Type is
51 -- The following data structures establish a mapping between nodes and
52 -- their interpretations. An overloaded node has an entry in Interp_Map,
53 -- which in turn contains a pointer into the All_Interp array. The
54 -- interpretations of a given node are contiguous in All_Interp. Each
55 -- set of interpretations is terminated with the marker No_Interp.
56 -- In order to speed up the retrieval of the interpretations of an
57 -- overloaded node, the Interp_Map table is accessed by means of a simple
58 -- hashing scheme, and the entries in Interp_Map are chained. The heads
59 -- of clash lists are stored in array Headers.
61 -- Headers Interp_Map All_Interp
63 -- _ ------- ----------
64 -- |_| |_____| --->|interp1 |
65 -- |_|---------->|node | | |interp2 |
66 -- |_| |index|---------| |nointerp|
71 -- This scheme does not currently reclaim interpretations. In principle,
72 -- after a unit is compiled, all overloadings have been resolved, and the
73 -- candidate interpretations should be deleted. This should be easier
74 -- now than with the previous scheme???
76 package All_Interp is new Table.Table (
77 Table_Component_Type => Interp,
78 Table_Index_Type => Int,
80 Table_Initial => Alloc.All_Interp_Initial,
81 Table_Increment => Alloc.All_Interp_Increment,
82 Table_Name => "All_Interp");
84 type Interp_Ref is record
90 Header_Size : constant Int := 2 ** 12;
91 No_Entry : constant Int := -1;
92 Headers : array (0 .. Header_Size) of Int := (others => No_Entry);
94 package Interp_Map is new Table.Table (
95 Table_Component_Type => Interp_Ref,
96 Table_Index_Type => Int,
98 Table_Initial => Alloc.Interp_Map_Initial,
99 Table_Increment => Alloc.Interp_Map_Increment,
100 Table_Name => "Interp_Map");
102 function Hash (N : Node_Id) return Int;
103 -- A trivial hashing function for nodes, used to insert an overloaded
104 -- node into the Interp_Map table.
106 -------------------------------------
107 -- Handling of Overload Resolution --
108 -------------------------------------
110 -- Overload resolution uses two passes over the syntax tree of a complete
111 -- context. In the first, bottom-up pass, the types of actuals in calls
112 -- are used to resolve possibly overloaded subprogram and operator names.
113 -- In the second top-down pass, the type of the context (for example the
114 -- condition in a while statement) is used to resolve a possibly ambiguous
115 -- call, and the unique subprogram name in turn imposes a specific context
116 -- on each of its actuals.
118 -- Most expressions are in fact unambiguous, and the bottom-up pass is
119 -- sufficient to resolve most everything. To simplify the common case,
120 -- names and expressions carry a flag Is_Overloaded to indicate whether
121 -- they have more than one interpretation. If the flag is off, then each
122 -- name has already a unique meaning and type, and the bottom-up pass is
123 -- sufficient (and much simpler).
125 --------------------------
126 -- Operator Overloading --
127 --------------------------
129 -- The visibility of operators is handled differently from that of
130 -- other entities. We do not introduce explicit versions of primitive
131 -- operators for each type definition. As a result, there is only one
132 -- entity corresponding to predefined addition on all numeric types, etc.
133 -- The back-end resolves predefined operators according to their type.
134 -- The visibility of primitive operations then reduces to the visibility
135 -- of the resulting type: (a + b) is a legal interpretation of some
136 -- primitive operator + if the type of the result (which must also be
137 -- the type of a and b) is directly visible (i.e. either immediately
138 -- visible or use-visible.)
140 -- User-defined operators are treated like other functions, but the
141 -- visibility of these user-defined operations must be special-cased
142 -- to determine whether they hide or are hidden by predefined operators.
143 -- The form P."+" (x, y) requires additional handling.
145 -- Concatenation is treated more conventionally: for every one-dimensional
146 -- array type we introduce a explicit concatenation operator. This is
147 -- necessary to handle the case of (element & element => array) which
148 -- cannot be handled conveniently if there is no explicit instance of
149 -- resulting type of the operation.
151 -----------------------
152 -- Local Subprograms --
153 -----------------------
155 procedure All_Overloads;
156 pragma Warnings (Off, All_Overloads);
157 -- Debugging procedure: list full contents of Overloads table.
159 procedure New_Interps (N : Node_Id);
160 -- Initialize collection of interpretations for the given node, which is
161 -- either an overloaded entity, or an operation whose arguments have
162 -- multiple intepretations. Interpretations can be added to only one
165 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
166 -- If T1 and T2 are compatible, return the one that is not
167 -- universal or is not a "class" type (any_character, etc).
173 procedure Add_One_Interp
177 Opnd_Type : Entity_Id := Empty)
179 Vis_Type : Entity_Id;
181 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id);
182 -- Add one interpretation to node. Node is already known to be
183 -- overloaded. Add new interpretation if not hidden by previous
184 -- one, and remove previous one if hidden by new one.
186 function Is_Universal_Operation (Op : Entity_Id) return Boolean;
187 -- True if the entity is a predefined operator and the operands have
188 -- a universal Interpretation.
194 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id) is
195 Index : Interp_Index;
199 Get_First_Interp (N, Index, It);
201 while Present (It.Nam) loop
203 -- A user-defined subprogram hides another declared at an outer
204 -- level, or one that is use-visible. So return if previous
205 -- definition hides new one (which is either in an outer
206 -- scope, or use-visible). Note that for functions use-visible
207 -- is the same as potentially use-visible. If new one hides
208 -- previous one, replace entry in table of interpretations.
209 -- If this is a universal operation, retain the operator in case
210 -- preference rule applies.
212 if (((Ekind (Name) = E_Function or else Ekind (Name) = E_Procedure)
213 and then Ekind (Name) = Ekind (It.Nam))
214 or else (Ekind (Name) = E_Operator
215 and then Ekind (It.Nam) = E_Function))
217 and then Is_Immediately_Visible (It.Nam)
218 and then Type_Conformant (Name, It.Nam)
219 and then Base_Type (It.Typ) = Base_Type (T)
221 if Is_Universal_Operation (Name) then
224 -- If node is an operator symbol, we have no actuals with
225 -- which to check hiding, and this is done in full in the
226 -- caller (Analyze_Subprogram_Renaming) so we include the
227 -- predefined operator in any case.
229 elsif Nkind (N) = N_Operator_Symbol
230 or else (Nkind (N) = N_Expanded_Name
232 Nkind (Selector_Name (N)) = N_Operator_Symbol)
236 elsif not In_Open_Scopes (Scope (Name))
237 or else Scope_Depth (Scope (Name))
238 <= Scope_Depth (Scope (It.Nam))
240 -- If ambiguity within instance, and entity is not an
241 -- implicit operation, save for later disambiguation.
243 if Scope (Name) = Scope (It.Nam)
244 and then not Is_Inherited_Operation (Name)
253 All_Interp.Table (Index).Nam := Name;
257 -- Avoid making duplicate entries in overloads
260 and then Base_Type (It.Typ) = Base_Type (T)
264 -- Otherwise keep going
267 Get_Next_Interp (Index, It);
272 -- On exit, enter new interpretation. The context, or a preference
273 -- rule, will resolve the ambiguity on the second pass.
275 All_Interp.Table (All_Interp.Last) := (Name, Typ);
276 All_Interp.Increment_Last;
277 All_Interp.Table (All_Interp.Last) := No_Interp;
280 ----------------------------
281 -- Is_Universal_Operation --
282 ----------------------------
284 function Is_Universal_Operation (Op : Entity_Id) return Boolean is
288 if Ekind (Op) /= E_Operator then
291 elsif Nkind (N) in N_Binary_Op then
292 return Present (Universal_Interpretation (Left_Opnd (N)))
293 and then Present (Universal_Interpretation (Right_Opnd (N)));
295 elsif Nkind (N) in N_Unary_Op then
296 return Present (Universal_Interpretation (Right_Opnd (N)));
298 elsif Nkind (N) = N_Function_Call then
299 Arg := First_Actual (N);
301 while Present (Arg) loop
303 if No (Universal_Interpretation (Arg)) then
315 end Is_Universal_Operation;
317 -- Start of processing for Add_One_Interp
320 -- If the interpretation is a predefined operator, verify that the
321 -- result type is visible, or that the entity has already been
322 -- resolved (case of an instantiation node that refers to a predefined
323 -- operation, or an internally generated operator node, or an operator
324 -- given as an expanded name). If the operator is a comparison or
325 -- equality, it is the type of the operand that matters to determine
326 -- whether the operator is visible. In an instance, the check is not
327 -- performed, given that the operator was visible in the generic.
329 if Ekind (E) = E_Operator then
331 if Present (Opnd_Type) then
332 Vis_Type := Opnd_Type;
334 Vis_Type := Base_Type (T);
337 if In_Open_Scopes (Scope (Vis_Type))
338 or else Is_Potentially_Use_Visible (Vis_Type)
339 or else In_Use (Vis_Type)
340 or else (In_Use (Scope (Vis_Type))
341 and then not Is_Hidden (Vis_Type))
342 or else Nkind (N) = N_Expanded_Name
343 or else (Nkind (N) in N_Op and then E = Entity (N))
348 -- If the node is given in functional notation and the prefix
349 -- is an expanded name, then the operator is visible if the
350 -- prefix is the scope of the result type as well. If the
351 -- operator is (implicitly) defined in an extension of system,
352 -- it is know to be valid (see Defined_In_Scope, sem_ch4.adb).
354 elsif Nkind (N) = N_Function_Call
355 and then Nkind (Name (N)) = N_Expanded_Name
356 and then (Entity (Prefix (Name (N))) = Scope (Base_Type (T))
357 or else Entity (Prefix (Name (N))) = Scope (Vis_Type)
358 or else Scope (Vis_Type) = System_Aux_Id)
362 -- Save type for subsequent error message, in case no other
363 -- interpretation is found.
366 Candidate_Type := Vis_Type;
370 -- In an instance, an abstract non-dispatching operation cannot
371 -- be a candidate interpretation, because it could not have been
372 -- one in the generic (it may be a spurious overloading in the
376 and then Is_Abstract (E)
377 and then not Is_Dispatching_Operation (E)
382 -- If this is the first interpretation of N, N has type Any_Type.
383 -- In that case place the new type on the node. If one interpretation
384 -- already exists, indicate that the node is overloaded, and store
385 -- both the previous and the new interpretation in All_Interp. If
386 -- this is a later interpretation, just add it to the set.
388 if Etype (N) = Any_Type then
393 -- Record both the operator or subprogram name, and its type.
395 if Nkind (N) in N_Op or else Is_Entity_Name (N) then
402 -- Either there is no current interpretation in the table for any
403 -- node or the interpretation that is present is for a different
404 -- node. In both cases add a new interpretation to the table.
406 elsif Interp_Map.Last < 0
408 (Interp_Map.Table (Interp_Map.Last).Node /= N
409 and then not Is_Overloaded (N))
413 if (Nkind (N) in N_Op or else Is_Entity_Name (N))
414 and then Present (Entity (N))
416 Add_Entry (Entity (N), Etype (N));
418 elsif (Nkind (N) = N_Function_Call
419 or else Nkind (N) = N_Procedure_Call_Statement)
420 and then (Nkind (Name (N)) = N_Operator_Symbol
421 or else Is_Entity_Name (Name (N)))
423 Add_Entry (Entity (Name (N)), Etype (N));
426 -- Overloaded prefix in indexed or selected component,
427 -- or call whose name is an expresion or another call.
429 Add_Entry (Etype (N), Etype (N));
443 procedure All_Overloads is
445 for J in All_Interp.First .. All_Interp.Last loop
447 if Present (All_Interp.Table (J).Nam) then
448 Write_Entity_Info (All_Interp.Table (J). Nam, " ");
450 Write_Str ("No Interp");
453 Write_Str ("=================");
458 ---------------------
459 -- Collect_Interps --
460 ---------------------
462 procedure Collect_Interps (N : Node_Id) is
463 Ent : constant Entity_Id := Entity (N);
465 First_Interp : Interp_Index;
470 -- Unconditionally add the entity that was initially matched
472 First_Interp := All_Interp.Last;
473 Add_One_Interp (N, Ent, Etype (N));
475 -- For expanded name, pick up all additional entities from the
476 -- same scope, since these are obviously also visible. Note that
477 -- these are not necessarily contiguous on the homonym chain.
479 if Nkind (N) = N_Expanded_Name then
481 while Present (H) loop
482 if Scope (H) = Scope (Entity (N)) then
483 Add_One_Interp (N, H, Etype (H));
489 -- Case of direct name
492 -- First, search the homonym chain for directly visible entities
494 H := Current_Entity (Ent);
495 while Present (H) loop
496 exit when (not Is_Overloadable (H))
497 and then Is_Immediately_Visible (H);
499 if Is_Immediately_Visible (H)
502 -- Only add interpretation if not hidden by an inner
503 -- immediately visible one.
505 for J in First_Interp .. All_Interp.Last - 1 loop
507 -- Current homograph is not hidden. Add to overloads.
509 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
512 -- Homograph is hidden, unless it is a predefined operator.
514 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
516 -- A homograph in the same scope can occur within an
517 -- instantiation, the resulting ambiguity has to be
520 if Scope (H) = Scope (Ent)
522 and then not Is_Inherited_Operation (H)
524 All_Interp.Table (All_Interp.Last) := (H, Etype (H));
525 All_Interp.Increment_Last;
526 All_Interp.Table (All_Interp.Last) := No_Interp;
529 elsif Scope (H) /= Standard_Standard then
535 -- On exit, we know that current homograph is not hidden.
537 Add_One_Interp (N, H, Etype (H));
540 Write_Str ("Add overloaded Interpretation ");
550 -- Scan list of homographs for use-visible entities only.
552 H := Current_Entity (Ent);
554 while Present (H) loop
555 if Is_Potentially_Use_Visible (H)
557 and then Is_Overloadable (H)
559 for J in First_Interp .. All_Interp.Last - 1 loop
561 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
564 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
565 goto Next_Use_Homograph;
569 Add_One_Interp (N, H, Etype (H));
572 <<Next_Use_Homograph>>
577 if All_Interp.Last = First_Interp + 1 then
579 -- The original interpretation is in fact not overloaded.
581 Set_Is_Overloaded (N, False);
589 function Covers (T1, T2 : Entity_Id) return Boolean is
591 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean;
592 -- In an instance the proper view may not always be correct for
593 -- private types, but private and full view are compatible. This
594 -- removes spurious errors from nested instantiations that involve,
595 -- among other things, types derived from private types.
597 ----------------------
598 -- Full_View_Covers --
599 ----------------------
601 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean is
604 Is_Private_Type (Typ1)
606 ((Present (Full_View (Typ1))
607 and then Covers (Full_View (Typ1), Typ2))
608 or else Base_Type (Typ1) = Typ2
609 or else Base_Type (Typ2) = Typ1);
610 end Full_View_Covers;
612 -- Start of processing for Covers
615 -- If either operand missing, then this is an error, but ignore
616 -- it (and pretend we have a cover) if errors already detected,
617 -- since this may simply mean we have malformed trees.
619 if No (T1) or else No (T2) then
620 if Total_Errors_Detected /= 0 then
627 -- Simplest case: same types are compatible, and types that have the
628 -- same base type and are not generic actuals are compatible. Generic
629 -- actuals belong to their class but are not compatible with other
630 -- types of their class, and in particular with other generic actuals.
631 -- They are however compatible with their own subtypes, and itypes
632 -- with the same base are compatible as well. Similary, constrained
633 -- subtypes obtained from expressions of an unconstrained nominal type
634 -- are compatible with the base type (may lead to spurious ambiguities
635 -- in obscure cases ???)
637 -- Generic actuals require special treatment to avoid spurious ambi-
638 -- guities in an instance, when two formal types are instantiated with
639 -- the same actual, so that different subprograms end up with the same
640 -- signature in the instance.
645 elsif Base_Type (T1) = Base_Type (T2) then
646 if not Is_Generic_Actual_Type (T1) then
649 return (not Is_Generic_Actual_Type (T2)
650 or else Is_Itype (T1)
651 or else Is_Itype (T2)
652 or else Is_Constr_Subt_For_U_Nominal (T1)
653 or else Is_Constr_Subt_For_U_Nominal (T2)
654 or else Scope (T1) /= Scope (T2));
657 -- Literals are compatible with types in a given "class"
659 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
660 or else (T2 = Universal_Real and then Is_Real_Type (T1))
661 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
662 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
663 or else (T2 = Any_String and then Is_String_Type (T1))
664 or else (T2 = Any_Character and then Is_Character_Type (T1))
665 or else (T2 = Any_Access and then Is_Access_Type (T1))
669 -- The context may be class wide.
671 elsif Is_Class_Wide_Type (T1)
672 and then Is_Ancestor (Root_Type (T1), T2)
676 elsif Is_Class_Wide_Type (T1)
677 and then Is_Class_Wide_Type (T2)
678 and then Base_Type (Etype (T1)) = Base_Type (Etype (T2))
682 -- In a dispatching call the actual may be class-wide
684 elsif Is_Class_Wide_Type (T2)
685 and then Base_Type (Root_Type (T2)) = Base_Type (T1)
689 -- Some contexts require a class of types rather than a specific type
691 elsif (T1 = Any_Integer and then Is_Integer_Type (T2))
692 or else (T1 = Any_Boolean and then Is_Boolean_Type (T2))
693 or else (T1 = Any_Real and then Is_Real_Type (T2))
694 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
695 or else (T1 = Any_Discrete and then Is_Discrete_Type (T2))
699 -- An aggregate is compatible with an array or record type
701 elsif T2 = Any_Composite
702 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
706 -- If the expected type is an anonymous access, the designated
707 -- type must cover that of the expression.
709 elsif Ekind (T1) = E_Anonymous_Access_Type
710 and then Is_Access_Type (T2)
711 and then Covers (Designated_Type (T1), Designated_Type (T2))
715 -- An Access_To_Subprogram is compatible with itself, or with an
716 -- anonymous type created for an attribute reference Access.
718 elsif (Ekind (Base_Type (T1)) = E_Access_Subprogram_Type
720 Ekind (Base_Type (T1)) = E_Access_Protected_Subprogram_Type)
721 and then Is_Access_Type (T2)
722 and then (not Comes_From_Source (T1)
723 or else not Comes_From_Source (T2))
724 and then (Is_Overloadable (Designated_Type (T2))
726 Ekind (Designated_Type (T2)) = E_Subprogram_Type)
728 Type_Conformant (Designated_Type (T1), Designated_Type (T2))
730 Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
734 -- The context can be a remote access type, and the expression the
735 -- corresponding source type declared in a categorized package, or
738 elsif Is_Record_Type (T1)
739 and then (Is_Remote_Call_Interface (T1)
740 or else Is_Remote_Types (T1))
741 and then Present (Corresponding_Remote_Type (T1))
743 return Covers (Corresponding_Remote_Type (T1), T2);
745 elsif Is_Record_Type (T2)
746 and then (Is_Remote_Call_Interface (T2)
747 or else Is_Remote_Types (T2))
748 and then Present (Corresponding_Remote_Type (T2))
750 return Covers (Corresponding_Remote_Type (T2), T1);
752 elsif Ekind (T2) = E_Access_Attribute_Type
753 and then (Ekind (Base_Type (T1)) = E_General_Access_Type
754 or else Ekind (Base_Type (T1)) = E_Access_Type)
755 and then Covers (Designated_Type (T1), Designated_Type (T2))
757 -- If the target type is a RACW type while the source is an access
758 -- attribute type, we are building a RACW that may be exported.
760 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (T1)) then
761 Set_Has_RACW (Current_Sem_Unit);
766 elsif Ekind (T2) = E_Allocator_Type
767 and then Is_Access_Type (T1)
769 return Covers (Designated_Type (T1), Designated_Type (T2))
771 (From_With_Type (Designated_Type (T1))
772 and then Covers (Designated_Type (T2), Designated_Type (T1)));
774 -- A boolean operation on integer literals is compatible with a
777 elsif T2 = Any_Modular
778 and then Is_Modular_Integer_Type (T1)
782 -- The actual type may be the result of a previous error
784 elsif Base_Type (T2) = Any_Type then
787 -- A packed array type covers its corresponding non-packed type.
788 -- This is not legitimate Ada, but allows the omission of a number
789 -- of otherwise useless unchecked conversions, and since this can
790 -- only arise in (known correct) expanded code, no harm is done
792 elsif Is_Array_Type (T2)
793 and then Is_Packed (T2)
794 and then T1 = Packed_Array_Type (T2)
798 -- Similarly an array type covers its corresponding packed array type
800 elsif Is_Array_Type (T1)
801 and then Is_Packed (T1)
802 and then T2 = Packed_Array_Type (T1)
808 (Full_View_Covers (T1, T2)
809 or else Full_View_Covers (T2, T1))
813 -- In the expansion of inlined bodies, types are compatible if they
814 -- are structurally equivalent.
816 elsif In_Inlined_Body
817 and then (Underlying_Type (T1) = Underlying_Type (T2)
818 or else (Is_Access_Type (T1)
819 and then Is_Access_Type (T2)
821 Designated_Type (T1) = Designated_Type (T2))
822 or else (T1 = Any_Access
823 and then Is_Access_Type (Underlying_Type (T2))))
827 elsif From_With_Type (T1) then
829 -- If the expected type is the non-limited view of a type, the
830 -- expression may have the limited view.
832 if Ekind (T1) = E_Incomplete_Type then
833 return Covers (Non_Limited_View (T1), T2);
835 elsif Ekind (T1) = E_Class_Wide_Type then
837 Covers (Class_Wide_Type (Non_Limited_View (Etype (T1))), T2);
842 elsif From_With_Type (T2) then
844 -- If units in the context have Limited_With clauses on each other,
845 -- either type might have a limited view. Checks performed elsewhere
846 -- verify that the context type is the non-limited view.
848 if Ekind (T2) = E_Incomplete_Type then
849 return Covers (T1, Non_Limited_View (T2));
851 elsif Ekind (T2) = E_Class_Wide_Type then
853 Covers (T1, Class_Wide_Type (Non_Limited_View (Etype (T2))));
858 -- Otherwise it doesn't cover!
869 function Disambiguate
871 I1, I2 : Interp_Index;
878 Nam1, Nam2 : Entity_Id;
879 Predef_Subp : Entity_Id;
880 User_Subp : Entity_Id;
882 function Is_Actual_Subprogram (S : Entity_Id) return Boolean;
883 -- Determine whether a subprogram is an actual in an enclosing
884 -- instance. An overloading between such a subprogram and one
885 -- declared outside the instance is resolved in favor of the first,
886 -- because it resolved in the generic.
888 function Matches (Actual, Formal : Node_Id) return Boolean;
889 -- Look for exact type match in an instance, to remove spurious
890 -- ambiguities when two formal types have the same actual.
892 function Standard_Operator return Boolean;
894 function Remove_Conversions return Interp;
895 -- Last chance for pathological cases involving comparisons on
896 -- literals, and user overloadings of the same operator. Such
897 -- pathologies have been removed from the ACVC, but still appear in
898 -- two DEC tests, with the following notable quote from Ben Brosgol:
900 -- [Note: I disclaim all credit/responsibility/blame for coming up with
901 -- this example; Robert Dewar brought it to our attention, since it
902 -- is apparently found in the ACVC 1.5. I did not attempt to find
903 -- the reason in the Reference Manual that makes the example legal,
904 -- since I was too nauseated by it to want to pursue it further.]
906 -- Accordingly, this is not a fully recursive solution, but it handles
907 -- DEC tests c460vsa, c460vsb. It also handles ai00136a, which pushes
908 -- pathology in the other direction with calls whose multiple overloaded
909 -- actuals make them truly unresolvable.
911 function Is_Actual_Subprogram (S : Entity_Id) return Boolean is
913 return In_Open_Scopes (Scope (S))
915 (Is_Generic_Instance (Scope (S))
916 or else Is_Wrapper_Package (Scope (S)));
917 end Is_Actual_Subprogram;
923 function Matches (Actual, Formal : Node_Id) return Boolean is
924 T1 : constant Entity_Id := Etype (Actual);
925 T2 : constant Entity_Id := Etype (Formal);
930 (Is_Numeric_Type (T2)
932 (T1 = Universal_Real or else T1 = Universal_Integer));
935 ------------------------
936 -- Remove_Conversions --
937 ------------------------
939 function Remove_Conversions return Interp is
949 Get_First_Interp (N, I, It);
951 while Present (It.Typ) loop
953 if not Is_Overloadable (It.Nam) then
957 F1 := First_Formal (It.Nam);
963 if Nkind (N) = N_Function_Call
964 or else Nkind (N) = N_Procedure_Call_Statement
966 Act1 := First_Actual (N);
968 if Present (Act1) then
969 Act2 := Next_Actual (Act1);
974 elsif Nkind (N) in N_Unary_Op then
975 Act1 := Right_Opnd (N);
978 elsif Nkind (N) in N_Binary_Op then
979 Act1 := Left_Opnd (N);
980 Act2 := Right_Opnd (N);
986 if Nkind (Act1) in N_Op
987 and then Is_Overloaded (Act1)
988 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
989 or else Nkind (Right_Opnd (Act1)) = N_Real_Literal)
990 and then Has_Compatible_Type (Act1, Standard_Boolean)
991 and then Etype (F1) = Standard_Boolean
993 -- If the two candidates are the original ones, the
994 -- ambiguity is real. Otherwise keep the original,
995 -- further calls to Disambiguate will take care of
996 -- others in the list of candidates.
998 if It1 /= No_Interp then
999 if It = Disambiguate.It1
1000 or else It = Disambiguate.It2
1002 if It1 = Disambiguate.It1
1003 or else It1 = Disambiguate.It2
1011 elsif Present (Act2)
1012 and then Nkind (Act2) in N_Op
1013 and then Is_Overloaded (Act2)
1014 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
1016 Nkind (Right_Opnd (Act1)) = N_Real_Literal)
1017 and then Has_Compatible_Type (Act2, Standard_Boolean)
1019 -- The preference rule on the first actual is not
1020 -- sufficient to disambiguate.
1031 Get_Next_Interp (I, It);
1034 if Serious_Errors_Detected > 0 then
1036 -- After some error, a formal may have Any_Type and yield
1037 -- a spurious match. To avoid cascaded errors if possible,
1038 -- check for such a formal in either candidate.
1044 Formal := First_Formal (Nam1);
1045 while Present (Formal) loop
1046 if Etype (Formal) = Any_Type then
1047 return Disambiguate.It2;
1050 Next_Formal (Formal);
1053 Formal := First_Formal (Nam2);
1054 while Present (Formal) loop
1055 if Etype (Formal) = Any_Type then
1056 return Disambiguate.It1;
1059 Next_Formal (Formal);
1065 end Remove_Conversions;
1067 -----------------------
1068 -- Standard_Operator --
1069 -----------------------
1071 function Standard_Operator return Boolean is
1075 if Nkind (N) in N_Op then
1078 elsif Nkind (N) = N_Function_Call then
1081 if Nkind (Nam) /= N_Expanded_Name then
1084 return Entity (Prefix (Nam)) = Standard_Standard;
1089 end Standard_Operator;
1091 -- Start of processing for Disambiguate
1094 -- Recover the two legal interpretations.
1096 Get_First_Interp (N, I, It);
1099 Get_Next_Interp (I, It);
1106 Get_Next_Interp (I, It);
1112 -- If the context is universal, the predefined operator is preferred.
1113 -- This includes bounds in numeric type declarations, and expressions
1114 -- in type conversions. If no interpretation yields a universal type,
1115 -- then we must check whether the user-defined entity hides the prede-
1118 if Chars (Nam1) in Any_Operator_Name
1119 and then Standard_Operator
1121 if Typ = Universal_Integer
1122 or else Typ = Universal_Real
1123 or else Typ = Any_Integer
1124 or else Typ = Any_Discrete
1125 or else Typ = Any_Real
1126 or else Typ = Any_Type
1128 -- Find an interpretation that yields the universal type, or else
1129 -- a predefined operator that yields a predefined numeric type.
1132 Candidate : Interp := No_Interp;
1134 Get_First_Interp (N, I, It);
1136 while Present (It.Typ) loop
1137 if (Covers (Typ, It.Typ)
1138 or else Typ = Any_Type)
1140 (It.Typ = Universal_Integer
1141 or else It.Typ = Universal_Real)
1145 elsif Covers (Typ, It.Typ)
1146 and then Scope (It.Typ) = Standard_Standard
1147 and then Scope (It.Nam) = Standard_Standard
1148 and then Is_Numeric_Type (It.Typ)
1153 Get_Next_Interp (I, It);
1156 if Candidate /= No_Interp then
1161 elsif Chars (Nam1) /= Name_Op_Not
1162 and then (Typ = Standard_Boolean
1163 or else Typ = Any_Boolean)
1165 -- Equality or comparison operation. Choose predefined operator
1166 -- if arguments are universal. The node may be an operator, a
1167 -- name, or a function call, so unpack arguments accordingly.
1170 Arg1, Arg2 : Node_Id;
1173 if Nkind (N) in N_Op then
1174 Arg1 := Left_Opnd (N);
1175 Arg2 := Right_Opnd (N);
1177 elsif Is_Entity_Name (N)
1178 or else Nkind (N) = N_Operator_Symbol
1180 Arg1 := First_Entity (Entity (N));
1181 Arg2 := Next_Entity (Arg1);
1184 Arg1 := First_Actual (N);
1185 Arg2 := Next_Actual (Arg1);
1189 and then Present (Universal_Interpretation (Arg1))
1190 and then Universal_Interpretation (Arg2) =
1191 Universal_Interpretation (Arg1)
1193 Get_First_Interp (N, I, It);
1195 while Scope (It.Nam) /= Standard_Standard loop
1196 Get_Next_Interp (I, It);
1205 -- If no universal interpretation, check whether user-defined operator
1206 -- hides predefined one, as well as other special cases. If the node
1207 -- is a range, then one or both bounds are ambiguous. Each will have
1208 -- to be disambiguated w.r.t. the context type. The type of the range
1209 -- itself is imposed by the context, so we can return either legal
1212 if Ekind (Nam1) = E_Operator then
1213 Predef_Subp := Nam1;
1216 elsif Ekind (Nam2) = E_Operator then
1217 Predef_Subp := Nam2;
1220 elsif Nkind (N) = N_Range then
1223 -- If two user defined-subprograms are visible, it is a true ambiguity,
1224 -- unless one of them is an entry and the context is a conditional or
1225 -- timed entry call, or unless we are within an instance and this is
1226 -- results from two formals types with the same actual.
1229 if Nkind (N) = N_Procedure_Call_Statement
1230 and then Nkind (Parent (N)) = N_Entry_Call_Alternative
1231 and then N = Entry_Call_Statement (Parent (N))
1233 if Ekind (Nam2) = E_Entry then
1235 elsif Ekind (Nam1) = E_Entry then
1241 -- If the ambiguity occurs within an instance, it is due to several
1242 -- formal types with the same actual. Look for an exact match
1243 -- between the types of the formals of the overloadable entities,
1244 -- and the actuals in the call, to recover the unambiguous match
1245 -- in the original generic.
1247 -- The ambiguity can also be due to an overloading between a formal
1248 -- subprogram and a subprogram declared outside the generic. If the
1249 -- node is overloaded, it did not resolve to the global entity in
1250 -- the generic, and we choose the formal subprogram.
1252 elsif In_Instance then
1253 if Nkind (N) = N_Function_Call
1254 or else Nkind (N) = N_Procedure_Call_Statement
1259 Is_Act1 : constant Boolean := Is_Actual_Subprogram (Nam1);
1260 Is_Act2 : constant Boolean := Is_Actual_Subprogram (Nam2);
1263 if Is_Act1 and then not Is_Act2 then
1266 elsif Is_Act2 and then not Is_Act1 then
1270 Actual := First_Actual (N);
1271 Formal := First_Formal (Nam1);
1272 while Present (Actual) loop
1273 if Etype (Actual) /= Etype (Formal) then
1277 Next_Actual (Actual);
1278 Next_Formal (Formal);
1284 elsif Nkind (N) in N_Binary_Op then
1286 if Matches (Left_Opnd (N), First_Formal (Nam1))
1288 Matches (Right_Opnd (N), Next_Formal (First_Formal (Nam1)))
1295 elsif Nkind (N) in N_Unary_Op then
1297 if Etype (Right_Opnd (N)) = Etype (First_Formal (Nam1)) then
1304 return Remove_Conversions;
1307 return Remove_Conversions;
1311 -- an implicit concatenation operator on a string type cannot be
1312 -- disambiguated from the predefined concatenation. This can only
1313 -- happen with concatenation of string literals.
1315 if Chars (User_Subp) = Name_Op_Concat
1316 and then Ekind (User_Subp) = E_Operator
1317 and then Is_String_Type (Etype (First_Formal (User_Subp)))
1321 -- If the user-defined operator is in an open scope, or in the scope
1322 -- of the resulting type, or given by an expanded name that names its
1323 -- scope, it hides the predefined operator for the type. Exponentiation
1324 -- has to be special-cased because the implicit operator does not have
1325 -- a symmetric signature, and may not be hidden by the explicit one.
1327 elsif (Nkind (N) = N_Function_Call
1328 and then Nkind (Name (N)) = N_Expanded_Name
1329 and then (Chars (Predef_Subp) /= Name_Op_Expon
1330 or else Hides_Op (User_Subp, Predef_Subp))
1331 and then Scope (User_Subp) = Entity (Prefix (Name (N))))
1332 or else Hides_Op (User_Subp, Predef_Subp)
1334 if It1.Nam = User_Subp then
1340 -- Otherwise, the predefined operator has precedence, or if the
1341 -- user-defined operation is directly visible we have a true ambiguity.
1342 -- If this is a fixed-point multiplication and division in Ada83 mode,
1343 -- exclude the universal_fixed operator, which often causes ambiguities
1347 if (In_Open_Scopes (Scope (User_Subp))
1348 or else Is_Potentially_Use_Visible (User_Subp))
1349 and then not In_Instance
1351 if Is_Fixed_Point_Type (Typ)
1352 and then (Chars (Nam1) = Name_Op_Multiply
1353 or else Chars (Nam1) = Name_Op_Divide)
1356 if It2.Nam = Predef_Subp then
1366 elsif It1.Nam = Predef_Subp then
1376 ---------------------
1377 -- End_Interp_List --
1378 ---------------------
1380 procedure End_Interp_List is
1382 All_Interp.Table (All_Interp.Last) := No_Interp;
1383 All_Interp.Increment_Last;
1384 end End_Interp_List;
1386 -------------------------
1387 -- Entity_Matches_Spec --
1388 -------------------------
1390 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean is
1392 -- Simple case: same entity kinds, type conformance is required.
1393 -- A parameterless function can also rename a literal.
1395 if Ekind (Old_S) = Ekind (New_S)
1396 or else (Ekind (New_S) = E_Function
1397 and then Ekind (Old_S) = E_Enumeration_Literal)
1399 return Type_Conformant (New_S, Old_S);
1401 elsif Ekind (New_S) = E_Function
1402 and then Ekind (Old_S) = E_Operator
1404 return Operator_Matches_Spec (Old_S, New_S);
1406 elsif Ekind (New_S) = E_Procedure
1407 and then Is_Entry (Old_S)
1409 return Type_Conformant (New_S, Old_S);
1414 end Entity_Matches_Spec;
1416 ----------------------
1417 -- Find_Unique_Type --
1418 ----------------------
1420 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id is
1421 T : constant Entity_Id := Etype (L);
1424 TR : Entity_Id := Any_Type;
1427 if Is_Overloaded (R) then
1428 Get_First_Interp (R, I, It);
1430 while Present (It.Typ) loop
1431 if Covers (T, It.Typ) or else Covers (It.Typ, T) then
1433 -- If several interpretations are possible and L is universal,
1434 -- apply preference rule.
1436 if TR /= Any_Type then
1438 if (T = Universal_Integer or else T = Universal_Real)
1449 Get_Next_Interp (I, It);
1454 -- In the non-overloaded case, the Etype of R is already set
1461 -- If one of the operands is Universal_Fixed, the type of the
1462 -- other operand provides the context.
1464 if Etype (R) = Universal_Fixed then
1467 elsif T = Universal_Fixed then
1471 return Specific_Type (T, Etype (R));
1474 end Find_Unique_Type;
1476 ----------------------
1477 -- Get_First_Interp --
1478 ----------------------
1480 procedure Get_First_Interp
1482 I : out Interp_Index;
1486 Int_Ind : Interp_Index;
1490 -- If a selected component is overloaded because the selector has
1491 -- multiple interpretations, the node is a call to a protected
1492 -- operation or an indirect call. Retrieve the interpretation from
1493 -- the selector name. The selected component may be overloaded as well
1494 -- if the prefix is overloaded. That case is unchanged.
1496 if Nkind (N) = N_Selected_Component
1497 and then Is_Overloaded (Selector_Name (N))
1499 O_N := Selector_Name (N);
1504 Map_Ptr := Headers (Hash (O_N));
1506 while Present (Interp_Map.Table (Map_Ptr).Node) loop
1507 if Interp_Map.Table (Map_Ptr).Node = O_N then
1508 Int_Ind := Interp_Map.Table (Map_Ptr).Index;
1509 It := All_Interp.Table (Int_Ind);
1513 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
1517 -- Procedure should never be called if the node has no interpretations
1519 raise Program_Error;
1520 end Get_First_Interp;
1522 ----------------------
1523 -- Get_Next_Interp --
1524 ----------------------
1526 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp) is
1529 It := All_Interp.Table (I);
1530 end Get_Next_Interp;
1532 -------------------------
1533 -- Has_Compatible_Type --
1534 -------------------------
1536 function Has_Compatible_Type
1549 if Nkind (N) = N_Subtype_Indication
1550 or else not Is_Overloaded (N)
1553 Covers (Typ, Etype (N))
1555 (not Is_Tagged_Type (Typ)
1556 and then Ekind (Typ) /= E_Anonymous_Access_Type
1557 and then Covers (Etype (N), Typ));
1560 Get_First_Interp (N, I, It);
1562 while Present (It.Typ) loop
1563 if (Covers (Typ, It.Typ)
1565 (Scope (It.Nam) /= Standard_Standard
1566 or else not Is_Invisible_Operator (N, Base_Type (Typ))))
1568 or else (not Is_Tagged_Type (Typ)
1569 and then Ekind (Typ) /= E_Anonymous_Access_Type
1570 and then Covers (It.Typ, Typ))
1575 Get_Next_Interp (I, It);
1580 end Has_Compatible_Type;
1586 function Hash (N : Node_Id) return Int is
1588 -- Nodes have a size that is power of two, so to select significant
1589 -- bits only we remove the low-order bits.
1591 return ((Int (N) / 2 ** 5) mod Header_Size);
1598 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean is
1599 Btyp : constant Entity_Id := Base_Type (Etype (First_Formal (F)));
1602 return Operator_Matches_Spec (Op, F)
1603 and then (In_Open_Scopes (Scope (F))
1604 or else Scope (F) = Scope (Btyp)
1605 or else (not In_Open_Scopes (Scope (Btyp))
1606 and then not In_Use (Btyp)
1607 and then not In_Use (Scope (Btyp))));
1610 ------------------------
1611 -- Init_Interp_Tables --
1612 ------------------------
1614 procedure Init_Interp_Tables is
1618 Headers := (others => No_Entry);
1619 end Init_Interp_Tables;
1621 ---------------------
1622 -- Intersect_Types --
1623 ---------------------
1625 function Intersect_Types (L, R : Node_Id) return Entity_Id is
1626 Index : Interp_Index;
1630 function Check_Right_Argument (T : Entity_Id) return Entity_Id;
1631 -- Find interpretation of right arg that has type compatible with T
1633 --------------------------
1634 -- Check_Right_Argument --
1635 --------------------------
1637 function Check_Right_Argument (T : Entity_Id) return Entity_Id is
1638 Index : Interp_Index;
1643 if not Is_Overloaded (R) then
1644 return Specific_Type (T, Etype (R));
1647 Get_First_Interp (R, Index, It);
1650 T2 := Specific_Type (T, It.Typ);
1652 if T2 /= Any_Type then
1656 Get_Next_Interp (Index, It);
1657 exit when No (It.Typ);
1662 end Check_Right_Argument;
1664 -- Start processing for Intersect_Types
1667 if Etype (L) = Any_Type or else Etype (R) = Any_Type then
1671 if not Is_Overloaded (L) then
1672 Typ := Check_Right_Argument (Etype (L));
1676 Get_First_Interp (L, Index, It);
1678 while Present (It.Typ) loop
1679 Typ := Check_Right_Argument (It.Typ);
1680 exit when Typ /= Any_Type;
1681 Get_Next_Interp (Index, It);
1686 -- If Typ is Any_Type, it means no compatible pair of types was found
1688 if Typ = Any_Type then
1690 if Nkind (Parent (L)) in N_Op then
1691 Error_Msg_N ("incompatible types for operator", Parent (L));
1693 elsif Nkind (Parent (L)) = N_Range then
1694 Error_Msg_N ("incompatible types given in constraint", Parent (L));
1697 Error_Msg_N ("incompatible types", Parent (L));
1702 end Intersect_Types;
1708 function Is_Ancestor (T1, T2 : Entity_Id) return Boolean is
1712 if Base_Type (T1) = Base_Type (T2) then
1715 elsif Is_Private_Type (T1)
1716 and then Present (Full_View (T1))
1717 and then Base_Type (T2) = Base_Type (Full_View (T1))
1725 -- If there was a error on the type declaration, do not recurse
1727 if Error_Posted (Par) then
1730 elsif Base_Type (T1) = Base_Type (Par)
1731 or else (Is_Private_Type (T1)
1732 and then Present (Full_View (T1))
1733 and then Base_Type (Par) = Base_Type (Full_View (T1)))
1737 elsif Is_Private_Type (Par)
1738 and then Present (Full_View (Par))
1739 and then Full_View (Par) = Base_Type (T1)
1743 elsif Etype (Par) /= Par then
1752 ---------------------------
1753 -- Is_Invisible_Operator --
1754 ---------------------------
1756 function Is_Invisible_Operator
1761 Orig_Node : constant Node_Id := Original_Node (N);
1764 if Nkind (N) not in N_Op then
1767 elsif not Comes_From_Source (N) then
1770 elsif No (Universal_Interpretation (Right_Opnd (N))) then
1773 elsif Nkind (N) in N_Binary_Op
1774 and then No (Universal_Interpretation (Left_Opnd (N)))
1780 and then not In_Open_Scopes (Scope (T))
1781 and then not Is_Potentially_Use_Visible (T)
1782 and then not In_Use (T)
1783 and then not In_Use (Scope (T))
1785 (Nkind (Orig_Node) /= N_Function_Call
1786 or else Nkind (Name (Orig_Node)) /= N_Expanded_Name
1787 or else Entity (Prefix (Name (Orig_Node))) /= Scope (T))
1789 and then not In_Instance;
1791 end Is_Invisible_Operator;
1797 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
1801 S := Ancestor_Subtype (T1);
1802 while Present (S) loop
1806 S := Ancestor_Subtype (S);
1817 procedure List_Interps (Nam : Node_Id; Err : Node_Id) is
1818 Index : Interp_Index;
1822 Get_First_Interp (Nam, Index, It);
1823 while Present (It.Nam) loop
1824 if Scope (It.Nam) = Standard_Standard
1825 and then Scope (It.Typ) /= Standard_Standard
1827 Error_Msg_Sloc := Sloc (Parent (It.Typ));
1828 Error_Msg_NE (" & (inherited) declared#!", Err, It.Nam);
1831 Error_Msg_Sloc := Sloc (It.Nam);
1832 Error_Msg_NE (" & declared#!", Err, It.Nam);
1835 Get_Next_Interp (Index, It);
1843 procedure New_Interps (N : Node_Id) is
1847 All_Interp.Increment_Last;
1848 All_Interp.Table (All_Interp.Last) := No_Interp;
1850 Map_Ptr := Headers (Hash (N));
1852 if Map_Ptr = No_Entry then
1854 -- Place new node at end of table
1856 Interp_Map.Increment_Last;
1857 Headers (Hash (N)) := Interp_Map.Last;
1860 -- Place node at end of chain, or locate its previous entry.
1863 if Interp_Map.Table (Map_Ptr).Node = N then
1865 -- Node is already in the table, and is being rewritten.
1866 -- Start a new interp section, retain hash link.
1868 Interp_Map.Table (Map_Ptr).Node := N;
1869 Interp_Map.Table (Map_Ptr).Index := All_Interp.Last;
1870 Set_Is_Overloaded (N, True);
1874 exit when Interp_Map.Table (Map_Ptr).Next = No_Entry;
1875 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
1879 -- Chain the new node.
1881 Interp_Map.Increment_Last;
1882 Interp_Map.Table (Map_Ptr).Next := Interp_Map.Last;
1885 Interp_Map.Table (Interp_Map.Last) := (N, All_Interp.Last, No_Entry);
1886 Set_Is_Overloaded (N, True);
1889 ---------------------------
1890 -- Operator_Matches_Spec --
1891 ---------------------------
1893 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean is
1894 Op_Name : constant Name_Id := Chars (Op);
1895 T : constant Entity_Id := Etype (New_S);
1903 -- To verify that a predefined operator matches a given signature,
1904 -- do a case analysis of the operator classes. Function can have one
1905 -- or two formals and must have the proper result type.
1907 New_F := First_Formal (New_S);
1908 Old_F := First_Formal (Op);
1911 while Present (New_F) and then Present (Old_F) loop
1913 Next_Formal (New_F);
1914 Next_Formal (Old_F);
1917 -- Definite mismatch if different number of parameters
1919 if Present (Old_F) or else Present (New_F) then
1925 T1 := Etype (First_Formal (New_S));
1927 if Op_Name = Name_Op_Subtract
1928 or else Op_Name = Name_Op_Add
1929 or else Op_Name = Name_Op_Abs
1931 return Base_Type (T1) = Base_Type (T)
1932 and then Is_Numeric_Type (T);
1934 elsif Op_Name = Name_Op_Not then
1935 return Base_Type (T1) = Base_Type (T)
1936 and then Valid_Boolean_Arg (Base_Type (T));
1945 T1 := Etype (First_Formal (New_S));
1946 T2 := Etype (Next_Formal (First_Formal (New_S)));
1948 if Op_Name = Name_Op_And or else Op_Name = Name_Op_Or
1949 or else Op_Name = Name_Op_Xor
1951 return Base_Type (T1) = Base_Type (T2)
1952 and then Base_Type (T1) = Base_Type (T)
1953 and then Valid_Boolean_Arg (Base_Type (T));
1955 elsif Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
1956 return Base_Type (T1) = Base_Type (T2)
1957 and then not Is_Limited_Type (T1)
1958 and then Is_Boolean_Type (T);
1960 elsif Op_Name = Name_Op_Lt or else Op_Name = Name_Op_Le
1961 or else Op_Name = Name_Op_Gt or else Op_Name = Name_Op_Ge
1963 return Base_Type (T1) = Base_Type (T2)
1964 and then Valid_Comparison_Arg (T1)
1965 and then Is_Boolean_Type (T);
1967 elsif Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
1968 return Base_Type (T1) = Base_Type (T2)
1969 and then Base_Type (T1) = Base_Type (T)
1970 and then Is_Numeric_Type (T);
1972 -- for division and multiplication, a user-defined function does
1973 -- not match the predefined universal_fixed operation, except in
1976 elsif Op_Name = Name_Op_Divide then
1977 return (Base_Type (T1) = Base_Type (T2)
1978 and then Base_Type (T1) = Base_Type (T)
1979 and then Is_Numeric_Type (T)
1980 and then (not Is_Fixed_Point_Type (T)
1983 -- Mixed_Mode operations on fixed-point types.
1985 or else (Base_Type (T1) = Base_Type (T)
1986 and then Base_Type (T2) = Base_Type (Standard_Integer)
1987 and then Is_Fixed_Point_Type (T))
1989 -- A user defined operator can also match (and hide) a mixed
1990 -- operation on universal literals.
1992 or else (Is_Integer_Type (T2)
1993 and then Is_Floating_Point_Type (T1)
1994 and then Base_Type (T1) = Base_Type (T));
1996 elsif Op_Name = Name_Op_Multiply then
1997 return (Base_Type (T1) = Base_Type (T2)
1998 and then Base_Type (T1) = Base_Type (T)
1999 and then Is_Numeric_Type (T)
2000 and then (not Is_Fixed_Point_Type (T)
2003 -- Mixed_Mode operations on fixed-point types.
2005 or else (Base_Type (T1) = Base_Type (T)
2006 and then Base_Type (T2) = Base_Type (Standard_Integer)
2007 and then Is_Fixed_Point_Type (T))
2009 or else (Base_Type (T2) = Base_Type (T)
2010 and then Base_Type (T1) = Base_Type (Standard_Integer)
2011 and then Is_Fixed_Point_Type (T))
2013 or else (Is_Integer_Type (T2)
2014 and then Is_Floating_Point_Type (T1)
2015 and then Base_Type (T1) = Base_Type (T))
2017 or else (Is_Integer_Type (T1)
2018 and then Is_Floating_Point_Type (T2)
2019 and then Base_Type (T2) = Base_Type (T));
2021 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
2022 return Base_Type (T1) = Base_Type (T2)
2023 and then Base_Type (T1) = Base_Type (T)
2024 and then Is_Integer_Type (T);
2026 elsif Op_Name = Name_Op_Expon then
2027 return Base_Type (T1) = Base_Type (T)
2028 and then Is_Numeric_Type (T)
2029 and then Base_Type (T2) = Base_Type (Standard_Integer);
2031 elsif Op_Name = Name_Op_Concat then
2032 return Is_Array_Type (T)
2033 and then (Base_Type (T) = Base_Type (Etype (Op)))
2034 and then (Base_Type (T1) = Base_Type (T)
2036 Base_Type (T1) = Base_Type (Component_Type (T)))
2037 and then (Base_Type (T2) = Base_Type (T)
2039 Base_Type (T2) = Base_Type (Component_Type (T)));
2045 end Operator_Matches_Spec;
2051 procedure Remove_Interp (I : in out Interp_Index) is
2055 -- Find end of Interp list and copy downward to erase the discarded one
2059 while Present (All_Interp.Table (II).Typ) loop
2063 for J in I + 1 .. II loop
2064 All_Interp.Table (J - 1) := All_Interp.Table (J);
2067 -- Back up interp. index to insure that iterator will pick up next
2068 -- available interpretation.
2077 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id) is
2079 O_N : Node_Id := Old_N;
2082 if Is_Overloaded (Old_N) then
2083 if Nkind (Old_N) = N_Selected_Component
2084 and then Is_Overloaded (Selector_Name (Old_N))
2086 O_N := Selector_Name (Old_N);
2089 Map_Ptr := Headers (Hash (O_N));
2091 while Interp_Map.Table (Map_Ptr).Node /= O_N loop
2092 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
2093 pragma Assert (Map_Ptr /= No_Entry);
2096 New_Interps (New_N);
2097 Interp_Map.Table (Interp_Map.Last).Index :=
2098 Interp_Map.Table (Map_Ptr).Index;
2106 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
2107 B1 : constant Entity_Id := Base_Type (T1);
2108 B2 : constant Entity_Id := Base_Type (T2);
2110 function Is_Remote_Access (T : Entity_Id) return Boolean;
2111 -- Check whether T is the equivalent type of a remote access type.
2112 -- If distribution is enabled, T is a legal context for Null.
2114 ----------------------
2115 -- Is_Remote_Access --
2116 ----------------------
2118 function Is_Remote_Access (T : Entity_Id) return Boolean is
2120 return Is_Record_Type (T)
2121 and then (Is_Remote_Call_Interface (T)
2122 or else Is_Remote_Types (T))
2123 and then Present (Corresponding_Remote_Type (T))
2124 and then Is_Access_Type (Corresponding_Remote_Type (T));
2125 end Is_Remote_Access;
2127 -- Start of processing for Specific_Type
2130 if T1 = Any_Type or else T2 = Any_Type then
2138 or else (T1 = Universal_Integer and then Is_Integer_Type (T2))
2139 or else (T1 = Universal_Real and then Is_Real_Type (T2))
2140 or else (T1 = Universal_Fixed and then Is_Fixed_Point_Type (T2))
2141 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
2146 or else (T2 = Universal_Integer and then Is_Integer_Type (T1))
2147 or else (T2 = Universal_Real and then Is_Real_Type (T1))
2148 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
2149 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
2153 elsif T2 = Any_String and then Is_String_Type (T1) then
2156 elsif T1 = Any_String and then Is_String_Type (T2) then
2159 elsif T2 = Any_Character and then Is_Character_Type (T1) then
2162 elsif T1 = Any_Character and then Is_Character_Type (T2) then
2165 elsif T1 = Any_Access
2166 and then (Is_Access_Type (T2) or else Is_Remote_Access (T2))
2170 elsif T2 = Any_Access
2171 and then (Is_Access_Type (T1) or else Is_Remote_Access (T1))
2175 elsif T2 = Any_Composite
2176 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
2180 elsif T1 = Any_Composite
2181 and then Ekind (T2) in E_Array_Type .. E_Record_Subtype
2185 elsif T1 = Any_Modular and then Is_Modular_Integer_Type (T2) then
2188 elsif T2 = Any_Modular and then Is_Modular_Integer_Type (T1) then
2191 -- Special cases for equality operators (all other predefined
2192 -- operators can never apply to tagged types)
2194 elsif Is_Class_Wide_Type (T1)
2195 and then Is_Ancestor (Root_Type (T1), T2)
2199 elsif Is_Class_Wide_Type (T2)
2200 and then Is_Ancestor (Root_Type (T2), T1)
2204 elsif (Ekind (B1) = E_Access_Subprogram_Type
2206 Ekind (B1) = E_Access_Protected_Subprogram_Type)
2207 and then Ekind (Designated_Type (B1)) /= E_Subprogram_Type
2208 and then Is_Access_Type (T2)
2212 elsif (Ekind (B2) = E_Access_Subprogram_Type
2214 Ekind (B2) = E_Access_Protected_Subprogram_Type)
2215 and then Ekind (Designated_Type (B2)) /= E_Subprogram_Type
2216 and then Is_Access_Type (T1)
2220 elsif (Ekind (T1) = E_Allocator_Type
2221 or else Ekind (T1) = E_Access_Attribute_Type
2222 or else Ekind (T1) = E_Anonymous_Access_Type)
2223 and then Is_Access_Type (T2)
2227 elsif (Ekind (T2) = E_Allocator_Type
2228 or else Ekind (T2) = E_Access_Attribute_Type
2229 or else Ekind (T2) = E_Anonymous_Access_Type)
2230 and then Is_Access_Type (T1)
2234 -- If none of the above cases applies, types are not compatible.
2241 -----------------------
2242 -- Valid_Boolean_Arg --
2243 -----------------------
2245 -- In addition to booleans and arrays of booleans, we must include
2246 -- aggregates as valid boolean arguments, because in the first pass
2247 -- of resolution their components are not examined. If it turns out not
2248 -- to be an aggregate of booleans, this will be diagnosed in Resolve.
2249 -- Any_Composite must be checked for prior to the array type checks
2250 -- because Any_Composite does not have any associated indexes.
2252 function Valid_Boolean_Arg (T : Entity_Id) return Boolean is
2254 return Is_Boolean_Type (T)
2255 or else T = Any_Composite
2256 or else (Is_Array_Type (T)
2257 and then T /= Any_String
2258 and then Number_Dimensions (T) = 1
2259 and then Is_Boolean_Type (Component_Type (T))
2260 and then (not Is_Private_Composite (T)
2261 or else In_Instance)
2262 and then (not Is_Limited_Composite (T)
2263 or else In_Instance))
2264 or else Is_Modular_Integer_Type (T)
2265 or else T = Universal_Integer;
2266 end Valid_Boolean_Arg;
2268 --------------------------
2269 -- Valid_Comparison_Arg --
2270 --------------------------
2272 function Valid_Comparison_Arg (T : Entity_Id) return Boolean is
2275 if T = Any_Composite then
2277 elsif Is_Discrete_Type (T)
2278 or else Is_Real_Type (T)
2281 elsif Is_Array_Type (T)
2282 and then Number_Dimensions (T) = 1
2283 and then Is_Discrete_Type (Component_Type (T))
2284 and then (not Is_Private_Composite (T)
2285 or else In_Instance)
2286 and then (not Is_Limited_Composite (T)
2287 or else In_Instance)
2290 elsif Is_String_Type (T) then
2295 end Valid_Comparison_Arg;
2297 ---------------------
2298 -- Write_Overloads --
2299 ---------------------
2301 procedure Write_Overloads (N : Node_Id) is
2307 if not Is_Overloaded (N) then
2308 Write_Str ("Non-overloaded entity ");
2310 Write_Entity_Info (Entity (N), " ");
2313 Get_First_Interp (N, I, It);
2314 Write_Str ("Overloaded entity ");
2318 while Present (Nam) loop
2319 Write_Entity_Info (Nam, " ");
2320 Write_Str ("=================");
2322 Get_Next_Interp (I, It);
2326 end Write_Overloads;
2328 -----------------------
2329 -- Write_Interp_Ref --
2330 -----------------------
2332 procedure Write_Interp_Ref (Map_Ptr : Int) is
2334 Write_Str (" Node: ");
2335 Write_Int (Int (Interp_Map.Table (Map_Ptr).Node));
2336 Write_Str (" Index: ");
2337 Write_Int (Int (Interp_Map.Table (Map_Ptr).Index));
2338 Write_Str (" Next: ");
2339 Write_Int (Int (Interp_Map.Table (Map_Ptr).Next));
2341 end Write_Interp_Ref;