1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
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;
64 with Uintp; use Uintp;
66 package body Sem_Ch4 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Concatenation_Rest (N : Node_Id);
73 -- Does the "rest" of the work of Analyze_Concatenation, after the left
74 -- operand has been analyzed. See Analyze_Concatenation for details.
76 procedure Analyze_Expression (N : Node_Id);
77 -- For expressions that are not names, this is just a call to analyze.
78 -- If the expression is a name, it may be a call to a parameterless
79 -- function, and if so must be converted into an explicit call node
80 -- and analyzed as such. This deproceduring must be done during the first
81 -- pass of overload resolution, because otherwise a procedure call with
82 -- overloaded actuals may fail to resolve.
84 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
85 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
86 -- is an operator name or an expanded name whose selector is an operator
87 -- name, and one possible interpretation is as a predefined operator.
89 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
90 -- If the prefix of a selected_component is overloaded, the proper
91 -- interpretation that yields a record type with the proper selector
92 -- name must be selected.
94 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
95 -- Procedure to analyze a user defined binary operator, which is resolved
96 -- like a function, but instead of a list of actuals it is presented
97 -- with the left and right operands of an operator node.
99 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
100 -- Procedure to analyze a user defined unary operator, which is resolved
101 -- like a function, but instead of a list of actuals, it is presented with
102 -- the operand of the operator node.
104 procedure Ambiguous_Operands (N : Node_Id);
105 -- For equality, membership, and comparison operators with overloaded
106 -- arguments, list possible interpretations.
108 procedure Analyze_One_Call
112 Success : out Boolean;
113 Skip_First : Boolean := False);
114 -- Check one interpretation of an overloaded subprogram name for
115 -- compatibility with the types of the actuals in a call. If there is a
116 -- single interpretation which does not match, post error if Report is
119 -- Nam is the entity that provides the formals against which the actuals
120 -- are checked. Nam is either the name of a subprogram, or the internal
121 -- subprogram type constructed for an access_to_subprogram. If the actuals
122 -- are compatible with Nam, then Nam is added to the list of candidate
123 -- interpretations for N, and Success is set to True.
125 -- The flag Skip_First is used when analyzing a call that was rewritten
126 -- from object notation. In this case the first actual may have to receive
127 -- an explicit dereference, depending on the first formal of the operation
128 -- being called. The caller will have verified that the object is legal
129 -- for the call. If the remaining parameters match, the first parameter
130 -- will rewritten as a dereference if needed, prior to completing analysis.
132 procedure Check_Misspelled_Selector
135 -- Give possible misspelling diagnostic if Sel is likely to be a mis-
136 -- spelling of one of the selectors of the Prefix. This is called by
137 -- Analyze_Selected_Component after producing an invalid selector error
140 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
141 -- Verify that type T is declared in scope S. Used to find interpretations
142 -- for operators given by expanded names. This is abstracted as a separate
143 -- function to handle extensions to System, where S is System, but T is
144 -- declared in the extension.
146 procedure Find_Arithmetic_Types
150 -- L and R are the operands of an arithmetic operator. Find
151 -- consistent pairs of interpretations for L and R that have a
152 -- numeric type consistent with the semantics of the operator.
154 procedure Find_Comparison_Types
158 -- L and R are operands of a comparison operator. Find consistent
159 -- pairs of interpretations for L and R.
161 procedure Find_Concatenation_Types
165 -- For the four varieties of concatenation
167 procedure Find_Equality_Types
171 -- Ditto for equality operators
173 procedure Find_Boolean_Types
177 -- Ditto for binary logical operations
179 procedure Find_Negation_Types
183 -- Find consistent interpretation for operand of negation operator
185 procedure Find_Non_Universal_Interpretations
190 -- For equality and comparison operators, the result is always boolean,
191 -- and the legality of the operation is determined from the visibility
192 -- of the operand types. If one of the operands has a universal interpre-
193 -- tation, the legality check uses some compatible non-universal
194 -- interpretation of the other operand. N can be an operator node, or
195 -- a function call whose name is an operator designator. Any_Access, which
196 -- is the initial type of the literal NULL, is a universal type for the
197 -- purpose of this routine.
199 function Find_Primitive_Operation (N : Node_Id) return Boolean;
200 -- Find candidate interpretations for the name Obj.Proc when it appears
201 -- in a subprogram renaming declaration.
203 procedure Find_Unary_Types
207 -- Unary arithmetic types: plus, minus, abs
209 procedure Check_Arithmetic_Pair
213 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
214 -- types for left and right operand. Determine whether they constitute
215 -- a valid pair for the given operator, and record the corresponding
216 -- interpretation of the operator node. The node N may be an operator
217 -- node (the usual case) or a function call whose prefix is an operator
218 -- designator. In both cases Op_Id is the operator name itself.
220 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
221 -- Give detailed information on overloaded call where none of the
222 -- interpretations match. N is the call node, Nam the designator for
223 -- the overloaded entity being called.
225 function Junk_Operand (N : Node_Id) return Boolean;
226 -- Test for an operand that is an inappropriate entity (e.g. a package
227 -- name or a label). If so, issue an error message and return True. If
228 -- the operand is not an inappropriate entity kind, return False.
230 procedure Operator_Check (N : Node_Id);
231 -- Verify that an operator has received some valid interpretation. If none
232 -- was found, determine whether a use clause would make the operation
233 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
234 -- every type compatible with the operator, even if the operator for the
235 -- type is not directly visible. The routine uses this type to emit a more
236 -- informative message.
238 function Process_Implicit_Dereference_Prefix
240 P : Node_Id) return Entity_Id;
241 -- Called when P is the prefix of an implicit dereference, denoting an
242 -- object E. The function returns the designated type of the prefix, taking
243 -- into account that the designated type of an anonymous access type may be
244 -- a limited view, when the non-limited view is visible.
245 -- If in semantics only mode (-gnatc or generic), the function also records
246 -- that the prefix is a reference to E, if any. Normally, such a reference
247 -- is generated only when the implicit dereference is expanded into an
248 -- explicit one, but for consistency we must generate the reference when
249 -- expansion is disabled as well.
251 procedure Remove_Abstract_Operations (N : Node_Id);
252 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
253 -- operation is not a candidate interpretation.
255 function Try_Container_Indexing
258 Exprs : List_Id) return Boolean;
259 -- AI05-0139: Generalized indexing to support iterators over containers
261 function Try_Indexed_Call
265 Skip_First : Boolean) return Boolean;
266 -- If a function has defaults for all its actuals, a call to it may in fact
267 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
268 -- interpretation as an indexing, prior to analysis as a call. If both are
269 -- possible, the node is overloaded with both interpretations (same symbol
270 -- but two different types). If the call is written in prefix form, the
271 -- prefix becomes the first parameter in the call, and only the remaining
272 -- actuals must be checked for the presence of defaults.
274 function Try_Indirect_Call
277 Typ : Entity_Id) return Boolean;
278 -- Similarly, a function F that needs no actuals can return an access to a
279 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
280 -- the call may be overloaded with both interpretations.
282 function Try_Object_Operation
284 CW_Test_Only : Boolean := False) return Boolean;
285 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
286 -- is a call in this notation, it is transformed into a normal subprogram
287 -- call where the prefix is a parameter, and True is returned. If node
288 -- N is not of this form, it is unchanged, and False is returned. if
289 -- CW_Test_Only is true then N is an N_Selected_Component node which
290 -- is part of a call to an entry or procedure of a tagged concurrent
291 -- type and this routine is invoked to search for class-wide subprograms
292 -- conflicting with the target entity.
294 procedure wpo (T : Entity_Id);
295 pragma Warnings (Off, wpo);
296 -- Used for debugging: obtain list of primitive operations even if
297 -- type is not frozen and dispatch table is not built yet.
299 ------------------------
300 -- Ambiguous_Operands --
301 ------------------------
303 procedure Ambiguous_Operands (N : Node_Id) is
304 procedure List_Operand_Interps (Opnd : Node_Id);
306 --------------------------
307 -- List_Operand_Interps --
308 --------------------------
310 procedure List_Operand_Interps (Opnd : Node_Id) is
315 if Is_Overloaded (Opnd) then
316 if Nkind (Opnd) in N_Op then
318 elsif Nkind (Opnd) = N_Function_Call then
320 elsif Ada_Version >= Ada_2012 then
326 Get_First_Interp (Opnd, I, It);
327 while Present (It.Nam) loop
328 if Has_Implicit_Dereference (It.Typ) then
330 ("can be interpreted as implicit dereference", Opnd);
334 Get_Next_Interp (I, It);
345 if Opnd = Left_Opnd (N) then
346 Error_Msg_N ("\left operand has the following interpretations", N);
349 ("\right operand has the following interpretations", N);
353 List_Interps (Nam, Err);
354 end List_Operand_Interps;
356 -- Start of processing for Ambiguous_Operands
359 if Nkind (N) in N_Membership_Test then
360 Error_Msg_N ("ambiguous operands for membership", N);
362 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
363 Error_Msg_N ("ambiguous operands for equality", N);
366 Error_Msg_N ("ambiguous operands for comparison", N);
369 if All_Errors_Mode then
370 List_Operand_Interps (Left_Opnd (N));
371 List_Operand_Interps (Right_Opnd (N));
373 Error_Msg_N ("\use -gnatf switch for details", N);
375 end Ambiguous_Operands;
377 -----------------------
378 -- Analyze_Aggregate --
379 -----------------------
381 -- Most of the analysis of Aggregates requires that the type be known,
382 -- and is therefore put off until resolution.
384 procedure Analyze_Aggregate (N : Node_Id) is
386 if No (Etype (N)) then
387 Set_Etype (N, Any_Composite);
389 end Analyze_Aggregate;
391 -----------------------
392 -- Analyze_Allocator --
393 -----------------------
395 procedure Analyze_Allocator (N : Node_Id) is
396 Loc : constant Source_Ptr := Sloc (N);
397 Sav_Errs : constant Nat := Serious_Errors_Detected;
398 E : Node_Id := Expression (N);
399 Acc_Type : Entity_Id;
405 Check_SPARK_Restriction ("allocator is not allowed", N);
407 -- Deal with allocator restrictions
409 -- In accordance with H.4(7), the No_Allocators restriction only applies
410 -- to user-written allocators. The same consideration applies to the
411 -- No_Allocators_Before_Elaboration restriction.
413 if Comes_From_Source (N) then
414 Check_Restriction (No_Allocators, N);
416 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
417 -- look at enclosing context, checking task/main subprogram case.
421 while Present (P) loop
423 -- In both cases we need a handled sequence of statements, where
424 -- the occurrence of the allocator is within the statements.
426 if Nkind (P) = N_Handled_Sequence_Of_Statements
427 and then Is_List_Member (C)
428 and then List_Containing (C) = Statements (P)
430 -- Check for allocator within task body, this is a definite
431 -- violation of No_Allocators_After_Elaboration we can detect.
433 if Nkind (Original_Node (Parent (P))) = N_Task_Body then
435 (No_Standard_Allocators_After_Elaboration, N);
439 -- The other case is appearance in a subprogram body. This may
440 -- be a violation if this is a library level subprogram, and it
441 -- turns out to be used as the main program, but only the
442 -- binder knows that, so just record the occurrence.
444 if Nkind (Original_Node (Parent (P))) = N_Subprogram_Body
445 and then Nkind (Parent (Parent (P))) = N_Compilation_Unit
447 Set_Has_Allocator (Current_Sem_Unit);
456 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
457 -- any. The expected type for the name is any type. A non-overloading
458 -- rule then requires it to be of a type descended from
459 -- System.Storage_Pools.Subpools.Subpool_Handle.
461 -- This isn't exactly what the AI says, but it seems to be the right
462 -- rule. The AI should be fixed.???
465 Subpool : constant Node_Id := Subpool_Handle_Name (N);
468 if Present (Subpool) then
471 if Is_Overloaded (Subpool) then
472 Error_Msg_N ("ambiguous subpool handle", Subpool);
475 -- Check that Etype (Subpool) is descended from Subpool_Handle
481 -- Analyze the qualified expression or subtype indication
483 if Nkind (E) = N_Qualified_Expression then
484 Acc_Type := Create_Itype (E_Allocator_Type, N);
485 Set_Etype (Acc_Type, Acc_Type);
486 Find_Type (Subtype_Mark (E));
488 -- Analyze the qualified expression, and apply the name resolution
489 -- rule given in 4.7(3).
492 Type_Id := Etype (E);
493 Set_Directly_Designated_Type (Acc_Type, Type_Id);
495 Resolve (Expression (E), Type_Id);
497 -- Allocators generated by the build-in-place expansion mechanism
498 -- are explicitly marked as coming from source but do not need to be
499 -- checked for limited initialization. To exclude this case, ensure
500 -- that the parent of the allocator is a source node.
502 if Is_Limited_Type (Type_Id)
503 and then Comes_From_Source (N)
504 and then Comes_From_Source (Parent (N))
505 and then not In_Instance_Body
507 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
508 Error_Msg_N ("initialization not allowed for limited types", N);
509 Explain_Limited_Type (Type_Id, N);
513 -- A qualified expression requires an exact match of the type,
514 -- class-wide matching is not allowed.
516 -- if Is_Class_Wide_Type (Type_Id)
517 -- and then Base_Type
518 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
520 -- Wrong_Type (Expression (E), Type_Id);
523 Check_Non_Static_Context (Expression (E));
525 -- We don't analyze the qualified expression itself because it's
526 -- part of the allocator
528 Set_Etype (E, Type_Id);
530 -- Case where allocator has a subtype indication
535 Base_Typ : Entity_Id;
538 -- If the allocator includes a N_Subtype_Indication then a
539 -- constraint is present, otherwise the node is a subtype mark.
540 -- Introduce an explicit subtype declaration into the tree
541 -- defining some anonymous subtype and rewrite the allocator to
542 -- use this subtype rather than the subtype indication.
544 -- It is important to introduce the explicit subtype declaration
545 -- so that the bounds of the subtype indication are attached to
546 -- the tree in case the allocator is inside a generic unit.
548 if Nkind (E) = N_Subtype_Indication then
550 -- A constraint is only allowed for a composite type in Ada
551 -- 95. In Ada 83, a constraint is also allowed for an
552 -- access-to-composite type, but the constraint is ignored.
554 Find_Type (Subtype_Mark (E));
555 Base_Typ := Entity (Subtype_Mark (E));
557 if Is_Elementary_Type (Base_Typ) then
558 if not (Ada_Version = Ada_83
559 and then Is_Access_Type (Base_Typ))
561 Error_Msg_N ("constraint not allowed here", E);
563 if Nkind (Constraint (E)) =
564 N_Index_Or_Discriminant_Constraint
566 Error_Msg_N -- CODEFIX
567 ("\if qualified expression was meant, " &
568 "use apostrophe", Constraint (E));
572 -- Get rid of the bogus constraint:
574 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
575 Analyze_Allocator (N);
579 if Expander_Active then
580 Def_Id := Make_Temporary (Loc, 'S');
583 Make_Subtype_Declaration (Loc,
584 Defining_Identifier => Def_Id,
585 Subtype_Indication => Relocate_Node (E)));
587 if Sav_Errs /= Serious_Errors_Detected
588 and then Nkind (Constraint (E)) =
589 N_Index_Or_Discriminant_Constraint
591 Error_Msg_N -- CODEFIX
592 ("if qualified expression was meant, " &
593 "use apostrophe!", Constraint (E));
596 E := New_Occurrence_Of (Def_Id, Loc);
597 Rewrite (Expression (N), E);
601 Type_Id := Process_Subtype (E, N);
602 Acc_Type := Create_Itype (E_Allocator_Type, N);
603 Set_Etype (Acc_Type, Acc_Type);
604 Set_Directly_Designated_Type (Acc_Type, Type_Id);
605 Check_Fully_Declared (Type_Id, N);
607 -- Ada 2005 (AI-231): If the designated type is itself an access
608 -- type that excludes null, its default initialization will
609 -- be a null object, and we can insert an unconditional raise
610 -- before the allocator.
612 -- Ada 2012 (AI-104): A not null indication here is altogether
615 if Can_Never_Be_Null (Type_Id) then
617 Not_Null_Check : constant Node_Id :=
618 Make_Raise_Constraint_Error (Sloc (E),
619 Reason => CE_Null_Not_Allowed);
622 if Expander_Active then
623 Insert_Action (N, Not_Null_Check);
624 Analyze (Not_Null_Check);
626 elsif Warn_On_Ada_2012_Compatibility then
628 ("null value not allowed here in Ada 2012?y?", E);
633 -- Check restriction against dynamically allocated protected
634 -- objects. Note that when limited aggregates are supported,
635 -- a similar test should be applied to an allocator with a
636 -- qualified expression ???
638 if Is_Protected_Type (Type_Id) then
639 Check_Restriction (No_Protected_Type_Allocators, N);
642 -- Check for missing initialization. Skip this check if we already
643 -- had errors on analyzing the allocator, since in that case these
644 -- are probably cascaded errors.
646 if Is_Indefinite_Subtype (Type_Id)
647 and then Serious_Errors_Detected = Sav_Errs
649 -- The build-in-place machinery may produce an allocator when
650 -- the designated type is indefinite but the underlying type is
651 -- not. In this case the unknown discriminants are meaningless
652 -- and should not trigger error messages. Check the parent node
653 -- because the allocator is marked as coming from source.
655 if Present (Underlying_Type (Type_Id))
656 and then not Is_Indefinite_Subtype (Underlying_Type (Type_Id))
657 and then not Comes_From_Source (Parent (N))
661 elsif Is_Class_Wide_Type (Type_Id) then
663 ("initialization required in class-wide allocation", N);
666 if Ada_Version < Ada_2005
667 and then Is_Limited_Type (Type_Id)
669 Error_Msg_N ("unconstrained allocation not allowed", N);
671 if Is_Array_Type (Type_Id) then
673 ("\constraint with array bounds required", N);
675 elsif Has_Unknown_Discriminants (Type_Id) then
678 else pragma Assert (Has_Discriminants (Type_Id));
680 ("\constraint with discriminant values required", N);
683 -- Limited Ada 2005 and general non-limited case
687 ("uninitialized unconstrained allocation not allowed",
690 if Is_Array_Type (Type_Id) then
692 ("\qualified expression or constraint with " &
693 "array bounds required", N);
695 elsif Has_Unknown_Discriminants (Type_Id) then
696 Error_Msg_N ("\qualified expression required", N);
698 else pragma Assert (Has_Discriminants (Type_Id));
700 ("\qualified expression or constraint with " &
701 "discriminant values required", N);
709 if Is_Abstract_Type (Type_Id) then
710 Error_Msg_N ("cannot allocate abstract object", E);
713 if Has_Task (Designated_Type (Acc_Type)) then
714 Check_Restriction (No_Tasking, N);
715 Check_Restriction (Max_Tasks, N);
716 Check_Restriction (No_Task_Allocators, N);
719 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
720 -- type is nested, and the designated type needs finalization. The rule
721 -- is conservative in that class-wide types need finalization.
723 if Needs_Finalization (Designated_Type (Acc_Type))
724 and then not Is_Library_Level_Entity (Acc_Type)
726 Check_Restriction (No_Nested_Finalization, N);
729 -- Check that an allocator of a nested access type doesn't create a
730 -- protected object when restriction No_Local_Protected_Objects applies.
731 -- We don't have an equivalent to Has_Task for protected types, so only
732 -- cases where the designated type itself is a protected type are
733 -- currently checked. ???
735 if Is_Protected_Type (Designated_Type (Acc_Type))
736 and then not Is_Library_Level_Entity (Acc_Type)
738 Check_Restriction (No_Local_Protected_Objects, N);
741 -- If the No_Streams restriction is set, check that the type of the
742 -- object is not, and does not contain, any subtype derived from
743 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
744 -- Has_Stream just for efficiency reasons. There is no point in
745 -- spending time on a Has_Stream check if the restriction is not set.
747 if Restriction_Check_Required (No_Streams) then
748 if Has_Stream (Designated_Type (Acc_Type)) then
749 Check_Restriction (No_Streams, N);
753 Set_Etype (N, Acc_Type);
755 if not Is_Library_Level_Entity (Acc_Type) then
756 Check_Restriction (No_Local_Allocators, N);
759 if Serious_Errors_Detected > Sav_Errs then
760 Set_Error_Posted (N);
761 Set_Etype (N, Any_Type);
763 end Analyze_Allocator;
765 ---------------------------
766 -- Analyze_Arithmetic_Op --
767 ---------------------------
769 procedure Analyze_Arithmetic_Op (N : Node_Id) is
770 L : constant Node_Id := Left_Opnd (N);
771 R : constant Node_Id := Right_Opnd (N);
775 Candidate_Type := Empty;
776 Analyze_Expression (L);
777 Analyze_Expression (R);
779 -- If the entity is already set, the node is the instantiation of a
780 -- generic node with a non-local reference, or was manufactured by a
781 -- call to Make_Op_xxx. In either case the entity is known to be valid,
782 -- and we do not need to collect interpretations, instead we just get
783 -- the single possible interpretation.
787 if Present (Op_Id) then
788 if Ekind (Op_Id) = E_Operator then
790 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
791 and then Treat_Fixed_As_Integer (N)
795 Set_Etype (N, Any_Type);
796 Find_Arithmetic_Types (L, R, Op_Id, N);
800 Set_Etype (N, Any_Type);
801 Add_One_Interp (N, Op_Id, Etype (Op_Id));
804 -- Entity is not already set, so we do need to collect interpretations
807 Op_Id := Get_Name_Entity_Id (Chars (N));
808 Set_Etype (N, Any_Type);
810 while Present (Op_Id) loop
811 if Ekind (Op_Id) = E_Operator
812 and then Present (Next_Entity (First_Entity (Op_Id)))
814 Find_Arithmetic_Types (L, R, Op_Id, N);
816 -- The following may seem superfluous, because an operator cannot
817 -- be generic, but this ignores the cleverness of the author of
820 elsif Is_Overloadable (Op_Id) then
821 Analyze_User_Defined_Binary_Op (N, Op_Id);
824 Op_Id := Homonym (Op_Id);
829 end Analyze_Arithmetic_Op;
835 -- Function, procedure, and entry calls are checked here. The Name in
836 -- the call may be overloaded. The actuals have been analyzed and may
837 -- themselves be overloaded. On exit from this procedure, the node N
838 -- may have zero, one or more interpretations. In the first case an
839 -- error message is produced. In the last case, the node is flagged
840 -- as overloaded and the interpretations are collected in All_Interp.
842 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
843 -- the type-checking is similar to that of other calls.
845 procedure Analyze_Call (N : Node_Id) is
846 Actuals : constant List_Id := Parameter_Associations (N);
851 Success : Boolean := False;
853 Deref : Boolean := False;
854 -- Flag indicates whether an interpretation of the prefix is a
855 -- parameterless call that returns an access_to_subprogram.
857 procedure Check_Ghost_Subprogram_Call;
858 -- Verify the legality of a call to a ghost subprogram. Such calls can
859 -- appear only in assertion expressions except subtype predicates or
860 -- from within another ghost subprogram.
862 procedure Check_Mixed_Parameter_And_Named_Associations;
863 -- Check that parameter and named associations are not mixed. This is
864 -- a restriction in SPARK mode.
866 function Name_Denotes_Function return Boolean;
867 -- If the type of the name is an access to subprogram, this may be the
868 -- type of a name, or the return type of the function being called. If
869 -- the name is not an entity then it can denote a protected function.
870 -- Until we distinguish Etype from Return_Type, we must use this routine
871 -- to resolve the meaning of the name in the call.
873 procedure No_Interpretation;
874 -- Output error message when no valid interpretation exists
876 ---------------------------------
877 -- Check_Ghost_Subprogram_Call --
878 ---------------------------------
880 procedure Check_Ghost_Subprogram_Call is
884 -- Do not perform the check while preanalyzing the enclosing context
885 -- because the call is not in its final place. Premature attempts to
886 -- verify the placement lead to bogus errors.
888 if In_Spec_Expression then
891 -- The ghost subprogram appears inside an assertion expression
892 -- which is one of the allowed cases.
894 elsif In_Assertion_Expression (N) then
897 -- Otherwise see if it inside another ghost subprogram
900 -- Loop to climb scopes
903 while Present (S) and then S /= Standard_Standard loop
905 -- The call appears inside another ghost subprogram
907 if Is_Ghost_Subprogram (S) then
914 -- If we fall through the loop it was not within another
915 -- ghost subprogram, so we have bad placement.
918 ("call to ghost subprogram must appear in assertion expression "
919 & "or another ghost subprogram", N);
921 end Check_Ghost_Subprogram_Call;
923 --------------------------------------------------
924 -- Check_Mixed_Parameter_And_Named_Associations --
925 --------------------------------------------------
927 procedure Check_Mixed_Parameter_And_Named_Associations is
929 Named_Seen : Boolean;
934 Actual := First (Actuals);
935 while Present (Actual) loop
936 case Nkind (Actual) is
937 when N_Parameter_Association =>
939 Check_SPARK_Restriction
940 ("named association cannot follow positional one",
950 end Check_Mixed_Parameter_And_Named_Associations;
952 ---------------------------
953 -- Name_Denotes_Function --
954 ---------------------------
956 function Name_Denotes_Function return Boolean is
958 if Is_Entity_Name (Nam) then
959 return Ekind (Entity (Nam)) = E_Function;
961 elsif Nkind (Nam) = N_Selected_Component then
962 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
967 end Name_Denotes_Function;
969 -----------------------
970 -- No_Interpretation --
971 -----------------------
973 procedure No_Interpretation is
974 L : constant Boolean := Is_List_Member (N);
975 K : constant Node_Kind := Nkind (Parent (N));
978 -- If the node is in a list whose parent is not an expression then it
979 -- must be an attempted procedure call.
981 if L and then K not in N_Subexpr then
982 if Ekind (Entity (Nam)) = E_Generic_Procedure then
984 ("must instantiate generic procedure& before call",
988 ("procedure or entry name expected", Nam);
991 -- Check for tasking cases where only an entry call will do
994 and then Nkind_In (K, N_Entry_Call_Alternative,
995 N_Triggering_Alternative)
997 Error_Msg_N ("entry name expected", Nam);
999 -- Otherwise give general error message
1002 Error_Msg_N ("invalid prefix in call", Nam);
1004 end No_Interpretation;
1006 -- Start of processing for Analyze_Call
1009 if Restriction_Check_Required (SPARK_05) then
1010 Check_Mixed_Parameter_And_Named_Associations;
1013 -- Mark a function that appears inside an assertion expression
1015 if Nkind (N) = N_Function_Call and then In_Assertion_Expr > 0 then
1016 Set_In_Assertion_Expression (N);
1019 -- Initialize the type of the result of the call to the error type,
1020 -- which will be reset if the type is successfully resolved.
1022 Set_Etype (N, Any_Type);
1026 if not Is_Overloaded (Nam) then
1028 -- Only one interpretation to check
1030 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1031 Nam_Ent := Etype (Nam);
1033 -- If the prefix is an access_to_subprogram, this may be an indirect
1034 -- call. This is the case if the name in the call is not an entity
1035 -- name, or if it is a function name in the context of a procedure
1036 -- call. In this latter case, we have a call to a parameterless
1037 -- function that returns a pointer_to_procedure which is the entity
1038 -- being called. Finally, F (X) may be a call to a parameterless
1039 -- function that returns a pointer to a function with parameters.
1040 -- Note that if F returns an access-to-subprogram whose designated
1041 -- type is an array, F (X) cannot be interpreted as an indirect call
1042 -- through the result of the call to F.
1044 elsif Is_Access_Type (Etype (Nam))
1045 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1047 (not Name_Denotes_Function
1048 or else Nkind (N) = N_Procedure_Call_Statement
1050 (Nkind (Parent (N)) /= N_Explicit_Dereference
1051 and then Is_Entity_Name (Nam)
1052 and then No (First_Formal (Entity (Nam)))
1054 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1055 and then Present (Actuals)))
1057 Nam_Ent := Designated_Type (Etype (Nam));
1058 Insert_Explicit_Dereference (Nam);
1060 -- Selected component case. Simple entry or protected operation,
1061 -- where the entry name is given by the selector name.
1063 elsif Nkind (Nam) = N_Selected_Component then
1064 Nam_Ent := Entity (Selector_Name (Nam));
1066 if not Ekind_In (Nam_Ent, E_Entry,
1071 Error_Msg_N ("name in call is not a callable entity", Nam);
1072 Set_Etype (N, Any_Type);
1076 -- If the name is an Indexed component, it can be a call to a member
1077 -- of an entry family. The prefix must be a selected component whose
1078 -- selector is the entry. Analyze_Procedure_Call normalizes several
1079 -- kinds of call into this form.
1081 elsif Nkind (Nam) = N_Indexed_Component then
1082 if Nkind (Prefix (Nam)) = N_Selected_Component then
1083 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1085 Error_Msg_N ("name in call is not a callable entity", Nam);
1086 Set_Etype (N, Any_Type);
1090 elsif not Is_Entity_Name (Nam) then
1091 Error_Msg_N ("name in call is not a callable entity", Nam);
1092 Set_Etype (N, Any_Type);
1096 Nam_Ent := Entity (Nam);
1098 -- If no interpretations, give error message
1100 if not Is_Overloadable (Nam_Ent) then
1106 -- Operations generated for RACW stub types are called only through
1107 -- dispatching, and can never be the static interpretation of a call.
1109 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1114 Analyze_One_Call (N, Nam_Ent, True, Success);
1116 -- If this is an indirect call, the return type of the access_to
1117 -- subprogram may be an incomplete type. At the point of the call,
1118 -- use the full type if available, and at the same time update the
1119 -- return type of the access_to_subprogram.
1122 and then Nkind (Nam) = N_Explicit_Dereference
1123 and then Ekind (Etype (N)) = E_Incomplete_Type
1124 and then Present (Full_View (Etype (N)))
1126 Set_Etype (N, Full_View (Etype (N)));
1127 Set_Etype (Nam_Ent, Etype (N));
1133 -- An overloaded selected component must denote overloaded operations
1134 -- of a concurrent type. The interpretations are attached to the
1135 -- simple name of those operations.
1137 if Nkind (Nam) = N_Selected_Component then
1138 Nam := Selector_Name (Nam);
1141 Get_First_Interp (Nam, X, It);
1143 while Present (It.Nam) loop
1147 -- Name may be call that returns an access to subprogram, or more
1148 -- generally an overloaded expression one of whose interpretations
1149 -- yields an access to subprogram. If the name is an entity, we do
1150 -- not dereference, because the node is a call that returns the
1151 -- access type: note difference between f(x), where the call may
1152 -- return an access subprogram type, and f(x)(y), where the type
1153 -- returned by the call to f is implicitly dereferenced to analyze
1156 if Is_Access_Type (Nam_Ent) then
1157 Nam_Ent := Designated_Type (Nam_Ent);
1159 elsif Is_Access_Type (Etype (Nam_Ent))
1161 (not Is_Entity_Name (Nam)
1162 or else Nkind (N) = N_Procedure_Call_Statement)
1163 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1166 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1168 if Is_Entity_Name (Nam) then
1173 -- If the call has been rewritten from a prefixed call, the first
1174 -- parameter has been analyzed, but may need a subsequent
1175 -- dereference, so skip its analysis now.
1177 if N /= Original_Node (N)
1178 and then Nkind (Original_Node (N)) = Nkind (N)
1179 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1180 and then Present (Parameter_Associations (N))
1181 and then Present (Etype (First (Parameter_Associations (N))))
1184 (N, Nam_Ent, False, Success, Skip_First => True);
1186 Analyze_One_Call (N, Nam_Ent, False, Success);
1189 -- If the interpretation succeeds, mark the proper type of the
1190 -- prefix (any valid candidate will do). If not, remove the
1191 -- candidate interpretation. This only needs to be done for
1192 -- overloaded protected operations, for other entities disambi-
1193 -- guation is done directly in Resolve.
1197 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1199 Set_Entity (Nam, It.Nam);
1200 Insert_Explicit_Dereference (Nam);
1201 Set_Etype (Nam, Nam_Ent);
1204 Set_Etype (Nam, It.Typ);
1207 elsif Nkind_In (Name (N), N_Selected_Component,
1213 Get_Next_Interp (X, It);
1216 -- If the name is the result of a function call, it can only be a
1217 -- call to a function returning an access to subprogram. Insert
1218 -- explicit dereference.
1220 if Nkind (Nam) = N_Function_Call then
1221 Insert_Explicit_Dereference (Nam);
1224 if Etype (N) = Any_Type then
1226 -- None of the interpretations is compatible with the actuals
1228 Diagnose_Call (N, Nam);
1230 -- Special checks for uninstantiated put routines
1232 if Nkind (N) = N_Procedure_Call_Statement
1233 and then Is_Entity_Name (Nam)
1234 and then Chars (Nam) = Name_Put
1235 and then List_Length (Actuals) = 1
1238 Arg : constant Node_Id := First (Actuals);
1242 if Nkind (Arg) = N_Parameter_Association then
1243 Typ := Etype (Explicit_Actual_Parameter (Arg));
1248 if Is_Signed_Integer_Type (Typ) then
1250 ("possible missing instantiation of " &
1251 "'Text_'I'O.'Integer_'I'O!", Nam);
1253 elsif Is_Modular_Integer_Type (Typ) then
1255 ("possible missing instantiation of " &
1256 "'Text_'I'O.'Modular_'I'O!", Nam);
1258 elsif Is_Floating_Point_Type (Typ) then
1260 ("possible missing instantiation of " &
1261 "'Text_'I'O.'Float_'I'O!", Nam);
1263 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1265 ("possible missing instantiation of " &
1266 "'Text_'I'O.'Fixed_'I'O!", Nam);
1268 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1270 ("possible missing instantiation of " &
1271 "'Text_'I'O.'Decimal_'I'O!", Nam);
1273 elsif Is_Enumeration_Type (Typ) then
1275 ("possible missing instantiation of " &
1276 "'Text_'I'O.'Enumeration_'I'O!", Nam);
1281 elsif not Is_Overloaded (N)
1282 and then Is_Entity_Name (Nam)
1284 -- Resolution yields a single interpretation. Verify that the
1285 -- reference has capitalization consistent with the declaration.
1287 Set_Entity_With_Style_Check (Nam, Entity (Nam));
1288 Generate_Reference (Entity (Nam), Nam);
1290 Set_Etype (Nam, Etype (Entity (Nam)));
1292 Remove_Abstract_Operations (N);
1298 -- A call to a ghost subprogram is allowed only in assertion expressions
1299 -- excluding subtype predicates or from within another ghost subprogram.
1301 if Is_Ghost_Subprogram (Get_Subprogram_Entity (N)) then
1302 Check_Ghost_Subprogram_Call;
1306 -----------------------------
1307 -- Analyze_Case_Expression --
1308 -----------------------------
1310 procedure Analyze_Case_Expression (N : Node_Id) is
1311 function Has_Static_Predicate (Subtyp : Entity_Id) return Boolean;
1312 -- Determine whether subtype Subtyp has aspect Static_Predicate
1314 procedure Non_Static_Choice_Error (Choice : Node_Id);
1315 -- Error routine invoked by the generic instantiation below when
1316 -- the case expression has a non static choice.
1318 package Case_Choices_Analysis is new
1319 Generic_Analyze_Choices
1320 (Process_Associated_Node => No_OP);
1321 use Case_Choices_Analysis;
1323 package Case_Choices_Checking is new
1324 Generic_Check_Choices
1325 (Process_Empty_Choice => No_OP,
1326 Process_Non_Static_Choice => Non_Static_Choice_Error,
1327 Process_Associated_Node => No_OP);
1328 use Case_Choices_Checking;
1330 --------------------------
1331 -- Has_Static_Predicate --
1332 --------------------------
1334 function Has_Static_Predicate (Subtyp : Entity_Id) return Boolean is
1338 Item := First_Rep_Item (Subtyp);
1339 while Present (Item) loop
1340 if Nkind (Item) = N_Aspect_Specification
1341 and then Chars (Identifier (Item)) = Name_Static_Predicate
1346 Next_Rep_Item (Item);
1350 end Has_Static_Predicate;
1352 -----------------------------
1353 -- Non_Static_Choice_Error --
1354 -----------------------------
1356 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1358 Flag_Non_Static_Expr
1359 ("choice given in case expression is not static!", Choice);
1360 end Non_Static_Choice_Error;
1364 Expr : constant Node_Id := Expression (N);
1365 FirstX : constant Node_Id := Expression (First (Alternatives (N)));
1367 Exp_Type : Entity_Id;
1368 Exp_Btype : Entity_Id;
1370 Others_Present : Boolean;
1371 -- Indicates if Others was present
1373 -- Start of processing for Analyze_Case_Expression
1376 if Comes_From_Source (N) then
1377 Check_Compiler_Unit (N);
1380 Analyze_And_Resolve (Expr, Any_Discrete);
1381 Check_Unset_Reference (Expr);
1382 Exp_Type := Etype (Expr);
1383 Exp_Btype := Base_Type (Exp_Type);
1385 Alt := First (Alternatives (N));
1386 while Present (Alt) loop
1387 Analyze (Expression (Alt));
1391 if not Is_Overloaded (FirstX) then
1392 Set_Etype (N, Etype (FirstX));
1400 Set_Etype (N, Any_Type);
1402 Get_First_Interp (FirstX, I, It);
1403 while Present (It.Nam) loop
1405 -- For each interpretation of the first expression, we only
1406 -- add the interpretation if every other expression in the
1407 -- case expression alternatives has a compatible type.
1409 Alt := Next (First (Alternatives (N)));
1410 while Present (Alt) loop
1411 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1416 Add_One_Interp (N, It.Typ, It.Typ);
1419 Get_Next_Interp (I, It);
1424 Exp_Btype := Base_Type (Exp_Type);
1426 -- The expression must be of a discrete type which must be determinable
1427 -- independently of the context in which the expression occurs, but
1428 -- using the fact that the expression must be of a discrete type.
1429 -- Moreover, the type this expression must not be a character literal
1430 -- (which is always ambiguous).
1432 -- If error already reported by Resolve, nothing more to do
1434 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1437 elsif Exp_Btype = Any_Character then
1439 ("character literal as case expression is ambiguous", Expr);
1443 -- If the case expression is a formal object of mode in out, then
1444 -- treat it as having a nonstatic subtype by forcing use of the base
1445 -- type (which has to get passed to Check_Case_Choices below). Also
1446 -- use base type when the case expression is parenthesized.
1448 if Paren_Count (Expr) > 0
1449 or else (Is_Entity_Name (Expr)
1450 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1452 Exp_Type := Exp_Btype;
1455 -- The case expression alternatives cover the range of a static subtype
1456 -- subject to aspect Static_Predicate. Do not check the choices when the
1457 -- case expression has not been fully analyzed yet because this may lead
1460 if Is_Static_Subtype (Exp_Type)
1461 and then Has_Static_Predicate (Exp_Type)
1462 and then In_Spec_Expression
1466 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1469 Analyze_Choices (Alternatives (N), Exp_Type);
1470 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1473 if Exp_Type = Universal_Integer and then not Others_Present then
1475 ("case on universal integer requires OTHERS choice", Expr);
1477 end Analyze_Case_Expression;
1479 ---------------------------
1480 -- Analyze_Comparison_Op --
1481 ---------------------------
1483 procedure Analyze_Comparison_Op (N : Node_Id) is
1484 L : constant Node_Id := Left_Opnd (N);
1485 R : constant Node_Id := Right_Opnd (N);
1486 Op_Id : Entity_Id := Entity (N);
1489 Set_Etype (N, Any_Type);
1490 Candidate_Type := Empty;
1492 Analyze_Expression (L);
1493 Analyze_Expression (R);
1495 if Present (Op_Id) then
1496 if Ekind (Op_Id) = E_Operator then
1497 Find_Comparison_Types (L, R, Op_Id, N);
1499 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1502 if Is_Overloaded (L) then
1503 Set_Etype (L, Intersect_Types (L, R));
1507 Op_Id := Get_Name_Entity_Id (Chars (N));
1508 while Present (Op_Id) loop
1509 if Ekind (Op_Id) = E_Operator then
1510 Find_Comparison_Types (L, R, Op_Id, N);
1512 Analyze_User_Defined_Binary_Op (N, Op_Id);
1515 Op_Id := Homonym (Op_Id);
1520 end Analyze_Comparison_Op;
1522 ---------------------------
1523 -- Analyze_Concatenation --
1524 ---------------------------
1526 procedure Analyze_Concatenation (N : Node_Id) is
1528 -- We wish to avoid deep recursion, because concatenations are often
1529 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1530 -- operands nonrecursively until we find something that is not a
1531 -- concatenation (A in this case), or has already been analyzed. We
1532 -- analyze that, and then walk back up the tree following Parent
1533 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1534 -- work at each level. The Parent pointers allow us to avoid recursion,
1535 -- and thus avoid running out of memory.
1541 Candidate_Type := Empty;
1543 -- The following code is equivalent to:
1545 -- Set_Etype (N, Any_Type);
1546 -- Analyze_Expression (Left_Opnd (N));
1547 -- Analyze_Concatenation_Rest (N);
1549 -- where the Analyze_Expression call recurses back here if the left
1550 -- operand is a concatenation.
1552 -- Walk down left operands
1555 Set_Etype (NN, Any_Type);
1556 L := Left_Opnd (NN);
1557 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1561 -- Now (given the above example) NN is A&B and L is A
1563 -- First analyze L ...
1565 Analyze_Expression (L);
1567 -- ... then walk NN back up until we reach N (where we started), calling
1568 -- Analyze_Concatenation_Rest along the way.
1571 Analyze_Concatenation_Rest (NN);
1575 end Analyze_Concatenation;
1577 --------------------------------
1578 -- Analyze_Concatenation_Rest --
1579 --------------------------------
1581 -- If the only one-dimensional array type in scope is String,
1582 -- this is the resulting type of the operation. Otherwise there
1583 -- will be a concatenation operation defined for each user-defined
1584 -- one-dimensional array.
1586 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1587 L : constant Node_Id := Left_Opnd (N);
1588 R : constant Node_Id := Right_Opnd (N);
1589 Op_Id : Entity_Id := Entity (N);
1594 Analyze_Expression (R);
1596 -- If the entity is present, the node appears in an instance, and
1597 -- denotes a predefined concatenation operation. The resulting type is
1598 -- obtained from the arguments when possible. If the arguments are
1599 -- aggregates, the array type and the concatenation type must be
1602 if Present (Op_Id) then
1603 if Ekind (Op_Id) = E_Operator then
1604 LT := Base_Type (Etype (L));
1605 RT := Base_Type (Etype (R));
1607 if Is_Array_Type (LT)
1608 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1610 Add_One_Interp (N, Op_Id, LT);
1612 elsif Is_Array_Type (RT)
1613 and then LT = Base_Type (Component_Type (RT))
1615 Add_One_Interp (N, Op_Id, RT);
1617 -- If one operand is a string type or a user-defined array type,
1618 -- and the other is a literal, result is of the specific type.
1621 (Root_Type (LT) = Standard_String
1622 or else Scope (LT) /= Standard_Standard)
1623 and then Etype (R) = Any_String
1625 Add_One_Interp (N, Op_Id, LT);
1628 (Root_Type (RT) = Standard_String
1629 or else Scope (RT) /= Standard_Standard)
1630 and then Etype (L) = Any_String
1632 Add_One_Interp (N, Op_Id, RT);
1634 elsif not Is_Generic_Type (Etype (Op_Id)) then
1635 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1638 -- Type and its operations must be visible
1640 Set_Entity (N, Empty);
1641 Analyze_Concatenation (N);
1645 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1649 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1650 while Present (Op_Id) loop
1651 if Ekind (Op_Id) = E_Operator then
1653 -- Do not consider operators declared in dead code, they can
1654 -- not be part of the resolution.
1656 if Is_Eliminated (Op_Id) then
1659 Find_Concatenation_Types (L, R, Op_Id, N);
1663 Analyze_User_Defined_Binary_Op (N, Op_Id);
1666 Op_Id := Homonym (Op_Id);
1671 end Analyze_Concatenation_Rest;
1673 -------------------------
1674 -- Analyze_Equality_Op --
1675 -------------------------
1677 procedure Analyze_Equality_Op (N : Node_Id) is
1678 Loc : constant Source_Ptr := Sloc (N);
1679 L : constant Node_Id := Left_Opnd (N);
1680 R : constant Node_Id := Right_Opnd (N);
1684 Set_Etype (N, Any_Type);
1685 Candidate_Type := Empty;
1687 Analyze_Expression (L);
1688 Analyze_Expression (R);
1690 -- If the entity is set, the node is a generic instance with a non-local
1691 -- reference to the predefined operator or to a user-defined function.
1692 -- It can also be an inequality that is expanded into the negation of a
1693 -- call to a user-defined equality operator.
1695 -- For the predefined case, the result is Boolean, regardless of the
1696 -- type of the operands. The operands may even be limited, if they are
1697 -- generic actuals. If they are overloaded, label the left argument with
1698 -- the common type that must be present, or with the type of the formal
1699 -- of the user-defined function.
1701 if Present (Entity (N)) then
1702 Op_Id := Entity (N);
1704 if Ekind (Op_Id) = E_Operator then
1705 Add_One_Interp (N, Op_Id, Standard_Boolean);
1707 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1710 if Is_Overloaded (L) then
1711 if Ekind (Op_Id) = E_Operator then
1712 Set_Etype (L, Intersect_Types (L, R));
1714 Set_Etype (L, Etype (First_Formal (Op_Id)));
1719 Op_Id := Get_Name_Entity_Id (Chars (N));
1720 while Present (Op_Id) loop
1721 if Ekind (Op_Id) = E_Operator then
1722 Find_Equality_Types (L, R, Op_Id, N);
1724 Analyze_User_Defined_Binary_Op (N, Op_Id);
1727 Op_Id := Homonym (Op_Id);
1731 -- If there was no match, and the operator is inequality, this may
1732 -- be a case where inequality has not been made explicit, as for
1733 -- tagged types. Analyze the node as the negation of an equality
1734 -- operation. This cannot be done earlier, because before analysis
1735 -- we cannot rule out the presence of an explicit inequality.
1737 if Etype (N) = Any_Type
1738 and then Nkind (N) = N_Op_Ne
1740 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1741 while Present (Op_Id) loop
1742 if Ekind (Op_Id) = E_Operator then
1743 Find_Equality_Types (L, R, Op_Id, N);
1745 Analyze_User_Defined_Binary_Op (N, Op_Id);
1748 Op_Id := Homonym (Op_Id);
1751 if Etype (N) /= Any_Type then
1752 Op_Id := Entity (N);
1758 Left_Opnd => Left_Opnd (N),
1759 Right_Opnd => Right_Opnd (N))));
1761 Set_Entity (Right_Opnd (N), Op_Id);
1767 end Analyze_Equality_Op;
1769 ----------------------------------
1770 -- Analyze_Explicit_Dereference --
1771 ----------------------------------
1773 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1774 Loc : constant Source_Ptr := Sloc (N);
1775 P : constant Node_Id := Prefix (N);
1781 function Is_Function_Type return Boolean;
1782 -- Check whether node may be interpreted as an implicit function call
1784 ----------------------
1785 -- Is_Function_Type --
1786 ----------------------
1788 function Is_Function_Type return Boolean is
1793 if not Is_Overloaded (N) then
1794 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1795 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1798 Get_First_Interp (N, I, It);
1799 while Present (It.Nam) loop
1800 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1801 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1806 Get_Next_Interp (I, It);
1811 end Is_Function_Type;
1813 -- Start of processing for Analyze_Explicit_Dereference
1816 -- If source node, check SPARK restriction. We guard this with the
1817 -- source node check, because ???
1819 if Comes_From_Source (N) then
1820 Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1823 -- In formal verification mode, keep track of all reads and writes
1824 -- through explicit dereferences.
1826 if GNATprove_Mode then
1827 SPARK_Specific.Generate_Dereference (N);
1831 Set_Etype (N, Any_Type);
1833 -- Test for remote access to subprogram type, and if so return
1834 -- after rewriting the original tree.
1836 if Remote_AST_E_Dereference (P) then
1840 -- Normal processing for other than remote access to subprogram type
1842 if not Is_Overloaded (P) then
1843 if Is_Access_Type (Etype (P)) then
1845 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1846 -- avoid other problems caused by the Private_Subtype and it is
1847 -- safe to go to the Base_Type because this is the same as
1848 -- converting the access value to its Base_Type.
1851 DT : Entity_Id := Designated_Type (Etype (P));
1854 if Ekind (DT) = E_Private_Subtype
1855 and then Is_For_Access_Subtype (DT)
1857 DT := Base_Type (DT);
1860 -- An explicit dereference is a legal occurrence of an
1861 -- incomplete type imported through a limited_with clause,
1862 -- if the full view is visible.
1864 if From_Limited_With (DT)
1865 and then not From_Limited_With (Scope (DT))
1867 (Is_Immediately_Visible (Scope (DT))
1869 (Is_Child_Unit (Scope (DT))
1870 and then Is_Visible_Lib_Unit (Scope (DT))))
1872 Set_Etype (N, Available_View (DT));
1879 elsif Etype (P) /= Any_Type then
1880 Error_Msg_N ("prefix of dereference must be an access type", N);
1885 Get_First_Interp (P, I, It);
1886 while Present (It.Nam) loop
1889 if Is_Access_Type (T) then
1890 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1893 Get_Next_Interp (I, It);
1896 -- Error if no interpretation of the prefix has an access type
1898 if Etype (N) = Any_Type then
1900 ("access type required in prefix of explicit dereference", P);
1901 Set_Etype (N, Any_Type);
1907 and then Nkind (Parent (N)) /= N_Indexed_Component
1909 and then (Nkind (Parent (N)) /= N_Function_Call
1910 or else N /= Name (Parent (N)))
1912 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1913 or else N /= Name (Parent (N)))
1915 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1916 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1918 (Attribute_Name (Parent (N)) /= Name_Address
1920 Attribute_Name (Parent (N)) /= Name_Access))
1922 -- Name is a function call with no actuals, in a context that
1923 -- requires deproceduring (including as an actual in an enclosing
1924 -- function or procedure call). There are some pathological cases
1925 -- where the prefix might include functions that return access to
1926 -- subprograms and others that return a regular type. Disambiguation
1927 -- of those has to take place in Resolve.
1930 Make_Function_Call (Loc,
1931 Name => Make_Explicit_Dereference (Loc, P),
1932 Parameter_Associations => New_List);
1934 -- If the prefix is overloaded, remove operations that have formals,
1935 -- we know that this is a parameterless call.
1937 if Is_Overloaded (P) then
1938 Get_First_Interp (P, I, It);
1939 while Present (It.Nam) loop
1942 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1948 Get_Next_Interp (I, It);
1955 elsif not Is_Function_Type
1956 and then Is_Overloaded (N)
1958 -- The prefix may include access to subprograms and other access
1959 -- types. If the context selects the interpretation that is a
1960 -- function call (not a procedure call) we cannot rewrite the node
1961 -- yet, but we include the result of the call interpretation.
1963 Get_First_Interp (N, I, It);
1964 while Present (It.Nam) loop
1965 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1966 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1967 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1969 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1972 Get_Next_Interp (I, It);
1976 -- A value of remote access-to-class-wide must not be dereferenced
1979 Validate_Remote_Access_To_Class_Wide_Type (N);
1980 end Analyze_Explicit_Dereference;
1982 ------------------------
1983 -- Analyze_Expression --
1984 ------------------------
1986 procedure Analyze_Expression (N : Node_Id) is
1989 Check_Parameterless_Call (N);
1990 end Analyze_Expression;
1992 -------------------------------------
1993 -- Analyze_Expression_With_Actions --
1994 -------------------------------------
1996 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2000 A := First (Actions (N));
2001 while Present (A) loop
2006 -- We currently hijack Expression_With_Actions with a VOID type and
2007 -- a NULL statement in the Expression. This will ultimately be replaced
2008 -- by a proper separate N_Compound_Statement node, at which point the
2009 -- test below can go away???
2011 if Nkind (Expression (N)) = N_Null_Statement then
2012 Set_Etype (N, Standard_Void_Type);
2014 Analyze_Expression (Expression (N));
2015 Set_Etype (N, Etype (Expression (N)));
2017 end Analyze_Expression_With_Actions;
2019 ---------------------------
2020 -- Analyze_If_Expression --
2021 ---------------------------
2023 procedure Analyze_If_Expression (N : Node_Id) is
2024 Condition : constant Node_Id := First (Expressions (N));
2025 Then_Expr : constant Node_Id := Next (Condition);
2026 Else_Expr : Node_Id;
2029 -- Defend against error of missing expressions from previous error
2031 if No (Then_Expr) then
2032 Check_Error_Detected;
2036 if Comes_From_Source (N) then
2037 Check_SPARK_Restriction ("if expression is not allowed", N);
2040 Else_Expr := Next (Then_Expr);
2042 if Comes_From_Source (N) then
2043 Check_Compiler_Unit (N);
2046 Analyze_Expression (Condition);
2047 Analyze_Expression (Then_Expr);
2049 if Present (Else_Expr) then
2050 Analyze_Expression (Else_Expr);
2053 -- If then expression not overloaded, then that decides the type
2055 if not Is_Overloaded (Then_Expr) then
2056 Set_Etype (N, Etype (Then_Expr));
2058 -- Case where then expression is overloaded
2066 Set_Etype (N, Any_Type);
2068 -- Shouldn't the following statement be down in the ELSE of the
2069 -- following loop? ???
2071 Get_First_Interp (Then_Expr, I, It);
2073 -- if no Else_Expression the conditional must be boolean
2075 if No (Else_Expr) then
2076 Set_Etype (N, Standard_Boolean);
2078 -- Else_Expression Present. For each possible intepretation of
2079 -- the Then_Expression, add it only if the Else_Expression has
2080 -- a compatible type.
2083 while Present (It.Nam) loop
2084 if Has_Compatible_Type (Else_Expr, It.Typ) then
2085 Add_One_Interp (N, It.Typ, It.Typ);
2088 Get_Next_Interp (I, It);
2093 end Analyze_If_Expression;
2095 ------------------------------------
2096 -- Analyze_Indexed_Component_Form --
2097 ------------------------------------
2099 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2100 P : constant Node_Id := Prefix (N);
2101 Exprs : constant List_Id := Expressions (N);
2107 procedure Process_Function_Call;
2108 -- Prefix in indexed component form is an overloadable entity,
2109 -- so the node is a function call. Reformat it as such.
2111 procedure Process_Indexed_Component;
2112 -- Prefix in indexed component form is actually an indexed component.
2113 -- This routine processes it, knowing that the prefix is already
2116 procedure Process_Indexed_Component_Or_Slice;
2117 -- An indexed component with a single index may designate a slice if
2118 -- the index is a subtype mark. This routine disambiguates these two
2119 -- cases by resolving the prefix to see if it is a subtype mark.
2121 procedure Process_Overloaded_Indexed_Component;
2122 -- If the prefix of an indexed component is overloaded, the proper
2123 -- interpretation is selected by the index types and the context.
2125 ---------------------------
2126 -- Process_Function_Call --
2127 ---------------------------
2129 procedure Process_Function_Call is
2133 Change_Node (N, N_Function_Call);
2135 Set_Parameter_Associations (N, Exprs);
2137 -- Analyze actuals prior to analyzing the call itself
2139 Actual := First (Parameter_Associations (N));
2140 while Present (Actual) loop
2142 Check_Parameterless_Call (Actual);
2144 -- Move to next actual. Note that we use Next, not Next_Actual
2145 -- here. The reason for this is a bit subtle. If a function call
2146 -- includes named associations, the parser recognizes the node as
2147 -- a call, and it is analyzed as such. If all associations are
2148 -- positional, the parser builds an indexed_component node, and
2149 -- it is only after analysis of the prefix that the construct
2150 -- is recognized as a call, in which case Process_Function_Call
2151 -- rewrites the node and analyzes the actuals. If the list of
2152 -- actuals is malformed, the parser may leave the node as an
2153 -- indexed component (despite the presence of named associations).
2154 -- The iterator Next_Actual is equivalent to Next if the list is
2155 -- positional, but follows the normalized chain of actuals when
2156 -- named associations are present. In this case normalization has
2157 -- not taken place, and actuals remain unanalyzed, which leads to
2158 -- subsequent crashes or loops if there is an attempt to continue
2159 -- analysis of the program.
2165 end Process_Function_Call;
2167 -------------------------------
2168 -- Process_Indexed_Component --
2169 -------------------------------
2171 procedure Process_Indexed_Component is
2173 Array_Type : Entity_Id;
2175 Pent : Entity_Id := Empty;
2178 Exp := First (Exprs);
2180 if Is_Overloaded (P) then
2181 Process_Overloaded_Indexed_Component;
2184 Array_Type := Etype (P);
2186 if Is_Entity_Name (P) then
2188 elsif Nkind (P) = N_Selected_Component
2189 and then Is_Entity_Name (Selector_Name (P))
2191 Pent := Entity (Selector_Name (P));
2194 -- Prefix must be appropriate for an array type, taking into
2195 -- account a possible implicit dereference.
2197 if Is_Access_Type (Array_Type) then
2199 (Warn_On_Dereference, "?d?implicit dereference", N);
2200 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2203 if Is_Array_Type (Array_Type) then
2206 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2208 Set_Etype (N, Any_Type);
2210 if not Has_Compatible_Type
2211 (Exp, Entry_Index_Type (Pent))
2213 Error_Msg_N ("invalid index type in entry name", N);
2215 elsif Present (Next (Exp)) then
2216 Error_Msg_N ("too many subscripts in entry reference", N);
2219 Set_Etype (N, Etype (P));
2224 elsif Is_Record_Type (Array_Type)
2225 and then Remote_AST_I_Dereference (P)
2229 elsif Try_Container_Indexing (N, P, Exprs) then
2232 elsif Array_Type = Any_Type then
2233 Set_Etype (N, Any_Type);
2235 -- In most cases the analysis of the prefix will have emitted
2236 -- an error already, but if the prefix may be interpreted as a
2237 -- call in prefixed notation, the report is left to the caller.
2238 -- To prevent cascaded errors, report only if no previous ones.
2240 if Serious_Errors_Detected = 0 then
2241 Error_Msg_N ("invalid prefix in indexed component", P);
2243 if Nkind (P) = N_Expanded_Name then
2244 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2250 -- Here we definitely have a bad indexing
2253 if Nkind (Parent (N)) = N_Requeue_Statement
2254 and then Present (Pent) and then Ekind (Pent) = E_Entry
2257 ("REQUEUE does not permit parameters", First (Exprs));
2259 elsif Is_Entity_Name (P)
2260 and then Etype (P) = Standard_Void_Type
2262 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2265 Error_Msg_N ("array type required in indexed component", P);
2268 Set_Etype (N, Any_Type);
2272 Index := First_Index (Array_Type);
2273 while Present (Index) and then Present (Exp) loop
2274 if not Has_Compatible_Type (Exp, Etype (Index)) then
2275 Wrong_Type (Exp, Etype (Index));
2276 Set_Etype (N, Any_Type);
2284 Set_Etype (N, Component_Type (Array_Type));
2285 Check_Implicit_Dereference (N, Etype (N));
2287 if Present (Index) then
2289 ("too few subscripts in array reference", First (Exprs));
2291 elsif Present (Exp) then
2292 Error_Msg_N ("too many subscripts in array reference", Exp);
2295 end Process_Indexed_Component;
2297 ----------------------------------------
2298 -- Process_Indexed_Component_Or_Slice --
2299 ----------------------------------------
2301 procedure Process_Indexed_Component_Or_Slice is
2303 Exp := First (Exprs);
2304 while Present (Exp) loop
2305 Analyze_Expression (Exp);
2309 Exp := First (Exprs);
2311 -- If one index is present, and it is a subtype name, then the
2312 -- node denotes a slice (note that the case of an explicit range
2313 -- for a slice was already built as an N_Slice node in the first
2314 -- place, so that case is not handled here).
2316 -- We use a replace rather than a rewrite here because this is one
2317 -- of the cases in which the tree built by the parser is plain wrong.
2320 and then Is_Entity_Name (Exp)
2321 and then Is_Type (Entity (Exp))
2324 Make_Slice (Sloc (N),
2326 Discrete_Range => New_Copy (Exp)));
2329 -- Otherwise (more than one index present, or single index is not
2330 -- a subtype name), then we have the indexed component case.
2333 Process_Indexed_Component;
2335 end Process_Indexed_Component_Or_Slice;
2337 ------------------------------------------
2338 -- Process_Overloaded_Indexed_Component --
2339 ------------------------------------------
2341 procedure Process_Overloaded_Indexed_Component is
2350 Set_Etype (N, Any_Type);
2352 Get_First_Interp (P, I, It);
2353 while Present (It.Nam) loop
2356 if Is_Access_Type (Typ) then
2357 Typ := Designated_Type (Typ);
2359 (Warn_On_Dereference, "?d?implicit dereference", N);
2362 if Is_Array_Type (Typ) then
2364 -- Got a candidate: verify that index types are compatible
2366 Index := First_Index (Typ);
2368 Exp := First (Exprs);
2369 while Present (Index) and then Present (Exp) loop
2370 if Has_Compatible_Type (Exp, Etype (Index)) then
2382 if Found and then No (Index) and then No (Exp) then
2384 CT : constant Entity_Id :=
2385 Base_Type (Component_Type (Typ));
2387 Add_One_Interp (N, CT, CT);
2388 Check_Implicit_Dereference (N, CT);
2392 elsif Try_Container_Indexing (N, P, Exprs) then
2397 Get_Next_Interp (I, It);
2400 if Etype (N) = Any_Type then
2401 Error_Msg_N ("no legal interpretation for indexed component", N);
2402 Set_Is_Overloaded (N, False);
2406 end Process_Overloaded_Indexed_Component;
2408 -- Start of processing for Analyze_Indexed_Component_Form
2411 -- Get name of array, function or type
2415 -- If P is an explicit dereference whose prefix is of a remote access-
2416 -- to-subprogram type, then N has already been rewritten as a subprogram
2417 -- call and analyzed.
2419 if Nkind (N) in N_Subprogram_Call then
2422 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2423 -- the indexed component denotes a loop name, the indexed form is turned
2424 -- into an attribute reference.
2426 elsif Nkind (N) = N_Attribute_Reference
2427 and then Attribute_Name (N) = Name_Loop_Entry
2432 pragma Assert (Nkind (N) = N_Indexed_Component);
2434 P_T := Base_Type (Etype (P));
2436 if Is_Entity_Name (P) and then Present (Entity (P)) then
2439 if Is_Type (U_N) then
2441 -- Reformat node as a type conversion
2443 E := Remove_Head (Exprs);
2445 if Present (First (Exprs)) then
2447 ("argument of type conversion must be single expression", N);
2450 Change_Node (N, N_Type_Conversion);
2451 Set_Subtype_Mark (N, P);
2453 Set_Expression (N, E);
2455 -- After changing the node, call for the specific Analysis
2456 -- routine directly, to avoid a double call to the expander.
2458 Analyze_Type_Conversion (N);
2462 if Is_Overloadable (U_N) then
2463 Process_Function_Call;
2465 elsif Ekind (Etype (P)) = E_Subprogram_Type
2466 or else (Is_Access_Type (Etype (P))
2468 Ekind (Designated_Type (Etype (P))) =
2471 -- Call to access_to-subprogram with possible implicit dereference
2473 Process_Function_Call;
2475 elsif Is_Generic_Subprogram (U_N) then
2477 -- A common beginner's (or C++ templates fan) error
2479 Error_Msg_N ("generic subprogram cannot be called", N);
2480 Set_Etype (N, Any_Type);
2484 Process_Indexed_Component_Or_Slice;
2487 -- If not an entity name, prefix is an expression that may denote
2488 -- an array or an access-to-subprogram.
2491 if Ekind (P_T) = E_Subprogram_Type
2492 or else (Is_Access_Type (P_T)
2494 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2496 Process_Function_Call;
2498 elsif Nkind (P) = N_Selected_Component
2499 and then Present (Entity (Selector_Name (P)))
2500 and then Is_Overloadable (Entity (Selector_Name (P)))
2502 Process_Function_Call;
2504 -- In ASIS mode within a generic, a prefixed call is analyzed and
2505 -- partially rewritten but the original indexed component has not
2506 -- yet been rewritten as a call. Perform the replacement now.
2508 elsif Nkind (P) = N_Selected_Component
2509 and then Nkind (Parent (P)) = N_Function_Call
2512 Rewrite (N, Parent (P));
2516 -- Indexed component, slice, or a call to a member of a family
2517 -- entry, which will be converted to an entry call later.
2519 Process_Indexed_Component_Or_Slice;
2523 Analyze_Dimension (N);
2524 end Analyze_Indexed_Component_Form;
2526 ------------------------
2527 -- Analyze_Logical_Op --
2528 ------------------------
2530 procedure Analyze_Logical_Op (N : Node_Id) is
2531 L : constant Node_Id := Left_Opnd (N);
2532 R : constant Node_Id := Right_Opnd (N);
2533 Op_Id : Entity_Id := Entity (N);
2536 Set_Etype (N, Any_Type);
2537 Candidate_Type := Empty;
2539 Analyze_Expression (L);
2540 Analyze_Expression (R);
2542 if Present (Op_Id) then
2544 if Ekind (Op_Id) = E_Operator then
2545 Find_Boolean_Types (L, R, Op_Id, N);
2547 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2551 Op_Id := Get_Name_Entity_Id (Chars (N));
2552 while Present (Op_Id) loop
2553 if Ekind (Op_Id) = E_Operator then
2554 Find_Boolean_Types (L, R, Op_Id, N);
2556 Analyze_User_Defined_Binary_Op (N, Op_Id);
2559 Op_Id := Homonym (Op_Id);
2564 end Analyze_Logical_Op;
2566 ---------------------------
2567 -- Analyze_Membership_Op --
2568 ---------------------------
2570 procedure Analyze_Membership_Op (N : Node_Id) is
2571 Loc : constant Source_Ptr := Sloc (N);
2572 L : constant Node_Id := Left_Opnd (N);
2573 R : constant Node_Id := Right_Opnd (N);
2575 Index : Interp_Index;
2577 Found : Boolean := False;
2581 procedure Try_One_Interp (T1 : Entity_Id);
2582 -- Routine to try one proposed interpretation. Note that the context
2583 -- of the operation plays no role in resolving the arguments, so that
2584 -- if there is more than one interpretation of the operands that is
2585 -- compatible with a membership test, the operation is ambiguous.
2587 --------------------
2588 -- Try_One_Interp --
2589 --------------------
2591 procedure Try_One_Interp (T1 : Entity_Id) is
2593 if Has_Compatible_Type (R, T1) then
2595 and then Base_Type (T1) /= Base_Type (T_F)
2597 It := Disambiguate (L, I_F, Index, Any_Type);
2599 if It = No_Interp then
2600 Ambiguous_Operands (N);
2601 Set_Etype (L, Any_Type);
2618 procedure Analyze_Set_Membership;
2619 -- If a set of alternatives is present, analyze each and find the
2620 -- common type to which they must all resolve.
2622 ----------------------------
2623 -- Analyze_Set_Membership --
2624 ----------------------------
2626 procedure Analyze_Set_Membership is
2628 Index : Interp_Index;
2630 Candidate_Interps : Node_Id;
2631 Common_Type : Entity_Id := Empty;
2635 Candidate_Interps := L;
2637 if not Is_Overloaded (L) then
2638 Common_Type := Etype (L);
2640 Alt := First (Alternatives (N));
2641 while Present (Alt) loop
2644 if not Has_Compatible_Type (Alt, Common_Type) then
2645 Wrong_Type (Alt, Common_Type);
2652 Alt := First (Alternatives (N));
2653 while Present (Alt) loop
2655 if not Is_Overloaded (Alt) then
2656 Common_Type := Etype (Alt);
2659 Get_First_Interp (Alt, Index, It);
2660 while Present (It.Typ) loop
2662 Has_Compatible_Type (Candidate_Interps, It.Typ)
2664 Remove_Interp (Index);
2667 Get_Next_Interp (Index, It);
2670 Get_First_Interp (Alt, Index, It);
2673 Error_Msg_N ("alternative has no legal type", Alt);
2677 -- If alternative is not overloaded, we have a unique type
2680 Set_Etype (Alt, It.Typ);
2681 Get_Next_Interp (Index, It);
2684 Set_Is_Overloaded (Alt, False);
2685 Common_Type := Etype (Alt);
2688 Candidate_Interps := Alt;
2695 Set_Etype (N, Standard_Boolean);
2697 if Present (Common_Type) then
2698 Set_Etype (L, Common_Type);
2699 Set_Is_Overloaded (L, False);
2702 Error_Msg_N ("cannot resolve membership operation", N);
2704 end Analyze_Set_Membership;
2706 -- Start of processing for Analyze_Membership_Op
2709 Analyze_Expression (L);
2712 and then Ada_Version >= Ada_2012
2714 Analyze_Set_Membership;
2718 if Nkind (R) = N_Range
2719 or else (Nkind (R) = N_Attribute_Reference
2720 and then Attribute_Name (R) = Name_Range)
2724 if not Is_Overloaded (L) then
2725 Try_One_Interp (Etype (L));
2728 Get_First_Interp (L, Index, It);
2729 while Present (It.Typ) loop
2730 Try_One_Interp (It.Typ);
2731 Get_Next_Interp (Index, It);
2735 -- If not a range, it can be a subtype mark, or else it is a degenerate
2736 -- membership test with a singleton value, i.e. a test for equality,
2737 -- if the types are compatible.
2742 if Is_Entity_Name (R)
2743 and then Is_Type (Entity (R))
2746 Check_Fully_Declared (Entity (R), R);
2748 elsif Ada_Version >= Ada_2012
2749 and then Has_Compatible_Type (R, Etype (L))
2751 if Nkind (N) = N_In then
2767 -- In all versions of the language, if we reach this point there
2768 -- is a previous error that will be diagnosed below.
2774 -- Compatibility between expression and subtype mark or range is
2775 -- checked during resolution. The result of the operation is Boolean
2778 Set_Etype (N, Standard_Boolean);
2780 if Comes_From_Source (N)
2781 and then Present (Right_Opnd (N))
2782 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2784 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2786 end Analyze_Membership_Op;
2792 procedure Analyze_Mod (N : Node_Id) is
2794 -- A special warning check, if we have an expression of the form:
2795 -- expr mod 2 * literal
2796 -- where literal is 64 or less, then probably what was meant was
2797 -- expr mod 2 ** literal
2798 -- so issue an appropriate warning.
2800 if Warn_On_Suspicious_Modulus_Value
2801 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2802 and then Intval (Right_Opnd (N)) = Uint_2
2803 and then Nkind (Parent (N)) = N_Op_Multiply
2804 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2805 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2808 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
2811 -- Remaining processing is same as for other arithmetic operators
2813 Analyze_Arithmetic_Op (N);
2816 ----------------------
2817 -- Analyze_Negation --
2818 ----------------------
2820 procedure Analyze_Negation (N : Node_Id) is
2821 R : constant Node_Id := Right_Opnd (N);
2822 Op_Id : Entity_Id := Entity (N);
2825 Set_Etype (N, Any_Type);
2826 Candidate_Type := Empty;
2828 Analyze_Expression (R);
2830 if Present (Op_Id) then
2831 if Ekind (Op_Id) = E_Operator then
2832 Find_Negation_Types (R, Op_Id, N);
2834 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2838 Op_Id := Get_Name_Entity_Id (Chars (N));
2839 while Present (Op_Id) loop
2840 if Ekind (Op_Id) = E_Operator then
2841 Find_Negation_Types (R, Op_Id, N);
2843 Analyze_User_Defined_Unary_Op (N, Op_Id);
2846 Op_Id := Homonym (Op_Id);
2851 end Analyze_Negation;
2857 procedure Analyze_Null (N : Node_Id) is
2859 Check_SPARK_Restriction ("null is not allowed", N);
2861 Set_Etype (N, Any_Access);
2864 ----------------------
2865 -- Analyze_One_Call --
2866 ----------------------
2868 procedure Analyze_One_Call
2872 Success : out Boolean;
2873 Skip_First : Boolean := False)
2875 Actuals : constant List_Id := Parameter_Associations (N);
2876 Prev_T : constant Entity_Id := Etype (N);
2878 Must_Skip : constant Boolean := Skip_First
2879 or else Nkind (Original_Node (N)) = N_Selected_Component
2881 (Nkind (Original_Node (N)) = N_Indexed_Component
2882 and then Nkind (Prefix (Original_Node (N)))
2883 = N_Selected_Component);
2884 -- The first formal must be omitted from the match when trying to find
2885 -- a primitive operation that is a possible interpretation, and also
2886 -- after the call has been rewritten, because the corresponding actual
2887 -- is already known to be compatible, and because this may be an
2888 -- indexing of a call with default parameters.
2892 Is_Indexed : Boolean := False;
2893 Is_Indirect : Boolean := False;
2894 Subp_Type : constant Entity_Id := Etype (Nam);
2897 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2898 -- There may be a user-defined operator that hides the current
2899 -- interpretation. We must check for this independently of the
2900 -- analysis of the call with the user-defined operation, because
2901 -- the parameter names may be wrong and yet the hiding takes place.
2902 -- This fixes a problem with ACATS test B34014O.
2904 -- When the type Address is a visible integer type, and the DEC
2905 -- system extension is visible, the predefined operator may be
2906 -- hidden as well, by one of the address operations in auxdec.
2907 -- Finally, The abstract operations on address do not hide the
2908 -- predefined operator (this is the purpose of making them abstract).
2910 procedure Indicate_Name_And_Type;
2911 -- If candidate interpretation matches, indicate name and type of
2912 -- result on call node.
2914 ----------------------------
2915 -- Indicate_Name_And_Type --
2916 ----------------------------
2918 procedure Indicate_Name_And_Type is
2920 Add_One_Interp (N, Nam, Etype (Nam));
2921 Check_Implicit_Dereference (N, Etype (Nam));
2924 -- If the prefix of the call is a name, indicate the entity
2925 -- being called. If it is not a name, it is an expression that
2926 -- denotes an access to subprogram or else an entry or family. In
2927 -- the latter case, the name is a selected component, and the entity
2928 -- being called is noted on the selector.
2930 if not Is_Type (Nam) then
2931 if Is_Entity_Name (Name (N)) then
2932 Set_Entity (Name (N), Nam);
2934 elsif Nkind (Name (N)) = N_Selected_Component then
2935 Set_Entity (Selector_Name (Name (N)), Nam);
2939 if Debug_Flag_E and not Report then
2940 Write_Str (" Overloaded call ");
2941 Write_Int (Int (N));
2942 Write_Str (" compatible with ");
2943 Write_Int (Int (Nam));
2946 end Indicate_Name_And_Type;
2948 ------------------------
2949 -- Operator_Hidden_By --
2950 ------------------------
2952 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2953 Act1 : constant Node_Id := First_Actual (N);
2954 Act2 : constant Node_Id := Next_Actual (Act1);
2955 Form1 : constant Entity_Id := First_Formal (Fun);
2956 Form2 : constant Entity_Id := Next_Formal (Form1);
2959 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
2962 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2965 elsif Present (Form2) then
2967 or else not Has_Compatible_Type (Act2, Etype (Form2))
2972 elsif Present (Act2) then
2976 -- Now we know that the arity of the operator matches the function,
2977 -- and the function call is a valid interpretation. The function
2978 -- hides the operator if it has the right signature, or if one of
2979 -- its operands is a non-abstract operation on Address when this is
2980 -- a visible integer type.
2982 return Hides_Op (Fun, Nam)
2983 or else Is_Descendent_Of_Address (Etype (Form1))
2986 and then Is_Descendent_Of_Address (Etype (Form2)));
2987 end Operator_Hidden_By;
2989 -- Start of processing for Analyze_One_Call
2994 -- If the subprogram has no formals or if all the formals have defaults,
2995 -- and the return type is an array type, the node may denote an indexing
2996 -- of the result of a parameterless call. In Ada 2005, the subprogram
2997 -- may have one non-defaulted formal, and the call may have been written
2998 -- in prefix notation, so that the rebuilt parameter list has more than
3001 if not Is_Overloadable (Nam)
3002 and then Ekind (Nam) /= E_Subprogram_Type
3003 and then Ekind (Nam) /= E_Entry_Family
3008 -- An indexing requires at least one actual. The name of the call cannot
3009 -- be an implicit indirect call, so it cannot be a generated explicit
3012 if not Is_Empty_List (Actuals)
3014 (Needs_No_Actuals (Nam)
3016 (Needs_One_Actual (Nam)
3017 and then Present (Next_Actual (First (Actuals)))))
3019 if Is_Array_Type (Subp_Type)
3021 (Nkind (Name (N)) /= N_Explicit_Dereference
3022 or else Comes_From_Source (Name (N)))
3024 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3026 elsif Is_Access_Type (Subp_Type)
3027 and then Is_Array_Type (Designated_Type (Subp_Type))
3031 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3033 -- The prefix can also be a parameterless function that returns an
3034 -- access to subprogram, in which case this is an indirect call.
3035 -- If this succeeds, an explicit dereference is added later on,
3036 -- in Analyze_Call or Resolve_Call.
3038 elsif Is_Access_Type (Subp_Type)
3039 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3041 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3046 -- If the call has been transformed into a slice, it is of the form
3047 -- F (Subtype) where F is parameterless. The node has been rewritten in
3048 -- Try_Indexed_Call and there is nothing else to do.
3051 and then Nkind (N) = N_Slice
3057 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3061 -- If an indirect call is a possible interpretation, indicate
3062 -- success to the caller. This may be an indexing of an explicit
3063 -- dereference of a call that returns an access type (see above).
3067 and then Nkind (Name (N)) = N_Explicit_Dereference
3068 and then Comes_From_Source (Name (N)))
3073 -- Mismatch in number or names of parameters
3075 elsif Debug_Flag_E then
3076 Write_Str (" normalization fails in call ");
3077 Write_Int (Int (N));
3078 Write_Str (" with subprogram ");
3079 Write_Int (Int (Nam));
3083 -- If the context expects a function call, discard any interpretation
3084 -- that is a procedure. If the node is not overloaded, leave as is for
3085 -- better error reporting when type mismatch is found.
3087 elsif Nkind (N) = N_Function_Call
3088 and then Is_Overloaded (Name (N))
3089 and then Ekind (Nam) = E_Procedure
3093 -- Ditto for function calls in a procedure context
3095 elsif Nkind (N) = N_Procedure_Call_Statement
3096 and then Is_Overloaded (Name (N))
3097 and then Etype (Nam) /= Standard_Void_Type
3101 elsif No (Actuals) then
3103 -- If Normalize succeeds, then there are default parameters for
3106 Indicate_Name_And_Type;
3108 elsif Ekind (Nam) = E_Operator then
3109 if Nkind (N) = N_Procedure_Call_Statement then
3113 -- This can occur when the prefix of the call is an operator
3114 -- name or an expanded name whose selector is an operator name.
3116 Analyze_Operator_Call (N, Nam);
3118 if Etype (N) /= Prev_T then
3120 -- Check that operator is not hidden by a function interpretation
3122 if Is_Overloaded (Name (N)) then
3128 Get_First_Interp (Name (N), I, It);
3129 while Present (It.Nam) loop
3130 if Operator_Hidden_By (It.Nam) then
3131 Set_Etype (N, Prev_T);
3135 Get_Next_Interp (I, It);
3140 -- If operator matches formals, record its name on the call.
3141 -- If the operator is overloaded, Resolve will select the
3142 -- correct one from the list of interpretations. The call
3143 -- node itself carries the first candidate.
3145 Set_Entity (Name (N), Nam);
3148 elsif Report and then Etype (N) = Any_Type then
3149 Error_Msg_N ("incompatible arguments for operator", N);
3153 -- Normalize_Actuals has chained the named associations in the
3154 -- correct order of the formals.
3156 Actual := First_Actual (N);
3157 Formal := First_Formal (Nam);
3159 -- If we are analyzing a call rewritten from object notation, skip
3160 -- first actual, which may be rewritten later as an explicit
3164 Next_Actual (Actual);
3165 Next_Formal (Formal);
3168 while Present (Actual) and then Present (Formal) loop
3169 if Nkind (Parent (Actual)) /= N_Parameter_Association
3170 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3172 -- The actual can be compatible with the formal, but we must
3173 -- also check that the context is not an address type that is
3174 -- visibly an integer type, as is the case in VMS_64. In this
3175 -- case the use of literals is illegal, except in the body of
3176 -- descendents of system, where arithmetic operations on
3177 -- address are of course used.
3179 if Has_Compatible_Type (Actual, Etype (Formal))
3181 (Etype (Actual) /= Universal_Integer
3182 or else not Is_Descendent_Of_Address (Etype (Formal))
3184 Is_Predefined_File_Name
3185 (Unit_File_Name (Get_Source_Unit (N))))
3187 Next_Actual (Actual);
3188 Next_Formal (Formal);
3190 -- In Allow_Integer_Address mode, we allow an actual integer to
3191 -- match a formal address type and vice versa. We only do this
3192 -- if we are certain that an error will otherwise be issued
3194 elsif Address_Integer_Convert_OK
3195 (Etype (Actual), Etype (Formal))
3196 and then (Report and not Is_Indexed and not Is_Indirect)
3198 -- Handle this case by introducing an unchecked conversion
3201 Unchecked_Convert_To (Etype (Formal),
3202 Relocate_Node (Actual)));
3203 Analyze_And_Resolve (Actual, Etype (Formal));
3204 Next_Actual (Actual);
3205 Next_Formal (Formal);
3208 if Debug_Flag_E then
3209 Write_Str (" type checking fails in call ");
3210 Write_Int (Int (N));
3211 Write_Str (" with formal ");
3212 Write_Int (Int (Formal));
3213 Write_Str (" in subprogram ");
3214 Write_Int (Int (Nam));
3218 -- Comment needed on the following test???
3220 if Report and not Is_Indexed and not Is_Indirect then
3222 -- Ada 2005 (AI-251): Complete the error notification
3223 -- to help new Ada 2005 users.
3225 if Is_Class_Wide_Type (Etype (Formal))
3226 and then Is_Interface (Etype (Etype (Formal)))
3227 and then not Interface_Present_In_Ancestor
3228 (Typ => Etype (Actual),
3229 Iface => Etype (Etype (Formal)))
3232 ("(Ada 2005) does not implement interface }",
3233 Actual, Etype (Etype (Formal)));
3236 Wrong_Type (Actual, Etype (Formal));
3238 if Nkind (Actual) = N_Op_Eq
3239 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3241 Formal := First_Formal (Nam);
3242 while Present (Formal) loop
3243 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3244 Error_Msg_N -- CODEFIX
3245 ("possible misspelling of `='>`!", Actual);
3249 Next_Formal (Formal);
3253 if All_Errors_Mode then
3254 Error_Msg_Sloc := Sloc (Nam);
3256 if Etype (Formal) = Any_Type then
3258 ("there is no legal actual parameter", Actual);
3261 if Is_Overloadable (Nam)
3262 and then Present (Alias (Nam))
3263 and then not Comes_From_Source (Nam)
3266 ("\\ =='> in call to inherited operation & #!",
3269 elsif Ekind (Nam) = E_Subprogram_Type then
3271 Access_To_Subprogram_Typ :
3272 constant Entity_Id :=
3274 (Associated_Node_For_Itype (Nam));
3277 "\\ =='> in call to dereference of &#!",
3278 Actual, Access_To_Subprogram_Typ);
3283 ("\\ =='> in call to &#!", Actual, Nam);
3293 -- Normalize_Actuals has verified that a default value exists
3294 -- for this formal. Current actual names a subsequent formal.
3296 Next_Formal (Formal);
3300 -- On exit, all actuals match
3302 Indicate_Name_And_Type;
3304 end Analyze_One_Call;
3306 ---------------------------
3307 -- Analyze_Operator_Call --
3308 ---------------------------
3310 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3311 Op_Name : constant Name_Id := Chars (Op_Id);
3312 Act1 : constant Node_Id := First_Actual (N);
3313 Act2 : constant Node_Id := Next_Actual (Act1);
3316 -- Binary operator case
3318 if Present (Act2) then
3320 -- If more than two operands, then not binary operator after all
3322 if Present (Next_Actual (Act2)) then
3326 -- Otherwise action depends on operator
3336 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3341 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3347 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3351 Find_Equality_Types (Act1, Act2, Op_Id, N);
3353 when Name_Op_Concat =>
3354 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3356 -- Is this when others, or should it be an abort???
3362 -- Unary operator case
3366 when Name_Op_Subtract |
3369 Find_Unary_Types (Act1, Op_Id, N);
3372 Find_Negation_Types (Act1, Op_Id, N);
3374 -- Is this when others correct, or should it be an abort???
3380 end Analyze_Operator_Call;
3382 -------------------------------------------
3383 -- Analyze_Overloaded_Selected_Component --
3384 -------------------------------------------
3386 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3387 Nam : constant Node_Id := Prefix (N);
3388 Sel : constant Node_Id := Selector_Name (N);
3395 Set_Etype (Sel, Any_Type);
3397 Get_First_Interp (Nam, I, It);
3398 while Present (It.Typ) loop
3399 if Is_Access_Type (It.Typ) then
3400 T := Designated_Type (It.Typ);
3401 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3406 -- Locate the component. For a private prefix the selector can denote
3409 if Is_Record_Type (T) or else Is_Private_Type (T) then
3411 -- If the prefix is a class-wide type, the visible components are
3412 -- those of the base type.
3414 if Is_Class_Wide_Type (T) then
3418 Comp := First_Entity (T);
3419 while Present (Comp) loop
3420 if Chars (Comp) = Chars (Sel)
3421 and then Is_Visible_Component (Comp)
3424 -- AI05-105: if the context is an object renaming with
3425 -- an anonymous access type, the expected type of the
3426 -- object must be anonymous. This is a name resolution rule.
3428 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3429 or else No (Access_Definition (Parent (N)))
3430 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3432 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3434 Set_Entity (Sel, Comp);
3435 Set_Etype (Sel, Etype (Comp));
3436 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3437 Check_Implicit_Dereference (N, Etype (Comp));
3439 -- This also specifies a candidate to resolve the name.
3440 -- Further overloading will be resolved from context.
3441 -- The selector name itself does not carry overloading
3444 Set_Etype (Nam, It.Typ);
3447 -- Named access type in the context of a renaming
3448 -- declaration with an access definition. Remove
3449 -- inapplicable candidate.
3458 elsif Is_Concurrent_Type (T) then
3459 Comp := First_Entity (T);
3460 while Present (Comp)
3461 and then Comp /= First_Private_Entity (T)
3463 if Chars (Comp) = Chars (Sel) then
3464 if Is_Overloadable (Comp) then
3465 Add_One_Interp (Sel, Comp, Etype (Comp));
3467 Set_Entity_With_Style_Check (Sel, Comp);
3468 Generate_Reference (Comp, Sel);
3471 Set_Etype (Sel, Etype (Comp));
3472 Set_Etype (N, Etype (Comp));
3473 Set_Etype (Nam, It.Typ);
3475 -- For access type case, introduce explicit dereference for
3476 -- more uniform treatment of entry calls. Do this only once
3477 -- if several interpretations yield an access type.
3479 if Is_Access_Type (Etype (Nam))
3480 and then Nkind (Nam) /= N_Explicit_Dereference
3482 Insert_Explicit_Dereference (Nam);
3484 (Warn_On_Dereference, "?d?implicit dereference", N);
3491 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3494 Get_Next_Interp (I, It);
3497 if Etype (N) = Any_Type
3498 and then not Try_Object_Operation (N)
3500 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3501 Set_Entity (Sel, Any_Id);
3502 Set_Etype (Sel, Any_Type);
3504 end Analyze_Overloaded_Selected_Component;
3506 ----------------------------------
3507 -- Analyze_Qualified_Expression --
3508 ----------------------------------
3510 procedure Analyze_Qualified_Expression (N : Node_Id) is
3511 Mark : constant Entity_Id := Subtype_Mark (N);
3512 Expr : constant Node_Id := Expression (N);
3518 Analyze_Expression (Expr);
3520 Set_Etype (N, Any_Type);
3525 if T = Any_Type then
3529 Check_Fully_Declared (T, N);
3531 -- If expected type is class-wide, check for exact match before
3532 -- expansion, because if the expression is a dispatching call it
3533 -- may be rewritten as explicit dereference with class-wide result.
3534 -- If expression is overloaded, retain only interpretations that
3535 -- will yield exact matches.
3537 if Is_Class_Wide_Type (T) then
3538 if not Is_Overloaded (Expr) then
3539 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3540 if Nkind (Expr) = N_Aggregate then
3541 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3543 Wrong_Type (Expr, T);
3548 Get_First_Interp (Expr, I, It);
3550 while Present (It.Nam) loop
3551 if Base_Type (It.Typ) /= Base_Type (T) then
3555 Get_Next_Interp (I, It);
3561 end Analyze_Qualified_Expression;
3563 -----------------------------------
3564 -- Analyze_Quantified_Expression --
3565 -----------------------------------
3567 procedure Analyze_Quantified_Expression (N : Node_Id) is
3568 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3569 -- If the iterator is part of a quantified expression, and the range is
3570 -- known to be statically empty, emit a warning and replace expression
3571 -- with its static value. Returns True if the replacement occurs.
3573 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3574 -- Determine whether if expression If_Expr lacks an else part or if it
3575 -- has one, it evaluates to True.
3577 --------------------
3578 -- Is_Empty_Range --
3579 --------------------
3581 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
3582 Loc : constant Source_Ptr := Sloc (N);
3585 if Is_Array_Type (Typ)
3586 and then Compile_Time_Known_Bounds (Typ)
3588 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
3589 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
3591 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3593 if All_Present (N) then
3595 ("??quantified expression with ALL "
3596 & "over a null range has value True", N);
3597 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3601 ("??quantified expression with SOME "
3602 & "over a null range has value False", N);
3603 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3614 -----------------------------
3615 -- No_Else_Or_Trivial_True --
3616 -----------------------------
3618 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
3619 Else_Expr : constant Node_Id :=
3620 Next (Next (First (Expressions (If_Expr))));
3624 or else (Compile_Time_Known_Value (Else_Expr)
3625 and then Is_True (Expr_Value (Else_Expr)));
3626 end No_Else_Or_Trivial_True;
3630 Cond : constant Node_Id := Condition (N);
3631 Loop_Id : Entity_Id;
3632 QE_Scop : Entity_Id;
3634 -- Start of processing for Analyze_Quantified_Expression
3637 Check_SPARK_Restriction ("quantified expression is not allowed", N);
3639 -- Create a scope to emulate the loop-like behavior of the quantified
3640 -- expression. The scope is needed to provide proper visibility of the
3643 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
3644 Set_Etype (QE_Scop, Standard_Void_Type);
3645 Set_Scope (QE_Scop, Current_Scope);
3646 Set_Parent (QE_Scop, N);
3648 Push_Scope (QE_Scop);
3650 -- All constituents are preanalyzed and resolved to avoid untimely
3651 -- generation of various temporaries and types. Full analysis and
3652 -- expansion is carried out when the quantified expression is
3653 -- transformed into an expression with actions.
3655 if Present (Iterator_Specification (N)) then
3656 Preanalyze (Iterator_Specification (N));
3658 -- Do not proceed with the analysis when the range of iteration is
3659 -- empty. The appropriate error is issued by Is_Empty_Range.
3661 if Is_Entity_Name (Name (Iterator_Specification (N)))
3662 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
3667 else pragma Assert (Present (Loop_Parameter_Specification (N)));
3669 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
3672 Preanalyze (Loop_Par);
3674 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
3675 and then Parent (Loop_Par) /= N
3677 -- The parser cannot distinguish between a loop specification
3678 -- and an iterator specification. If after pre-analysis the
3679 -- proper form has been recognized, rewrite the expression to
3680 -- reflect the right kind. This is needed for proper ASIS
3681 -- navigation. If expansion is enabled, the transformation is
3682 -- performed when the expression is rewritten as a loop.
3684 Set_Iterator_Specification (N,
3685 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
3687 Set_Defining_Identifier (Iterator_Specification (N),
3688 Relocate_Node (Defining_Identifier (Loop_Par)));
3689 Set_Name (Iterator_Specification (N),
3690 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
3691 Set_Comes_From_Source (Iterator_Specification (N),
3692 Comes_From_Source (Loop_Parameter_Specification (N)));
3693 Set_Loop_Parameter_Specification (N, Empty);
3698 Preanalyze_And_Resolve (Cond, Standard_Boolean);
3701 Set_Etype (N, Standard_Boolean);
3703 -- Verify that the loop variable is used within the condition of the
3704 -- quantified expression.
3706 if Present (Iterator_Specification (N)) then
3707 Loop_Id := Defining_Identifier (Iterator_Specification (N));
3709 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
3712 if Warn_On_Suspicious_Contract
3713 and then not Referenced (Loop_Id, Cond)
3715 Error_Msg_N ("?T?unused variable &", Loop_Id);
3718 -- Diagnose a possible misuse of the SOME existential quantifier. When
3719 -- we have a quantified expression of the form:
3721 -- for some X => (if P then Q [else True])
3723 -- any value for X that makes P False results in the if expression being
3724 -- trivially True, and so also results in the the quantified expression
3725 -- being trivially True.
3727 if Warn_On_Suspicious_Contract
3728 and then not All_Present (N)
3729 and then Nkind (Cond) = N_If_Expression
3730 and then No_Else_Or_Trivial_True (Cond)
3732 Error_Msg_N ("?T?suspicious expression", N);
3733 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
3734 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
3736 end Analyze_Quantified_Expression;
3742 procedure Analyze_Range (N : Node_Id) is
3743 L : constant Node_Id := Low_Bound (N);
3744 H : constant Node_Id := High_Bound (N);
3745 I1, I2 : Interp_Index;
3748 procedure Check_Common_Type (T1, T2 : Entity_Id);
3749 -- Verify the compatibility of two types, and choose the
3750 -- non universal one if the other is universal.
3752 procedure Check_High_Bound (T : Entity_Id);
3753 -- Test one interpretation of the low bound against all those
3754 -- of the high bound.
3756 procedure Check_Universal_Expression (N : Node_Id);
3757 -- In Ada 83, reject bounds of a universal range that are not literals
3760 -----------------------
3761 -- Check_Common_Type --
3762 -----------------------
3764 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3766 if Covers (T1 => T1, T2 => T2)
3768 Covers (T1 => T2, T2 => T1)
3770 if T1 = Universal_Integer
3771 or else T1 = Universal_Real
3772 or else T1 = Any_Character
3774 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3777 Add_One_Interp (N, T1, T1);
3780 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3783 end Check_Common_Type;
3785 ----------------------
3786 -- Check_High_Bound --
3787 ----------------------
3789 procedure Check_High_Bound (T : Entity_Id) is
3791 if not Is_Overloaded (H) then
3792 Check_Common_Type (T, Etype (H));
3794 Get_First_Interp (H, I2, It2);
3795 while Present (It2.Typ) loop
3796 Check_Common_Type (T, It2.Typ);
3797 Get_Next_Interp (I2, It2);
3800 end Check_High_Bound;
3802 -----------------------------
3803 -- Is_Universal_Expression --
3804 -----------------------------
3806 procedure Check_Universal_Expression (N : Node_Id) is
3808 if Etype (N) = Universal_Integer
3809 and then Nkind (N) /= N_Integer_Literal
3810 and then not Is_Entity_Name (N)
3811 and then Nkind (N) /= N_Attribute_Reference
3813 Error_Msg_N ("illegal bound in discrete range", N);
3815 end Check_Universal_Expression;
3817 -- Start of processing for Analyze_Range
3820 Set_Etype (N, Any_Type);
3821 Analyze_Expression (L);
3822 Analyze_Expression (H);
3824 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3828 if not Is_Overloaded (L) then
3829 Check_High_Bound (Etype (L));
3831 Get_First_Interp (L, I1, It1);
3832 while Present (It1.Typ) loop
3833 Check_High_Bound (It1.Typ);
3834 Get_Next_Interp (I1, It1);
3838 -- If result is Any_Type, then we did not find a compatible pair
3840 if Etype (N) = Any_Type then
3841 Error_Msg_N ("incompatible types in range ", N);
3845 if Ada_Version = Ada_83
3847 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3848 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3850 Check_Universal_Expression (L);
3851 Check_Universal_Expression (H);
3854 Check_Function_Writable_Actuals (N);
3857 -----------------------
3858 -- Analyze_Reference --
3859 -----------------------
3861 procedure Analyze_Reference (N : Node_Id) is
3862 P : constant Node_Id := Prefix (N);
3865 Acc_Type : Entity_Id;
3870 -- An interesting error check, if we take the 'Reference of an object
3871 -- for which a pragma Atomic or Volatile has been given, and the type
3872 -- of the object is not Atomic or Volatile, then we are in trouble. The
3873 -- problem is that no trace of the atomic/volatile status will remain
3874 -- for the backend to respect when it deals with the resulting pointer,
3875 -- since the pointer type will not be marked atomic (it is a pointer to
3876 -- the base type of the object).
3878 -- It is not clear if that can ever occur, but in case it does, we will
3879 -- generate an error message. Not clear if this message can ever be
3880 -- generated, and pretty clear that it represents a bug if it is, still
3881 -- seems worth checking, except in CodePeer mode where we do not really
3882 -- care and don't want to bother the user.
3886 if Is_Entity_Name (P)
3887 and then Is_Object_Reference (P)
3888 and then not CodePeer_Mode
3893 if (Has_Atomic_Components (E)
3894 and then not Has_Atomic_Components (T))
3896 (Has_Volatile_Components (E)
3897 and then not Has_Volatile_Components (T))
3898 or else (Is_Atomic (E) and then not Is_Atomic (T))
3899 or else (Is_Volatile (E) and then not Is_Volatile (T))
3901 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3905 -- Carry on with normal processing
3907 Acc_Type := Create_Itype (E_Allocator_Type, N);
3908 Set_Etype (Acc_Type, Acc_Type);
3909 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3910 Set_Etype (N, Acc_Type);
3911 end Analyze_Reference;
3913 --------------------------------
3914 -- Analyze_Selected_Component --
3915 --------------------------------
3917 -- Prefix is a record type or a task or protected type. In the latter case,
3918 -- the selector must denote a visible entry.
3920 procedure Analyze_Selected_Component (N : Node_Id) is
3921 Name : constant Node_Id := Prefix (N);
3922 Sel : constant Node_Id := Selector_Name (N);
3925 Has_Candidate : Boolean := False;
3928 Pent : Entity_Id := Empty;
3929 Prefix_Type : Entity_Id;
3931 Type_To_Use : Entity_Id;
3932 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3933 -- a class-wide type, we use its root type, whose components are
3934 -- present in the class-wide type.
3936 Is_Single_Concurrent_Object : Boolean;
3937 -- Set True if the prefix is a single task or a single protected object
3939 procedure Find_Component_In_Instance (Rec : Entity_Id);
3940 -- In an instance, a component of a private extension may not be visible
3941 -- while it was visible in the generic. Search candidate scope for a
3942 -- component with the proper identifier. This is only done if all other
3943 -- searches have failed. When the match is found (it always will be),
3944 -- the Etype of both N and Sel are set from this component, and the
3945 -- entity of Sel is set to reference this component.
3947 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3948 -- It is known that the parent of N denotes a subprogram call. Comp
3949 -- is an overloadable component of the concurrent type of the prefix.
3950 -- Determine whether all formals of the parent of N and Comp are mode
3951 -- conformant. If the parent node is not analyzed yet it may be an
3952 -- indexed component rather than a function call.
3954 --------------------------------
3955 -- Find_Component_In_Instance --
3956 --------------------------------
3958 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3962 Comp := First_Component (Rec);
3963 while Present (Comp) loop
3964 if Chars (Comp) = Chars (Sel) then
3965 Set_Entity_With_Style_Check (Sel, Comp);
3966 Set_Etype (Sel, Etype (Comp));
3967 Set_Etype (N, Etype (Comp));
3971 Next_Component (Comp);
3974 -- This must succeed because code was legal in the generic
3976 raise Program_Error;
3977 end Find_Component_In_Instance;
3979 ------------------------------
3980 -- Has_Mode_Conformant_Spec --
3981 ------------------------------
3983 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3984 Comp_Param : Entity_Id;
3986 Param_Typ : Entity_Id;
3989 Comp_Param := First_Formal (Comp);
3991 if Nkind (Parent (N)) = N_Indexed_Component then
3992 Param := First (Expressions (Parent (N)));
3994 Param := First (Parameter_Associations (Parent (N)));
3997 while Present (Comp_Param)
3998 and then Present (Param)
4000 Param_Typ := Find_Parameter_Type (Param);
4002 if Present (Param_Typ)
4004 not Conforming_Types
4005 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4010 Next_Formal (Comp_Param);
4014 -- One of the specs has additional formals; there is no match, unless
4015 -- this may be an indexing of a parameterless call.
4017 -- Note that when expansion is disabled, the corresponding record
4018 -- type of synchronized types is not constructed, so that there is
4019 -- no point is attempting an interpretation as a prefixed call, as
4020 -- this is bound to fail because the primitive operations will not
4021 -- be properly located.
4023 if Present (Comp_Param) or else Present (Param) then
4024 if Needs_No_Actuals (Comp)
4025 and then Is_Array_Type (Etype (Comp))
4026 and then not Expander_Active
4035 end Has_Mode_Conformant_Spec;
4037 -- Start of processing for Analyze_Selected_Component
4040 Set_Etype (N, Any_Type);
4042 if Is_Overloaded (Name) then
4043 Analyze_Overloaded_Selected_Component (N);
4046 elsif Etype (Name) = Any_Type then
4047 Set_Entity (Sel, Any_Id);
4048 Set_Etype (Sel, Any_Type);
4052 Prefix_Type := Etype (Name);
4055 if Is_Access_Type (Prefix_Type) then
4057 -- A RACW object can never be used as prefix of a selected component
4058 -- since that means it is dereferenced without being a controlling
4059 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4060 -- reporting an error, we must check whether this is actually a
4061 -- dispatching call in prefix form.
4063 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4064 and then Comes_From_Source (N)
4066 if Try_Object_Operation (N) then
4070 ("invalid dereference of a remote access-to-class-wide value",
4074 -- Normal case of selected component applied to access type
4077 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4079 if Is_Entity_Name (Name) then
4080 Pent := Entity (Name);
4081 elsif Nkind (Name) = N_Selected_Component
4082 and then Is_Entity_Name (Selector_Name (Name))
4084 Pent := Entity (Selector_Name (Name));
4087 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4090 -- If we have an explicit dereference of a remote access-to-class-wide
4091 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4092 -- have to check for the case of a prefix that is a controlling operand
4093 -- of a prefixed dispatching call, as the dereference is legal in that
4094 -- case. Normally this condition is checked in Validate_Remote_Access_
4095 -- To_Class_Wide_Type, but we have to defer the checking for selected
4096 -- component prefixes because of the prefixed dispatching call case.
4097 -- Note that implicit dereferences are checked for this just above.
4099 elsif Nkind (Name) = N_Explicit_Dereference
4100 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4101 and then Comes_From_Source (N)
4103 if Try_Object_Operation (N) then
4107 ("invalid dereference of a remote access-to-class-wide value",
4112 -- (Ada 2005): if the prefix is the limited view of a type, and
4113 -- the context already includes the full view, use the full view
4114 -- in what follows, either to retrieve a component of to find
4115 -- a primitive operation. If the prefix is an explicit dereference,
4116 -- set the type of the prefix to reflect this transformation.
4117 -- If the non-limited view is itself an incomplete type, get the
4118 -- full view if available.
4120 if Is_Incomplete_Type (Prefix_Type)
4121 and then From_Limited_With (Prefix_Type)
4122 and then Present (Non_Limited_View (Prefix_Type))
4124 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4126 if Nkind (N) = N_Explicit_Dereference then
4127 Set_Etype (Prefix (N), Prefix_Type);
4130 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
4131 and then From_Limited_With (Prefix_Type)
4132 and then Present (Non_Limited_View (Etype (Prefix_Type)))
4135 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
4137 if Nkind (N) = N_Explicit_Dereference then
4138 Set_Etype (Prefix (N), Prefix_Type);
4142 if Ekind (Prefix_Type) = E_Private_Subtype then
4143 Prefix_Type := Base_Type (Prefix_Type);
4146 Type_To_Use := Prefix_Type;
4148 -- For class-wide types, use the entity list of the root type. This
4149 -- indirection is specially important for private extensions because
4150 -- only the root type get switched (not the class-wide type).
4152 if Is_Class_Wide_Type (Prefix_Type) then
4153 Type_To_Use := Root_Type (Prefix_Type);
4156 -- If the prefix is a single concurrent object, use its name in error
4157 -- messages, rather than that of its anonymous type.
4159 Is_Single_Concurrent_Object :=
4160 Is_Concurrent_Type (Prefix_Type)
4161 and then Is_Internal_Name (Chars (Prefix_Type))
4162 and then not Is_Derived_Type (Prefix_Type)
4163 and then Is_Entity_Name (Name);
4165 Comp := First_Entity (Type_To_Use);
4167 -- If the selector has an original discriminant, the node appears in
4168 -- an instance. Replace the discriminant with the corresponding one
4169 -- in the current discriminated type. For nested generics, this must
4170 -- be done transitively, so note the new original discriminant.
4172 if Nkind (Sel) = N_Identifier
4173 and then In_Instance
4174 and then Present (Original_Discriminant (Sel))
4176 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4178 -- Mark entity before rewriting, for completeness and because
4179 -- subsequent semantic checks might examine the original node.
4181 Set_Entity (Sel, Comp);
4182 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4183 Set_Original_Discriminant (Selector_Name (N), Comp);
4184 Set_Etype (N, Etype (Comp));
4185 Check_Implicit_Dereference (N, Etype (Comp));
4187 if Is_Access_Type (Etype (Name)) then
4188 Insert_Explicit_Dereference (Name);
4189 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4192 elsif Is_Record_Type (Prefix_Type) then
4194 -- Find component with given name. In an instance, if the node is
4195 -- known as a prefixed call, do not examine components whose
4196 -- visibility may be accidental.
4198 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4199 if Chars (Comp) = Chars (Sel)
4200 and then Is_Visible_Component (Comp, N)
4202 Set_Entity_With_Style_Check (Sel, Comp);
4203 Set_Etype (Sel, Etype (Comp));
4205 if Ekind (Comp) = E_Discriminant then
4206 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4208 ("cannot reference discriminant of unchecked union",
4212 if Is_Generic_Type (Prefix_Type)
4214 Is_Generic_Type (Root_Type (Prefix_Type))
4216 Set_Original_Discriminant (Sel, Comp);
4220 -- Resolve the prefix early otherwise it is not possible to
4221 -- build the actual subtype of the component: it may need
4222 -- to duplicate this prefix and duplication is only allowed
4223 -- on fully resolved expressions.
4227 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4228 -- subtypes in a package specification.
4231 -- limited with Pkg;
4233 -- type Acc_Inc is access Pkg.T;
4235 -- N : Natural := X.all.Comp; -- ERROR, limited view
4236 -- end Pkg; -- Comp is not visible
4238 if Nkind (Name) = N_Explicit_Dereference
4239 and then From_Limited_With (Etype (Prefix (Name)))
4240 and then not Is_Potentially_Use_Visible (Etype (Name))
4241 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4242 N_Package_Specification
4245 ("premature usage of incomplete}", Prefix (Name),
4246 Etype (Prefix (Name)));
4249 -- We never need an actual subtype for the case of a selection
4250 -- for a indexed component of a non-packed array, since in
4251 -- this case gigi generates all the checks and can find the
4252 -- necessary bounds information.
4254 -- We also do not need an actual subtype for the case of a
4255 -- first, last, length, or range attribute applied to a
4256 -- non-packed array, since gigi can again get the bounds in
4257 -- these cases (gigi cannot handle the packed case, since it
4258 -- has the bounds of the packed array type, not the original
4259 -- bounds of the type). However, if the prefix is itself a
4260 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4261 -- as a dynamic-sized temporary, so we do generate an actual
4262 -- subtype for this case.
4264 Parent_N := Parent (N);
4266 if not Is_Packed (Etype (Comp))
4268 ((Nkind (Parent_N) = N_Indexed_Component
4269 and then Nkind (Name) /= N_Selected_Component)
4271 (Nkind (Parent_N) = N_Attribute_Reference
4273 Nam_In (Attribute_Name (Parent_N), Name_First,
4278 Set_Etype (N, Etype (Comp));
4280 -- If full analysis is not enabled, we do not generate an
4281 -- actual subtype, because in the absence of expansion
4282 -- reference to a formal of a protected type, for example,
4283 -- will not be properly transformed, and will lead to
4284 -- out-of-scope references in gigi.
4286 -- In all other cases, we currently build an actual subtype.
4287 -- It seems likely that many of these cases can be avoided,
4288 -- but right now, the front end makes direct references to the
4289 -- bounds (e.g. in generating a length check), and if we do
4290 -- not make an actual subtype, we end up getting a direct
4291 -- reference to a discriminant, which will not do.
4293 elsif Full_Analysis then
4295 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4296 Insert_Action (N, Act_Decl);
4298 if No (Act_Decl) then
4299 Set_Etype (N, Etype (Comp));
4302 -- Component type depends on discriminants. Enter the
4303 -- main attributes of the subtype.
4306 Subt : constant Entity_Id :=
4307 Defining_Identifier (Act_Decl);
4310 Set_Etype (Subt, Base_Type (Etype (Comp)));
4311 Set_Ekind (Subt, Ekind (Etype (Comp)));
4312 Set_Etype (N, Subt);
4316 -- If Full_Analysis not enabled, just set the Etype
4319 Set_Etype (N, Etype (Comp));
4322 Check_Implicit_Dereference (N, Etype (N));
4326 -- If the prefix is a private extension, check only the visible
4327 -- components of the partial view. This must include the tag,
4328 -- which can appear in expanded code in a tag check.
4330 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4331 and then Chars (Selector_Name (N)) /= Name_uTag
4333 exit when Comp = Last_Entity (Type_To_Use);
4339 -- Ada 2005 (AI-252): The selected component can be interpreted as
4340 -- a prefixed view of a subprogram. Depending on the context, this is
4341 -- either a name that can appear in a renaming declaration, or part
4342 -- of an enclosing call given in prefix form.
4344 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4345 -- selected component should resolve to a name.
4347 if Ada_Version >= Ada_2005
4348 and then Is_Tagged_Type (Prefix_Type)
4349 and then not Is_Concurrent_Type (Prefix_Type)
4351 if Nkind (Parent (N)) = N_Generic_Association
4352 or else Nkind (Parent (N)) = N_Requeue_Statement
4353 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4355 if Find_Primitive_Operation (N) then
4359 elsif Try_Object_Operation (N) then
4363 -- If the transformation fails, it will be necessary to redo the
4364 -- analysis with all errors enabled, to indicate candidate
4365 -- interpretations and reasons for each failure ???
4369 elsif Is_Private_Type (Prefix_Type) then
4371 -- Allow access only to discriminants of the type. If the type has
4372 -- no full view, gigi uses the parent type for the components, so we
4373 -- do the same here.
4375 if No (Full_View (Prefix_Type)) then
4376 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4377 Comp := First_Entity (Type_To_Use);
4380 while Present (Comp) loop
4381 if Chars (Comp) = Chars (Sel) then
4382 if Ekind (Comp) = E_Discriminant then
4383 Set_Entity_With_Style_Check (Sel, Comp);
4384 Generate_Reference (Comp, Sel);
4386 Set_Etype (Sel, Etype (Comp));
4387 Set_Etype (N, Etype (Comp));
4388 Check_Implicit_Dereference (N, Etype (N));
4390 if Is_Generic_Type (Prefix_Type)
4391 or else Is_Generic_Type (Root_Type (Prefix_Type))
4393 Set_Original_Discriminant (Sel, Comp);
4396 -- Before declaring an error, check whether this is tagged
4397 -- private type and a call to a primitive operation.
4399 elsif Ada_Version >= Ada_2005
4400 and then Is_Tagged_Type (Prefix_Type)
4401 and then Try_Object_Operation (N)
4406 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4407 Error_Msg_NE ("invisible selector& for }", N, Sel);
4408 Set_Entity (Sel, Any_Id);
4409 Set_Etype (N, Any_Type);
4418 elsif Is_Concurrent_Type (Prefix_Type) then
4420 -- Find visible operation with given name. For a protected type,
4421 -- the possible candidates are discriminants, entries or protected
4422 -- procedures. For a task type, the set can only include entries or
4423 -- discriminants if the task type is not an enclosing scope. If it
4424 -- is an enclosing scope (e.g. in an inner task) then all entities
4425 -- are visible, but the prefix must denote the enclosing scope, i.e.
4426 -- can only be a direct name or an expanded name.
4428 Set_Etype (Sel, Any_Type);
4429 In_Scope := In_Open_Scopes (Prefix_Type);
4431 while Present (Comp) loop
4432 if Chars (Comp) = Chars (Sel) then
4433 if Is_Overloadable (Comp) then
4434 Add_One_Interp (Sel, Comp, Etype (Comp));
4436 -- If the prefix is tagged, the correct interpretation may
4437 -- lie in the primitive or class-wide operations of the
4438 -- type. Perform a simple conformance check to determine
4439 -- whether Try_Object_Operation should be invoked even if
4440 -- a visible entity is found.
4442 if Is_Tagged_Type (Prefix_Type)
4444 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4446 N_Indexed_Component)
4447 and then Has_Mode_Conformant_Spec (Comp)
4449 Has_Candidate := True;
4452 -- Note: a selected component may not denote a component of a
4453 -- protected type (4.1.3(7)).
4455 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4457 and then not Is_Protected_Type (Prefix_Type)
4458 and then Is_Entity_Name (Name))
4460 Set_Entity_With_Style_Check (Sel, Comp);
4461 Generate_Reference (Comp, Sel);
4463 -- The selector is not overloadable, so we have a candidate
4466 Has_Candidate := True;
4472 Set_Etype (Sel, Etype (Comp));
4473 Set_Etype (N, Etype (Comp));
4475 if Ekind (Comp) = E_Discriminant then
4476 Set_Original_Discriminant (Sel, Comp);
4479 -- For access type case, introduce explicit dereference for
4480 -- more uniform treatment of entry calls.
4482 if Is_Access_Type (Etype (Name)) then
4483 Insert_Explicit_Dereference (Name);
4485 (Warn_On_Dereference, "?d?implicit dereference", N);
4491 exit when not In_Scope
4493 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4496 -- If there is no visible entity with the given name or none of the
4497 -- visible entities are plausible interpretations, check whether
4498 -- there is some other primitive operation with that name.
4500 if Ada_Version >= Ada_2005
4501 and then Is_Tagged_Type (Prefix_Type)
4503 if (Etype (N) = Any_Type
4504 or else not Has_Candidate)
4505 and then Try_Object_Operation (N)
4509 -- If the context is not syntactically a procedure call, it
4510 -- may be a call to a primitive function declared outside of
4511 -- the synchronized type.
4513 -- If the context is a procedure call, there might still be
4514 -- an overloading between an entry and a primitive procedure
4515 -- declared outside of the synchronized type, called in prefix
4516 -- notation. This is harder to disambiguate because in one case
4517 -- the controlling formal is implicit ???
4519 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4520 and then Nkind (Parent (N)) /= N_Indexed_Component
4521 and then Try_Object_Operation (N)
4526 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4527 -- entry or procedure of a tagged concurrent type we must check
4528 -- if there are class-wide subprograms covering the primitive. If
4529 -- true then Try_Object_Operation reports the error.
4532 and then Is_Concurrent_Type (Prefix_Type)
4533 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4535 -- Duplicate the call. This is required to avoid problems with
4536 -- the tree transformations performed by Try_Object_Operation.
4537 -- Set properly the parent of the copied call, because it is
4538 -- about to be reanalyzed.
4542 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4545 Set_Parent (Par, Parent (Parent (N)));
4547 if Try_Object_Operation
4548 (Sinfo.Name (Par), CW_Test_Only => True)
4556 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4558 -- Case of a prefix of a protected type: selector might denote
4559 -- an invisible private component.
4561 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4562 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4566 if Present (Comp) then
4567 if Is_Single_Concurrent_Object then
4568 Error_Msg_Node_2 := Entity (Name);
4569 Error_Msg_NE ("invisible selector& for &", N, Sel);
4572 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4573 Error_Msg_NE ("invisible selector& for }", N, Sel);
4579 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4584 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4587 -- If N still has no type, the component is not defined in the prefix
4589 if Etype (N) = Any_Type then
4591 if Is_Single_Concurrent_Object then
4592 Error_Msg_Node_2 := Entity (Name);
4593 Error_Msg_NE ("no selector& for&", N, Sel);
4595 Check_Misspelled_Selector (Type_To_Use, Sel);
4597 elsif Is_Generic_Type (Prefix_Type)
4598 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4599 and then Prefix_Type /= Etype (Prefix_Type)
4600 and then Is_Record_Type (Etype (Prefix_Type))
4602 -- If this is a derived formal type, the parent may have
4603 -- different visibility at this point. Try for an inherited
4604 -- component before reporting an error.
4606 Set_Etype (Prefix (N), Etype (Prefix_Type));
4607 Analyze_Selected_Component (N);
4610 -- Similarly, if this is the actual for a formal derived type, the
4611 -- component inherited from the generic parent may not be visible
4612 -- in the actual, but the selected component is legal.
4614 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4615 and then Is_Generic_Actual_Type (Prefix_Type)
4616 and then Present (Full_View (Prefix_Type))
4619 Find_Component_In_Instance
4620 (Generic_Parent_Type (Parent (Prefix_Type)));
4623 -- Finally, the formal and the actual may be private extensions,
4624 -- but the generic is declared in a child unit of the parent, and
4625 -- an additional step is needed to retrieve the proper scope.
4628 and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4630 Find_Component_In_Instance
4631 (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4634 -- Component not found, specialize error message when appropriate
4637 if Ekind (Prefix_Type) = E_Record_Subtype then
4639 -- Check whether this is a component of the base type which
4640 -- is absent from a statically constrained subtype. This will
4641 -- raise constraint error at run time, but is not a compile-
4642 -- time error. When the selector is illegal for base type as
4643 -- well fall through and generate a compilation error anyway.
4645 Comp := First_Component (Base_Type (Prefix_Type));
4646 while Present (Comp) loop
4647 if Chars (Comp) = Chars (Sel)
4648 and then Is_Visible_Component (Comp)
4650 Set_Entity_With_Style_Check (Sel, Comp);
4651 Generate_Reference (Comp, Sel);
4652 Set_Etype (Sel, Etype (Comp));
4653 Set_Etype (N, Etype (Comp));
4655 -- Emit appropriate message. The node will be replaced
4656 -- by an appropriate raise statement.
4658 -- Note that in SPARK mode, as with all calls to apply a
4659 -- compile time constraint error, this will be made into
4660 -- an error to simplify the processing of the formal
4661 -- verification backend.
4663 Apply_Compile_Time_Constraint_Error
4664 (N, "component not present in }??",
4665 CE_Discriminant_Check_Failed,
4666 Ent => Prefix_Type, Rep => False);
4668 Set_Raises_Constraint_Error (N);
4672 Next_Component (Comp);
4677 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4678 Error_Msg_NE ("no selector& for}", N, Sel);
4680 -- Add information in the case of an incomplete prefix
4682 if Is_Incomplete_Type (Type_To_Use) then
4684 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4687 if From_Limited_With (Scope (Type_To_Use)) then
4689 ("\limited view of& has no components", N, Inc);
4693 ("\premature usage of incomplete type&", N, Inc);
4695 if Nkind (Parent (Inc)) =
4696 N_Incomplete_Type_Declaration
4698 -- Record location of premature use in entity so that
4699 -- a continuation message is generated when the
4700 -- completion is seen.
4702 Set_Premature_Use (Parent (Inc), N);
4708 Check_Misspelled_Selector (Type_To_Use, Sel);
4711 Set_Entity (Sel, Any_Id);
4712 Set_Etype (Sel, Any_Type);
4714 end Analyze_Selected_Component;
4716 ---------------------------
4717 -- Analyze_Short_Circuit --
4718 ---------------------------
4720 procedure Analyze_Short_Circuit (N : Node_Id) is
4721 L : constant Node_Id := Left_Opnd (N);
4722 R : constant Node_Id := Right_Opnd (N);
4727 Analyze_Expression (L);
4728 Analyze_Expression (R);
4729 Set_Etype (N, Any_Type);
4731 if not Is_Overloaded (L) then
4732 if Root_Type (Etype (L)) = Standard_Boolean
4733 and then Has_Compatible_Type (R, Etype (L))
4735 Add_One_Interp (N, Etype (L), Etype (L));
4739 Get_First_Interp (L, Ind, It);
4740 while Present (It.Typ) loop
4741 if Root_Type (It.Typ) = Standard_Boolean
4742 and then Has_Compatible_Type (R, It.Typ)
4744 Add_One_Interp (N, It.Typ, It.Typ);
4747 Get_Next_Interp (Ind, It);
4751 -- Here we have failed to find an interpretation. Clearly we know that
4752 -- it is not the case that both operands can have an interpretation of
4753 -- Boolean, but this is by far the most likely intended interpretation.
4754 -- So we simply resolve both operands as Booleans, and at least one of
4755 -- these resolutions will generate an error message, and we do not need
4756 -- to give another error message on the short circuit operation itself.
4758 if Etype (N) = Any_Type then
4759 Resolve (L, Standard_Boolean);
4760 Resolve (R, Standard_Boolean);
4761 Set_Etype (N, Standard_Boolean);
4763 end Analyze_Short_Circuit;
4769 procedure Analyze_Slice (N : Node_Id) is
4770 D : constant Node_Id := Discrete_Range (N);
4771 P : constant Node_Id := Prefix (N);
4772 Array_Type : Entity_Id;
4773 Index_Type : Entity_Id;
4775 procedure Analyze_Overloaded_Slice;
4776 -- If the prefix is overloaded, select those interpretations that
4777 -- yield a one-dimensional array type.
4779 ------------------------------
4780 -- Analyze_Overloaded_Slice --
4781 ------------------------------
4783 procedure Analyze_Overloaded_Slice is
4789 Set_Etype (N, Any_Type);
4791 Get_First_Interp (P, I, It);
4792 while Present (It.Nam) loop
4795 if Is_Access_Type (Typ) then
4796 Typ := Designated_Type (Typ);
4798 (Warn_On_Dereference, "?d?implicit dereference", N);
4801 if Is_Array_Type (Typ)
4802 and then Number_Dimensions (Typ) = 1
4803 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4805 Add_One_Interp (N, Typ, Typ);
4808 Get_Next_Interp (I, It);
4811 if Etype (N) = Any_Type then
4812 Error_Msg_N ("expect array type in prefix of slice", N);
4814 end Analyze_Overloaded_Slice;
4816 -- Start of processing for Analyze_Slice
4819 if Comes_From_Source (N) then
4820 Check_SPARK_Restriction ("slice is not allowed", N);
4826 if Is_Overloaded (P) then
4827 Analyze_Overloaded_Slice;
4830 Array_Type := Etype (P);
4831 Set_Etype (N, Any_Type);
4833 if Is_Access_Type (Array_Type) then
4834 Array_Type := Designated_Type (Array_Type);
4835 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4838 if not Is_Array_Type (Array_Type) then
4839 Wrong_Type (P, Any_Array);
4841 elsif Number_Dimensions (Array_Type) > 1 then
4843 ("type is not one-dimensional array in slice prefix", N);
4846 if Ekind (Array_Type) = E_String_Literal_Subtype then
4847 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
4849 Index_Type := Etype (First_Index (Array_Type));
4852 if not Has_Compatible_Type (D, Index_Type) then
4853 Wrong_Type (D, Index_Type);
4855 Set_Etype (N, Array_Type);
4861 -----------------------------
4862 -- Analyze_Type_Conversion --
4863 -----------------------------
4865 procedure Analyze_Type_Conversion (N : Node_Id) is
4866 Expr : constant Node_Id := Expression (N);
4870 -- If Conversion_OK is set, then the Etype is already set, and the
4871 -- only processing required is to analyze the expression. This is
4872 -- used to construct certain "illegal" conversions which are not
4873 -- allowed by Ada semantics, but can be handled OK by Gigi, see
4874 -- Sinfo for further details.
4876 if Conversion_OK (N) then
4881 -- Otherwise full type analysis is required, as well as some semantic
4882 -- checks to make sure the argument of the conversion is appropriate.
4884 Find_Type (Subtype_Mark (N));
4885 T := Entity (Subtype_Mark (N));
4887 Check_Fully_Declared (T, N);
4888 Analyze_Expression (Expr);
4889 Validate_Remote_Type_Type_Conversion (N);
4891 -- Only remaining step is validity checks on the argument. These
4892 -- are skipped if the conversion does not come from the source.
4894 if not Comes_From_Source (N) then
4897 -- If there was an error in a generic unit, no need to replicate the
4898 -- error message. Conversely, constant-folding in the generic may
4899 -- transform the argument of a conversion into a string literal, which
4900 -- is legal. Therefore the following tests are not performed in an
4903 elsif In_Instance then
4906 elsif Nkind (Expr) = N_Null then
4907 Error_Msg_N ("argument of conversion cannot be null", N);
4908 Error_Msg_N ("\use qualified expression instead", N);
4909 Set_Etype (N, Any_Type);
4911 elsif Nkind (Expr) = N_Aggregate then
4912 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4913 Error_Msg_N ("\use qualified expression instead", N);
4915 elsif Nkind (Expr) = N_Allocator then
4916 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4917 Error_Msg_N ("\use qualified expression instead", N);
4919 elsif Nkind (Expr) = N_String_Literal then
4920 Error_Msg_N ("argument of conversion cannot be string literal", N);
4921 Error_Msg_N ("\use qualified expression instead", N);
4923 elsif Nkind (Expr) = N_Character_Literal then
4924 if Ada_Version = Ada_83 then
4927 Error_Msg_N ("argument of conversion cannot be character literal",
4929 Error_Msg_N ("\use qualified expression instead", N);
4932 elsif Nkind (Expr) = N_Attribute_Reference
4934 Nam_In (Attribute_Name (Expr), Name_Access,
4935 Name_Unchecked_Access,
4936 Name_Unrestricted_Access)
4938 Error_Msg_N ("argument of conversion cannot be access", N);
4939 Error_Msg_N ("\use qualified expression instead", N);
4941 end Analyze_Type_Conversion;
4943 ----------------------
4944 -- Analyze_Unary_Op --
4945 ----------------------
4947 procedure Analyze_Unary_Op (N : Node_Id) is
4948 R : constant Node_Id := Right_Opnd (N);
4949 Op_Id : Entity_Id := Entity (N);
4952 Set_Etype (N, Any_Type);
4953 Candidate_Type := Empty;
4955 Analyze_Expression (R);
4957 if Present (Op_Id) then
4958 if Ekind (Op_Id) = E_Operator then
4959 Find_Unary_Types (R, Op_Id, N);
4961 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4965 Op_Id := Get_Name_Entity_Id (Chars (N));
4966 while Present (Op_Id) loop
4967 if Ekind (Op_Id) = E_Operator then
4968 if No (Next_Entity (First_Entity (Op_Id))) then
4969 Find_Unary_Types (R, Op_Id, N);
4972 elsif Is_Overloadable (Op_Id) then
4973 Analyze_User_Defined_Unary_Op (N, Op_Id);
4976 Op_Id := Homonym (Op_Id);
4981 end Analyze_Unary_Op;
4983 ----------------------------------
4984 -- Analyze_Unchecked_Expression --
4985 ----------------------------------
4987 procedure Analyze_Unchecked_Expression (N : Node_Id) is
4989 Analyze (Expression (N), Suppress => All_Checks);
4990 Set_Etype (N, Etype (Expression (N)));
4991 Save_Interps (Expression (N), N);
4992 end Analyze_Unchecked_Expression;
4994 ---------------------------------------
4995 -- Analyze_Unchecked_Type_Conversion --
4996 ---------------------------------------
4998 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5000 Find_Type (Subtype_Mark (N));
5001 Analyze_Expression (Expression (N));
5002 Set_Etype (N, Entity (Subtype_Mark (N)));
5003 end Analyze_Unchecked_Type_Conversion;
5005 ------------------------------------
5006 -- Analyze_User_Defined_Binary_Op --
5007 ------------------------------------
5009 procedure Analyze_User_Defined_Binary_Op
5014 -- Only do analysis if the operator Comes_From_Source, since otherwise
5015 -- the operator was generated by the expander, and all such operators
5016 -- always refer to the operators in package Standard.
5018 if Comes_From_Source (N) then
5020 F1 : constant Entity_Id := First_Formal (Op_Id);
5021 F2 : constant Entity_Id := Next_Formal (F1);
5024 -- Verify that Op_Id is a visible binary function. Note that since
5025 -- we know Op_Id is overloaded, potentially use visible means use
5026 -- visible for sure (RM 9.4(11)).
5028 if Ekind (Op_Id) = E_Function
5029 and then Present (F2)
5030 and then (Is_Immediately_Visible (Op_Id)
5031 or else Is_Potentially_Use_Visible (Op_Id))
5032 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5033 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5035 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5037 -- If the left operand is overloaded, indicate that the
5038 -- current type is a viable candidate. This is redundant
5039 -- in most cases, but for equality and comparison operators
5040 -- where the context does not impose a type on the operands,
5041 -- setting the proper type is necessary to avoid subsequent
5042 -- ambiguities during resolution, when both user-defined and
5043 -- predefined operators may be candidates.
5045 if Is_Overloaded (Left_Opnd (N)) then
5046 Set_Etype (Left_Opnd (N), Etype (F1));
5049 if Debug_Flag_E then
5050 Write_Str ("user defined operator ");
5051 Write_Name (Chars (Op_Id));
5052 Write_Str (" on node ");
5053 Write_Int (Int (N));
5059 end Analyze_User_Defined_Binary_Op;
5061 -----------------------------------
5062 -- Analyze_User_Defined_Unary_Op --
5063 -----------------------------------
5065 procedure Analyze_User_Defined_Unary_Op
5070 -- Only do analysis if the operator Comes_From_Source, since otherwise
5071 -- the operator was generated by the expander, and all such operators
5072 -- always refer to the operators in package Standard.
5074 if Comes_From_Source (N) then
5076 F : constant Entity_Id := First_Formal (Op_Id);
5079 -- Verify that Op_Id is a visible unary function. Note that since
5080 -- we know Op_Id is overloaded, potentially use visible means use
5081 -- visible for sure (RM 9.4(11)).
5083 if Ekind (Op_Id) = E_Function
5084 and then No (Next_Formal (F))
5085 and then (Is_Immediately_Visible (Op_Id)
5086 or else Is_Potentially_Use_Visible (Op_Id))
5087 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5089 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5093 end Analyze_User_Defined_Unary_Op;
5095 ---------------------------
5096 -- Check_Arithmetic_Pair --
5097 ---------------------------
5099 procedure Check_Arithmetic_Pair
5100 (T1, T2 : Entity_Id;
5104 Op_Name : constant Name_Id := Chars (Op_Id);
5106 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5107 -- Check whether the fixed-point type Typ has a user-defined operator
5108 -- (multiplication or division) that should hide the corresponding
5109 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5110 -- such operators more visible and therefore useful.
5112 -- If the name of the operation is an expanded name with prefix
5113 -- Standard, the predefined universal fixed operator is available,
5114 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5116 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5117 -- Get specific type (i.e. non-universal type if there is one)
5123 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5124 Bas : constant Entity_Id := Base_Type (Typ);
5130 -- If the universal_fixed operation is given explicitly the rule
5131 -- concerning primitive operations of the type do not apply.
5133 if Nkind (N) = N_Function_Call
5134 and then Nkind (Name (N)) = N_Expanded_Name
5135 and then Entity (Prefix (Name (N))) = Standard_Standard
5140 -- The operation is treated as primitive if it is declared in the
5141 -- same scope as the type, and therefore on the same entity chain.
5143 Ent := Next_Entity (Typ);
5144 while Present (Ent) loop
5145 if Chars (Ent) = Chars (Op) then
5146 F1 := First_Formal (Ent);
5147 F2 := Next_Formal (F1);
5149 -- The operation counts as primitive if either operand or
5150 -- result are of the given base type, and both operands are
5151 -- fixed point types.
5153 if (Base_Type (Etype (F1)) = Bas
5154 and then Is_Fixed_Point_Type (Etype (F2)))
5157 (Base_Type (Etype (F2)) = Bas
5158 and then Is_Fixed_Point_Type (Etype (F1)))
5161 (Base_Type (Etype (Ent)) = Bas
5162 and then Is_Fixed_Point_Type (Etype (F1))
5163 and then Is_Fixed_Point_Type (Etype (F2)))
5179 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5181 if T1 = Universal_Integer or else T1 = Universal_Real then
5182 return Base_Type (T2);
5184 return Base_Type (T1);
5188 -- Start of processing for Check_Arithmetic_Pair
5191 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5192 if Is_Numeric_Type (T1)
5193 and then Is_Numeric_Type (T2)
5194 and then (Covers (T1 => T1, T2 => T2)
5196 Covers (T1 => T2, T2 => T1))
5198 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5201 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5202 if Is_Fixed_Point_Type (T1)
5203 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5205 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5206 -- and no further processing is required (this is the case of an
5207 -- operator constructed by Exp_Fixd for a fixed point operation)
5208 -- Otherwise add one interpretation with universal fixed result
5209 -- If the operator is given in functional notation, it comes
5210 -- from source and Fixed_As_Integer cannot apply.
5212 if (Nkind (N) not in N_Op
5213 or else not Treat_Fixed_As_Integer (N))
5215 (not Has_Fixed_Op (T1, Op_Id)
5216 or else Nkind (Parent (N)) = N_Type_Conversion)
5218 Add_One_Interp (N, Op_Id, Universal_Fixed);
5221 elsif Is_Fixed_Point_Type (T2)
5222 and then (Nkind (N) not in N_Op
5223 or else not Treat_Fixed_As_Integer (N))
5224 and then T1 = Universal_Real
5226 (not Has_Fixed_Op (T1, Op_Id)
5227 or else Nkind (Parent (N)) = N_Type_Conversion)
5229 Add_One_Interp (N, Op_Id, Universal_Fixed);
5231 elsif Is_Numeric_Type (T1)
5232 and then Is_Numeric_Type (T2)
5233 and then (Covers (T1 => T1, T2 => T2)
5235 Covers (T1 => T2, T2 => T1))
5237 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5239 elsif Is_Fixed_Point_Type (T1)
5240 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5241 or else T2 = Universal_Integer)
5243 Add_One_Interp (N, Op_Id, T1);
5245 elsif T2 = Universal_Real
5246 and then Base_Type (T1) = Base_Type (Standard_Integer)
5247 and then Op_Name = Name_Op_Multiply
5249 Add_One_Interp (N, Op_Id, Any_Fixed);
5251 elsif T1 = Universal_Real
5252 and then Base_Type (T2) = Base_Type (Standard_Integer)
5254 Add_One_Interp (N, Op_Id, Any_Fixed);
5256 elsif Is_Fixed_Point_Type (T2)
5257 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5258 or else T1 = Universal_Integer)
5259 and then Op_Name = Name_Op_Multiply
5261 Add_One_Interp (N, Op_Id, T2);
5263 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5264 Add_One_Interp (N, Op_Id, T1);
5266 elsif T2 = Universal_Real
5267 and then T1 = Universal_Integer
5268 and then Op_Name = Name_Op_Multiply
5270 Add_One_Interp (N, Op_Id, T2);
5273 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5275 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5276 -- set does not require any special processing, since the Etype is
5277 -- already set (case of operation constructed by Exp_Fixed).
5279 if Is_Integer_Type (T1)
5280 and then (Covers (T1 => T1, T2 => T2)
5282 Covers (T1 => T2, T2 => T1))
5284 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5287 elsif Op_Name = Name_Op_Expon then
5288 if Is_Numeric_Type (T1)
5289 and then not Is_Fixed_Point_Type (T1)
5290 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5291 or else T2 = Universal_Integer)
5293 Add_One_Interp (N, Op_Id, Base_Type (T1));
5296 else pragma Assert (Nkind (N) in N_Op_Shift);
5298 -- If not one of the predefined operators, the node may be one
5299 -- of the intrinsic functions. Its kind is always specific, and
5300 -- we can use it directly, rather than the name of the operation.
5302 if Is_Integer_Type (T1)
5303 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5304 or else T2 = Universal_Integer)
5306 Add_One_Interp (N, Op_Id, Base_Type (T1));
5309 end Check_Arithmetic_Pair;
5311 -------------------------------
5312 -- Check_Misspelled_Selector --
5313 -------------------------------
5315 procedure Check_Misspelled_Selector
5316 (Prefix : Entity_Id;
5319 Max_Suggestions : constant := 2;
5320 Nr_Of_Suggestions : Natural := 0;
5322 Suggestion_1 : Entity_Id := Empty;
5323 Suggestion_2 : Entity_Id := Empty;
5328 -- All the components of the prefix of selector Sel are matched
5329 -- against Sel and a count is maintained of possible misspellings.
5330 -- When at the end of the analysis there are one or two (not more!)
5331 -- possible misspellings, these misspellings will be suggested as
5332 -- possible correction.
5334 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5336 -- Concurrent types should be handled as well ???
5341 Comp := First_Entity (Prefix);
5342 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5343 if Is_Visible_Component (Comp) then
5344 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5345 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5347 case Nr_Of_Suggestions is
5348 when 1 => Suggestion_1 := Comp;
5349 when 2 => Suggestion_2 := Comp;
5350 when others => exit;
5355 Comp := Next_Entity (Comp);
5358 -- Report at most two suggestions
5360 if Nr_Of_Suggestions = 1 then
5361 Error_Msg_NE -- CODEFIX
5362 ("\possible misspelling of&", Sel, Suggestion_1);
5364 elsif Nr_Of_Suggestions = 2 then
5365 Error_Msg_Node_2 := Suggestion_2;
5366 Error_Msg_NE -- CODEFIX
5367 ("\possible misspelling of& or&", Sel, Suggestion_1);
5369 end Check_Misspelled_Selector;
5371 ----------------------
5372 -- Defined_In_Scope --
5373 ----------------------
5375 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5377 S1 : constant Entity_Id := Scope (Base_Type (T));
5380 or else (S1 = System_Aux_Id and then S = Scope (S1));
5381 end Defined_In_Scope;
5387 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5393 Void_Interp_Seen : Boolean := False;
5396 pragma Warnings (Off, Boolean);
5399 if Ada_Version >= Ada_2005 then
5400 Actual := First_Actual (N);
5401 while Present (Actual) loop
5403 -- Ada 2005 (AI-50217): Post an error in case of premature
5404 -- usage of an entity from the limited view.
5406 if not Analyzed (Etype (Actual))
5407 and then From_Limited_With (Etype (Actual))
5409 Error_Msg_Qual_Level := 1;
5411 ("missing with_clause for scope of imported type&",
5412 Actual, Etype (Actual));
5413 Error_Msg_Qual_Level := 0;
5416 Next_Actual (Actual);
5420 -- Analyze each candidate call again, with full error reporting
5424 ("no candidate interpretations match the actuals:!", Nam);
5425 Err_Mode := All_Errors_Mode;
5426 All_Errors_Mode := True;
5428 -- If this is a call to an operation of a concurrent type,
5429 -- the failed interpretations have been removed from the
5430 -- name. Recover them to provide full diagnostics.
5432 if Nkind (Parent (Nam)) = N_Selected_Component then
5433 Set_Entity (Nam, Empty);
5434 New_Nam := New_Copy_Tree (Parent (Nam));
5435 Set_Is_Overloaded (New_Nam, False);
5436 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5437 Set_Parent (New_Nam, Parent (Parent (Nam)));
5438 Analyze_Selected_Component (New_Nam);
5439 Get_First_Interp (Selector_Name (New_Nam), X, It);
5441 Get_First_Interp (Nam, X, It);
5444 while Present (It.Nam) loop
5445 if Etype (It.Nam) = Standard_Void_Type then
5446 Void_Interp_Seen := True;
5449 Analyze_One_Call (N, It.Nam, True, Success);
5450 Get_Next_Interp (X, It);
5453 if Nkind (N) = N_Function_Call then
5454 Get_First_Interp (Nam, X, It);
5455 while Present (It.Nam) loop
5456 if Ekind_In (It.Nam, E_Function, E_Operator) then
5459 Get_Next_Interp (X, It);
5463 -- If all interpretations are procedures, this deserves a
5464 -- more precise message. Ditto if this appears as the prefix
5465 -- of a selected component, which may be a lexical error.
5468 ("\context requires function call, found procedure name", Nam);
5470 if Nkind (Parent (N)) = N_Selected_Component
5471 and then N = Prefix (Parent (N))
5473 Error_Msg_N -- CODEFIX
5474 ("\period should probably be semicolon", Parent (N));
5477 elsif Nkind (N) = N_Procedure_Call_Statement
5478 and then not Void_Interp_Seen
5481 "\function name found in procedure call", Nam);
5484 All_Errors_Mode := Err_Mode;
5487 ---------------------------
5488 -- Find_Arithmetic_Types --
5489 ---------------------------
5491 procedure Find_Arithmetic_Types
5496 Index1 : Interp_Index;
5497 Index2 : Interp_Index;
5501 procedure Check_Right_Argument (T : Entity_Id);
5502 -- Check right operand of operator
5504 --------------------------
5505 -- Check_Right_Argument --
5506 --------------------------
5508 procedure Check_Right_Argument (T : Entity_Id) is
5510 if not Is_Overloaded (R) then
5511 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5513 Get_First_Interp (R, Index2, It2);
5514 while Present (It2.Typ) loop
5515 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5516 Get_Next_Interp (Index2, It2);
5519 end Check_Right_Argument;
5521 -- Start of processing for Find_Arithmetic_Types
5524 if not Is_Overloaded (L) then
5525 Check_Right_Argument (Etype (L));
5528 Get_First_Interp (L, Index1, It1);
5529 while Present (It1.Typ) loop
5530 Check_Right_Argument (It1.Typ);
5531 Get_Next_Interp (Index1, It1);
5535 end Find_Arithmetic_Types;
5537 ------------------------
5538 -- Find_Boolean_Types --
5539 ------------------------
5541 procedure Find_Boolean_Types
5546 Index : Interp_Index;
5549 procedure Check_Numeric_Argument (T : Entity_Id);
5550 -- Special case for logical operations one of whose operands is an
5551 -- integer literal. If both are literal the result is any modular type.
5553 ----------------------------
5554 -- Check_Numeric_Argument --
5555 ----------------------------
5557 procedure Check_Numeric_Argument (T : Entity_Id) is
5559 if T = Universal_Integer then
5560 Add_One_Interp (N, Op_Id, Any_Modular);
5562 elsif Is_Modular_Integer_Type (T) then
5563 Add_One_Interp (N, Op_Id, T);
5565 end Check_Numeric_Argument;
5567 -- Start of processing for Find_Boolean_Types
5570 if not Is_Overloaded (L) then
5571 if Etype (L) = Universal_Integer
5572 or else Etype (L) = Any_Modular
5574 if not Is_Overloaded (R) then
5575 Check_Numeric_Argument (Etype (R));
5578 Get_First_Interp (R, Index, It);
5579 while Present (It.Typ) loop
5580 Check_Numeric_Argument (It.Typ);
5581 Get_Next_Interp (Index, It);
5585 -- If operands are aggregates, we must assume that they may be
5586 -- boolean arrays, and leave disambiguation for the second pass.
5587 -- If only one is an aggregate, verify that the other one has an
5588 -- interpretation as a boolean array
5590 elsif Nkind (L) = N_Aggregate then
5591 if Nkind (R) = N_Aggregate then
5592 Add_One_Interp (N, Op_Id, Etype (L));
5594 elsif not Is_Overloaded (R) then
5595 if Valid_Boolean_Arg (Etype (R)) then
5596 Add_One_Interp (N, Op_Id, Etype (R));
5600 Get_First_Interp (R, Index, It);
5601 while Present (It.Typ) loop
5602 if Valid_Boolean_Arg (It.Typ) then
5603 Add_One_Interp (N, Op_Id, It.Typ);
5606 Get_Next_Interp (Index, It);
5610 elsif Valid_Boolean_Arg (Etype (L))
5611 and then Has_Compatible_Type (R, Etype (L))
5613 Add_One_Interp (N, Op_Id, Etype (L));
5617 Get_First_Interp (L, Index, It);
5618 while Present (It.Typ) loop
5619 if Valid_Boolean_Arg (It.Typ)
5620 and then Has_Compatible_Type (R, It.Typ)
5622 Add_One_Interp (N, Op_Id, It.Typ);
5625 Get_Next_Interp (Index, It);
5628 end Find_Boolean_Types;
5630 ---------------------------
5631 -- Find_Comparison_Types --
5632 ---------------------------
5634 procedure Find_Comparison_Types
5639 Index : Interp_Index;
5641 Found : Boolean := False;
5644 Scop : Entity_Id := Empty;
5646 procedure Try_One_Interp (T1 : Entity_Id);
5647 -- Routine to try one proposed interpretation. Note that the context
5648 -- of the operator plays no role in resolving the arguments, so that
5649 -- if there is more than one interpretation of the operands that is
5650 -- compatible with comparison, the operation is ambiguous.
5652 --------------------
5653 -- Try_One_Interp --
5654 --------------------
5656 procedure Try_One_Interp (T1 : Entity_Id) is
5659 -- If the operator is an expanded name, then the type of the operand
5660 -- must be defined in the corresponding scope. If the type is
5661 -- universal, the context will impose the correct type.
5664 and then not Defined_In_Scope (T1, Scop)
5665 and then T1 /= Universal_Integer
5666 and then T1 /= Universal_Real
5667 and then T1 /= Any_String
5668 and then T1 /= Any_Composite
5673 if Valid_Comparison_Arg (T1)
5674 and then Has_Compatible_Type (R, T1)
5677 and then Base_Type (T1) /= Base_Type (T_F)
5679 It := Disambiguate (L, I_F, Index, Any_Type);
5681 if It = No_Interp then
5682 Ambiguous_Operands (N);
5683 Set_Etype (L, Any_Type);
5697 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5702 -- Start of processing for Find_Comparison_Types
5705 -- If left operand is aggregate, the right operand has to
5706 -- provide a usable type for it.
5708 if Nkind (L) = N_Aggregate
5709 and then Nkind (R) /= N_Aggregate
5711 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5715 if Nkind (N) = N_Function_Call
5716 and then Nkind (Name (N)) = N_Expanded_Name
5718 Scop := Entity (Prefix (Name (N)));
5720 -- The prefix may be a package renaming, and the subsequent test
5721 -- requires the original package.
5723 if Ekind (Scop) = E_Package
5724 and then Present (Renamed_Entity (Scop))
5726 Scop := Renamed_Entity (Scop);
5727 Set_Entity (Prefix (Name (N)), Scop);
5731 if not Is_Overloaded (L) then
5732 Try_One_Interp (Etype (L));
5735 Get_First_Interp (L, Index, It);
5736 while Present (It.Typ) loop
5737 Try_One_Interp (It.Typ);
5738 Get_Next_Interp (Index, It);
5741 end Find_Comparison_Types;
5743 ----------------------------------------
5744 -- Find_Non_Universal_Interpretations --
5745 ----------------------------------------
5747 procedure Find_Non_Universal_Interpretations
5753 Index : Interp_Index;
5757 if T1 = Universal_Integer
5758 or else T1 = Universal_Real
5760 -- If the left operand of an equality operator is null, the visibility
5761 -- of the operator must be determined from the interpretation of the
5762 -- right operand. This processing must be done for Any_Access, which
5763 -- is the internal representation of the type of the literal null.
5765 or else T1 = Any_Access
5767 if not Is_Overloaded (R) then
5769 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5771 Get_First_Interp (R, Index, It);
5772 while Present (It.Typ) loop
5773 if Covers (It.Typ, T1) then
5775 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5778 Get_Next_Interp (Index, It);
5782 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5784 end Find_Non_Universal_Interpretations;
5786 ------------------------------
5787 -- Find_Concatenation_Types --
5788 ------------------------------
5790 procedure Find_Concatenation_Types
5795 Op_Type : constant Entity_Id := Etype (Op_Id);
5798 if Is_Array_Type (Op_Type)
5799 and then not Is_Limited_Type (Op_Type)
5801 and then (Has_Compatible_Type (L, Op_Type)
5803 Has_Compatible_Type (L, Component_Type (Op_Type)))
5805 and then (Has_Compatible_Type (R, Op_Type)
5807 Has_Compatible_Type (R, Component_Type (Op_Type)))
5809 Add_One_Interp (N, Op_Id, Op_Type);
5811 end Find_Concatenation_Types;
5813 -------------------------
5814 -- Find_Equality_Types --
5815 -------------------------
5817 procedure Find_Equality_Types
5822 Index : Interp_Index;
5824 Found : Boolean := False;
5827 Scop : Entity_Id := Empty;
5829 procedure Try_One_Interp (T1 : Entity_Id);
5830 -- The context of the equality operator plays no role in resolving the
5831 -- arguments, so that if there is more than one interpretation of the
5832 -- operands that is compatible with equality, the construct is ambiguous
5833 -- and an error can be emitted now, after trying to disambiguate, i.e.
5834 -- applying preference rules.
5836 --------------------
5837 -- Try_One_Interp --
5838 --------------------
5840 procedure Try_One_Interp (T1 : Entity_Id) is
5841 Bas : constant Entity_Id := Base_Type (T1);
5844 -- If the operator is an expanded name, then the type of the operand
5845 -- must be defined in the corresponding scope. If the type is
5846 -- universal, the context will impose the correct type. An anonymous
5847 -- type for a 'Access reference is also universal in this sense, as
5848 -- the actual type is obtained from context.
5849 -- In Ada 2005, the equality operator for anonymous access types
5850 -- is declared in Standard, and preference rules apply to it.
5852 if Present (Scop) then
5853 if Defined_In_Scope (T1, Scop)
5854 or else T1 = Universal_Integer
5855 or else T1 = Universal_Real
5856 or else T1 = Any_Access
5857 or else T1 = Any_String
5858 or else T1 = Any_Composite
5859 or else (Ekind (T1) = E_Access_Subprogram_Type
5860 and then not Comes_From_Source (T1))
5864 elsif Ekind (T1) = E_Anonymous_Access_Type
5865 and then Scop = Standard_Standard
5870 -- The scope does not contain an operator for the type
5875 -- If we have infix notation, the operator must be usable. Within
5876 -- an instance, if the type is already established we know it is
5877 -- correct. If an operand is universal it is compatible with any
5880 -- In Ada 2005, the equality on anonymous access types is declared
5881 -- in Standard, and is always visible.
5883 elsif In_Open_Scopes (Scope (Bas))
5884 or else Is_Potentially_Use_Visible (Bas)
5885 or else In_Use (Bas)
5886 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
5887 or else (In_Instance
5889 (First_Subtype (T1) = First_Subtype (Etype (R))
5891 (Is_Numeric_Type (T1)
5892 and then Is_Universal_Numeric_Type (Etype (R)))))
5893 or else Ekind (T1) = E_Anonymous_Access_Type
5898 -- Save candidate type for subsequent error message, if any
5900 if not Is_Limited_Type (T1) then
5901 Candidate_Type := T1;
5907 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5908 -- Do not allow anonymous access types in equality operators.
5910 if Ada_Version < Ada_2005
5911 and then Ekind (T1) = E_Anonymous_Access_Type
5916 -- If the right operand has a type compatible with T1, check for an
5917 -- acceptable interpretation, unless T1 is limited (no predefined
5918 -- equality available), or this is use of a "/=" for a tagged type.
5919 -- In the latter case, possible interpretations of equality need to
5920 -- be considered, we don't want the default inequality declared in
5921 -- Standard to be chosen, and the "/=" will be rewritten as a
5922 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
5923 -- that that rewriting happens during analysis rather than being
5924 -- delayed until expansion (this is needed for ASIS, which only sees
5925 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
5926 -- is Name_Op_Eq then we still proceed with the interpretation,
5927 -- because that indicates the potential rewriting case where the
5928 -- interpretation to consider is actually "=" and the node may be
5929 -- about to be rewritten by Analyze_Equality_Op.
5931 if T1 /= Standard_Void_Type
5932 and then Has_Compatible_Type (R, T1)
5935 ((not Is_Limited_Type (T1)
5936 and then not Is_Limited_Composite (T1))
5940 and then not Is_Limited_Type (Component_Type (T1))
5941 and then Available_Full_View_Of_Component (T1)))
5944 (Nkind (N) /= N_Op_Ne
5945 or else not Is_Tagged_Type (T1)
5946 or else Chars (Op_Id) = Name_Op_Eq)
5949 and then Base_Type (T1) /= Base_Type (T_F)
5951 It := Disambiguate (L, I_F, Index, Any_Type);
5953 if It = No_Interp then
5954 Ambiguous_Operands (N);
5955 Set_Etype (L, Any_Type);
5968 if not Analyzed (L) then
5972 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5974 -- Case of operator was not visible, Etype still set to Any_Type
5976 if Etype (N) = Any_Type then
5980 elsif Scop = Standard_Standard
5981 and then Ekind (T1) = E_Anonymous_Access_Type
5987 -- Start of processing for Find_Equality_Types
5990 -- If left operand is aggregate, the right operand has to
5991 -- provide a usable type for it.
5993 if Nkind (L) = N_Aggregate
5994 and then Nkind (R) /= N_Aggregate
5996 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6000 if Nkind (N) = N_Function_Call
6001 and then Nkind (Name (N)) = N_Expanded_Name
6003 Scop := Entity (Prefix (Name (N)));
6005 -- The prefix may be a package renaming, and the subsequent test
6006 -- requires the original package.
6008 if Ekind (Scop) = E_Package
6009 and then Present (Renamed_Entity (Scop))
6011 Scop := Renamed_Entity (Scop);
6012 Set_Entity (Prefix (Name (N)), Scop);
6016 if not Is_Overloaded (L) then
6017 Try_One_Interp (Etype (L));
6020 Get_First_Interp (L, Index, It);
6021 while Present (It.Typ) loop
6022 Try_One_Interp (It.Typ);
6023 Get_Next_Interp (Index, It);
6026 end Find_Equality_Types;
6028 -------------------------
6029 -- Find_Negation_Types --
6030 -------------------------
6032 procedure Find_Negation_Types
6037 Index : Interp_Index;
6041 if not Is_Overloaded (R) then
6042 if Etype (R) = Universal_Integer then
6043 Add_One_Interp (N, Op_Id, Any_Modular);
6044 elsif Valid_Boolean_Arg (Etype (R)) then
6045 Add_One_Interp (N, Op_Id, Etype (R));
6049 Get_First_Interp (R, Index, It);
6050 while Present (It.Typ) loop
6051 if Valid_Boolean_Arg (It.Typ) then
6052 Add_One_Interp (N, Op_Id, It.Typ);
6055 Get_Next_Interp (Index, It);
6058 end Find_Negation_Types;
6060 ------------------------------
6061 -- Find_Primitive_Operation --
6062 ------------------------------
6064 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6065 Obj : constant Node_Id := Prefix (N);
6066 Op : constant Node_Id := Selector_Name (N);
6073 Set_Etype (Op, Any_Type);
6075 if Is_Access_Type (Etype (Obj)) then
6076 Typ := Designated_Type (Etype (Obj));
6081 if Is_Class_Wide_Type (Typ) then
6082 Typ := Root_Type (Typ);
6085 Prims := Primitive_Operations (Typ);
6087 Prim := First_Elmt (Prims);
6088 while Present (Prim) loop
6089 if Chars (Node (Prim)) = Chars (Op) then
6090 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6091 Set_Etype (N, Etype (Node (Prim)));
6097 -- Now look for class-wide operations of the type or any of its
6098 -- ancestors by iterating over the homonyms of the selector.
6101 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6105 Hom := Current_Entity (Op);
6106 while Present (Hom) loop
6107 if (Ekind (Hom) = E_Procedure
6109 Ekind (Hom) = E_Function)
6110 and then Scope (Hom) = Scope (Typ)
6111 and then Present (First_Formal (Hom))
6113 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6115 (Is_Access_Type (Etype (First_Formal (Hom)))
6117 Ekind (Etype (First_Formal (Hom))) =
6118 E_Anonymous_Access_Type
6121 (Designated_Type (Etype (First_Formal (Hom)))) =
6124 Add_One_Interp (Op, Hom, Etype (Hom));
6125 Set_Etype (N, Etype (Hom));
6128 Hom := Homonym (Hom);
6132 return Etype (Op) /= Any_Type;
6133 end Find_Primitive_Operation;
6135 ----------------------
6136 -- Find_Unary_Types --
6137 ----------------------
6139 procedure Find_Unary_Types
6144 Index : Interp_Index;
6148 if not Is_Overloaded (R) then
6149 if Is_Numeric_Type (Etype (R)) then
6151 -- In an instance a generic actual may be a numeric type even if
6152 -- the formal in the generic unit was not. In that case, the
6153 -- predefined operator was not a possible interpretation in the
6154 -- generic, and cannot be one in the instance.
6158 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6162 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6167 Get_First_Interp (R, Index, It);
6168 while Present (It.Typ) loop
6169 if Is_Numeric_Type (It.Typ) then
6173 (Corresponding_Generic_Type (Etype (It.Typ)))
6178 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6182 Get_Next_Interp (Index, It);
6185 end Find_Unary_Types;
6191 function Junk_Operand (N : Node_Id) return Boolean is
6195 if Error_Posted (N) then
6199 -- Get entity to be tested
6201 if Is_Entity_Name (N)
6202 and then Present (Entity (N))
6206 -- An odd case, a procedure name gets converted to a very peculiar
6207 -- function call, and here is where we detect this happening.
6209 elsif Nkind (N) = N_Function_Call
6210 and then Is_Entity_Name (Name (N))
6211 and then Present (Entity (Name (N)))
6215 -- Another odd case, there are at least some cases of selected
6216 -- components where the selected component is not marked as having
6217 -- an entity, even though the selector does have an entity
6219 elsif Nkind (N) = N_Selected_Component
6220 and then Present (Entity (Selector_Name (N)))
6222 Enode := Selector_Name (N);
6228 -- Now test the entity we got to see if it is a bad case
6230 case Ekind (Entity (Enode)) is
6234 ("package name cannot be used as operand", Enode);
6236 when Generic_Unit_Kind =>
6238 ("generic unit name cannot be used as operand", Enode);
6242 ("subtype name cannot be used as operand", Enode);
6246 ("entry name cannot be used as operand", Enode);
6250 ("procedure name cannot be used as operand", Enode);
6254 ("exception name cannot be used as operand", Enode);
6256 when E_Block | E_Label | E_Loop =>
6258 ("label name cannot be used as operand", Enode);
6268 --------------------
6269 -- Operator_Check --
6270 --------------------
6272 procedure Operator_Check (N : Node_Id) is
6274 Remove_Abstract_Operations (N);
6276 -- Test for case of no interpretation found for operator
6278 if Etype (N) = Any_Type then
6282 Op_Id : Entity_Id := Empty;
6285 R := Right_Opnd (N);
6287 if Nkind (N) in N_Binary_Op then
6293 -- If either operand has no type, then don't complain further,
6294 -- since this simply means that we have a propagated error.
6297 or else Etype (R) = Any_Type
6298 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6302 -- We explicitly check for the case of concatenation of component
6303 -- with component to avoid reporting spurious matching array types
6304 -- that might happen to be lurking in distant packages (such as
6305 -- run-time packages). This also prevents inconsistencies in the
6306 -- messages for certain ACVC B tests, which can vary depending on
6307 -- types declared in run-time interfaces. Another improvement when
6308 -- aggregates are present is to look for a well-typed operand.
6310 elsif Present (Candidate_Type)
6311 and then (Nkind (N) /= N_Op_Concat
6312 or else Is_Array_Type (Etype (L))
6313 or else Is_Array_Type (Etype (R)))
6315 if Nkind (N) = N_Op_Concat then
6316 if Etype (L) /= Any_Composite
6317 and then Is_Array_Type (Etype (L))
6319 Candidate_Type := Etype (L);
6321 elsif Etype (R) /= Any_Composite
6322 and then Is_Array_Type (Etype (R))
6324 Candidate_Type := Etype (R);
6328 Error_Msg_NE -- CODEFIX
6329 ("operator for} is not directly visible!",
6330 N, First_Subtype (Candidate_Type));
6333 U : constant Node_Id :=
6334 Cunit (Get_Source_Unit (Candidate_Type));
6336 if Unit_Is_Visible (U) then
6337 Error_Msg_N -- CODEFIX
6338 ("use clause would make operation legal!", N);
6340 Error_Msg_NE -- CODEFIX
6341 ("add with_clause and use_clause for&!",
6342 N, Defining_Entity (Unit (U)));
6347 -- If either operand is a junk operand (e.g. package name), then
6348 -- post appropriate error messages, but do not complain further.
6350 -- Note that the use of OR in this test instead of OR ELSE is
6351 -- quite deliberate, we may as well check both operands in the
6352 -- binary operator case.
6354 elsif Junk_Operand (R)
6355 or -- really mean OR here and not OR ELSE, see above
6356 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6360 -- If we have a logical operator, one of whose operands is
6361 -- Boolean, then we know that the other operand cannot resolve to
6362 -- Boolean (since we got no interpretations), but in that case we
6363 -- pretty much know that the other operand should be Boolean, so
6364 -- resolve it that way (generating an error)
6366 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6367 if Etype (L) = Standard_Boolean then
6368 Resolve (R, Standard_Boolean);
6370 elsif Etype (R) = Standard_Boolean then
6371 Resolve (L, Standard_Boolean);
6375 -- For an arithmetic operator or comparison operator, if one
6376 -- of the operands is numeric, then we know the other operand
6377 -- is not the same numeric type. If it is a non-numeric type,
6378 -- then probably it is intended to match the other operand.
6380 elsif Nkind_In (N, N_Op_Add,
6386 Nkind_In (N, N_Op_Lt,
6392 -- If Allow_Integer_Address is active, check whether the
6393 -- operation becomes legal after converting an operand.
6395 if Is_Numeric_Type (Etype (L))
6396 and then not Is_Numeric_Type (Etype (R))
6398 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6400 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6401 Analyze_Arithmetic_Op (N);
6404 Resolve (R, Etype (L));
6408 elsif Is_Numeric_Type (Etype (R))
6409 and then not Is_Numeric_Type (Etype (L))
6411 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
6413 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
6414 Analyze_Arithmetic_Op (N);
6418 Resolve (L, Etype (R));
6423 elsif Allow_Integer_Address
6424 and then Is_Descendent_Of_Address (Etype (L))
6425 and then Is_Descendent_Of_Address (Etype (R))
6426 and then not Error_Posted (N)
6429 Addr_Type : constant Entity_Id := Etype (L);
6433 Unchecked_Convert_To (
6434 Standard_Integer, Relocate_Node (L)));
6436 Unchecked_Convert_To (
6437 Standard_Integer, Relocate_Node (R)));
6438 Analyze_Arithmetic_Op (N);
6440 -- If this is an operand in an enclosing arithmetic
6441 -- operation, Convert the result as an address so that
6442 -- arithmetic folding of address can continue.
6444 if Nkind (Parent (N)) in N_Op then
6446 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
6453 -- Comparisons on A'Access are common enough to deserve a
6456 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6457 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6458 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6461 ("two access attributes cannot be compared directly", N);
6463 ("\use qualified expression for one of the operands",
6467 -- Another one for C programmers
6469 elsif Nkind (N) = N_Op_Concat
6470 and then Valid_Boolean_Arg (Etype (L))
6471 and then Valid_Boolean_Arg (Etype (R))
6473 Error_Msg_N ("invalid operands for concatenation", N);
6474 Error_Msg_N -- CODEFIX
6475 ("\maybe AND was meant", N);
6478 -- A special case for comparison of access parameter with null
6480 elsif Nkind (N) = N_Op_Eq
6481 and then Is_Entity_Name (L)
6482 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6483 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6485 and then Nkind (R) = N_Null
6487 Error_Msg_N ("access parameter is not allowed to be null", L);
6488 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6491 -- Another special case for exponentiation, where the right
6492 -- operand must be Natural, independently of the base.
6494 elsif Nkind (N) = N_Op_Expon
6495 and then Is_Numeric_Type (Etype (L))
6496 and then not Is_Overloaded (R)
6498 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6499 and then Base_Type (Etype (R)) /= Universal_Integer
6501 if Ada_Version >= Ada_2012
6502 and then Has_Dimension_System (Etype (L))
6505 ("exponent for dimensioned type must be a rational" &
6506 ", found}", R, Etype (R));
6509 ("exponent must be of type Natural, found}", R, Etype (R));
6515 -- If we fall through then just give general message. Note that in
6516 -- the following messages, if the operand is overloaded we choose
6517 -- an arbitrary type to complain about, but that is probably more
6518 -- useful than not giving a type at all.
6520 if Nkind (N) in N_Unary_Op then
6521 Error_Msg_Node_2 := Etype (R);
6522 Error_Msg_N ("operator& not defined for}", N);
6526 if Nkind (N) in N_Binary_Op then
6527 if not Is_Overloaded (L)
6528 and then not Is_Overloaded (R)
6529 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6531 Error_Msg_Node_2 := First_Subtype (Etype (R));
6532 Error_Msg_N ("there is no applicable operator& for}", N);
6535 -- Another attempt to find a fix: one of the candidate
6536 -- interpretations may not be use-visible. This has
6537 -- already been checked for predefined operators, so
6538 -- we examine only user-defined functions.
6540 Op_Id := Get_Name_Entity_Id (Chars (N));
6542 while Present (Op_Id) loop
6543 if Ekind (Op_Id) /= E_Operator
6544 and then Is_Overloadable (Op_Id)
6546 if not Is_Immediately_Visible (Op_Id)
6547 and then not In_Use (Scope (Op_Id))
6548 and then not Is_Abstract_Subprogram (Op_Id)
6549 and then not Is_Hidden (Op_Id)
6550 and then Ekind (Scope (Op_Id)) = E_Package
6553 (L, Etype (First_Formal (Op_Id)))
6555 (Next_Formal (First_Formal (Op_Id)))
6559 Etype (Next_Formal (First_Formal (Op_Id))))
6562 ("No legal interpretation for operator&", N);
6564 ("\use clause on& would make operation legal",
6570 Op_Id := Homonym (Op_Id);
6574 Error_Msg_N ("invalid operand types for operator&", N);
6576 if Nkind (N) /= N_Op_Concat then
6577 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6578 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6588 -----------------------------------------
6589 -- Process_Implicit_Dereference_Prefix --
6590 -----------------------------------------
6592 function Process_Implicit_Dereference_Prefix
6594 P : Entity_Id) return Entity_Id
6597 Typ : constant Entity_Id := Designated_Type (Etype (P));
6601 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6603 -- We create a dummy reference to E to ensure that the reference
6604 -- is not considered as part of an assignment (an implicit
6605 -- dereference can never assign to its prefix). The Comes_From_Source
6606 -- attribute needs to be propagated for accurate warnings.
6608 Ref := New_Reference_To (E, Sloc (P));
6609 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6610 Generate_Reference (E, Ref);
6613 -- An implicit dereference is a legal occurrence of an
6614 -- incomplete type imported through a limited_with clause,
6615 -- if the full view is visible.
6617 if From_Limited_With (Typ)
6618 and then not From_Limited_With (Scope (Typ))
6620 (Is_Immediately_Visible (Scope (Typ))
6622 (Is_Child_Unit (Scope (Typ))
6623 and then Is_Visible_Lib_Unit (Scope (Typ))))
6625 return Available_View (Typ);
6629 end Process_Implicit_Dereference_Prefix;
6631 --------------------------------
6632 -- Remove_Abstract_Operations --
6633 --------------------------------
6635 procedure Remove_Abstract_Operations (N : Node_Id) is
6636 Abstract_Op : Entity_Id := Empty;
6637 Address_Kludge : Boolean := False;
6641 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6642 -- activate this if either extensions are enabled, or if the abstract
6643 -- operation in question comes from a predefined file. This latter test
6644 -- allows us to use abstract to make operations invisible to users. In
6645 -- particular, if type Address is non-private and abstract subprograms
6646 -- are used to hide its operators, they will be truly hidden.
6648 type Operand_Position is (First_Op, Second_Op);
6649 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6651 procedure Remove_Address_Interpretations (Op : Operand_Position);
6652 -- Ambiguities may arise when the operands are literal and the address
6653 -- operations in s-auxdec are visible. In that case, remove the
6654 -- interpretation of a literal as Address, to retain the semantics of
6655 -- Address as a private type.
6657 ------------------------------------
6658 -- Remove_Address_Interpretations --
6659 ------------------------------------
6661 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6665 if Is_Overloaded (N) then
6666 Get_First_Interp (N, I, It);
6667 while Present (It.Nam) loop
6668 Formal := First_Entity (It.Nam);
6670 if Op = Second_Op then
6671 Formal := Next_Entity (Formal);
6674 if Is_Descendent_Of_Address (Etype (Formal)) then
6675 Address_Kludge := True;
6679 Get_Next_Interp (I, It);
6682 end Remove_Address_Interpretations;
6684 -- Start of processing for Remove_Abstract_Operations
6687 if Is_Overloaded (N) then
6688 if Debug_Flag_V then
6689 Write_Str ("Remove_Abstract_Operations: ");
6690 Write_Overloads (N);
6693 Get_First_Interp (N, I, It);
6695 while Present (It.Nam) loop
6696 if Is_Overloadable (It.Nam)
6697 and then Is_Abstract_Subprogram (It.Nam)
6698 and then not Is_Dispatching_Operation (It.Nam)
6700 Abstract_Op := It.Nam;
6702 if Is_Descendent_Of_Address (It.Typ) then
6703 Address_Kludge := True;
6707 -- In Ada 2005, this operation does not participate in overload
6708 -- resolution. If the operation is defined in a predefined
6709 -- unit, it is one of the operations declared abstract in some
6710 -- variants of System, and it must be removed as well.
6712 elsif Ada_Version >= Ada_2005
6713 or else Is_Predefined_File_Name
6714 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6721 Get_Next_Interp (I, It);
6724 if No (Abstract_Op) then
6726 -- If some interpretation yields an integer type, it is still
6727 -- possible that there are address interpretations. Remove them
6728 -- if one operand is a literal, to avoid spurious ambiguities
6729 -- on systems where Address is a visible integer type.
6731 if Is_Overloaded (N)
6732 and then Nkind (N) in N_Op
6733 and then Is_Integer_Type (Etype (N))
6735 if Nkind (N) in N_Binary_Op then
6736 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6737 Remove_Address_Interpretations (Second_Op);
6739 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6740 Remove_Address_Interpretations (First_Op);
6745 elsif Nkind (N) in N_Op then
6747 -- Remove interpretations that treat literals as addresses. This
6748 -- is never appropriate, even when Address is defined as a visible
6749 -- Integer type. The reason is that we would really prefer Address
6750 -- to behave as a private type, even in this case, which is there
6751 -- only to accommodate oddities of VMS address sizes. If Address
6752 -- is a visible integer type, we get lots of overload ambiguities.
6754 if Nkind (N) in N_Binary_Op then
6756 U1 : constant Boolean :=
6757 Present (Universal_Interpretation (Right_Opnd (N)));
6758 U2 : constant Boolean :=
6759 Present (Universal_Interpretation (Left_Opnd (N)));
6763 Remove_Address_Interpretations (Second_Op);
6767 Remove_Address_Interpretations (First_Op);
6770 if not (U1 and U2) then
6772 -- Remove corresponding predefined operator, which is
6773 -- always added to the overload set.
6775 Get_First_Interp (N, I, It);
6776 while Present (It.Nam) loop
6777 if Scope (It.Nam) = Standard_Standard
6778 and then Base_Type (It.Typ) =
6779 Base_Type (Etype (Abstract_Op))
6784 Get_Next_Interp (I, It);
6787 elsif Is_Overloaded (N)
6788 and then Present (Univ_Type)
6790 -- If both operands have a universal interpretation,
6791 -- it is still necessary to remove interpretations that
6792 -- yield Address. Any remaining ambiguities will be
6793 -- removed in Disambiguate.
6795 Get_First_Interp (N, I, It);
6796 while Present (It.Nam) loop
6797 if Is_Descendent_Of_Address (It.Typ) then
6800 elsif not Is_Type (It.Nam) then
6801 Set_Entity (N, It.Nam);
6804 Get_Next_Interp (I, It);
6810 elsif Nkind (N) = N_Function_Call
6812 (Nkind (Name (N)) = N_Operator_Symbol
6814 (Nkind (Name (N)) = N_Expanded_Name
6816 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6820 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6821 U1 : constant Boolean :=
6822 Present (Universal_Interpretation (Arg1));
6823 U2 : constant Boolean :=
6824 Present (Next (Arg1)) and then
6825 Present (Universal_Interpretation (Next (Arg1)));
6829 Remove_Address_Interpretations (First_Op);
6833 Remove_Address_Interpretations (Second_Op);
6836 if not (U1 and U2) then
6837 Get_First_Interp (N, I, It);
6838 while Present (It.Nam) loop
6839 if Scope (It.Nam) = Standard_Standard
6840 and then It.Typ = Base_Type (Etype (Abstract_Op))
6845 Get_Next_Interp (I, It);
6851 -- If the removal has left no valid interpretations, emit an error
6852 -- message now and label node as illegal.
6854 if Present (Abstract_Op) then
6855 Get_First_Interp (N, I, It);
6859 -- Removal of abstract operation left no viable candidate
6861 Set_Etype (N, Any_Type);
6862 Error_Msg_Sloc := Sloc (Abstract_Op);
6864 ("cannot call abstract operation& declared#", N, Abstract_Op);
6866 -- In Ada 2005, an abstract operation may disable predefined
6867 -- operators. Since the context is not yet known, we mark the
6868 -- predefined operators as potentially hidden. Do not include
6869 -- predefined operators when addresses are involved since this
6870 -- case is handled separately.
6872 elsif Ada_Version >= Ada_2005
6873 and then not Address_Kludge
6875 while Present (It.Nam) loop
6876 if Is_Numeric_Type (It.Typ)
6877 and then Scope (It.Typ) = Standard_Standard
6879 Set_Abstract_Op (I, Abstract_Op);
6882 Get_Next_Interp (I, It);
6887 if Debug_Flag_V then
6888 Write_Str ("Remove_Abstract_Operations done: ");
6889 Write_Overloads (N);
6892 end Remove_Abstract_Operations;
6894 ----------------------------
6895 -- Try_Container_Indexing --
6896 ----------------------------
6898 function Try_Container_Indexing
6901 Exprs : List_Id) return Boolean
6903 Loc : constant Source_Ptr := Sloc (N);
6907 Func_Name : Node_Id;
6912 -- Check whether type has a specified indexing aspect
6916 if Is_Variable (Prefix) then
6918 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
6921 if No (Func_Name) then
6923 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
6926 -- If aspect does not exist the expression is illegal. Error is
6927 -- diagnosed in caller.
6929 if No (Func_Name) then
6931 -- The prefix itself may be an indexing of a container: rewrite
6932 -- as such and re-analyze.
6934 if Has_Implicit_Dereference (Etype (Prefix)) then
6935 Build_Explicit_Dereference
6936 (Prefix, First_Discriminant (Etype (Prefix)));
6937 return Try_Container_Indexing (N, Prefix, Exprs);
6944 Assoc := New_List (Relocate_Node (Prefix));
6946 -- A generalized iterator may have nore than one index expression, so
6947 -- transfer all of them to the argument list to be used in the call.
6952 Arg := First (Exprs);
6953 while Present (Arg) loop
6954 Append (Relocate_Node (Arg), Assoc);
6959 if not Is_Overloaded (Func_Name) then
6960 Func := Entity (Func_Name);
6962 Make_Function_Call (Loc,
6963 Name => New_Occurrence_Of (Func, Loc),
6964 Parameter_Associations => Assoc);
6965 Rewrite (N, Indexing);
6968 -- If the return type of the indexing function is a reference type,
6969 -- add the dereference as a possible interpretation. Note that the
6970 -- indexing aspect may be a function that returns the element type
6971 -- with no intervening implicit dereference.
6973 if Has_Discriminants (Etype (Func)) then
6974 Disc := First_Discriminant (Etype (Func));
6975 while Present (Disc) loop
6976 if Has_Implicit_Dereference (Disc) then
6977 Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
6981 Next_Discriminant (Disc);
6986 Indexing := Make_Function_Call (Loc,
6987 Name => Make_Identifier (Loc, Chars (Func_Name)),
6988 Parameter_Associations => Assoc);
6990 Rewrite (N, Indexing);
6998 Get_First_Interp (Func_Name, I, It);
6999 Set_Etype (N, Any_Type);
7000 while Present (It.Nam) loop
7001 Analyze_One_Call (N, It.Nam, False, Success);
7003 Set_Etype (Name (N), It.Typ);
7004 Set_Entity (Name (N), It.Nam);
7006 -- Add implicit dereference interpretation
7008 if Has_Discriminants (Etype (It.Nam)) then
7009 Disc := First_Discriminant (Etype (It.Nam));
7010 while Present (Disc) loop
7011 if Has_Implicit_Dereference (Disc) then
7013 (N, Disc, Designated_Type (Etype (Disc)));
7017 Next_Discriminant (Disc);
7023 Get_Next_Interp (I, It);
7028 if Etype (N) = Any_Type then
7030 ("container cannot be indexed with&", N, Etype (First (Exprs)));
7031 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
7037 end Try_Container_Indexing;
7039 -----------------------
7040 -- Try_Indirect_Call --
7041 -----------------------
7043 function Try_Indirect_Call
7046 Typ : Entity_Id) return Boolean
7052 pragma Warnings (Off, Call_OK);
7055 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
7057 Actual := First_Actual (N);
7058 Formal := First_Formal (Designated_Type (Typ));
7059 while Present (Actual) and then Present (Formal) loop
7060 if not Has_Compatible_Type (Actual, Etype (Formal)) then
7065 Next_Formal (Formal);
7068 if No (Actual) and then No (Formal) then
7069 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
7071 -- Nam is a candidate interpretation for the name in the call,
7072 -- if it is not an indirect call.
7074 if not Is_Type (Nam)
7075 and then Is_Entity_Name (Name (N))
7077 Set_Entity (Name (N), Nam);
7084 end Try_Indirect_Call;
7086 ----------------------
7087 -- Try_Indexed_Call --
7088 ----------------------
7090 function Try_Indexed_Call
7094 Skip_First : Boolean) return Boolean
7096 Loc : constant Source_Ptr := Sloc (N);
7097 Actuals : constant List_Id := Parameter_Associations (N);
7102 Actual := First (Actuals);
7104 -- If the call was originally written in prefix form, skip the first
7105 -- actual, which is obviously not defaulted.
7111 Index := First_Index (Typ);
7112 while Present (Actual) and then Present (Index) loop
7114 -- If the parameter list has a named association, the expression
7115 -- is definitely a call and not an indexed component.
7117 if Nkind (Actual) = N_Parameter_Association then
7121 if Is_Entity_Name (Actual)
7122 and then Is_Type (Entity (Actual))
7123 and then No (Next (Actual))
7125 -- A single actual that is a type name indicates a slice if the
7126 -- type is discrete, and an error otherwise.
7128 if Is_Discrete_Type (Entity (Actual)) then
7132 Make_Function_Call (Loc,
7133 Name => Relocate_Node (Name (N))),
7135 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
7140 Error_Msg_N ("invalid use of type in expression", Actual);
7141 Set_Etype (N, Any_Type);
7146 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
7154 if No (Actual) and then No (Index) then
7155 Add_One_Interp (N, Nam, Component_Type (Typ));
7157 -- Nam is a candidate interpretation for the name in the call,
7158 -- if it is not an indirect call.
7160 if not Is_Type (Nam)
7161 and then Is_Entity_Name (Name (N))
7163 Set_Entity (Name (N), Nam);
7170 end Try_Indexed_Call;
7172 --------------------------
7173 -- Try_Object_Operation --
7174 --------------------------
7176 function Try_Object_Operation
7177 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
7179 K : constant Node_Kind := Nkind (Parent (N));
7180 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
7181 Loc : constant Source_Ptr := Sloc (N);
7182 Obj : constant Node_Id := Prefix (N);
7184 Subprog : constant Node_Id :=
7185 Make_Identifier (Sloc (Selector_Name (N)),
7186 Chars => Chars (Selector_Name (N)));
7187 -- Identifier on which possible interpretations will be collected
7189 Report_Error : Boolean := False;
7190 -- If no candidate interpretation matches the context, redo the
7191 -- analysis with error enabled to provide additional information.
7194 Candidate : Entity_Id := Empty;
7195 New_Call_Node : Node_Id := Empty;
7196 Node_To_Replace : Node_Id;
7197 Obj_Type : Entity_Id := Etype (Obj);
7198 Success : Boolean := False;
7200 function Valid_Candidate
7203 Subp : Entity_Id) return Entity_Id;
7204 -- If the subprogram is a valid interpretation, record it, and add
7205 -- to the list of interpretations of Subprog. Otherwise return Empty.
7207 procedure Complete_Object_Operation
7208 (Call_Node : Node_Id;
7209 Node_To_Replace : Node_Id);
7210 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
7211 -- Call_Node, insert the object (or its dereference) as the first actual
7212 -- in the call, and complete the analysis of the call.
7214 procedure Report_Ambiguity (Op : Entity_Id);
7215 -- If a prefixed procedure call is ambiguous, indicate whether the
7216 -- call includes an implicit dereference or an implicit 'Access.
7218 procedure Transform_Object_Operation
7219 (Call_Node : out Node_Id;
7220 Node_To_Replace : out Node_Id);
7221 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
7222 -- Call_Node is the resulting subprogram call, Node_To_Replace is
7223 -- either N or the parent of N, and Subprog is a reference to the
7224 -- subprogram we are trying to match.
7226 function Try_Class_Wide_Operation
7227 (Call_Node : Node_Id;
7228 Node_To_Replace : Node_Id) return Boolean;
7229 -- Traverse all ancestor types looking for a class-wide subprogram
7230 -- for which the current operation is a valid non-dispatching call.
7232 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
7233 -- If prefix is overloaded, its interpretation may include different
7234 -- tagged types, and we must examine the primitive operations and
7235 -- the class-wide operations of each in order to find candidate
7236 -- interpretations for the call as a whole.
7238 function Try_Primitive_Operation
7239 (Call_Node : Node_Id;
7240 Node_To_Replace : Node_Id) return Boolean;
7241 -- Traverse the list of primitive subprograms looking for a dispatching
7242 -- operation for which the current node is a valid call .
7244 ---------------------
7245 -- Valid_Candidate --
7246 ---------------------
7248 function Valid_Candidate
7251 Subp : Entity_Id) return Entity_Id
7253 Arr_Type : Entity_Id;
7254 Comp_Type : Entity_Id;
7257 -- If the subprogram is a valid interpretation, record it in global
7258 -- variable Subprog, to collect all possible overloadings.
7261 if Subp /= Entity (Subprog) then
7262 Add_One_Interp (Subprog, Subp, Etype (Subp));
7266 -- If the call may be an indexed call, retrieve component type of
7267 -- resulting expression, and add possible interpretation.
7272 if Nkind (Call) = N_Function_Call
7273 and then Nkind (Parent (N)) = N_Indexed_Component
7274 and then Needs_One_Actual (Subp)
7276 if Is_Array_Type (Etype (Subp)) then
7277 Arr_Type := Etype (Subp);
7279 elsif Is_Access_Type (Etype (Subp))
7280 and then Is_Array_Type (Designated_Type (Etype (Subp)))
7282 Arr_Type := Designated_Type (Etype (Subp));
7286 if Present (Arr_Type) then
7288 -- Verify that the actuals (excluding the object) match the types
7296 Actual := Next (First_Actual (Call));
7297 Index := First_Index (Arr_Type);
7298 while Present (Actual) and then Present (Index) loop
7299 if not Has_Compatible_Type (Actual, Etype (Index)) then
7304 Next_Actual (Actual);
7310 and then Present (Arr_Type)
7312 Comp_Type := Component_Type (Arr_Type);
7316 if Present (Comp_Type)
7317 and then Etype (Subprog) /= Comp_Type
7319 Add_One_Interp (Subprog, Subp, Comp_Type);
7323 if Etype (Call) /= Any_Type then
7328 end Valid_Candidate;
7330 -------------------------------
7331 -- Complete_Object_Operation --
7332 -------------------------------
7334 procedure Complete_Object_Operation
7335 (Call_Node : Node_Id;
7336 Node_To_Replace : Node_Id)
7338 Control : constant Entity_Id := First_Formal (Entity (Subprog));
7339 Formal_Type : constant Entity_Id := Etype (Control);
7340 First_Actual : Node_Id;
7343 -- Place the name of the operation, with its interpretations,
7344 -- on the rewritten call.
7346 Set_Name (Call_Node, Subprog);
7348 First_Actual := First (Parameter_Associations (Call_Node));
7350 -- For cross-reference purposes, treat the new node as being in
7351 -- the source if the original one is. Set entity and type, even
7352 -- though they may be overwritten during resolution if overloaded.
7354 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
7355 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
7357 if Nkind (N) = N_Selected_Component
7358 and then not Inside_A_Generic
7360 Set_Entity (Selector_Name (N), Entity (Subprog));
7361 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
7364 -- If need be, rewrite first actual as an explicit dereference
7365 -- If the call is overloaded, the rewriting can only be done
7366 -- once the primitive operation is identified.
7368 if Is_Overloaded (Subprog) then
7370 -- The prefix itself may be overloaded, and its interpretations
7371 -- must be propagated to the new actual in the call.
7373 if Is_Overloaded (Obj) then
7374 Save_Interps (Obj, First_Actual);
7377 Rewrite (First_Actual, Obj);
7379 elsif not Is_Access_Type (Formal_Type)
7380 and then Is_Access_Type (Etype (Obj))
7382 Rewrite (First_Actual,
7383 Make_Explicit_Dereference (Sloc (Obj), Obj));
7384 Analyze (First_Actual);
7386 -- If we need to introduce an explicit dereference, verify that
7387 -- the resulting actual is compatible with the mode of the formal.
7389 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
7390 and then Is_Access_Constant (Etype (Obj))
7393 ("expect variable in call to&", Prefix (N), Entity (Subprog));
7396 -- Conversely, if the formal is an access parameter and the object
7397 -- is not, replace the actual with a 'Access reference. Its analysis
7398 -- will check that the object is aliased.
7400 elsif Is_Access_Type (Formal_Type)
7401 and then not Is_Access_Type (Etype (Obj))
7403 -- A special case: A.all'access is illegal if A is an access to a
7404 -- constant and the context requires an access to a variable.
7406 if not Is_Access_Constant (Formal_Type) then
7407 if (Nkind (Obj) = N_Explicit_Dereference
7408 and then Is_Access_Constant (Etype (Prefix (Obj))))
7409 or else not Is_Variable (Obj)
7412 ("actual for& must be a variable", Obj, Control);
7416 Rewrite (First_Actual,
7417 Make_Attribute_Reference (Loc,
7418 Attribute_Name => Name_Access,
7419 Prefix => Relocate_Node (Obj)));
7421 if not Is_Aliased_View (Obj) then
7423 ("object in prefixed call to& must be aliased"
7424 & " (RM-2005 4.3.1 (13))",
7425 Prefix (First_Actual), Subprog);
7428 Analyze (First_Actual);
7431 if Is_Overloaded (Obj) then
7432 Save_Interps (Obj, First_Actual);
7435 Rewrite (First_Actual, Obj);
7438 Rewrite (Node_To_Replace, Call_Node);
7440 -- Propagate the interpretations collected in subprog to the new
7441 -- function call node, to be resolved from context.
7443 if Is_Overloaded (Subprog) then
7444 Save_Interps (Subprog, Node_To_Replace);
7447 Analyze (Node_To_Replace);
7449 -- If the operation has been rewritten into a call, which may get
7450 -- subsequently an explicit dereference, preserve the type on the
7451 -- original node (selected component or indexed component) for
7452 -- subsequent legality tests, e.g. Is_Variable. which examines
7453 -- the original node.
7455 if Nkind (Node_To_Replace) = N_Function_Call then
7457 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
7460 end Complete_Object_Operation;
7462 ----------------------
7463 -- Report_Ambiguity --
7464 ----------------------
7466 procedure Report_Ambiguity (Op : Entity_Id) is
7467 Access_Actual : constant Boolean :=
7468 Is_Access_Type (Etype (Prefix (N)));
7469 Access_Formal : Boolean := False;
7472 Error_Msg_Sloc := Sloc (Op);
7474 if Present (First_Formal (Op)) then
7475 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7478 if Access_Formal and then not Access_Actual then
7479 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7481 ("\possible interpretation"
7482 & " (inherited, with implicit 'Access) #", N);
7485 ("\possible interpretation (with implicit 'Access) #", N);
7488 elsif not Access_Formal and then Access_Actual then
7489 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7491 ("\possible interpretation"
7492 & " ( inherited, with implicit dereference) #", N);
7495 ("\possible interpretation (with implicit dereference) #", N);
7499 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7500 Error_Msg_N ("\possible interpretation (inherited)#", N);
7502 Error_Msg_N -- CODEFIX
7503 ("\possible interpretation#", N);
7506 end Report_Ambiguity;
7508 --------------------------------
7509 -- Transform_Object_Operation --
7510 --------------------------------
7512 procedure Transform_Object_Operation
7513 (Call_Node : out Node_Id;
7514 Node_To_Replace : out Node_Id)
7516 Dummy : constant Node_Id := New_Copy (Obj);
7517 -- Placeholder used as a first parameter in the call, replaced
7518 -- eventually by the proper object.
7520 Parent_Node : constant Node_Id := Parent (N);
7526 -- Common case covering 1) Call to a procedure and 2) Call to a
7527 -- function that has some additional actuals.
7529 if Nkind (Parent_Node) in N_Subprogram_Call
7531 -- N is a selected component node containing the name of the
7532 -- subprogram. If N is not the name of the parent node we must
7533 -- not replace the parent node by the new construct. This case
7534 -- occurs when N is a parameterless call to a subprogram that
7535 -- is an actual parameter of a call to another subprogram. For
7537 -- Some_Subprogram (..., Obj.Operation, ...)
7539 and then Name (Parent_Node) = N
7541 Node_To_Replace := Parent_Node;
7543 Actuals := Parameter_Associations (Parent_Node);
7545 if Present (Actuals) then
7546 Prepend (Dummy, Actuals);
7548 Actuals := New_List (Dummy);
7551 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7553 Make_Procedure_Call_Statement (Loc,
7554 Name => New_Copy (Subprog),
7555 Parameter_Associations => Actuals);
7559 Make_Function_Call (Loc,
7560 Name => New_Copy (Subprog),
7561 Parameter_Associations => Actuals);
7565 -- Before analysis, a function call appears as an indexed component
7566 -- if there are no named associations.
7568 elsif Nkind (Parent_Node) = N_Indexed_Component
7569 and then N = Prefix (Parent_Node)
7571 Node_To_Replace := Parent_Node;
7572 Actuals := Expressions (Parent_Node);
7574 Actual := First (Actuals);
7575 while Present (Actual) loop
7580 Prepend (Dummy, Actuals);
7583 Make_Function_Call (Loc,
7584 Name => New_Copy (Subprog),
7585 Parameter_Associations => Actuals);
7587 -- Parameterless call: Obj.F is rewritten as F (Obj)
7590 Node_To_Replace := N;
7593 Make_Function_Call (Loc,
7594 Name => New_Copy (Subprog),
7595 Parameter_Associations => New_List (Dummy));
7597 end Transform_Object_Operation;
7599 ------------------------------
7600 -- Try_Class_Wide_Operation --
7601 ------------------------------
7603 function Try_Class_Wide_Operation
7604 (Call_Node : Node_Id;
7605 Node_To_Replace : Node_Id) return Boolean
7607 Anc_Type : Entity_Id;
7608 Matching_Op : Entity_Id := Empty;
7611 procedure Traverse_Homonyms
7612 (Anc_Type : Entity_Id;
7613 Error : out Boolean);
7614 -- Traverse the homonym chain of the subprogram searching for those
7615 -- homonyms whose first formal has the Anc_Type's class-wide type,
7616 -- or an anonymous access type designating the class-wide type. If
7617 -- an ambiguity is detected, then Error is set to True.
7619 procedure Traverse_Interfaces
7620 (Anc_Type : Entity_Id;
7621 Error : out Boolean);
7622 -- Traverse the list of interfaces, if any, associated with Anc_Type
7623 -- and search for acceptable class-wide homonyms associated with each
7624 -- interface. If an ambiguity is detected, then Error is set to True.
7626 -----------------------
7627 -- Traverse_Homonyms --
7628 -----------------------
7630 procedure Traverse_Homonyms
7631 (Anc_Type : Entity_Id;
7632 Error : out Boolean)
7634 Cls_Type : Entity_Id;
7642 Cls_Type := Class_Wide_Type (Anc_Type);
7644 Hom := Current_Entity (Subprog);
7646 -- Find a non-hidden operation whose first parameter is of the
7647 -- class-wide type, a subtype thereof, or an anonymous access
7648 -- to same. If in an instance, the operation can be considered
7649 -- even if hidden (it may be hidden because the instantiation is
7650 -- expanded after the containing package has been analyzed).
7652 while Present (Hom) loop
7653 if Ekind_In (Hom, E_Procedure, E_Function)
7654 and then (not Is_Hidden (Hom) or else In_Instance)
7655 and then Scope (Hom) = Scope (Anc_Type)
7656 and then Present (First_Formal (Hom))
7658 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7660 (Is_Access_Type (Etype (First_Formal (Hom)))
7662 Ekind (Etype (First_Formal (Hom))) =
7663 E_Anonymous_Access_Type
7666 (Designated_Type (Etype (First_Formal (Hom)))) =
7669 -- If the context is a procedure call, ignore functions
7670 -- in the name of the call.
7672 if Ekind (Hom) = E_Function
7673 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7674 and then N = Name (Parent (N))
7678 -- If the context is a function call, ignore procedures
7679 -- in the name of the call.
7681 elsif Ekind (Hom) = E_Procedure
7682 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7687 Set_Etype (Call_Node, Any_Type);
7688 Set_Is_Overloaded (Call_Node, False);
7691 if No (Matching_Op) then
7692 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
7693 Set_Etype (Call_Node, Any_Type);
7694 Set_Parent (Call_Node, Parent (Node_To_Replace));
7696 Set_Name (Call_Node, Hom_Ref);
7701 Report => Report_Error,
7703 Skip_First => True);
7706 Valid_Candidate (Success, Call_Node, Hom);
7712 Report => Report_Error,
7714 Skip_First => True);
7716 if Present (Valid_Candidate (Success, Call_Node, Hom))
7717 and then Nkind (Call_Node) /= N_Function_Call
7719 Error_Msg_NE ("ambiguous call to&", N, Hom);
7720 Report_Ambiguity (Matching_Op);
7721 Report_Ambiguity (Hom);
7729 Hom := Homonym (Hom);
7731 end Traverse_Homonyms;
7733 -------------------------
7734 -- Traverse_Interfaces --
7735 -------------------------
7737 procedure Traverse_Interfaces
7738 (Anc_Type : Entity_Id;
7739 Error : out Boolean)
7741 Intface_List : constant List_Id :=
7742 Abstract_Interface_List (Anc_Type);
7748 if Is_Non_Empty_List (Intface_List) then
7749 Intface := First (Intface_List);
7750 while Present (Intface) loop
7752 -- Look for acceptable class-wide homonyms associated with
7755 Traverse_Homonyms (Etype (Intface), Error);
7761 -- Continue the search by looking at each of the interface's
7762 -- associated interface ancestors.
7764 Traverse_Interfaces (Etype (Intface), Error);
7773 end Traverse_Interfaces;
7775 -- Start of processing for Try_Class_Wide_Operation
7778 -- If we are searching only for conflicting class-wide subprograms
7779 -- then initialize directly Matching_Op with the target entity.
7781 if CW_Test_Only then
7782 Matching_Op := Entity (Selector_Name (N));
7785 -- Loop through ancestor types (including interfaces), traversing
7786 -- the homonym chain of the subprogram, trying out those homonyms
7787 -- whose first formal has the class-wide type of the ancestor, or
7788 -- an anonymous access type designating the class-wide type.
7790 Anc_Type := Obj_Type;
7792 -- Look for a match among homonyms associated with the ancestor
7794 Traverse_Homonyms (Anc_Type, Error);
7800 -- Continue the search for matches among homonyms associated with
7801 -- any interfaces implemented by the ancestor.
7803 Traverse_Interfaces (Anc_Type, Error);
7809 exit when Etype (Anc_Type) = Anc_Type;
7810 Anc_Type := Etype (Anc_Type);
7813 if Present (Matching_Op) then
7814 Set_Etype (Call_Node, Etype (Matching_Op));
7817 return Present (Matching_Op);
7818 end Try_Class_Wide_Operation;
7820 -----------------------------------
7821 -- Try_One_Prefix_Interpretation --
7822 -----------------------------------
7824 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7828 if Is_Access_Type (Obj_Type) then
7829 Obj_Type := Designated_Type (Obj_Type);
7832 if Ekind (Obj_Type) = E_Private_Subtype then
7833 Obj_Type := Base_Type (Obj_Type);
7836 if Is_Class_Wide_Type (Obj_Type) then
7837 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7840 -- The type may have be obtained through a limited_with clause,
7841 -- in which case the primitive operations are available on its
7842 -- non-limited view. If still incomplete, retrieve full view.
7844 if Ekind (Obj_Type) = E_Incomplete_Type
7845 and then From_Limited_With (Obj_Type)
7847 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7850 -- If the object is not tagged, or the type is still an incomplete
7851 -- type, this is not a prefixed call.
7853 if not Is_Tagged_Type (Obj_Type)
7854 or else Is_Incomplete_Type (Obj_Type)
7860 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7861 CW_Result : Boolean;
7862 Prim_Result : Boolean;
7863 pragma Unreferenced (CW_Result);
7866 if not CW_Test_Only then
7868 Try_Primitive_Operation
7869 (Call_Node => New_Call_Node,
7870 Node_To_Replace => Node_To_Replace);
7873 -- Check if there is a class-wide subprogram covering the
7874 -- primitive. This check must be done even if a candidate
7875 -- was found in order to report ambiguous calls.
7877 if not (Prim_Result) then
7879 Try_Class_Wide_Operation
7880 (Call_Node => New_Call_Node,
7881 Node_To_Replace => Node_To_Replace);
7883 -- If we found a primitive we search for class-wide subprograms
7884 -- using a duplicate of the call node (done to avoid missing its
7885 -- decoration if there is no ambiguity).
7889 Try_Class_Wide_Operation
7890 (Call_Node => Dup_Call_Node,
7891 Node_To_Replace => Node_To_Replace);
7894 end Try_One_Prefix_Interpretation;
7896 -----------------------------
7897 -- Try_Primitive_Operation --
7898 -----------------------------
7900 function Try_Primitive_Operation
7901 (Call_Node : Node_Id;
7902 Node_To_Replace : Node_Id) return Boolean
7905 Prim_Op : Entity_Id;
7906 Matching_Op : Entity_Id := Empty;
7907 Prim_Op_Ref : Node_Id := Empty;
7909 Corr_Type : Entity_Id := Empty;
7910 -- If the prefix is a synchronized type, the controlling type of
7911 -- the primitive operation is the corresponding record type, else
7912 -- this is the object type itself.
7914 Success : Boolean := False;
7916 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7917 -- For tagged types the candidate interpretations are found in
7918 -- the list of primitive operations of the type and its ancestors.
7919 -- For formal tagged types we have to find the operations declared
7920 -- in the same scope as the type (including in the generic formal
7921 -- part) because the type itself carries no primitive operations,
7922 -- except for formal derived types that inherit the operations of
7923 -- the parent and progenitors.
7924 -- If the context is a generic subprogram body, the generic formals
7925 -- are visible by name, but are not in the entity list of the
7926 -- subprogram because that list starts with the subprogram formals.
7927 -- We retrieve the candidate operations from the generic declaration.
7929 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7930 -- An operation that overrides an inherited operation in the private
7931 -- part of its package may be hidden, but if the inherited operation
7932 -- is visible a direct call to it will dispatch to the private one,
7933 -- which is therefore a valid candidate.
7935 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7936 -- Verify that the prefix, dereferenced if need be, is a valid
7937 -- controlling argument in a call to Op. The remaining actuals
7938 -- are checked in the subsequent call to Analyze_One_Call.
7940 ------------------------------
7941 -- Collect_Generic_Type_Ops --
7942 ------------------------------
7944 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7945 Bas : constant Entity_Id := Base_Type (T);
7946 Candidates : constant Elist_Id := New_Elmt_List;
7950 procedure Check_Candidate;
7951 -- The operation is a candidate if its first parameter is a
7952 -- controlling operand of the desired type.
7954 -----------------------
7955 -- Check_Candidate; --
7956 -----------------------
7958 procedure Check_Candidate is
7960 Formal := First_Formal (Subp);
7963 and then Is_Controlling_Formal (Formal)
7965 (Base_Type (Etype (Formal)) = Bas
7967 (Is_Access_Type (Etype (Formal))
7968 and then Designated_Type (Etype (Formal)) = Bas))
7970 Append_Elmt (Subp, Candidates);
7972 end Check_Candidate;
7974 -- Start of processing for Collect_Generic_Type_Ops
7977 if Is_Derived_Type (T) then
7978 return Primitive_Operations (T);
7980 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7982 -- Scan the list of generic formals to find subprograms
7983 -- that may have a first controlling formal of the type.
7985 if Nkind (Unit_Declaration_Node (Scope (T)))
7986 = N_Generic_Subprogram_Declaration
7993 First (Generic_Formal_Declarations
7994 (Unit_Declaration_Node (Scope (T))));
7995 while Present (Decl) loop
7996 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7997 Subp := Defining_Entity (Decl);
8008 -- Scan the list of entities declared in the same scope as
8009 -- the type. In general this will be an open scope, given that
8010 -- the call we are analyzing can only appear within a generic
8011 -- declaration or body (either the one that declares T, or a
8014 -- For a subtype representing a generic actual type, go to the
8017 if Is_Generic_Actual_Type (T) then
8018 Subp := First_Entity (Scope (Base_Type (T)));
8020 Subp := First_Entity (Scope (T));
8023 while Present (Subp) loop
8024 if Is_Overloadable (Subp) then
8033 end Collect_Generic_Type_Ops;
8035 ---------------------------
8036 -- Is_Private_Overriding --
8037 ---------------------------
8039 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
8040 Visible_Op : constant Entity_Id := Homonym (Op);
8043 return Present (Visible_Op)
8044 and then Scope (Op) = Scope (Visible_Op)
8045 and then not Comes_From_Source (Visible_Op)
8046 and then Alias (Visible_Op) = Op
8047 and then not Is_Hidden (Visible_Op);
8048 end Is_Private_Overriding;
8050 -----------------------------
8051 -- Valid_First_Argument_Of --
8052 -----------------------------
8054 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
8055 Typ : Entity_Id := Etype (First_Formal (Op));
8058 if Is_Concurrent_Type (Typ)
8059 and then Present (Corresponding_Record_Type (Typ))
8061 Typ := Corresponding_Record_Type (Typ);
8064 -- Simple case. Object may be a subtype of the tagged type or
8065 -- may be the corresponding record of a synchronized type.
8067 return Obj_Type = Typ
8068 or else Base_Type (Obj_Type) = Typ
8069 or else Corr_Type = Typ
8071 -- Prefix can be dereferenced
8074 (Is_Access_Type (Corr_Type)
8075 and then Designated_Type (Corr_Type) = Typ)
8077 -- Formal is an access parameter, for which the object
8078 -- can provide an access.
8081 (Ekind (Typ) = E_Anonymous_Access_Type
8083 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
8084 end Valid_First_Argument_Of;
8086 -- Start of processing for Try_Primitive_Operation
8089 -- Look for subprograms in the list of primitive operations. The name
8090 -- must be identical, and the kind of call indicates the expected
8091 -- kind of operation (function or procedure). If the type is a
8092 -- (tagged) synchronized type, the primitive ops are attached to the
8093 -- corresponding record (base) type.
8095 if Is_Concurrent_Type (Obj_Type) then
8096 if Present (Corresponding_Record_Type (Obj_Type)) then
8097 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
8098 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
8100 Corr_Type := Obj_Type;
8101 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8104 elsif not Is_Generic_Type (Obj_Type) then
8105 Corr_Type := Obj_Type;
8106 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
8109 Corr_Type := Obj_Type;
8110 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8113 while Present (Elmt) loop
8114 Prim_Op := Node (Elmt);
8116 if Chars (Prim_Op) = Chars (Subprog)
8117 and then Present (First_Formal (Prim_Op))
8118 and then Valid_First_Argument_Of (Prim_Op)
8120 (Nkind (Call_Node) = N_Function_Call)
8121 = (Ekind (Prim_Op) = E_Function)
8123 -- Ada 2005 (AI-251): If this primitive operation corresponds
8124 -- with an immediate ancestor interface there is no need to add
8125 -- it to the list of interpretations; the corresponding aliased
8126 -- primitive is also in this list of primitive operations and
8127 -- will be used instead.
8129 if (Present (Interface_Alias (Prim_Op))
8130 and then Is_Ancestor (Find_Dispatching_Type
8131 (Alias (Prim_Op)), Corr_Type))
8133 -- Do not consider hidden primitives unless the type is in an
8134 -- open scope or we are within an instance, where visibility
8135 -- is known to be correct, or else if this is an overriding
8136 -- operation in the private part for an inherited operation.
8138 or else (Is_Hidden (Prim_Op)
8139 and then not Is_Immediately_Visible (Obj_Type)
8140 and then not In_Instance
8141 and then not Is_Private_Overriding (Prim_Op))
8146 Set_Etype (Call_Node, Any_Type);
8147 Set_Is_Overloaded (Call_Node, False);
8149 if No (Matching_Op) then
8150 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
8151 Candidate := Prim_Op;
8153 Set_Parent (Call_Node, Parent (Node_To_Replace));
8155 Set_Name (Call_Node, Prim_Op_Ref);
8161 Report => Report_Error,
8163 Skip_First => True);
8165 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
8167 -- More than one interpretation, collect for subsequent
8168 -- disambiguation. If this is a procedure call and there
8169 -- is another match, report ambiguity now.
8175 Report => Report_Error,
8177 Skip_First => True);
8179 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
8180 and then Nkind (Call_Node) /= N_Function_Call
8182 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
8183 Report_Ambiguity (Matching_Op);
8184 Report_Ambiguity (Prim_Op);
8194 if Present (Matching_Op) then
8195 Set_Etype (Call_Node, Etype (Matching_Op));
8198 return Present (Matching_Op);
8199 end Try_Primitive_Operation;
8201 -- Start of processing for Try_Object_Operation
8204 Analyze_Expression (Obj);
8206 -- Analyze the actuals if node is known to be a subprogram call
8208 if Is_Subprg_Call and then N = Name (Parent (N)) then
8209 Actual := First (Parameter_Associations (Parent (N)));
8210 while Present (Actual) loop
8211 Analyze_Expression (Actual);
8216 -- Build a subprogram call node, using a copy of Obj as its first
8217 -- actual. This is a placeholder, to be replaced by an explicit
8218 -- dereference when needed.
8220 Transform_Object_Operation
8221 (Call_Node => New_Call_Node,
8222 Node_To_Replace => Node_To_Replace);
8224 Set_Etype (New_Call_Node, Any_Type);
8225 Set_Etype (Subprog, Any_Type);
8226 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
8228 if not Is_Overloaded (Obj) then
8229 Try_One_Prefix_Interpretation (Obj_Type);
8236 Get_First_Interp (Obj, I, It);
8237 while Present (It.Nam) loop
8238 Try_One_Prefix_Interpretation (It.Typ);
8239 Get_Next_Interp (I, It);
8244 if Etype (New_Call_Node) /= Any_Type then
8246 -- No need to complete the tree transformations if we are only
8247 -- searching for conflicting class-wide subprograms
8249 if CW_Test_Only then
8252 Complete_Object_Operation
8253 (Call_Node => New_Call_Node,
8254 Node_To_Replace => Node_To_Replace);
8258 elsif Present (Candidate) then
8260 -- The argument list is not type correct. Re-analyze with error
8261 -- reporting enabled, and use one of the possible candidates.
8262 -- In All_Errors_Mode, re-analyze all failed interpretations.
8264 if All_Errors_Mode then
8265 Report_Error := True;
8266 if Try_Primitive_Operation
8267 (Call_Node => New_Call_Node,
8268 Node_To_Replace => Node_To_Replace)
8271 Try_Class_Wide_Operation
8272 (Call_Node => New_Call_Node,
8273 Node_To_Replace => Node_To_Replace)
8280 (N => New_Call_Node,
8284 Skip_First => True);
8287 -- No need for further errors
8292 -- There was no candidate operation, so report it as an error
8293 -- in the caller: Analyze_Selected_Component.
8297 end Try_Object_Operation;
8303 procedure wpo (T : Entity_Id) is
8308 if not Is_Tagged_Type (T) then
8312 E := First_Elmt (Primitive_Operations (Base_Type (T)));
8313 while Present (E) loop
8315 Write_Int (Int (Op));
8316 Write_Str (" === ");
8317 Write_Name (Chars (Op));
8319 Write_Name (Chars (Scope (Op)));