1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Namet.Sp; use Namet.Sp;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch5; use Sem_Ch5;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Tbuild; use Tbuild;
65 package body Sem_Ch4 is
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Analyze_Concatenation_Rest (N : Node_Id);
72 -- Does the "rest" of the work of Analyze_Concatenation, after the left
73 -- operand has been analyzed. See Analyze_Concatenation for details.
75 procedure Analyze_Expression (N : Node_Id);
76 -- For expressions that are not names, this is just a call to analyze.
77 -- If the expression is a name, it may be a call to a parameterless
78 -- function, and if so must be converted into an explicit call node
79 -- and analyzed as such. This deproceduring must be done during the first
80 -- pass of overload resolution, because otherwise a procedure call with
81 -- overloaded actuals may fail to resolve.
83 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
84 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
85 -- is an operator name or an expanded name whose selector is an operator
86 -- name, and one possible interpretation is as a predefined operator.
88 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
89 -- If the prefix of a selected_component is overloaded, the proper
90 -- interpretation that yields a record type with the proper selector
91 -- name must be selected.
93 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
94 -- Procedure to analyze a user defined binary operator, which is resolved
95 -- like a function, but instead of a list of actuals it is presented
96 -- with the left and right operands of an operator node.
98 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
99 -- Procedure to analyze a user defined unary operator, which is resolved
100 -- like a function, but instead of a list of actuals, it is presented with
101 -- the operand of the operator node.
103 procedure Ambiguous_Operands (N : Node_Id);
104 -- For equality, membership, and comparison operators with overloaded
105 -- arguments, list possible interpretations.
107 procedure Analyze_One_Call
111 Success : out Boolean;
112 Skip_First : Boolean := False);
113 -- Check one interpretation of an overloaded subprogram name for
114 -- compatibility with the types of the actuals in a call. If there is a
115 -- single interpretation which does not match, post error if Report is
118 -- Nam is the entity that provides the formals against which the actuals
119 -- are checked. Nam is either the name of a subprogram, or the internal
120 -- subprogram type constructed for an access_to_subprogram. If the actuals
121 -- are compatible with Nam, then Nam is added to the list of candidate
122 -- interpretations for N, and Success is set to True.
124 -- The flag Skip_First is used when analyzing a call that was rewritten
125 -- from object notation. In this case the first actual may have to receive
126 -- an explicit dereference, depending on the first formal of the operation
127 -- being called. The caller will have verified that the object is legal
128 -- for the call. If the remaining parameters match, the first parameter
129 -- will rewritten as a dereference if needed, prior to completing analysis.
131 procedure Check_Misspelled_Selector
134 -- Give possible misspelling diagnostic if Sel is likely to be a mis-
135 -- spelling of one of the selectors of the Prefix. This is called by
136 -- Analyze_Selected_Component after producing an invalid selector error
139 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
140 -- Verify that type T is declared in scope S. Used to find interpretations
141 -- for operators given by expanded names. This is abstracted as a separate
142 -- function to handle extensions to System, where S is System, but T is
143 -- declared in the extension.
145 procedure Find_Arithmetic_Types
149 -- L and R are the operands of an arithmetic operator. Find
150 -- consistent pairs of interpretations for L and R that have a
151 -- numeric type consistent with the semantics of the operator.
153 procedure Find_Comparison_Types
157 -- L and R are operands of a comparison operator. Find consistent
158 -- pairs of interpretations for L and R.
160 procedure Find_Concatenation_Types
164 -- For the four varieties of concatenation
166 procedure Find_Equality_Types
170 -- Ditto for equality operators
172 procedure Find_Boolean_Types
176 -- Ditto for binary logical operations
178 procedure Find_Negation_Types
182 -- Find consistent interpretation for operand of negation operator
184 procedure Find_Non_Universal_Interpretations
189 -- For equality and comparison operators, the result is always boolean,
190 -- and the legality of the operation is determined from the visibility
191 -- of the operand types. If one of the operands has a universal interpre-
192 -- tation, the legality check uses some compatible non-universal
193 -- interpretation of the other operand. N can be an operator node, or
194 -- a function call whose name is an operator designator.
196 function Find_Primitive_Operation (N : Node_Id) return Boolean;
197 -- Find candidate interpretations for the name Obj.Proc when it appears
198 -- in a subprogram renaming declaration.
200 procedure Find_Unary_Types
204 -- Unary arithmetic types: plus, minus, abs
206 procedure Check_Arithmetic_Pair
210 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
211 -- types for left and right operand. Determine whether they constitute
212 -- a valid pair for the given operator, and record the corresponding
213 -- interpretation of the operator node. The node N may be an operator
214 -- node (the usual case) or a function call whose prefix is an operator
215 -- designator. In both cases Op_Id is the operator name itself.
217 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
218 -- Give detailed information on overloaded call where none of the
219 -- interpretations match. N is the call node, Nam the designator for
220 -- the overloaded entity being called.
222 function Junk_Operand (N : Node_Id) return Boolean;
223 -- Test for an operand that is an inappropriate entity (e.g. a package
224 -- name or a label). If so, issue an error message and return True. If
225 -- the operand is not an inappropriate entity kind, return False.
227 procedure Operator_Check (N : Node_Id);
228 -- Verify that an operator has received some valid interpretation. If none
229 -- was found, determine whether a use clause would make the operation
230 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
231 -- every type compatible with the operator, even if the operator for the
232 -- type is not directly visible. The routine uses this type to emit a more
233 -- informative message.
235 function Process_Implicit_Dereference_Prefix
237 P : Node_Id) return Entity_Id;
238 -- Called when P is the prefix of an implicit dereference, denoting an
239 -- object E. The function returns the designated type of the prefix, taking
240 -- into account that the designated type of an anonymous access type may be
241 -- a limited view, when the non-limited view is visible.
242 -- If in semantics only mode (-gnatc or generic), the function also records
243 -- that the prefix is a reference to E, if any. Normally, such a reference
244 -- is generated only when the implicit dereference is expanded into an
245 -- explicit one, but for consistency we must generate the reference when
246 -- expansion is disabled as well.
248 procedure Remove_Abstract_Operations (N : Node_Id);
249 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
250 -- operation is not a candidate interpretation.
252 function Try_Container_Indexing
255 Expr : Node_Id) return Boolean;
256 -- AI05-0139: Generalized indexing to support iterators over containers
258 function Try_Indexed_Call
262 Skip_First : Boolean) return Boolean;
263 -- If a function has defaults for all its actuals, a call to it may in fact
264 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
265 -- interpretation as an indexing, prior to analysis as a call. If both are
266 -- possible, the node is overloaded with both interpretations (same symbol
267 -- but two different types). If the call is written in prefix form, the
268 -- prefix becomes the first parameter in the call, and only the remaining
269 -- actuals must be checked for the presence of defaults.
271 function Try_Indirect_Call
274 Typ : Entity_Id) return Boolean;
275 -- Similarly, a function F that needs no actuals can return an access to a
276 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
277 -- the call may be overloaded with both interpretations.
279 function Try_Object_Operation (N : Node_Id) return Boolean;
280 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
281 -- is a call in this notation, it is transformed into a normal subprogram
282 -- call where the prefix is a parameter, and True is returned. If node
283 -- N is not of this form, it is unchanged, and False is returned.
285 procedure wpo (T : Entity_Id);
286 pragma Warnings (Off, wpo);
287 -- Used for debugging: obtain list of primitive operations even if
288 -- type is not frozen and dispatch table is not built yet.
290 ------------------------
291 -- Ambiguous_Operands --
292 ------------------------
294 procedure Ambiguous_Operands (N : Node_Id) is
295 procedure List_Operand_Interps (Opnd : Node_Id);
297 --------------------------
298 -- List_Operand_Interps --
299 --------------------------
301 procedure List_Operand_Interps (Opnd : Node_Id) is
306 if Is_Overloaded (Opnd) then
307 if Nkind (Opnd) in N_Op then
309 elsif Nkind (Opnd) = N_Function_Call then
311 elsif Ada_Version >= Ada_2012 then
317 Get_First_Interp (Opnd, I, It);
318 while Present (It.Nam) loop
319 if Has_Implicit_Dereference (It.Typ) then
321 ("can be interpreted as implicit dereference", Opnd);
325 Get_Next_Interp (I, It);
336 if Opnd = Left_Opnd (N) then
337 Error_Msg_N ("\left operand has the following interpretations", N);
340 ("\right operand has the following interpretations", N);
344 List_Interps (Nam, Err);
345 end List_Operand_Interps;
347 -- Start of processing for Ambiguous_Operands
350 if Nkind (N) in N_Membership_Test then
351 Error_Msg_N ("ambiguous operands for membership", N);
353 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
354 Error_Msg_N ("ambiguous operands for equality", N);
357 Error_Msg_N ("ambiguous operands for comparison", N);
360 if All_Errors_Mode then
361 List_Operand_Interps (Left_Opnd (N));
362 List_Operand_Interps (Right_Opnd (N));
364 Error_Msg_N ("\use -gnatf switch for details", N);
366 end Ambiguous_Operands;
368 -----------------------
369 -- Analyze_Aggregate --
370 -----------------------
372 -- Most of the analysis of Aggregates requires that the type be known,
373 -- and is therefore put off until resolution.
375 procedure Analyze_Aggregate (N : Node_Id) is
377 if No (Etype (N)) then
378 Set_Etype (N, Any_Composite);
380 end Analyze_Aggregate;
382 -----------------------
383 -- Analyze_Allocator --
384 -----------------------
386 procedure Analyze_Allocator (N : Node_Id) is
387 Loc : constant Source_Ptr := Sloc (N);
388 Sav_Errs : constant Nat := Serious_Errors_Detected;
389 E : Node_Id := Expression (N);
390 Acc_Type : Entity_Id;
396 Check_SPARK_Restriction ("allocator is not allowed", N);
398 -- Deal with allocator restrictions
400 -- In accordance with H.4(7), the No_Allocators restriction only applies
401 -- to user-written allocators. The same consideration applies to the
402 -- No_Allocators_Before_Elaboration restriction.
404 if Comes_From_Source (N) then
405 Check_Restriction (No_Allocators, N);
407 -- Processing for No_Allocators_After_Elaboration, loop to look at
408 -- enclosing context, checking task case and main subprogram case.
412 while Present (P) loop
414 -- In both cases we need a handled sequence of statements, where
415 -- the occurrence of the allocator is within the statements.
417 if Nkind (P) = N_Handled_Sequence_Of_Statements
418 and then Is_List_Member (C)
419 and then List_Containing (C) = Statements (P)
421 -- Check for allocator within task body, this is a definite
422 -- violation of No_Allocators_After_Elaboration we can detect.
424 if Nkind (Original_Node (Parent (P))) = N_Task_Body then
425 Check_Restriction (No_Allocators_After_Elaboration, N);
429 -- The other case is appearance in a subprogram body. This may
430 -- be a violation if this is a library level subprogram, and it
431 -- turns out to be used as the main program, but only the
432 -- binder knows that, so just record the occurrence.
434 if Nkind (Original_Node (Parent (P))) = N_Subprogram_Body
435 and then Nkind (Parent (Parent (P))) = N_Compilation_Unit
437 Set_Has_Allocator (Current_Sem_Unit);
446 -- Analyze the allocator
448 if Nkind (E) = N_Qualified_Expression then
449 Acc_Type := Create_Itype (E_Allocator_Type, N);
450 Set_Etype (Acc_Type, Acc_Type);
451 Find_Type (Subtype_Mark (E));
453 -- Analyze the qualified expression, and apply the name resolution
454 -- rule given in 4.7 (3).
457 Type_Id := Etype (E);
458 Set_Directly_Designated_Type (Acc_Type, Type_Id);
460 Resolve (Expression (E), Type_Id);
462 if Is_Limited_Type (Type_Id)
463 and then Comes_From_Source (N)
464 and then not In_Instance_Body
466 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
467 Error_Msg_N ("initialization not allowed for limited types", N);
468 Explain_Limited_Type (Type_Id, N);
472 -- A qualified expression requires an exact match of the type,
473 -- class-wide matching is not allowed.
475 -- if Is_Class_Wide_Type (Type_Id)
476 -- and then Base_Type
477 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
479 -- Wrong_Type (Expression (E), Type_Id);
482 Check_Non_Static_Context (Expression (E));
484 -- We don't analyze the qualified expression itself because it's
485 -- part of the allocator
487 Set_Etype (E, Type_Id);
489 -- Case where allocator has a subtype indication
494 Base_Typ : Entity_Id;
497 -- If the allocator includes a N_Subtype_Indication then a
498 -- constraint is present, otherwise the node is a subtype mark.
499 -- Introduce an explicit subtype declaration into the tree
500 -- defining some anonymous subtype and rewrite the allocator to
501 -- use this subtype rather than the subtype indication.
503 -- It is important to introduce the explicit subtype declaration
504 -- so that the bounds of the subtype indication are attached to
505 -- the tree in case the allocator is inside a generic unit.
507 if Nkind (E) = N_Subtype_Indication then
509 -- A constraint is only allowed for a composite type in Ada
510 -- 95. In Ada 83, a constraint is also allowed for an
511 -- access-to-composite type, but the constraint is ignored.
513 Find_Type (Subtype_Mark (E));
514 Base_Typ := Entity (Subtype_Mark (E));
516 if Is_Elementary_Type (Base_Typ) then
517 if not (Ada_Version = Ada_83
518 and then Is_Access_Type (Base_Typ))
520 Error_Msg_N ("constraint not allowed here", E);
522 if Nkind (Constraint (E)) =
523 N_Index_Or_Discriminant_Constraint
525 Error_Msg_N -- CODEFIX
526 ("\if qualified expression was meant, " &
527 "use apostrophe", Constraint (E));
531 -- Get rid of the bogus constraint:
533 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
534 Analyze_Allocator (N);
537 -- Ada 2005, AI-363: if the designated type has a constrained
538 -- partial view, it cannot receive a discriminant constraint,
539 -- and the allocated object is unconstrained.
541 elsif Ada_Version >= Ada_2005
542 and then Has_Constrained_Partial_View (Base_Typ)
545 ("constraint no allowed when type " &
546 "has a constrained partial view", Constraint (E));
549 if Expander_Active then
550 Def_Id := Make_Temporary (Loc, 'S');
553 Make_Subtype_Declaration (Loc,
554 Defining_Identifier => Def_Id,
555 Subtype_Indication => Relocate_Node (E)));
557 if Sav_Errs /= Serious_Errors_Detected
558 and then Nkind (Constraint (E)) =
559 N_Index_Or_Discriminant_Constraint
561 Error_Msg_N -- CODEFIX
562 ("if qualified expression was meant, " &
563 "use apostrophe!", Constraint (E));
566 E := New_Occurrence_Of (Def_Id, Loc);
567 Rewrite (Expression (N), E);
571 Type_Id := Process_Subtype (E, N);
572 Acc_Type := Create_Itype (E_Allocator_Type, N);
573 Set_Etype (Acc_Type, Acc_Type);
574 Set_Directly_Designated_Type (Acc_Type, Type_Id);
575 Check_Fully_Declared (Type_Id, N);
577 -- Ada 2005 (AI-231): If the designated type is itself an access
578 -- type that excludes null, its default initialization will
579 -- be a null object, and we can insert an unconditional raise
580 -- before the allocator.
582 -- Ada 2012 (AI-104): A not null indication here is altogether
585 if Can_Never_Be_Null (Type_Id) then
587 Not_Null_Check : constant Node_Id :=
588 Make_Raise_Constraint_Error (Sloc (E),
589 Reason => CE_Null_Not_Allowed);
592 if Ada_Version >= Ada_2012 then
594 ("an uninitialized allocator cannot have"
595 & " a null exclusion", N);
597 elsif Expander_Active then
598 Insert_Action (N, Not_Null_Check);
599 Analyze (Not_Null_Check);
602 Error_Msg_N ("null value not allowed here?", E);
607 -- Check restriction against dynamically allocated protected
608 -- objects. Note that when limited aggregates are supported,
609 -- a similar test should be applied to an allocator with a
610 -- qualified expression ???
612 if Is_Protected_Type (Type_Id) then
613 Check_Restriction (No_Protected_Type_Allocators, N);
616 -- Check for missing initialization. Skip this check if we already
617 -- had errors on analyzing the allocator, since in that case these
618 -- are probably cascaded errors.
620 if Is_Indefinite_Subtype (Type_Id)
621 and then Serious_Errors_Detected = Sav_Errs
623 if Is_Class_Wide_Type (Type_Id) then
625 ("initialization required in class-wide allocation", N);
627 if Ada_Version < Ada_2005
628 and then Is_Limited_Type (Type_Id)
630 Error_Msg_N ("unconstrained allocation not allowed", N);
632 if Is_Array_Type (Type_Id) then
634 ("\constraint with array bounds required", N);
636 elsif Has_Unknown_Discriminants (Type_Id) then
639 else pragma Assert (Has_Discriminants (Type_Id));
641 ("\constraint with discriminant values required", N);
644 -- Limited Ada 2005 and general non-limited case
648 ("uninitialized unconstrained allocation not allowed",
651 if Is_Array_Type (Type_Id) then
653 ("\qualified expression or constraint with " &
654 "array bounds required", N);
656 elsif Has_Unknown_Discriminants (Type_Id) then
657 Error_Msg_N ("\qualified expression required", N);
659 else pragma Assert (Has_Discriminants (Type_Id));
661 ("\qualified expression or constraint with " &
662 "discriminant values required", N);
670 if Is_Abstract_Type (Type_Id) then
671 Error_Msg_N ("cannot allocate abstract object", E);
674 if Has_Task (Designated_Type (Acc_Type)) then
675 Check_Restriction (No_Tasking, N);
676 Check_Restriction (Max_Tasks, N);
677 Check_Restriction (No_Task_Allocators, N);
680 -- Check that an allocator of a nested access type doesn't create a
681 -- protected object when restriction No_Local_Protected_Objects applies.
682 -- We don't have an equivalent to Has_Task for protected types, so only
683 -- cases where the designated type itself is a protected type are
684 -- currently checked. ???
686 if Is_Protected_Type (Designated_Type (Acc_Type))
687 and then not Is_Library_Level_Entity (Acc_Type)
689 Check_Restriction (No_Local_Protected_Objects, N);
692 -- If the No_Streams restriction is set, check that the type of the
693 -- object is not, and does not contain, any subtype derived from
694 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
695 -- Has_Stream just for efficiency reasons. There is no point in
696 -- spending time on a Has_Stream check if the restriction is not set.
698 if Restriction_Check_Required (No_Streams) then
699 if Has_Stream (Designated_Type (Acc_Type)) then
700 Check_Restriction (No_Streams, N);
704 Set_Etype (N, Acc_Type);
706 if not Is_Library_Level_Entity (Acc_Type) then
707 Check_Restriction (No_Local_Allocators, N);
710 if Serious_Errors_Detected > Sav_Errs then
711 Set_Error_Posted (N);
712 Set_Etype (N, Any_Type);
714 end Analyze_Allocator;
716 ---------------------------
717 -- Analyze_Arithmetic_Op --
718 ---------------------------
720 procedure Analyze_Arithmetic_Op (N : Node_Id) is
721 L : constant Node_Id := Left_Opnd (N);
722 R : constant Node_Id := Right_Opnd (N);
726 Candidate_Type := Empty;
727 Analyze_Expression (L);
728 Analyze_Expression (R);
730 -- If the entity is already set, the node is the instantiation of a
731 -- generic node with a non-local reference, or was manufactured by a
732 -- call to Make_Op_xxx. In either case the entity is known to be valid,
733 -- and we do not need to collect interpretations, instead we just get
734 -- the single possible interpretation.
738 if Present (Op_Id) then
739 if Ekind (Op_Id) = E_Operator then
741 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
742 and then Treat_Fixed_As_Integer (N)
746 Set_Etype (N, Any_Type);
747 Find_Arithmetic_Types (L, R, Op_Id, N);
751 Set_Etype (N, Any_Type);
752 Add_One_Interp (N, Op_Id, Etype (Op_Id));
755 -- Entity is not already set, so we do need to collect interpretations
758 Op_Id := Get_Name_Entity_Id (Chars (N));
759 Set_Etype (N, Any_Type);
761 while Present (Op_Id) loop
762 if Ekind (Op_Id) = E_Operator
763 and then Present (Next_Entity (First_Entity (Op_Id)))
765 Find_Arithmetic_Types (L, R, Op_Id, N);
767 -- The following may seem superfluous, because an operator cannot
768 -- be generic, but this ignores the cleverness of the author of
771 elsif Is_Overloadable (Op_Id) then
772 Analyze_User_Defined_Binary_Op (N, Op_Id);
775 Op_Id := Homonym (Op_Id);
780 end Analyze_Arithmetic_Op;
786 -- Function, procedure, and entry calls are checked here. The Name in
787 -- the call may be overloaded. The actuals have been analyzed and may
788 -- themselves be overloaded. On exit from this procedure, the node N
789 -- may have zero, one or more interpretations. In the first case an
790 -- error message is produced. In the last case, the node is flagged
791 -- as overloaded and the interpretations are collected in All_Interp.
793 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
794 -- the type-checking is similar to that of other calls.
796 procedure Analyze_Call (N : Node_Id) is
797 Actuals : constant List_Id := Parameter_Associations (N);
802 Success : Boolean := False;
804 Deref : Boolean := False;
805 -- Flag indicates whether an interpretation of the prefix is a
806 -- parameterless call that returns an access_to_subprogram.
808 procedure Check_Mixed_Parameter_And_Named_Associations;
809 -- Check that parameter and named associations are not mixed. This is
810 -- a restriction in SPARK mode.
812 function Name_Denotes_Function return Boolean;
813 -- If the type of the name is an access to subprogram, this may be the
814 -- type of a name, or the return type of the function being called. If
815 -- the name is not an entity then it can denote a protected function.
816 -- Until we distinguish Etype from Return_Type, we must use this routine
817 -- to resolve the meaning of the name in the call.
819 procedure No_Interpretation;
820 -- Output error message when no valid interpretation exists
822 --------------------------------------------------
823 -- Check_Mixed_Parameter_And_Named_Associations --
824 --------------------------------------------------
826 procedure Check_Mixed_Parameter_And_Named_Associations is
828 Named_Seen : Boolean;
833 Actual := First (Actuals);
834 while Present (Actual) loop
835 case Nkind (Actual) is
836 when N_Parameter_Association =>
838 Check_SPARK_Restriction
839 ("named association cannot follow positional one",
849 end Check_Mixed_Parameter_And_Named_Associations;
851 ---------------------------
852 -- Name_Denotes_Function --
853 ---------------------------
855 function Name_Denotes_Function return Boolean is
857 if Is_Entity_Name (Nam) then
858 return Ekind (Entity (Nam)) = E_Function;
860 elsif Nkind (Nam) = N_Selected_Component then
861 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
866 end Name_Denotes_Function;
868 -----------------------
869 -- No_Interpretation --
870 -----------------------
872 procedure No_Interpretation is
873 L : constant Boolean := Is_List_Member (N);
874 K : constant Node_Kind := Nkind (Parent (N));
877 -- If the node is in a list whose parent is not an expression then it
878 -- must be an attempted procedure call.
880 if L and then K not in N_Subexpr then
881 if Ekind (Entity (Nam)) = E_Generic_Procedure then
883 ("must instantiate generic procedure& before call",
887 ("procedure or entry name expected", Nam);
890 -- Check for tasking cases where only an entry call will do
893 and then Nkind_In (K, N_Entry_Call_Alternative,
894 N_Triggering_Alternative)
896 Error_Msg_N ("entry name expected", Nam);
898 -- Otherwise give general error message
901 Error_Msg_N ("invalid prefix in call", Nam);
903 end No_Interpretation;
905 -- Start of processing for Analyze_Call
908 if Restriction_Check_Required (SPARK) then
909 Check_Mixed_Parameter_And_Named_Associations;
912 -- Initialize the type of the result of the call to the error type,
913 -- which will be reset if the type is successfully resolved.
915 Set_Etype (N, Any_Type);
919 if not Is_Overloaded (Nam) then
921 -- Only one interpretation to check
923 if Ekind (Etype (Nam)) = E_Subprogram_Type then
924 Nam_Ent := Etype (Nam);
926 -- If the prefix is an access_to_subprogram, this may be an indirect
927 -- call. This is the case if the name in the call is not an entity
928 -- name, or if it is a function name in the context of a procedure
929 -- call. In this latter case, we have a call to a parameterless
930 -- function that returns a pointer_to_procedure which is the entity
931 -- being called. Finally, F (X) may be a call to a parameterless
932 -- function that returns a pointer to a function with parameters.
934 elsif Is_Access_Type (Etype (Nam))
935 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
937 (not Name_Denotes_Function
938 or else Nkind (N) = N_Procedure_Call_Statement
940 (Nkind (Parent (N)) /= N_Explicit_Dereference
941 and then Is_Entity_Name (Nam)
942 and then No (First_Formal (Entity (Nam)))
943 and then Present (Actuals)))
945 Nam_Ent := Designated_Type (Etype (Nam));
946 Insert_Explicit_Dereference (Nam);
948 -- Selected component case. Simple entry or protected operation,
949 -- where the entry name is given by the selector name.
951 elsif Nkind (Nam) = N_Selected_Component then
952 Nam_Ent := Entity (Selector_Name (Nam));
954 if not Ekind_In (Nam_Ent, E_Entry,
959 Error_Msg_N ("name in call is not a callable entity", Nam);
960 Set_Etype (N, Any_Type);
964 -- If the name is an Indexed component, it can be a call to a member
965 -- of an entry family. The prefix must be a selected component whose
966 -- selector is the entry. Analyze_Procedure_Call normalizes several
967 -- kinds of call into this form.
969 elsif Nkind (Nam) = N_Indexed_Component then
970 if Nkind (Prefix (Nam)) = N_Selected_Component then
971 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
973 Error_Msg_N ("name in call is not a callable entity", Nam);
974 Set_Etype (N, Any_Type);
978 elsif not Is_Entity_Name (Nam) then
979 Error_Msg_N ("name in call is not a callable entity", Nam);
980 Set_Etype (N, Any_Type);
984 Nam_Ent := Entity (Nam);
986 -- If no interpretations, give error message
988 if not Is_Overloadable (Nam_Ent) then
994 -- Operations generated for RACW stub types are called only through
995 -- dispatching, and can never be the static interpretation of a call.
997 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1002 Analyze_One_Call (N, Nam_Ent, True, Success);
1004 -- If this is an indirect call, the return type of the access_to
1005 -- subprogram may be an incomplete type. At the point of the call,
1006 -- use the full type if available, and at the same time update the
1007 -- return type of the access_to_subprogram.
1010 and then Nkind (Nam) = N_Explicit_Dereference
1011 and then Ekind (Etype (N)) = E_Incomplete_Type
1012 and then Present (Full_View (Etype (N)))
1014 Set_Etype (N, Full_View (Etype (N)));
1015 Set_Etype (Nam_Ent, Etype (N));
1019 -- An overloaded selected component must denote overloaded operations
1020 -- of a concurrent type. The interpretations are attached to the
1021 -- simple name of those operations.
1023 if Nkind (Nam) = N_Selected_Component then
1024 Nam := Selector_Name (Nam);
1027 Get_First_Interp (Nam, X, It);
1029 while Present (It.Nam) loop
1033 -- Name may be call that returns an access to subprogram, or more
1034 -- generally an overloaded expression one of whose interpretations
1035 -- yields an access to subprogram. If the name is an entity, we do
1036 -- not dereference, because the node is a call that returns the
1037 -- access type: note difference between f(x), where the call may
1038 -- return an access subprogram type, and f(x)(y), where the type
1039 -- returned by the call to f is implicitly dereferenced to analyze
1042 if Is_Access_Type (Nam_Ent) then
1043 Nam_Ent := Designated_Type (Nam_Ent);
1045 elsif Is_Access_Type (Etype (Nam_Ent))
1047 (not Is_Entity_Name (Nam)
1048 or else Nkind (N) = N_Procedure_Call_Statement)
1049 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1052 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1054 if Is_Entity_Name (Nam) then
1059 -- If the call has been rewritten from a prefixed call, the first
1060 -- parameter has been analyzed, but may need a subsequent
1061 -- dereference, so skip its analysis now.
1063 if N /= Original_Node (N)
1064 and then Nkind (Original_Node (N)) = Nkind (N)
1065 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1066 and then Present (Parameter_Associations (N))
1067 and then Present (Etype (First (Parameter_Associations (N))))
1070 (N, Nam_Ent, False, Success, Skip_First => True);
1072 Analyze_One_Call (N, Nam_Ent, False, Success);
1075 -- If the interpretation succeeds, mark the proper type of the
1076 -- prefix (any valid candidate will do). If not, remove the
1077 -- candidate interpretation. This only needs to be done for
1078 -- overloaded protected operations, for other entities disambi-
1079 -- guation is done directly in Resolve.
1083 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1085 Set_Entity (Nam, It.Nam);
1086 Insert_Explicit_Dereference (Nam);
1087 Set_Etype (Nam, Nam_Ent);
1090 Set_Etype (Nam, It.Typ);
1093 elsif Nkind_In (Name (N), N_Selected_Component,
1099 Get_Next_Interp (X, It);
1102 -- If the name is the result of a function call, it can only
1103 -- be a call to a function returning an access to subprogram.
1104 -- Insert explicit dereference.
1106 if Nkind (Nam) = N_Function_Call then
1107 Insert_Explicit_Dereference (Nam);
1110 if Etype (N) = Any_Type then
1112 -- None of the interpretations is compatible with the actuals
1114 Diagnose_Call (N, Nam);
1116 -- Special checks for uninstantiated put routines
1118 if Nkind (N) = N_Procedure_Call_Statement
1119 and then Is_Entity_Name (Nam)
1120 and then Chars (Nam) = Name_Put
1121 and then List_Length (Actuals) = 1
1124 Arg : constant Node_Id := First (Actuals);
1128 if Nkind (Arg) = N_Parameter_Association then
1129 Typ := Etype (Explicit_Actual_Parameter (Arg));
1134 if Is_Signed_Integer_Type (Typ) then
1136 ("possible missing instantiation of " &
1137 "'Text_'I'O.'Integer_'I'O!", Nam);
1139 elsif Is_Modular_Integer_Type (Typ) then
1141 ("possible missing instantiation of " &
1142 "'Text_'I'O.'Modular_'I'O!", Nam);
1144 elsif Is_Floating_Point_Type (Typ) then
1146 ("possible missing instantiation of " &
1147 "'Text_'I'O.'Float_'I'O!", Nam);
1149 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1151 ("possible missing instantiation of " &
1152 "'Text_'I'O.'Fixed_'I'O!", Nam);
1154 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1156 ("possible missing instantiation of " &
1157 "'Text_'I'O.'Decimal_'I'O!", Nam);
1159 elsif Is_Enumeration_Type (Typ) then
1161 ("possible missing instantiation of " &
1162 "'Text_'I'O.'Enumeration_'I'O!", Nam);
1167 elsif not Is_Overloaded (N)
1168 and then Is_Entity_Name (Nam)
1170 -- Resolution yields a single interpretation. Verify that the
1171 -- reference has capitalization consistent with the declaration.
1173 Set_Entity_With_Style_Check (Nam, Entity (Nam));
1174 Generate_Reference (Entity (Nam), Nam);
1176 Set_Etype (Nam, Etype (Entity (Nam)));
1178 Remove_Abstract_Operations (N);
1185 -----------------------------
1186 -- Analyze_Case_Expression --
1187 -----------------------------
1189 procedure Analyze_Case_Expression (N : Node_Id) is
1190 Expr : constant Node_Id := Expression (N);
1191 FirstX : constant Node_Id := Expression (First (Alternatives (N)));
1193 Exp_Type : Entity_Id;
1194 Exp_Btype : Entity_Id;
1196 Dont_Care : Boolean;
1197 Others_Present : Boolean;
1199 procedure Non_Static_Choice_Error (Choice : Node_Id);
1200 -- Error routine invoked by the generic instantiation below when
1201 -- the case expression has a non static choice.
1203 package Case_Choices_Processing is new
1204 Generic_Choices_Processing
1205 (Get_Alternatives => Alternatives,
1206 Get_Choices => Discrete_Choices,
1207 Process_Empty_Choice => No_OP,
1208 Process_Non_Static_Choice => Non_Static_Choice_Error,
1209 Process_Associated_Node => No_OP);
1210 use Case_Choices_Processing;
1212 -----------------------------
1213 -- Non_Static_Choice_Error --
1214 -----------------------------
1216 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1218 Flag_Non_Static_Expr
1219 ("choice given in case expression is not static!", Choice);
1220 end Non_Static_Choice_Error;
1222 -- Start of processing for Analyze_Case_Expression
1225 if Comes_From_Source (N) then
1226 Check_Compiler_Unit (N);
1229 Analyze_And_Resolve (Expr, Any_Discrete);
1230 Check_Unset_Reference (Expr);
1231 Exp_Type := Etype (Expr);
1232 Exp_Btype := Base_Type (Exp_Type);
1234 Alt := First (Alternatives (N));
1235 while Present (Alt) loop
1236 Analyze (Expression (Alt));
1240 if not Is_Overloaded (FirstX) then
1241 Set_Etype (N, Etype (FirstX));
1249 Set_Etype (N, Any_Type);
1251 Get_First_Interp (FirstX, I, It);
1252 while Present (It.Nam) loop
1254 -- For each interpretation of the first expression, we only
1255 -- add the interpretation if every other expression in the
1256 -- case expression alternatives has a compatible type.
1258 Alt := Next (First (Alternatives (N)));
1259 while Present (Alt) loop
1260 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1265 Add_One_Interp (N, It.Typ, It.Typ);
1268 Get_Next_Interp (I, It);
1273 Exp_Btype := Base_Type (Exp_Type);
1275 -- The expression must be of a discrete type which must be determinable
1276 -- independently of the context in which the expression occurs, but
1277 -- using the fact that the expression must be of a discrete type.
1278 -- Moreover, the type this expression must not be a character literal
1279 -- (which is always ambiguous).
1281 -- If error already reported by Resolve, nothing more to do
1283 if Exp_Btype = Any_Discrete
1284 or else Exp_Btype = Any_Type
1288 elsif Exp_Btype = Any_Character then
1290 ("character literal as case expression is ambiguous", Expr);
1294 -- If the case expression is a formal object of mode in out, then
1295 -- treat it as having a nonstatic subtype by forcing use of the base
1296 -- type (which has to get passed to Check_Case_Choices below). Also
1297 -- use base type when the case expression is parenthesized.
1299 if Paren_Count (Expr) > 0
1300 or else (Is_Entity_Name (Expr)
1301 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1303 Exp_Type := Exp_Btype;
1306 -- Call instantiated Analyze_Choices which does the rest of the work
1308 Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1310 if Exp_Type = Universal_Integer and then not Others_Present then
1312 ("case on universal integer requires OTHERS choice", Expr);
1314 end Analyze_Case_Expression;
1316 ---------------------------
1317 -- Analyze_Comparison_Op --
1318 ---------------------------
1320 procedure Analyze_Comparison_Op (N : Node_Id) is
1321 L : constant Node_Id := Left_Opnd (N);
1322 R : constant Node_Id := Right_Opnd (N);
1323 Op_Id : Entity_Id := Entity (N);
1326 Set_Etype (N, Any_Type);
1327 Candidate_Type := Empty;
1329 Analyze_Expression (L);
1330 Analyze_Expression (R);
1332 if Present (Op_Id) then
1333 if Ekind (Op_Id) = E_Operator then
1334 Find_Comparison_Types (L, R, Op_Id, N);
1336 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1339 if Is_Overloaded (L) then
1340 Set_Etype (L, Intersect_Types (L, R));
1344 Op_Id := Get_Name_Entity_Id (Chars (N));
1345 while Present (Op_Id) loop
1346 if Ekind (Op_Id) = E_Operator then
1347 Find_Comparison_Types (L, R, Op_Id, N);
1349 Analyze_User_Defined_Binary_Op (N, Op_Id);
1352 Op_Id := Homonym (Op_Id);
1357 end Analyze_Comparison_Op;
1359 ---------------------------
1360 -- Analyze_Concatenation --
1361 ---------------------------
1363 procedure Analyze_Concatenation (N : Node_Id) is
1365 -- We wish to avoid deep recursion, because concatenations are often
1366 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1367 -- operands nonrecursively until we find something that is not a
1368 -- concatenation (A in this case), or has already been analyzed. We
1369 -- analyze that, and then walk back up the tree following Parent
1370 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1371 -- work at each level. The Parent pointers allow us to avoid recursion,
1372 -- and thus avoid running out of memory.
1378 Candidate_Type := Empty;
1380 -- The following code is equivalent to:
1382 -- Set_Etype (N, Any_Type);
1383 -- Analyze_Expression (Left_Opnd (N));
1384 -- Analyze_Concatenation_Rest (N);
1386 -- where the Analyze_Expression call recurses back here if the left
1387 -- operand is a concatenation.
1389 -- Walk down left operands
1392 Set_Etype (NN, Any_Type);
1393 L := Left_Opnd (NN);
1394 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1398 -- Now (given the above example) NN is A&B and L is A
1400 -- First analyze L ...
1402 Analyze_Expression (L);
1404 -- ... then walk NN back up until we reach N (where we started), calling
1405 -- Analyze_Concatenation_Rest along the way.
1408 Analyze_Concatenation_Rest (NN);
1412 end Analyze_Concatenation;
1414 --------------------------------
1415 -- Analyze_Concatenation_Rest --
1416 --------------------------------
1418 -- If the only one-dimensional array type in scope is String,
1419 -- this is the resulting type of the operation. Otherwise there
1420 -- will be a concatenation operation defined for each user-defined
1421 -- one-dimensional array.
1423 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1424 L : constant Node_Id := Left_Opnd (N);
1425 R : constant Node_Id := Right_Opnd (N);
1426 Op_Id : Entity_Id := Entity (N);
1431 Analyze_Expression (R);
1433 -- If the entity is present, the node appears in an instance, and
1434 -- denotes a predefined concatenation operation. The resulting type is
1435 -- obtained from the arguments when possible. If the arguments are
1436 -- aggregates, the array type and the concatenation type must be
1439 if Present (Op_Id) then
1440 if Ekind (Op_Id) = E_Operator then
1441 LT := Base_Type (Etype (L));
1442 RT := Base_Type (Etype (R));
1444 if Is_Array_Type (LT)
1445 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1447 Add_One_Interp (N, Op_Id, LT);
1449 elsif Is_Array_Type (RT)
1450 and then LT = Base_Type (Component_Type (RT))
1452 Add_One_Interp (N, Op_Id, RT);
1454 -- If one operand is a string type or a user-defined array type,
1455 -- and the other is a literal, result is of the specific type.
1458 (Root_Type (LT) = Standard_String
1459 or else Scope (LT) /= Standard_Standard)
1460 and then Etype (R) = Any_String
1462 Add_One_Interp (N, Op_Id, LT);
1465 (Root_Type (RT) = Standard_String
1466 or else Scope (RT) /= Standard_Standard)
1467 and then Etype (L) = Any_String
1469 Add_One_Interp (N, Op_Id, RT);
1471 elsif not Is_Generic_Type (Etype (Op_Id)) then
1472 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1475 -- Type and its operations must be visible
1477 Set_Entity (N, Empty);
1478 Analyze_Concatenation (N);
1482 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1486 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1487 while Present (Op_Id) loop
1488 if Ekind (Op_Id) = E_Operator then
1490 -- Do not consider operators declared in dead code, they can
1491 -- not be part of the resolution.
1493 if Is_Eliminated (Op_Id) then
1496 Find_Concatenation_Types (L, R, Op_Id, N);
1500 Analyze_User_Defined_Binary_Op (N, Op_Id);
1503 Op_Id := Homonym (Op_Id);
1508 end Analyze_Concatenation_Rest;
1510 ------------------------------------
1511 -- Analyze_Conditional_Expression --
1512 ------------------------------------
1514 procedure Analyze_Conditional_Expression (N : Node_Id) is
1515 Condition : constant Node_Id := First (Expressions (N));
1516 Then_Expr : constant Node_Id := Next (Condition);
1517 Else_Expr : Node_Id;
1520 -- Defend against error of missing expressions from previous error
1522 if No (Then_Expr) then
1526 Check_SPARK_Restriction ("conditional expression is not allowed", N);
1528 Else_Expr := Next (Then_Expr);
1530 if Comes_From_Source (N) then
1531 Check_Compiler_Unit (N);
1534 Analyze_Expression (Condition);
1535 Analyze_Expression (Then_Expr);
1537 if Present (Else_Expr) then
1538 Analyze_Expression (Else_Expr);
1541 -- If then expression not overloaded, then that decides the type
1543 if not Is_Overloaded (Then_Expr) then
1544 Set_Etype (N, Etype (Then_Expr));
1546 -- Case where then expression is overloaded
1554 Set_Etype (N, Any_Type);
1556 -- Shouldn't the following statement be down in the ELSE of the
1557 -- following loop? ???
1559 Get_First_Interp (Then_Expr, I, It);
1561 -- if no Else_Expression the conditional must be boolean
1563 if No (Else_Expr) then
1564 Set_Etype (N, Standard_Boolean);
1566 -- Else_Expression Present. For each possible intepretation of
1567 -- the Then_Expression, add it only if the Else_Expression has
1568 -- a compatible type.
1571 while Present (It.Nam) loop
1572 if Has_Compatible_Type (Else_Expr, It.Typ) then
1573 Add_One_Interp (N, It.Typ, It.Typ);
1576 Get_Next_Interp (I, It);
1581 end Analyze_Conditional_Expression;
1583 -------------------------
1584 -- Analyze_Equality_Op --
1585 -------------------------
1587 procedure Analyze_Equality_Op (N : Node_Id) is
1588 Loc : constant Source_Ptr := Sloc (N);
1589 L : constant Node_Id := Left_Opnd (N);
1590 R : constant Node_Id := Right_Opnd (N);
1594 Set_Etype (N, Any_Type);
1595 Candidate_Type := Empty;
1597 Analyze_Expression (L);
1598 Analyze_Expression (R);
1600 -- If the entity is set, the node is a generic instance with a non-local
1601 -- reference to the predefined operator or to a user-defined function.
1602 -- It can also be an inequality that is expanded into the negation of a
1603 -- call to a user-defined equality operator.
1605 -- For the predefined case, the result is Boolean, regardless of the
1606 -- type of the operands. The operands may even be limited, if they are
1607 -- generic actuals. If they are overloaded, label the left argument with
1608 -- the common type that must be present, or with the type of the formal
1609 -- of the user-defined function.
1611 if Present (Entity (N)) then
1612 Op_Id := Entity (N);
1614 if Ekind (Op_Id) = E_Operator then
1615 Add_One_Interp (N, Op_Id, Standard_Boolean);
1617 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1620 if Is_Overloaded (L) then
1621 if Ekind (Op_Id) = E_Operator then
1622 Set_Etype (L, Intersect_Types (L, R));
1624 Set_Etype (L, Etype (First_Formal (Op_Id)));
1629 Op_Id := Get_Name_Entity_Id (Chars (N));
1630 while Present (Op_Id) loop
1631 if Ekind (Op_Id) = E_Operator then
1632 Find_Equality_Types (L, R, Op_Id, N);
1634 Analyze_User_Defined_Binary_Op (N, Op_Id);
1637 Op_Id := Homonym (Op_Id);
1641 -- If there was no match, and the operator is inequality, this may
1642 -- be a case where inequality has not been made explicit, as for
1643 -- tagged types. Analyze the node as the negation of an equality
1644 -- operation. This cannot be done earlier, because before analysis
1645 -- we cannot rule out the presence of an explicit inequality.
1647 if Etype (N) = Any_Type
1648 and then Nkind (N) = N_Op_Ne
1650 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1651 while Present (Op_Id) loop
1652 if Ekind (Op_Id) = E_Operator then
1653 Find_Equality_Types (L, R, Op_Id, N);
1655 Analyze_User_Defined_Binary_Op (N, Op_Id);
1658 Op_Id := Homonym (Op_Id);
1661 if Etype (N) /= Any_Type then
1662 Op_Id := Entity (N);
1668 Left_Opnd => Left_Opnd (N),
1669 Right_Opnd => Right_Opnd (N))));
1671 Set_Entity (Right_Opnd (N), Op_Id);
1677 end Analyze_Equality_Op;
1679 ----------------------------------
1680 -- Analyze_Explicit_Dereference --
1681 ----------------------------------
1683 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1684 Loc : constant Source_Ptr := Sloc (N);
1685 P : constant Node_Id := Prefix (N);
1691 function Is_Function_Type return Boolean;
1692 -- Check whether node may be interpreted as an implicit function call
1694 ----------------------
1695 -- Is_Function_Type --
1696 ----------------------
1698 function Is_Function_Type return Boolean is
1703 if not Is_Overloaded (N) then
1704 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1705 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1708 Get_First_Interp (N, I, It);
1709 while Present (It.Nam) loop
1710 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1711 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1716 Get_Next_Interp (I, It);
1721 end Is_Function_Type;
1723 -- Start of processing for Analyze_Explicit_Dereference
1726 Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1729 Set_Etype (N, Any_Type);
1731 -- Test for remote access to subprogram type, and if so return
1732 -- after rewriting the original tree.
1734 if Remote_AST_E_Dereference (P) then
1738 -- Normal processing for other than remote access to subprogram type
1740 if not Is_Overloaded (P) then
1741 if Is_Access_Type (Etype (P)) then
1743 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1744 -- avoid other problems caused by the Private_Subtype and it is
1745 -- safe to go to the Base_Type because this is the same as
1746 -- converting the access value to its Base_Type.
1749 DT : Entity_Id := Designated_Type (Etype (P));
1752 if Ekind (DT) = E_Private_Subtype
1753 and then Is_For_Access_Subtype (DT)
1755 DT := Base_Type (DT);
1758 -- An explicit dereference is a legal occurrence of an
1759 -- incomplete type imported through a limited_with clause,
1760 -- if the full view is visible.
1762 if From_With_Type (DT)
1763 and then not From_With_Type (Scope (DT))
1765 (Is_Immediately_Visible (Scope (DT))
1767 (Is_Child_Unit (Scope (DT))
1768 and then Is_Visible_Child_Unit (Scope (DT))))
1770 Set_Etype (N, Available_View (DT));
1777 elsif Etype (P) /= Any_Type then
1778 Error_Msg_N ("prefix of dereference must be an access type", N);
1783 Get_First_Interp (P, I, It);
1784 while Present (It.Nam) loop
1787 if Is_Access_Type (T) then
1788 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1791 Get_Next_Interp (I, It);
1794 -- Error if no interpretation of the prefix has an access type
1796 if Etype (N) = Any_Type then
1798 ("access type required in prefix of explicit dereference", P);
1799 Set_Etype (N, Any_Type);
1805 and then Nkind (Parent (N)) /= N_Indexed_Component
1807 and then (Nkind (Parent (N)) /= N_Function_Call
1808 or else N /= Name (Parent (N)))
1810 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1811 or else N /= Name (Parent (N)))
1813 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1814 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1816 (Attribute_Name (Parent (N)) /= Name_Address
1818 Attribute_Name (Parent (N)) /= Name_Access))
1820 -- Name is a function call with no actuals, in a context that
1821 -- requires deproceduring (including as an actual in an enclosing
1822 -- function or procedure call). There are some pathological cases
1823 -- where the prefix might include functions that return access to
1824 -- subprograms and others that return a regular type. Disambiguation
1825 -- of those has to take place in Resolve.
1828 Make_Function_Call (Loc,
1829 Name => Make_Explicit_Dereference (Loc, P),
1830 Parameter_Associations => New_List);
1832 -- If the prefix is overloaded, remove operations that have formals,
1833 -- we know that this is a parameterless call.
1835 if Is_Overloaded (P) then
1836 Get_First_Interp (P, I, It);
1837 while Present (It.Nam) loop
1840 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1846 Get_Next_Interp (I, It);
1853 elsif not Is_Function_Type
1854 and then Is_Overloaded (N)
1856 -- The prefix may include access to subprograms and other access
1857 -- types. If the context selects the interpretation that is a
1858 -- function call (not a procedure call) we cannot rewrite the node
1859 -- yet, but we include the result of the call interpretation.
1861 Get_First_Interp (N, I, It);
1862 while Present (It.Nam) loop
1863 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1864 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1865 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1867 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1870 Get_Next_Interp (I, It);
1874 -- A value of remote access-to-class-wide must not be dereferenced
1877 Validate_Remote_Access_To_Class_Wide_Type (N);
1878 end Analyze_Explicit_Dereference;
1880 ------------------------
1881 -- Analyze_Expression --
1882 ------------------------
1884 procedure Analyze_Expression (N : Node_Id) is
1887 Check_Parameterless_Call (N);
1888 end Analyze_Expression;
1890 -------------------------------------
1891 -- Analyze_Expression_With_Actions --
1892 -------------------------------------
1894 procedure Analyze_Expression_With_Actions (N : Node_Id) is
1898 A := First (Actions (N));
1905 Analyze_Expression (Expression (N));
1906 Set_Etype (N, Etype (Expression (N)));
1907 end Analyze_Expression_With_Actions;
1909 ------------------------------------
1910 -- Analyze_Indexed_Component_Form --
1911 ------------------------------------
1913 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1914 P : constant Node_Id := Prefix (N);
1915 Exprs : constant List_Id := Expressions (N);
1921 procedure Process_Function_Call;
1922 -- Prefix in indexed component form is an overloadable entity,
1923 -- so the node is a function call. Reformat it as such.
1925 procedure Process_Indexed_Component;
1926 -- Prefix in indexed component form is actually an indexed component.
1927 -- This routine processes it, knowing that the prefix is already
1930 procedure Process_Indexed_Component_Or_Slice;
1931 -- An indexed component with a single index may designate a slice if
1932 -- the index is a subtype mark. This routine disambiguates these two
1933 -- cases by resolving the prefix to see if it is a subtype mark.
1935 procedure Process_Overloaded_Indexed_Component;
1936 -- If the prefix of an indexed component is overloaded, the proper
1937 -- interpretation is selected by the index types and the context.
1939 ---------------------------
1940 -- Process_Function_Call --
1941 ---------------------------
1943 procedure Process_Function_Call is
1947 Change_Node (N, N_Function_Call);
1949 Set_Parameter_Associations (N, Exprs);
1951 -- Analyze actuals prior to analyzing the call itself
1953 Actual := First (Parameter_Associations (N));
1954 while Present (Actual) loop
1956 Check_Parameterless_Call (Actual);
1958 -- Move to next actual. Note that we use Next, not Next_Actual
1959 -- here. The reason for this is a bit subtle. If a function call
1960 -- includes named associations, the parser recognizes the node as
1961 -- a call, and it is analyzed as such. If all associations are
1962 -- positional, the parser builds an indexed_component node, and
1963 -- it is only after analysis of the prefix that the construct
1964 -- is recognized as a call, in which case Process_Function_Call
1965 -- rewrites the node and analyzes the actuals. If the list of
1966 -- actuals is malformed, the parser may leave the node as an
1967 -- indexed component (despite the presence of named associations).
1968 -- The iterator Next_Actual is equivalent to Next if the list is
1969 -- positional, but follows the normalized chain of actuals when
1970 -- named associations are present. In this case normalization has
1971 -- not taken place, and actuals remain unanalyzed, which leads to
1972 -- subsequent crashes or loops if there is an attempt to continue
1973 -- analysis of the program.
1979 end Process_Function_Call;
1981 -------------------------------
1982 -- Process_Indexed_Component --
1983 -------------------------------
1985 procedure Process_Indexed_Component is
1987 Array_Type : Entity_Id;
1989 Pent : Entity_Id := Empty;
1992 Exp := First (Exprs);
1994 if Is_Overloaded (P) then
1995 Process_Overloaded_Indexed_Component;
1998 Array_Type := Etype (P);
2000 if Is_Entity_Name (P) then
2002 elsif Nkind (P) = N_Selected_Component
2003 and then Is_Entity_Name (Selector_Name (P))
2005 Pent := Entity (Selector_Name (P));
2008 -- Prefix must be appropriate for an array type, taking into
2009 -- account a possible implicit dereference.
2011 if Is_Access_Type (Array_Type) then
2012 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2013 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2016 if Is_Array_Type (Array_Type) then
2019 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2021 Set_Etype (N, Any_Type);
2023 if not Has_Compatible_Type
2024 (Exp, Entry_Index_Type (Pent))
2026 Error_Msg_N ("invalid index type in entry name", N);
2028 elsif Present (Next (Exp)) then
2029 Error_Msg_N ("too many subscripts in entry reference", N);
2032 Set_Etype (N, Etype (P));
2037 elsif Is_Record_Type (Array_Type)
2038 and then Remote_AST_I_Dereference (P)
2042 elsif Try_Container_Indexing (N, P, Exp) then
2045 elsif Array_Type = Any_Type then
2046 Set_Etype (N, Any_Type);
2048 -- In most cases the analysis of the prefix will have emitted
2049 -- an error already, but if the prefix may be interpreted as a
2050 -- call in prefixed notation, the report is left to the caller.
2051 -- To prevent cascaded errors, report only if no previous ones.
2053 if Serious_Errors_Detected = 0 then
2054 Error_Msg_N ("invalid prefix in indexed component", P);
2056 if Nkind (P) = N_Expanded_Name then
2057 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2063 -- Here we definitely have a bad indexing
2066 if Nkind (Parent (N)) = N_Requeue_Statement
2067 and then Present (Pent) and then Ekind (Pent) = E_Entry
2070 ("REQUEUE does not permit parameters", First (Exprs));
2072 elsif Is_Entity_Name (P)
2073 and then Etype (P) = Standard_Void_Type
2075 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2078 Error_Msg_N ("array type required in indexed component", P);
2081 Set_Etype (N, Any_Type);
2085 Index := First_Index (Array_Type);
2086 while Present (Index) and then Present (Exp) loop
2087 if not Has_Compatible_Type (Exp, Etype (Index)) then
2088 Wrong_Type (Exp, Etype (Index));
2089 Set_Etype (N, Any_Type);
2097 Set_Etype (N, Component_Type (Array_Type));
2098 Check_Implicit_Dereference (N, Etype (N));
2100 if Present (Index) then
2102 ("too few subscripts in array reference", First (Exprs));
2104 elsif Present (Exp) then
2105 Error_Msg_N ("too many subscripts in array reference", Exp);
2108 end Process_Indexed_Component;
2110 ----------------------------------------
2111 -- Process_Indexed_Component_Or_Slice --
2112 ----------------------------------------
2114 procedure Process_Indexed_Component_Or_Slice is
2116 Exp := First (Exprs);
2117 while Present (Exp) loop
2118 Analyze_Expression (Exp);
2122 Exp := First (Exprs);
2124 -- If one index is present, and it is a subtype name, then the
2125 -- node denotes a slice (note that the case of an explicit range
2126 -- for a slice was already built as an N_Slice node in the first
2127 -- place, so that case is not handled here).
2129 -- We use a replace rather than a rewrite here because this is one
2130 -- of the cases in which the tree built by the parser is plain wrong.
2133 and then Is_Entity_Name (Exp)
2134 and then Is_Type (Entity (Exp))
2137 Make_Slice (Sloc (N),
2139 Discrete_Range => New_Copy (Exp)));
2142 -- Otherwise (more than one index present, or single index is not
2143 -- a subtype name), then we have the indexed component case.
2146 Process_Indexed_Component;
2148 end Process_Indexed_Component_Or_Slice;
2150 ------------------------------------------
2151 -- Process_Overloaded_Indexed_Component --
2152 ------------------------------------------
2154 procedure Process_Overloaded_Indexed_Component is
2163 Set_Etype (N, Any_Type);
2165 Get_First_Interp (P, I, It);
2166 while Present (It.Nam) loop
2169 if Is_Access_Type (Typ) then
2170 Typ := Designated_Type (Typ);
2171 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2174 if Is_Array_Type (Typ) then
2176 -- Got a candidate: verify that index types are compatible
2178 Index := First_Index (Typ);
2180 Exp := First (Exprs);
2181 while Present (Index) and then Present (Exp) loop
2182 if Has_Compatible_Type (Exp, Etype (Index)) then
2194 if Found and then No (Index) and then No (Exp) then
2196 CT : constant Entity_Id :=
2197 Base_Type (Component_Type (Typ));
2199 Add_One_Interp (N, CT, CT);
2200 Check_Implicit_Dereference (N, CT);
2205 Get_Next_Interp (I, It);
2208 if Etype (N) = Any_Type then
2209 Error_Msg_N ("no legal interpretation for indexed component", N);
2210 Set_Is_Overloaded (N, False);
2214 end Process_Overloaded_Indexed_Component;
2216 -- Start of processing for Analyze_Indexed_Component_Form
2219 -- Get name of array, function or type
2223 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) then
2225 -- If P is an explicit dereference whose prefix is of a
2226 -- remote access-to-subprogram type, then N has already
2227 -- been rewritten as a subprogram call and analyzed.
2232 pragma Assert (Nkind (N) = N_Indexed_Component);
2234 P_T := Base_Type (Etype (P));
2236 if Is_Entity_Name (P) and then Present (Entity (P)) then
2239 if Is_Type (U_N) then
2241 -- Reformat node as a type conversion
2243 E := Remove_Head (Exprs);
2245 if Present (First (Exprs)) then
2247 ("argument of type conversion must be single expression", N);
2250 Change_Node (N, N_Type_Conversion);
2251 Set_Subtype_Mark (N, P);
2253 Set_Expression (N, E);
2255 -- After changing the node, call for the specific Analysis
2256 -- routine directly, to avoid a double call to the expander.
2258 Analyze_Type_Conversion (N);
2262 if Is_Overloadable (U_N) then
2263 Process_Function_Call;
2265 elsif Ekind (Etype (P)) = E_Subprogram_Type
2266 or else (Is_Access_Type (Etype (P))
2268 Ekind (Designated_Type (Etype (P))) =
2271 -- Call to access_to-subprogram with possible implicit dereference
2273 Process_Function_Call;
2275 elsif Is_Generic_Subprogram (U_N) then
2277 -- A common beginner's (or C++ templates fan) error
2279 Error_Msg_N ("generic subprogram cannot be called", N);
2280 Set_Etype (N, Any_Type);
2284 Process_Indexed_Component_Or_Slice;
2287 -- If not an entity name, prefix is an expression that may denote
2288 -- an array or an access-to-subprogram.
2291 if Ekind (P_T) = E_Subprogram_Type
2292 or else (Is_Access_Type (P_T)
2294 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2296 Process_Function_Call;
2298 elsif Nkind (P) = N_Selected_Component
2299 and then Is_Overloadable (Entity (Selector_Name (P)))
2301 Process_Function_Call;
2304 -- Indexed component, slice, or a call to a member of a family
2305 -- entry, which will be converted to an entry call later.
2307 Process_Indexed_Component_Or_Slice;
2310 end Analyze_Indexed_Component_Form;
2312 ------------------------
2313 -- Analyze_Logical_Op --
2314 ------------------------
2316 procedure Analyze_Logical_Op (N : Node_Id) is
2317 L : constant Node_Id := Left_Opnd (N);
2318 R : constant Node_Id := Right_Opnd (N);
2319 Op_Id : Entity_Id := Entity (N);
2322 Set_Etype (N, Any_Type);
2323 Candidate_Type := Empty;
2325 Analyze_Expression (L);
2326 Analyze_Expression (R);
2328 if Present (Op_Id) then
2330 if Ekind (Op_Id) = E_Operator then
2331 Find_Boolean_Types (L, R, Op_Id, N);
2333 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2337 Op_Id := Get_Name_Entity_Id (Chars (N));
2338 while Present (Op_Id) loop
2339 if Ekind (Op_Id) = E_Operator then
2340 Find_Boolean_Types (L, R, Op_Id, N);
2342 Analyze_User_Defined_Binary_Op (N, Op_Id);
2345 Op_Id := Homonym (Op_Id);
2350 end Analyze_Logical_Op;
2352 ---------------------------
2353 -- Analyze_Membership_Op --
2354 ---------------------------
2356 procedure Analyze_Membership_Op (N : Node_Id) is
2357 Loc : constant Source_Ptr := Sloc (N);
2358 L : constant Node_Id := Left_Opnd (N);
2359 R : constant Node_Id := Right_Opnd (N);
2361 Index : Interp_Index;
2363 Found : Boolean := False;
2367 procedure Try_One_Interp (T1 : Entity_Id);
2368 -- Routine to try one proposed interpretation. Note that the context
2369 -- of the operation plays no role in resolving the arguments, so that
2370 -- if there is more than one interpretation of the operands that is
2371 -- compatible with a membership test, the operation is ambiguous.
2373 --------------------
2374 -- Try_One_Interp --
2375 --------------------
2377 procedure Try_One_Interp (T1 : Entity_Id) is
2379 if Has_Compatible_Type (R, T1) then
2381 and then Base_Type (T1) /= Base_Type (T_F)
2383 It := Disambiguate (L, I_F, Index, Any_Type);
2385 if It = No_Interp then
2386 Ambiguous_Operands (N);
2387 Set_Etype (L, Any_Type);
2404 procedure Analyze_Set_Membership;
2405 -- If a set of alternatives is present, analyze each and find the
2406 -- common type to which they must all resolve.
2408 ----------------------------
2409 -- Analyze_Set_Membership --
2410 ----------------------------
2412 procedure Analyze_Set_Membership is
2414 Index : Interp_Index;
2416 Candidate_Interps : Node_Id;
2417 Common_Type : Entity_Id := Empty;
2421 Candidate_Interps := L;
2423 if not Is_Overloaded (L) then
2424 Common_Type := Etype (L);
2426 Alt := First (Alternatives (N));
2427 while Present (Alt) loop
2430 if not Has_Compatible_Type (Alt, Common_Type) then
2431 Wrong_Type (Alt, Common_Type);
2438 Alt := First (Alternatives (N));
2439 while Present (Alt) loop
2441 if not Is_Overloaded (Alt) then
2442 Common_Type := Etype (Alt);
2445 Get_First_Interp (Alt, Index, It);
2446 while Present (It.Typ) loop
2448 Has_Compatible_Type (Candidate_Interps, It.Typ)
2450 Remove_Interp (Index);
2453 Get_Next_Interp (Index, It);
2456 Get_First_Interp (Alt, Index, It);
2459 Error_Msg_N ("alternative has no legal type", Alt);
2463 -- If alternative is not overloaded, we have a unique type
2466 Set_Etype (Alt, It.Typ);
2467 Get_Next_Interp (Index, It);
2470 Set_Is_Overloaded (Alt, False);
2471 Common_Type := Etype (Alt);
2474 Candidate_Interps := Alt;
2481 Set_Etype (N, Standard_Boolean);
2483 if Present (Common_Type) then
2484 Set_Etype (L, Common_Type);
2485 Set_Is_Overloaded (L, False);
2488 Error_Msg_N ("cannot resolve membership operation", N);
2490 end Analyze_Set_Membership;
2492 -- Start of processing for Analyze_Membership_Op
2495 Analyze_Expression (L);
2498 and then Ada_Version >= Ada_2012
2500 Analyze_Set_Membership;
2504 if Nkind (R) = N_Range
2505 or else (Nkind (R) = N_Attribute_Reference
2506 and then Attribute_Name (R) = Name_Range)
2510 if not Is_Overloaded (L) then
2511 Try_One_Interp (Etype (L));
2514 Get_First_Interp (L, Index, It);
2515 while Present (It.Typ) loop
2516 Try_One_Interp (It.Typ);
2517 Get_Next_Interp (Index, It);
2521 -- If not a range, it can be a subtype mark, or else it is a degenerate
2522 -- membership test with a singleton value, i.e. a test for equality,
2523 -- if the types are compatible.
2528 if Is_Entity_Name (R)
2529 and then Is_Type (Entity (R))
2532 Check_Fully_Declared (Entity (R), R);
2534 elsif Ada_Version >= Ada_2012
2535 and then Has_Compatible_Type (R, Etype (L))
2537 if Nkind (N) = N_In then
2553 -- In all versions of the language, if we reach this point there
2554 -- is a previous error that will be diagnosed below.
2560 -- Compatibility between expression and subtype mark or range is
2561 -- checked during resolution. The result of the operation is Boolean
2564 Set_Etype (N, Standard_Boolean);
2566 if Comes_From_Source (N)
2567 and then Present (Right_Opnd (N))
2568 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2570 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2572 end Analyze_Membership_Op;
2574 ----------------------
2575 -- Analyze_Negation --
2576 ----------------------
2578 procedure Analyze_Negation (N : Node_Id) is
2579 R : constant Node_Id := Right_Opnd (N);
2580 Op_Id : Entity_Id := Entity (N);
2583 Set_Etype (N, Any_Type);
2584 Candidate_Type := Empty;
2586 Analyze_Expression (R);
2588 if Present (Op_Id) then
2589 if Ekind (Op_Id) = E_Operator then
2590 Find_Negation_Types (R, Op_Id, N);
2592 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2596 Op_Id := Get_Name_Entity_Id (Chars (N));
2597 while Present (Op_Id) loop
2598 if Ekind (Op_Id) = E_Operator then
2599 Find_Negation_Types (R, Op_Id, N);
2601 Analyze_User_Defined_Unary_Op (N, Op_Id);
2604 Op_Id := Homonym (Op_Id);
2609 end Analyze_Negation;
2615 procedure Analyze_Null (N : Node_Id) is
2617 Check_SPARK_Restriction ("null is not allowed", N);
2619 Set_Etype (N, Any_Access);
2622 ----------------------
2623 -- Analyze_One_Call --
2624 ----------------------
2626 procedure Analyze_One_Call
2630 Success : out Boolean;
2631 Skip_First : Boolean := False)
2633 Actuals : constant List_Id := Parameter_Associations (N);
2634 Prev_T : constant Entity_Id := Etype (N);
2636 Must_Skip : constant Boolean := Skip_First
2637 or else Nkind (Original_Node (N)) = N_Selected_Component
2639 (Nkind (Original_Node (N)) = N_Indexed_Component
2640 and then Nkind (Prefix (Original_Node (N)))
2641 = N_Selected_Component);
2642 -- The first formal must be omitted from the match when trying to find
2643 -- a primitive operation that is a possible interpretation, and also
2644 -- after the call has been rewritten, because the corresponding actual
2645 -- is already known to be compatible, and because this may be an
2646 -- indexing of a call with default parameters.
2650 Is_Indexed : Boolean := False;
2651 Is_Indirect : Boolean := False;
2652 Subp_Type : constant Entity_Id := Etype (Nam);
2655 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2656 -- There may be a user-defined operator that hides the current
2657 -- interpretation. We must check for this independently of the
2658 -- analysis of the call with the user-defined operation, because
2659 -- the parameter names may be wrong and yet the hiding takes place.
2660 -- This fixes a problem with ACATS test B34014O.
2662 -- When the type Address is a visible integer type, and the DEC
2663 -- system extension is visible, the predefined operator may be
2664 -- hidden as well, by one of the address operations in auxdec.
2665 -- Finally, The abstract operations on address do not hide the
2666 -- predefined operator (this is the purpose of making them abstract).
2668 procedure Indicate_Name_And_Type;
2669 -- If candidate interpretation matches, indicate name and type of
2670 -- result on call node.
2672 ----------------------------
2673 -- Indicate_Name_And_Type --
2674 ----------------------------
2676 procedure Indicate_Name_And_Type is
2678 Add_One_Interp (N, Nam, Etype (Nam));
2679 Check_Implicit_Dereference (N, Etype (Nam));
2682 -- If the prefix of the call is a name, indicate the entity
2683 -- being called. If it is not a name, it is an expression that
2684 -- denotes an access to subprogram or else an entry or family. In
2685 -- the latter case, the name is a selected component, and the entity
2686 -- being called is noted on the selector.
2688 if not Is_Type (Nam) then
2689 if Is_Entity_Name (Name (N)) then
2690 Set_Entity (Name (N), Nam);
2692 elsif Nkind (Name (N)) = N_Selected_Component then
2693 Set_Entity (Selector_Name (Name (N)), Nam);
2697 if Debug_Flag_E and not Report then
2698 Write_Str (" Overloaded call ");
2699 Write_Int (Int (N));
2700 Write_Str (" compatible with ");
2701 Write_Int (Int (Nam));
2704 end Indicate_Name_And_Type;
2706 ------------------------
2707 -- Operator_Hidden_By --
2708 ------------------------
2710 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2711 Act1 : constant Node_Id := First_Actual (N);
2712 Act2 : constant Node_Id := Next_Actual (Act1);
2713 Form1 : constant Entity_Id := First_Formal (Fun);
2714 Form2 : constant Entity_Id := Next_Formal (Form1);
2717 if Ekind (Fun) /= E_Function
2718 or else Is_Abstract_Subprogram (Fun)
2722 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2725 elsif Present (Form2) then
2727 No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2732 elsif Present (Act2) then
2736 -- Now we know that the arity of the operator matches the function,
2737 -- and the function call is a valid interpretation. The function
2738 -- hides the operator if it has the right signature, or if one of
2739 -- its operands is a non-abstract operation on Address when this is
2740 -- a visible integer type.
2742 return Hides_Op (Fun, Nam)
2743 or else Is_Descendent_Of_Address (Etype (Form1))
2746 and then Is_Descendent_Of_Address (Etype (Form2)));
2747 end Operator_Hidden_By;
2749 -- Start of processing for Analyze_One_Call
2754 -- If the subprogram has no formals or if all the formals have defaults,
2755 -- and the return type is an array type, the node may denote an indexing
2756 -- of the result of a parameterless call. In Ada 2005, the subprogram
2757 -- may have one non-defaulted formal, and the call may have been written
2758 -- in prefix notation, so that the rebuilt parameter list has more than
2761 if not Is_Overloadable (Nam)
2762 and then Ekind (Nam) /= E_Subprogram_Type
2763 and then Ekind (Nam) /= E_Entry_Family
2768 -- An indexing requires at least one actual
2770 if not Is_Empty_List (Actuals)
2772 (Needs_No_Actuals (Nam)
2774 (Needs_One_Actual (Nam)
2775 and then Present (Next_Actual (First (Actuals)))))
2777 if Is_Array_Type (Subp_Type) then
2778 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2780 elsif Is_Access_Type (Subp_Type)
2781 and then Is_Array_Type (Designated_Type (Subp_Type))
2785 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2787 -- The prefix can also be a parameterless function that returns an
2788 -- access to subprogram, in which case this is an indirect call.
2789 -- If this succeeds, an explicit dereference is added later on,
2790 -- in Analyze_Call or Resolve_Call.
2792 elsif Is_Access_Type (Subp_Type)
2793 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2795 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
2800 -- If the call has been transformed into a slice, it is of the form
2801 -- F (Subtype) where F is parameterless. The node has been rewritten in
2802 -- Try_Indexed_Call and there is nothing else to do.
2805 and then Nkind (N) = N_Slice
2811 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
2815 -- If an indirect call is a possible interpretation, indicate
2816 -- success to the caller.
2822 -- Mismatch in number or names of parameters
2824 elsif Debug_Flag_E then
2825 Write_Str (" normalization fails in call ");
2826 Write_Int (Int (N));
2827 Write_Str (" with subprogram ");
2828 Write_Int (Int (Nam));
2832 -- If the context expects a function call, discard any interpretation
2833 -- that is a procedure. If the node is not overloaded, leave as is for
2834 -- better error reporting when type mismatch is found.
2836 elsif Nkind (N) = N_Function_Call
2837 and then Is_Overloaded (Name (N))
2838 and then Ekind (Nam) = E_Procedure
2842 -- Ditto for function calls in a procedure context
2844 elsif Nkind (N) = N_Procedure_Call_Statement
2845 and then Is_Overloaded (Name (N))
2846 and then Etype (Nam) /= Standard_Void_Type
2850 elsif No (Actuals) then
2852 -- If Normalize succeeds, then there are default parameters for
2855 Indicate_Name_And_Type;
2857 elsif Ekind (Nam) = E_Operator then
2858 if Nkind (N) = N_Procedure_Call_Statement then
2862 -- This can occur when the prefix of the call is an operator
2863 -- name or an expanded name whose selector is an operator name.
2865 Analyze_Operator_Call (N, Nam);
2867 if Etype (N) /= Prev_T then
2869 -- Check that operator is not hidden by a function interpretation
2871 if Is_Overloaded (Name (N)) then
2877 Get_First_Interp (Name (N), I, It);
2878 while Present (It.Nam) loop
2879 if Operator_Hidden_By (It.Nam) then
2880 Set_Etype (N, Prev_T);
2884 Get_Next_Interp (I, It);
2889 -- If operator matches formals, record its name on the call.
2890 -- If the operator is overloaded, Resolve will select the
2891 -- correct one from the list of interpretations. The call
2892 -- node itself carries the first candidate.
2894 Set_Entity (Name (N), Nam);
2897 elsif Report and then Etype (N) = Any_Type then
2898 Error_Msg_N ("incompatible arguments for operator", N);
2902 -- Normalize_Actuals has chained the named associations in the
2903 -- correct order of the formals.
2905 Actual := First_Actual (N);
2906 Formal := First_Formal (Nam);
2908 -- If we are analyzing a call rewritten from object notation, skip
2909 -- first actual, which may be rewritten later as an explicit
2913 Next_Actual (Actual);
2914 Next_Formal (Formal);
2917 while Present (Actual) and then Present (Formal) loop
2918 if Nkind (Parent (Actual)) /= N_Parameter_Association
2919 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2921 -- The actual can be compatible with the formal, but we must
2922 -- also check that the context is not an address type that is
2923 -- visibly an integer type, as is the case in VMS_64. In this
2924 -- case the use of literals is illegal, except in the body of
2925 -- descendents of system, where arithmetic operations on
2926 -- address are of course used.
2928 if Has_Compatible_Type (Actual, Etype (Formal))
2930 (Etype (Actual) /= Universal_Integer
2931 or else not Is_Descendent_Of_Address (Etype (Formal))
2933 Is_Predefined_File_Name
2934 (Unit_File_Name (Get_Source_Unit (N))))
2936 Next_Actual (Actual);
2937 Next_Formal (Formal);
2940 if Debug_Flag_E then
2941 Write_Str (" type checking fails in call ");
2942 Write_Int (Int (N));
2943 Write_Str (" with formal ");
2944 Write_Int (Int (Formal));
2945 Write_Str (" in subprogram ");
2946 Write_Int (Int (Nam));
2950 if Report and not Is_Indexed and not Is_Indirect then
2952 -- Ada 2005 (AI-251): Complete the error notification
2953 -- to help new Ada 2005 users.
2955 if Is_Class_Wide_Type (Etype (Formal))
2956 and then Is_Interface (Etype (Etype (Formal)))
2957 and then not Interface_Present_In_Ancestor
2958 (Typ => Etype (Actual),
2959 Iface => Etype (Etype (Formal)))
2962 ("(Ada 2005) does not implement interface }",
2963 Actual, Etype (Etype (Formal)));
2966 Wrong_Type (Actual, Etype (Formal));
2968 if Nkind (Actual) = N_Op_Eq
2969 and then Nkind (Left_Opnd (Actual)) = N_Identifier
2971 Formal := First_Formal (Nam);
2972 while Present (Formal) loop
2973 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2974 Error_Msg_N -- CODEFIX
2975 ("possible misspelling of `='>`!", Actual);
2979 Next_Formal (Formal);
2983 if All_Errors_Mode then
2984 Error_Msg_Sloc := Sloc (Nam);
2986 if Etype (Formal) = Any_Type then
2988 ("there is no legal actual parameter", Actual);
2991 if Is_Overloadable (Nam)
2992 and then Present (Alias (Nam))
2993 and then not Comes_From_Source (Nam)
2996 ("\\ =='> in call to inherited operation & #!",
2999 elsif Ekind (Nam) = E_Subprogram_Type then
3001 Access_To_Subprogram_Typ :
3002 constant Entity_Id :=
3004 (Associated_Node_For_Itype (Nam));
3007 "\\ =='> in call to dereference of &#!",
3008 Actual, Access_To_Subprogram_Typ);
3013 ("\\ =='> in call to &#!", Actual, Nam);
3023 -- Normalize_Actuals has verified that a default value exists
3024 -- for this formal. Current actual names a subsequent formal.
3026 Next_Formal (Formal);
3030 -- On exit, all actuals match
3032 Indicate_Name_And_Type;
3034 end Analyze_One_Call;
3036 ---------------------------
3037 -- Analyze_Operator_Call --
3038 ---------------------------
3040 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3041 Op_Name : constant Name_Id := Chars (Op_Id);
3042 Act1 : constant Node_Id := First_Actual (N);
3043 Act2 : constant Node_Id := Next_Actual (Act1);
3046 -- Binary operator case
3048 if Present (Act2) then
3050 -- If more than two operands, then not binary operator after all
3052 if Present (Next_Actual (Act2)) then
3055 elsif Op_Name = Name_Op_Add
3056 or else Op_Name = Name_Op_Subtract
3057 or else Op_Name = Name_Op_Multiply
3058 or else Op_Name = Name_Op_Divide
3059 or else Op_Name = Name_Op_Mod
3060 or else Op_Name = Name_Op_Rem
3061 or else Op_Name = Name_Op_Expon
3063 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3065 elsif Op_Name = Name_Op_And
3066 or else Op_Name = Name_Op_Or
3067 or else Op_Name = Name_Op_Xor
3069 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3071 elsif Op_Name = Name_Op_Lt
3072 or else Op_Name = Name_Op_Le
3073 or else Op_Name = Name_Op_Gt
3074 or else Op_Name = Name_Op_Ge
3076 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3078 elsif Op_Name = Name_Op_Eq
3079 or else Op_Name = Name_Op_Ne
3081 Find_Equality_Types (Act1, Act2, Op_Id, N);
3083 elsif Op_Name = Name_Op_Concat then
3084 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3086 -- Is this else null correct, or should it be an abort???
3092 -- Unary operator case
3095 if Op_Name = Name_Op_Subtract or else
3096 Op_Name = Name_Op_Add or else
3097 Op_Name = Name_Op_Abs
3099 Find_Unary_Types (Act1, Op_Id, N);
3102 Op_Name = Name_Op_Not
3104 Find_Negation_Types (Act1, Op_Id, N);
3106 -- Is this else null correct, or should it be an abort???
3112 end Analyze_Operator_Call;
3114 -------------------------------------------
3115 -- Analyze_Overloaded_Selected_Component --
3116 -------------------------------------------
3118 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3119 Nam : constant Node_Id := Prefix (N);
3120 Sel : constant Node_Id := Selector_Name (N);
3127 Set_Etype (Sel, Any_Type);
3129 Get_First_Interp (Nam, I, It);
3130 while Present (It.Typ) loop
3131 if Is_Access_Type (It.Typ) then
3132 T := Designated_Type (It.Typ);
3133 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3138 -- Locate the component. For a private prefix the selector can denote
3141 if Is_Record_Type (T) or else Is_Private_Type (T) then
3143 -- If the prefix is a class-wide type, the visible components are
3144 -- those of the base type.
3146 if Is_Class_Wide_Type (T) then
3150 Comp := First_Entity (T);
3151 while Present (Comp) loop
3152 if Chars (Comp) = Chars (Sel)
3153 and then Is_Visible_Component (Comp)
3156 -- AI05-105: if the context is an object renaming with
3157 -- an anonymous access type, the expected type of the
3158 -- object must be anonymous. This is a name resolution rule.
3160 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3161 or else No (Access_Definition (Parent (N)))
3162 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3164 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3166 Set_Entity (Sel, Comp);
3167 Set_Etype (Sel, Etype (Comp));
3168 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3169 Check_Implicit_Dereference (N, Etype (Comp));
3171 -- This also specifies a candidate to resolve the name.
3172 -- Further overloading will be resolved from context.
3173 -- The selector name itself does not carry overloading
3176 Set_Etype (Nam, It.Typ);
3179 -- Named access type in the context of a renaming
3180 -- declaration with an access definition. Remove
3181 -- inapplicable candidate.
3190 elsif Is_Concurrent_Type (T) then
3191 Comp := First_Entity (T);
3192 while Present (Comp)
3193 and then Comp /= First_Private_Entity (T)
3195 if Chars (Comp) = Chars (Sel) then
3196 if Is_Overloadable (Comp) then
3197 Add_One_Interp (Sel, Comp, Etype (Comp));
3199 Set_Entity_With_Style_Check (Sel, Comp);
3200 Generate_Reference (Comp, Sel);
3203 Set_Etype (Sel, Etype (Comp));
3204 Set_Etype (N, Etype (Comp));
3205 Set_Etype (Nam, It.Typ);
3207 -- For access type case, introduce explicit dereference for
3208 -- more uniform treatment of entry calls. Do this only once
3209 -- if several interpretations yield an access type.
3211 if Is_Access_Type (Etype (Nam))
3212 and then Nkind (Nam) /= N_Explicit_Dereference
3214 Insert_Explicit_Dereference (Nam);
3216 (Warn_On_Dereference, "?implicit dereference", N);
3223 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3226 Get_Next_Interp (I, It);
3229 if Etype (N) = Any_Type
3230 and then not Try_Object_Operation (N)
3232 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3233 Set_Entity (Sel, Any_Id);
3234 Set_Etype (Sel, Any_Type);
3236 end Analyze_Overloaded_Selected_Component;
3238 ----------------------------------
3239 -- Analyze_Qualified_Expression --
3240 ----------------------------------
3242 procedure Analyze_Qualified_Expression (N : Node_Id) is
3243 Mark : constant Entity_Id := Subtype_Mark (N);
3244 Expr : constant Node_Id := Expression (N);
3250 Analyze_Expression (Expr);
3252 Set_Etype (N, Any_Type);
3257 if T = Any_Type then
3261 Check_Fully_Declared (T, N);
3263 -- If expected type is class-wide, check for exact match before
3264 -- expansion, because if the expression is a dispatching call it
3265 -- may be rewritten as explicit dereference with class-wide result.
3266 -- If expression is overloaded, retain only interpretations that
3267 -- will yield exact matches.
3269 if Is_Class_Wide_Type (T) then
3270 if not Is_Overloaded (Expr) then
3271 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3272 if Nkind (Expr) = N_Aggregate then
3273 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3275 Wrong_Type (Expr, T);
3280 Get_First_Interp (Expr, I, It);
3282 while Present (It.Nam) loop
3283 if Base_Type (It.Typ) /= Base_Type (T) then
3287 Get_Next_Interp (I, It);
3293 end Analyze_Qualified_Expression;
3295 -----------------------------------
3296 -- Analyze_Quantified_Expression --
3297 -----------------------------------
3299 procedure Analyze_Quantified_Expression (N : Node_Id) is
3300 Loc : constant Source_Ptr := Sloc (N);
3301 Ent : constant Entity_Id :=
3303 (E_Loop, Current_Scope, Sloc (N), 'L');
3308 Check_SPARK_Restriction ("quantified expression is not allowed", N);
3310 Set_Etype (Ent, Standard_Void_Type);
3311 Set_Parent (Ent, N);
3313 if Present (Loop_Parameter_Specification (N)) then
3315 Make_Iteration_Scheme (Loc,
3316 Loop_Parameter_Specification =>
3317 Loop_Parameter_Specification (N));
3320 Make_Iteration_Scheme (Loc,
3321 Iterator_Specification =>
3322 Iterator_Specification (N));
3326 Set_Parent (Iterator, N);
3327 Analyze_Iteration_Scheme (Iterator);
3329 -- The loop specification may have been converted into an
3330 -- iterator specification during its analysis. Update the
3331 -- quantified node accordingly.
3333 if Present (Iterator_Specification (Iterator)) then
3334 Set_Iterator_Specification
3335 (N, Iterator_Specification (Iterator));
3336 Set_Loop_Parameter_Specification (N, Empty);
3339 Analyze (Condition (N));
3342 Set_Etype (N, Standard_Boolean);
3343 end Analyze_Quantified_Expression;
3349 procedure Analyze_Range (N : Node_Id) is
3350 L : constant Node_Id := Low_Bound (N);
3351 H : constant Node_Id := High_Bound (N);
3352 I1, I2 : Interp_Index;
3355 procedure Check_Common_Type (T1, T2 : Entity_Id);
3356 -- Verify the compatibility of two types, and choose the
3357 -- non universal one if the other is universal.
3359 procedure Check_High_Bound (T : Entity_Id);
3360 -- Test one interpretation of the low bound against all those
3361 -- of the high bound.
3363 procedure Check_Universal_Expression (N : Node_Id);
3364 -- In Ada83, reject bounds of a universal range that are not
3365 -- literals or entity names.
3367 -----------------------
3368 -- Check_Common_Type --
3369 -----------------------
3371 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3373 if Covers (T1 => T1, T2 => T2)
3375 Covers (T1 => T2, T2 => T1)
3377 if T1 = Universal_Integer
3378 or else T1 = Universal_Real
3379 or else T1 = Any_Character
3381 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3384 Add_One_Interp (N, T1, T1);
3387 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3390 end Check_Common_Type;
3392 ----------------------
3393 -- Check_High_Bound --
3394 ----------------------
3396 procedure Check_High_Bound (T : Entity_Id) is
3398 if not Is_Overloaded (H) then
3399 Check_Common_Type (T, Etype (H));
3401 Get_First_Interp (H, I2, It2);
3402 while Present (It2.Typ) loop
3403 Check_Common_Type (T, It2.Typ);
3404 Get_Next_Interp (I2, It2);
3407 end Check_High_Bound;
3409 -----------------------------
3410 -- Is_Universal_Expression --
3411 -----------------------------
3413 procedure Check_Universal_Expression (N : Node_Id) is
3415 if Etype (N) = Universal_Integer
3416 and then Nkind (N) /= N_Integer_Literal
3417 and then not Is_Entity_Name (N)
3418 and then Nkind (N) /= N_Attribute_Reference
3420 Error_Msg_N ("illegal bound in discrete range", N);
3422 end Check_Universal_Expression;
3424 -- Start of processing for Analyze_Range
3427 Set_Etype (N, Any_Type);
3428 Analyze_Expression (L);
3429 Analyze_Expression (H);
3431 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3435 if not Is_Overloaded (L) then
3436 Check_High_Bound (Etype (L));
3438 Get_First_Interp (L, I1, It1);
3439 while Present (It1.Typ) loop
3440 Check_High_Bound (It1.Typ);
3441 Get_Next_Interp (I1, It1);
3445 -- If result is Any_Type, then we did not find a compatible pair
3447 if Etype (N) = Any_Type then
3448 Error_Msg_N ("incompatible types in range ", N);
3452 if Ada_Version = Ada_83
3454 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3455 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3457 Check_Universal_Expression (L);
3458 Check_Universal_Expression (H);
3462 -----------------------
3463 -- Analyze_Reference --
3464 -----------------------
3466 procedure Analyze_Reference (N : Node_Id) is
3467 P : constant Node_Id := Prefix (N);
3470 Acc_Type : Entity_Id;
3475 -- An interesting error check, if we take the 'Reference of an object
3476 -- for which a pragma Atomic or Volatile has been given, and the type
3477 -- of the object is not Atomic or Volatile, then we are in trouble. The
3478 -- problem is that no trace of the atomic/volatile status will remain
3479 -- for the backend to respect when it deals with the resulting pointer,
3480 -- since the pointer type will not be marked atomic (it is a pointer to
3481 -- the base type of the object).
3483 -- It is not clear if that can ever occur, but in case it does, we will
3484 -- generate an error message. Not clear if this message can ever be
3485 -- generated, and pretty clear that it represents a bug if it is, still
3486 -- seems worth checking, except in CodePeer mode where we do not really
3487 -- care and don't want to bother the user.
3491 if Is_Entity_Name (P)
3492 and then Is_Object_Reference (P)
3493 and then not CodePeer_Mode
3498 if (Has_Atomic_Components (E)
3499 and then not Has_Atomic_Components (T))
3501 (Has_Volatile_Components (E)
3502 and then not Has_Volatile_Components (T))
3503 or else (Is_Atomic (E) and then not Is_Atomic (T))
3504 or else (Is_Volatile (E) and then not Is_Volatile (T))
3506 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3510 -- Carry on with normal processing
3512 Acc_Type := Create_Itype (E_Allocator_Type, N);
3513 Set_Etype (Acc_Type, Acc_Type);
3514 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3515 Set_Etype (N, Acc_Type);
3516 end Analyze_Reference;
3518 --------------------------------
3519 -- Analyze_Selected_Component --
3520 --------------------------------
3522 -- Prefix is a record type or a task or protected type. In the latter case,
3523 -- the selector must denote a visible entry.
3525 procedure Analyze_Selected_Component (N : Node_Id) is
3526 Name : constant Node_Id := Prefix (N);
3527 Sel : constant Node_Id := Selector_Name (N);
3530 Has_Candidate : Boolean := False;
3533 Pent : Entity_Id := Empty;
3534 Prefix_Type : Entity_Id;
3536 Type_To_Use : Entity_Id;
3537 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3538 -- a class-wide type, we use its root type, whose components are
3539 -- present in the class-wide type.
3541 Is_Single_Concurrent_Object : Boolean;
3542 -- Set True if the prefix is a single task or a single protected object
3544 procedure Find_Component_In_Instance (Rec : Entity_Id);
3545 -- In an instance, a component of a private extension may not be visible
3546 -- while it was visible in the generic. Search candidate scope for a
3547 -- component with the proper identifier. This is only done if all other
3548 -- searches have failed. When the match is found (it always will be),
3549 -- the Etype of both N and Sel are set from this component, and the
3550 -- entity of Sel is set to reference this component.
3552 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3553 -- It is known that the parent of N denotes a subprogram call. Comp
3554 -- is an overloadable component of the concurrent type of the prefix.
3555 -- Determine whether all formals of the parent of N and Comp are mode
3556 -- conformant. If the parent node is not analyzed yet it may be an
3557 -- indexed component rather than a function call.
3559 --------------------------------
3560 -- Find_Component_In_Instance --
3561 --------------------------------
3563 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3567 Comp := First_Component (Rec);
3568 while Present (Comp) loop
3569 if Chars (Comp) = Chars (Sel) then
3570 Set_Entity_With_Style_Check (Sel, Comp);
3571 Set_Etype (Sel, Etype (Comp));
3572 Set_Etype (N, Etype (Comp));
3576 Next_Component (Comp);
3579 -- This must succeed because code was legal in the generic
3581 raise Program_Error;
3582 end Find_Component_In_Instance;
3584 ------------------------------
3585 -- Has_Mode_Conformant_Spec --
3586 ------------------------------
3588 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3589 Comp_Param : Entity_Id;
3591 Param_Typ : Entity_Id;
3594 Comp_Param := First_Formal (Comp);
3596 if Nkind (Parent (N)) = N_Indexed_Component then
3597 Param := First (Expressions (Parent (N)));
3599 Param := First (Parameter_Associations (Parent (N)));
3602 while Present (Comp_Param)
3603 and then Present (Param)
3605 Param_Typ := Find_Parameter_Type (Param);
3607 if Present (Param_Typ)
3609 not Conforming_Types
3610 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
3615 Next_Formal (Comp_Param);
3619 -- One of the specs has additional formals
3621 if Present (Comp_Param) or else Present (Param) then
3626 end Has_Mode_Conformant_Spec;
3628 -- Start of processing for Analyze_Selected_Component
3631 Set_Etype (N, Any_Type);
3633 if Is_Overloaded (Name) then
3634 Analyze_Overloaded_Selected_Component (N);
3637 elsif Etype (Name) = Any_Type then
3638 Set_Entity (Sel, Any_Id);
3639 Set_Etype (Sel, Any_Type);
3643 Prefix_Type := Etype (Name);
3646 if Is_Access_Type (Prefix_Type) then
3648 -- A RACW object can never be used as prefix of a selected component
3649 -- since that means it is dereferenced without being a controlling
3650 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
3651 -- reporting an error, we must check whether this is actually a
3652 -- dispatching call in prefix form.
3654 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
3655 and then Comes_From_Source (N)
3657 if Try_Object_Operation (N) then
3661 ("invalid dereference of a remote access-to-class-wide value",
3665 -- Normal case of selected component applied to access type
3668 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3670 if Is_Entity_Name (Name) then
3671 Pent := Entity (Name);
3672 elsif Nkind (Name) = N_Selected_Component
3673 and then Is_Entity_Name (Selector_Name (Name))
3675 Pent := Entity (Selector_Name (Name));
3678 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3681 -- If we have an explicit dereference of a remote access-to-class-wide
3682 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
3683 -- have to check for the case of a prefix that is a controlling operand
3684 -- of a prefixed dispatching call, as the dereference is legal in that
3685 -- case. Normally this condition is checked in Validate_Remote_Access_
3686 -- To_Class_Wide_Type, but we have to defer the checking for selected
3687 -- component prefixes because of the prefixed dispatching call case.
3688 -- Note that implicit dereferences are checked for this just above.
3690 elsif Nkind (Name) = N_Explicit_Dereference
3691 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3692 and then Comes_From_Source (N)
3694 if Try_Object_Operation (N) then
3698 ("invalid dereference of a remote access-to-class-wide value",
3703 -- (Ada 2005): if the prefix is the limited view of a type, and
3704 -- the context already includes the full view, use the full view
3705 -- in what follows, either to retrieve a component of to find
3706 -- a primitive operation. If the prefix is an explicit dereference,
3707 -- set the type of the prefix to reflect this transformation.
3708 -- If the non-limited view is itself an incomplete type, get the
3709 -- full view if available.
3711 if Is_Incomplete_Type (Prefix_Type)
3712 and then From_With_Type (Prefix_Type)
3713 and then Present (Non_Limited_View (Prefix_Type))
3715 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3717 if Nkind (N) = N_Explicit_Dereference then
3718 Set_Etype (Prefix (N), Prefix_Type);
3721 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3722 and then From_With_Type (Prefix_Type)
3723 and then Present (Non_Limited_View (Etype (Prefix_Type)))
3726 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3728 if Nkind (N) = N_Explicit_Dereference then
3729 Set_Etype (Prefix (N), Prefix_Type);
3733 if Ekind (Prefix_Type) = E_Private_Subtype then
3734 Prefix_Type := Base_Type (Prefix_Type);
3737 Type_To_Use := Prefix_Type;
3739 -- For class-wide types, use the entity list of the root type. This
3740 -- indirection is specially important for private extensions because
3741 -- only the root type get switched (not the class-wide type).
3743 if Is_Class_Wide_Type (Prefix_Type) then
3744 Type_To_Use := Root_Type (Prefix_Type);
3747 -- If the prefix is a single concurrent object, use its name in error
3748 -- messages, rather than that of its anonymous type.
3750 Is_Single_Concurrent_Object :=
3751 Is_Concurrent_Type (Prefix_Type)
3752 and then Is_Internal_Name (Chars (Prefix_Type))
3753 and then not Is_Derived_Type (Prefix_Type)
3754 and then Is_Entity_Name (Name);
3756 Comp := First_Entity (Type_To_Use);
3758 -- If the selector has an original discriminant, the node appears in
3759 -- an instance. Replace the discriminant with the corresponding one
3760 -- in the current discriminated type. For nested generics, this must
3761 -- be done transitively, so note the new original discriminant.
3763 if Nkind (Sel) = N_Identifier
3764 and then In_Instance
3765 and then Present (Original_Discriminant (Sel))
3767 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3769 -- Mark entity before rewriting, for completeness and because
3770 -- subsequent semantic checks might examine the original node.
3772 Set_Entity (Sel, Comp);
3773 Rewrite (Selector_Name (N),
3774 New_Occurrence_Of (Comp, Sloc (N)));
3775 Set_Original_Discriminant (Selector_Name (N), Comp);
3776 Set_Etype (N, Etype (Comp));
3777 Check_Implicit_Dereference (N, Etype (Comp));
3779 if Is_Access_Type (Etype (Name)) then
3780 Insert_Explicit_Dereference (Name);
3781 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3784 elsif Is_Record_Type (Prefix_Type) then
3786 -- Find component with given name
3788 while Present (Comp) loop
3789 if Chars (Comp) = Chars (Sel)
3790 and then Is_Visible_Component (Comp)
3792 Set_Entity_With_Style_Check (Sel, Comp);
3793 Set_Etype (Sel, Etype (Comp));
3795 if Ekind (Comp) = E_Discriminant then
3796 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3798 ("cannot reference discriminant of Unchecked_Union",
3802 if Is_Generic_Type (Prefix_Type)
3804 Is_Generic_Type (Root_Type (Prefix_Type))
3806 Set_Original_Discriminant (Sel, Comp);
3810 -- Resolve the prefix early otherwise it is not possible to
3811 -- build the actual subtype of the component: it may need
3812 -- to duplicate this prefix and duplication is only allowed
3813 -- on fully resolved expressions.
3817 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
3818 -- subtypes in a package specification.
3821 -- limited with Pkg;
3823 -- type Acc_Inc is access Pkg.T;
3825 -- N : Natural := X.all.Comp; -- ERROR, limited view
3826 -- end Pkg; -- Comp is not visible
3828 if Nkind (Name) = N_Explicit_Dereference
3829 and then From_With_Type (Etype (Prefix (Name)))
3830 and then not Is_Potentially_Use_Visible (Etype (Name))
3831 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3832 N_Package_Specification
3835 ("premature usage of incomplete}", Prefix (Name),
3836 Etype (Prefix (Name)));
3839 -- We never need an actual subtype for the case of a selection
3840 -- for a indexed component of a non-packed array, since in
3841 -- this case gigi generates all the checks and can find the
3842 -- necessary bounds information.
3844 -- We also do not need an actual subtype for the case of a
3845 -- first, last, length, or range attribute applied to a
3846 -- non-packed array, since gigi can again get the bounds in
3847 -- these cases (gigi cannot handle the packed case, since it
3848 -- has the bounds of the packed array type, not the original
3849 -- bounds of the type). However, if the prefix is itself a
3850 -- selected component, as in a.b.c (i), gigi may regard a.b.c
3851 -- as a dynamic-sized temporary, so we do generate an actual
3852 -- subtype for this case.
3854 Parent_N := Parent (N);
3856 if not Is_Packed (Etype (Comp))
3858 ((Nkind (Parent_N) = N_Indexed_Component
3859 and then Nkind (Name) /= N_Selected_Component)
3861 (Nkind (Parent_N) = N_Attribute_Reference
3862 and then (Attribute_Name (Parent_N) = Name_First
3864 Attribute_Name (Parent_N) = Name_Last
3866 Attribute_Name (Parent_N) = Name_Length
3868 Attribute_Name (Parent_N) = Name_Range)))
3870 Set_Etype (N, Etype (Comp));
3872 -- If full analysis is not enabled, we do not generate an
3873 -- actual subtype, because in the absence of expansion
3874 -- reference to a formal of a protected type, for example,
3875 -- will not be properly transformed, and will lead to
3876 -- out-of-scope references in gigi.
3878 -- In all other cases, we currently build an actual subtype.
3879 -- It seems likely that many of these cases can be avoided,
3880 -- but right now, the front end makes direct references to the
3881 -- bounds (e.g. in generating a length check), and if we do
3882 -- not make an actual subtype, we end up getting a direct
3883 -- reference to a discriminant, which will not do.
3885 elsif Full_Analysis then
3887 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3888 Insert_Action (N, Act_Decl);
3890 if No (Act_Decl) then
3891 Set_Etype (N, Etype (Comp));
3894 -- Component type depends on discriminants. Enter the
3895 -- main attributes of the subtype.
3898 Subt : constant Entity_Id :=
3899 Defining_Identifier (Act_Decl);
3902 Set_Etype (Subt, Base_Type (Etype (Comp)));
3903 Set_Ekind (Subt, Ekind (Etype (Comp)));
3904 Set_Etype (N, Subt);
3908 -- If Full_Analysis not enabled, just set the Etype
3911 Set_Etype (N, Etype (Comp));
3914 Check_Implicit_Dereference (N, Etype (N));
3918 -- If the prefix is a private extension, check only the visible
3919 -- components of the partial view. This must include the tag,
3920 -- which can appear in expanded code in a tag check.
3922 if Ekind (Type_To_Use) = E_Record_Type_With_Private
3923 and then Chars (Selector_Name (N)) /= Name_uTag
3925 exit when Comp = Last_Entity (Type_To_Use);
3931 -- Ada 2005 (AI-252): The selected component can be interpreted as
3932 -- a prefixed view of a subprogram. Depending on the context, this is
3933 -- either a name that can appear in a renaming declaration, or part
3934 -- of an enclosing call given in prefix form.
3936 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
3937 -- selected component should resolve to a name.
3939 if Ada_Version >= Ada_2005
3940 and then Is_Tagged_Type (Prefix_Type)
3941 and then not Is_Concurrent_Type (Prefix_Type)
3943 if Nkind (Parent (N)) = N_Generic_Association
3944 or else Nkind (Parent (N)) = N_Requeue_Statement
3945 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
3947 if Find_Primitive_Operation (N) then
3951 elsif Try_Object_Operation (N) then
3955 -- If the transformation fails, it will be necessary to redo the
3956 -- analysis with all errors enabled, to indicate candidate
3957 -- interpretations and reasons for each failure ???
3961 elsif Is_Private_Type (Prefix_Type) then
3963 -- Allow access only to discriminants of the type. If the type has
3964 -- no full view, gigi uses the parent type for the components, so we
3965 -- do the same here.
3967 if No (Full_View (Prefix_Type)) then
3968 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
3969 Comp := First_Entity (Type_To_Use);
3972 while Present (Comp) loop
3973 if Chars (Comp) = Chars (Sel) then
3974 if Ekind (Comp) = E_Discriminant then
3975 Set_Entity_With_Style_Check (Sel, Comp);
3976 Generate_Reference (Comp, Sel);
3978 Set_Etype (Sel, Etype (Comp));
3979 Set_Etype (N, Etype (Comp));
3980 Check_Implicit_Dereference (N, Etype (N));
3982 if Is_Generic_Type (Prefix_Type)
3983 or else Is_Generic_Type (Root_Type (Prefix_Type))
3985 Set_Original_Discriminant (Sel, Comp);
3988 -- Before declaring an error, check whether this is tagged
3989 -- private type and a call to a primitive operation.
3991 elsif Ada_Version >= Ada_2005
3992 and then Is_Tagged_Type (Prefix_Type)
3993 and then Try_Object_Operation (N)
3998 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
3999 Error_Msg_NE ("invisible selector& for }", N, Sel);
4000 Set_Entity (Sel, Any_Id);
4001 Set_Etype (N, Any_Type);
4010 elsif Is_Concurrent_Type (Prefix_Type) then
4012 -- Find visible operation with given name. For a protected type,
4013 -- the possible candidates are discriminants, entries or protected
4014 -- procedures. For a task type, the set can only include entries or
4015 -- discriminants if the task type is not an enclosing scope. If it
4016 -- is an enclosing scope (e.g. in an inner task) then all entities
4017 -- are visible, but the prefix must denote the enclosing scope, i.e.
4018 -- can only be a direct name or an expanded name.
4020 Set_Etype (Sel, Any_Type);
4021 In_Scope := In_Open_Scopes (Prefix_Type);
4023 while Present (Comp) loop
4024 if Chars (Comp) = Chars (Sel) then
4025 if Is_Overloadable (Comp) then
4026 Add_One_Interp (Sel, Comp, Etype (Comp));
4028 -- If the prefix is tagged, the correct interpretation may
4029 -- lie in the primitive or class-wide operations of the
4030 -- type. Perform a simple conformance check to determine
4031 -- whether Try_Object_Operation should be invoked even if
4032 -- a visible entity is found.
4034 if Is_Tagged_Type (Prefix_Type)
4036 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4038 N_Indexed_Component)
4039 and then Has_Mode_Conformant_Spec (Comp)
4041 Has_Candidate := True;
4044 -- Note: a selected component may not denote a component of a
4045 -- protected type (4.1.3(7)).
4047 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4049 and then not Is_Protected_Type (Prefix_Type)
4050 and then Is_Entity_Name (Name))
4052 Set_Entity_With_Style_Check (Sel, Comp);
4053 Generate_Reference (Comp, Sel);
4059 Set_Etype (Sel, Etype (Comp));
4060 Set_Etype (N, Etype (Comp));
4062 if Ekind (Comp) = E_Discriminant then
4063 Set_Original_Discriminant (Sel, Comp);
4066 -- For access type case, introduce explicit dereference for
4067 -- more uniform treatment of entry calls.
4069 if Is_Access_Type (Etype (Name)) then
4070 Insert_Explicit_Dereference (Name);
4072 (Warn_On_Dereference, "?implicit dereference", N);
4078 exit when not In_Scope
4080 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4083 -- If there is no visible entity with the given name or none of the
4084 -- visible entities are plausible interpretations, check whether
4085 -- there is some other primitive operation with that name.
4087 if Ada_Version >= Ada_2005
4088 and then Is_Tagged_Type (Prefix_Type)
4090 if (Etype (N) = Any_Type
4091 or else not Has_Candidate)
4092 and then Try_Object_Operation (N)
4096 -- If the context is not syntactically a procedure call, it
4097 -- may be a call to a primitive function declared outside of
4098 -- the synchronized type.
4100 -- If the context is a procedure call, there might still be
4101 -- an overloading between an entry and a primitive procedure
4102 -- declared outside of the synchronized type, called in prefix
4103 -- notation. This is harder to disambiguate because in one case
4104 -- the controlling formal is implicit ???
4106 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4107 and then Nkind (Parent (N)) /= N_Indexed_Component
4108 and then Try_Object_Operation (N)
4114 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4115 -- Case of a prefix of a protected type: selector might denote
4116 -- an invisible private component.
4118 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4119 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4123 if Present (Comp) then
4124 if Is_Single_Concurrent_Object then
4125 Error_Msg_Node_2 := Entity (Name);
4126 Error_Msg_NE ("invisible selector& for &", N, Sel);
4129 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4130 Error_Msg_NE ("invisible selector& for }", N, Sel);
4136 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4141 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4144 -- If N still has no type, the component is not defined in the prefix
4146 if Etype (N) = Any_Type then
4148 if Is_Single_Concurrent_Object then
4149 Error_Msg_Node_2 := Entity (Name);
4150 Error_Msg_NE ("no selector& for&", N, Sel);
4152 Check_Misspelled_Selector (Type_To_Use, Sel);
4154 elsif Is_Generic_Type (Prefix_Type)
4155 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4156 and then Prefix_Type /= Etype (Prefix_Type)
4157 and then Is_Record_Type (Etype (Prefix_Type))
4159 -- If this is a derived formal type, the parent may have
4160 -- different visibility at this point. Try for an inherited
4161 -- component before reporting an error.
4163 Set_Etype (Prefix (N), Etype (Prefix_Type));
4164 Analyze_Selected_Component (N);
4167 -- Similarly, if this is the actual for a formal derived type, the
4168 -- component inherited from the generic parent may not be visible
4169 -- in the actual, but the selected component is legal.
4171 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4172 and then Is_Generic_Actual_Type (Prefix_Type)
4173 and then Present (Full_View (Prefix_Type))
4176 Find_Component_In_Instance
4177 (Generic_Parent_Type (Parent (Prefix_Type)));
4180 -- Finally, the formal and the actual may be private extensions,
4181 -- but the generic is declared in a child unit of the parent, and
4182 -- an additional step is needed to retrieve the proper scope.
4185 and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4187 Find_Component_In_Instance
4188 (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4191 -- Component not found, specialize error message when appropriate
4194 if Ekind (Prefix_Type) = E_Record_Subtype then
4196 -- Check whether this is a component of the base type which
4197 -- is absent from a statically constrained subtype. This will
4198 -- raise constraint error at run time, but is not a compile-
4199 -- time error. When the selector is illegal for base type as
4200 -- well fall through and generate a compilation error anyway.
4202 Comp := First_Component (Base_Type (Prefix_Type));
4203 while Present (Comp) loop
4204 if Chars (Comp) = Chars (Sel)
4205 and then Is_Visible_Component (Comp)
4207 Set_Entity_With_Style_Check (Sel, Comp);
4208 Generate_Reference (Comp, Sel);
4209 Set_Etype (Sel, Etype (Comp));
4210 Set_Etype (N, Etype (Comp));
4212 -- Emit appropriate message. Gigi will replace the
4213 -- node subsequently with the appropriate Raise.
4215 Apply_Compile_Time_Constraint_Error
4216 (N, "component not present in }?",
4217 CE_Discriminant_Check_Failed,
4218 Ent => Prefix_Type, Rep => False);
4219 Set_Raises_Constraint_Error (N);
4223 Next_Component (Comp);
4228 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4229 Error_Msg_NE ("no selector& for}", N, Sel);
4231 Check_Misspelled_Selector (Type_To_Use, Sel);
4234 Set_Entity (Sel, Any_Id);
4235 Set_Etype (Sel, Any_Type);
4237 end Analyze_Selected_Component;
4239 ---------------------------
4240 -- Analyze_Short_Circuit --
4241 ---------------------------
4243 procedure Analyze_Short_Circuit (N : Node_Id) is
4244 L : constant Node_Id := Left_Opnd (N);
4245 R : constant Node_Id := Right_Opnd (N);
4250 Analyze_Expression (L);
4251 Analyze_Expression (R);
4252 Set_Etype (N, Any_Type);
4254 if not Is_Overloaded (L) then
4255 if Root_Type (Etype (L)) = Standard_Boolean
4256 and then Has_Compatible_Type (R, Etype (L))
4258 Add_One_Interp (N, Etype (L), Etype (L));
4262 Get_First_Interp (L, Ind, It);
4263 while Present (It.Typ) loop
4264 if Root_Type (It.Typ) = Standard_Boolean
4265 and then Has_Compatible_Type (R, It.Typ)
4267 Add_One_Interp (N, It.Typ, It.Typ);
4270 Get_Next_Interp (Ind, It);
4274 -- Here we have failed to find an interpretation. Clearly we know that
4275 -- it is not the case that both operands can have an interpretation of
4276 -- Boolean, but this is by far the most likely intended interpretation.
4277 -- So we simply resolve both operands as Booleans, and at least one of
4278 -- these resolutions will generate an error message, and we do not need
4279 -- to give another error message on the short circuit operation itself.
4281 if Etype (N) = Any_Type then
4282 Resolve (L, Standard_Boolean);
4283 Resolve (R, Standard_Boolean);
4284 Set_Etype (N, Standard_Boolean);
4286 end Analyze_Short_Circuit;
4292 procedure Analyze_Slice (N : Node_Id) is
4293 P : constant Node_Id := Prefix (N);
4294 D : constant Node_Id := Discrete_Range (N);
4295 Array_Type : Entity_Id;
4297 procedure Analyze_Overloaded_Slice;
4298 -- If the prefix is overloaded, select those interpretations that
4299 -- yield a one-dimensional array type.
4301 ------------------------------
4302 -- Analyze_Overloaded_Slice --
4303 ------------------------------
4305 procedure Analyze_Overloaded_Slice is
4311 Set_Etype (N, Any_Type);
4313 Get_First_Interp (P, I, It);
4314 while Present (It.Nam) loop
4317 if Is_Access_Type (Typ) then
4318 Typ := Designated_Type (Typ);
4319 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4322 if Is_Array_Type (Typ)
4323 and then Number_Dimensions (Typ) = 1
4324 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4326 Add_One_Interp (N, Typ, Typ);
4329 Get_Next_Interp (I, It);
4332 if Etype (N) = Any_Type then
4333 Error_Msg_N ("expect array type in prefix of slice", N);
4335 end Analyze_Overloaded_Slice;
4337 -- Start of processing for Analyze_Slice
4340 Check_SPARK_Restriction ("slice is not allowed", N);
4345 if Is_Overloaded (P) then
4346 Analyze_Overloaded_Slice;
4349 Array_Type := Etype (P);
4350 Set_Etype (N, Any_Type);
4352 if Is_Access_Type (Array_Type) then
4353 Array_Type := Designated_Type (Array_Type);
4354 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4357 if not Is_Array_Type (Array_Type) then
4358 Wrong_Type (P, Any_Array);
4360 elsif Number_Dimensions (Array_Type) > 1 then
4362 ("type is not one-dimensional array in slice prefix", N);
4365 Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
4367 Wrong_Type (D, Etype (First_Index (Array_Type)));
4370 Set_Etype (N, Array_Type);
4375 -----------------------------
4376 -- Analyze_Type_Conversion --
4377 -----------------------------
4379 procedure Analyze_Type_Conversion (N : Node_Id) is
4380 Expr : constant Node_Id := Expression (N);
4384 -- If Conversion_OK is set, then the Etype is already set, and the
4385 -- only processing required is to analyze the expression. This is
4386 -- used to construct certain "illegal" conversions which are not
4387 -- allowed by Ada semantics, but can be handled OK by Gigi, see
4388 -- Sinfo for further details.
4390 if Conversion_OK (N) then
4395 -- Otherwise full type analysis is required, as well as some semantic
4396 -- checks to make sure the argument of the conversion is appropriate.
4398 Find_Type (Subtype_Mark (N));
4399 T := Entity (Subtype_Mark (N));
4401 Check_Fully_Declared (T, N);
4402 Analyze_Expression (Expr);
4403 Validate_Remote_Type_Type_Conversion (N);
4405 -- Only remaining step is validity checks on the argument. These
4406 -- are skipped if the conversion does not come from the source.
4408 if not Comes_From_Source (N) then
4411 -- If there was an error in a generic unit, no need to replicate the
4412 -- error message. Conversely, constant-folding in the generic may
4413 -- transform the argument of a conversion into a string literal, which
4414 -- is legal. Therefore the following tests are not performed in an
4417 elsif In_Instance then
4420 elsif Nkind (Expr) = N_Null then
4421 Error_Msg_N ("argument of conversion cannot be null", N);
4422 Error_Msg_N ("\use qualified expression instead", N);
4423 Set_Etype (N, Any_Type);
4425 elsif Nkind (Expr) = N_Aggregate then
4426 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4427 Error_Msg_N ("\use qualified expression instead", N);
4429 elsif Nkind (Expr) = N_Allocator then
4430 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4431 Error_Msg_N ("\use qualified expression instead", N);
4433 elsif Nkind (Expr) = N_String_Literal then
4434 Error_Msg_N ("argument of conversion cannot be string literal", N);
4435 Error_Msg_N ("\use qualified expression instead", N);
4437 elsif Nkind (Expr) = N_Character_Literal then
4438 if Ada_Version = Ada_83 then
4441 Error_Msg_N ("argument of conversion cannot be character literal",
4443 Error_Msg_N ("\use qualified expression instead", N);
4446 elsif Nkind (Expr) = N_Attribute_Reference
4448 (Attribute_Name (Expr) = Name_Access or else
4449 Attribute_Name (Expr) = Name_Unchecked_Access or else
4450 Attribute_Name (Expr) = Name_Unrestricted_Access)
4452 Error_Msg_N ("argument of conversion cannot be access", N);
4453 Error_Msg_N ("\use qualified expression instead", N);
4455 end Analyze_Type_Conversion;
4457 ----------------------
4458 -- Analyze_Unary_Op --
4459 ----------------------
4461 procedure Analyze_Unary_Op (N : Node_Id) is
4462 R : constant Node_Id := Right_Opnd (N);
4463 Op_Id : Entity_Id := Entity (N);
4466 Set_Etype (N, Any_Type);
4467 Candidate_Type := Empty;
4469 Analyze_Expression (R);
4471 if Present (Op_Id) then
4472 if Ekind (Op_Id) = E_Operator then
4473 Find_Unary_Types (R, Op_Id, N);
4475 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4479 Op_Id := Get_Name_Entity_Id (Chars (N));
4480 while Present (Op_Id) loop
4481 if Ekind (Op_Id) = E_Operator then
4482 if No (Next_Entity (First_Entity (Op_Id))) then
4483 Find_Unary_Types (R, Op_Id, N);
4486 elsif Is_Overloadable (Op_Id) then
4487 Analyze_User_Defined_Unary_Op (N, Op_Id);
4490 Op_Id := Homonym (Op_Id);
4495 end Analyze_Unary_Op;
4497 ----------------------------------
4498 -- Analyze_Unchecked_Expression --
4499 ----------------------------------
4501 procedure Analyze_Unchecked_Expression (N : Node_Id) is
4503 Analyze (Expression (N), Suppress => All_Checks);
4504 Set_Etype (N, Etype (Expression (N)));
4505 Save_Interps (Expression (N), N);
4506 end Analyze_Unchecked_Expression;
4508 ---------------------------------------
4509 -- Analyze_Unchecked_Type_Conversion --
4510 ---------------------------------------
4512 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
4514 Find_Type (Subtype_Mark (N));
4515 Analyze_Expression (Expression (N));
4516 Set_Etype (N, Entity (Subtype_Mark (N)));
4517 end Analyze_Unchecked_Type_Conversion;
4519 ------------------------------------
4520 -- Analyze_User_Defined_Binary_Op --
4521 ------------------------------------
4523 procedure Analyze_User_Defined_Binary_Op
4528 -- Only do analysis if the operator Comes_From_Source, since otherwise
4529 -- the operator was generated by the expander, and all such operators
4530 -- always refer to the operators in package Standard.
4532 if Comes_From_Source (N) then
4534 F1 : constant Entity_Id := First_Formal (Op_Id);
4535 F2 : constant Entity_Id := Next_Formal (F1);
4538 -- Verify that Op_Id is a visible binary function. Note that since
4539 -- we know Op_Id is overloaded, potentially use visible means use
4540 -- visible for sure (RM 9.4(11)).
4542 if Ekind (Op_Id) = E_Function
4543 and then Present (F2)
4544 and then (Is_Immediately_Visible (Op_Id)
4545 or else Is_Potentially_Use_Visible (Op_Id))
4546 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
4547 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
4549 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4551 -- If the left operand is overloaded, indicate that the
4552 -- current type is a viable candidate. This is redundant
4553 -- in most cases, but for equality and comparison operators
4554 -- where the context does not impose a type on the operands,
4555 -- setting the proper type is necessary to avoid subsequent
4556 -- ambiguities during resolution, when both user-defined and
4557 -- predefined operators may be candidates.
4559 if Is_Overloaded (Left_Opnd (N)) then
4560 Set_Etype (Left_Opnd (N), Etype (F1));
4563 if Debug_Flag_E then
4564 Write_Str ("user defined operator ");
4565 Write_Name (Chars (Op_Id));
4566 Write_Str (" on node ");
4567 Write_Int (Int (N));
4573 end Analyze_User_Defined_Binary_Op;
4575 -----------------------------------
4576 -- Analyze_User_Defined_Unary_Op --
4577 -----------------------------------
4579 procedure Analyze_User_Defined_Unary_Op
4584 -- Only do analysis if the operator Comes_From_Source, since otherwise
4585 -- the operator was generated by the expander, and all such operators
4586 -- always refer to the operators in package Standard.
4588 if Comes_From_Source (N) then
4590 F : constant Entity_Id := First_Formal (Op_Id);
4593 -- Verify that Op_Id is a visible unary function. Note that since
4594 -- we know Op_Id is overloaded, potentially use visible means use
4595 -- visible for sure (RM 9.4(11)).
4597 if Ekind (Op_Id) = E_Function
4598 and then No (Next_Formal (F))
4599 and then (Is_Immediately_Visible (Op_Id)
4600 or else Is_Potentially_Use_Visible (Op_Id))
4601 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
4603 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4607 end Analyze_User_Defined_Unary_Op;
4609 ---------------------------
4610 -- Check_Arithmetic_Pair --
4611 ---------------------------
4613 procedure Check_Arithmetic_Pair
4614 (T1, T2 : Entity_Id;
4618 Op_Name : constant Name_Id := Chars (Op_Id);
4620 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
4621 -- Check whether the fixed-point type Typ has a user-defined operator
4622 -- (multiplication or division) that should hide the corresponding
4623 -- predefined operator. Used to implement Ada 2005 AI-264, to make
4624 -- such operators more visible and therefore useful.
4626 -- If the name of the operation is an expanded name with prefix
4627 -- Standard, the predefined universal fixed operator is available,
4628 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
4630 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
4631 -- Get specific type (i.e. non-universal type if there is one)
4637 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
4638 Bas : constant Entity_Id := Base_Type (Typ);
4644 -- If the universal_fixed operation is given explicitly the rule
4645 -- concerning primitive operations of the type do not apply.
4647 if Nkind (N) = N_Function_Call
4648 and then Nkind (Name (N)) = N_Expanded_Name
4649 and then Entity (Prefix (Name (N))) = Standard_Standard
4654 -- The operation is treated as primitive if it is declared in the
4655 -- same scope as the type, and therefore on the same entity chain.
4657 Ent := Next_Entity (Typ);
4658 while Present (Ent) loop
4659 if Chars (Ent) = Chars (Op) then
4660 F1 := First_Formal (Ent);
4661 F2 := Next_Formal (F1);
4663 -- The operation counts as primitive if either operand or
4664 -- result are of the given base type, and both operands are
4665 -- fixed point types.
4667 if (Base_Type (Etype (F1)) = Bas
4668 and then Is_Fixed_Point_Type (Etype (F2)))
4671 (Base_Type (Etype (F2)) = Bas
4672 and then Is_Fixed_Point_Type (Etype (F1)))
4675 (Base_Type (Etype (Ent)) = Bas
4676 and then Is_Fixed_Point_Type (Etype (F1))
4677 and then Is_Fixed_Point_Type (Etype (F2)))
4693 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
4695 if T1 = Universal_Integer or else T1 = Universal_Real then
4696 return Base_Type (T2);
4698 return Base_Type (T1);
4702 -- Start of processing for Check_Arithmetic_Pair
4705 if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
4707 if Is_Numeric_Type (T1)
4708 and then Is_Numeric_Type (T2)
4709 and then (Covers (T1 => T1, T2 => T2)
4711 Covers (T1 => T2, T2 => T1))
4713 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4716 elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4718 if Is_Fixed_Point_Type (T1)
4719 and then (Is_Fixed_Point_Type (T2)
4720 or else T2 = Universal_Real)
4722 -- If Treat_Fixed_As_Integer is set then the Etype is already set
4723 -- and no further processing is required (this is the case of an
4724 -- operator constructed by Exp_Fixd for a fixed point operation)
4725 -- Otherwise add one interpretation with universal fixed result
4726 -- If the operator is given in functional notation, it comes
4727 -- from source and Fixed_As_Integer cannot apply.
4729 if (Nkind (N) not in N_Op
4730 or else not Treat_Fixed_As_Integer (N))
4732 (not Has_Fixed_Op (T1, Op_Id)
4733 or else Nkind (Parent (N)) = N_Type_Conversion)
4735 Add_One_Interp (N, Op_Id, Universal_Fixed);
4738 elsif Is_Fixed_Point_Type (T2)
4739 and then (Nkind (N) not in N_Op
4740 or else not Treat_Fixed_As_Integer (N))
4741 and then T1 = Universal_Real
4743 (not Has_Fixed_Op (T1, Op_Id)
4744 or else Nkind (Parent (N)) = N_Type_Conversion)
4746 Add_One_Interp (N, Op_Id, Universal_Fixed);
4748 elsif Is_Numeric_Type (T1)
4749 and then Is_Numeric_Type (T2)
4750 and then (Covers (T1 => T1, T2 => T2)
4752 Covers (T1 => T2, T2 => T1))
4754 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4756 elsif Is_Fixed_Point_Type (T1)
4757 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4758 or else T2 = Universal_Integer)
4760 Add_One_Interp (N, Op_Id, T1);
4762 elsif T2 = Universal_Real
4763 and then Base_Type (T1) = Base_Type (Standard_Integer)
4764 and then Op_Name = Name_Op_Multiply
4766 Add_One_Interp (N, Op_Id, Any_Fixed);
4768 elsif T1 = Universal_Real
4769 and then Base_Type (T2) = Base_Type (Standard_Integer)
4771 Add_One_Interp (N, Op_Id, Any_Fixed);
4773 elsif Is_Fixed_Point_Type (T2)
4774 and then (Base_Type (T1) = Base_Type (Standard_Integer)
4775 or else T1 = Universal_Integer)
4776 and then Op_Name = Name_Op_Multiply
4778 Add_One_Interp (N, Op_Id, T2);
4780 elsif T1 = Universal_Real and then T2 = Universal_Integer then
4781 Add_One_Interp (N, Op_Id, T1);
4783 elsif T2 = Universal_Real
4784 and then T1 = Universal_Integer
4785 and then Op_Name = Name_Op_Multiply
4787 Add_One_Interp (N, Op_Id, T2);
4790 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4792 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
4793 -- set does not require any special processing, since the Etype is
4794 -- already set (case of operation constructed by Exp_Fixed).
4796 if Is_Integer_Type (T1)
4797 and then (Covers (T1 => T1, T2 => T2)
4799 Covers (T1 => T2, T2 => T1))
4801 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4804 elsif Op_Name = Name_Op_Expon then
4805 if Is_Numeric_Type (T1)
4806 and then not Is_Fixed_Point_Type (T1)
4807 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4808 or else T2 = Universal_Integer)
4810 Add_One_Interp (N, Op_Id, Base_Type (T1));
4813 else pragma Assert (Nkind (N) in N_Op_Shift);
4815 -- If not one of the predefined operators, the node may be one
4816 -- of the intrinsic functions. Its kind is always specific, and
4817 -- we can use it directly, rather than the name of the operation.
4819 if Is_Integer_Type (T1)
4820 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4821 or else T2 = Universal_Integer)
4823 Add_One_Interp (N, Op_Id, Base_Type (T1));
4826 end Check_Arithmetic_Pair;
4828 -------------------------------
4829 -- Check_Misspelled_Selector --
4830 -------------------------------
4832 procedure Check_Misspelled_Selector
4833 (Prefix : Entity_Id;
4836 Max_Suggestions : constant := 2;
4837 Nr_Of_Suggestions : Natural := 0;
4839 Suggestion_1 : Entity_Id := Empty;
4840 Suggestion_2 : Entity_Id := Empty;
4845 -- All the components of the prefix of selector Sel are matched
4846 -- against Sel and a count is maintained of possible misspellings.
4847 -- When at the end of the analysis there are one or two (not more!)
4848 -- possible misspellings, these misspellings will be suggested as
4849 -- possible correction.
4851 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
4853 -- Concurrent types should be handled as well ???
4858 Comp := First_Entity (Prefix);
4859 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
4860 if Is_Visible_Component (Comp) then
4861 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
4862 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
4864 case Nr_Of_Suggestions is
4865 when 1 => Suggestion_1 := Comp;
4866 when 2 => Suggestion_2 := Comp;
4867 when others => exit;
4872 Comp := Next_Entity (Comp);
4875 -- Report at most two suggestions
4877 if Nr_Of_Suggestions = 1 then
4878 Error_Msg_NE -- CODEFIX
4879 ("\possible misspelling of&", Sel, Suggestion_1);
4881 elsif Nr_Of_Suggestions = 2 then
4882 Error_Msg_Node_2 := Suggestion_2;
4883 Error_Msg_NE -- CODEFIX
4884 ("\possible misspelling of& or&", Sel, Suggestion_1);
4886 end Check_Misspelled_Selector;
4888 ----------------------
4889 -- Defined_In_Scope --
4890 ----------------------
4892 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
4894 S1 : constant Entity_Id := Scope (Base_Type (T));
4897 or else (S1 = System_Aux_Id and then S = Scope (S1));
4898 end Defined_In_Scope;
4904 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
4910 Void_Interp_Seen : Boolean := False;
4913 pragma Warnings (Off, Boolean);
4916 if Ada_Version >= Ada_2005 then
4917 Actual := First_Actual (N);
4918 while Present (Actual) loop
4920 -- Ada 2005 (AI-50217): Post an error in case of premature
4921 -- usage of an entity from the limited view.
4923 if not Analyzed (Etype (Actual))
4924 and then From_With_Type (Etype (Actual))
4926 Error_Msg_Qual_Level := 1;
4928 ("missing with_clause for scope of imported type&",
4929 Actual, Etype (Actual));
4930 Error_Msg_Qual_Level := 0;
4933 Next_Actual (Actual);
4937 -- Analyze each candidate call again, with full error reporting
4941 ("no candidate interpretations match the actuals:!", Nam);
4942 Err_Mode := All_Errors_Mode;
4943 All_Errors_Mode := True;
4945 -- If this is a call to an operation of a concurrent type,
4946 -- the failed interpretations have been removed from the
4947 -- name. Recover them to provide full diagnostics.
4949 if Nkind (Parent (Nam)) = N_Selected_Component then
4950 Set_Entity (Nam, Empty);
4951 New_Nam := New_Copy_Tree (Parent (Nam));
4952 Set_Is_Overloaded (New_Nam, False);
4953 Set_Is_Overloaded (Selector_Name (New_Nam), False);
4954 Set_Parent (New_Nam, Parent (Parent (Nam)));
4955 Analyze_Selected_Component (New_Nam);
4956 Get_First_Interp (Selector_Name (New_Nam), X, It);
4958 Get_First_Interp (Nam, X, It);
4961 while Present (It.Nam) loop
4962 if Etype (It.Nam) = Standard_Void_Type then
4963 Void_Interp_Seen := True;
4966 Analyze_One_Call (N, It.Nam, True, Success);
4967 Get_Next_Interp (X, It);
4970 if Nkind (N) = N_Function_Call then
4971 Get_First_Interp (Nam, X, It);
4972 while Present (It.Nam) loop
4973 if Ekind_In (It.Nam, E_Function, E_Operator) then
4976 Get_Next_Interp (X, It);
4980 -- If all interpretations are procedures, this deserves a
4981 -- more precise message. Ditto if this appears as the prefix
4982 -- of a selected component, which may be a lexical error.
4985 ("\context requires function call, found procedure name", Nam);
4987 if Nkind (Parent (N)) = N_Selected_Component
4988 and then N = Prefix (Parent (N))
4990 Error_Msg_N -- CODEFIX
4991 ("\period should probably be semicolon", Parent (N));
4994 elsif Nkind (N) = N_Procedure_Call_Statement
4995 and then not Void_Interp_Seen
4998 "\function name found in procedure call", Nam);
5001 All_Errors_Mode := Err_Mode;
5004 ---------------------------
5005 -- Find_Arithmetic_Types --
5006 ---------------------------
5008 procedure Find_Arithmetic_Types
5013 Index1 : Interp_Index;
5014 Index2 : Interp_Index;
5018 procedure Check_Right_Argument (T : Entity_Id);
5019 -- Check right operand of operator
5021 --------------------------
5022 -- Check_Right_Argument --
5023 --------------------------
5025 procedure Check_Right_Argument (T : Entity_Id) is
5027 if not Is_Overloaded (R) then
5028 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5030 Get_First_Interp (R, Index2, It2);
5031 while Present (It2.Typ) loop
5032 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5033 Get_Next_Interp (Index2, It2);
5036 end Check_Right_Argument;
5038 -- Start of processing for Find_Arithmetic_Types
5041 if not Is_Overloaded (L) then
5042 Check_Right_Argument (Etype (L));
5045 Get_First_Interp (L, Index1, It1);
5046 while Present (It1.Typ) loop
5047 Check_Right_Argument (It1.Typ);
5048 Get_Next_Interp (Index1, It1);
5052 end Find_Arithmetic_Types;
5054 ------------------------
5055 -- Find_Boolean_Types --
5056 ------------------------
5058 procedure Find_Boolean_Types
5063 Index : Interp_Index;
5066 procedure Check_Numeric_Argument (T : Entity_Id);
5067 -- Special case for logical operations one of whose operands is an
5068 -- integer literal. If both are literal the result is any modular type.
5070 ----------------------------
5071 -- Check_Numeric_Argument --
5072 ----------------------------
5074 procedure Check_Numeric_Argument (T : Entity_Id) is
5076 if T = Universal_Integer then
5077 Add_One_Interp (N, Op_Id, Any_Modular);
5079 elsif Is_Modular_Integer_Type (T) then
5080 Add_One_Interp (N, Op_Id, T);
5082 end Check_Numeric_Argument;
5084 -- Start of processing for Find_Boolean_Types
5087 if not Is_Overloaded (L) then
5088 if Etype (L) = Universal_Integer
5089 or else Etype (L) = Any_Modular
5091 if not Is_Overloaded (R) then
5092 Check_Numeric_Argument (Etype (R));
5095 Get_First_Interp (R, Index, It);
5096 while Present (It.Typ) loop
5097 Check_Numeric_Argument (It.Typ);
5098 Get_Next_Interp (Index, It);
5102 -- If operands are aggregates, we must assume that they may be
5103 -- boolean arrays, and leave disambiguation for the second pass.
5104 -- If only one is an aggregate, verify that the other one has an
5105 -- interpretation as a boolean array
5107 elsif Nkind (L) = N_Aggregate then
5108 if Nkind (R) = N_Aggregate then
5109 Add_One_Interp (N, Op_Id, Etype (L));
5111 elsif not Is_Overloaded (R) then
5112 if Valid_Boolean_Arg (Etype (R)) then
5113 Add_One_Interp (N, Op_Id, Etype (R));
5117 Get_First_Interp (R, Index, It);
5118 while Present (It.Typ) loop
5119 if Valid_Boolean_Arg (It.Typ) then
5120 Add_One_Interp (N, Op_Id, It.Typ);
5123 Get_Next_Interp (Index, It);
5127 elsif Valid_Boolean_Arg (Etype (L))
5128 and then Has_Compatible_Type (R, Etype (L))
5130 Add_One_Interp (N, Op_Id, Etype (L));
5134 Get_First_Interp (L, Index, It);
5135 while Present (It.Typ) loop
5136 if Valid_Boolean_Arg (It.Typ)
5137 and then Has_Compatible_Type (R, It.Typ)
5139 Add_One_Interp (N, Op_Id, It.Typ);
5142 Get_Next_Interp (Index, It);
5145 end Find_Boolean_Types;
5147 ---------------------------
5148 -- Find_Comparison_Types --
5149 ---------------------------
5151 procedure Find_Comparison_Types
5156 Index : Interp_Index;
5158 Found : Boolean := False;
5161 Scop : Entity_Id := Empty;
5163 procedure Try_One_Interp (T1 : Entity_Id);
5164 -- Routine to try one proposed interpretation. Note that the context
5165 -- of the operator plays no role in resolving the arguments, so that
5166 -- if there is more than one interpretation of the operands that is
5167 -- compatible with comparison, the operation is ambiguous.
5169 --------------------
5170 -- Try_One_Interp --
5171 --------------------
5173 procedure Try_One_Interp (T1 : Entity_Id) is
5176 -- If the operator is an expanded name, then the type of the operand
5177 -- must be defined in the corresponding scope. If the type is
5178 -- universal, the context will impose the correct type.
5181 and then not Defined_In_Scope (T1, Scop)
5182 and then T1 /= Universal_Integer
5183 and then T1 /= Universal_Real
5184 and then T1 /= Any_String
5185 and then T1 /= Any_Composite
5190 if Valid_Comparison_Arg (T1)
5191 and then Has_Compatible_Type (R, T1)
5194 and then Base_Type (T1) /= Base_Type (T_F)
5196 It := Disambiguate (L, I_F, Index, Any_Type);
5198 if It = No_Interp then
5199 Ambiguous_Operands (N);
5200 Set_Etype (L, Any_Type);
5214 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5219 -- Start of processing for Find_Comparison_Types
5222 -- If left operand is aggregate, the right operand has to
5223 -- provide a usable type for it.
5225 if Nkind (L) = N_Aggregate
5226 and then Nkind (R) /= N_Aggregate
5228 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5232 if Nkind (N) = N_Function_Call
5233 and then Nkind (Name (N)) = N_Expanded_Name
5235 Scop := Entity (Prefix (Name (N)));
5237 -- The prefix may be a package renaming, and the subsequent test
5238 -- requires the original package.
5240 if Ekind (Scop) = E_Package
5241 and then Present (Renamed_Entity (Scop))
5243 Scop := Renamed_Entity (Scop);
5244 Set_Entity (Prefix (Name (N)), Scop);
5248 if not Is_Overloaded (L) then
5249 Try_One_Interp (Etype (L));
5252 Get_First_Interp (L, Index, It);
5253 while Present (It.Typ) loop
5254 Try_One_Interp (It.Typ);
5255 Get_Next_Interp (Index, It);
5258 end Find_Comparison_Types;
5260 ----------------------------------------
5261 -- Find_Non_Universal_Interpretations --
5262 ----------------------------------------
5264 procedure Find_Non_Universal_Interpretations
5270 Index : Interp_Index;
5274 if T1 = Universal_Integer
5275 or else T1 = Universal_Real
5277 if not Is_Overloaded (R) then
5279 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5281 Get_First_Interp (R, Index, It);
5282 while Present (It.Typ) loop
5283 if Covers (It.Typ, T1) then
5285 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5288 Get_Next_Interp (Index, It);
5292 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5294 end Find_Non_Universal_Interpretations;
5296 ------------------------------
5297 -- Find_Concatenation_Types --
5298 ------------------------------
5300 procedure Find_Concatenation_Types
5305 Op_Type : constant Entity_Id := Etype (Op_Id);
5308 if Is_Array_Type (Op_Type)
5309 and then not Is_Limited_Type (Op_Type)
5311 and then (Has_Compatible_Type (L, Op_Type)
5313 Has_Compatible_Type (L, Component_Type (Op_Type)))
5315 and then (Has_Compatible_Type (R, Op_Type)
5317 Has_Compatible_Type (R, Component_Type (Op_Type)))
5319 Add_One_Interp (N, Op_Id, Op_Type);
5321 end Find_Concatenation_Types;
5323 -------------------------
5324 -- Find_Equality_Types --
5325 -------------------------
5327 procedure Find_Equality_Types
5332 Index : Interp_Index;
5334 Found : Boolean := False;
5337 Scop : Entity_Id := Empty;
5339 procedure Try_One_Interp (T1 : Entity_Id);
5340 -- The context of the equality operator plays no role in resolving the
5341 -- arguments, so that if there is more than one interpretation of the
5342 -- operands that is compatible with equality, the construct is ambiguous
5343 -- and an error can be emitted now, after trying to disambiguate, i.e.
5344 -- applying preference rules.
5346 --------------------
5347 -- Try_One_Interp --
5348 --------------------
5350 procedure Try_One_Interp (T1 : Entity_Id) is
5351 Bas : constant Entity_Id := Base_Type (T1);
5354 -- If the operator is an expanded name, then the type of the operand
5355 -- must be defined in the corresponding scope. If the type is
5356 -- universal, the context will impose the correct type. An anonymous
5357 -- type for a 'Access reference is also universal in this sense, as
5358 -- the actual type is obtained from context.
5359 -- In Ada 2005, the equality operator for anonymous access types
5360 -- is declared in Standard, and preference rules apply to it.
5362 if Present (Scop) then
5363 if Defined_In_Scope (T1, Scop)
5364 or else T1 = Universal_Integer
5365 or else T1 = Universal_Real
5366 or else T1 = Any_Access
5367 or else T1 = Any_String
5368 or else T1 = Any_Composite
5369 or else (Ekind (T1) = E_Access_Subprogram_Type
5370 and then not Comes_From_Source (T1))
5374 elsif Ekind (T1) = E_Anonymous_Access_Type
5375 and then Scop = Standard_Standard
5380 -- The scope does not contain an operator for the type
5385 -- If we have infix notation, the operator must be usable.
5386 -- Within an instance, if the type is already established we
5387 -- know it is correct.
5388 -- In Ada 2005, the equality on anonymous access types is declared
5389 -- in Standard, and is always visible.
5391 elsif In_Open_Scopes (Scope (Bas))
5392 or else Is_Potentially_Use_Visible (Bas)
5393 or else In_Use (Bas)
5394 or else (In_Use (Scope (Bas))
5395 and then not Is_Hidden (Bas))
5396 or else (In_Instance
5397 and then First_Subtype (T1) = First_Subtype (Etype (R)))
5398 or else Ekind (T1) = E_Anonymous_Access_Type
5403 -- Save candidate type for subsequent error message, if any
5405 if not Is_Limited_Type (T1) then
5406 Candidate_Type := T1;
5412 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5413 -- Do not allow anonymous access types in equality operators.
5415 if Ada_Version < Ada_2005
5416 and then Ekind (T1) = E_Anonymous_Access_Type
5421 if T1 /= Standard_Void_Type
5422 and then not Is_Limited_Type (T1)
5423 and then not Is_Limited_Composite (T1)
5424 and then Has_Compatible_Type (R, T1)
5427 and then Base_Type (T1) /= Base_Type (T_F)
5429 It := Disambiguate (L, I_F, Index, Any_Type);
5431 if It = No_Interp then
5432 Ambiguous_Operands (N);
5433 Set_Etype (L, Any_Type);
5446 if not Analyzed (L) then
5450 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5452 -- Case of operator was not visible, Etype still set to Any_Type
5454 if Etype (N) = Any_Type then
5458 elsif Scop = Standard_Standard
5459 and then Ekind (T1) = E_Anonymous_Access_Type
5465 -- Start of processing for Find_Equality_Types
5468 -- If left operand is aggregate, the right operand has to
5469 -- provide a usable type for it.
5471 if Nkind (L) = N_Aggregate
5472 and then Nkind (R) /= N_Aggregate
5474 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5478 if Nkind (N) = N_Function_Call
5479 and then Nkind (Name (N)) = N_Expanded_Name
5481 Scop := Entity (Prefix (Name (N)));
5483 -- The prefix may be a package renaming, and the subsequent test
5484 -- requires the original package.
5486 if Ekind (Scop) = E_Package
5487 and then Present (Renamed_Entity (Scop))
5489 Scop := Renamed_Entity (Scop);
5490 Set_Entity (Prefix (Name (N)), Scop);
5494 if not Is_Overloaded (L) then
5495 Try_One_Interp (Etype (L));
5498 Get_First_Interp (L, Index, It);
5499 while Present (It.Typ) loop
5500 Try_One_Interp (It.Typ);
5501 Get_Next_Interp (Index, It);
5504 end Find_Equality_Types;
5506 -------------------------
5507 -- Find_Negation_Types --
5508 -------------------------
5510 procedure Find_Negation_Types
5515 Index : Interp_Index;
5519 if not Is_Overloaded (R) then
5520 if Etype (R) = Universal_Integer then
5521 Add_One_Interp (N, Op_Id, Any_Modular);
5522 elsif Valid_Boolean_Arg (Etype (R)) then
5523 Add_One_Interp (N, Op_Id, Etype (R));
5527 Get_First_Interp (R, Index, It);
5528 while Present (It.Typ) loop
5529 if Valid_Boolean_Arg (It.Typ) then
5530 Add_One_Interp (N, Op_Id, It.Typ);
5533 Get_Next_Interp (Index, It);
5536 end Find_Negation_Types;
5538 ------------------------------
5539 -- Find_Primitive_Operation --
5540 ------------------------------
5542 function Find_Primitive_Operation (N : Node_Id) return Boolean is
5543 Obj : constant Node_Id := Prefix (N);
5544 Op : constant Node_Id := Selector_Name (N);
5551 Set_Etype (Op, Any_Type);
5553 if Is_Access_Type (Etype (Obj)) then
5554 Typ := Designated_Type (Etype (Obj));
5559 if Is_Class_Wide_Type (Typ) then
5560 Typ := Root_Type (Typ);
5563 Prims := Primitive_Operations (Typ);
5565 Prim := First_Elmt (Prims);
5566 while Present (Prim) loop
5567 if Chars (Node (Prim)) = Chars (Op) then
5568 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
5569 Set_Etype (N, Etype (Node (Prim)));
5575 -- Now look for class-wide operations of the type or any of its
5576 -- ancestors by iterating over the homonyms of the selector.
5579 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
5583 Hom := Current_Entity (Op);
5584 while Present (Hom) loop
5585 if (Ekind (Hom) = E_Procedure
5587 Ekind (Hom) = E_Function)
5588 and then Scope (Hom) = Scope (Typ)
5589 and then Present (First_Formal (Hom))
5591 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
5593 (Is_Access_Type (Etype (First_Formal (Hom)))
5595 Ekind (Etype (First_Formal (Hom))) =
5596 E_Anonymous_Access_Type
5599 (Designated_Type (Etype (First_Formal (Hom)))) =
5602 Add_One_Interp (Op, Hom, Etype (Hom));
5603 Set_Etype (N, Etype (Hom));
5606 Hom := Homonym (Hom);
5610 return Etype (Op) /= Any_Type;
5611 end Find_Primitive_Operation;
5613 ----------------------
5614 -- Find_Unary_Types --
5615 ----------------------
5617 procedure Find_Unary_Types
5622 Index : Interp_Index;
5626 if not Is_Overloaded (R) then
5627 if Is_Numeric_Type (Etype (R)) then
5628 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
5632 Get_First_Interp (R, Index, It);
5633 while Present (It.Typ) loop
5634 if Is_Numeric_Type (It.Typ) then
5635 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
5638 Get_Next_Interp (Index, It);
5641 end Find_Unary_Types;
5647 function Junk_Operand (N : Node_Id) return Boolean is
5651 if Error_Posted (N) then
5655 -- Get entity to be tested
5657 if Is_Entity_Name (N)
5658 and then Present (Entity (N))
5662 -- An odd case, a procedure name gets converted to a very peculiar
5663 -- function call, and here is where we detect this happening.
5665 elsif Nkind (N) = N_Function_Call
5666 and then Is_Entity_Name (Name (N))
5667 and then Present (Entity (Name (N)))
5671 -- Another odd case, there are at least some cases of selected
5672 -- components where the selected component is not marked as having
5673 -- an entity, even though the selector does have an entity
5675 elsif Nkind (N) = N_Selected_Component
5676 and then Present (Entity (Selector_Name (N)))
5678 Enode := Selector_Name (N);
5684 -- Now test the entity we got to see if it is a bad case
5686 case Ekind (Entity (Enode)) is
5690 ("package name cannot be used as operand", Enode);
5692 when Generic_Unit_Kind =>
5694 ("generic unit name cannot be used as operand", Enode);
5698 ("subtype name cannot be used as operand", Enode);
5702 ("entry name cannot be used as operand", Enode);
5706 ("procedure name cannot be used as operand", Enode);
5710 ("exception name cannot be used as operand", Enode);
5712 when E_Block | E_Label | E_Loop =>
5714 ("label name cannot be used as operand", Enode);
5724 --------------------
5725 -- Operator_Check --
5726 --------------------
5728 procedure Operator_Check (N : Node_Id) is
5730 Remove_Abstract_Operations (N);
5732 -- Test for case of no interpretation found for operator
5734 if Etype (N) = Any_Type then
5738 Op_Id : Entity_Id := Empty;
5741 R := Right_Opnd (N);
5743 if Nkind (N) in N_Binary_Op then
5749 -- If either operand has no type, then don't complain further,
5750 -- since this simply means that we have a propagated error.
5753 or else Etype (R) = Any_Type
5754 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5758 -- We explicitly check for the case of concatenation of component
5759 -- with component to avoid reporting spurious matching array types
5760 -- that might happen to be lurking in distant packages (such as
5761 -- run-time packages). This also prevents inconsistencies in the
5762 -- messages for certain ACVC B tests, which can vary depending on
5763 -- types declared in run-time interfaces. Another improvement when
5764 -- aggregates are present is to look for a well-typed operand.
5766 elsif Present (Candidate_Type)
5767 and then (Nkind (N) /= N_Op_Concat
5768 or else Is_Array_Type (Etype (L))
5769 or else Is_Array_Type (Etype (R)))
5771 if Nkind (N) = N_Op_Concat then
5772 if Etype (L) /= Any_Composite
5773 and then Is_Array_Type (Etype (L))
5775 Candidate_Type := Etype (L);
5777 elsif Etype (R) /= Any_Composite
5778 and then Is_Array_Type (Etype (R))
5780 Candidate_Type := Etype (R);
5784 Error_Msg_NE -- CODEFIX
5785 ("operator for} is not directly visible!",
5786 N, First_Subtype (Candidate_Type));
5789 U : constant Node_Id :=
5790 Cunit (Get_Source_Unit (Candidate_Type));
5792 if Unit_Is_Visible (U) then
5793 Error_Msg_N -- CODEFIX
5794 ("use clause would make operation legal!", N);
5796 Error_Msg_NE -- CODEFIX
5797 ("add with_clause and use_clause for&!",
5798 N, Defining_Entity (Unit (U)));
5803 -- If either operand is a junk operand (e.g. package name), then
5804 -- post appropriate error messages, but do not complain further.
5806 -- Note that the use of OR in this test instead of OR ELSE is
5807 -- quite deliberate, we may as well check both operands in the
5808 -- binary operator case.
5810 elsif Junk_Operand (R)
5811 or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
5815 -- If we have a logical operator, one of whose operands is
5816 -- Boolean, then we know that the other operand cannot resolve to
5817 -- Boolean (since we got no interpretations), but in that case we
5818 -- pretty much know that the other operand should be Boolean, so
5819 -- resolve it that way (generating an error)
5821 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
5822 if Etype (L) = Standard_Boolean then
5823 Resolve (R, Standard_Boolean);
5825 elsif Etype (R) = Standard_Boolean then
5826 Resolve (L, Standard_Boolean);
5830 -- For an arithmetic operator or comparison operator, if one
5831 -- of the operands is numeric, then we know the other operand
5832 -- is not the same numeric type. If it is a non-numeric type,
5833 -- then probably it is intended to match the other operand.
5835 elsif Nkind_In (N, N_Op_Add,
5841 Nkind_In (N, N_Op_Lt,
5847 if Is_Numeric_Type (Etype (L))
5848 and then not Is_Numeric_Type (Etype (R))
5850 Resolve (R, Etype (L));
5853 elsif Is_Numeric_Type (Etype (R))
5854 and then not Is_Numeric_Type (Etype (L))
5856 Resolve (L, Etype (R));
5860 -- Comparisons on A'Access are common enough to deserve a
5863 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
5864 and then Ekind (Etype (L)) = E_Access_Attribute_Type
5865 and then Ekind (Etype (R)) = E_Access_Attribute_Type
5868 ("two access attributes cannot be compared directly", N);
5870 ("\use qualified expression for one of the operands",
5874 -- Another one for C programmers
5876 elsif Nkind (N) = N_Op_Concat
5877 and then Valid_Boolean_Arg (Etype (L))
5878 and then Valid_Boolean_Arg (Etype (R))
5880 Error_Msg_N ("invalid operands for concatenation", N);
5881 Error_Msg_N -- CODEFIX
5882 ("\maybe AND was meant", N);
5885 -- A special case for comparison of access parameter with null
5887 elsif Nkind (N) = N_Op_Eq
5888 and then Is_Entity_Name (L)
5889 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
5890 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
5892 and then Nkind (R) = N_Null
5894 Error_Msg_N ("access parameter is not allowed to be null", L);
5895 Error_Msg_N ("\(call would raise Constraint_Error)", L);
5898 -- Another special case for exponentiation, where the right
5899 -- operand must be Natural, independently of the base.
5901 elsif Nkind (N) = N_Op_Expon
5902 and then Is_Numeric_Type (Etype (L))
5903 and then not Is_Overloaded (R)
5905 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
5906 and then Base_Type (Etype (R)) /= Universal_Integer
5909 ("exponent must be of type Natural, found}", R, Etype (R));
5913 -- If we fall through then just give general message. Note that in
5914 -- the following messages, if the operand is overloaded we choose
5915 -- an arbitrary type to complain about, but that is probably more
5916 -- useful than not giving a type at all.
5918 if Nkind (N) in N_Unary_Op then
5919 Error_Msg_Node_2 := Etype (R);
5920 Error_Msg_N ("operator& not defined for}", N);
5924 if Nkind (N) in N_Binary_Op then
5925 if not Is_Overloaded (L)
5926 and then not Is_Overloaded (R)
5927 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
5929 Error_Msg_Node_2 := First_Subtype (Etype (R));
5930 Error_Msg_N ("there is no applicable operator& for}", N);
5933 -- Another attempt to find a fix: one of the candidate
5934 -- interpretations may not be use-visible. This has
5935 -- already been checked for predefined operators, so
5936 -- we examine only user-defined functions.
5938 Op_Id := Get_Name_Entity_Id (Chars (N));
5940 while Present (Op_Id) loop
5941 if Ekind (Op_Id) /= E_Operator
5942 and then Is_Overloadable (Op_Id)
5944 if not Is_Immediately_Visible (Op_Id)
5945 and then not In_Use (Scope (Op_Id))
5946 and then not Is_Abstract_Subprogram (Op_Id)
5947 and then not Is_Hidden (Op_Id)
5948 and then Ekind (Scope (Op_Id)) = E_Package
5951 (L, Etype (First_Formal (Op_Id)))
5953 (Next_Formal (First_Formal (Op_Id)))
5957 Etype (Next_Formal (First_Formal (Op_Id))))
5960 ("No legal interpretation for operator&", N);
5962 ("\use clause on& would make operation legal",
5968 Op_Id := Homonym (Op_Id);
5972 Error_Msg_N ("invalid operand types for operator&", N);
5974 if Nkind (N) /= N_Op_Concat then
5975 Error_Msg_NE ("\left operand has}!", N, Etype (L));
5976 Error_Msg_NE ("\right operand has}!", N, Etype (R));
5986 -----------------------------------------
5987 -- Process_Implicit_Dereference_Prefix --
5988 -----------------------------------------
5990 function Process_Implicit_Dereference_Prefix
5992 P : Entity_Id) return Entity_Id
5995 Typ : constant Entity_Id := Designated_Type (Etype (P));
5999 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6001 -- We create a dummy reference to E to ensure that the reference
6002 -- is not considered as part of an assignment (an implicit
6003 -- dereference can never assign to its prefix). The Comes_From_Source
6004 -- attribute needs to be propagated for accurate warnings.
6006 Ref := New_Reference_To (E, Sloc (P));
6007 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6008 Generate_Reference (E, Ref);
6011 -- An implicit dereference is a legal occurrence of an
6012 -- incomplete type imported through a limited_with clause,
6013 -- if the full view is visible.
6015 if From_With_Type (Typ)
6016 and then not From_With_Type (Scope (Typ))
6018 (Is_Immediately_Visible (Scope (Typ))
6020 (Is_Child_Unit (Scope (Typ))
6021 and then Is_Visible_Child_Unit (Scope (Typ))))
6023 return Available_View (Typ);
6028 end Process_Implicit_Dereference_Prefix;
6030 --------------------------------
6031 -- Remove_Abstract_Operations --
6032 --------------------------------
6034 procedure Remove_Abstract_Operations (N : Node_Id) is
6035 Abstract_Op : Entity_Id := Empty;
6036 Address_Kludge : Boolean := False;
6040 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6041 -- activate this if either extensions are enabled, or if the abstract
6042 -- operation in question comes from a predefined file. This latter test
6043 -- allows us to use abstract to make operations invisible to users. In
6044 -- particular, if type Address is non-private and abstract subprograms
6045 -- are used to hide its operators, they will be truly hidden.
6047 type Operand_Position is (First_Op, Second_Op);
6048 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6050 procedure Remove_Address_Interpretations (Op : Operand_Position);
6051 -- Ambiguities may arise when the operands are literal and the address
6052 -- operations in s-auxdec are visible. In that case, remove the
6053 -- interpretation of a literal as Address, to retain the semantics of
6054 -- Address as a private type.
6056 ------------------------------------
6057 -- Remove_Address_Interpretations --
6058 ------------------------------------
6060 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6064 if Is_Overloaded (N) then
6065 Get_First_Interp (N, I, It);
6066 while Present (It.Nam) loop
6067 Formal := First_Entity (It.Nam);
6069 if Op = Second_Op then
6070 Formal := Next_Entity (Formal);
6073 if Is_Descendent_Of_Address (Etype (Formal)) then
6074 Address_Kludge := True;
6078 Get_Next_Interp (I, It);
6081 end Remove_Address_Interpretations;
6083 -- Start of processing for Remove_Abstract_Operations
6086 if Is_Overloaded (N) then
6087 Get_First_Interp (N, I, It);
6089 while Present (It.Nam) loop
6090 if Is_Overloadable (It.Nam)
6091 and then Is_Abstract_Subprogram (It.Nam)
6092 and then not Is_Dispatching_Operation (It.Nam)
6094 Abstract_Op := It.Nam;
6096 if Is_Descendent_Of_Address (It.Typ) then
6097 Address_Kludge := True;
6101 -- In Ada 2005, this operation does not participate in Overload
6102 -- resolution. If the operation is defined in a predefined
6103 -- unit, it is one of the operations declared abstract in some
6104 -- variants of System, and it must be removed as well.
6106 elsif Ada_Version >= Ada_2005
6107 or else Is_Predefined_File_Name
6108 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6115 Get_Next_Interp (I, It);
6118 if No (Abstract_Op) then
6120 -- If some interpretation yields an integer type, it is still
6121 -- possible that there are address interpretations. Remove them
6122 -- if one operand is a literal, to avoid spurious ambiguities
6123 -- on systems where Address is a visible integer type.
6125 if Is_Overloaded (N)
6126 and then Nkind (N) in N_Op
6127 and then Is_Integer_Type (Etype (N))
6129 if Nkind (N) in N_Binary_Op then
6130 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6131 Remove_Address_Interpretations (Second_Op);
6133 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6134 Remove_Address_Interpretations (First_Op);
6139 elsif Nkind (N) in N_Op then
6141 -- Remove interpretations that treat literals as addresses. This
6142 -- is never appropriate, even when Address is defined as a visible
6143 -- Integer type. The reason is that we would really prefer Address
6144 -- to behave as a private type, even in this case, which is there
6145 -- only to accommodate oddities of VMS address sizes. If Address
6146 -- is a visible integer type, we get lots of overload ambiguities.
6148 if Nkind (N) in N_Binary_Op then
6150 U1 : constant Boolean :=
6151 Present (Universal_Interpretation (Right_Opnd (N)));
6152 U2 : constant Boolean :=
6153 Present (Universal_Interpretation (Left_Opnd (N)));
6157 Remove_Address_Interpretations (Second_Op);
6161 Remove_Address_Interpretations (First_Op);
6164 if not (U1 and U2) then
6166 -- Remove corresponding predefined operator, which is
6167 -- always added to the overload set.
6169 Get_First_Interp (N, I, It);
6170 while Present (It.Nam) loop
6171 if Scope (It.Nam) = Standard_Standard
6172 and then Base_Type (It.Typ) =
6173 Base_Type (Etype (Abstract_Op))
6178 Get_Next_Interp (I, It);
6181 elsif Is_Overloaded (N)
6182 and then Present (Univ_Type)
6184 -- If both operands have a universal interpretation,
6185 -- it is still necessary to remove interpretations that
6186 -- yield Address. Any remaining ambiguities will be
6187 -- removed in Disambiguate.
6189 Get_First_Interp (N, I, It);
6190 while Present (It.Nam) loop
6191 if Is_Descendent_Of_Address (It.Typ) then
6194 elsif not Is_Type (It.Nam) then
6195 Set_Entity (N, It.Nam);
6198 Get_Next_Interp (I, It);
6204 elsif Nkind (N) = N_Function_Call
6206 (Nkind (Name (N)) = N_Operator_Symbol
6208 (Nkind (Name (N)) = N_Expanded_Name
6210 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6214 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6215 U1 : constant Boolean :=
6216 Present (Universal_Interpretation (Arg1));
6217 U2 : constant Boolean :=
6218 Present (Next (Arg1)) and then
6219 Present (Universal_Interpretation (Next (Arg1)));
6223 Remove_Address_Interpretations (First_Op);
6227 Remove_Address_Interpretations (Second_Op);
6230 if not (U1 and U2) then
6231 Get_First_Interp (N, I, It);
6232 while Present (It.Nam) loop
6233 if Scope (It.Nam) = Standard_Standard
6234 and then It.Typ = Base_Type (Etype (Abstract_Op))
6239 Get_Next_Interp (I, It);
6245 -- If the removal has left no valid interpretations, emit an error
6246 -- message now and label node as illegal.
6248 if Present (Abstract_Op) then
6249 Get_First_Interp (N, I, It);
6253 -- Removal of abstract operation left no viable candidate
6255 Set_Etype (N, Any_Type);
6256 Error_Msg_Sloc := Sloc (Abstract_Op);
6258 ("cannot call abstract operation& declared#", N, Abstract_Op);
6260 -- In Ada 2005, an abstract operation may disable predefined
6261 -- operators. Since the context is not yet known, we mark the
6262 -- predefined operators as potentially hidden. Do not include
6263 -- predefined operators when addresses are involved since this
6264 -- case is handled separately.
6266 elsif Ada_Version >= Ada_2005
6267 and then not Address_Kludge
6269 while Present (It.Nam) loop
6270 if Is_Numeric_Type (It.Typ)
6271 and then Scope (It.Typ) = Standard_Standard
6273 Set_Abstract_Op (I, Abstract_Op);
6276 Get_Next_Interp (I, It);
6281 end Remove_Abstract_Operations;
6283 ----------------------------
6284 -- Try_Container_Indexing --
6285 ----------------------------
6287 function Try_Container_Indexing
6290 Expr : Node_Id) return Boolean
6292 Loc : constant Source_Ptr := Sloc (N);
6295 Func_Name : Node_Id;
6302 -- Check whether type has a specified indexing aspect.
6306 Ritem := First_Rep_Item (Etype (Prefix));
6308 while Present (Ritem) loop
6309 if Nkind (Ritem) = N_Aspect_Specification then
6311 -- Prefer Variable_Indexing, but will settle for Constant.
6313 if Get_Aspect_Id (Chars (Identifier (Ritem))) =
6314 Aspect_Constant_Indexing
6316 Func_Name := Expression (Ritem);
6318 elsif Get_Aspect_Id (Chars (Identifier (Ritem))) =
6319 Aspect_Variable_Indexing
6321 Func_Name := Expression (Ritem);
6326 Next_Rep_Item (Ritem);
6329 -- If aspect does not exist the expression is illegal. Error is
6330 -- diagnosed in caller.
6332 if No (Func_Name) then
6337 and then not Is_Variable (Prefix)
6339 Error_Msg_N ("Variable indexing cannot be applied to a constant", N);
6342 if not Is_Overloaded (Func_Name) then
6343 Func := Entity (Func_Name);
6344 Indexing := Make_Function_Call (Loc,
6345 Name => New_Occurrence_Of (Func, Loc),
6346 Parameter_Associations =>
6347 New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6348 Rewrite (N, Indexing);
6351 -- The return type of the indexing function is a reference type, so
6352 -- add the dereference as a possible interpretation.
6354 Disc := First_Discriminant (Etype (Func));
6355 while Present (Disc) loop
6356 if Has_Implicit_Dereference (Disc) then
6357 Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
6361 Next_Discriminant (Disc);
6365 Indexing := Make_Function_Call (Loc,
6366 Name => Make_Identifier (Loc, Chars (Func_Name)),
6367 Parameter_Associations =>
6368 New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6370 Rewrite (N, Indexing);
6378 Get_First_Interp (Func_Name, I, It);
6379 Set_Etype (N, Any_Type);
6380 while Present (It.Nam) loop
6381 Analyze_One_Call (N, It.Nam, False, Success);
6383 Set_Etype (Name (N), It.Typ);
6385 -- Add implicit dereference interpretation.
6387 Disc := First_Discriminant (Etype (It.Nam));
6389 while Present (Disc) loop
6390 if Has_Implicit_Dereference (Disc) then
6392 (N, Disc, Designated_Type (Etype (Disc)));
6396 Next_Discriminant (Disc);
6399 Get_Next_Interp (I, It);
6405 end Try_Container_Indexing;
6407 -----------------------
6408 -- Try_Indirect_Call --
6409 -----------------------
6411 function Try_Indirect_Call
6414 Typ : Entity_Id) return Boolean
6420 pragma Warnings (Off, Call_OK);
6423 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
6425 Actual := First_Actual (N);
6426 Formal := First_Formal (Designated_Type (Typ));
6427 while Present (Actual) and then Present (Formal) loop
6428 if not Has_Compatible_Type (Actual, Etype (Formal)) then
6433 Next_Formal (Formal);
6436 if No (Actual) and then No (Formal) then
6437 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
6439 -- Nam is a candidate interpretation for the name in the call,
6440 -- if it is not an indirect call.
6442 if not Is_Type (Nam)
6443 and then Is_Entity_Name (Name (N))
6445 Set_Entity (Name (N), Nam);
6452 end Try_Indirect_Call;
6454 ----------------------
6455 -- Try_Indexed_Call --
6456 ----------------------
6458 function Try_Indexed_Call
6462 Skip_First : Boolean) return Boolean
6464 Loc : constant Source_Ptr := Sloc (N);
6465 Actuals : constant List_Id := Parameter_Associations (N);
6470 Actual := First (Actuals);
6472 -- If the call was originally written in prefix form, skip the first
6473 -- actual, which is obviously not defaulted.
6479 Index := First_Index (Typ);
6480 while Present (Actual) and then Present (Index) loop
6482 -- If the parameter list has a named association, the expression
6483 -- is definitely a call and not an indexed component.
6485 if Nkind (Actual) = N_Parameter_Association then
6489 if Is_Entity_Name (Actual)
6490 and then Is_Type (Entity (Actual))
6491 and then No (Next (Actual))
6493 -- A single actual that is a type name indicates a slice if the
6494 -- type is discrete, and an error otherwise.
6496 if Is_Discrete_Type (Entity (Actual)) then
6500 Make_Function_Call (Loc,
6501 Name => Relocate_Node (Name (N))),
6503 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
6508 Error_Msg_N ("invalid use of type in expression", Actual);
6509 Set_Etype (N, Any_Type);
6514 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
6522 if No (Actual) and then No (Index) then
6523 Add_One_Interp (N, Nam, Component_Type (Typ));
6525 -- Nam is a candidate interpretation for the name in the call,
6526 -- if it is not an indirect call.
6528 if not Is_Type (Nam)
6529 and then Is_Entity_Name (Name (N))
6531 Set_Entity (Name (N), Nam);
6538 end Try_Indexed_Call;
6540 --------------------------
6541 -- Try_Object_Operation --
6542 --------------------------
6544 function Try_Object_Operation (N : Node_Id) return Boolean is
6545 K : constant Node_Kind := Nkind (Parent (N));
6546 Is_Subprg_Call : constant Boolean := Nkind_In
6547 (K, N_Procedure_Call_Statement,
6549 Loc : constant Source_Ptr := Sloc (N);
6550 Obj : constant Node_Id := Prefix (N);
6552 Subprog : constant Node_Id :=
6553 Make_Identifier (Sloc (Selector_Name (N)),
6554 Chars => Chars (Selector_Name (N)));
6555 -- Identifier on which possible interpretations will be collected
6557 Report_Error : Boolean := False;
6558 -- If no candidate interpretation matches the context, redo the
6559 -- analysis with error enabled to provide additional information.
6562 Candidate : Entity_Id := Empty;
6563 New_Call_Node : Node_Id := Empty;
6564 Node_To_Replace : Node_Id;
6565 Obj_Type : Entity_Id := Etype (Obj);
6566 Success : Boolean := False;
6568 function Valid_Candidate
6571 Subp : Entity_Id) return Entity_Id;
6572 -- If the subprogram is a valid interpretation, record it, and add
6573 -- to the list of interpretations of Subprog.
6575 procedure Complete_Object_Operation
6576 (Call_Node : Node_Id;
6577 Node_To_Replace : Node_Id);
6578 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
6579 -- Call_Node, insert the object (or its dereference) as the first actual
6580 -- in the call, and complete the analysis of the call.
6582 procedure Report_Ambiguity (Op : Entity_Id);
6583 -- If a prefixed procedure call is ambiguous, indicate whether the
6584 -- call includes an implicit dereference or an implicit 'Access.
6586 procedure Transform_Object_Operation
6587 (Call_Node : out Node_Id;
6588 Node_To_Replace : out Node_Id);
6589 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
6590 -- Call_Node is the resulting subprogram call, Node_To_Replace is
6591 -- either N or the parent of N, and Subprog is a reference to the
6592 -- subprogram we are trying to match.
6594 function Try_Class_Wide_Operation
6595 (Call_Node : Node_Id;
6596 Node_To_Replace : Node_Id) return Boolean;
6597 -- Traverse all ancestor types looking for a class-wide subprogram
6598 -- for which the current operation is a valid non-dispatching call.
6600 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
6601 -- If prefix is overloaded, its interpretation may include different
6602 -- tagged types, and we must examine the primitive operations and
6603 -- the class-wide operations of each in order to find candidate
6604 -- interpretations for the call as a whole.
6606 function Try_Primitive_Operation
6607 (Call_Node : Node_Id;
6608 Node_To_Replace : Node_Id) return Boolean;
6609 -- Traverse the list of primitive subprograms looking for a dispatching
6610 -- operation for which the current node is a valid call .
6612 ---------------------
6613 -- Valid_Candidate --
6614 ---------------------
6616 function Valid_Candidate
6619 Subp : Entity_Id) return Entity_Id
6621 Arr_Type : Entity_Id;
6622 Comp_Type : Entity_Id;
6625 -- If the subprogram is a valid interpretation, record it in global
6626 -- variable Subprog, to collect all possible overloadings.
6629 if Subp /= Entity (Subprog) then
6630 Add_One_Interp (Subprog, Subp, Etype (Subp));
6634 -- If the call may be an indexed call, retrieve component type of
6635 -- resulting expression, and add possible interpretation.
6640 if Nkind (Call) = N_Function_Call
6641 and then Nkind (Parent (N)) = N_Indexed_Component
6642 and then Needs_One_Actual (Subp)
6644 if Is_Array_Type (Etype (Subp)) then
6645 Arr_Type := Etype (Subp);
6647 elsif Is_Access_Type (Etype (Subp))
6648 and then Is_Array_Type (Designated_Type (Etype (Subp)))
6650 Arr_Type := Designated_Type (Etype (Subp));
6654 if Present (Arr_Type) then
6656 -- Verify that the actuals (excluding the object) match the types
6664 Actual := Next (First_Actual (Call));
6665 Index := First_Index (Arr_Type);
6666 while Present (Actual) and then Present (Index) loop
6667 if not Has_Compatible_Type (Actual, Etype (Index)) then
6672 Next_Actual (Actual);
6678 and then Present (Arr_Type)
6680 Comp_Type := Component_Type (Arr_Type);
6684 if Present (Comp_Type)
6685 and then Etype (Subprog) /= Comp_Type
6687 Add_One_Interp (Subprog, Subp, Comp_Type);
6691 if Etype (Call) /= Any_Type then
6696 end Valid_Candidate;
6698 -------------------------------
6699 -- Complete_Object_Operation --
6700 -------------------------------
6702 procedure Complete_Object_Operation
6703 (Call_Node : Node_Id;
6704 Node_To_Replace : Node_Id)
6706 Control : constant Entity_Id := First_Formal (Entity (Subprog));
6707 Formal_Type : constant Entity_Id := Etype (Control);
6708 First_Actual : Node_Id;
6711 -- Place the name of the operation, with its interpretations,
6712 -- on the rewritten call.
6714 Set_Name (Call_Node, Subprog);
6716 First_Actual := First (Parameter_Associations (Call_Node));
6718 -- For cross-reference purposes, treat the new node as being in
6719 -- the source if the original one is.
6721 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
6722 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
6724 if Nkind (N) = N_Selected_Component
6725 and then not Inside_A_Generic
6727 Set_Entity (Selector_Name (N), Entity (Subprog));
6730 -- If need be, rewrite first actual as an explicit dereference
6731 -- If the call is overloaded, the rewriting can only be done
6732 -- once the primitive operation is identified.
6734 if Is_Overloaded (Subprog) then
6736 -- The prefix itself may be overloaded, and its interpretations
6737 -- must be propagated to the new actual in the call.
6739 if Is_Overloaded (Obj) then
6740 Save_Interps (Obj, First_Actual);
6743 Rewrite (First_Actual, Obj);
6745 elsif not Is_Access_Type (Formal_Type)
6746 and then Is_Access_Type (Etype (Obj))
6748 Rewrite (First_Actual,
6749 Make_Explicit_Dereference (Sloc (Obj), Obj));
6750 Analyze (First_Actual);
6752 -- If we need to introduce an explicit dereference, verify that
6753 -- the resulting actual is compatible with the mode of the formal.
6755 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
6756 and then Is_Access_Constant (Etype (Obj))
6759 ("expect variable in call to&", Prefix (N), Entity (Subprog));
6762 -- Conversely, if the formal is an access parameter and the object
6763 -- is not, replace the actual with a 'Access reference. Its analysis
6764 -- will check that the object is aliased.
6766 elsif Is_Access_Type (Formal_Type)
6767 and then not Is_Access_Type (Etype (Obj))
6769 -- A special case: A.all'access is illegal if A is an access to a
6770 -- constant and the context requires an access to a variable.
6772 if not Is_Access_Constant (Formal_Type) then
6773 if (Nkind (Obj) = N_Explicit_Dereference
6774 and then Is_Access_Constant (Etype (Prefix (Obj))))
6775 or else not Is_Variable (Obj)
6778 ("actual for& must be a variable", Obj, Control);
6782 Rewrite (First_Actual,
6783 Make_Attribute_Reference (Loc,
6784 Attribute_Name => Name_Access,
6785 Prefix => Relocate_Node (Obj)));
6787 if not Is_Aliased_View (Obj) then
6789 ("object in prefixed call to& must be aliased"
6790 & " (RM-2005 4.3.1 (13))",
6791 Prefix (First_Actual), Subprog);
6794 Analyze (First_Actual);
6797 if Is_Overloaded (Obj) then
6798 Save_Interps (Obj, First_Actual);
6801 Rewrite (First_Actual, Obj);
6804 Rewrite (Node_To_Replace, Call_Node);
6806 -- Propagate the interpretations collected in subprog to the new
6807 -- function call node, to be resolved from context.
6809 if Is_Overloaded (Subprog) then
6810 Save_Interps (Subprog, Node_To_Replace);
6813 Analyze (Node_To_Replace);
6815 -- If the operation has been rewritten into a call, which may get
6816 -- subsequently an explicit dereference, preserve the type on the
6817 -- original node (selected component or indexed component) for
6818 -- subsequent legality tests, e.g. Is_Variable. which examines
6819 -- the original node.
6821 if Nkind (Node_To_Replace) = N_Function_Call then
6823 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
6826 end Complete_Object_Operation;
6828 ----------------------
6829 -- Report_Ambiguity --
6830 ----------------------
6832 procedure Report_Ambiguity (Op : Entity_Id) is
6833 Access_Formal : constant Boolean :=
6834 Is_Access_Type (Etype (First_Formal (Op)));
6835 Access_Actual : constant Boolean :=
6836 Is_Access_Type (Etype (Prefix (N)));
6839 Error_Msg_Sloc := Sloc (Op);
6841 if Access_Formal and then not Access_Actual then
6842 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6844 ("\possible interpretation"
6845 & " (inherited, with implicit 'Access) #", N);
6848 ("\possible interpretation (with implicit 'Access) #", N);
6851 elsif not Access_Formal and then Access_Actual then
6852 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6854 ("\possible interpretation"
6855 & " ( inherited, with implicit dereference) #", N);
6858 ("\possible interpretation (with implicit dereference) #", N);
6862 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6863 Error_Msg_N ("\possible interpretation (inherited)#", N);
6865 Error_Msg_N -- CODEFIX
6866 ("\possible interpretation#", N);
6869 end Report_Ambiguity;
6871 --------------------------------
6872 -- Transform_Object_Operation --
6873 --------------------------------
6875 procedure Transform_Object_Operation
6876 (Call_Node : out Node_Id;
6877 Node_To_Replace : out Node_Id)
6879 Dummy : constant Node_Id := New_Copy (Obj);
6880 -- Placeholder used as a first parameter in the call, replaced
6881 -- eventually by the proper object.
6883 Parent_Node : constant Node_Id := Parent (N);
6889 -- Common case covering 1) Call to a procedure and 2) Call to a
6890 -- function that has some additional actuals.
6892 if Nkind_In (Parent_Node, N_Function_Call,
6893 N_Procedure_Call_Statement)
6895 -- N is a selected component node containing the name of the
6896 -- subprogram. If N is not the name of the parent node we must
6897 -- not replace the parent node by the new construct. This case
6898 -- occurs when N is a parameterless call to a subprogram that
6899 -- is an actual parameter of a call to another subprogram. For
6901 -- Some_Subprogram (..., Obj.Operation, ...)
6903 and then Name (Parent_Node) = N
6905 Node_To_Replace := Parent_Node;
6907 Actuals := Parameter_Associations (Parent_Node);
6909 if Present (Actuals) then
6910 Prepend (Dummy, Actuals);
6912 Actuals := New_List (Dummy);
6915 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
6917 Make_Procedure_Call_Statement (Loc,
6918 Name => New_Copy (Subprog),
6919 Parameter_Associations => Actuals);
6923 Make_Function_Call (Loc,
6924 Name => New_Copy (Subprog),
6925 Parameter_Associations => Actuals);
6929 -- Before analysis, a function call appears as an indexed component
6930 -- if there are no named associations.
6932 elsif Nkind (Parent_Node) = N_Indexed_Component
6933 and then N = Prefix (Parent_Node)
6935 Node_To_Replace := Parent_Node;
6936 Actuals := Expressions (Parent_Node);
6938 Actual := First (Actuals);
6939 while Present (Actual) loop
6944 Prepend (Dummy, Actuals);
6947 Make_Function_Call (Loc,
6948 Name => New_Copy (Subprog),
6949 Parameter_Associations => Actuals);
6951 -- Parameterless call: Obj.F is rewritten as F (Obj)
6954 Node_To_Replace := N;
6957 Make_Function_Call (Loc,
6958 Name => New_Copy (Subprog),
6959 Parameter_Associations => New_List (Dummy));
6961 end Transform_Object_Operation;
6963 ------------------------------
6964 -- Try_Class_Wide_Operation --
6965 ------------------------------
6967 function Try_Class_Wide_Operation
6968 (Call_Node : Node_Id;
6969 Node_To_Replace : Node_Id) return Boolean
6971 Anc_Type : Entity_Id;
6972 Matching_Op : Entity_Id := Empty;
6975 procedure Traverse_Homonyms
6976 (Anc_Type : Entity_Id;
6977 Error : out Boolean);
6978 -- Traverse the homonym chain of the subprogram searching for those
6979 -- homonyms whose first formal has the Anc_Type's class-wide type,
6980 -- or an anonymous access type designating the class-wide type. If
6981 -- an ambiguity is detected, then Error is set to True.
6983 procedure Traverse_Interfaces
6984 (Anc_Type : Entity_Id;
6985 Error : out Boolean);
6986 -- Traverse the list of interfaces, if any, associated with Anc_Type
6987 -- and search for acceptable class-wide homonyms associated with each
6988 -- interface. If an ambiguity is detected, then Error is set to True.
6990 -----------------------
6991 -- Traverse_Homonyms --
6992 -----------------------
6994 procedure Traverse_Homonyms
6995 (Anc_Type : Entity_Id;
6996 Error : out Boolean)
6998 Cls_Type : Entity_Id;
7006 Cls_Type := Class_Wide_Type (Anc_Type);
7008 Hom := Current_Entity (Subprog);
7010 -- Find a non-hidden operation whose first parameter is of the
7011 -- class-wide type, a subtype thereof, or an anonymous access
7014 while Present (Hom) loop
7015 if Ekind_In (Hom, E_Procedure, E_Function)
7016 and then not Is_Hidden (Hom)
7017 and then Scope (Hom) = Scope (Anc_Type)
7018 and then Present (First_Formal (Hom))
7020 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7022 (Is_Access_Type (Etype (First_Formal (Hom)))
7024 Ekind (Etype (First_Formal (Hom))) =
7025 E_Anonymous_Access_Type
7028 (Designated_Type (Etype (First_Formal (Hom)))) =
7031 -- If the context is a procedure call, ignore functions
7032 -- in the name of the call.
7034 if Ekind (Hom) = E_Function
7035 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7036 and then N = Name (Parent (N))
7041 Set_Etype (Call_Node, Any_Type);
7042 Set_Is_Overloaded (Call_Node, False);
7045 if No (Matching_Op) then
7046 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
7047 Set_Etype (Call_Node, Any_Type);
7048 Set_Parent (Call_Node, Parent (Node_To_Replace));
7050 Set_Name (Call_Node, Hom_Ref);
7055 Report => Report_Error,
7057 Skip_First => True);
7060 Valid_Candidate (Success, Call_Node, Hom);
7066 Report => Report_Error,
7068 Skip_First => True);
7070 if Present (Valid_Candidate (Success, Call_Node, Hom))
7071 and then Nkind (Call_Node) /= N_Function_Call
7073 Error_Msg_NE ("ambiguous call to&", N, Hom);
7074 Report_Ambiguity (Matching_Op);
7075 Report_Ambiguity (Hom);
7083 Hom := Homonym (Hom);
7085 end Traverse_Homonyms;
7087 -------------------------
7088 -- Traverse_Interfaces --
7089 -------------------------
7091 procedure Traverse_Interfaces
7092 (Anc_Type : Entity_Id;
7093 Error : out Boolean)
7095 Intface_List : constant List_Id :=
7096 Abstract_Interface_List (Anc_Type);
7102 if Is_Non_Empty_List (Intface_List) then
7103 Intface := First (Intface_List);
7104 while Present (Intface) loop
7106 -- Look for acceptable class-wide homonyms associated with
7109 Traverse_Homonyms (Etype (Intface), Error);
7115 -- Continue the search by looking at each of the interface's
7116 -- associated interface ancestors.
7118 Traverse_Interfaces (Etype (Intface), Error);
7127 end Traverse_Interfaces;
7129 -- Start of processing for Try_Class_Wide_Operation
7132 -- Loop through ancestor types (including interfaces), traversing
7133 -- the homonym chain of the subprogram, trying out those homonyms
7134 -- whose first formal has the class-wide type of the ancestor, or
7135 -- an anonymous access type designating the class-wide type.
7137 Anc_Type := Obj_Type;
7139 -- Look for a match among homonyms associated with the ancestor
7141 Traverse_Homonyms (Anc_Type, Error);
7147 -- Continue the search for matches among homonyms associated with
7148 -- any interfaces implemented by the ancestor.
7150 Traverse_Interfaces (Anc_Type, Error);
7156 exit when Etype (Anc_Type) = Anc_Type;
7157 Anc_Type := Etype (Anc_Type);
7160 if Present (Matching_Op) then
7161 Set_Etype (Call_Node, Etype (Matching_Op));
7164 return Present (Matching_Op);
7165 end Try_Class_Wide_Operation;
7167 -----------------------------------
7168 -- Try_One_Prefix_Interpretation --
7169 -----------------------------------
7171 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7175 if Is_Access_Type (Obj_Type) then
7176 Obj_Type := Designated_Type (Obj_Type);
7179 if Ekind (Obj_Type) = E_Private_Subtype then
7180 Obj_Type := Base_Type (Obj_Type);
7183 if Is_Class_Wide_Type (Obj_Type) then
7184 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7187 -- The type may have be obtained through a limited_with clause,
7188 -- in which case the primitive operations are available on its
7189 -- non-limited view. If still incomplete, retrieve full view.
7191 if Ekind (Obj_Type) = E_Incomplete_Type
7192 and then From_With_Type (Obj_Type)
7194 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7197 -- If the object is not tagged, or the type is still an incomplete
7198 -- type, this is not a prefixed call.
7200 if not Is_Tagged_Type (Obj_Type)
7201 or else Is_Incomplete_Type (Obj_Type)
7206 if Try_Primitive_Operation
7207 (Call_Node => New_Call_Node,
7208 Node_To_Replace => Node_To_Replace)
7210 Try_Class_Wide_Operation
7211 (Call_Node => New_Call_Node,
7212 Node_To_Replace => Node_To_Replace)
7216 end Try_One_Prefix_Interpretation;
7218 -----------------------------
7219 -- Try_Primitive_Operation --
7220 -----------------------------
7222 function Try_Primitive_Operation
7223 (Call_Node : Node_Id;
7224 Node_To_Replace : Node_Id) return Boolean
7227 Prim_Op : Entity_Id;
7228 Matching_Op : Entity_Id := Empty;
7229 Prim_Op_Ref : Node_Id := Empty;
7231 Corr_Type : Entity_Id := Empty;
7232 -- If the prefix is a synchronized type, the controlling type of
7233 -- the primitive operation is the corresponding record type, else
7234 -- this is the object type itself.
7236 Success : Boolean := False;
7238 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7239 -- For tagged types the candidate interpretations are found in
7240 -- the list of primitive operations of the type and its ancestors.
7241 -- For formal tagged types we have to find the operations declared
7242 -- in the same scope as the type (including in the generic formal
7243 -- part) because the type itself carries no primitive operations,
7244 -- except for formal derived types that inherit the operations of
7245 -- the parent and progenitors.
7246 -- If the context is a generic subprogram body, the generic formals
7247 -- are visible by name, but are not in the entity list of the
7248 -- subprogram because that list starts with the subprogram formals.
7249 -- We retrieve the candidate operations from the generic declaration.
7251 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7252 -- An operation that overrides an inherited operation in the private
7253 -- part of its package may be hidden, but if the inherited operation
7254 -- is visible a direct call to it will dispatch to the private one,
7255 -- which is therefore a valid candidate.
7257 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7258 -- Verify that the prefix, dereferenced if need be, is a valid
7259 -- controlling argument in a call to Op. The remaining actuals
7260 -- are checked in the subsequent call to Analyze_One_Call.
7262 ------------------------------
7263 -- Collect_Generic_Type_Ops --
7264 ------------------------------
7266 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7267 Bas : constant Entity_Id := Base_Type (T);
7268 Candidates : constant Elist_Id := New_Elmt_List;
7272 procedure Check_Candidate;
7273 -- The operation is a candidate if its first parameter is a
7274 -- controlling operand of the desired type.
7276 -----------------------
7277 -- Check_Candidate; --
7278 -----------------------
7280 procedure Check_Candidate is
7282 Formal := First_Formal (Subp);
7285 and then Is_Controlling_Formal (Formal)
7287 (Base_Type (Etype (Formal)) = Bas
7289 (Is_Access_Type (Etype (Formal))
7290 and then Designated_Type (Etype (Formal)) = Bas))
7292 Append_Elmt (Subp, Candidates);
7294 end Check_Candidate;
7296 -- Start of processing for Collect_Generic_Type_Ops
7299 if Is_Derived_Type (T) then
7300 return Primitive_Operations (T);
7302 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7304 -- Scan the list of generic formals to find subprograms
7305 -- that may have a first controlling formal of the type.
7307 if Nkind (Unit_Declaration_Node (Scope (T)))
7308 = N_Generic_Subprogram_Declaration
7315 First (Generic_Formal_Declarations
7316 (Unit_Declaration_Node (Scope (T))));
7317 while Present (Decl) loop
7318 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7319 Subp := Defining_Entity (Decl);
7330 -- Scan the list of entities declared in the same scope as
7331 -- the type. In general this will be an open scope, given that
7332 -- the call we are analyzing can only appear within a generic
7333 -- declaration or body (either the one that declares T, or a
7336 -- For a subtype representing a generic actual type, go to the
7339 if Is_Generic_Actual_Type (T) then
7340 Subp := First_Entity (Scope (Base_Type (T)));
7342 Subp := First_Entity (Scope (T));
7345 while Present (Subp) loop
7346 if Is_Overloadable (Subp) then
7355 end Collect_Generic_Type_Ops;
7357 ---------------------------
7358 -- Is_Private_Overriding --
7359 ---------------------------
7361 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
7362 Visible_Op : constant Entity_Id := Homonym (Op);
7365 return Present (Visible_Op)
7366 and then Scope (Op) = Scope (Visible_Op)
7367 and then not Comes_From_Source (Visible_Op)
7368 and then Alias (Visible_Op) = Op
7369 and then not Is_Hidden (Visible_Op);
7370 end Is_Private_Overriding;
7372 -----------------------------
7373 -- Valid_First_Argument_Of --
7374 -----------------------------
7376 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
7377 Typ : Entity_Id := Etype (First_Formal (Op));
7380 if Is_Concurrent_Type (Typ)
7381 and then Present (Corresponding_Record_Type (Typ))
7383 Typ := Corresponding_Record_Type (Typ);
7386 -- Simple case. Object may be a subtype of the tagged type or
7387 -- may be the corresponding record of a synchronized type.
7389 return Obj_Type = Typ
7390 or else Base_Type (Obj_Type) = Typ
7391 or else Corr_Type = Typ
7393 -- Prefix can be dereferenced
7396 (Is_Access_Type (Corr_Type)
7397 and then Designated_Type (Corr_Type) = Typ)
7399 -- Formal is an access parameter, for which the object
7400 -- can provide an access.
7403 (Ekind (Typ) = E_Anonymous_Access_Type
7405 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
7406 end Valid_First_Argument_Of;
7408 -- Start of processing for Try_Primitive_Operation
7411 -- Look for subprograms in the list of primitive operations. The name
7412 -- must be identical, and the kind of call indicates the expected
7413 -- kind of operation (function or procedure). If the type is a
7414 -- (tagged) synchronized type, the primitive ops are attached to the
7415 -- corresponding record (base) type.
7417 if Is_Concurrent_Type (Obj_Type) then
7418 if Present (Corresponding_Record_Type (Obj_Type)) then
7419 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
7420 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
7422 Corr_Type := Obj_Type;
7423 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7426 elsif not Is_Generic_Type (Obj_Type) then
7427 Corr_Type := Obj_Type;
7428 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
7431 Corr_Type := Obj_Type;
7432 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7435 while Present (Elmt) loop
7436 Prim_Op := Node (Elmt);
7438 if Chars (Prim_Op) = Chars (Subprog)
7439 and then Present (First_Formal (Prim_Op))
7440 and then Valid_First_Argument_Of (Prim_Op)
7442 (Nkind (Call_Node) = N_Function_Call)
7443 = (Ekind (Prim_Op) = E_Function)
7445 -- Ada 2005 (AI-251): If this primitive operation corresponds
7446 -- with an immediate ancestor interface there is no need to add
7447 -- it to the list of interpretations; the corresponding aliased
7448 -- primitive is also in this list of primitive operations and
7449 -- will be used instead.
7451 if (Present (Interface_Alias (Prim_Op))
7452 and then Is_Ancestor (Find_Dispatching_Type
7453 (Alias (Prim_Op)), Corr_Type))
7455 -- Do not consider hidden primitives unless the type is in an
7456 -- open scope or we are within an instance, where visibility
7457 -- is known to be correct, or else if this is an overriding
7458 -- operation in the private part for an inherited operation.
7460 or else (Is_Hidden (Prim_Op)
7461 and then not Is_Immediately_Visible (Obj_Type)
7462 and then not In_Instance
7463 and then not Is_Private_Overriding (Prim_Op))
7468 Set_Etype (Call_Node, Any_Type);
7469 Set_Is_Overloaded (Call_Node, False);
7471 if No (Matching_Op) then
7472 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
7473 Candidate := Prim_Op;
7475 Set_Parent (Call_Node, Parent (Node_To_Replace));
7477 Set_Name (Call_Node, Prim_Op_Ref);
7483 Report => Report_Error,
7485 Skip_First => True);
7487 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
7489 -- More than one interpretation, collect for subsequent
7490 -- disambiguation. If this is a procedure call and there
7491 -- is another match, report ambiguity now.
7497 Report => Report_Error,
7499 Skip_First => True);
7501 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
7502 and then Nkind (Call_Node) /= N_Function_Call
7504 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
7505 Report_Ambiguity (Matching_Op);
7506 Report_Ambiguity (Prim_Op);
7516 if Present (Matching_Op) then
7517 Set_Etype (Call_Node, Etype (Matching_Op));
7520 return Present (Matching_Op);
7521 end Try_Primitive_Operation;
7523 -- Start of processing for Try_Object_Operation
7526 Analyze_Expression (Obj);
7528 -- Analyze the actuals if node is known to be a subprogram call
7530 if Is_Subprg_Call and then N = Name (Parent (N)) then
7531 Actual := First (Parameter_Associations (Parent (N)));
7532 while Present (Actual) loop
7533 Analyze_Expression (Actual);
7538 -- Build a subprogram call node, using a copy of Obj as its first
7539 -- actual. This is a placeholder, to be replaced by an explicit
7540 -- dereference when needed.
7542 Transform_Object_Operation
7543 (Call_Node => New_Call_Node,
7544 Node_To_Replace => Node_To_Replace);
7546 Set_Etype (New_Call_Node, Any_Type);
7547 Set_Etype (Subprog, Any_Type);
7548 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
7550 if not Is_Overloaded (Obj) then
7551 Try_One_Prefix_Interpretation (Obj_Type);
7558 Get_First_Interp (Obj, I, It);
7559 while Present (It.Nam) loop
7560 Try_One_Prefix_Interpretation (It.Typ);
7561 Get_Next_Interp (I, It);
7566 if Etype (New_Call_Node) /= Any_Type then
7567 Complete_Object_Operation
7568 (Call_Node => New_Call_Node,
7569 Node_To_Replace => Node_To_Replace);
7572 elsif Present (Candidate) then
7574 -- The argument list is not type correct. Re-analyze with error
7575 -- reporting enabled, and use one of the possible candidates.
7576 -- In All_Errors_Mode, re-analyze all failed interpretations.
7578 if All_Errors_Mode then
7579 Report_Error := True;
7580 if Try_Primitive_Operation
7581 (Call_Node => New_Call_Node,
7582 Node_To_Replace => Node_To_Replace)
7585 Try_Class_Wide_Operation
7586 (Call_Node => New_Call_Node,
7587 Node_To_Replace => Node_To_Replace)
7594 (N => New_Call_Node,
7598 Skip_First => True);
7601 -- No need for further errors
7606 -- There was no candidate operation, so report it as an error
7607 -- in the caller: Analyze_Selected_Component.
7611 end Try_Object_Operation;
7617 procedure wpo (T : Entity_Id) is
7622 if not Is_Tagged_Type (T) then
7626 E := First_Elmt (Primitive_Operations (Base_Type (T)));
7627 while Present (E) loop
7629 Write_Int (Int (Op));
7630 Write_Str (" === ");
7631 Write_Name (Chars (Op));
7633 Write_Name (Chars (Scope (Op)));