1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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);
200 while Present (It.Nam) loop
202 -- A user-defined subprogram hides another declared at an outer
203 -- level, or one that is use-visible. So return if previous
204 -- definition hides new one (which is either in an outer
205 -- scope, or use-visible). Note that for functions use-visible
206 -- is the same as potentially use-visible. If new one hides
207 -- previous one, replace entry in table of interpretations.
208 -- If this is a universal operation, retain the operator in case
209 -- preference rule applies.
211 if (((Ekind (Name) = E_Function or else Ekind (Name) = E_Procedure)
212 and then Ekind (Name) = Ekind (It.Nam))
213 or else (Ekind (Name) = E_Operator
214 and then Ekind (It.Nam) = E_Function))
216 and then Is_Immediately_Visible (It.Nam)
217 and then Type_Conformant (Name, It.Nam)
218 and then Base_Type (It.Typ) = Base_Type (T)
220 if Is_Universal_Operation (Name) then
223 -- If node is an operator symbol, we have no actuals with
224 -- which to check hiding, and this is done in full in the
225 -- caller (Analyze_Subprogram_Renaming) so we include the
226 -- predefined operator in any case.
228 elsif Nkind (N) = N_Operator_Symbol
229 or else (Nkind (N) = N_Expanded_Name
231 Nkind (Selector_Name (N)) = N_Operator_Symbol)
235 elsif not In_Open_Scopes (Scope (Name))
236 or else Scope_Depth (Scope (Name)) <=
237 Scope_Depth (Scope (It.Nam))
239 -- If ambiguity within instance, and entity is not an
240 -- implicit operation, save for later disambiguation.
242 if Scope (Name) = Scope (It.Nam)
243 and then not Is_Inherited_Operation (Name)
252 All_Interp.Table (Index).Nam := Name;
256 -- Avoid making duplicate entries in overloads
259 and then Base_Type (It.Typ) = Base_Type (T)
263 -- Otherwise keep going
266 Get_Next_Interp (Index, It);
271 -- On exit, enter new interpretation. The context, or a preference
272 -- rule, will resolve the ambiguity on the second pass.
274 All_Interp.Table (All_Interp.Last) := (Name, Typ);
275 All_Interp.Increment_Last;
276 All_Interp.Table (All_Interp.Last) := No_Interp;
279 ----------------------------
280 -- Is_Universal_Operation --
281 ----------------------------
283 function Is_Universal_Operation (Op : Entity_Id) return Boolean is
287 if Ekind (Op) /= E_Operator then
290 elsif Nkind (N) in N_Binary_Op then
291 return Present (Universal_Interpretation (Left_Opnd (N)))
292 and then Present (Universal_Interpretation (Right_Opnd (N)));
294 elsif Nkind (N) in N_Unary_Op then
295 return Present (Universal_Interpretation (Right_Opnd (N)));
297 elsif Nkind (N) = N_Function_Call then
298 Arg := First_Actual (N);
299 while Present (Arg) loop
300 if No (Universal_Interpretation (Arg)) then
312 end Is_Universal_Operation;
314 -- Start of processing for Add_One_Interp
317 -- If the interpretation is a predefined operator, verify that the
318 -- result type is visible, or that the entity has already been
319 -- resolved (case of an instantiation node that refers to a predefined
320 -- operation, or an internally generated operator node, or an operator
321 -- given as an expanded name). If the operator is a comparison or
322 -- equality, it is the type of the operand that matters to determine
323 -- whether the operator is visible. In an instance, the check is not
324 -- performed, given that the operator was visible in the generic.
326 if Ekind (E) = E_Operator then
328 if Present (Opnd_Type) then
329 Vis_Type := Opnd_Type;
331 Vis_Type := Base_Type (T);
334 if In_Open_Scopes (Scope (Vis_Type))
335 or else Is_Potentially_Use_Visible (Vis_Type)
336 or else In_Use (Vis_Type)
337 or else (In_Use (Scope (Vis_Type))
338 and then not Is_Hidden (Vis_Type))
339 or else Nkind (N) = N_Expanded_Name
340 or else (Nkind (N) in N_Op and then E = Entity (N))
345 -- If the node is given in functional notation and the prefix
346 -- is an expanded name, then the operator is visible if the
347 -- prefix is the scope of the result type as well. If the
348 -- operator is (implicitly) defined in an extension of system,
349 -- it is know to be valid (see Defined_In_Scope, sem_ch4.adb).
351 elsif Nkind (N) = N_Function_Call
352 and then Nkind (Name (N)) = N_Expanded_Name
353 and then (Entity (Prefix (Name (N))) = Scope (Base_Type (T))
354 or else Entity (Prefix (Name (N))) = Scope (Vis_Type)
355 or else Scope (Vis_Type) = System_Aux_Id)
359 -- Save type for subsequent error message, in case no other
360 -- interpretation is found.
363 Candidate_Type := Vis_Type;
367 -- In an instance, an abstract non-dispatching operation cannot
368 -- be a candidate interpretation, because it could not have been
369 -- one in the generic (it may be a spurious overloading in the
373 and then Is_Abstract (E)
374 and then not Is_Dispatching_Operation (E)
379 -- If this is the first interpretation of N, N has type Any_Type.
380 -- In that case place the new type on the node. If one interpretation
381 -- already exists, indicate that the node is overloaded, and store
382 -- both the previous and the new interpretation in All_Interp. If
383 -- this is a later interpretation, just add it to the set.
385 if Etype (N) = Any_Type then
390 -- Record both the operator or subprogram name, and its type
392 if Nkind (N) in N_Op or else Is_Entity_Name (N) then
399 -- Either there is no current interpretation in the table for any
400 -- node or the interpretation that is present is for a different
401 -- node. In both cases add a new interpretation to the table.
403 elsif Interp_Map.Last < 0
405 (Interp_Map.Table (Interp_Map.Last).Node /= N
406 and then not Is_Overloaded (N))
410 if (Nkind (N) in N_Op or else Is_Entity_Name (N))
411 and then Present (Entity (N))
413 Add_Entry (Entity (N), Etype (N));
415 elsif (Nkind (N) = N_Function_Call
416 or else Nkind (N) = N_Procedure_Call_Statement)
417 and then (Nkind (Name (N)) = N_Operator_Symbol
418 or else Is_Entity_Name (Name (N)))
420 Add_Entry (Entity (Name (N)), Etype (N));
423 -- Overloaded prefix in indexed or selected component,
424 -- or call whose name is an expresion or another call.
426 Add_Entry (Etype (N), Etype (N));
440 procedure All_Overloads is
442 for J in All_Interp.First .. All_Interp.Last loop
444 if Present (All_Interp.Table (J).Nam) then
445 Write_Entity_Info (All_Interp.Table (J). Nam, " ");
447 Write_Str ("No Interp");
450 Write_Str ("=================");
455 ---------------------
456 -- Collect_Interps --
457 ---------------------
459 procedure Collect_Interps (N : Node_Id) is
460 Ent : constant Entity_Id := Entity (N);
462 First_Interp : Interp_Index;
467 -- Unconditionally add the entity that was initially matched
469 First_Interp := All_Interp.Last;
470 Add_One_Interp (N, Ent, Etype (N));
472 -- For expanded name, pick up all additional entities from the
473 -- same scope, since these are obviously also visible. Note that
474 -- these are not necessarily contiguous on the homonym chain.
476 if Nkind (N) = N_Expanded_Name then
478 while Present (H) loop
479 if Scope (H) = Scope (Entity (N)) then
480 Add_One_Interp (N, H, Etype (H));
486 -- Case of direct name
489 -- First, search the homonym chain for directly visible entities
491 H := Current_Entity (Ent);
492 while Present (H) loop
493 exit when (not Is_Overloadable (H))
494 and then Is_Immediately_Visible (H);
496 if Is_Immediately_Visible (H)
499 -- Only add interpretation if not hidden by an inner
500 -- immediately visible one.
502 for J in First_Interp .. All_Interp.Last - 1 loop
504 -- Current homograph is not hidden. Add to overloads
506 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
509 -- Homograph is hidden, unless it is a predefined operator
511 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
513 -- A homograph in the same scope can occur within an
514 -- instantiation, the resulting ambiguity has to be
517 if Scope (H) = Scope (Ent)
519 and then not Is_Inherited_Operation (H)
521 All_Interp.Table (All_Interp.Last) := (H, Etype (H));
522 All_Interp.Increment_Last;
523 All_Interp.Table (All_Interp.Last) := No_Interp;
526 elsif Scope (H) /= Standard_Standard then
532 -- On exit, we know that current homograph is not hidden.
534 Add_One_Interp (N, H, Etype (H));
537 Write_Str ("Add overloaded Interpretation ");
547 -- Scan list of homographs for use-visible entities only
549 H := Current_Entity (Ent);
551 while Present (H) loop
552 if Is_Potentially_Use_Visible (H)
554 and then Is_Overloadable (H)
556 for J in First_Interp .. All_Interp.Last - 1 loop
558 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
561 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
562 goto Next_Use_Homograph;
566 Add_One_Interp (N, H, Etype (H));
569 <<Next_Use_Homograph>>
574 if All_Interp.Last = First_Interp + 1 then
576 -- The original interpretation is in fact not overloaded
578 Set_Is_Overloaded (N, False);
586 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 BT1 := Base_Type (T1);
628 BT2 := Base_Type (T2);
631 -- Simplest case: same types are compatible, and types that have the
632 -- same base type and are not generic actuals are compatible. Generic
633 -- actuals belong to their class but are not compatible with other
634 -- types of their class, and in particular with other generic actuals.
635 -- They are however compatible with their own subtypes, and itypes
636 -- with the same base are compatible as well. Similary, constrained
637 -- subtypes obtained from expressions of an unconstrained nominal type
638 -- are compatible with the base type (may lead to spurious ambiguities
639 -- in obscure cases ???)
641 -- Generic actuals require special treatment to avoid spurious ambi-
642 -- guities in an instance, when two formal types are instantiated with
643 -- the same actual, so that different subprograms end up with the same
644 -- signature in the instance.
653 if not Is_Generic_Actual_Type (T1) then
656 return (not Is_Generic_Actual_Type (T2)
657 or else Is_Itype (T1)
658 or else Is_Itype (T2)
659 or else Is_Constr_Subt_For_U_Nominal (T1)
660 or else Is_Constr_Subt_For_U_Nominal (T2)
661 or else Scope (T1) /= Scope (T2));
664 -- Literals are compatible with types in a given "class"
666 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
667 or else (T2 = Universal_Real and then Is_Real_Type (T1))
668 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
669 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
670 or else (T2 = Any_String and then Is_String_Type (T1))
671 or else (T2 = Any_Character and then Is_Character_Type (T1))
672 or else (T2 = Any_Access and then Is_Access_Type (T1))
676 -- The context may be class wide
678 elsif Is_Class_Wide_Type (T1)
679 and then Is_Ancestor (Root_Type (T1), T2)
683 elsif Is_Class_Wide_Type (T1)
684 and then Is_Class_Wide_Type (T2)
685 and then Base_Type (Etype (T1)) = Base_Type (Etype (T2))
689 -- In a dispatching call the actual may be class-wide
691 elsif Is_Class_Wide_Type (T2)
692 and then Base_Type (Root_Type (T2)) = Base_Type (T1)
696 -- Some contexts require a class of types rather than a specific type
698 elsif (T1 = Any_Integer and then Is_Integer_Type (T2))
699 or else (T1 = Any_Boolean and then Is_Boolean_Type (T2))
700 or else (T1 = Any_Real and then Is_Real_Type (T2))
701 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
702 or else (T1 = Any_Discrete and then Is_Discrete_Type (T2))
706 -- An aggregate is compatible with an array or record type
708 elsif T2 = Any_Composite
709 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
713 -- If the expected type is an anonymous access, the designated
714 -- type must cover that of the expression.
716 elsif Ekind (T1) = E_Anonymous_Access_Type
717 and then Is_Access_Type (T2)
718 and then Covers (Designated_Type (T1), Designated_Type (T2))
722 -- An Access_To_Subprogram is compatible with itself, or with an
723 -- anonymous type created for an attribute reference Access.
725 elsif (Ekind (BT1) = E_Access_Subprogram_Type
727 Ekind (BT1) = E_Access_Protected_Subprogram_Type)
728 and then Is_Access_Type (T2)
729 and then (not Comes_From_Source (T1)
730 or else not Comes_From_Source (T2))
731 and then (Is_Overloadable (Designated_Type (T2))
733 Ekind (Designated_Type (T2)) = E_Subprogram_Type)
735 Type_Conformant (Designated_Type (T1), Designated_Type (T2))
737 Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
741 -- Ada 2005 (AI-254): An Anonymous_Access_To_Subprogram is compatible
742 -- with itself, or with an anonymous type created for an attribute
745 elsif (Ekind (BT1) = E_Anonymous_Access_Subprogram_Type
748 = E_Anonymous_Access_Protected_Subprogram_Type)
749 and then Is_Access_Type (T2)
750 and then (not Comes_From_Source (T1)
751 or else not Comes_From_Source (T2))
752 and then (Is_Overloadable (Designated_Type (T2))
754 Ekind (Designated_Type (T2)) = E_Subprogram_Type)
756 Type_Conformant (Designated_Type (T1), Designated_Type (T2))
758 Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
762 -- The context can be a remote access type, and the expression the
763 -- corresponding source type declared in a categorized package, or
766 elsif Is_Record_Type (T1)
767 and then (Is_Remote_Call_Interface (T1)
768 or else Is_Remote_Types (T1))
769 and then Present (Corresponding_Remote_Type (T1))
771 return Covers (Corresponding_Remote_Type (T1), T2);
773 elsif Is_Record_Type (T2)
774 and then (Is_Remote_Call_Interface (T2)
775 or else Is_Remote_Types (T2))
776 and then Present (Corresponding_Remote_Type (T2))
778 return Covers (Corresponding_Remote_Type (T2), T1);
780 elsif Ekind (T2) = E_Access_Attribute_Type
781 and then (Ekind (BT1) = E_General_Access_Type
782 or else Ekind (BT1) = E_Access_Type)
783 and then Covers (Designated_Type (T1), Designated_Type (T2))
785 -- If the target type is a RACW type while the source is an access
786 -- attribute type, we are building a RACW that may be exported.
788 if Is_Remote_Access_To_Class_Wide_Type (BT1) then
789 Set_Has_RACW (Current_Sem_Unit);
794 elsif Ekind (T2) = E_Allocator_Type
795 and then Is_Access_Type (T1)
797 return Covers (Designated_Type (T1), Designated_Type (T2))
799 (From_With_Type (Designated_Type (T1))
800 and then Covers (Designated_Type (T2), Designated_Type (T1)));
802 -- A boolean operation on integer literals is compatible with a
805 elsif T2 = Any_Modular
806 and then Is_Modular_Integer_Type (T1)
810 -- The actual type may be the result of a previous error
812 elsif Base_Type (T2) = Any_Type then
815 -- A packed array type covers its corresponding non-packed type.
816 -- This is not legitimate Ada, but allows the omission of a number
817 -- of otherwise useless unchecked conversions, and since this can
818 -- only arise in (known correct) expanded code, no harm is done
820 elsif Is_Array_Type (T2)
821 and then Is_Packed (T2)
822 and then T1 = Packed_Array_Type (T2)
826 -- Similarly an array type covers its corresponding packed array type
828 elsif Is_Array_Type (T1)
829 and then Is_Packed (T1)
830 and then T2 = Packed_Array_Type (T1)
836 (Full_View_Covers (T1, T2)
837 or else Full_View_Covers (T2, T1))
841 -- In the expansion of inlined bodies, types are compatible if they
842 -- are structurally equivalent.
844 elsif In_Inlined_Body
845 and then (Underlying_Type (T1) = Underlying_Type (T2)
846 or else (Is_Access_Type (T1)
847 and then Is_Access_Type (T2)
849 Designated_Type (T1) = Designated_Type (T2))
850 or else (T1 = Any_Access
851 and then Is_Access_Type (Underlying_Type (T2))))
855 -- Ada 2005 (AI-50217): Additional branches to make the shadow entity
856 -- compatible with its real entity.
858 elsif From_With_Type (T1) then
860 -- If the expected type is the non-limited view of a type, the
861 -- expression may have the limited view.
863 if Ekind (T1) = E_Incomplete_Type then
864 return Covers (Non_Limited_View (T1), T2);
866 elsif Ekind (T1) = E_Class_Wide_Type then
868 Covers (Class_Wide_Type (Non_Limited_View (Etype (T1))), T2);
873 elsif From_With_Type (T2) then
875 -- If units in the context have Limited_With clauses on each other,
876 -- either type might have a limited view. Checks performed elsewhere
877 -- verify that the context type is the non-limited view.
879 if Ekind (T2) = E_Incomplete_Type then
880 return Covers (T1, Non_Limited_View (T2));
882 elsif Ekind (T2) = E_Class_Wide_Type then
884 Covers (T1, Class_Wide_Type (Non_Limited_View (Etype (T2))));
889 -- Otherwise it doesn't cover!
900 function Disambiguate
902 I1, I2 : Interp_Index;
909 Nam1, Nam2 : Entity_Id;
910 Predef_Subp : Entity_Id;
911 User_Subp : Entity_Id;
913 function Inherited_From_Actual (S : Entity_Id) return Boolean;
914 -- Determine whether one of the candidates is an operation inherited
915 -- by a type that is derived from an actual in an instantiation.
917 function Is_Actual_Subprogram (S : Entity_Id) return Boolean;
918 -- Determine whether a subprogram is an actual in an enclosing
919 -- instance. An overloading between such a subprogram and one
920 -- declared outside the instance is resolved in favor of the first,
921 -- because it resolved in the generic.
923 function Matches (Actual, Formal : Node_Id) return Boolean;
924 -- Look for exact type match in an instance, to remove spurious
925 -- ambiguities when two formal types have the same actual.
927 function Standard_Operator return Boolean;
928 -- Comment required ???
930 function Remove_Conversions return Interp;
931 -- Last chance for pathological cases involving comparisons on
932 -- literals, and user overloadings of the same operator. Such
933 -- pathologies have been removed from the ACVC, but still appear in
934 -- two DEC tests, with the following notable quote from Ben Brosgol:
936 -- [Note: I disclaim all credit/responsibility/blame for coming up with
937 -- this example; Robert Dewar brought it to our attention, since it
938 -- is apparently found in the ACVC 1.5. I did not attempt to find
939 -- the reason in the Reference Manual that makes the example legal,
940 -- since I was too nauseated by it to want to pursue it further.]
942 -- Accordingly, this is not a fully recursive solution, but it handles
943 -- DEC tests c460vsa, c460vsb. It also handles ai00136a, which pushes
944 -- pathology in the other direction with calls whose multiple overloaded
945 -- actuals make them truly unresolvable.
947 ---------------------------
948 -- Inherited_From_Actual --
949 ---------------------------
951 function Inherited_From_Actual (S : Entity_Id) return Boolean is
952 Par : constant Node_Id := Parent (S);
954 if Nkind (Par) /= N_Full_Type_Declaration
955 or else Nkind (Type_Definition (Par)) /= N_Derived_Type_Definition
959 return Is_Entity_Name (Subtype_Indication (Type_Definition (Par)))
961 Is_Generic_Actual_Type (
962 Entity (Subtype_Indication (Type_Definition (Par))));
964 end Inherited_From_Actual;
966 --------------------------
967 -- Is_Actual_Subprogram --
968 --------------------------
970 function Is_Actual_Subprogram (S : Entity_Id) return Boolean is
972 return In_Open_Scopes (Scope (S))
974 (Is_Generic_Instance (Scope (S))
975 or else Is_Wrapper_Package (Scope (S)));
976 end Is_Actual_Subprogram;
982 function Matches (Actual, Formal : Node_Id) return Boolean is
983 T1 : constant Entity_Id := Etype (Actual);
984 T2 : constant Entity_Id := Etype (Formal);
988 (Is_Numeric_Type (T2)
990 (T1 = Universal_Real or else T1 = Universal_Integer));
993 ------------------------
994 -- Remove_Conversions --
995 ------------------------
997 function Remove_Conversions return Interp is
1008 Get_First_Interp (N, I, It);
1009 while Present (It.Typ) loop
1011 if not Is_Overloadable (It.Nam) then
1015 F1 := First_Formal (It.Nam);
1021 if Nkind (N) = N_Function_Call
1022 or else Nkind (N) = N_Procedure_Call_Statement
1024 Act1 := First_Actual (N);
1026 if Present (Act1) then
1027 Act2 := Next_Actual (Act1);
1032 elsif Nkind (N) in N_Unary_Op then
1033 Act1 := Right_Opnd (N);
1036 elsif Nkind (N) in N_Binary_Op then
1037 Act1 := Left_Opnd (N);
1038 Act2 := Right_Opnd (N);
1044 if Nkind (Act1) in N_Op
1045 and then Is_Overloaded (Act1)
1046 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
1047 or else Nkind (Right_Opnd (Act1)) = N_Real_Literal)
1048 and then Has_Compatible_Type (Act1, Standard_Boolean)
1049 and then Etype (F1) = Standard_Boolean
1051 -- If the two candidates are the original ones, the
1052 -- ambiguity is real. Otherwise keep the original,
1053 -- further calls to Disambiguate will take care of
1054 -- others in the list of candidates.
1056 if It1 /= No_Interp then
1057 if It = Disambiguate.It1
1058 or else It = Disambiguate.It2
1060 if It1 = Disambiguate.It1
1061 or else It1 = Disambiguate.It2
1069 elsif Present (Act2)
1070 and then Nkind (Act2) in N_Op
1071 and then Is_Overloaded (Act2)
1072 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
1074 Nkind (Right_Opnd (Act1)) = N_Real_Literal)
1075 and then Has_Compatible_Type (Act2, Standard_Boolean)
1077 -- The preference rule on the first actual is not
1078 -- sufficient to disambiguate.
1089 Get_Next_Interp (I, It);
1092 -- After some error, a formal may have Any_Type and yield
1093 -- a spurious match. To avoid cascaded errors if possible,
1094 -- check for such a formal in either candidate.
1096 if Serious_Errors_Detected > 0 then
1101 Formal := First_Formal (Nam1);
1102 while Present (Formal) loop
1103 if Etype (Formal) = Any_Type then
1104 return Disambiguate.It2;
1107 Next_Formal (Formal);
1110 Formal := First_Formal (Nam2);
1111 while Present (Formal) loop
1112 if Etype (Formal) = Any_Type then
1113 return Disambiguate.It1;
1116 Next_Formal (Formal);
1122 end Remove_Conversions;
1124 -----------------------
1125 -- Standard_Operator --
1126 -----------------------
1128 function Standard_Operator return Boolean is
1132 if Nkind (N) in N_Op then
1135 elsif Nkind (N) = N_Function_Call then
1138 if Nkind (Nam) /= N_Expanded_Name then
1141 return Entity (Prefix (Nam)) = Standard_Standard;
1146 end Standard_Operator;
1148 -- Start of processing for Disambiguate
1151 -- Recover the two legal interpretations
1153 Get_First_Interp (N, I, It);
1155 Get_Next_Interp (I, It);
1161 Get_Next_Interp (I, It);
1167 -- If the context is universal, the predefined operator is preferred.
1168 -- This includes bounds in numeric type declarations, and expressions
1169 -- in type conversions. If no interpretation yields a universal type,
1170 -- then we must check whether the user-defined entity hides the prede-
1173 if Chars (Nam1) in Any_Operator_Name
1174 and then Standard_Operator
1176 if Typ = Universal_Integer
1177 or else Typ = Universal_Real
1178 or else Typ = Any_Integer
1179 or else Typ = Any_Discrete
1180 or else Typ = Any_Real
1181 or else Typ = Any_Type
1183 -- Find an interpretation that yields the universal type, or else
1184 -- a predefined operator that yields a predefined numeric type.
1187 Candidate : Interp := No_Interp;
1190 Get_First_Interp (N, I, It);
1191 while Present (It.Typ) loop
1192 if (Covers (Typ, It.Typ)
1193 or else Typ = Any_Type)
1195 (It.Typ = Universal_Integer
1196 or else It.Typ = Universal_Real)
1200 elsif Covers (Typ, It.Typ)
1201 and then Scope (It.Typ) = Standard_Standard
1202 and then Scope (It.Nam) = Standard_Standard
1203 and then Is_Numeric_Type (It.Typ)
1208 Get_Next_Interp (I, It);
1211 if Candidate /= No_Interp then
1216 elsif Chars (Nam1) /= Name_Op_Not
1217 and then (Typ = Standard_Boolean or else Typ = Any_Boolean)
1219 -- Equality or comparison operation. Choose predefined operator
1220 -- if arguments are universal. The node may be an operator, a
1221 -- name, or a function call, so unpack arguments accordingly.
1224 Arg1, Arg2 : Node_Id;
1227 if Nkind (N) in N_Op then
1228 Arg1 := Left_Opnd (N);
1229 Arg2 := Right_Opnd (N);
1231 elsif Is_Entity_Name (N)
1232 or else Nkind (N) = N_Operator_Symbol
1234 Arg1 := First_Entity (Entity (N));
1235 Arg2 := Next_Entity (Arg1);
1238 Arg1 := First_Actual (N);
1239 Arg2 := Next_Actual (Arg1);
1243 and then Present (Universal_Interpretation (Arg1))
1244 and then Universal_Interpretation (Arg2) =
1245 Universal_Interpretation (Arg1)
1247 Get_First_Interp (N, I, It);
1248 while Scope (It.Nam) /= Standard_Standard loop
1249 Get_Next_Interp (I, It);
1258 -- If no universal interpretation, check whether user-defined operator
1259 -- hides predefined one, as well as other special cases. If the node
1260 -- is a range, then one or both bounds are ambiguous. Each will have
1261 -- to be disambiguated w.r.t. the context type. The type of the range
1262 -- itself is imposed by the context, so we can return either legal
1265 if Ekind (Nam1) = E_Operator then
1266 Predef_Subp := Nam1;
1269 elsif Ekind (Nam2) = E_Operator then
1270 Predef_Subp := Nam2;
1273 elsif Nkind (N) = N_Range then
1276 -- If two user defined-subprograms are visible, it is a true ambiguity,
1277 -- unless one of them is an entry and the context is a conditional or
1278 -- timed entry call, or unless we are within an instance and this is
1279 -- results from two formals types with the same actual.
1282 if Nkind (N) = N_Procedure_Call_Statement
1283 and then Nkind (Parent (N)) = N_Entry_Call_Alternative
1284 and then N = Entry_Call_Statement (Parent (N))
1286 if Ekind (Nam2) = E_Entry then
1288 elsif Ekind (Nam1) = E_Entry then
1294 -- If the ambiguity occurs within an instance, it is due to several
1295 -- formal types with the same actual. Look for an exact match
1296 -- between the types of the formals of the overloadable entities,
1297 -- and the actuals in the call, to recover the unambiguous match
1298 -- in the original generic.
1300 -- The ambiguity can also be due to an overloading between a formal
1301 -- subprogram and a subprogram declared outside the generic. If the
1302 -- node is overloaded, it did not resolve to the global entity in
1303 -- the generic, and we choose the formal subprogram.
1305 -- Finally, the ambiguity can be between an explicit subprogram and
1306 -- one inherited (with different defaults) from an actual. In this
1307 -- case the resolution was to the explicit declaration in the
1308 -- generic, and remains so in the instance.
1310 elsif In_Instance then
1311 if Nkind (N) = N_Function_Call
1312 or else Nkind (N) = N_Procedure_Call_Statement
1317 Is_Act1 : constant Boolean := Is_Actual_Subprogram (Nam1);
1318 Is_Act2 : constant Boolean := Is_Actual_Subprogram (Nam2);
1321 if Is_Act1 and then not Is_Act2 then
1324 elsif Is_Act2 and then not Is_Act1 then
1327 elsif Inherited_From_Actual (Nam1)
1328 and then Comes_From_Source (Nam2)
1332 elsif Inherited_From_Actual (Nam2)
1333 and then Comes_From_Source (Nam1)
1338 Actual := First_Actual (N);
1339 Formal := First_Formal (Nam1);
1340 while Present (Actual) loop
1341 if Etype (Actual) /= Etype (Formal) then
1345 Next_Actual (Actual);
1346 Next_Formal (Formal);
1352 elsif Nkind (N) in N_Binary_Op then
1353 if Matches (Left_Opnd (N), First_Formal (Nam1))
1355 Matches (Right_Opnd (N), Next_Formal (First_Formal (Nam1)))
1362 elsif Nkind (N) in N_Unary_Op then
1363 if Etype (Right_Opnd (N)) = Etype (First_Formal (Nam1)) then
1370 return Remove_Conversions;
1373 return Remove_Conversions;
1377 -- an implicit concatenation operator on a string type cannot be
1378 -- disambiguated from the predefined concatenation. This can only
1379 -- happen with concatenation of string literals.
1381 if Chars (User_Subp) = Name_Op_Concat
1382 and then Ekind (User_Subp) = E_Operator
1383 and then Is_String_Type (Etype (First_Formal (User_Subp)))
1387 -- If the user-defined operator is in an open scope, or in the scope
1388 -- of the resulting type, or given by an expanded name that names its
1389 -- scope, it hides the predefined operator for the type. Exponentiation
1390 -- has to be special-cased because the implicit operator does not have
1391 -- a symmetric signature, and may not be hidden by the explicit one.
1393 elsif (Nkind (N) = N_Function_Call
1394 and then Nkind (Name (N)) = N_Expanded_Name
1395 and then (Chars (Predef_Subp) /= Name_Op_Expon
1396 or else Hides_Op (User_Subp, Predef_Subp))
1397 and then Scope (User_Subp) = Entity (Prefix (Name (N))))
1398 or else Hides_Op (User_Subp, Predef_Subp)
1400 if It1.Nam = User_Subp then
1406 -- Otherwise, the predefined operator has precedence, or if the
1407 -- user-defined operation is directly visible we have a true ambiguity.
1408 -- If this is a fixed-point multiplication and division in Ada83 mode,
1409 -- exclude the universal_fixed operator, which often causes ambiguities
1413 if (In_Open_Scopes (Scope (User_Subp))
1414 or else Is_Potentially_Use_Visible (User_Subp))
1415 and then not In_Instance
1417 if Is_Fixed_Point_Type (Typ)
1418 and then (Chars (Nam1) = Name_Op_Multiply
1419 or else Chars (Nam1) = Name_Op_Divide)
1420 and then Ada_Version = Ada_83
1422 if It2.Nam = Predef_Subp then
1431 elsif It1.Nam = Predef_Subp then
1440 ---------------------
1441 -- End_Interp_List --
1442 ---------------------
1444 procedure End_Interp_List is
1446 All_Interp.Table (All_Interp.Last) := No_Interp;
1447 All_Interp.Increment_Last;
1448 end End_Interp_List;
1450 -------------------------
1451 -- Entity_Matches_Spec --
1452 -------------------------
1454 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean is
1456 -- Simple case: same entity kinds, type conformance is required.
1457 -- A parameterless function can also rename a literal.
1459 if Ekind (Old_S) = Ekind (New_S)
1460 or else (Ekind (New_S) = E_Function
1461 and then Ekind (Old_S) = E_Enumeration_Literal)
1463 return Type_Conformant (New_S, Old_S);
1465 elsif Ekind (New_S) = E_Function
1466 and then Ekind (Old_S) = E_Operator
1468 return Operator_Matches_Spec (Old_S, New_S);
1470 elsif Ekind (New_S) = E_Procedure
1471 and then Is_Entry (Old_S)
1473 return Type_Conformant (New_S, Old_S);
1478 end Entity_Matches_Spec;
1480 ----------------------
1481 -- Find_Unique_Type --
1482 ----------------------
1484 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id is
1485 T : constant Entity_Id := Etype (L);
1488 TR : Entity_Id := Any_Type;
1491 if Is_Overloaded (R) then
1492 Get_First_Interp (R, I, It);
1493 while Present (It.Typ) loop
1494 if Covers (T, It.Typ) or else Covers (It.Typ, T) then
1496 -- If several interpretations are possible and L is universal,
1497 -- apply preference rule.
1499 if TR /= Any_Type then
1501 if (T = Universal_Integer or else T = Universal_Real)
1512 Get_Next_Interp (I, It);
1517 -- In the non-overloaded case, the Etype of R is already set correctly
1523 -- If one of the operands is Universal_Fixed, the type of the
1524 -- other operand provides the context.
1526 if Etype (R) = Universal_Fixed then
1529 elsif T = Universal_Fixed then
1532 -- Ada 2005 (AI-230): Support the following operators:
1534 -- function "=" (L, R : universal_access) return Boolean;
1535 -- function "/=" (L, R : universal_access) return Boolean;
1537 elsif Ada_Version >= Ada_05
1538 and then Ekind (Etype (L)) = E_Anonymous_Access_Type
1539 and then Is_Access_Type (Etype (R))
1543 elsif Ada_Version >= Ada_05
1544 and then Ekind (Etype (R)) = E_Anonymous_Access_Type
1545 and then Is_Access_Type (Etype (L))
1550 return Specific_Type (T, Etype (R));
1553 end Find_Unique_Type;
1555 ----------------------
1556 -- Get_First_Interp --
1557 ----------------------
1559 procedure Get_First_Interp
1561 I : out Interp_Index;
1565 Int_Ind : Interp_Index;
1569 -- If a selected component is overloaded because the selector has
1570 -- multiple interpretations, the node is a call to a protected
1571 -- operation or an indirect call. Retrieve the interpretation from
1572 -- the selector name. The selected component may be overloaded as well
1573 -- if the prefix is overloaded. That case is unchanged.
1575 if Nkind (N) = N_Selected_Component
1576 and then Is_Overloaded (Selector_Name (N))
1578 O_N := Selector_Name (N);
1583 Map_Ptr := Headers (Hash (O_N));
1584 while Present (Interp_Map.Table (Map_Ptr).Node) loop
1585 if Interp_Map.Table (Map_Ptr).Node = O_N then
1586 Int_Ind := Interp_Map.Table (Map_Ptr).Index;
1587 It := All_Interp.Table (Int_Ind);
1591 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
1595 -- Procedure should never be called if the node has no interpretations
1597 raise Program_Error;
1598 end Get_First_Interp;
1600 ---------------------
1601 -- Get_Next_Interp --
1602 ---------------------
1604 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp) is
1607 It := All_Interp.Table (I);
1608 end Get_Next_Interp;
1610 -------------------------
1611 -- Has_Compatible_Type --
1612 -------------------------
1614 function Has_Compatible_Type
1627 if Nkind (N) = N_Subtype_Indication
1628 or else not Is_Overloaded (N)
1631 Covers (Typ, Etype (N))
1633 (not Is_Tagged_Type (Typ)
1634 and then Ekind (Typ) /= E_Anonymous_Access_Type
1635 and then Covers (Etype (N), Typ));
1638 Get_First_Interp (N, I, It);
1639 while Present (It.Typ) loop
1640 if (Covers (Typ, It.Typ)
1642 (Scope (It.Nam) /= Standard_Standard
1643 or else not Is_Invisible_Operator (N, Base_Type (Typ))))
1644 or else (not Is_Tagged_Type (Typ)
1645 and then Ekind (Typ) /= E_Anonymous_Access_Type
1646 and then Covers (It.Typ, Typ))
1651 Get_Next_Interp (I, It);
1656 end Has_Compatible_Type;
1662 function Hash (N : Node_Id) return Int is
1664 -- Nodes have a size that is power of two, so to select significant
1665 -- bits only we remove the low-order bits.
1667 return ((Int (N) / 2 ** 5) mod Header_Size);
1674 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean is
1675 Btyp : constant Entity_Id := Base_Type (Etype (First_Formal (F)));
1678 return Operator_Matches_Spec (Op, F)
1679 and then (In_Open_Scopes (Scope (F))
1680 or else Scope (F) = Scope (Btyp)
1681 or else (not In_Open_Scopes (Scope (Btyp))
1682 and then not In_Use (Btyp)
1683 and then not In_Use (Scope (Btyp))));
1686 ------------------------
1687 -- Init_Interp_Tables --
1688 ------------------------
1690 procedure Init_Interp_Tables is
1694 Headers := (others => No_Entry);
1695 end Init_Interp_Tables;
1697 ---------------------
1698 -- Intersect_Types --
1699 ---------------------
1701 function Intersect_Types (L, R : Node_Id) return Entity_Id is
1702 Index : Interp_Index;
1706 function Check_Right_Argument (T : Entity_Id) return Entity_Id;
1707 -- Find interpretation of right arg that has type compatible with T
1709 --------------------------
1710 -- Check_Right_Argument --
1711 --------------------------
1713 function Check_Right_Argument (T : Entity_Id) return Entity_Id is
1714 Index : Interp_Index;
1719 if not Is_Overloaded (R) then
1720 return Specific_Type (T, Etype (R));
1723 Get_First_Interp (R, Index, It);
1725 T2 := Specific_Type (T, It.Typ);
1727 if T2 /= Any_Type then
1731 Get_Next_Interp (Index, It);
1732 exit when No (It.Typ);
1737 end Check_Right_Argument;
1739 -- Start processing for Intersect_Types
1742 if Etype (L) = Any_Type or else Etype (R) = Any_Type then
1746 if not Is_Overloaded (L) then
1747 Typ := Check_Right_Argument (Etype (L));
1751 Get_First_Interp (L, Index, It);
1752 while Present (It.Typ) loop
1753 Typ := Check_Right_Argument (It.Typ);
1754 exit when Typ /= Any_Type;
1755 Get_Next_Interp (Index, It);
1760 -- If Typ is Any_Type, it means no compatible pair of types was found
1762 if Typ = Any_Type then
1763 if Nkind (Parent (L)) in N_Op then
1764 Error_Msg_N ("incompatible types for operator", Parent (L));
1766 elsif Nkind (Parent (L)) = N_Range then
1767 Error_Msg_N ("incompatible types given in constraint", Parent (L));
1770 Error_Msg_N ("incompatible types", Parent (L));
1775 end Intersect_Types;
1781 function Is_Ancestor (T1, T2 : Entity_Id) return Boolean is
1785 if Base_Type (T1) = Base_Type (T2) then
1788 elsif Is_Private_Type (T1)
1789 and then Present (Full_View (T1))
1790 and then Base_Type (T2) = Base_Type (Full_View (T1))
1798 -- If there was a error on the type declaration, do not recurse
1800 if Error_Posted (Par) then
1803 elsif Base_Type (T1) = Base_Type (Par)
1804 or else (Is_Private_Type (T1)
1805 and then Present (Full_View (T1))
1806 and then Base_Type (Par) = Base_Type (Full_View (T1)))
1810 elsif Is_Private_Type (Par)
1811 and then Present (Full_View (Par))
1812 and then Full_View (Par) = Base_Type (T1)
1816 elsif Etype (Par) /= Par then
1825 ---------------------------
1826 -- Is_Invisible_Operator --
1827 ---------------------------
1829 function Is_Invisible_Operator
1834 Orig_Node : constant Node_Id := Original_Node (N);
1837 if Nkind (N) not in N_Op then
1840 elsif not Comes_From_Source (N) then
1843 elsif No (Universal_Interpretation (Right_Opnd (N))) then
1846 elsif Nkind (N) in N_Binary_Op
1847 and then No (Universal_Interpretation (Left_Opnd (N)))
1853 and then not In_Open_Scopes (Scope (T))
1854 and then not Is_Potentially_Use_Visible (T)
1855 and then not In_Use (T)
1856 and then not In_Use (Scope (T))
1858 (Nkind (Orig_Node) /= N_Function_Call
1859 or else Nkind (Name (Orig_Node)) /= N_Expanded_Name
1860 or else Entity (Prefix (Name (Orig_Node))) /= Scope (T))
1862 and then not In_Instance;
1864 end Is_Invisible_Operator;
1870 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
1874 S := Ancestor_Subtype (T1);
1875 while Present (S) loop
1879 S := Ancestor_Subtype (S);
1890 procedure List_Interps (Nam : Node_Id; Err : Node_Id) is
1891 Index : Interp_Index;
1895 Get_First_Interp (Nam, Index, It);
1896 while Present (It.Nam) loop
1897 if Scope (It.Nam) = Standard_Standard
1898 and then Scope (It.Typ) /= Standard_Standard
1900 Error_Msg_Sloc := Sloc (Parent (It.Typ));
1901 Error_Msg_NE (" & (inherited) declared#!", Err, It.Nam);
1904 Error_Msg_Sloc := Sloc (It.Nam);
1905 Error_Msg_NE (" & declared#!", Err, It.Nam);
1908 Get_Next_Interp (Index, It);
1916 procedure New_Interps (N : Node_Id) is
1920 All_Interp.Increment_Last;
1921 All_Interp.Table (All_Interp.Last) := No_Interp;
1923 Map_Ptr := Headers (Hash (N));
1925 if Map_Ptr = No_Entry then
1927 -- Place new node at end of table
1929 Interp_Map.Increment_Last;
1930 Headers (Hash (N)) := Interp_Map.Last;
1933 -- Place node at end of chain, or locate its previous entry.
1936 if Interp_Map.Table (Map_Ptr).Node = N then
1938 -- Node is already in the table, and is being rewritten.
1939 -- Start a new interp section, retain hash link.
1941 Interp_Map.Table (Map_Ptr).Node := N;
1942 Interp_Map.Table (Map_Ptr).Index := All_Interp.Last;
1943 Set_Is_Overloaded (N, True);
1947 exit when Interp_Map.Table (Map_Ptr).Next = No_Entry;
1948 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
1952 -- Chain the new node.
1954 Interp_Map.Increment_Last;
1955 Interp_Map.Table (Map_Ptr).Next := Interp_Map.Last;
1958 Interp_Map.Table (Interp_Map.Last) := (N, All_Interp.Last, No_Entry);
1959 Set_Is_Overloaded (N, True);
1962 ---------------------------
1963 -- Operator_Matches_Spec --
1964 ---------------------------
1966 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean is
1967 Op_Name : constant Name_Id := Chars (Op);
1968 T : constant Entity_Id := Etype (New_S);
1976 -- To verify that a predefined operator matches a given signature,
1977 -- do a case analysis of the operator classes. Function can have one
1978 -- or two formals and must have the proper result type.
1980 New_F := First_Formal (New_S);
1981 Old_F := First_Formal (Op);
1983 while Present (New_F) and then Present (Old_F) loop
1985 Next_Formal (New_F);
1986 Next_Formal (Old_F);
1989 -- Definite mismatch if different number of parameters
1991 if Present (Old_F) or else Present (New_F) then
1997 T1 := Etype (First_Formal (New_S));
1999 if Op_Name = Name_Op_Subtract
2000 or else Op_Name = Name_Op_Add
2001 or else Op_Name = Name_Op_Abs
2003 return Base_Type (T1) = Base_Type (T)
2004 and then Is_Numeric_Type (T);
2006 elsif Op_Name = Name_Op_Not then
2007 return Base_Type (T1) = Base_Type (T)
2008 and then Valid_Boolean_Arg (Base_Type (T));
2017 T1 := Etype (First_Formal (New_S));
2018 T2 := Etype (Next_Formal (First_Formal (New_S)));
2020 if Op_Name = Name_Op_And or else Op_Name = Name_Op_Or
2021 or else Op_Name = Name_Op_Xor
2023 return Base_Type (T1) = Base_Type (T2)
2024 and then Base_Type (T1) = Base_Type (T)
2025 and then Valid_Boolean_Arg (Base_Type (T));
2027 elsif Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
2028 return Base_Type (T1) = Base_Type (T2)
2029 and then not Is_Limited_Type (T1)
2030 and then Is_Boolean_Type (T);
2032 elsif Op_Name = Name_Op_Lt or else Op_Name = Name_Op_Le
2033 or else Op_Name = Name_Op_Gt or else Op_Name = Name_Op_Ge
2035 return Base_Type (T1) = Base_Type (T2)
2036 and then Valid_Comparison_Arg (T1)
2037 and then Is_Boolean_Type (T);
2039 elsif Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
2040 return Base_Type (T1) = Base_Type (T2)
2041 and then Base_Type (T1) = Base_Type (T)
2042 and then Is_Numeric_Type (T);
2044 -- for division and multiplication, a user-defined function does
2045 -- not match the predefined universal_fixed operation, except in
2048 elsif Op_Name = Name_Op_Divide then
2049 return (Base_Type (T1) = Base_Type (T2)
2050 and then Base_Type (T1) = Base_Type (T)
2051 and then Is_Numeric_Type (T)
2052 and then (not Is_Fixed_Point_Type (T)
2053 or else Ada_Version = Ada_83))
2055 -- Mixed_Mode operations on fixed-point types
2057 or else (Base_Type (T1) = Base_Type (T)
2058 and then Base_Type (T2) = Base_Type (Standard_Integer)
2059 and then Is_Fixed_Point_Type (T))
2061 -- A user defined operator can also match (and hide) a mixed
2062 -- operation on universal literals.
2064 or else (Is_Integer_Type (T2)
2065 and then Is_Floating_Point_Type (T1)
2066 and then Base_Type (T1) = Base_Type (T));
2068 elsif Op_Name = Name_Op_Multiply then
2069 return (Base_Type (T1) = Base_Type (T2)
2070 and then Base_Type (T1) = Base_Type (T)
2071 and then Is_Numeric_Type (T)
2072 and then (not Is_Fixed_Point_Type (T)
2073 or else Ada_Version = Ada_83))
2075 -- Mixed_Mode operations on fixed-point types
2077 or else (Base_Type (T1) = Base_Type (T)
2078 and then Base_Type (T2) = Base_Type (Standard_Integer)
2079 and then Is_Fixed_Point_Type (T))
2081 or else (Base_Type (T2) = Base_Type (T)
2082 and then Base_Type (T1) = Base_Type (Standard_Integer)
2083 and then Is_Fixed_Point_Type (T))
2085 or else (Is_Integer_Type (T2)
2086 and then Is_Floating_Point_Type (T1)
2087 and then Base_Type (T1) = Base_Type (T))
2089 or else (Is_Integer_Type (T1)
2090 and then Is_Floating_Point_Type (T2)
2091 and then Base_Type (T2) = Base_Type (T));
2093 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
2094 return Base_Type (T1) = Base_Type (T2)
2095 and then Base_Type (T1) = Base_Type (T)
2096 and then Is_Integer_Type (T);
2098 elsif Op_Name = Name_Op_Expon then
2099 return Base_Type (T1) = Base_Type (T)
2100 and then Is_Numeric_Type (T)
2101 and then Base_Type (T2) = Base_Type (Standard_Integer);
2103 elsif Op_Name = Name_Op_Concat then
2104 return Is_Array_Type (T)
2105 and then (Base_Type (T) = Base_Type (Etype (Op)))
2106 and then (Base_Type (T1) = Base_Type (T)
2108 Base_Type (T1) = Base_Type (Component_Type (T)))
2109 and then (Base_Type (T2) = Base_Type (T)
2111 Base_Type (T2) = Base_Type (Component_Type (T)));
2117 end Operator_Matches_Spec;
2123 procedure Remove_Interp (I : in out Interp_Index) is
2127 -- Find end of Interp list and copy downward to erase the discarded one
2130 while Present (All_Interp.Table (II).Typ) loop
2134 for J in I + 1 .. II loop
2135 All_Interp.Table (J - 1) := All_Interp.Table (J);
2138 -- Back up interp. index to insure that iterator will pick up next
2139 -- available interpretation.
2148 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id) is
2150 O_N : Node_Id := Old_N;
2153 if Is_Overloaded (Old_N) then
2154 if Nkind (Old_N) = N_Selected_Component
2155 and then Is_Overloaded (Selector_Name (Old_N))
2157 O_N := Selector_Name (Old_N);
2160 Map_Ptr := Headers (Hash (O_N));
2162 while Interp_Map.Table (Map_Ptr).Node /= O_N loop
2163 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
2164 pragma Assert (Map_Ptr /= No_Entry);
2167 New_Interps (New_N);
2168 Interp_Map.Table (Interp_Map.Last).Index :=
2169 Interp_Map.Table (Map_Ptr).Index;
2177 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
2178 B1 : constant Entity_Id := Base_Type (T1);
2179 B2 : constant Entity_Id := Base_Type (T2);
2181 function Is_Remote_Access (T : Entity_Id) return Boolean;
2182 -- Check whether T is the equivalent type of a remote access type.
2183 -- If distribution is enabled, T is a legal context for Null.
2185 ----------------------
2186 -- Is_Remote_Access --
2187 ----------------------
2189 function Is_Remote_Access (T : Entity_Id) return Boolean is
2191 return Is_Record_Type (T)
2192 and then (Is_Remote_Call_Interface (T)
2193 or else Is_Remote_Types (T))
2194 and then Present (Corresponding_Remote_Type (T))
2195 and then Is_Access_Type (Corresponding_Remote_Type (T));
2196 end Is_Remote_Access;
2198 -- Start of processing for Specific_Type
2201 if T1 = Any_Type or else T2 = Any_Type then
2209 or else (T1 = Universal_Integer and then Is_Integer_Type (T2))
2210 or else (T1 = Universal_Real and then Is_Real_Type (T2))
2211 or else (T1 = Universal_Fixed and then Is_Fixed_Point_Type (T2))
2212 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
2217 or else (T2 = Universal_Integer and then Is_Integer_Type (T1))
2218 or else (T2 = Universal_Real and then Is_Real_Type (T1))
2219 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
2220 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
2224 elsif T2 = Any_String and then Is_String_Type (T1) then
2227 elsif T1 = Any_String and then Is_String_Type (T2) then
2230 elsif T2 = Any_Character and then Is_Character_Type (T1) then
2233 elsif T1 = Any_Character and then Is_Character_Type (T2) then
2236 elsif T1 = Any_Access
2237 and then (Is_Access_Type (T2) or else Is_Remote_Access (T2))
2241 elsif T2 = Any_Access
2242 and then (Is_Access_Type (T1) or else Is_Remote_Access (T1))
2246 elsif T2 = Any_Composite
2247 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
2251 elsif T1 = Any_Composite
2252 and then Ekind (T2) in E_Array_Type .. E_Record_Subtype
2256 elsif T1 = Any_Modular and then Is_Modular_Integer_Type (T2) then
2259 elsif T2 = Any_Modular and then Is_Modular_Integer_Type (T1) then
2262 -- Special cases for equality operators (all other predefined
2263 -- operators can never apply to tagged types)
2265 elsif Is_Class_Wide_Type (T1)
2266 and then Is_Ancestor (Root_Type (T1), T2)
2270 elsif Is_Class_Wide_Type (T2)
2271 and then Is_Ancestor (Root_Type (T2), T1)
2275 elsif (Ekind (B1) = E_Access_Subprogram_Type
2277 Ekind (B1) = E_Access_Protected_Subprogram_Type)
2278 and then Ekind (Designated_Type (B1)) /= E_Subprogram_Type
2279 and then Is_Access_Type (T2)
2283 elsif (Ekind (B2) = E_Access_Subprogram_Type
2285 Ekind (B2) = E_Access_Protected_Subprogram_Type)
2286 and then Ekind (Designated_Type (B2)) /= E_Subprogram_Type
2287 and then Is_Access_Type (T1)
2291 elsif (Ekind (T1) = E_Allocator_Type
2292 or else Ekind (T1) = E_Access_Attribute_Type
2293 or else Ekind (T1) = E_Anonymous_Access_Type)
2294 and then Is_Access_Type (T2)
2298 elsif (Ekind (T2) = E_Allocator_Type
2299 or else Ekind (T2) = E_Access_Attribute_Type
2300 or else Ekind (T2) = E_Anonymous_Access_Type)
2301 and then Is_Access_Type (T1)
2305 -- If none of the above cases applies, types are not compatible.
2312 -----------------------
2313 -- Valid_Boolean_Arg --
2314 -----------------------
2316 -- In addition to booleans and arrays of booleans, we must include
2317 -- aggregates as valid boolean arguments, because in the first pass
2318 -- of resolution their components are not examined. If it turns out not
2319 -- to be an aggregate of booleans, this will be diagnosed in Resolve.
2320 -- Any_Composite must be checked for prior to the array type checks
2321 -- because Any_Composite does not have any associated indexes.
2323 function Valid_Boolean_Arg (T : Entity_Id) return Boolean is
2325 return Is_Boolean_Type (T)
2326 or else T = Any_Composite
2327 or else (Is_Array_Type (T)
2328 and then T /= Any_String
2329 and then Number_Dimensions (T) = 1
2330 and then Is_Boolean_Type (Component_Type (T))
2331 and then (not Is_Private_Composite (T)
2332 or else In_Instance)
2333 and then (not Is_Limited_Composite (T)
2334 or else In_Instance))
2335 or else Is_Modular_Integer_Type (T)
2336 or else T = Universal_Integer;
2337 end Valid_Boolean_Arg;
2339 --------------------------
2340 -- Valid_Comparison_Arg --
2341 --------------------------
2343 function Valid_Comparison_Arg (T : Entity_Id) return Boolean is
2346 if T = Any_Composite then
2348 elsif Is_Discrete_Type (T)
2349 or else Is_Real_Type (T)
2352 elsif Is_Array_Type (T)
2353 and then Number_Dimensions (T) = 1
2354 and then Is_Discrete_Type (Component_Type (T))
2355 and then (not Is_Private_Composite (T)
2356 or else In_Instance)
2357 and then (not Is_Limited_Composite (T)
2358 or else In_Instance)
2361 elsif Is_String_Type (T) then
2366 end Valid_Comparison_Arg;
2368 ---------------------
2369 -- Write_Overloads --
2370 ---------------------
2372 procedure Write_Overloads (N : Node_Id) is
2378 if not Is_Overloaded (N) then
2379 Write_Str ("Non-overloaded entity ");
2381 Write_Entity_Info (Entity (N), " ");
2384 Get_First_Interp (N, I, It);
2385 Write_Str ("Overloaded entity ");
2389 while Present (Nam) loop
2390 Write_Entity_Info (Nam, " ");
2391 Write_Str ("=================");
2393 Get_Next_Interp (I, It);
2397 end Write_Overloads;
2399 ----------------------
2400 -- Write_Interp_Ref --
2401 ----------------------
2403 procedure Write_Interp_Ref (Map_Ptr : Int) is
2405 Write_Str (" Node: ");
2406 Write_Int (Int (Interp_Map.Table (Map_Ptr).Node));
2407 Write_Str (" Index: ");
2408 Write_Int (Int (Interp_Map.Table (Map_Ptr).Index));
2409 Write_Str (" Next: ");
2410 Write_Int (Int (Interp_Map.Table (Map_Ptr).Next));
2412 end Write_Interp_Ref;