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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Elists; use Elists;
32 with Errout; use Errout;
35 with Output; use Output;
37 with Sem_Ch6; use Sem_Ch6;
38 with Sem_Ch8; use Sem_Ch8;
39 with Sem_Util; use Sem_Util;
40 with Stand; use Stand;
41 with Sinfo; use Sinfo;
42 with Snames; use Snames;
44 with Uintp; use Uintp;
46 package body Sem_Type is
52 -- The following data structures establish a mapping between nodes and
53 -- their interpretations. An overloaded node has an entry in Interp_Map,
54 -- which in turn contains a pointer into the All_Interp array. The
55 -- interpretations of a given node are contiguous in All_Interp. Each
56 -- set of interpretations is terminated with the marker No_Interp.
57 -- In order to speed up the retrieval of the interpretations of an
58 -- overloaded node, the Interp_Map table is accessed by means of a simple
59 -- hashing scheme, and the entries in Interp_Map are chained. The heads
60 -- of clash lists are stored in array Headers.
62 -- Headers Interp_Map All_Interp
64 -- _ +-----+ +--------+
65 -- |_| |_____| --->|interp1 |
66 -- |_|---------->|node | | |interp2 |
67 -- |_| |index|---------| |nointerp|
72 -- This scheme does not currently reclaim interpretations. In principle,
73 -- after a unit is compiled, all overloadings have been resolved, and the
74 -- candidate interpretations should be deleted. This should be easier
75 -- now than with the previous scheme???
77 package All_Interp is new Table.Table (
78 Table_Component_Type => Interp,
79 Table_Index_Type => Int,
81 Table_Initial => Alloc.All_Interp_Initial,
82 Table_Increment => Alloc.All_Interp_Increment,
83 Table_Name => "All_Interp");
85 type Interp_Ref is record
91 Header_Size : constant Int := 2 ** 12;
92 No_Entry : constant Int := -1;
93 Headers : array (0 .. Header_Size) of Int := (others => No_Entry);
95 package Interp_Map is new Table.Table (
96 Table_Component_Type => Interp_Ref,
97 Table_Index_Type => Int,
99 Table_Initial => Alloc.Interp_Map_Initial,
100 Table_Increment => Alloc.Interp_Map_Increment,
101 Table_Name => "Interp_Map");
103 function Hash (N : Node_Id) return Int;
104 -- A trivial hashing function for nodes, used to insert an overloaded
105 -- node into the Interp_Map table.
107 -------------------------------------
108 -- Handling of Overload Resolution --
109 -------------------------------------
111 -- Overload resolution uses two passes over the syntax tree of a complete
112 -- context. In the first, bottom-up pass, the types of actuals in calls
113 -- are used to resolve possibly overloaded subprogram and operator names.
114 -- In the second top-down pass, the type of the context (for example the
115 -- condition in a while statement) is used to resolve a possibly ambiguous
116 -- call, and the unique subprogram name in turn imposes a specific context
117 -- on each of its actuals.
119 -- Most expressions are in fact unambiguous, and the bottom-up pass is
120 -- sufficient to resolve most everything. To simplify the common case,
121 -- names and expressions carry a flag Is_Overloaded to indicate whether
122 -- they have more than one interpretation. If the flag is off, then each
123 -- name has already a unique meaning and type, and the bottom-up pass is
124 -- sufficient (and much simpler).
126 --------------------------
127 -- Operator Overloading --
128 --------------------------
130 -- The visibility of operators is handled differently from that of
131 -- other entities. We do not introduce explicit versions of primitive
132 -- operators for each type definition. As a result, there is only one
133 -- entity corresponding to predefined addition on all numeric types, etc.
134 -- The back-end resolves predefined operators according to their type.
135 -- The visibility of primitive operations then reduces to the visibility
136 -- of the resulting type: (a + b) is a legal interpretation of some
137 -- primitive operator + if the type of the result (which must also be
138 -- the type of a and b) is directly visible (i.e. either immediately
139 -- visible or use-visible.)
141 -- User-defined operators are treated like other functions, but the
142 -- visibility of these user-defined operations must be special-cased
143 -- to determine whether they hide or are hidden by predefined operators.
144 -- The form P."+" (x, y) requires additional handling.
146 -- Concatenation is treated more conventionally: for every one-dimensional
147 -- array type we introduce a explicit concatenation operator. This is
148 -- necessary to handle the case of (element & element => array) which
149 -- cannot be handled conveniently if there is no explicit instance of
150 -- resulting type of the operation.
152 -----------------------
153 -- Local Subprograms --
154 -----------------------
156 procedure All_Overloads;
157 pragma Warnings (Off, All_Overloads);
158 -- Debugging procedure: list full contents of Overloads table
160 procedure New_Interps (N : Node_Id);
161 -- Initialize collection of interpretations for the given node, which is
162 -- either an overloaded entity, or an operation whose arguments have
163 -- multiple intepretations. Interpretations can be added to only one
166 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
167 -- If T1 and T2 are compatible, return the one that is not
168 -- universal or is not a "class" type (any_character, etc).
174 procedure Add_One_Interp
178 Opnd_Type : Entity_Id := Empty)
180 Vis_Type : Entity_Id;
182 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id);
183 -- Add one interpretation to node. Node is already known to be
184 -- overloaded. Add new interpretation if not hidden by previous
185 -- one, and remove previous one if hidden by new one.
187 function Is_Universal_Operation (Op : Entity_Id) return Boolean;
188 -- True if the entity is a predefined operator and the operands have
189 -- a universal Interpretation.
195 procedure Add_Entry (Name : Entity_Id; Typ : Entity_Id) is
196 Index : Interp_Index;
200 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);
300 while Present (Arg) loop
301 if No (Universal_Interpretation (Arg)) then
313 end Is_Universal_Operation;
315 -- Start of processing for Add_One_Interp
318 -- If the interpretation is a predefined operator, verify that the
319 -- result type is visible, or that the entity has already been
320 -- resolved (case of an instantiation node that refers to a predefined
321 -- operation, or an internally generated operator node, or an operator
322 -- given as an expanded name). If the operator is a comparison or
323 -- equality, it is the type of the operand that matters to determine
324 -- whether the operator is visible. In an instance, the check is not
325 -- performed, given that the operator was visible in the generic.
327 if Ekind (E) = E_Operator then
329 if Present (Opnd_Type) then
330 Vis_Type := Opnd_Type;
332 Vis_Type := Base_Type (T);
335 if In_Open_Scopes (Scope (Vis_Type))
336 or else Is_Potentially_Use_Visible (Vis_Type)
337 or else In_Use (Vis_Type)
338 or else (In_Use (Scope (Vis_Type))
339 and then not Is_Hidden (Vis_Type))
340 or else Nkind (N) = N_Expanded_Name
341 or else (Nkind (N) in N_Op and then E = Entity (N))
346 -- If the node is given in functional notation and the prefix
347 -- is an expanded name, then the operator is visible if the
348 -- prefix is the scope of the result type as well. If the
349 -- operator is (implicitly) defined in an extension of system,
350 -- it is know to be valid (see Defined_In_Scope, sem_ch4.adb).
352 elsif Nkind (N) = N_Function_Call
353 and then Nkind (Name (N)) = N_Expanded_Name
354 and then (Entity (Prefix (Name (N))) = Scope (Base_Type (T))
355 or else Entity (Prefix (Name (N))) = Scope (Vis_Type)
356 or else Scope (Vis_Type) = System_Aux_Id)
360 -- Save type for subsequent error message, in case no other
361 -- interpretation is found.
364 Candidate_Type := Vis_Type;
368 -- In an instance, an abstract non-dispatching operation cannot
369 -- be a candidate interpretation, because it could not have been
370 -- one in the generic (it may be a spurious overloading in the
374 and then Is_Abstract (E)
375 and then not Is_Dispatching_Operation (E)
380 -- If this is the first interpretation of N, N has type Any_Type.
381 -- In that case place the new type on the node. If one interpretation
382 -- already exists, indicate that the node is overloaded, and store
383 -- both the previous and the new interpretation in All_Interp. If
384 -- this is a later interpretation, just add it to the set.
386 if Etype (N) = Any_Type then
391 -- Record both the operator or subprogram name, and its type
393 if Nkind (N) in N_Op or else Is_Entity_Name (N) then
400 -- Either there is no current interpretation in the table for any
401 -- node or the interpretation that is present is for a different
402 -- node. In both cases add a new interpretation to the table.
404 elsif Interp_Map.Last < 0
406 (Interp_Map.Table (Interp_Map.Last).Node /= N
407 and then not Is_Overloaded (N))
411 if (Nkind (N) in N_Op or else Is_Entity_Name (N))
412 and then Present (Entity (N))
414 Add_Entry (Entity (N), Etype (N));
416 elsif (Nkind (N) = N_Function_Call
417 or else Nkind (N) = N_Procedure_Call_Statement)
418 and then (Nkind (Name (N)) = N_Operator_Symbol
419 or else Is_Entity_Name (Name (N)))
421 Add_Entry (Entity (Name (N)), Etype (N));
424 -- Overloaded prefix in indexed or selected component,
425 -- or call whose name is an expresion or another call.
427 Add_Entry (Etype (N), Etype (N));
441 procedure All_Overloads is
443 for J in All_Interp.First .. All_Interp.Last loop
445 if Present (All_Interp.Table (J).Nam) then
446 Write_Entity_Info (All_Interp.Table (J). Nam, " ");
448 Write_Str ("No Interp");
451 Write_Str ("=================");
456 ---------------------
457 -- Collect_Interps --
458 ---------------------
460 procedure Collect_Interps (N : Node_Id) is
461 Ent : constant Entity_Id := Entity (N);
463 First_Interp : Interp_Index;
468 -- Unconditionally add the entity that was initially matched
470 First_Interp := All_Interp.Last;
471 Add_One_Interp (N, Ent, Etype (N));
473 -- For expanded name, pick up all additional entities from the
474 -- same scope, since these are obviously also visible. Note that
475 -- these are not necessarily contiguous on the homonym chain.
477 if Nkind (N) = N_Expanded_Name then
479 while Present (H) loop
480 if Scope (H) = Scope (Entity (N)) then
481 Add_One_Interp (N, H, Etype (H));
487 -- Case of direct name
490 -- First, search the homonym chain for directly visible entities
492 H := Current_Entity (Ent);
493 while Present (H) loop
494 exit when (not Is_Overloadable (H))
495 and then Is_Immediately_Visible (H);
497 if Is_Immediately_Visible (H)
500 -- Only add interpretation if not hidden by an inner
501 -- immediately visible one.
503 for J in First_Interp .. All_Interp.Last - 1 loop
505 -- Current homograph is not hidden. Add to overloads
507 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
510 -- Homograph is hidden, unless it is a predefined operator
512 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
514 -- A homograph in the same scope can occur within an
515 -- instantiation, the resulting ambiguity has to be
518 if Scope (H) = Scope (Ent)
520 and then not Is_Inherited_Operation (H)
522 All_Interp.Table (All_Interp.Last) := (H, Etype (H));
523 All_Interp.Increment_Last;
524 All_Interp.Table (All_Interp.Last) := No_Interp;
527 elsif Scope (H) /= Standard_Standard then
533 -- On exit, we know that current homograph is not hidden
535 Add_One_Interp (N, H, Etype (H));
538 Write_Str ("Add overloaded Interpretation ");
548 -- Scan list of homographs for use-visible entities only
550 H := Current_Entity (Ent);
552 while Present (H) loop
553 if Is_Potentially_Use_Visible (H)
555 and then Is_Overloadable (H)
557 for J in First_Interp .. All_Interp.Last - 1 loop
559 if not Is_Immediately_Visible (All_Interp.Table (J).Nam) then
562 elsif Type_Conformant (H, All_Interp.Table (J).Nam) then
563 goto Next_Use_Homograph;
567 Add_One_Interp (N, H, Etype (H));
570 <<Next_Use_Homograph>>
575 if All_Interp.Last = First_Interp + 1 then
577 -- The original interpretation is in fact not overloaded
579 Set_Is_Overloaded (N, False);
587 function Covers (T1, T2 : Entity_Id) return Boolean is
592 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean;
593 -- In an instance the proper view may not always be correct for
594 -- private types, but private and full view are compatible. This
595 -- removes spurious errors from nested instantiations that involve,
596 -- among other things, types derived from private types.
598 ----------------------
599 -- Full_View_Covers --
600 ----------------------
602 function Full_View_Covers (Typ1, Typ2 : Entity_Id) return Boolean is
605 Is_Private_Type (Typ1)
607 ((Present (Full_View (Typ1))
608 and then Covers (Full_View (Typ1), Typ2))
609 or else Base_Type (Typ1) = Typ2
610 or else Base_Type (Typ2) = Typ1);
611 end Full_View_Covers;
613 -- Start of processing for Covers
616 -- If either operand missing, then this is an error, but ignore it (and
617 -- pretend we have a cover) if errors already detected, since this may
618 -- simply mean we have malformed trees.
620 if No (T1) or else No (T2) then
621 if Total_Errors_Detected /= 0 then
628 BT1 := Base_Type (T1);
629 BT2 := Base_Type (T2);
632 -- Simplest case: same types are compatible, and types that have the
633 -- same base type and are not generic actuals are compatible. Generic
634 -- actuals belong to their class but are not compatible with other
635 -- types of their class, and in particular with other generic actuals.
636 -- They are however compatible with their own subtypes, and itypes
637 -- with the same base are compatible as well. Similary, constrained
638 -- subtypes obtained from expressions of an unconstrained nominal type
639 -- are compatible with the base type (may lead to spurious ambiguities
640 -- in obscure cases ???)
642 -- Generic actuals require special treatment to avoid spurious ambi-
643 -- guities in an instance, when two formal types are instantiated with
644 -- the same actual, so that different subprograms end up with the same
645 -- signature in the instance.
654 if not Is_Generic_Actual_Type (T1) then
657 return (not Is_Generic_Actual_Type (T2)
658 or else Is_Itype (T1)
659 or else Is_Itype (T2)
660 or else Is_Constr_Subt_For_U_Nominal (T1)
661 or else Is_Constr_Subt_For_U_Nominal (T2)
662 or else Scope (T1) /= Scope (T2));
665 -- Literals are compatible with types in a given "class"
667 elsif (T2 = Universal_Integer and then Is_Integer_Type (T1))
668 or else (T2 = Universal_Real and then Is_Real_Type (T1))
669 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
670 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
671 or else (T2 = Any_String and then Is_String_Type (T1))
672 or else (T2 = Any_Character and then Is_Character_Type (T1))
673 or else (T2 = Any_Access and then Is_Access_Type (T1))
677 -- The context may be class wide
679 elsif Is_Class_Wide_Type (T1)
680 and then Is_Ancestor (Root_Type (T1), T2)
684 elsif Is_Class_Wide_Type (T1)
685 and then Is_Class_Wide_Type (T2)
686 and then Base_Type (Etype (T1)) = Base_Type (Etype (T2))
690 -- Ada 2005 (AI-345): A class-wide abstract interface type T1 covers a
691 -- task_type or protected_type implementing T1
693 elsif Ada_Version >= Ada_05
694 and then Is_Class_Wide_Type (T1)
695 and then Is_Interface (Etype (T1))
696 and then Is_Concurrent_Type (T2)
697 and then Interface_Present_In_Ancestor (
698 Typ => Corresponding_Record_Type (Base_Type (T2)),
703 -- Ada 2005 (AI-251): A class-wide abstract interface type T1 covers an
704 -- object T2 implementing T1
706 elsif Ada_Version >= Ada_05
707 and then Is_Class_Wide_Type (T1)
708 and then Is_Interface (Etype (T1))
709 and then Is_Tagged_Type (T2)
711 if Interface_Present_In_Ancestor (Typ => T2,
716 elsif Present (Abstract_Interfaces (T2)) then
718 -- Ada 2005 (AI-251): A class-wide abstract interface type T1
719 -- covers an object T2 that implements a direct derivation of T1.
722 E : Elmt_Id := First_Elmt (Abstract_Interfaces (T2));
724 while Present (E) loop
725 if Is_Ancestor (Etype (T1), Node (E)) then
733 -- We should also check the case in which T1 is an ancestor of
734 -- some implemented interface???
742 -- In a dispatching call the actual may be class-wide
744 elsif Is_Class_Wide_Type (T2)
745 and then Base_Type (Root_Type (T2)) = Base_Type (T1)
749 -- Some contexts require a class of types rather than a specific type
751 elsif (T1 = Any_Integer and then Is_Integer_Type (T2))
752 or else (T1 = Any_Boolean and then Is_Boolean_Type (T2))
753 or else (T1 = Any_Real and then Is_Real_Type (T2))
754 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
755 or else (T1 = Any_Discrete and then Is_Discrete_Type (T2))
759 -- An aggregate is compatible with an array or record type
761 elsif T2 = Any_Composite
762 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
766 -- If the expected type is an anonymous access, the designated type must
767 -- cover that of the expression.
769 elsif Ekind (T1) = E_Anonymous_Access_Type
770 and then Is_Access_Type (T2)
771 and then Covers (Designated_Type (T1), Designated_Type (T2))
775 -- An Access_To_Subprogram is compatible with itself, or with an
776 -- anonymous type created for an attribute reference Access.
778 elsif (Ekind (BT1) = E_Access_Subprogram_Type
780 Ekind (BT1) = E_Access_Protected_Subprogram_Type)
781 and then Is_Access_Type (T2)
782 and then (not Comes_From_Source (T1)
783 or else not Comes_From_Source (T2))
784 and then (Is_Overloadable (Designated_Type (T2))
786 Ekind (Designated_Type (T2)) = E_Subprogram_Type)
788 Type_Conformant (Designated_Type (T1), Designated_Type (T2))
790 Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
794 -- Ada 2005 (AI-254): An Anonymous_Access_To_Subprogram is compatible
795 -- with itself, or with an anonymous type created for an attribute
798 elsif (Ekind (BT1) = E_Anonymous_Access_Subprogram_Type
801 = E_Anonymous_Access_Protected_Subprogram_Type)
802 and then Is_Access_Type (T2)
803 and then (not Comes_From_Source (T1)
804 or else not Comes_From_Source (T2))
805 and then (Is_Overloadable (Designated_Type (T2))
807 Ekind (Designated_Type (T2)) = E_Subprogram_Type)
809 Type_Conformant (Designated_Type (T1), Designated_Type (T2))
811 Mode_Conformant (Designated_Type (T1), Designated_Type (T2))
815 -- The context can be a remote access type, and the expression the
816 -- corresponding source type declared in a categorized package, or
819 elsif Is_Record_Type (T1)
820 and then (Is_Remote_Call_Interface (T1)
821 or else Is_Remote_Types (T1))
822 and then Present (Corresponding_Remote_Type (T1))
824 return Covers (Corresponding_Remote_Type (T1), T2);
826 elsif Is_Record_Type (T2)
827 and then (Is_Remote_Call_Interface (T2)
828 or else Is_Remote_Types (T2))
829 and then Present (Corresponding_Remote_Type (T2))
831 return Covers (Corresponding_Remote_Type (T2), T1);
833 elsif Ekind (T2) = E_Access_Attribute_Type
834 and then (Ekind (BT1) = E_General_Access_Type
835 or else Ekind (BT1) = E_Access_Type)
836 and then Covers (Designated_Type (T1), Designated_Type (T2))
838 -- If the target type is a RACW type while the source is an access
839 -- attribute type, we are building a RACW that may be exported.
841 if Is_Remote_Access_To_Class_Wide_Type (BT1) then
842 Set_Has_RACW (Current_Sem_Unit);
847 elsif Ekind (T2) = E_Allocator_Type
848 and then Is_Access_Type (T1)
850 return Covers (Designated_Type (T1), Designated_Type (T2))
852 (From_With_Type (Designated_Type (T1))
853 and then Covers (Designated_Type (T2), Designated_Type (T1)));
855 -- A boolean operation on integer literals is compatible with modular
858 elsif T2 = Any_Modular
859 and then Is_Modular_Integer_Type (T1)
863 -- The actual type may be the result of a previous error
865 elsif Base_Type (T2) = Any_Type then
868 -- A packed array type covers its corresponding non-packed type. This is
869 -- not legitimate Ada, but allows the omission of a number of otherwise
870 -- useless unchecked conversions, and since this can only arise in
871 -- (known correct) expanded code, no harm is done
873 elsif Is_Array_Type (T2)
874 and then Is_Packed (T2)
875 and then T1 = Packed_Array_Type (T2)
879 -- Similarly an array type covers its corresponding packed array type
881 elsif Is_Array_Type (T1)
882 and then Is_Packed (T1)
883 and then T2 = Packed_Array_Type (T1)
889 (Full_View_Covers (T1, T2)
890 or else Full_View_Covers (T2, T1))
894 -- In the expansion of inlined bodies, types are compatible if they
895 -- are structurally equivalent.
897 elsif In_Inlined_Body
898 and then (Underlying_Type (T1) = Underlying_Type (T2)
899 or else (Is_Access_Type (T1)
900 and then Is_Access_Type (T2)
902 Designated_Type (T1) = Designated_Type (T2))
903 or else (T1 = Any_Access
904 and then Is_Access_Type (Underlying_Type (T2))))
908 -- Ada 2005 (AI-50217): Additional branches to make the shadow entity
909 -- compatible with its real entity.
911 elsif From_With_Type (T1) then
913 -- If the expected type is the non-limited view of a type, the
914 -- expression may have the limited view.
916 if Ekind (T1) = E_Incomplete_Type then
917 return Covers (Non_Limited_View (T1), T2);
919 elsif Ekind (T1) = E_Class_Wide_Type then
921 Covers (Class_Wide_Type (Non_Limited_View (Etype (T1))), T2);
926 elsif From_With_Type (T2) then
928 -- If units in the context have Limited_With clauses on each other,
929 -- either type might have a limited view. Checks performed elsewhere
930 -- verify that the context type is the non-limited view.
932 if Ekind (T2) = E_Incomplete_Type then
933 return Covers (T1, Non_Limited_View (T2));
935 elsif Ekind (T2) = E_Class_Wide_Type then
937 Covers (T1, Class_Wide_Type (Non_Limited_View (Etype (T2))));
942 -- Otherwise it doesn't cover!
953 function Disambiguate
955 I1, I2 : Interp_Index;
962 Nam1, Nam2 : Entity_Id;
963 Predef_Subp : Entity_Id;
964 User_Subp : Entity_Id;
966 function Inherited_From_Actual (S : Entity_Id) return Boolean;
967 -- Determine whether one of the candidates is an operation inherited by
968 -- a type that is derived from an actual in an instantiation.
970 function Is_Actual_Subprogram (S : Entity_Id) return Boolean;
971 -- Determine whether a subprogram is an actual in an enclosing instance.
972 -- An overloading between such a subprogram and one declared outside the
973 -- instance is resolved in favor of the first, because it resolved in
976 function Matches (Actual, Formal : Node_Id) return Boolean;
977 -- Look for exact type match in an instance, to remove spurious
978 -- ambiguities when two formal types have the same actual.
980 function Standard_Operator return Boolean;
981 -- Comment required ???
983 function Remove_Conversions return Interp;
984 -- Last chance for pathological cases involving comparisons on literals,
985 -- and user overloadings of the same operator. Such pathologies have
986 -- been removed from the ACVC, but still appear in two DEC tests, with
987 -- the following notable quote from Ben Brosgol:
989 -- [Note: I disclaim all credit/responsibility/blame for coming up with
990 -- this example; Robert Dewar brought it to our attention, since it is
991 -- apparently found in the ACVC 1.5. I did not attempt to find the
992 -- reason in the Reference Manual that makes the example legal, since I
993 -- was too nauseated by it to want to pursue it further.]
995 -- Accordingly, this is not a fully recursive solution, but it handles
996 -- DEC tests c460vsa, c460vsb. It also handles ai00136a, which pushes
997 -- pathology in the other direction with calls whose multiple overloaded
998 -- actuals make them truly unresolvable.
1000 ---------------------------
1001 -- Inherited_From_Actual --
1002 ---------------------------
1004 function Inherited_From_Actual (S : Entity_Id) return Boolean is
1005 Par : constant Node_Id := Parent (S);
1007 if Nkind (Par) /= N_Full_Type_Declaration
1008 or else Nkind (Type_Definition (Par)) /= N_Derived_Type_Definition
1012 return Is_Entity_Name (Subtype_Indication (Type_Definition (Par)))
1014 Is_Generic_Actual_Type (
1015 Entity (Subtype_Indication (Type_Definition (Par))));
1017 end Inherited_From_Actual;
1019 --------------------------
1020 -- Is_Actual_Subprogram --
1021 --------------------------
1023 function Is_Actual_Subprogram (S : Entity_Id) return Boolean is
1025 return In_Open_Scopes (Scope (S))
1027 (Is_Generic_Instance (Scope (S))
1028 or else Is_Wrapper_Package (Scope (S)));
1029 end Is_Actual_Subprogram;
1035 function Matches (Actual, Formal : Node_Id) return Boolean is
1036 T1 : constant Entity_Id := Etype (Actual);
1037 T2 : constant Entity_Id := Etype (Formal);
1041 (Is_Numeric_Type (T2)
1043 (T1 = Universal_Real or else T1 = Universal_Integer));
1046 ------------------------
1047 -- Remove_Conversions --
1048 ------------------------
1050 function Remove_Conversions return Interp is
1061 Get_First_Interp (N, I, It);
1062 while Present (It.Typ) loop
1064 if not Is_Overloadable (It.Nam) then
1068 F1 := First_Formal (It.Nam);
1074 if Nkind (N) = N_Function_Call
1075 or else Nkind (N) = N_Procedure_Call_Statement
1077 Act1 := First_Actual (N);
1079 if Present (Act1) then
1080 Act2 := Next_Actual (Act1);
1085 elsif Nkind (N) in N_Unary_Op then
1086 Act1 := Right_Opnd (N);
1089 elsif Nkind (N) in N_Binary_Op then
1090 Act1 := Left_Opnd (N);
1091 Act2 := Right_Opnd (N);
1097 if Nkind (Act1) in N_Op
1098 and then Is_Overloaded (Act1)
1099 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
1100 or else Nkind (Right_Opnd (Act1)) = N_Real_Literal)
1101 and then Has_Compatible_Type (Act1, Standard_Boolean)
1102 and then Etype (F1) = Standard_Boolean
1104 -- If the two candidates are the original ones, the
1105 -- ambiguity is real. Otherwise keep the original, further
1106 -- calls to Disambiguate will take care of others in the
1107 -- list of candidates.
1109 if It1 /= No_Interp then
1110 if It = Disambiguate.It1
1111 or else It = Disambiguate.It2
1113 if It1 = Disambiguate.It1
1114 or else It1 = Disambiguate.It2
1122 elsif Present (Act2)
1123 and then Nkind (Act2) in N_Op
1124 and then Is_Overloaded (Act2)
1125 and then (Nkind (Right_Opnd (Act1)) = N_Integer_Literal
1127 Nkind (Right_Opnd (Act1)) = N_Real_Literal)
1128 and then Has_Compatible_Type (Act2, Standard_Boolean)
1130 -- The preference rule on the first actual is not
1131 -- sufficient to disambiguate.
1142 Get_Next_Interp (I, It);
1145 -- After some error, a formal may have Any_Type and yield a spurious
1146 -- match. To avoid cascaded errors if possible, check for such a
1147 -- formal in either candidate.
1149 if Serious_Errors_Detected > 0 then
1154 Formal := First_Formal (Nam1);
1155 while Present (Formal) loop
1156 if Etype (Formal) = Any_Type then
1157 return Disambiguate.It2;
1160 Next_Formal (Formal);
1163 Formal := First_Formal (Nam2);
1164 while Present (Formal) loop
1165 if Etype (Formal) = Any_Type then
1166 return Disambiguate.It1;
1169 Next_Formal (Formal);
1175 end Remove_Conversions;
1177 -----------------------
1178 -- Standard_Operator --
1179 -----------------------
1181 function Standard_Operator return Boolean is
1185 if Nkind (N) in N_Op then
1188 elsif Nkind (N) = N_Function_Call then
1191 if Nkind (Nam) /= N_Expanded_Name then
1194 return Entity (Prefix (Nam)) = Standard_Standard;
1199 end Standard_Operator;
1201 -- Start of processing for Disambiguate
1204 -- Recover the two legal interpretations
1206 Get_First_Interp (N, I, It);
1208 Get_Next_Interp (I, It);
1214 Get_Next_Interp (I, It);
1220 -- If the context is universal, the predefined operator is preferred.
1221 -- This includes bounds in numeric type declarations, and expressions
1222 -- in type conversions. If no interpretation yields a universal type,
1223 -- then we must check whether the user-defined entity hides the prede-
1226 if Chars (Nam1) in Any_Operator_Name
1227 and then Standard_Operator
1229 if Typ = Universal_Integer
1230 or else Typ = Universal_Real
1231 or else Typ = Any_Integer
1232 or else Typ = Any_Discrete
1233 or else Typ = Any_Real
1234 or else Typ = Any_Type
1236 -- Find an interpretation that yields the universal type, or else
1237 -- a predefined operator that yields a predefined numeric type.
1240 Candidate : Interp := No_Interp;
1243 Get_First_Interp (N, I, It);
1244 while Present (It.Typ) loop
1245 if (Covers (Typ, It.Typ)
1246 or else Typ = Any_Type)
1248 (It.Typ = Universal_Integer
1249 or else It.Typ = Universal_Real)
1253 elsif Covers (Typ, It.Typ)
1254 and then Scope (It.Typ) = Standard_Standard
1255 and then Scope (It.Nam) = Standard_Standard
1256 and then Is_Numeric_Type (It.Typ)
1261 Get_Next_Interp (I, It);
1264 if Candidate /= No_Interp then
1269 elsif Chars (Nam1) /= Name_Op_Not
1270 and then (Typ = Standard_Boolean or else Typ = Any_Boolean)
1272 -- Equality or comparison operation. Choose predefined operator if
1273 -- arguments are universal. The node may be an operator, name, or
1274 -- a function call, so unpack arguments accordingly.
1277 Arg1, Arg2 : Node_Id;
1280 if Nkind (N) in N_Op then
1281 Arg1 := Left_Opnd (N);
1282 Arg2 := Right_Opnd (N);
1284 elsif Is_Entity_Name (N)
1285 or else Nkind (N) = N_Operator_Symbol
1287 Arg1 := First_Entity (Entity (N));
1288 Arg2 := Next_Entity (Arg1);
1291 Arg1 := First_Actual (N);
1292 Arg2 := Next_Actual (Arg1);
1296 and then Present (Universal_Interpretation (Arg1))
1297 and then Universal_Interpretation (Arg2) =
1298 Universal_Interpretation (Arg1)
1300 Get_First_Interp (N, I, It);
1301 while Scope (It.Nam) /= Standard_Standard loop
1302 Get_Next_Interp (I, It);
1311 -- If no universal interpretation, check whether user-defined operator
1312 -- hides predefined one, as well as other special cases. If the node
1313 -- is a range, then one or both bounds are ambiguous. Each will have
1314 -- to be disambiguated w.r.t. the context type. The type of the range
1315 -- itself is imposed by the context, so we can return either legal
1318 if Ekind (Nam1) = E_Operator then
1319 Predef_Subp := Nam1;
1322 elsif Ekind (Nam2) = E_Operator then
1323 Predef_Subp := Nam2;
1326 elsif Nkind (N) = N_Range then
1329 -- If two user defined-subprograms are visible, it is a true ambiguity,
1330 -- unless one of them is an entry and the context is a conditional or
1331 -- timed entry call, or unless we are within an instance and this is
1332 -- results from two formals types with the same actual.
1335 if Nkind (N) = N_Procedure_Call_Statement
1336 and then Nkind (Parent (N)) = N_Entry_Call_Alternative
1337 and then N = Entry_Call_Statement (Parent (N))
1339 if Ekind (Nam2) = E_Entry then
1341 elsif Ekind (Nam1) = E_Entry then
1347 -- If the ambiguity occurs within an instance, it is due to several
1348 -- formal types with the same actual. Look for an exact match between
1349 -- the types of the formals of the overloadable entities, and the
1350 -- actuals in the call, to recover the unambiguous match in the
1351 -- original generic.
1353 -- The ambiguity can also be due to an overloading between a formal
1354 -- subprogram and a subprogram declared outside the generic. If the
1355 -- node is overloaded, it did not resolve to the global entity in
1356 -- the generic, and we choose the formal subprogram.
1358 -- Finally, the ambiguity can be between an explicit subprogram and
1359 -- one inherited (with different defaults) from an actual. In this
1360 -- case the resolution was to the explicit declaration in the
1361 -- generic, and remains so in the instance.
1363 elsif In_Instance then
1364 if Nkind (N) = N_Function_Call
1365 or else Nkind (N) = N_Procedure_Call_Statement
1370 Is_Act1 : constant Boolean := Is_Actual_Subprogram (Nam1);
1371 Is_Act2 : constant Boolean := Is_Actual_Subprogram (Nam2);
1374 if Is_Act1 and then not Is_Act2 then
1377 elsif Is_Act2 and then not Is_Act1 then
1380 elsif Inherited_From_Actual (Nam1)
1381 and then Comes_From_Source (Nam2)
1385 elsif Inherited_From_Actual (Nam2)
1386 and then Comes_From_Source (Nam1)
1391 Actual := First_Actual (N);
1392 Formal := First_Formal (Nam1);
1393 while Present (Actual) loop
1394 if Etype (Actual) /= Etype (Formal) then
1398 Next_Actual (Actual);
1399 Next_Formal (Formal);
1405 elsif Nkind (N) in N_Binary_Op then
1406 if Matches (Left_Opnd (N), First_Formal (Nam1))
1408 Matches (Right_Opnd (N), Next_Formal (First_Formal (Nam1)))
1415 elsif Nkind (N) in N_Unary_Op then
1416 if Etype (Right_Opnd (N)) = Etype (First_Formal (Nam1)) then
1423 return Remove_Conversions;
1426 return Remove_Conversions;
1430 -- an implicit concatenation operator on a string type cannot be
1431 -- disambiguated from the predefined concatenation. This can only
1432 -- happen with concatenation of string literals.
1434 if Chars (User_Subp) = Name_Op_Concat
1435 and then Ekind (User_Subp) = E_Operator
1436 and then Is_String_Type (Etype (First_Formal (User_Subp)))
1440 -- If the user-defined operator is in an open scope, or in the scope
1441 -- of the resulting type, or given by an expanded name that names its
1442 -- scope, it hides the predefined operator for the type. Exponentiation
1443 -- has to be special-cased because the implicit operator does not have
1444 -- a symmetric signature, and may not be hidden by the explicit one.
1446 elsif (Nkind (N) = N_Function_Call
1447 and then Nkind (Name (N)) = N_Expanded_Name
1448 and then (Chars (Predef_Subp) /= Name_Op_Expon
1449 or else Hides_Op (User_Subp, Predef_Subp))
1450 and then Scope (User_Subp) = Entity (Prefix (Name (N))))
1451 or else Hides_Op (User_Subp, Predef_Subp)
1453 if It1.Nam = User_Subp then
1459 -- Otherwise, the predefined operator has precedence, or if the user-
1460 -- defined operation is directly visible we have a true ambiguity. If
1461 -- this is a fixed-point multiplication and division in Ada83 mode,
1462 -- exclude the universal_fixed operator, which often causes ambiguities
1466 if (In_Open_Scopes (Scope (User_Subp))
1467 or else Is_Potentially_Use_Visible (User_Subp))
1468 and then not In_Instance
1470 if Is_Fixed_Point_Type (Typ)
1471 and then (Chars (Nam1) = Name_Op_Multiply
1472 or else Chars (Nam1) = Name_Op_Divide)
1473 and then Ada_Version = Ada_83
1475 if It2.Nam = Predef_Subp then
1484 elsif It1.Nam = Predef_Subp then
1493 ---------------------
1494 -- End_Interp_List --
1495 ---------------------
1497 procedure End_Interp_List is
1499 All_Interp.Table (All_Interp.Last) := No_Interp;
1500 All_Interp.Increment_Last;
1501 end End_Interp_List;
1503 -------------------------
1504 -- Entity_Matches_Spec --
1505 -------------------------
1507 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean is
1509 -- Simple case: same entity kinds, type conformance is required. A
1510 -- parameterless function can also rename a literal.
1512 if Ekind (Old_S) = Ekind (New_S)
1513 or else (Ekind (New_S) = E_Function
1514 and then Ekind (Old_S) = E_Enumeration_Literal)
1516 return Type_Conformant (New_S, Old_S);
1518 elsif Ekind (New_S) = E_Function
1519 and then Ekind (Old_S) = E_Operator
1521 return Operator_Matches_Spec (Old_S, New_S);
1523 elsif Ekind (New_S) = E_Procedure
1524 and then Is_Entry (Old_S)
1526 return Type_Conformant (New_S, Old_S);
1531 end Entity_Matches_Spec;
1533 ----------------------
1534 -- Find_Unique_Type --
1535 ----------------------
1537 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id is
1538 T : constant Entity_Id := Etype (L);
1541 TR : Entity_Id := Any_Type;
1544 if Is_Overloaded (R) then
1545 Get_First_Interp (R, I, It);
1546 while Present (It.Typ) loop
1547 if Covers (T, It.Typ) or else Covers (It.Typ, T) then
1549 -- If several interpretations are possible and L is universal,
1550 -- apply preference rule.
1552 if TR /= Any_Type then
1554 if (T = Universal_Integer or else T = Universal_Real)
1565 Get_Next_Interp (I, It);
1570 -- In the non-overloaded case, the Etype of R is already set correctly
1576 -- If one of the operands is Universal_Fixed, the type of the other
1577 -- operand provides the context.
1579 if Etype (R) = Universal_Fixed then
1582 elsif T = Universal_Fixed then
1585 -- Ada 2005 (AI-230): Support the following operators:
1587 -- function "=" (L, R : universal_access) return Boolean;
1588 -- function "/=" (L, R : universal_access) return Boolean;
1590 elsif Ada_Version >= Ada_05
1591 and then Ekind (Etype (L)) = E_Anonymous_Access_Type
1592 and then Is_Access_Type (Etype (R))
1596 elsif Ada_Version >= Ada_05
1597 and then Ekind (Etype (R)) = E_Anonymous_Access_Type
1598 and then Is_Access_Type (Etype (L))
1603 return Specific_Type (T, Etype (R));
1606 end Find_Unique_Type;
1608 ----------------------
1609 -- Get_First_Interp --
1610 ----------------------
1612 procedure Get_First_Interp
1614 I : out Interp_Index;
1618 Int_Ind : Interp_Index;
1622 -- If a selected component is overloaded because the selector has
1623 -- multiple interpretations, the node is a call to a protected
1624 -- operation or an indirect call. Retrieve the interpretation from
1625 -- the selector name. The selected component may be overloaded as well
1626 -- if the prefix is overloaded. That case is unchanged.
1628 if Nkind (N) = N_Selected_Component
1629 and then Is_Overloaded (Selector_Name (N))
1631 O_N := Selector_Name (N);
1636 Map_Ptr := Headers (Hash (O_N));
1637 while Present (Interp_Map.Table (Map_Ptr).Node) loop
1638 if Interp_Map.Table (Map_Ptr).Node = O_N then
1639 Int_Ind := Interp_Map.Table (Map_Ptr).Index;
1640 It := All_Interp.Table (Int_Ind);
1644 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
1648 -- Procedure should never be called if the node has no interpretations
1650 raise Program_Error;
1651 end Get_First_Interp;
1653 ---------------------
1654 -- Get_Next_Interp --
1655 ---------------------
1657 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp) is
1660 It := All_Interp.Table (I);
1661 end Get_Next_Interp;
1663 -------------------------
1664 -- Has_Compatible_Type --
1665 -------------------------
1667 function Has_Compatible_Type
1680 if Nkind (N) = N_Subtype_Indication
1681 or else not Is_Overloaded (N)
1684 Covers (Typ, Etype (N))
1686 -- Ada 2005 (AI-345) The context may be a synchronized interface.
1687 -- If the type is already frozen use the corresponding_record
1688 -- to check whether it is a proper descendant.
1691 (Is_Concurrent_Type (Etype (N))
1692 and then Present (Corresponding_Record_Type (Etype (N)))
1693 and then Covers (Typ, Corresponding_Record_Type (Etype (N))))
1696 (not Is_Tagged_Type (Typ)
1697 and then Ekind (Typ) /= E_Anonymous_Access_Type
1698 and then Covers (Etype (N), Typ));
1701 Get_First_Interp (N, I, It);
1702 while Present (It.Typ) loop
1703 if (Covers (Typ, It.Typ)
1705 (Scope (It.Nam) /= Standard_Standard
1706 or else not Is_Invisible_Operator (N, Base_Type (Typ))))
1708 -- Ada 2005 (AI-345)
1711 (Is_Concurrent_Type (It.Typ)
1712 and then Covers (Typ, Corresponding_Record_Type
1715 or else (not Is_Tagged_Type (Typ)
1716 and then Ekind (Typ) /= E_Anonymous_Access_Type
1717 and then Covers (It.Typ, Typ))
1722 Get_Next_Interp (I, It);
1727 end Has_Compatible_Type;
1733 function Hash (N : Node_Id) return Int is
1735 -- Nodes have a size that is power of two, so to select significant
1736 -- bits only we remove the low-order bits.
1738 return ((Int (N) / 2 ** 5) mod Header_Size);
1745 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean is
1746 Btyp : constant Entity_Id := Base_Type (Etype (First_Formal (F)));
1748 return Operator_Matches_Spec (Op, F)
1749 and then (In_Open_Scopes (Scope (F))
1750 or else Scope (F) = Scope (Btyp)
1751 or else (not In_Open_Scopes (Scope (Btyp))
1752 and then not In_Use (Btyp)
1753 and then not In_Use (Scope (Btyp))));
1756 ------------------------
1757 -- Init_Interp_Tables --
1758 ------------------------
1760 procedure Init_Interp_Tables is
1764 Headers := (others => No_Entry);
1765 end Init_Interp_Tables;
1767 -----------------------------------
1768 -- Interface_Present_In_Ancestor --
1769 -----------------------------------
1771 function Interface_Present_In_Ancestor
1773 Iface : Entity_Id) return Boolean
1780 if Is_Access_Type (Typ) then
1781 E := Etype (Directly_Designated_Type (Typ));
1786 if Is_Concurrent_Type (E) then
1787 E := Corresponding_Record_Type (E);
1790 if Is_Class_Wide_Type (E) then
1799 if Present (Abstract_Interfaces (E))
1800 and then Abstract_Interfaces (E) /= Empty_List_Or_Node -- ????
1801 and then not Is_Empty_Elmt_List (Abstract_Interfaces (E))
1803 Elmt := First_Elmt (Abstract_Interfaces (E));
1805 while Present (Elmt) loop
1808 if AI = Iface or else Is_Ancestor (Iface, AI) then
1816 exit when Etype (E) = E;
1818 -- Check if the current type is a direct derivation of the
1821 if Etype (E) = Iface then
1825 -- Climb to the immediate ancestor
1831 end Interface_Present_In_Ancestor;
1833 ---------------------
1834 -- Intersect_Types --
1835 ---------------------
1837 function Intersect_Types (L, R : Node_Id) return Entity_Id is
1838 Index : Interp_Index;
1842 function Check_Right_Argument (T : Entity_Id) return Entity_Id;
1843 -- Find interpretation of right arg that has type compatible with T
1845 --------------------------
1846 -- Check_Right_Argument --
1847 --------------------------
1849 function Check_Right_Argument (T : Entity_Id) return Entity_Id is
1850 Index : Interp_Index;
1855 if not Is_Overloaded (R) then
1856 return Specific_Type (T, Etype (R));
1859 Get_First_Interp (R, Index, It);
1861 T2 := Specific_Type (T, It.Typ);
1863 if T2 /= Any_Type then
1867 Get_Next_Interp (Index, It);
1868 exit when No (It.Typ);
1873 end Check_Right_Argument;
1875 -- Start processing for Intersect_Types
1878 if Etype (L) = Any_Type or else Etype (R) = Any_Type then
1882 if not Is_Overloaded (L) then
1883 Typ := Check_Right_Argument (Etype (L));
1887 Get_First_Interp (L, Index, It);
1888 while Present (It.Typ) loop
1889 Typ := Check_Right_Argument (It.Typ);
1890 exit when Typ /= Any_Type;
1891 Get_Next_Interp (Index, It);
1896 -- If Typ is Any_Type, it means no compatible pair of types was found
1898 if Typ = Any_Type then
1899 if Nkind (Parent (L)) in N_Op then
1900 Error_Msg_N ("incompatible types for operator", Parent (L));
1902 elsif Nkind (Parent (L)) = N_Range then
1903 Error_Msg_N ("incompatible types given in constraint", Parent (L));
1905 -- Ada 2005 (AI-251): Complete the error notification
1907 elsif Is_Class_Wide_Type (Etype (R))
1908 and then Is_Interface (Etype (Class_Wide_Type (Etype (R))))
1910 Error_Msg_Name_1 := Chars (L);
1911 Error_Msg_Name_2 := Chars (Etype (Class_Wide_Type (Etype (R))));
1912 Error_Msg_NE ("(Ada 2005) % does not implement interface %",
1913 L, Etype (Class_Wide_Type (Etype (R))));
1916 Error_Msg_N ("incompatible types", Parent (L));
1921 end Intersect_Types;
1927 function Is_Ancestor (T1, T2 : Entity_Id) return Boolean is
1931 if Base_Type (T1) = Base_Type (T2) then
1934 elsif Is_Private_Type (T1)
1935 and then Present (Full_View (T1))
1936 and then Base_Type (T2) = Base_Type (Full_View (T1))
1944 -- If there was a error on the type declaration, do not recurse
1946 if Error_Posted (Par) then
1949 elsif Base_Type (T1) = Base_Type (Par)
1950 or else (Is_Private_Type (T1)
1951 and then Present (Full_View (T1))
1952 and then Base_Type (Par) = Base_Type (Full_View (T1)))
1956 elsif Is_Private_Type (Par)
1957 and then Present (Full_View (Par))
1958 and then Full_View (Par) = Base_Type (T1)
1962 elsif Etype (Par) /= Par then
1971 ---------------------------
1972 -- Is_Invisible_Operator --
1973 ---------------------------
1975 function Is_Invisible_Operator
1980 Orig_Node : constant Node_Id := Original_Node (N);
1983 if Nkind (N) not in N_Op then
1986 elsif not Comes_From_Source (N) then
1989 elsif No (Universal_Interpretation (Right_Opnd (N))) then
1992 elsif Nkind (N) in N_Binary_Op
1993 and then No (Universal_Interpretation (Left_Opnd (N)))
1999 and then not In_Open_Scopes (Scope (T))
2000 and then not Is_Potentially_Use_Visible (T)
2001 and then not In_Use (T)
2002 and then not In_Use (Scope (T))
2004 (Nkind (Orig_Node) /= N_Function_Call
2005 or else Nkind (Name (Orig_Node)) /= N_Expanded_Name
2006 or else Entity (Prefix (Name (Orig_Node))) /= Scope (T))
2008 and then not In_Instance;
2010 end Is_Invisible_Operator;
2016 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
2020 S := Ancestor_Subtype (T1);
2021 while Present (S) loop
2025 S := Ancestor_Subtype (S);
2036 procedure List_Interps (Nam : Node_Id; Err : Node_Id) is
2037 Index : Interp_Index;
2041 Get_First_Interp (Nam, Index, It);
2042 while Present (It.Nam) loop
2043 if Scope (It.Nam) = Standard_Standard
2044 and then Scope (It.Typ) /= Standard_Standard
2046 Error_Msg_Sloc := Sloc (Parent (It.Typ));
2047 Error_Msg_NE (" & (inherited) declared#!", Err, It.Nam);
2050 Error_Msg_Sloc := Sloc (It.Nam);
2051 Error_Msg_NE (" & declared#!", Err, It.Nam);
2054 Get_Next_Interp (Index, It);
2062 procedure New_Interps (N : Node_Id) is
2066 All_Interp.Increment_Last;
2067 All_Interp.Table (All_Interp.Last) := No_Interp;
2069 Map_Ptr := Headers (Hash (N));
2071 if Map_Ptr = No_Entry then
2073 -- Place new node at end of table
2075 Interp_Map.Increment_Last;
2076 Headers (Hash (N)) := Interp_Map.Last;
2079 -- Place node at end of chain, or locate its previous entry
2082 if Interp_Map.Table (Map_Ptr).Node = N then
2084 -- Node is already in the table, and is being rewritten.
2085 -- Start a new interp section, retain hash link.
2087 Interp_Map.Table (Map_Ptr).Node := N;
2088 Interp_Map.Table (Map_Ptr).Index := All_Interp.Last;
2089 Set_Is_Overloaded (N, True);
2093 exit when Interp_Map.Table (Map_Ptr).Next = No_Entry;
2094 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
2098 -- Chain the new node
2100 Interp_Map.Increment_Last;
2101 Interp_Map.Table (Map_Ptr).Next := Interp_Map.Last;
2104 Interp_Map.Table (Interp_Map.Last) := (N, All_Interp.Last, No_Entry);
2105 Set_Is_Overloaded (N, True);
2108 ---------------------------
2109 -- Operator_Matches_Spec --
2110 ---------------------------
2112 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean is
2113 Op_Name : constant Name_Id := Chars (Op);
2114 T : constant Entity_Id := Etype (New_S);
2122 -- To verify that a predefined operator matches a given signature,
2123 -- do a case analysis of the operator classes. Function can have one
2124 -- or two formals and must have the proper result type.
2126 New_F := First_Formal (New_S);
2127 Old_F := First_Formal (Op);
2129 while Present (New_F) and then Present (Old_F) loop
2131 Next_Formal (New_F);
2132 Next_Formal (Old_F);
2135 -- Definite mismatch if different number of parameters
2137 if Present (Old_F) or else Present (New_F) then
2143 T1 := Etype (First_Formal (New_S));
2145 if Op_Name = Name_Op_Subtract
2146 or else Op_Name = Name_Op_Add
2147 or else Op_Name = Name_Op_Abs
2149 return Base_Type (T1) = Base_Type (T)
2150 and then Is_Numeric_Type (T);
2152 elsif Op_Name = Name_Op_Not then
2153 return Base_Type (T1) = Base_Type (T)
2154 and then Valid_Boolean_Arg (Base_Type (T));
2163 T1 := Etype (First_Formal (New_S));
2164 T2 := Etype (Next_Formal (First_Formal (New_S)));
2166 if Op_Name = Name_Op_And or else Op_Name = Name_Op_Or
2167 or else Op_Name = Name_Op_Xor
2169 return Base_Type (T1) = Base_Type (T2)
2170 and then Base_Type (T1) = Base_Type (T)
2171 and then Valid_Boolean_Arg (Base_Type (T));
2173 elsif Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
2174 return Base_Type (T1) = Base_Type (T2)
2175 and then not Is_Limited_Type (T1)
2176 and then Is_Boolean_Type (T);
2178 elsif Op_Name = Name_Op_Lt or else Op_Name = Name_Op_Le
2179 or else Op_Name = Name_Op_Gt or else Op_Name = Name_Op_Ge
2181 return Base_Type (T1) = Base_Type (T2)
2182 and then Valid_Comparison_Arg (T1)
2183 and then Is_Boolean_Type (T);
2185 elsif Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
2186 return Base_Type (T1) = Base_Type (T2)
2187 and then Base_Type (T1) = Base_Type (T)
2188 and then Is_Numeric_Type (T);
2190 -- for division and multiplication, a user-defined function does
2191 -- not match the predefined universal_fixed operation, except in
2194 elsif Op_Name = Name_Op_Divide then
2195 return (Base_Type (T1) = Base_Type (T2)
2196 and then Base_Type (T1) = Base_Type (T)
2197 and then Is_Numeric_Type (T)
2198 and then (not Is_Fixed_Point_Type (T)
2199 or else Ada_Version = Ada_83))
2201 -- Mixed_Mode operations on fixed-point types
2203 or else (Base_Type (T1) = Base_Type (T)
2204 and then Base_Type (T2) = Base_Type (Standard_Integer)
2205 and then Is_Fixed_Point_Type (T))
2207 -- A user defined operator can also match (and hide) a mixed
2208 -- operation on universal literals.
2210 or else (Is_Integer_Type (T2)
2211 and then Is_Floating_Point_Type (T1)
2212 and then Base_Type (T1) = Base_Type (T));
2214 elsif Op_Name = Name_Op_Multiply then
2215 return (Base_Type (T1) = Base_Type (T2)
2216 and then Base_Type (T1) = Base_Type (T)
2217 and then Is_Numeric_Type (T)
2218 and then (not Is_Fixed_Point_Type (T)
2219 or else Ada_Version = Ada_83))
2221 -- Mixed_Mode operations on fixed-point types
2223 or else (Base_Type (T1) = Base_Type (T)
2224 and then Base_Type (T2) = Base_Type (Standard_Integer)
2225 and then Is_Fixed_Point_Type (T))
2227 or else (Base_Type (T2) = Base_Type (T)
2228 and then Base_Type (T1) = Base_Type (Standard_Integer)
2229 and then Is_Fixed_Point_Type (T))
2231 or else (Is_Integer_Type (T2)
2232 and then Is_Floating_Point_Type (T1)
2233 and then Base_Type (T1) = Base_Type (T))
2235 or else (Is_Integer_Type (T1)
2236 and then Is_Floating_Point_Type (T2)
2237 and then Base_Type (T2) = Base_Type (T));
2239 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
2240 return Base_Type (T1) = Base_Type (T2)
2241 and then Base_Type (T1) = Base_Type (T)
2242 and then Is_Integer_Type (T);
2244 elsif Op_Name = Name_Op_Expon then
2245 return Base_Type (T1) = Base_Type (T)
2246 and then Is_Numeric_Type (T)
2247 and then Base_Type (T2) = Base_Type (Standard_Integer);
2249 elsif Op_Name = Name_Op_Concat then
2250 return Is_Array_Type (T)
2251 and then (Base_Type (T) = Base_Type (Etype (Op)))
2252 and then (Base_Type (T1) = Base_Type (T)
2254 Base_Type (T1) = Base_Type (Component_Type (T)))
2255 and then (Base_Type (T2) = Base_Type (T)
2257 Base_Type (T2) = Base_Type (Component_Type (T)));
2263 end Operator_Matches_Spec;
2269 procedure Remove_Interp (I : in out Interp_Index) is
2273 -- Find end of Interp list and copy downward to erase the discarded one
2276 while Present (All_Interp.Table (II).Typ) loop
2280 for J in I + 1 .. II loop
2281 All_Interp.Table (J - 1) := All_Interp.Table (J);
2284 -- Back up interp. index to insure that iterator will pick up next
2285 -- available interpretation.
2294 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id) is
2296 O_N : Node_Id := Old_N;
2299 if Is_Overloaded (Old_N) then
2300 if Nkind (Old_N) = N_Selected_Component
2301 and then Is_Overloaded (Selector_Name (Old_N))
2303 O_N := Selector_Name (Old_N);
2306 Map_Ptr := Headers (Hash (O_N));
2308 while Interp_Map.Table (Map_Ptr).Node /= O_N loop
2309 Map_Ptr := Interp_Map.Table (Map_Ptr).Next;
2310 pragma Assert (Map_Ptr /= No_Entry);
2313 New_Interps (New_N);
2314 Interp_Map.Table (Interp_Map.Last).Index :=
2315 Interp_Map.Table (Map_Ptr).Index;
2323 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
2324 B1 : constant Entity_Id := Base_Type (T1);
2325 B2 : constant Entity_Id := Base_Type (T2);
2327 function Is_Remote_Access (T : Entity_Id) return Boolean;
2328 -- Check whether T is the equivalent type of a remote access type.
2329 -- If distribution is enabled, T is a legal context for Null.
2331 ----------------------
2332 -- Is_Remote_Access --
2333 ----------------------
2335 function Is_Remote_Access (T : Entity_Id) return Boolean is
2337 return Is_Record_Type (T)
2338 and then (Is_Remote_Call_Interface (T)
2339 or else Is_Remote_Types (T))
2340 and then Present (Corresponding_Remote_Type (T))
2341 and then Is_Access_Type (Corresponding_Remote_Type (T));
2342 end Is_Remote_Access;
2344 -- Start of processing for Specific_Type
2347 if T1 = Any_Type or else T2 = Any_Type then
2355 or else (T1 = Universal_Integer and then Is_Integer_Type (T2))
2356 or else (T1 = Universal_Real and then Is_Real_Type (T2))
2357 or else (T1 = Universal_Fixed and then Is_Fixed_Point_Type (T2))
2358 or else (T1 = Any_Fixed and then Is_Fixed_Point_Type (T2))
2363 or else (T2 = Universal_Integer and then Is_Integer_Type (T1))
2364 or else (T2 = Universal_Real and then Is_Real_Type (T1))
2365 or else (T2 = Universal_Fixed and then Is_Fixed_Point_Type (T1))
2366 or else (T2 = Any_Fixed and then Is_Fixed_Point_Type (T1))
2370 elsif T2 = Any_String and then Is_String_Type (T1) then
2373 elsif T1 = Any_String and then Is_String_Type (T2) then
2376 elsif T2 = Any_Character and then Is_Character_Type (T1) then
2379 elsif T1 = Any_Character and then Is_Character_Type (T2) then
2382 elsif T1 = Any_Access
2383 and then (Is_Access_Type (T2) or else Is_Remote_Access (T2))
2387 elsif T2 = Any_Access
2388 and then (Is_Access_Type (T1) or else Is_Remote_Access (T1))
2392 elsif T2 = Any_Composite
2393 and then Ekind (T1) in E_Array_Type .. E_Record_Subtype
2397 elsif T1 = Any_Composite
2398 and then Ekind (T2) in E_Array_Type .. E_Record_Subtype
2402 elsif T1 = Any_Modular and then Is_Modular_Integer_Type (T2) then
2405 elsif T2 = Any_Modular and then Is_Modular_Integer_Type (T1) then
2408 -- ----------------------------------------------------------
2409 -- Special cases for equality operators (all other predefined
2410 -- operators can never apply to tagged types)
2411 -- ----------------------------------------------------------
2413 -- Ada 2005 (AI-251): T1 and T2 are class-wide types, and T2 is an
2416 elsif Is_Class_Wide_Type (T1)
2417 and then Is_Class_Wide_Type (T2)
2418 and then Is_Interface (Etype (T2))
2422 -- Ada 2005 (AI-251): T1 is a concrete type that implements the
2423 -- class-wide interface T2
2425 elsif Is_Class_Wide_Type (T2)
2426 and then Is_Interface (Etype (T2))
2427 and then Interface_Present_In_Ancestor (Typ => T1,
2428 Iface => Etype (T2))
2432 elsif Is_Class_Wide_Type (T1)
2433 and then Is_Ancestor (Root_Type (T1), T2)
2437 elsif Is_Class_Wide_Type (T2)
2438 and then Is_Ancestor (Root_Type (T2), T1)
2442 elsif (Ekind (B1) = E_Access_Subprogram_Type
2444 Ekind (B1) = E_Access_Protected_Subprogram_Type)
2445 and then Ekind (Designated_Type (B1)) /= E_Subprogram_Type
2446 and then Is_Access_Type (T2)
2450 elsif (Ekind (B2) = E_Access_Subprogram_Type
2452 Ekind (B2) = E_Access_Protected_Subprogram_Type)
2453 and then Ekind (Designated_Type (B2)) /= E_Subprogram_Type
2454 and then Is_Access_Type (T1)
2458 elsif (Ekind (T1) = E_Allocator_Type
2459 or else Ekind (T1) = E_Access_Attribute_Type
2460 or else Ekind (T1) = E_Anonymous_Access_Type)
2461 and then Is_Access_Type (T2)
2465 elsif (Ekind (T2) = E_Allocator_Type
2466 or else Ekind (T2) = E_Access_Attribute_Type
2467 or else Ekind (T2) = E_Anonymous_Access_Type)
2468 and then Is_Access_Type (T1)
2472 -- If none of the above cases applies, types are not compatible
2479 -----------------------
2480 -- Valid_Boolean_Arg --
2481 -----------------------
2483 -- In addition to booleans and arrays of booleans, we must include
2484 -- aggregates as valid boolean arguments, because in the first pass of
2485 -- resolution their components are not examined. If it turns out not to be
2486 -- an aggregate of booleans, this will be diagnosed in Resolve.
2487 -- Any_Composite must be checked for prior to the array type checks because
2488 -- Any_Composite does not have any associated indexes.
2490 function Valid_Boolean_Arg (T : Entity_Id) return Boolean is
2492 return Is_Boolean_Type (T)
2493 or else T = Any_Composite
2494 or else (Is_Array_Type (T)
2495 and then T /= Any_String
2496 and then Number_Dimensions (T) = 1
2497 and then Is_Boolean_Type (Component_Type (T))
2498 and then (not Is_Private_Composite (T)
2499 or else In_Instance)
2500 and then (not Is_Limited_Composite (T)
2501 or else In_Instance))
2502 or else Is_Modular_Integer_Type (T)
2503 or else T = Universal_Integer;
2504 end Valid_Boolean_Arg;
2506 --------------------------
2507 -- Valid_Comparison_Arg --
2508 --------------------------
2510 function Valid_Comparison_Arg (T : Entity_Id) return Boolean is
2513 if T = Any_Composite then
2515 elsif Is_Discrete_Type (T)
2516 or else Is_Real_Type (T)
2519 elsif Is_Array_Type (T)
2520 and then Number_Dimensions (T) = 1
2521 and then Is_Discrete_Type (Component_Type (T))
2522 and then (not Is_Private_Composite (T)
2523 or else In_Instance)
2524 and then (not Is_Limited_Composite (T)
2525 or else In_Instance)
2528 elsif Is_String_Type (T) then
2533 end Valid_Comparison_Arg;
2535 ---------------------
2536 -- Write_Overloads --
2537 ---------------------
2539 procedure Write_Overloads (N : Node_Id) is
2545 if not Is_Overloaded (N) then
2546 Write_Str ("Non-overloaded entity ");
2548 Write_Entity_Info (Entity (N), " ");
2551 Get_First_Interp (N, I, It);
2552 Write_Str ("Overloaded entity ");
2556 while Present (Nam) loop
2557 Write_Entity_Info (Nam, " ");
2558 Write_Str ("=================");
2560 Get_Next_Interp (I, It);
2564 end Write_Overloads;
2566 ----------------------
2567 -- Write_Interp_Ref --
2568 ----------------------
2570 procedure Write_Interp_Ref (Map_Ptr : Int) is
2572 Write_Str (" Node: ");
2573 Write_Int (Int (Interp_Map.Table (Map_Ptr).Node));
2574 Write_Str (" Index: ");
2575 Write_Int (Int (Interp_Map.Table (Map_Ptr).Index));
2576 Write_Str (" Next: ");
2577 Write_Int (Int (Interp_Map.Table (Map_Ptr).Next));
2579 end Write_Interp_Ref;