1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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 -- Tables which speed up the identification of dangerous calls to Ada 2012
69 -- functions with writable actuals (AI05-0144).
71 -- The following table enumerates the Ada constructs which may evaluate in
72 -- arbitrary order. It does not cover all the language constructs which can
73 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
75 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
77 N_Assignment_Statement => True,
78 N_Entry_Call_Statement => True,
79 N_Extension_Aggregate => True,
80 N_Full_Type_Declaration => True,
81 N_Indexed_Component => True,
82 N_Object_Declaration => True,
86 N_Array_Type_Definition => True,
87 N_Membership_Test => True,
89 N_Subprogram_Call => True,
92 -- The following table enumerates the nodes on which we stop climbing when
93 -- locating the outermost Ada construct that can be evaluated in arbitrary
96 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
98 N_Assignment_Statement => True,
99 N_Entry_Call_Statement => True,
100 N_Extended_Return_Statement => True,
101 N_Extension_Aggregate => True,
102 N_Full_Type_Declaration => True,
103 N_Object_Declaration => True,
104 N_Object_Renaming_Declaration => True,
105 N_Package_Specification => True,
107 N_Procedure_Call_Statement => True,
108 N_Simple_Return_Statement => True,
109 N_Has_Condition => True,
112 -----------------------
113 -- Local Subprograms --
114 -----------------------
116 procedure Analyze_Concatenation_Rest (N : Node_Id);
117 -- Does the "rest" of the work of Analyze_Concatenation, after the left
118 -- operand has been analyzed. See Analyze_Concatenation for details.
120 procedure Analyze_Expression (N : Node_Id);
121 -- For expressions that are not names, this is just a call to analyze. If
122 -- the expression is a name, it may be a call to a parameterless function,
123 -- and if so must be converted into an explicit call node and analyzed as
124 -- such. This deproceduring must be done during the first pass of overload
125 -- resolution, because otherwise a procedure call with overloaded actuals
126 -- may fail to resolve.
128 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
129 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
130 -- operator name or an expanded name whose selector is an operator name,
131 -- and one possible interpretation is as a predefined operator.
133 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
134 -- If the prefix of a selected_component is overloaded, the proper
135 -- interpretation that yields a record type with the proper selector
136 -- name must be selected.
138 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
139 -- Procedure to analyze a user defined binary operator, which is resolved
140 -- like a function, but instead of a list of actuals it is presented
141 -- with the left and right operands of an operator node.
143 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
144 -- Procedure to analyze a user defined unary operator, which is resolved
145 -- like a function, but instead of a list of actuals, it is presented with
146 -- the operand of the operator node.
148 procedure Ambiguous_Operands (N : Node_Id);
149 -- For equality, membership, and comparison operators with overloaded
150 -- arguments, list possible interpretations.
152 procedure Analyze_One_Call
156 Success : out Boolean;
157 Skip_First : Boolean := False);
158 -- Check one interpretation of an overloaded subprogram name for
159 -- compatibility with the types of the actuals in a call. If there is a
160 -- single interpretation which does not match, post error if Report is
163 -- Nam is the entity that provides the formals against which the actuals
164 -- are checked. Nam is either the name of a subprogram, or the internal
165 -- subprogram type constructed for an access_to_subprogram. If the actuals
166 -- are compatible with Nam, then Nam is added to the list of candidate
167 -- interpretations for N, and Success is set to True.
169 -- The flag Skip_First is used when analyzing a call that was rewritten
170 -- from object notation. In this case the first actual may have to receive
171 -- an explicit dereference, depending on the first formal of the operation
172 -- being called. The caller will have verified that the object is legal
173 -- for the call. If the remaining parameters match, the first parameter
174 -- will rewritten as a dereference if needed, prior to completing analysis.
176 procedure Check_Misspelled_Selector
179 -- Give possible misspelling message if Sel seems likely to be a mis-
180 -- spelling of one of the selectors of the Prefix. This is called by
181 -- Analyze_Selected_Component after producing an invalid selector error
184 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
185 -- Verify that type T is declared in scope S. Used to find interpretations
186 -- for operators given by expanded names. This is abstracted as a separate
187 -- function to handle extensions to System, where S is System, but T is
188 -- declared in the extension.
190 procedure Find_Arithmetic_Types
194 -- L and R are the operands of an arithmetic operator. Find consistent
195 -- pairs of interpretations for L and R that have a numeric type consistent
196 -- with the semantics of the operator.
198 procedure Find_Comparison_Types
202 -- L and R are operands of a comparison operator. Find consistent pairs of
203 -- interpretations for L and R.
205 procedure Find_Concatenation_Types
209 -- For the four varieties of concatenation
211 procedure Find_Equality_Types
215 -- Ditto for equality operators
217 procedure Find_Boolean_Types
221 -- Ditto for binary logical operations
223 procedure Find_Negation_Types
227 -- Find consistent interpretation for operand of negation operator
229 procedure Find_Non_Universal_Interpretations
234 -- For equality and comparison operators, the result is always boolean, and
235 -- the legality of the operation is determined from the visibility of the
236 -- operand types. If one of the operands has a universal interpretation,
237 -- the legality check uses some compatible non-universal interpretation of
238 -- the other operand. N can be an operator node, or a function call whose
239 -- name is an operator designator. Any_Access, which is the initial type of
240 -- the literal NULL, is a universal type for the purpose of this routine.
242 function Find_Primitive_Operation (N : Node_Id) return Boolean;
243 -- Find candidate interpretations for the name Obj.Proc when it appears in
244 -- a subprogram renaming declaration.
246 procedure Find_Unary_Types
250 -- Unary arithmetic types: plus, minus, abs
252 procedure Check_Arithmetic_Pair
256 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
257 -- for left and right operand. Determine whether they constitute a valid
258 -- pair for the given operator, and record the corresponding interpretation
259 -- of the operator node. The node N may be an operator node (the usual
260 -- case) or a function call whose prefix is an operator designator. In
261 -- both cases Op_Id is the operator name itself.
263 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
264 -- Give detailed information on overloaded call where none of the
265 -- interpretations match. N is the call node, Nam the designator for
266 -- the overloaded entity being called.
268 function Junk_Operand (N : Node_Id) return Boolean;
269 -- Test for an operand that is an inappropriate entity (e.g. a package
270 -- name or a label). If so, issue an error message and return True. If
271 -- the operand is not an inappropriate entity kind, return False.
273 procedure Operator_Check (N : Node_Id);
274 -- Verify that an operator has received some valid interpretation. If none
275 -- was found, determine whether a use clause would make the operation
276 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
277 -- every type compatible with the operator, even if the operator for the
278 -- type is not directly visible. The routine uses this type to emit a more
279 -- informative message.
281 function Process_Implicit_Dereference_Prefix
283 P : Node_Id) return Entity_Id;
284 -- Called when P is the prefix of an implicit dereference, denoting an
285 -- object E. The function returns the designated type of the prefix, taking
286 -- into account that the designated type of an anonymous access type may be
287 -- a limited view, when the non-limited view is visible.
289 -- If in semantics only mode (-gnatc or generic), the function also records
290 -- that the prefix is a reference to E, if any. Normally, such a reference
291 -- is generated only when the implicit dereference is expanded into an
292 -- explicit one, but for consistency we must generate the reference when
293 -- expansion is disabled as well.
295 procedure Remove_Abstract_Operations (N : Node_Id);
296 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
297 -- operation is not a candidate interpretation.
299 function Try_Container_Indexing
302 Exprs : List_Id) return Boolean;
303 -- AI05-0139: Generalized indexing to support iterators over containers
305 function Try_Indexed_Call
309 Skip_First : Boolean) return Boolean;
310 -- If a function has defaults for all its actuals, a call to it may in fact
311 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
312 -- interpretation as an indexing, prior to analysis as a call. If both are
313 -- possible, the node is overloaded with both interpretations (same symbol
314 -- but two different types). If the call is written in prefix form, the
315 -- prefix becomes the first parameter in the call, and only the remaining
316 -- actuals must be checked for the presence of defaults.
318 function Try_Indirect_Call
321 Typ : Entity_Id) return Boolean;
322 -- Similarly, a function F that needs no actuals can return an access to a
323 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
324 -- the call may be overloaded with both interpretations.
326 procedure wpo (T : Entity_Id);
327 pragma Warnings (Off, wpo);
328 -- Used for debugging: obtain list of primitive operations even if
329 -- type is not frozen and dispatch table is not built yet.
331 ------------------------
332 -- Ambiguous_Operands --
333 ------------------------
335 procedure Ambiguous_Operands (N : Node_Id) is
336 procedure List_Operand_Interps (Opnd : Node_Id);
338 --------------------------
339 -- List_Operand_Interps --
340 --------------------------
342 procedure List_Operand_Interps (Opnd : Node_Id) is
347 if Is_Overloaded (Opnd) then
348 if Nkind (Opnd) in N_Op then
351 elsif Nkind (Opnd) = N_Function_Call then
354 elsif Ada_Version >= Ada_2012 then
360 Get_First_Interp (Opnd, I, It);
361 while Present (It.Nam) loop
362 if Has_Implicit_Dereference (It.Typ) then
364 ("can be interpreted as implicit dereference", Opnd);
368 Get_Next_Interp (I, It);
379 if Opnd = Left_Opnd (N) then
381 ("\left operand has the following interpretations", N);
384 ("\right operand has the following interpretations", N);
388 List_Interps (Nam, Err);
389 end List_Operand_Interps;
391 -- Start of processing for Ambiguous_Operands
394 if Nkind (N) in N_Membership_Test then
395 Error_Msg_N ("ambiguous operands for membership", N);
397 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
398 Error_Msg_N ("ambiguous operands for equality", N);
401 Error_Msg_N ("ambiguous operands for comparison", N);
404 if All_Errors_Mode then
405 List_Operand_Interps (Left_Opnd (N));
406 List_Operand_Interps (Right_Opnd (N));
408 Error_Msg_N ("\use -gnatf switch for details", N);
410 end Ambiguous_Operands;
412 -----------------------
413 -- Analyze_Aggregate --
414 -----------------------
416 -- Most of the analysis of Aggregates requires that the type be known,
417 -- and is therefore put off until resolution.
419 procedure Analyze_Aggregate (N : Node_Id) is
421 if No (Etype (N)) then
422 Set_Etype (N, Any_Composite);
424 end Analyze_Aggregate;
426 -----------------------
427 -- Analyze_Allocator --
428 -----------------------
430 procedure Analyze_Allocator (N : Node_Id) is
431 Loc : constant Source_Ptr := Sloc (N);
432 Sav_Errs : constant Nat := Serious_Errors_Detected;
433 E : Node_Id := Expression (N);
434 Acc_Type : Entity_Id;
441 Check_SPARK_05_Restriction ("allocator is not allowed", N);
443 -- Deal with allocator restrictions
445 -- In accordance with H.4(7), the No_Allocators restriction only applies
446 -- to user-written allocators. The same consideration applies to the
447 -- No_Standard_Allocators_Before_Elaboration restriction.
449 if Comes_From_Source (N) then
450 Check_Restriction (No_Allocators, N);
452 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
453 -- look at enclosing context, checking task/main subprogram case.
457 while Present (P) loop
459 -- For the task case we need a handled sequence of statements,
460 -- where the occurrence of the allocator is within the statements
461 -- and the parent is a task body
463 if Nkind (P) = N_Handled_Sequence_Of_Statements
464 and then Is_List_Member (C)
465 and then List_Containing (C) = Statements (P)
467 Onode := Original_Node (Parent (P));
469 -- Check for allocator within task body, this is a definite
470 -- violation of No_Allocators_After_Elaboration we can detect
473 if Nkind (Onode) = N_Task_Body then
475 (No_Standard_Allocators_After_Elaboration, N);
480 -- The other case is appearance in a subprogram body. This is
481 -- a violation if this is a library level subprogram with no
482 -- parameters. Note that this is now a static error even if the
483 -- subprogram is not the main program (this is a change, in an
484 -- earlier version only the main program was affected, and the
485 -- check had to be done in the binder.
487 if Nkind (P) = N_Subprogram_Body
488 and then Nkind (Parent (P)) = N_Compilation_Unit
489 and then No (Parameter_Specifications (Specification (P)))
492 (No_Standard_Allocators_After_Elaboration, N);
500 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
501 -- any. The expected type for the name is any type. A non-overloading
502 -- rule then requires it to be of a type descended from
503 -- System.Storage_Pools.Subpools.Subpool_Handle.
505 -- This isn't exactly what the AI says, but it seems to be the right
506 -- rule. The AI should be fixed.???
509 Subpool : constant Node_Id := Subpool_Handle_Name (N);
512 if Present (Subpool) then
515 if Is_Overloaded (Subpool) then
516 Error_Msg_N ("ambiguous subpool handle", Subpool);
519 -- Check that Etype (Subpool) is descended from Subpool_Handle
525 -- Analyze the qualified expression or subtype indication
527 if Nkind (E) = N_Qualified_Expression then
528 Acc_Type := Create_Itype (E_Allocator_Type, N);
529 Set_Etype (Acc_Type, Acc_Type);
530 Find_Type (Subtype_Mark (E));
532 -- Analyze the qualified expression, and apply the name resolution
533 -- rule given in 4.7(3).
536 Type_Id := Etype (E);
537 Set_Directly_Designated_Type (Acc_Type, Type_Id);
539 -- A qualified expression requires an exact match of the type,
540 -- class-wide matching is not allowed.
542 -- if Is_Class_Wide_Type (Type_Id)
543 -- and then Base_Type
544 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
546 -- Wrong_Type (Expression (E), Type_Id);
549 -- We don't analyze the qualified expression itself because it's
550 -- part of the allocator. It is fully analyzed and resolved when
551 -- the allocator is resolved with the context type.
553 Set_Etype (E, Type_Id);
555 -- Case where allocator has a subtype indication
560 Base_Typ : Entity_Id;
563 -- If the allocator includes a N_Subtype_Indication then a
564 -- constraint is present, otherwise the node is a subtype mark.
565 -- Introduce an explicit subtype declaration into the tree
566 -- defining some anonymous subtype and rewrite the allocator to
567 -- use this subtype rather than the subtype indication.
569 -- It is important to introduce the explicit subtype declaration
570 -- so that the bounds of the subtype indication are attached to
571 -- the tree in case the allocator is inside a generic unit.
573 -- Finally, if there is no subtype indication and the type is
574 -- a tagged unconstrained type with discriminants, the designated
575 -- object is constrained by their default values, and it is
576 -- simplest to introduce an explicit constraint now. In some cases
577 -- this is done during expansion, but freeze actions are certain
578 -- to be emitted in the proper order if constraint is explicit.
580 if Is_Entity_Name (E) and then Expander_Active then
582 Type_Id := Entity (E);
584 if Is_Tagged_Type (Type_Id)
585 and then Has_Discriminants (Type_Id)
586 and then not Is_Constrained (Type_Id)
589 (Discriminant_Default_Value
590 (First_Discriminant (Type_Id)))
593 Constr : constant List_Id := New_List;
594 Loc : constant Source_Ptr := Sloc (E);
595 Discr : Entity_Id := First_Discriminant (Type_Id);
598 if Present (Discriminant_Default_Value (Discr)) then
599 while Present (Discr) loop
600 Append (Discriminant_Default_Value (Discr), Constr);
601 Next_Discriminant (Discr);
605 Make_Subtype_Indication (Loc,
606 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
608 Make_Index_Or_Discriminant_Constraint (Loc,
609 Constraints => Constr)));
615 if Nkind (E) = N_Subtype_Indication then
617 -- A constraint is only allowed for a composite type in Ada
618 -- 95. In Ada 83, a constraint is also allowed for an
619 -- access-to-composite type, but the constraint is ignored.
621 Find_Type (Subtype_Mark (E));
622 Base_Typ := Entity (Subtype_Mark (E));
624 if Is_Elementary_Type (Base_Typ) then
625 if not (Ada_Version = Ada_83
626 and then Is_Access_Type (Base_Typ))
628 Error_Msg_N ("constraint not allowed here", E);
630 if Nkind (Constraint (E)) =
631 N_Index_Or_Discriminant_Constraint
633 Error_Msg_N -- CODEFIX
634 ("\if qualified expression was meant, " &
635 "use apostrophe", Constraint (E));
639 -- Get rid of the bogus constraint:
641 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
642 Analyze_Allocator (N);
646 if Expander_Active then
647 Def_Id := Make_Temporary (Loc, 'S');
650 Make_Subtype_Declaration (Loc,
651 Defining_Identifier => Def_Id,
652 Subtype_Indication => Relocate_Node (E)));
654 if Sav_Errs /= Serious_Errors_Detected
655 and then Nkind (Constraint (E)) =
656 N_Index_Or_Discriminant_Constraint
658 Error_Msg_N -- CODEFIX
659 ("if qualified expression was meant, "
660 & "use apostrophe!", Constraint (E));
663 E := New_Occurrence_Of (Def_Id, Loc);
664 Rewrite (Expression (N), E);
668 Type_Id := Process_Subtype (E, N);
669 Acc_Type := Create_Itype (E_Allocator_Type, N);
670 Set_Etype (Acc_Type, Acc_Type);
671 Set_Directly_Designated_Type (Acc_Type, Type_Id);
672 Check_Fully_Declared (Type_Id, N);
674 -- Ada 2005 (AI-231): If the designated type is itself an access
675 -- type that excludes null, its default initialization will
676 -- be a null object, and we can insert an unconditional raise
677 -- before the allocator.
679 -- Ada 2012 (AI-104): A not null indication here is altogether
682 if Can_Never_Be_Null (Type_Id) then
684 Not_Null_Check : constant Node_Id :=
685 Make_Raise_Constraint_Error (Sloc (E),
686 Reason => CE_Null_Not_Allowed);
689 if Expander_Active then
690 Insert_Action (N, Not_Null_Check);
691 Analyze (Not_Null_Check);
693 elsif Warn_On_Ada_2012_Compatibility then
695 ("null value not allowed here in Ada 2012?y?", E);
700 -- Check for missing initialization. Skip this check if we already
701 -- had errors on analyzing the allocator, since in that case these
702 -- are probably cascaded errors.
704 if not Is_Definite_Subtype (Type_Id)
705 and then Serious_Errors_Detected = Sav_Errs
707 -- The build-in-place machinery may produce an allocator when
708 -- the designated type is indefinite but the underlying type is
709 -- not. In this case the unknown discriminants are meaningless
710 -- and should not trigger error messages. Check the parent node
711 -- because the allocator is marked as coming from source.
713 if Present (Underlying_Type (Type_Id))
714 and then Is_Definite_Subtype (Underlying_Type (Type_Id))
715 and then not Comes_From_Source (Parent (N))
719 -- An unusual case arises when the parent of a derived type is
720 -- a limited record extension with unknown discriminants, and
721 -- its full view has no discriminants.
723 -- A more general fix might be to create the proper underlying
724 -- type for such a derived type, but it is a record type with
725 -- no private attributes, so this required extending the
726 -- meaning of this attribute. ???
728 elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private
729 and then Present (Underlying_Type (Etype (Type_Id)))
731 not Has_Discriminants (Underlying_Type (Etype (Type_Id)))
732 and then not Comes_From_Source (Parent (N))
736 elsif Is_Class_Wide_Type (Type_Id) then
738 ("initialization required in class-wide allocation", N);
741 if Ada_Version < Ada_2005
742 and then Is_Limited_Type (Type_Id)
744 Error_Msg_N ("unconstrained allocation not allowed", N);
746 if Is_Array_Type (Type_Id) then
748 ("\constraint with array bounds required", N);
750 elsif Has_Unknown_Discriminants (Type_Id) then
753 else pragma Assert (Has_Discriminants (Type_Id));
755 ("\constraint with discriminant values required", N);
758 -- Limited Ada 2005 and general non-limited case
762 ("uninitialized unconstrained allocation not "
765 if Is_Array_Type (Type_Id) then
767 ("\qualified expression or constraint with "
768 & "array bounds required", N);
770 elsif Has_Unknown_Discriminants (Type_Id) then
771 Error_Msg_N ("\qualified expression required", N);
773 else pragma Assert (Has_Discriminants (Type_Id));
775 ("\qualified expression or constraint with "
776 & "discriminant values required", N);
784 if Is_Abstract_Type (Type_Id) then
785 Error_Msg_N ("cannot allocate abstract object", E);
788 if Has_Task (Designated_Type (Acc_Type)) then
789 Check_Restriction (No_Tasking, N);
790 Check_Restriction (Max_Tasks, N);
791 Check_Restriction (No_Task_Allocators, N);
794 -- Check restriction against dynamically allocated protected objects
796 if Has_Protected (Designated_Type (Acc_Type)) then
797 Check_Restriction (No_Protected_Type_Allocators, N);
800 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
801 -- type is nested, and the designated type needs finalization. The rule
802 -- is conservative in that class-wide types need finalization.
804 if Needs_Finalization (Designated_Type (Acc_Type))
805 and then not Is_Library_Level_Entity (Acc_Type)
807 Check_Restriction (No_Nested_Finalization, N);
810 -- Check that an allocator of a nested access type doesn't create a
811 -- protected object when restriction No_Local_Protected_Objects applies.
813 if Has_Protected (Designated_Type (Acc_Type))
814 and then not Is_Library_Level_Entity (Acc_Type)
816 Check_Restriction (No_Local_Protected_Objects, N);
819 -- Likewise for No_Local_Timing_Events
821 if Has_Timing_Event (Designated_Type (Acc_Type))
822 and then not Is_Library_Level_Entity (Acc_Type)
824 Check_Restriction (No_Local_Timing_Events, N);
827 -- If the No_Streams restriction is set, check that the type of the
828 -- object is not, and does not contain, any subtype derived from
829 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
830 -- Has_Stream just for efficiency reasons. There is no point in
831 -- spending time on a Has_Stream check if the restriction is not set.
833 if Restriction_Check_Required (No_Streams) then
834 if Has_Stream (Designated_Type (Acc_Type)) then
835 Check_Restriction (No_Streams, N);
839 Set_Etype (N, Acc_Type);
841 if not Is_Library_Level_Entity (Acc_Type) then
842 Check_Restriction (No_Local_Allocators, N);
845 if Serious_Errors_Detected > Sav_Errs then
846 Set_Error_Posted (N);
847 Set_Etype (N, Any_Type);
849 end Analyze_Allocator;
851 ---------------------------
852 -- Analyze_Arithmetic_Op --
853 ---------------------------
855 procedure Analyze_Arithmetic_Op (N : Node_Id) is
856 L : constant Node_Id := Left_Opnd (N);
857 R : constant Node_Id := Right_Opnd (N);
861 Candidate_Type := Empty;
862 Analyze_Expression (L);
863 Analyze_Expression (R);
865 -- If the entity is already set, the node is the instantiation of a
866 -- generic node with a non-local reference, or was manufactured by a
867 -- call to Make_Op_xxx. In either case the entity is known to be valid,
868 -- and we do not need to collect interpretations, instead we just get
869 -- the single possible interpretation.
873 if Present (Op_Id) then
874 if Ekind (Op_Id) = E_Operator then
876 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
877 and then Treat_Fixed_As_Integer (N)
881 Set_Etype (N, Any_Type);
882 Find_Arithmetic_Types (L, R, Op_Id, N);
886 Set_Etype (N, Any_Type);
887 Add_One_Interp (N, Op_Id, Etype (Op_Id));
890 -- Entity is not already set, so we do need to collect interpretations
893 Set_Etype (N, Any_Type);
895 Op_Id := Get_Name_Entity_Id (Chars (N));
896 while Present (Op_Id) loop
897 if Ekind (Op_Id) = E_Operator
898 and then Present (Next_Entity (First_Entity (Op_Id)))
900 Find_Arithmetic_Types (L, R, Op_Id, N);
902 -- The following may seem superfluous, because an operator cannot
903 -- be generic, but this ignores the cleverness of the author of
906 elsif Is_Overloadable (Op_Id) then
907 Analyze_User_Defined_Binary_Op (N, Op_Id);
910 Op_Id := Homonym (Op_Id);
915 Check_Function_Writable_Actuals (N);
916 end Analyze_Arithmetic_Op;
922 -- Function, procedure, and entry calls are checked here. The Name in
923 -- the call may be overloaded. The actuals have been analyzed and may
924 -- themselves be overloaded. On exit from this procedure, the node N
925 -- may have zero, one or more interpretations. In the first case an
926 -- error message is produced. In the last case, the node is flagged
927 -- as overloaded and the interpretations are collected in All_Interp.
929 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
930 -- the type-checking is similar to that of other calls.
932 procedure Analyze_Call (N : Node_Id) is
933 Actuals : constant List_Id := Parameter_Associations (N);
934 Loc : constant Source_Ptr := Sloc (N);
939 Success : Boolean := False;
941 Deref : Boolean := False;
942 -- Flag indicates whether an interpretation of the prefix is a
943 -- parameterless call that returns an access_to_subprogram.
945 procedure Check_Mixed_Parameter_And_Named_Associations;
946 -- Check that parameter and named associations are not mixed. This is
947 -- a restriction in SPARK mode.
949 procedure Check_Writable_Actuals (N : Node_Id);
950 -- If the call has out or in-out parameters then mark its outermost
951 -- enclosing construct as a node on which the writable actuals check
952 -- must be performed.
954 function Name_Denotes_Function return Boolean;
955 -- If the type of the name is an access to subprogram, this may be the
956 -- type of a name, or the return type of the function being called. If
957 -- the name is not an entity then it can denote a protected function.
958 -- Until we distinguish Etype from Return_Type, we must use this routine
959 -- to resolve the meaning of the name in the call.
961 procedure No_Interpretation;
962 -- Output error message when no valid interpretation exists
964 --------------------------------------------------
965 -- Check_Mixed_Parameter_And_Named_Associations --
966 --------------------------------------------------
968 procedure Check_Mixed_Parameter_And_Named_Associations is
970 Named_Seen : Boolean;
975 Actual := First (Actuals);
976 while Present (Actual) loop
977 case Nkind (Actual) is
978 when N_Parameter_Association =>
980 Check_SPARK_05_Restriction
981 ("named association cannot follow positional one",
992 end Check_Mixed_Parameter_And_Named_Associations;
994 ----------------------------
995 -- Check_Writable_Actuals --
996 ----------------------------
998 -- The identification of conflicts in calls to functions with writable
999 -- actuals is performed in the analysis phase of the front end to ensure
1000 -- that it reports exactly the same errors compiling with and without
1001 -- expansion enabled. It is performed in two stages:
1003 -- 1) When a call to a function with out-mode parameters is found,
1004 -- we climb to the outermost enclosing construct that can be
1005 -- evaluated in arbitrary order and we mark it with the flag
1008 -- 2) When the analysis of the marked node is complete, we traverse
1009 -- its decorated subtree searching for conflicts (see function
1010 -- Sem_Util.Check_Function_Writable_Actuals).
1012 -- The unique exception to this general rule is for aggregates, since
1013 -- their analysis is performed by the front end in the resolution
1014 -- phase. For aggregates we do not climb to their enclosing construct:
1015 -- we restrict the analysis to the subexpressions initializing the
1016 -- aggregate components.
1018 -- This implies that the analysis of expressions containing aggregates
1019 -- is not complete, since there may be conflicts on writable actuals
1020 -- involving subexpressions of the enclosing logical or arithmetic
1021 -- expressions. However, we cannot wait and perform the analysis when
1022 -- the whole subtree is resolved, since the subtrees may be transformed,
1023 -- thus adding extra complexity and computation cost to identify and
1024 -- report exactly the same errors compiling with and without expansion
1027 procedure Check_Writable_Actuals (N : Node_Id) is
1029 if Comes_From_Source (N)
1030 and then Present (Get_Subprogram_Entity (N))
1031 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1033 -- For procedures and entries there is no need to climb since
1034 -- we only need to check if the actuals of this call invoke
1035 -- functions whose out-mode parameters overlap.
1037 if Nkind (N) /= N_Function_Call then
1038 Set_Check_Actuals (N);
1040 -- For calls to functions we climb to the outermost enclosing
1041 -- construct where the out-mode actuals of this function may
1042 -- introduce conflicts.
1046 Outermost : Node_Id;
1050 while Present (P) loop
1052 -- For object declarations we can climb to the node from
1053 -- its object definition branch or from its initializing
1054 -- expression. We prefer to mark the child node as the
1055 -- outermost construct to avoid adding further complexity
1056 -- to the routine that will later take care of
1057 -- performing the writable actuals check.
1059 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1060 and then not Nkind_In (P, N_Assignment_Statement,
1061 N_Object_Declaration)
1066 -- Avoid climbing more than needed!
1068 exit when Stop_Subtree_Climbing (Nkind (P))
1069 or else (Nkind (P) = N_Range
1071 Nkind_In (Parent (P), N_In, N_Not_In));
1076 Set_Check_Actuals (Outermost);
1080 end Check_Writable_Actuals;
1082 ---------------------------
1083 -- Name_Denotes_Function --
1084 ---------------------------
1086 function Name_Denotes_Function return Boolean is
1088 if Is_Entity_Name (Nam) then
1089 return Ekind (Entity (Nam)) = E_Function;
1090 elsif Nkind (Nam) = N_Selected_Component then
1091 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1095 end Name_Denotes_Function;
1097 -----------------------
1098 -- No_Interpretation --
1099 -----------------------
1101 procedure No_Interpretation is
1102 L : constant Boolean := Is_List_Member (N);
1103 K : constant Node_Kind := Nkind (Parent (N));
1106 -- If the node is in a list whose parent is not an expression then it
1107 -- must be an attempted procedure call.
1109 if L and then K not in N_Subexpr then
1110 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1112 ("must instantiate generic procedure& before call",
1115 Error_Msg_N ("procedure or entry name expected", Nam);
1118 -- Check for tasking cases where only an entry call will do
1121 and then Nkind_In (K, N_Entry_Call_Alternative,
1122 N_Triggering_Alternative)
1124 Error_Msg_N ("entry name expected", Nam);
1126 -- Otherwise give general error message
1129 Error_Msg_N ("invalid prefix in call", Nam);
1131 end No_Interpretation;
1133 -- Start of processing for Analyze_Call
1136 if Restriction_Check_Required (SPARK_05) then
1137 Check_Mixed_Parameter_And_Named_Associations;
1140 -- Initialize the type of the result of the call to the error type,
1141 -- which will be reset if the type is successfully resolved.
1143 Set_Etype (N, Any_Type);
1147 if not Is_Overloaded (Nam) then
1149 -- Only one interpretation to check
1151 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1152 Nam_Ent := Etype (Nam);
1154 -- If the prefix is an access_to_subprogram, this may be an indirect
1155 -- call. This is the case if the name in the call is not an entity
1156 -- name, or if it is a function name in the context of a procedure
1157 -- call. In this latter case, we have a call to a parameterless
1158 -- function that returns a pointer_to_procedure which is the entity
1159 -- being called. Finally, F (X) may be a call to a parameterless
1160 -- function that returns a pointer to a function with parameters.
1161 -- Note that if F returns an access-to-subprogram whose designated
1162 -- type is an array, F (X) cannot be interpreted as an indirect call
1163 -- through the result of the call to F.
1165 elsif Is_Access_Type (Etype (Nam))
1166 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1168 (not Name_Denotes_Function
1169 or else Nkind (N) = N_Procedure_Call_Statement
1171 (Nkind (Parent (N)) /= N_Explicit_Dereference
1172 and then Is_Entity_Name (Nam)
1173 and then No (First_Formal (Entity (Nam)))
1175 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1176 and then Present (Actuals)))
1178 Nam_Ent := Designated_Type (Etype (Nam));
1179 Insert_Explicit_Dereference (Nam);
1181 -- Selected component case. Simple entry or protected operation,
1182 -- where the entry name is given by the selector name.
1184 elsif Nkind (Nam) = N_Selected_Component then
1185 Nam_Ent := Entity (Selector_Name (Nam));
1187 if not Ekind_In (Nam_Ent, E_Entry,
1192 Error_Msg_N ("name in call is not a callable entity", Nam);
1193 Set_Etype (N, Any_Type);
1197 -- If the name is an Indexed component, it can be a call to a member
1198 -- of an entry family. The prefix must be a selected component whose
1199 -- selector is the entry. Analyze_Procedure_Call normalizes several
1200 -- kinds of call into this form.
1202 elsif Nkind (Nam) = N_Indexed_Component then
1203 if Nkind (Prefix (Nam)) = N_Selected_Component then
1204 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1206 Error_Msg_N ("name in call is not a callable entity", Nam);
1207 Set_Etype (N, Any_Type);
1211 elsif not Is_Entity_Name (Nam) then
1212 Error_Msg_N ("name in call is not a callable entity", Nam);
1213 Set_Etype (N, Any_Type);
1217 Nam_Ent := Entity (Nam);
1219 -- If not overloadable, this may be a generalized indexing
1220 -- operation with named associations. Rewrite again as an
1221 -- indexed component and analyze as container indexing.
1223 if not Is_Overloadable (Nam_Ent) then
1225 (Find_Value_Of_Aspect
1226 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1229 Make_Indexed_Component (Sloc (N),
1231 Expressions => Parameter_Associations (N)));
1233 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1247 -- Operations generated for RACW stub types are called only through
1248 -- dispatching, and can never be the static interpretation of a call.
1250 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1255 Analyze_One_Call (N, Nam_Ent, True, Success);
1257 -- If this is an indirect call, the return type of the access_to
1258 -- subprogram may be an incomplete type. At the point of the call,
1259 -- use the full type if available, and at the same time update the
1260 -- return type of the access_to_subprogram.
1263 and then Nkind (Nam) = N_Explicit_Dereference
1264 and then Ekind (Etype (N)) = E_Incomplete_Type
1265 and then Present (Full_View (Etype (N)))
1267 Set_Etype (N, Full_View (Etype (N)));
1268 Set_Etype (Nam_Ent, Etype (N));
1274 -- An overloaded selected component must denote overloaded operations
1275 -- of a concurrent type. The interpretations are attached to the
1276 -- simple name of those operations.
1278 if Nkind (Nam) = N_Selected_Component then
1279 Nam := Selector_Name (Nam);
1282 Get_First_Interp (Nam, X, It);
1283 while Present (It.Nam) loop
1287 -- Name may be call that returns an access to subprogram, or more
1288 -- generally an overloaded expression one of whose interpretations
1289 -- yields an access to subprogram. If the name is an entity, we do
1290 -- not dereference, because the node is a call that returns the
1291 -- access type: note difference between f(x), where the call may
1292 -- return an access subprogram type, and f(x)(y), where the type
1293 -- returned by the call to f is implicitly dereferenced to analyze
1296 if Is_Access_Type (Nam_Ent) then
1297 Nam_Ent := Designated_Type (Nam_Ent);
1299 elsif Is_Access_Type (Etype (Nam_Ent))
1301 (not Is_Entity_Name (Nam)
1302 or else Nkind (N) = N_Procedure_Call_Statement)
1303 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1306 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1308 if Is_Entity_Name (Nam) then
1313 -- If the call has been rewritten from a prefixed call, the first
1314 -- parameter has been analyzed, but may need a subsequent
1315 -- dereference, so skip its analysis now.
1317 if N /= Original_Node (N)
1318 and then Nkind (Original_Node (N)) = Nkind (N)
1319 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1320 and then Present (Parameter_Associations (N))
1321 and then Present (Etype (First (Parameter_Associations (N))))
1324 (N, Nam_Ent, False, Success, Skip_First => True);
1326 Analyze_One_Call (N, Nam_Ent, False, Success);
1329 -- If the interpretation succeeds, mark the proper type of the
1330 -- prefix (any valid candidate will do). If not, remove the
1331 -- candidate interpretation. If this is a parameterless call
1332 -- on an anonymous access to subprogram, X is a variable with
1333 -- an access discriminant D, the entity in the interpretation is
1334 -- D, so rewrite X as X.D.all.
1338 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1340 if Ekind (It.Nam) = E_Discriminant
1341 and then Has_Implicit_Dereference (It.Nam)
1344 Make_Explicit_Dereference (Loc,
1346 Make_Selected_Component (Loc,
1348 New_Occurrence_Of (Entity (Nam), Loc),
1350 New_Occurrence_Of (It.Nam, Loc))));
1356 Set_Entity (Nam, It.Nam);
1357 Insert_Explicit_Dereference (Nam);
1358 Set_Etype (Nam, Nam_Ent);
1362 Set_Etype (Nam, It.Typ);
1365 elsif Nkind_In (Name (N), N_Function_Call, N_Selected_Component)
1370 Get_Next_Interp (X, It);
1373 -- If the name is the result of a function call, it can only be a
1374 -- call to a function returning an access to subprogram. Insert
1375 -- explicit dereference.
1377 if Nkind (Nam) = N_Function_Call then
1378 Insert_Explicit_Dereference (Nam);
1381 if Etype (N) = Any_Type then
1383 -- None of the interpretations is compatible with the actuals
1385 Diagnose_Call (N, Nam);
1387 -- Special checks for uninstantiated put routines
1389 if Nkind (N) = N_Procedure_Call_Statement
1390 and then Is_Entity_Name (Nam)
1391 and then Chars (Nam) = Name_Put
1392 and then List_Length (Actuals) = 1
1395 Arg : constant Node_Id := First (Actuals);
1399 if Nkind (Arg) = N_Parameter_Association then
1400 Typ := Etype (Explicit_Actual_Parameter (Arg));
1405 if Is_Signed_Integer_Type (Typ) then
1407 ("possible missing instantiation of "
1408 & "'Text_'I'O.'Integer_'I'O!", Nam);
1410 elsif Is_Modular_Integer_Type (Typ) then
1412 ("possible missing instantiation of "
1413 & "'Text_'I'O.'Modular_'I'O!", Nam);
1415 elsif Is_Floating_Point_Type (Typ) then
1417 ("possible missing instantiation of "
1418 & "'Text_'I'O.'Float_'I'O!", Nam);
1420 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1422 ("possible missing instantiation of "
1423 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1425 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1427 ("possible missing instantiation of "
1428 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1430 elsif Is_Enumeration_Type (Typ) then
1432 ("possible missing instantiation of "
1433 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1438 elsif not Is_Overloaded (N)
1439 and then Is_Entity_Name (Nam)
1441 -- Resolution yields a single interpretation. Verify that the
1442 -- reference has capitalization consistent with the declaration.
1444 Set_Entity_With_Checks (Nam, Entity (Nam));
1445 Generate_Reference (Entity (Nam), Nam);
1447 Set_Etype (Nam, Etype (Entity (Nam)));
1449 Remove_Abstract_Operations (N);
1455 if Ada_Version >= Ada_2012 then
1457 -- Check if the call contains a function with writable actuals
1459 Check_Writable_Actuals (N);
1461 -- If found and the outermost construct that can be evaluated in
1462 -- an arbitrary order is precisely this call, then check all its
1465 Check_Function_Writable_Actuals (N);
1469 -----------------------------
1470 -- Analyze_Case_Expression --
1471 -----------------------------
1473 procedure Analyze_Case_Expression (N : Node_Id) is
1474 procedure Non_Static_Choice_Error (Choice : Node_Id);
1475 -- Error routine invoked by the generic instantiation below when
1476 -- the case expression has a non static choice.
1478 package Case_Choices_Analysis is new
1479 Generic_Analyze_Choices
1480 (Process_Associated_Node => No_OP);
1481 use Case_Choices_Analysis;
1483 package Case_Choices_Checking is new
1484 Generic_Check_Choices
1485 (Process_Empty_Choice => No_OP,
1486 Process_Non_Static_Choice => Non_Static_Choice_Error,
1487 Process_Associated_Node => No_OP);
1488 use Case_Choices_Checking;
1490 -----------------------------
1491 -- Non_Static_Choice_Error --
1492 -----------------------------
1494 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1496 Flag_Non_Static_Expr
1497 ("choice given in case expression is not static!", Choice);
1498 end Non_Static_Choice_Error;
1502 Expr : constant Node_Id := Expression (N);
1504 Exp_Type : Entity_Id;
1505 Exp_Btype : Entity_Id;
1507 FirstX : Node_Id := Empty;
1508 -- First expression in the case for which there is some type information
1509 -- available, i.e. it is not Any_Type, which can happen because of some
1510 -- error, or from the use of e.g. raise Constraint_Error.
1512 Others_Present : Boolean;
1513 -- Indicates if Others was present
1515 Wrong_Alt : Node_Id := Empty;
1516 -- For error reporting
1518 -- Start of processing for Analyze_Case_Expression
1521 if Comes_From_Source (N) then
1522 Check_Compiler_Unit ("case expression", N);
1525 Analyze_And_Resolve (Expr, Any_Discrete);
1526 Check_Unset_Reference (Expr);
1527 Exp_Type := Etype (Expr);
1528 Exp_Btype := Base_Type (Exp_Type);
1530 Alt := First (Alternatives (N));
1531 while Present (Alt) loop
1532 Analyze (Expression (Alt));
1534 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1535 FirstX := Expression (Alt);
1541 -- Get our initial type from the first expression for which we got some
1542 -- useful type information from the expression.
1544 if not Is_Overloaded (FirstX) then
1545 Set_Etype (N, Etype (FirstX));
1553 Set_Etype (N, Any_Type);
1555 Get_First_Interp (FirstX, I, It);
1556 while Present (It.Nam) loop
1558 -- For each interpretation of the first expression, we only
1559 -- add the interpretation if every other expression in the
1560 -- case expression alternatives has a compatible type.
1562 Alt := Next (First (Alternatives (N)));
1563 while Present (Alt) loop
1564 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1569 Add_One_Interp (N, It.Typ, It.Typ);
1574 Get_Next_Interp (I, It);
1579 Exp_Btype := Base_Type (Exp_Type);
1581 -- The expression must be of a discrete type which must be determinable
1582 -- independently of the context in which the expression occurs, but
1583 -- using the fact that the expression must be of a discrete type.
1584 -- Moreover, the type this expression must not be a character literal
1585 -- (which is always ambiguous).
1587 -- If error already reported by Resolve, nothing more to do
1589 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1592 -- Special casee message for character literal
1594 elsif Exp_Btype = Any_Character then
1596 ("character literal as case expression is ambiguous", Expr);
1600 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1602 ("type incompatible with that of previous alternatives",
1603 Expression (Wrong_Alt));
1607 -- If the case expression is a formal object of mode in out, then
1608 -- treat it as having a nonstatic subtype by forcing use of the base
1609 -- type (which has to get passed to Check_Case_Choices below). Also
1610 -- use base type when the case expression is parenthesized.
1612 if Paren_Count (Expr) > 0
1613 or else (Is_Entity_Name (Expr)
1614 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1616 Exp_Type := Exp_Btype;
1619 -- The case expression alternatives cover the range of a static subtype
1620 -- subject to aspect Static_Predicate. Do not check the choices when the
1621 -- case expression has not been fully analyzed yet because this may lead
1624 if Is_OK_Static_Subtype (Exp_Type)
1625 and then Has_Static_Predicate_Aspect (Exp_Type)
1626 and then In_Spec_Expression
1630 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1633 Analyze_Choices (Alternatives (N), Exp_Type);
1634 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1637 if Exp_Type = Universal_Integer and then not Others_Present then
1639 ("case on universal integer requires OTHERS choice", Expr);
1641 end Analyze_Case_Expression;
1643 ---------------------------
1644 -- Analyze_Comparison_Op --
1645 ---------------------------
1647 procedure Analyze_Comparison_Op (N : Node_Id) is
1648 L : constant Node_Id := Left_Opnd (N);
1649 R : constant Node_Id := Right_Opnd (N);
1650 Op_Id : Entity_Id := Entity (N);
1653 Set_Etype (N, Any_Type);
1654 Candidate_Type := Empty;
1656 Analyze_Expression (L);
1657 Analyze_Expression (R);
1659 if Present (Op_Id) then
1660 if Ekind (Op_Id) = E_Operator then
1661 Find_Comparison_Types (L, R, Op_Id, N);
1663 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1666 if Is_Overloaded (L) then
1667 Set_Etype (L, Intersect_Types (L, R));
1671 Op_Id := Get_Name_Entity_Id (Chars (N));
1672 while Present (Op_Id) loop
1673 if Ekind (Op_Id) = E_Operator then
1674 Find_Comparison_Types (L, R, Op_Id, N);
1676 Analyze_User_Defined_Binary_Op (N, Op_Id);
1679 Op_Id := Homonym (Op_Id);
1684 Check_Function_Writable_Actuals (N);
1685 end Analyze_Comparison_Op;
1687 ---------------------------
1688 -- Analyze_Concatenation --
1689 ---------------------------
1691 procedure Analyze_Concatenation (N : Node_Id) is
1693 -- We wish to avoid deep recursion, because concatenations are often
1694 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1695 -- operands nonrecursively until we find something that is not a
1696 -- concatenation (A in this case), or has already been analyzed. We
1697 -- analyze that, and then walk back up the tree following Parent
1698 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1699 -- work at each level. The Parent pointers allow us to avoid recursion,
1700 -- and thus avoid running out of memory.
1706 Candidate_Type := Empty;
1708 -- The following code is equivalent to:
1710 -- Set_Etype (N, Any_Type);
1711 -- Analyze_Expression (Left_Opnd (N));
1712 -- Analyze_Concatenation_Rest (N);
1714 -- where the Analyze_Expression call recurses back here if the left
1715 -- operand is a concatenation.
1717 -- Walk down left operands
1720 Set_Etype (NN, Any_Type);
1721 L := Left_Opnd (NN);
1722 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1726 -- Now (given the above example) NN is A&B and L is A
1728 -- First analyze L ...
1730 Analyze_Expression (L);
1732 -- ... then walk NN back up until we reach N (where we started), calling
1733 -- Analyze_Concatenation_Rest along the way.
1736 Analyze_Concatenation_Rest (NN);
1740 end Analyze_Concatenation;
1742 --------------------------------
1743 -- Analyze_Concatenation_Rest --
1744 --------------------------------
1746 -- If the only one-dimensional array type in scope is String,
1747 -- this is the resulting type of the operation. Otherwise there
1748 -- will be a concatenation operation defined for each user-defined
1749 -- one-dimensional array.
1751 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1752 L : constant Node_Id := Left_Opnd (N);
1753 R : constant Node_Id := Right_Opnd (N);
1754 Op_Id : Entity_Id := Entity (N);
1759 Analyze_Expression (R);
1761 -- If the entity is present, the node appears in an instance, and
1762 -- denotes a predefined concatenation operation. The resulting type is
1763 -- obtained from the arguments when possible. If the arguments are
1764 -- aggregates, the array type and the concatenation type must be
1767 if Present (Op_Id) then
1768 if Ekind (Op_Id) = E_Operator then
1769 LT := Base_Type (Etype (L));
1770 RT := Base_Type (Etype (R));
1772 if Is_Array_Type (LT)
1773 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1775 Add_One_Interp (N, Op_Id, LT);
1777 elsif Is_Array_Type (RT)
1778 and then LT = Base_Type (Component_Type (RT))
1780 Add_One_Interp (N, Op_Id, RT);
1782 -- If one operand is a string type or a user-defined array type,
1783 -- and the other is a literal, result is of the specific type.
1786 (Root_Type (LT) = Standard_String
1787 or else Scope (LT) /= Standard_Standard)
1788 and then Etype (R) = Any_String
1790 Add_One_Interp (N, Op_Id, LT);
1793 (Root_Type (RT) = Standard_String
1794 or else Scope (RT) /= Standard_Standard)
1795 and then Etype (L) = Any_String
1797 Add_One_Interp (N, Op_Id, RT);
1799 elsif not Is_Generic_Type (Etype (Op_Id)) then
1800 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1803 -- Type and its operations must be visible
1805 Set_Entity (N, Empty);
1806 Analyze_Concatenation (N);
1810 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1814 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1815 while Present (Op_Id) loop
1816 if Ekind (Op_Id) = E_Operator then
1818 -- Do not consider operators declared in dead code, they can
1819 -- not be part of the resolution.
1821 if Is_Eliminated (Op_Id) then
1824 Find_Concatenation_Types (L, R, Op_Id, N);
1828 Analyze_User_Defined_Binary_Op (N, Op_Id);
1831 Op_Id := Homonym (Op_Id);
1836 end Analyze_Concatenation_Rest;
1838 -------------------------
1839 -- Analyze_Equality_Op --
1840 -------------------------
1842 procedure Analyze_Equality_Op (N : Node_Id) is
1843 Loc : constant Source_Ptr := Sloc (N);
1844 L : constant Node_Id := Left_Opnd (N);
1845 R : constant Node_Id := Right_Opnd (N);
1849 Set_Etype (N, Any_Type);
1850 Candidate_Type := Empty;
1852 Analyze_Expression (L);
1853 Analyze_Expression (R);
1855 -- If the entity is set, the node is a generic instance with a non-local
1856 -- reference to the predefined operator or to a user-defined function.
1857 -- It can also be an inequality that is expanded into the negation of a
1858 -- call to a user-defined equality operator.
1860 -- For the predefined case, the result is Boolean, regardless of the
1861 -- type of the operands. The operands may even be limited, if they are
1862 -- generic actuals. If they are overloaded, label the left argument with
1863 -- the common type that must be present, or with the type of the formal
1864 -- of the user-defined function.
1866 if Present (Entity (N)) then
1867 Op_Id := Entity (N);
1869 if Ekind (Op_Id) = E_Operator then
1870 Add_One_Interp (N, Op_Id, Standard_Boolean);
1872 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1875 if Is_Overloaded (L) then
1876 if Ekind (Op_Id) = E_Operator then
1877 Set_Etype (L, Intersect_Types (L, R));
1879 Set_Etype (L, Etype (First_Formal (Op_Id)));
1884 Op_Id := Get_Name_Entity_Id (Chars (N));
1885 while Present (Op_Id) loop
1886 if Ekind (Op_Id) = E_Operator then
1887 Find_Equality_Types (L, R, Op_Id, N);
1889 Analyze_User_Defined_Binary_Op (N, Op_Id);
1892 Op_Id := Homonym (Op_Id);
1896 -- If there was no match, and the operator is inequality, this may be
1897 -- a case where inequality has not been made explicit, as for tagged
1898 -- types. Analyze the node as the negation of an equality operation.
1899 -- This cannot be done earlier, because before analysis we cannot rule
1900 -- out the presence of an explicit inequality.
1902 if Etype (N) = Any_Type
1903 and then Nkind (N) = N_Op_Ne
1905 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1906 while Present (Op_Id) loop
1907 if Ekind (Op_Id) = E_Operator then
1908 Find_Equality_Types (L, R, Op_Id, N);
1910 Analyze_User_Defined_Binary_Op (N, Op_Id);
1913 Op_Id := Homonym (Op_Id);
1916 if Etype (N) /= Any_Type then
1917 Op_Id := Entity (N);
1923 Left_Opnd => Left_Opnd (N),
1924 Right_Opnd => Right_Opnd (N))));
1926 Set_Entity (Right_Opnd (N), Op_Id);
1932 Check_Function_Writable_Actuals (N);
1933 end Analyze_Equality_Op;
1935 ----------------------------------
1936 -- Analyze_Explicit_Dereference --
1937 ----------------------------------
1939 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1940 Loc : constant Source_Ptr := Sloc (N);
1941 P : constant Node_Id := Prefix (N);
1947 function Is_Function_Type return Boolean;
1948 -- Check whether node may be interpreted as an implicit function call
1950 ----------------------
1951 -- Is_Function_Type --
1952 ----------------------
1954 function Is_Function_Type return Boolean is
1959 if not Is_Overloaded (N) then
1960 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1961 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1964 Get_First_Interp (N, I, It);
1965 while Present (It.Nam) loop
1966 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1967 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1972 Get_Next_Interp (I, It);
1977 end Is_Function_Type;
1979 -- Start of processing for Analyze_Explicit_Dereference
1982 -- If source node, check SPARK restriction. We guard this with the
1983 -- source node check, because ???
1985 if Comes_From_Source (N) then
1986 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
1989 -- In formal verification mode, keep track of all reads and writes
1990 -- through explicit dereferences.
1992 if GNATprove_Mode then
1993 SPARK_Specific.Generate_Dereference (N);
1997 Set_Etype (N, Any_Type);
1999 -- Test for remote access to subprogram type, and if so return
2000 -- after rewriting the original tree.
2002 if Remote_AST_E_Dereference (P) then
2006 -- Normal processing for other than remote access to subprogram type
2008 if not Is_Overloaded (P) then
2009 if Is_Access_Type (Etype (P)) then
2011 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
2012 -- avoid other problems caused by the Private_Subtype and it is
2013 -- safe to go to the Base_Type because this is the same as
2014 -- converting the access value to its Base_Type.
2017 DT : Entity_Id := Designated_Type (Etype (P));
2020 if Ekind (DT) = E_Private_Subtype
2021 and then Is_For_Access_Subtype (DT)
2023 DT := Base_Type (DT);
2026 -- An explicit dereference is a legal occurrence of an
2027 -- incomplete type imported through a limited_with clause, if
2028 -- the full view is visible, or if we are within an instance
2029 -- body, where the enclosing body has a regular with_clause
2032 if From_Limited_With (DT)
2033 and then not From_Limited_With (Scope (DT))
2035 (Is_Immediately_Visible (Scope (DT))
2037 (Is_Child_Unit (Scope (DT))
2038 and then Is_Visible_Lib_Unit (Scope (DT)))
2039 or else In_Instance_Body)
2041 Set_Etype (N, Available_View (DT));
2048 elsif Etype (P) /= Any_Type then
2049 Error_Msg_N ("prefix of dereference must be an access type", N);
2054 Get_First_Interp (P, I, It);
2055 while Present (It.Nam) loop
2058 if Is_Access_Type (T) then
2059 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2062 Get_Next_Interp (I, It);
2065 -- Error if no interpretation of the prefix has an access type
2067 if Etype (N) = Any_Type then
2069 ("access type required in prefix of explicit dereference", P);
2070 Set_Etype (N, Any_Type);
2076 and then Nkind (Parent (N)) /= N_Indexed_Component
2078 and then (Nkind (Parent (N)) /= N_Function_Call
2079 or else N /= Name (Parent (N)))
2081 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2082 or else N /= Name (Parent (N)))
2084 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2085 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2087 (Attribute_Name (Parent (N)) /= Name_Address
2089 Attribute_Name (Parent (N)) /= Name_Access))
2091 -- Name is a function call with no actuals, in a context that
2092 -- requires deproceduring (including as an actual in an enclosing
2093 -- function or procedure call). There are some pathological cases
2094 -- where the prefix might include functions that return access to
2095 -- subprograms and others that return a regular type. Disambiguation
2096 -- of those has to take place in Resolve.
2099 Make_Function_Call (Loc,
2100 Name => Make_Explicit_Dereference (Loc, P),
2101 Parameter_Associations => New_List);
2103 -- If the prefix is overloaded, remove operations that have formals,
2104 -- we know that this is a parameterless call.
2106 if Is_Overloaded (P) then
2107 Get_First_Interp (P, I, It);
2108 while Present (It.Nam) loop
2111 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2117 Get_Next_Interp (I, It);
2124 elsif not Is_Function_Type
2125 and then Is_Overloaded (N)
2127 -- The prefix may include access to subprograms and other access
2128 -- types. If the context selects the interpretation that is a
2129 -- function call (not a procedure call) we cannot rewrite the node
2130 -- yet, but we include the result of the call interpretation.
2132 Get_First_Interp (N, I, It);
2133 while Present (It.Nam) loop
2134 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2135 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2136 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2138 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2141 Get_Next_Interp (I, It);
2145 -- A value of remote access-to-class-wide must not be dereferenced
2148 Validate_Remote_Access_To_Class_Wide_Type (N);
2149 end Analyze_Explicit_Dereference;
2151 ------------------------
2152 -- Analyze_Expression --
2153 ------------------------
2155 procedure Analyze_Expression (N : Node_Id) is
2158 -- If the expression is an indexed component that will be rewritten
2159 -- as a container indexing, it has already been analyzed.
2161 if Nkind (N) = N_Indexed_Component
2162 and then Present (Generalized_Indexing (N))
2168 Check_Parameterless_Call (N);
2170 end Analyze_Expression;
2172 -------------------------------------
2173 -- Analyze_Expression_With_Actions --
2174 -------------------------------------
2176 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2180 A := First (Actions (N));
2181 while Present (A) loop
2186 Analyze_Expression (Expression (N));
2187 Set_Etype (N, Etype (Expression (N)));
2188 end Analyze_Expression_With_Actions;
2190 ---------------------------
2191 -- Analyze_If_Expression --
2192 ---------------------------
2194 procedure Analyze_If_Expression (N : Node_Id) is
2195 Condition : constant Node_Id := First (Expressions (N));
2196 Then_Expr : constant Node_Id := Next (Condition);
2197 Else_Expr : Node_Id;
2200 -- Defend against error of missing expressions from previous error
2202 if No (Then_Expr) then
2203 Check_Error_Detected;
2207 if Comes_From_Source (N) then
2208 Check_SPARK_05_Restriction ("if expression is not allowed", N);
2211 Else_Expr := Next (Then_Expr);
2213 if Comes_From_Source (N) then
2214 Check_Compiler_Unit ("if expression", N);
2217 -- Analyze and resolve the condition. We need to resolve this now so
2218 -- that it gets folded to True/False if possible, before we analyze
2219 -- the THEN/ELSE branches, because when analyzing these branches, we
2220 -- may call Is_Statically_Unevaluated, which expects the condition of
2221 -- an enclosing IF to have been analyze/resolved/evaluated.
2223 Analyze_Expression (Condition);
2224 Resolve (Condition, Any_Boolean);
2226 -- Analyze THEN expression and (if present) ELSE expression. For those
2227 -- we delay resolution in the normal manner, because of overloading etc.
2229 Analyze_Expression (Then_Expr);
2231 if Present (Else_Expr) then
2232 Analyze_Expression (Else_Expr);
2235 -- If then expression not overloaded, then that decides the type
2237 if not Is_Overloaded (Then_Expr) then
2238 Set_Etype (N, Etype (Then_Expr));
2240 -- Case where then expression is overloaded
2248 Set_Etype (N, Any_Type);
2250 -- Loop through interpretations of Then_Expr
2252 Get_First_Interp (Then_Expr, I, It);
2253 while Present (It.Nam) loop
2255 -- Add possible interpretation of Then_Expr if no Else_Expr, or
2256 -- Else_Expr is present and has a compatible type.
2259 or else Has_Compatible_Type (Else_Expr, It.Typ)
2261 Add_One_Interp (N, It.Typ, It.Typ);
2264 Get_Next_Interp (I, It);
2267 -- If no valid interpretation has been found, then the type of the
2268 -- ELSE expression does not match any interpretation of the THEN
2271 if Etype (N) = Any_Type then
2273 ("type incompatible with that of `THEN` expression",
2279 end Analyze_If_Expression;
2281 ------------------------------------
2282 -- Analyze_Indexed_Component_Form --
2283 ------------------------------------
2285 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2286 P : constant Node_Id := Prefix (N);
2287 Exprs : constant List_Id := Expressions (N);
2293 procedure Process_Function_Call;
2294 -- Prefix in indexed component form is an overloadable entity, so the
2295 -- node is a function call. Reformat it as such.
2297 procedure Process_Indexed_Component;
2298 -- Prefix in indexed component form is actually an indexed component.
2299 -- This routine processes it, knowing that the prefix is already
2302 procedure Process_Indexed_Component_Or_Slice;
2303 -- An indexed component with a single index may designate a slice if
2304 -- the index is a subtype mark. This routine disambiguates these two
2305 -- cases by resolving the prefix to see if it is a subtype mark.
2307 procedure Process_Overloaded_Indexed_Component;
2308 -- If the prefix of an indexed component is overloaded, the proper
2309 -- interpretation is selected by the index types and the context.
2311 ---------------------------
2312 -- Process_Function_Call --
2313 ---------------------------
2315 procedure Process_Function_Call is
2316 Loc : constant Source_Ptr := Sloc (N);
2320 Change_Node (N, N_Function_Call);
2322 Set_Parameter_Associations (N, Exprs);
2324 -- Analyze actuals prior to analyzing the call itself
2326 Actual := First (Parameter_Associations (N));
2327 while Present (Actual) loop
2329 Check_Parameterless_Call (Actual);
2331 -- Move to next actual. Note that we use Next, not Next_Actual
2332 -- here. The reason for this is a bit subtle. If a function call
2333 -- includes named associations, the parser recognizes the node
2334 -- as a call, and it is analyzed as such. If all associations are
2335 -- positional, the parser builds an indexed_component node, and
2336 -- it is only after analysis of the prefix that the construct
2337 -- is recognized as a call, in which case Process_Function_Call
2338 -- rewrites the node and analyzes the actuals. If the list of
2339 -- actuals is malformed, the parser may leave the node as an
2340 -- indexed component (despite the presence of named associations).
2341 -- The iterator Next_Actual is equivalent to Next if the list is
2342 -- positional, but follows the normalized chain of actuals when
2343 -- named associations are present. In this case normalization has
2344 -- not taken place, and actuals remain unanalyzed, which leads to
2345 -- subsequent crashes or loops if there is an attempt to continue
2346 -- analysis of the program.
2348 -- IF there is a single actual and it is a type name, the node
2349 -- can only be interpreted as a slice of a parameterless call.
2350 -- Rebuild the node as such and analyze.
2352 if No (Next (Actual))
2353 and then Is_Entity_Name (Actual)
2354 and then Is_Type (Entity (Actual))
2355 and then Is_Discrete_Type (Entity (Actual))
2361 New_Occurrence_Of (Entity (Actual), Loc)));
2371 end Process_Function_Call;
2373 -------------------------------
2374 -- Process_Indexed_Component --
2375 -------------------------------
2377 procedure Process_Indexed_Component is
2379 Array_Type : Entity_Id;
2381 Pent : Entity_Id := Empty;
2384 Exp := First (Exprs);
2386 if Is_Overloaded (P) then
2387 Process_Overloaded_Indexed_Component;
2390 Array_Type := Etype (P);
2392 if Is_Entity_Name (P) then
2394 elsif Nkind (P) = N_Selected_Component
2395 and then Is_Entity_Name (Selector_Name (P))
2397 Pent := Entity (Selector_Name (P));
2400 -- Prefix must be appropriate for an array type, taking into
2401 -- account a possible implicit dereference.
2403 if Is_Access_Type (Array_Type) then
2405 (Warn_On_Dereference, "?d?implicit dereference", N);
2406 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2409 if Is_Array_Type (Array_Type) then
2412 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2414 Set_Etype (N, Any_Type);
2416 if not Has_Compatible_Type
2417 (Exp, Entry_Index_Type (Pent))
2419 Error_Msg_N ("invalid index type in entry name", N);
2421 elsif Present (Next (Exp)) then
2422 Error_Msg_N ("too many subscripts in entry reference", N);
2425 Set_Etype (N, Etype (P));
2430 elsif Is_Record_Type (Array_Type)
2431 and then Remote_AST_I_Dereference (P)
2435 elsif Try_Container_Indexing (N, P, Exprs) then
2438 elsif Array_Type = Any_Type then
2439 Set_Etype (N, Any_Type);
2441 -- In most cases the analysis of the prefix will have emitted
2442 -- an error already, but if the prefix may be interpreted as a
2443 -- call in prefixed notation, the report is left to the caller.
2444 -- To prevent cascaded errors, report only if no previous ones.
2446 if Serious_Errors_Detected = 0 then
2447 Error_Msg_N ("invalid prefix in indexed component", P);
2449 if Nkind (P) = N_Expanded_Name then
2450 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2456 -- Here we definitely have a bad indexing
2459 if Nkind (Parent (N)) = N_Requeue_Statement
2460 and then Present (Pent) and then Ekind (Pent) = E_Entry
2463 ("REQUEUE does not permit parameters", First (Exprs));
2465 elsif Is_Entity_Name (P)
2466 and then Etype (P) = Standard_Void_Type
2468 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2471 Error_Msg_N ("array type required in indexed component", P);
2474 Set_Etype (N, Any_Type);
2478 Index := First_Index (Array_Type);
2479 while Present (Index) and then Present (Exp) loop
2480 if not Has_Compatible_Type (Exp, Etype (Index)) then
2481 Wrong_Type (Exp, Etype (Index));
2482 Set_Etype (N, Any_Type);
2490 Set_Etype (N, Component_Type (Array_Type));
2491 Check_Implicit_Dereference (N, Etype (N));
2493 if Present (Index) then
2495 ("too few subscripts in array reference", First (Exprs));
2497 elsif Present (Exp) then
2498 Error_Msg_N ("too many subscripts in array reference", Exp);
2501 end Process_Indexed_Component;
2503 ----------------------------------------
2504 -- Process_Indexed_Component_Or_Slice --
2505 ----------------------------------------
2507 procedure Process_Indexed_Component_Or_Slice is
2509 Exp := First (Exprs);
2510 while Present (Exp) loop
2511 Analyze_Expression (Exp);
2515 Exp := First (Exprs);
2517 -- If one index is present, and it is a subtype name, then the node
2518 -- denotes a slice (note that the case of an explicit range for a
2519 -- slice was already built as an N_Slice node in the first place,
2520 -- so that case is not handled here).
2522 -- We use a replace rather than a rewrite here because this is one
2523 -- of the cases in which the tree built by the parser is plain wrong.
2526 and then Is_Entity_Name (Exp)
2527 and then Is_Type (Entity (Exp))
2530 Make_Slice (Sloc (N),
2532 Discrete_Range => New_Copy (Exp)));
2535 -- Otherwise (more than one index present, or single index is not
2536 -- a subtype name), then we have the indexed component case.
2539 Process_Indexed_Component;
2541 end Process_Indexed_Component_Or_Slice;
2543 ------------------------------------------
2544 -- Process_Overloaded_Indexed_Component --
2545 ------------------------------------------
2547 procedure Process_Overloaded_Indexed_Component is
2556 Set_Etype (N, Any_Type);
2558 Get_First_Interp (P, I, It);
2559 while Present (It.Nam) loop
2562 if Is_Access_Type (Typ) then
2563 Typ := Designated_Type (Typ);
2565 (Warn_On_Dereference, "?d?implicit dereference", N);
2568 if Is_Array_Type (Typ) then
2570 -- Got a candidate: verify that index types are compatible
2572 Index := First_Index (Typ);
2574 Exp := First (Exprs);
2575 while Present (Index) and then Present (Exp) loop
2576 if Has_Compatible_Type (Exp, Etype (Index)) then
2588 if Found and then No (Index) and then No (Exp) then
2590 CT : constant Entity_Id :=
2591 Base_Type (Component_Type (Typ));
2593 Add_One_Interp (N, CT, CT);
2594 Check_Implicit_Dereference (N, CT);
2598 elsif Try_Container_Indexing (N, P, Exprs) then
2603 Get_Next_Interp (I, It);
2606 if Etype (N) = Any_Type then
2607 Error_Msg_N ("no legal interpretation for indexed component", N);
2608 Set_Is_Overloaded (N, False);
2612 end Process_Overloaded_Indexed_Component;
2614 -- Start of processing for Analyze_Indexed_Component_Form
2617 -- Get name of array, function or type
2621 -- If P is an explicit dereference whose prefix is of a remote access-
2622 -- to-subprogram type, then N has already been rewritten as a subprogram
2623 -- call and analyzed.
2625 if Nkind (N) in N_Subprogram_Call then
2628 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2629 -- the indexed component denotes a loop name, the indexed form is turned
2630 -- into an attribute reference.
2632 elsif Nkind (N) = N_Attribute_Reference
2633 and then Attribute_Name (N) = Name_Loop_Entry
2638 pragma Assert (Nkind (N) = N_Indexed_Component);
2640 P_T := Base_Type (Etype (P));
2642 if Is_Entity_Name (P) and then Present (Entity (P)) then
2645 if Is_Type (U_N) then
2647 -- Reformat node as a type conversion
2649 E := Remove_Head (Exprs);
2651 if Present (First (Exprs)) then
2653 ("argument of type conversion must be single expression", N);
2656 Change_Node (N, N_Type_Conversion);
2657 Set_Subtype_Mark (N, P);
2659 Set_Expression (N, E);
2661 -- After changing the node, call for the specific Analysis
2662 -- routine directly, to avoid a double call to the expander.
2664 Analyze_Type_Conversion (N);
2668 if Is_Overloadable (U_N) then
2669 Process_Function_Call;
2671 elsif Ekind (Etype (P)) = E_Subprogram_Type
2672 or else (Is_Access_Type (Etype (P))
2674 Ekind (Designated_Type (Etype (P))) =
2677 -- Call to access_to-subprogram with possible implicit dereference
2679 Process_Function_Call;
2681 elsif Is_Generic_Subprogram (U_N) then
2683 -- A common beginner's (or C++ templates fan) error
2685 Error_Msg_N ("generic subprogram cannot be called", N);
2686 Set_Etype (N, Any_Type);
2690 Process_Indexed_Component_Or_Slice;
2693 -- If not an entity name, prefix is an expression that may denote
2694 -- an array or an access-to-subprogram.
2697 if Ekind (P_T) = E_Subprogram_Type
2698 or else (Is_Access_Type (P_T)
2700 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2702 Process_Function_Call;
2704 elsif Nkind (P) = N_Selected_Component
2705 and then Present (Entity (Selector_Name (P)))
2706 and then Is_Overloadable (Entity (Selector_Name (P)))
2708 Process_Function_Call;
2710 -- In ASIS mode within a generic, a prefixed call is analyzed and
2711 -- partially rewritten but the original indexed component has not
2712 -- yet been rewritten as a call. Perform the replacement now.
2714 elsif Nkind (P) = N_Selected_Component
2715 and then Nkind (Parent (P)) = N_Function_Call
2718 Rewrite (N, Parent (P));
2722 -- Indexed component, slice, or a call to a member of a family
2723 -- entry, which will be converted to an entry call later.
2725 Process_Indexed_Component_Or_Slice;
2729 Analyze_Dimension (N);
2730 end Analyze_Indexed_Component_Form;
2732 ------------------------
2733 -- Analyze_Logical_Op --
2734 ------------------------
2736 procedure Analyze_Logical_Op (N : Node_Id) is
2737 L : constant Node_Id := Left_Opnd (N);
2738 R : constant Node_Id := Right_Opnd (N);
2739 Op_Id : Entity_Id := Entity (N);
2742 Set_Etype (N, Any_Type);
2743 Candidate_Type := Empty;
2745 Analyze_Expression (L);
2746 Analyze_Expression (R);
2748 if Present (Op_Id) then
2750 if Ekind (Op_Id) = E_Operator then
2751 Find_Boolean_Types (L, R, Op_Id, N);
2753 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2757 Op_Id := Get_Name_Entity_Id (Chars (N));
2758 while Present (Op_Id) loop
2759 if Ekind (Op_Id) = E_Operator then
2760 Find_Boolean_Types (L, R, Op_Id, N);
2762 Analyze_User_Defined_Binary_Op (N, Op_Id);
2765 Op_Id := Homonym (Op_Id);
2770 Check_Function_Writable_Actuals (N);
2771 end Analyze_Logical_Op;
2773 ---------------------------
2774 -- Analyze_Membership_Op --
2775 ---------------------------
2777 procedure Analyze_Membership_Op (N : Node_Id) is
2778 Loc : constant Source_Ptr := Sloc (N);
2779 L : constant Node_Id := Left_Opnd (N);
2780 R : constant Node_Id := Right_Opnd (N);
2782 Index : Interp_Index;
2784 Found : Boolean := False;
2788 procedure Try_One_Interp (T1 : Entity_Id);
2789 -- Routine to try one proposed interpretation. Note that the context
2790 -- of the operation plays no role in resolving the arguments, so that
2791 -- if there is more than one interpretation of the operands that is
2792 -- compatible with a membership test, the operation is ambiguous.
2794 --------------------
2795 -- Try_One_Interp --
2796 --------------------
2798 procedure Try_One_Interp (T1 : Entity_Id) is
2800 if Has_Compatible_Type (R, T1) then
2802 and then Base_Type (T1) /= Base_Type (T_F)
2804 It := Disambiguate (L, I_F, Index, Any_Type);
2806 if It = No_Interp then
2807 Ambiguous_Operands (N);
2808 Set_Etype (L, Any_Type);
2825 procedure Analyze_Set_Membership;
2826 -- If a set of alternatives is present, analyze each and find the
2827 -- common type to which they must all resolve.
2829 ----------------------------
2830 -- Analyze_Set_Membership --
2831 ----------------------------
2833 procedure Analyze_Set_Membership is
2835 Index : Interp_Index;
2837 Candidate_Interps : Node_Id;
2838 Common_Type : Entity_Id := Empty;
2841 if Comes_From_Source (N) then
2842 Check_Compiler_Unit ("set membership", N);
2846 Candidate_Interps := L;
2848 if not Is_Overloaded (L) then
2849 Common_Type := Etype (L);
2851 Alt := First (Alternatives (N));
2852 while Present (Alt) loop
2855 if not Has_Compatible_Type (Alt, Common_Type) then
2856 Wrong_Type (Alt, Common_Type);
2863 Alt := First (Alternatives (N));
2864 while Present (Alt) loop
2866 if not Is_Overloaded (Alt) then
2867 Common_Type := Etype (Alt);
2870 Get_First_Interp (Alt, Index, It);
2871 while Present (It.Typ) loop
2873 Has_Compatible_Type (Candidate_Interps, It.Typ)
2875 Remove_Interp (Index);
2878 Get_Next_Interp (Index, It);
2881 Get_First_Interp (Alt, Index, It);
2884 Error_Msg_N ("alternative has no legal type", Alt);
2888 -- If alternative is not overloaded, we have a unique type
2891 Set_Etype (Alt, It.Typ);
2892 Get_Next_Interp (Index, It);
2895 Set_Is_Overloaded (Alt, False);
2896 Common_Type := Etype (Alt);
2899 Candidate_Interps := Alt;
2906 Set_Etype (N, Standard_Boolean);
2908 if Present (Common_Type) then
2909 Set_Etype (L, Common_Type);
2911 -- The left operand may still be overloaded, to be resolved using
2915 Error_Msg_N ("cannot resolve membership operation", N);
2917 end Analyze_Set_Membership;
2919 -- Start of processing for Analyze_Membership_Op
2922 Analyze_Expression (L);
2924 if No (R) and then Ada_Version >= Ada_2012 then
2925 Analyze_Set_Membership;
2926 Check_Function_Writable_Actuals (N);
2931 if Nkind (R) = N_Range
2932 or else (Nkind (R) = N_Attribute_Reference
2933 and then Attribute_Name (R) = Name_Range)
2937 if not Is_Overloaded (L) then
2938 Try_One_Interp (Etype (L));
2941 Get_First_Interp (L, Index, It);
2942 while Present (It.Typ) loop
2943 Try_One_Interp (It.Typ);
2944 Get_Next_Interp (Index, It);
2948 -- If not a range, it can be a subtype mark, or else it is a degenerate
2949 -- membership test with a singleton value, i.e. a test for equality,
2950 -- if the types are compatible.
2955 if Is_Entity_Name (R)
2956 and then Is_Type (Entity (R))
2959 Check_Fully_Declared (Entity (R), R);
2961 elsif Ada_Version >= Ada_2012
2962 and then Has_Compatible_Type (R, Etype (L))
2964 if Nkind (N) = N_In then
2980 -- In all versions of the language, if we reach this point there
2981 -- is a previous error that will be diagnosed below.
2987 -- Compatibility between expression and subtype mark or range is
2988 -- checked during resolution. The result of the operation is Boolean
2991 Set_Etype (N, Standard_Boolean);
2993 if Comes_From_Source (N)
2994 and then Present (Right_Opnd (N))
2995 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2997 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3000 Check_Function_Writable_Actuals (N);
3001 end Analyze_Membership_Op;
3007 procedure Analyze_Mod (N : Node_Id) is
3009 -- A special warning check, if we have an expression of the form:
3010 -- expr mod 2 * literal
3011 -- where literal is 64 or less, then probably what was meant was
3012 -- expr mod 2 ** literal
3013 -- so issue an appropriate warning.
3015 if Warn_On_Suspicious_Modulus_Value
3016 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3017 and then Intval (Right_Opnd (N)) = Uint_2
3018 and then Nkind (Parent (N)) = N_Op_Multiply
3019 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3020 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
3023 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
3026 -- Remaining processing is same as for other arithmetic operators
3028 Analyze_Arithmetic_Op (N);
3031 ----------------------
3032 -- Analyze_Negation --
3033 ----------------------
3035 procedure Analyze_Negation (N : Node_Id) is
3036 R : constant Node_Id := Right_Opnd (N);
3037 Op_Id : Entity_Id := Entity (N);
3040 Set_Etype (N, Any_Type);
3041 Candidate_Type := Empty;
3043 Analyze_Expression (R);
3045 if Present (Op_Id) then
3046 if Ekind (Op_Id) = E_Operator then
3047 Find_Negation_Types (R, Op_Id, N);
3049 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3053 Op_Id := Get_Name_Entity_Id (Chars (N));
3054 while Present (Op_Id) loop
3055 if Ekind (Op_Id) = E_Operator then
3056 Find_Negation_Types (R, Op_Id, N);
3058 Analyze_User_Defined_Unary_Op (N, Op_Id);
3061 Op_Id := Homonym (Op_Id);
3066 end Analyze_Negation;
3072 procedure Analyze_Null (N : Node_Id) is
3074 Check_SPARK_05_Restriction ("null is not allowed", N);
3076 Set_Etype (N, Any_Access);
3079 ----------------------
3080 -- Analyze_One_Call --
3081 ----------------------
3083 procedure Analyze_One_Call
3087 Success : out Boolean;
3088 Skip_First : Boolean := False)
3090 Actuals : constant List_Id := Parameter_Associations (N);
3091 Prev_T : constant Entity_Id := Etype (N);
3093 Must_Skip : constant Boolean := Skip_First
3094 or else Nkind (Original_Node (N)) = N_Selected_Component
3096 (Nkind (Original_Node (N)) = N_Indexed_Component
3097 and then Nkind (Prefix (Original_Node (N)))
3098 = N_Selected_Component);
3099 -- The first formal must be omitted from the match when trying to find
3100 -- a primitive operation that is a possible interpretation, and also
3101 -- after the call has been rewritten, because the corresponding actual
3102 -- is already known to be compatible, and because this may be an
3103 -- indexing of a call with default parameters.
3107 Is_Indexed : Boolean := False;
3108 Is_Indirect : Boolean := False;
3109 Subp_Type : constant Entity_Id := Etype (Nam);
3112 function Compatible_Types_In_Predicate
3114 T2 : Entity_Id) return Boolean;
3115 -- For an Ada 2012 predicate or invariant, a call may mention an
3116 -- incomplete type, while resolution of the corresponding predicate
3117 -- function may see the full view, as a consequence of the delayed
3118 -- resolution of the corresponding expressions. This may occur in
3119 -- the body of a predicate function, or in a call to such. Anomalies
3120 -- involving private and full views can also happen. In each case,
3121 -- rewrite node or add conversions to remove spurious type errors.
3123 procedure Indicate_Name_And_Type;
3124 -- If candidate interpretation matches, indicate name and type of result
3127 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3128 -- There may be a user-defined operator that hides the current
3129 -- interpretation. We must check for this independently of the
3130 -- analysis of the call with the user-defined operation, because
3131 -- the parameter names may be wrong and yet the hiding takes place.
3132 -- This fixes a problem with ACATS test B34014O.
3134 -- When the type Address is a visible integer type, and the DEC
3135 -- system extension is visible, the predefined operator may be
3136 -- hidden as well, by one of the address operations in auxdec.
3137 -- Finally, The abstract operations on address do not hide the
3138 -- predefined operator (this is the purpose of making them abstract).
3140 -----------------------------------
3141 -- Compatible_Types_In_Predicate --
3142 -----------------------------------
3144 function Compatible_Types_In_Predicate
3146 T2 : Entity_Id) return Boolean
3148 function Common_Type (T : Entity_Id) return Entity_Id;
3149 -- Find non-private full view if any, without going to ancestor type
3150 -- (as opposed to Underlying_Type).
3156 function Common_Type (T : Entity_Id) return Entity_Id is
3158 if Is_Private_Type (T) and then Present (Full_View (T)) then
3159 return Base_Type (Full_View (T));
3161 return Base_Type (T);
3165 -- Start of processing for Compatible_Types_In_Predicate
3168 if (Ekind (Current_Scope) = E_Function
3169 and then Is_Predicate_Function (Current_Scope))
3171 (Ekind (Nam) = E_Function
3172 and then Is_Predicate_Function (Nam))
3174 if Is_Incomplete_Type (T1)
3175 and then Present (Full_View (T1))
3176 and then Full_View (T1) = T2
3178 Set_Etype (Formal, Etype (Actual));
3181 elsif Common_Type (T1) = Common_Type (T2) then
3182 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3192 end Compatible_Types_In_Predicate;
3194 ----------------------------
3195 -- Indicate_Name_And_Type --
3196 ----------------------------
3198 procedure Indicate_Name_And_Type is
3200 Add_One_Interp (N, Nam, Etype (Nam));
3201 Check_Implicit_Dereference (N, Etype (Nam));
3204 -- If the prefix of the call is a name, indicate the entity
3205 -- being called. If it is not a name, it is an expression that
3206 -- denotes an access to subprogram or else an entry or family. In
3207 -- the latter case, the name is a selected component, and the entity
3208 -- being called is noted on the selector.
3210 if not Is_Type (Nam) then
3211 if Is_Entity_Name (Name (N)) then
3212 Set_Entity (Name (N), Nam);
3213 Set_Etype (Name (N), Etype (Nam));
3215 elsif Nkind (Name (N)) = N_Selected_Component then
3216 Set_Entity (Selector_Name (Name (N)), Nam);
3220 if Debug_Flag_E and not Report then
3221 Write_Str (" Overloaded call ");
3222 Write_Int (Int (N));
3223 Write_Str (" compatible with ");
3224 Write_Int (Int (Nam));
3227 end Indicate_Name_And_Type;
3229 ------------------------
3230 -- Operator_Hidden_By --
3231 ------------------------
3233 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3234 Act1 : constant Node_Id := First_Actual (N);
3235 Act2 : constant Node_Id := Next_Actual (Act1);
3236 Form1 : constant Entity_Id := First_Formal (Fun);
3237 Form2 : constant Entity_Id := Next_Formal (Form1);
3240 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3243 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3246 elsif Present (Form2) then
3248 or else not Has_Compatible_Type (Act2, Etype (Form2))
3253 elsif Present (Act2) then
3257 -- Now we know that the arity of the operator matches the function,
3258 -- and the function call is a valid interpretation. The function
3259 -- hides the operator if it has the right signature, or if one of
3260 -- its operands is a non-abstract operation on Address when this is
3261 -- a visible integer type.
3263 return Hides_Op (Fun, Nam)
3264 or else Is_Descendant_Of_Address (Etype (Form1))
3267 and then Is_Descendant_Of_Address (Etype (Form2)));
3268 end Operator_Hidden_By;
3270 -- Start of processing for Analyze_One_Call
3275 -- If the subprogram has no formals or if all the formals have defaults,
3276 -- and the return type is an array type, the node may denote an indexing
3277 -- of the result of a parameterless call. In Ada 2005, the subprogram
3278 -- may have one non-defaulted formal, and the call may have been written
3279 -- in prefix notation, so that the rebuilt parameter list has more than
3282 if not Is_Overloadable (Nam)
3283 and then Ekind (Nam) /= E_Subprogram_Type
3284 and then Ekind (Nam) /= E_Entry_Family
3289 -- An indexing requires at least one actual. The name of the call cannot
3290 -- be an implicit indirect call, so it cannot be a generated explicit
3293 if not Is_Empty_List (Actuals)
3295 (Needs_No_Actuals (Nam)
3297 (Needs_One_Actual (Nam)
3298 and then Present (Next_Actual (First (Actuals)))))
3300 if Is_Array_Type (Subp_Type)
3302 (Nkind (Name (N)) /= N_Explicit_Dereference
3303 or else Comes_From_Source (Name (N)))
3305 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3307 elsif Is_Access_Type (Subp_Type)
3308 and then Is_Array_Type (Designated_Type (Subp_Type))
3312 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3314 -- The prefix can also be a parameterless function that returns an
3315 -- access to subprogram, in which case this is an indirect call.
3316 -- If this succeeds, an explicit dereference is added later on,
3317 -- in Analyze_Call or Resolve_Call.
3319 elsif Is_Access_Type (Subp_Type)
3320 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3322 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3327 -- If the call has been transformed into a slice, it is of the form
3328 -- F (Subtype) where F is parameterless. The node has been rewritten in
3329 -- Try_Indexed_Call and there is nothing else to do.
3332 and then Nkind (N) = N_Slice
3338 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3342 -- If an indirect call is a possible interpretation, indicate
3343 -- success to the caller. This may be an indexing of an explicit
3344 -- dereference of a call that returns an access type (see above).
3348 and then Nkind (Name (N)) = N_Explicit_Dereference
3349 and then Comes_From_Source (Name (N)))
3354 -- Mismatch in number or names of parameters
3356 elsif Debug_Flag_E then
3357 Write_Str (" normalization fails in call ");
3358 Write_Int (Int (N));
3359 Write_Str (" with subprogram ");
3360 Write_Int (Int (Nam));
3364 -- If the context expects a function call, discard any interpretation
3365 -- that is a procedure. If the node is not overloaded, leave as is for
3366 -- better error reporting when type mismatch is found.
3368 elsif Nkind (N) = N_Function_Call
3369 and then Is_Overloaded (Name (N))
3370 and then Ekind (Nam) = E_Procedure
3374 -- Ditto for function calls in a procedure context
3376 elsif Nkind (N) = N_Procedure_Call_Statement
3377 and then Is_Overloaded (Name (N))
3378 and then Etype (Nam) /= Standard_Void_Type
3382 elsif No (Actuals) then
3384 -- If Normalize succeeds, then there are default parameters for
3387 Indicate_Name_And_Type;
3389 elsif Ekind (Nam) = E_Operator then
3390 if Nkind (N) = N_Procedure_Call_Statement then
3394 -- This can occur when the prefix of the call is an operator
3395 -- name or an expanded name whose selector is an operator name.
3397 Analyze_Operator_Call (N, Nam);
3399 if Etype (N) /= Prev_T then
3401 -- Check that operator is not hidden by a function interpretation
3403 if Is_Overloaded (Name (N)) then
3409 Get_First_Interp (Name (N), I, It);
3410 while Present (It.Nam) loop
3411 if Operator_Hidden_By (It.Nam) then
3412 Set_Etype (N, Prev_T);
3416 Get_Next_Interp (I, It);
3421 -- If operator matches formals, record its name on the call.
3422 -- If the operator is overloaded, Resolve will select the
3423 -- correct one from the list of interpretations. The call
3424 -- node itself carries the first candidate.
3426 Set_Entity (Name (N), Nam);
3429 elsif Report and then Etype (N) = Any_Type then
3430 Error_Msg_N ("incompatible arguments for operator", N);
3434 -- Normalize_Actuals has chained the named associations in the
3435 -- correct order of the formals.
3437 Actual := First_Actual (N);
3438 Formal := First_Formal (Nam);
3440 -- If we are analyzing a call rewritten from object notation, skip
3441 -- first actual, which may be rewritten later as an explicit
3445 Next_Actual (Actual);
3446 Next_Formal (Formal);
3449 while Present (Actual) and then Present (Formal) loop
3450 if Nkind (Parent (Actual)) /= N_Parameter_Association
3451 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3453 -- The actual can be compatible with the formal, but we must
3454 -- also check that the context is not an address type that is
3455 -- visibly an integer type. In this case the use of literals is
3456 -- illegal, except in the body of descendants of system, where
3457 -- arithmetic operations on address are of course used.
3459 if Has_Compatible_Type (Actual, Etype (Formal))
3461 (Etype (Actual) /= Universal_Integer
3462 or else not Is_Descendant_Of_Address (Etype (Formal))
3464 Is_Predefined_File_Name
3465 (Unit_File_Name (Get_Source_Unit (N))))
3467 Next_Actual (Actual);
3468 Next_Formal (Formal);
3470 -- In Allow_Integer_Address mode, we allow an actual integer to
3471 -- match a formal address type and vice versa. We only do this
3472 -- if we are certain that an error will otherwise be issued
3474 elsif Address_Integer_Convert_OK
3475 (Etype (Actual), Etype (Formal))
3476 and then (Report and not Is_Indexed and not Is_Indirect)
3478 -- Handle this case by introducing an unchecked conversion
3481 Unchecked_Convert_To (Etype (Formal),
3482 Relocate_Node (Actual)));
3483 Analyze_And_Resolve (Actual, Etype (Formal));
3484 Next_Actual (Actual);
3485 Next_Formal (Formal);
3487 -- Under relaxed RM semantics silently replace occurrences of
3488 -- null by System.Address_Null. We only do this if we know that
3489 -- an error will otherwise be issued.
3491 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3492 and then (Report and not Is_Indexed and not Is_Indirect)
3494 Replace_Null_By_Null_Address (Actual);
3495 Analyze_And_Resolve (Actual, Etype (Formal));
3496 Next_Actual (Actual);
3497 Next_Formal (Formal);
3499 elsif Compatible_Types_In_Predicate
3500 (Etype (Formal), Etype (Actual))
3502 Next_Actual (Actual);
3503 Next_Formal (Formal);
3505 -- In a complex case where an enclosing generic and a nested
3506 -- generic package, both declared with partially parameterized
3507 -- formal subprograms with the same names, are instantiated
3508 -- with the same type, the types of the actual parameter and
3509 -- that of the formal may appear incompatible at first sight.
3512 -- type Outer_T is private;
3513 -- with function Func (Formal : Outer_T)
3514 -- return ... is <>;
3516 -- package Outer_Gen is
3518 -- type Inner_T is private;
3519 -- with function Func (Formal : Inner_T) -- (1)
3520 -- return ... is <>;
3522 -- package Inner_Gen is
3523 -- function Inner_Func (Formal : Inner_T) -- (2)
3524 -- return ... is (Func (Formal));
3526 -- end Outer_Generic;
3528 -- package Outer_Inst is new Outer_Gen (Actual_T);
3529 -- package Inner_Inst is new Outer_Inst.Inner_Gen (Actual_T);
3531 -- In the example above, the type of parameter
3532 -- Inner_Func.Formal at (2) is incompatible with the type of
3533 -- Func.Formal at (1) in the context of instantiations
3534 -- Outer_Inst and Inner_Inst. In reality both types are generic
3535 -- actual subtypes renaming base type Actual_T as part of the
3536 -- generic prologues for the instantiations.
3538 -- Recognize this case and add a type conversion to allow this
3539 -- kind of generic actual subtype conformance. Note that this
3540 -- is done only when the call is non-overloaded because the
3541 -- resolution mechanism already has the means to disambiguate
3544 elsif not Is_Overloaded (Name (N))
3545 and then Is_Type (Etype (Actual))
3546 and then Is_Type (Etype (Formal))
3547 and then Is_Generic_Actual_Type (Etype (Actual))
3548 and then Is_Generic_Actual_Type (Etype (Formal))
3549 and then Base_Type (Etype (Actual)) =
3550 Base_Type (Etype (Formal))
3553 Convert_To (Etype (Formal), Relocate_Node (Actual)));
3554 Analyze_And_Resolve (Actual, Etype (Formal));
3555 Next_Actual (Actual);
3556 Next_Formal (Formal);
3558 -- Handle failed type check
3561 if Debug_Flag_E then
3562 Write_Str (" type checking fails in call ");
3563 Write_Int (Int (N));
3564 Write_Str (" with formal ");
3565 Write_Int (Int (Formal));
3566 Write_Str (" in subprogram ");
3567 Write_Int (Int (Nam));
3571 -- Comment needed on the following test???
3573 if Report and not Is_Indexed and not Is_Indirect then
3575 -- Ada 2005 (AI-251): Complete the error notification
3576 -- to help new Ada 2005 users.
3578 if Is_Class_Wide_Type (Etype (Formal))
3579 and then Is_Interface (Etype (Etype (Formal)))
3580 and then not Interface_Present_In_Ancestor
3581 (Typ => Etype (Actual),
3582 Iface => Etype (Etype (Formal)))
3585 ("(Ada 2005) does not implement interface }",
3586 Actual, Etype (Etype (Formal)));
3589 Wrong_Type (Actual, Etype (Formal));
3591 if Nkind (Actual) = N_Op_Eq
3592 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3594 Formal := First_Formal (Nam);
3595 while Present (Formal) loop
3596 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3597 Error_Msg_N -- CODEFIX
3598 ("possible misspelling of `='>`!", Actual);
3602 Next_Formal (Formal);
3606 if All_Errors_Mode then
3607 Error_Msg_Sloc := Sloc (Nam);
3609 if Etype (Formal) = Any_Type then
3611 ("there is no legal actual parameter", Actual);
3614 if Is_Overloadable (Nam)
3615 and then Present (Alias (Nam))
3616 and then not Comes_From_Source (Nam)
3619 ("\\ =='> in call to inherited operation & #!",
3622 elsif Ekind (Nam) = E_Subprogram_Type then
3624 Access_To_Subprogram_Typ :
3625 constant Entity_Id :=
3627 (Associated_Node_For_Itype (Nam));
3630 ("\\ =='> in call to dereference of &#!",
3631 Actual, Access_To_Subprogram_Typ);
3636 ("\\ =='> in call to &#!", Actual, Nam);
3646 -- Normalize_Actuals has verified that a default value exists
3647 -- for this formal. Current actual names a subsequent formal.
3649 Next_Formal (Formal);
3653 -- On exit, all actuals match
3655 Indicate_Name_And_Type;
3657 end Analyze_One_Call;
3659 ---------------------------
3660 -- Analyze_Operator_Call --
3661 ---------------------------
3663 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3664 Op_Name : constant Name_Id := Chars (Op_Id);
3665 Act1 : constant Node_Id := First_Actual (N);
3666 Act2 : constant Node_Id := Next_Actual (Act1);
3669 -- Binary operator case
3671 if Present (Act2) then
3673 -- If more than two operands, then not binary operator after all
3675 if Present (Next_Actual (Act2)) then
3679 -- Otherwise action depends on operator
3690 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3696 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3703 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3708 Find_Equality_Types (Act1, Act2, Op_Id, N);
3710 when Name_Op_Concat =>
3711 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3713 -- Is this when others, or should it be an abort???
3719 -- Unary operator case
3727 Find_Unary_Types (Act1, Op_Id, N);
3730 Find_Negation_Types (Act1, Op_Id, N);
3732 -- Is this when others correct, or should it be an abort???
3738 end Analyze_Operator_Call;
3740 -------------------------------------------
3741 -- Analyze_Overloaded_Selected_Component --
3742 -------------------------------------------
3744 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3745 Nam : constant Node_Id := Prefix (N);
3746 Sel : constant Node_Id := Selector_Name (N);
3753 Set_Etype (Sel, Any_Type);
3755 Get_First_Interp (Nam, I, It);
3756 while Present (It.Typ) loop
3757 if Is_Access_Type (It.Typ) then
3758 T := Designated_Type (It.Typ);
3759 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3764 -- Locate the component. For a private prefix the selector can denote
3767 if Is_Record_Type (T) or else Is_Private_Type (T) then
3769 -- If the prefix is a class-wide type, the visible components are
3770 -- those of the base type.
3772 if Is_Class_Wide_Type (T) then
3776 Comp := First_Entity (T);
3777 while Present (Comp) loop
3778 if Chars (Comp) = Chars (Sel)
3779 and then Is_Visible_Component (Comp)
3782 -- AI05-105: if the context is an object renaming with
3783 -- an anonymous access type, the expected type of the
3784 -- object must be anonymous. This is a name resolution rule.
3786 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3787 or else No (Access_Definition (Parent (N)))
3788 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3790 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3792 Set_Entity (Sel, Comp);
3793 Set_Etype (Sel, Etype (Comp));
3794 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3795 Check_Implicit_Dereference (N, Etype (Comp));
3797 -- This also specifies a candidate to resolve the name.
3798 -- Further overloading will be resolved from context.
3799 -- The selector name itself does not carry overloading
3802 Set_Etype (Nam, It.Typ);
3805 -- Named access type in the context of a renaming
3806 -- declaration with an access definition. Remove
3807 -- inapplicable candidate.
3816 elsif Is_Concurrent_Type (T) then
3817 Comp := First_Entity (T);
3818 while Present (Comp)
3819 and then Comp /= First_Private_Entity (T)
3821 if Chars (Comp) = Chars (Sel) then
3822 if Is_Overloadable (Comp) then
3823 Add_One_Interp (Sel, Comp, Etype (Comp));
3825 Set_Entity_With_Checks (Sel, Comp);
3826 Generate_Reference (Comp, Sel);
3829 Set_Etype (Sel, Etype (Comp));
3830 Set_Etype (N, Etype (Comp));
3831 Set_Etype (Nam, It.Typ);
3833 -- For access type case, introduce explicit dereference for
3834 -- more uniform treatment of entry calls. Do this only once
3835 -- if several interpretations yield an access type.
3837 if Is_Access_Type (Etype (Nam))
3838 and then Nkind (Nam) /= N_Explicit_Dereference
3840 Insert_Explicit_Dereference (Nam);
3842 (Warn_On_Dereference, "?d?implicit dereference", N);
3849 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3852 Get_Next_Interp (I, It);
3855 if Etype (N) = Any_Type
3856 and then not Try_Object_Operation (N)
3858 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3859 Set_Entity (Sel, Any_Id);
3860 Set_Etype (Sel, Any_Type);
3862 end Analyze_Overloaded_Selected_Component;
3864 ----------------------------------
3865 -- Analyze_Qualified_Expression --
3866 ----------------------------------
3868 procedure Analyze_Qualified_Expression (N : Node_Id) is
3869 Mark : constant Entity_Id := Subtype_Mark (N);
3870 Expr : constant Node_Id := Expression (N);
3876 Analyze_Expression (Expr);
3878 Set_Etype (N, Any_Type);
3883 if T = Any_Type then
3887 Check_Fully_Declared (T, N);
3889 -- If expected type is class-wide, check for exact match before
3890 -- expansion, because if the expression is a dispatching call it
3891 -- may be rewritten as explicit dereference with class-wide result.
3892 -- If expression is overloaded, retain only interpretations that
3893 -- will yield exact matches.
3895 if Is_Class_Wide_Type (T) then
3896 if not Is_Overloaded (Expr) then
3897 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3898 if Nkind (Expr) = N_Aggregate then
3899 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3901 Wrong_Type (Expr, T);
3906 Get_First_Interp (Expr, I, It);
3908 while Present (It.Nam) loop
3909 if Base_Type (It.Typ) /= Base_Type (T) then
3913 Get_Next_Interp (I, It);
3919 end Analyze_Qualified_Expression;
3921 -----------------------------------
3922 -- Analyze_Quantified_Expression --
3923 -----------------------------------
3925 procedure Analyze_Quantified_Expression (N : Node_Id) is
3926 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3927 -- If the iterator is part of a quantified expression, and the range is
3928 -- known to be statically empty, emit a warning and replace expression
3929 -- with its static value. Returns True if the replacement occurs.
3931 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3932 -- Determine whether if expression If_Expr lacks an else part or if it
3933 -- has one, it evaluates to True.
3935 --------------------
3936 -- Is_Empty_Range --
3937 --------------------
3939 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
3940 Loc : constant Source_Ptr := Sloc (N);
3943 if Is_Array_Type (Typ)
3944 and then Compile_Time_Known_Bounds (Typ)
3946 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
3947 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
3949 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3951 if All_Present (N) then
3953 ("??quantified expression with ALL "
3954 & "over a null range has value True", N);
3955 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3959 ("??quantified expression with SOME "
3960 & "over a null range has value False", N);
3961 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3972 -----------------------------
3973 -- No_Else_Or_Trivial_True --
3974 -----------------------------
3976 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
3977 Else_Expr : constant Node_Id :=
3978 Next (Next (First (Expressions (If_Expr))));
3982 or else (Compile_Time_Known_Value (Else_Expr)
3983 and then Is_True (Expr_Value (Else_Expr)));
3984 end No_Else_Or_Trivial_True;
3988 Cond : constant Node_Id := Condition (N);
3989 Loop_Id : Entity_Id;
3990 QE_Scop : Entity_Id;
3992 -- Start of processing for Analyze_Quantified_Expression
3995 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
3997 -- Create a scope to emulate the loop-like behavior of the quantified
3998 -- expression. The scope is needed to provide proper visibility of the
4001 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
4002 Set_Etype (QE_Scop, Standard_Void_Type);
4003 Set_Scope (QE_Scop, Current_Scope);
4004 Set_Parent (QE_Scop, N);
4006 Push_Scope (QE_Scop);
4008 -- All constituents are preanalyzed and resolved to avoid untimely
4009 -- generation of various temporaries and types. Full analysis and
4010 -- expansion is carried out when the quantified expression is
4011 -- transformed into an expression with actions.
4013 if Present (Iterator_Specification (N)) then
4014 Preanalyze (Iterator_Specification (N));
4016 -- Do not proceed with the analysis when the range of iteration is
4017 -- empty. The appropriate error is issued by Is_Empty_Range.
4019 if Is_Entity_Name (Name (Iterator_Specification (N)))
4020 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4025 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4027 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4030 Preanalyze (Loop_Par);
4032 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4033 and then Parent (Loop_Par) /= N
4035 -- The parser cannot distinguish between a loop specification
4036 -- and an iterator specification. If after pre-analysis the
4037 -- proper form has been recognized, rewrite the expression to
4038 -- reflect the right kind. This is needed for proper ASIS
4039 -- navigation. If expansion is enabled, the transformation is
4040 -- performed when the expression is rewritten as a loop.
4042 Set_Iterator_Specification (N,
4043 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4045 Set_Defining_Identifier (Iterator_Specification (N),
4046 Relocate_Node (Defining_Identifier (Loop_Par)));
4047 Set_Name (Iterator_Specification (N),
4048 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4049 Set_Comes_From_Source (Iterator_Specification (N),
4050 Comes_From_Source (Loop_Parameter_Specification (N)));
4051 Set_Loop_Parameter_Specification (N, Empty);
4056 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4059 Set_Etype (N, Standard_Boolean);
4061 -- Verify that the loop variable is used within the condition of the
4062 -- quantified expression.
4064 if Present (Iterator_Specification (N)) then
4065 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4067 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4070 if Warn_On_Suspicious_Contract
4071 and then not Referenced (Loop_Id, Cond)
4073 -- Generating C, this check causes spurious warnings on inlined
4074 -- postconditions; we can safely disable it because this check
4075 -- was previously performed when analyzing the internally built
4076 -- postconditions procedure.
4078 if Modify_Tree_For_C and then In_Inlined_Body then
4081 Error_Msg_N ("?T?unused variable &", Loop_Id);
4085 -- Diagnose a possible misuse of the SOME existential quantifier. When
4086 -- we have a quantified expression of the form:
4088 -- for some X => (if P then Q [else True])
4090 -- any value for X that makes P False results in the if expression being
4091 -- trivially True, and so also results in the quantified expression
4092 -- being trivially True.
4094 if Warn_On_Suspicious_Contract
4095 and then not All_Present (N)
4096 and then Nkind (Cond) = N_If_Expression
4097 and then No_Else_Or_Trivial_True (Cond)
4099 Error_Msg_N ("?T?suspicious expression", N);
4100 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4101 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4103 end Analyze_Quantified_Expression;
4109 procedure Analyze_Range (N : Node_Id) is
4110 L : constant Node_Id := Low_Bound (N);
4111 H : constant Node_Id := High_Bound (N);
4112 I1, I2 : Interp_Index;
4115 procedure Check_Common_Type (T1, T2 : Entity_Id);
4116 -- Verify the compatibility of two types, and choose the
4117 -- non universal one if the other is universal.
4119 procedure Check_High_Bound (T : Entity_Id);
4120 -- Test one interpretation of the low bound against all those
4121 -- of the high bound.
4123 procedure Check_Universal_Expression (N : Node_Id);
4124 -- In Ada 83, reject bounds of a universal range that are not literals
4127 -----------------------
4128 -- Check_Common_Type --
4129 -----------------------
4131 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4133 if Covers (T1 => T1, T2 => T2)
4135 Covers (T1 => T2, T2 => T1)
4137 if T1 = Universal_Integer
4138 or else T1 = Universal_Real
4139 or else T1 = Any_Character
4141 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4144 Add_One_Interp (N, T1, T1);
4147 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4150 end Check_Common_Type;
4152 ----------------------
4153 -- Check_High_Bound --
4154 ----------------------
4156 procedure Check_High_Bound (T : Entity_Id) is
4158 if not Is_Overloaded (H) then
4159 Check_Common_Type (T, Etype (H));
4161 Get_First_Interp (H, I2, It2);
4162 while Present (It2.Typ) loop
4163 Check_Common_Type (T, It2.Typ);
4164 Get_Next_Interp (I2, It2);
4167 end Check_High_Bound;
4169 -----------------------------
4170 -- Is_Universal_Expression --
4171 -----------------------------
4173 procedure Check_Universal_Expression (N : Node_Id) is
4175 if Etype (N) = Universal_Integer
4176 and then Nkind (N) /= N_Integer_Literal
4177 and then not Is_Entity_Name (N)
4178 and then Nkind (N) /= N_Attribute_Reference
4180 Error_Msg_N ("illegal bound in discrete range", N);
4182 end Check_Universal_Expression;
4184 -- Start of processing for Analyze_Range
4187 Set_Etype (N, Any_Type);
4188 Analyze_Expression (L);
4189 Analyze_Expression (H);
4191 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4195 if not Is_Overloaded (L) then
4196 Check_High_Bound (Etype (L));
4198 Get_First_Interp (L, I1, It1);
4199 while Present (It1.Typ) loop
4200 Check_High_Bound (It1.Typ);
4201 Get_Next_Interp (I1, It1);
4205 -- If result is Any_Type, then we did not find a compatible pair
4207 if Etype (N) = Any_Type then
4208 Error_Msg_N ("incompatible types in range ", N);
4212 if Ada_Version = Ada_83
4214 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4215 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4217 Check_Universal_Expression (L);
4218 Check_Universal_Expression (H);
4221 Check_Function_Writable_Actuals (N);
4224 -----------------------
4225 -- Analyze_Reference --
4226 -----------------------
4228 procedure Analyze_Reference (N : Node_Id) is
4229 P : constant Node_Id := Prefix (N);
4232 Acc_Type : Entity_Id;
4237 -- An interesting error check, if we take the 'Ref of an object for
4238 -- which a pragma Atomic or Volatile has been given, and the type of the
4239 -- object is not Atomic or Volatile, then we are in trouble. The problem
4240 -- is that no trace of the atomic/volatile status will remain for the
4241 -- backend to respect when it deals with the resulting pointer, since
4242 -- the pointer type will not be marked atomic (it is a pointer to the
4243 -- base type of the object).
4245 -- It is not clear if that can ever occur, but in case it does, we will
4246 -- generate an error message. Not clear if this message can ever be
4247 -- generated, and pretty clear that it represents a bug if it is, still
4248 -- seems worth checking, except in CodePeer mode where we do not really
4249 -- care and don't want to bother the user.
4253 if Is_Entity_Name (P)
4254 and then Is_Object_Reference (P)
4255 and then not CodePeer_Mode
4260 if (Has_Atomic_Components (E)
4261 and then not Has_Atomic_Components (T))
4263 (Has_Volatile_Components (E)
4264 and then not Has_Volatile_Components (T))
4265 or else (Is_Atomic (E) and then not Is_Atomic (T))
4266 or else (Is_Volatile (E) and then not Is_Volatile (T))
4268 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4272 -- Carry on with normal processing
4274 Acc_Type := Create_Itype (E_Allocator_Type, N);
4275 Set_Etype (Acc_Type, Acc_Type);
4276 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4277 Set_Etype (N, Acc_Type);
4278 end Analyze_Reference;
4280 --------------------------------
4281 -- Analyze_Selected_Component --
4282 --------------------------------
4284 -- Prefix is a record type or a task or protected type. In the latter case,
4285 -- the selector must denote a visible entry.
4287 procedure Analyze_Selected_Component (N : Node_Id) is
4288 Name : constant Node_Id := Prefix (N);
4289 Sel : constant Node_Id := Selector_Name (N);
4292 Has_Candidate : Boolean := False;
4295 Pent : Entity_Id := Empty;
4296 Prefix_Type : Entity_Id;
4298 Type_To_Use : Entity_Id;
4299 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4300 -- a class-wide type, we use its root type, whose components are
4301 -- present in the class-wide type.
4303 Is_Single_Concurrent_Object : Boolean;
4304 -- Set True if the prefix is a single task or a single protected object
4306 procedure Find_Component_In_Instance (Rec : Entity_Id);
4307 -- In an instance, a component of a private extension may not be visible
4308 -- while it was visible in the generic. Search candidate scope for a
4309 -- component with the proper identifier. This is only done if all other
4310 -- searches have failed. If a match is found, the Etype of both N and
4311 -- Sel are set from this component, and the entity of Sel is set to
4312 -- reference this component. If no match is found, Entity (Sel) remains
4313 -- unset. For a derived type that is an actual of the instance, the
4314 -- desired component may be found in any ancestor.
4316 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4317 -- It is known that the parent of N denotes a subprogram call. Comp
4318 -- is an overloadable component of the concurrent type of the prefix.
4319 -- Determine whether all formals of the parent of N and Comp are mode
4320 -- conformant. If the parent node is not analyzed yet it may be an
4321 -- indexed component rather than a function call.
4323 function Has_Dereference (Nod : Node_Id) return Boolean;
4324 -- Check whether prefix includes a dereference at any level.
4326 --------------------------------
4327 -- Find_Component_In_Instance --
4328 --------------------------------
4330 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4336 while Present (Typ) loop
4337 Comp := First_Component (Typ);
4338 while Present (Comp) loop
4339 if Chars (Comp) = Chars (Sel) then
4340 Set_Entity_With_Checks (Sel, Comp);
4341 Set_Etype (Sel, Etype (Comp));
4342 Set_Etype (N, Etype (Comp));
4346 Next_Component (Comp);
4349 -- If not found, the component may be declared in the parent
4350 -- type or its full view, if any.
4352 if Is_Derived_Type (Typ) then
4355 if Is_Private_Type (Typ) then
4356 Typ := Full_View (Typ);
4364 -- If we fall through, no match, so no changes made
4367 end Find_Component_In_Instance;
4369 ------------------------------
4370 -- Has_Mode_Conformant_Spec --
4371 ------------------------------
4373 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4374 Comp_Param : Entity_Id;
4376 Param_Typ : Entity_Id;
4379 Comp_Param := First_Formal (Comp);
4381 if Nkind (Parent (N)) = N_Indexed_Component then
4382 Param := First (Expressions (Parent (N)));
4384 Param := First (Parameter_Associations (Parent (N)));
4387 while Present (Comp_Param)
4388 and then Present (Param)
4390 Param_Typ := Find_Parameter_Type (Param);
4392 if Present (Param_Typ)
4394 not Conforming_Types
4395 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4400 Next_Formal (Comp_Param);
4404 -- One of the specs has additional formals; there is no match, unless
4405 -- this may be an indexing of a parameterless call.
4407 -- Note that when expansion is disabled, the corresponding record
4408 -- type of synchronized types is not constructed, so that there is
4409 -- no point is attempting an interpretation as a prefixed call, as
4410 -- this is bound to fail because the primitive operations will not
4411 -- be properly located.
4413 if Present (Comp_Param) or else Present (Param) then
4414 if Needs_No_Actuals (Comp)
4415 and then Is_Array_Type (Etype (Comp))
4416 and then not Expander_Active
4425 end Has_Mode_Conformant_Spec;
4427 ---------------------
4428 -- Has_Dereference --
4429 ---------------------
4431 function Has_Dereference (Nod : Node_Id) return Boolean is
4433 if Nkind (Nod) = N_Explicit_Dereference then
4436 -- When expansion is disabled an explicit dereference may not have
4437 -- been inserted, but if this is an access type the indirection makes
4440 elsif Is_Access_Type (Etype (Nod)) then
4443 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4444 return Has_Dereference (Prefix (Nod));
4449 end Has_Dereference;
4451 -- Start of processing for Analyze_Selected_Component
4454 Set_Etype (N, Any_Type);
4456 if Is_Overloaded (Name) then
4457 Analyze_Overloaded_Selected_Component (N);
4460 elsif Etype (Name) = Any_Type then
4461 Set_Entity (Sel, Any_Id);
4462 Set_Etype (Sel, Any_Type);
4466 Prefix_Type := Etype (Name);
4469 if Is_Access_Type (Prefix_Type) then
4471 -- A RACW object can never be used as prefix of a selected component
4472 -- since that means it is dereferenced without being a controlling
4473 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4474 -- reporting an error, we must check whether this is actually a
4475 -- dispatching call in prefix form.
4477 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4478 and then Comes_From_Source (N)
4480 if Try_Object_Operation (N) then
4484 ("invalid dereference of a remote access-to-class-wide value",
4488 -- Normal case of selected component applied to access type
4491 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4493 if Is_Entity_Name (Name) then
4494 Pent := Entity (Name);
4495 elsif Nkind (Name) = N_Selected_Component
4496 and then Is_Entity_Name (Selector_Name (Name))
4498 Pent := Entity (Selector_Name (Name));
4501 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4504 -- If we have an explicit dereference of a remote access-to-class-wide
4505 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4506 -- have to check for the case of a prefix that is a controlling operand
4507 -- of a prefixed dispatching call, as the dereference is legal in that
4508 -- case. Normally this condition is checked in Validate_Remote_Access_
4509 -- To_Class_Wide_Type, but we have to defer the checking for selected
4510 -- component prefixes because of the prefixed dispatching call case.
4511 -- Note that implicit dereferences are checked for this just above.
4513 elsif Nkind (Name) = N_Explicit_Dereference
4514 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4515 and then Comes_From_Source (N)
4517 if Try_Object_Operation (N) then
4521 ("invalid dereference of a remote access-to-class-wide value",
4526 -- (Ada 2005): if the prefix is the limited view of a type, and
4527 -- the context already includes the full view, use the full view
4528 -- in what follows, either to retrieve a component of to find
4529 -- a primitive operation. If the prefix is an explicit dereference,
4530 -- set the type of the prefix to reflect this transformation.
4531 -- If the non-limited view is itself an incomplete type, get the
4532 -- full view if available.
4534 if From_Limited_With (Prefix_Type)
4535 and then Has_Non_Limited_View (Prefix_Type)
4537 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4539 if Nkind (N) = N_Explicit_Dereference then
4540 Set_Etype (Prefix (N), Prefix_Type);
4544 if Ekind (Prefix_Type) = E_Private_Subtype then
4545 Prefix_Type := Base_Type (Prefix_Type);
4548 Type_To_Use := Prefix_Type;
4550 -- For class-wide types, use the entity list of the root type. This
4551 -- indirection is specially important for private extensions because
4552 -- only the root type get switched (not the class-wide type).
4554 if Is_Class_Wide_Type (Prefix_Type) then
4555 Type_To_Use := Root_Type (Prefix_Type);
4558 -- If the prefix is a single concurrent object, use its name in error
4559 -- messages, rather than that of its anonymous type.
4561 Is_Single_Concurrent_Object :=
4562 Is_Concurrent_Type (Prefix_Type)
4563 and then Is_Internal_Name (Chars (Prefix_Type))
4564 and then not Is_Derived_Type (Prefix_Type)
4565 and then Is_Entity_Name (Name);
4567 Comp := First_Entity (Type_To_Use);
4569 -- If the selector has an original discriminant, the node appears in
4570 -- an instance. Replace the discriminant with the corresponding one
4571 -- in the current discriminated type. For nested generics, this must
4572 -- be done transitively, so note the new original discriminant.
4574 if Nkind (Sel) = N_Identifier
4575 and then In_Instance
4576 and then Present (Original_Discriminant (Sel))
4578 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4580 -- Mark entity before rewriting, for completeness and because
4581 -- subsequent semantic checks might examine the original node.
4583 Set_Entity (Sel, Comp);
4584 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4585 Set_Original_Discriminant (Selector_Name (N), Comp);
4586 Set_Etype (N, Etype (Comp));
4587 Check_Implicit_Dereference (N, Etype (Comp));
4589 if Is_Access_Type (Etype (Name)) then
4590 Insert_Explicit_Dereference (Name);
4591 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4594 elsif Is_Record_Type (Prefix_Type) then
4596 -- Find component with given name. In an instance, if the node is
4597 -- known as a prefixed call, do not examine components whose
4598 -- visibility may be accidental.
4600 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4601 if Chars (Comp) = Chars (Sel)
4602 and then Is_Visible_Component (Comp, N)
4604 Set_Entity_With_Checks (Sel, Comp);
4605 Set_Etype (Sel, Etype (Comp));
4607 if Ekind (Comp) = E_Discriminant then
4608 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4610 ("cannot reference discriminant of unchecked union",
4614 if Is_Generic_Type (Prefix_Type)
4616 Is_Generic_Type (Root_Type (Prefix_Type))
4618 Set_Original_Discriminant (Sel, Comp);
4622 -- Resolve the prefix early otherwise it is not possible to
4623 -- build the actual subtype of the component: it may need
4624 -- to duplicate this prefix and duplication is only allowed
4625 -- on fully resolved expressions.
4629 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4630 -- subtypes in a package specification.
4633 -- limited with Pkg;
4635 -- type Acc_Inc is access Pkg.T;
4637 -- N : Natural := X.all.Comp; -- ERROR, limited view
4638 -- end Pkg; -- Comp is not visible
4640 if Nkind (Name) = N_Explicit_Dereference
4641 and then From_Limited_With (Etype (Prefix (Name)))
4642 and then not Is_Potentially_Use_Visible (Etype (Name))
4643 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4644 N_Package_Specification
4647 ("premature usage of incomplete}", Prefix (Name),
4648 Etype (Prefix (Name)));
4651 -- We never need an actual subtype for the case of a selection
4652 -- for a indexed component of a non-packed array, since in
4653 -- this case gigi generates all the checks and can find the
4654 -- necessary bounds information.
4656 -- We also do not need an actual subtype for the case of a
4657 -- first, last, length, or range attribute applied to a
4658 -- non-packed array, since gigi can again get the bounds in
4659 -- these cases (gigi cannot handle the packed case, since it
4660 -- has the bounds of the packed array type, not the original
4661 -- bounds of the type). However, if the prefix is itself a
4662 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4663 -- as a dynamic-sized temporary, so we do generate an actual
4664 -- subtype for this case.
4666 Parent_N := Parent (N);
4668 if not Is_Packed (Etype (Comp))
4670 ((Nkind (Parent_N) = N_Indexed_Component
4671 and then Nkind (Name) /= N_Selected_Component)
4673 (Nkind (Parent_N) = N_Attribute_Reference
4675 Nam_In (Attribute_Name (Parent_N), Name_First,
4680 Set_Etype (N, Etype (Comp));
4682 -- If full analysis is not enabled, we do not generate an
4683 -- actual subtype, because in the absence of expansion
4684 -- reference to a formal of a protected type, for example,
4685 -- will not be properly transformed, and will lead to
4686 -- out-of-scope references in gigi.
4688 -- In all other cases, we currently build an actual subtype.
4689 -- It seems likely that many of these cases can be avoided,
4690 -- but right now, the front end makes direct references to the
4691 -- bounds (e.g. in generating a length check), and if we do
4692 -- not make an actual subtype, we end up getting a direct
4693 -- reference to a discriminant, which will not do.
4695 elsif Full_Analysis then
4697 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4698 Insert_Action (N, Act_Decl);
4700 if No (Act_Decl) then
4701 Set_Etype (N, Etype (Comp));
4704 -- Component type depends on discriminants. Enter the
4705 -- main attributes of the subtype.
4708 Subt : constant Entity_Id :=
4709 Defining_Identifier (Act_Decl);
4712 Set_Etype (Subt, Base_Type (Etype (Comp)));
4713 Set_Ekind (Subt, Ekind (Etype (Comp)));
4714 Set_Etype (N, Subt);
4718 -- If Full_Analysis not enabled, just set the Etype
4721 Set_Etype (N, Etype (Comp));
4724 Check_Implicit_Dereference (N, Etype (N));
4728 -- If the prefix is a private extension, check only the visible
4729 -- components of the partial view. This must include the tag,
4730 -- which can appear in expanded code in a tag check.
4732 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4733 and then Chars (Selector_Name (N)) /= Name_uTag
4735 exit when Comp = Last_Entity (Type_To_Use);
4741 -- Ada 2005 (AI-252): The selected component can be interpreted as
4742 -- a prefixed view of a subprogram. Depending on the context, this is
4743 -- either a name that can appear in a renaming declaration, or part
4744 -- of an enclosing call given in prefix form.
4746 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4747 -- selected component should resolve to a name.
4749 if Ada_Version >= Ada_2005
4750 and then Is_Tagged_Type (Prefix_Type)
4751 and then not Is_Concurrent_Type (Prefix_Type)
4753 if Nkind (Parent (N)) = N_Generic_Association
4754 or else Nkind (Parent (N)) = N_Requeue_Statement
4755 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4757 if Find_Primitive_Operation (N) then
4761 elsif Try_Object_Operation (N) then
4765 -- If the transformation fails, it will be necessary to redo the
4766 -- analysis with all errors enabled, to indicate candidate
4767 -- interpretations and reasons for each failure ???
4771 elsif Is_Private_Type (Prefix_Type) then
4773 -- Allow access only to discriminants of the type. If the type has
4774 -- no full view, gigi uses the parent type for the components, so we
4775 -- do the same here.
4777 if No (Full_View (Prefix_Type)) then
4778 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4779 Comp := First_Entity (Type_To_Use);
4782 while Present (Comp) loop
4783 if Chars (Comp) = Chars (Sel) then
4784 if Ekind (Comp) = E_Discriminant then
4785 Set_Entity_With_Checks (Sel, Comp);
4786 Generate_Reference (Comp, Sel);
4788 Set_Etype (Sel, Etype (Comp));
4789 Set_Etype (N, Etype (Comp));
4790 Check_Implicit_Dereference (N, Etype (N));
4792 if Is_Generic_Type (Prefix_Type)
4793 or else Is_Generic_Type (Root_Type (Prefix_Type))
4795 Set_Original_Discriminant (Sel, Comp);
4798 -- Before declaring an error, check whether this is tagged
4799 -- private type and a call to a primitive operation.
4801 elsif Ada_Version >= Ada_2005
4802 and then Is_Tagged_Type (Prefix_Type)
4803 and then Try_Object_Operation (N)
4808 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4809 Error_Msg_NE ("invisible selector& for }", N, Sel);
4810 Set_Entity (Sel, Any_Id);
4811 Set_Etype (N, Any_Type);
4820 elsif Is_Concurrent_Type (Prefix_Type) then
4822 -- Find visible operation with given name. For a protected type,
4823 -- the possible candidates are discriminants, entries or protected
4824 -- procedures. For a task type, the set can only include entries or
4825 -- discriminants if the task type is not an enclosing scope. If it
4826 -- is an enclosing scope (e.g. in an inner task) then all entities
4827 -- are visible, but the prefix must denote the enclosing scope, i.e.
4828 -- can only be a direct name or an expanded name.
4830 Set_Etype (Sel, Any_Type);
4831 In_Scope := In_Open_Scopes (Prefix_Type);
4833 while Present (Comp) loop
4835 -- Do not examine private operations of the type if not within
4838 if Chars (Comp) = Chars (Sel) then
4839 if Is_Overloadable (Comp)
4841 or else Comp /= First_Private_Entity (Type_To_Use))
4843 Add_One_Interp (Sel, Comp, Etype (Comp));
4845 -- If the prefix is tagged, the correct interpretation may
4846 -- lie in the primitive or class-wide operations of the
4847 -- type. Perform a simple conformance check to determine
4848 -- whether Try_Object_Operation should be invoked even if
4849 -- a visible entity is found.
4851 if Is_Tagged_Type (Prefix_Type)
4852 and then Nkind_In (Parent (N), N_Function_Call,
4853 N_Indexed_Component,
4854 N_Procedure_Call_Statement)
4855 and then Has_Mode_Conformant_Spec (Comp)
4857 Has_Candidate := True;
4860 -- Note: a selected component may not denote a component of a
4861 -- protected type (4.1.3(7)).
4863 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4865 and then not Is_Protected_Type (Prefix_Type)
4866 and then Is_Entity_Name (Name))
4868 Set_Entity_With_Checks (Sel, Comp);
4869 Generate_Reference (Comp, Sel);
4871 -- The selector is not overloadable, so we have a candidate
4874 Has_Candidate := True;
4880 Set_Etype (Sel, Etype (Comp));
4881 Set_Etype (N, Etype (Comp));
4883 if Ekind (Comp) = E_Discriminant then
4884 Set_Original_Discriminant (Sel, Comp);
4887 -- For access type case, introduce explicit dereference for
4888 -- more uniform treatment of entry calls.
4890 if Is_Access_Type (Etype (Name)) then
4891 Insert_Explicit_Dereference (Name);
4893 (Warn_On_Dereference, "?d?implicit dereference", N);
4899 exit when not In_Scope
4901 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4904 -- If the scope is a current instance, the prefix cannot be an
4905 -- expression of the same type, unless the selector designates a
4906 -- public operation (otherwise that would represent an attempt to
4907 -- reach an internal entity of another synchronized object).
4908 -- This is legal if prefix is an access to such type and there is
4909 -- a dereference, or is a component with a dereferenced prefix.
4910 -- It is also legal if the prefix is a component of a task type,
4911 -- and the selector is one of the task operations.
4914 and then not Is_Entity_Name (Name)
4915 and then not Has_Dereference (Name)
4917 if Is_Task_Type (Prefix_Type)
4918 and then Present (Entity (Sel))
4919 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
4925 ("invalid reference to internal operation of some object of "
4926 & "type &", N, Type_To_Use);
4927 Set_Entity (Sel, Any_Id);
4928 Set_Etype (Sel, Any_Type);
4933 -- If there is no visible entity with the given name or none of the
4934 -- visible entities are plausible interpretations, check whether
4935 -- there is some other primitive operation with that name.
4937 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
4938 if (Etype (N) = Any_Type
4939 or else not Has_Candidate)
4940 and then Try_Object_Operation (N)
4944 -- If the context is not syntactically a procedure call, it
4945 -- may be a call to a primitive function declared outside of
4946 -- the synchronized type.
4948 -- If the context is a procedure call, there might still be
4949 -- an overloading between an entry and a primitive procedure
4950 -- declared outside of the synchronized type, called in prefix
4951 -- notation. This is harder to disambiguate because in one case
4952 -- the controlling formal is implicit ???
4954 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4955 and then Nkind (Parent (N)) /= N_Indexed_Component
4956 and then Try_Object_Operation (N)
4961 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4962 -- entry or procedure of a tagged concurrent type we must check
4963 -- if there are class-wide subprograms covering the primitive. If
4964 -- true then Try_Object_Operation reports the error.
4967 and then Is_Concurrent_Type (Prefix_Type)
4968 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4970 -- Duplicate the call. This is required to avoid problems with
4971 -- the tree transformations performed by Try_Object_Operation.
4972 -- Set properly the parent of the copied call, because it is
4973 -- about to be reanalyzed.
4976 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4979 Set_Parent (Par, Parent (Parent (N)));
4981 if Try_Object_Operation
4982 (Sinfo.Name (Par), CW_Test_Only => True)
4990 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4992 -- Case of a prefix of a protected type: selector might denote
4993 -- an invisible private component.
4995 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4996 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5000 if Present (Comp) then
5001 if Is_Single_Concurrent_Object then
5002 Error_Msg_Node_2 := Entity (Name);
5003 Error_Msg_NE ("invisible selector& for &", N, Sel);
5006 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5007 Error_Msg_NE ("invisible selector& for }", N, Sel);
5013 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5018 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5021 -- If N still has no type, the component is not defined in the prefix
5023 if Etype (N) = Any_Type then
5025 if Is_Single_Concurrent_Object then
5026 Error_Msg_Node_2 := Entity (Name);
5027 Error_Msg_NE ("no selector& for&", N, Sel);
5029 Check_Misspelled_Selector (Type_To_Use, Sel);
5031 -- If this is a derived formal type, the parent may have different
5032 -- visibility at this point. Try for an inherited component before
5033 -- reporting an error.
5035 elsif Is_Generic_Type (Prefix_Type)
5036 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5037 and then Prefix_Type /= Etype (Prefix_Type)
5038 and then Is_Record_Type (Etype (Prefix_Type))
5040 Set_Etype (Prefix (N), Etype (Prefix_Type));
5041 Analyze_Selected_Component (N);
5044 -- Similarly, if this is the actual for a formal derived type, or
5045 -- a derived type thereof, the component inherited from the generic
5046 -- parent may not be visible in the actual, but the selected
5047 -- component is legal. Climb up the derivation chain of the generic
5048 -- parent type until we find the proper ancestor type.
5050 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5052 Par : Entity_Id := Prefix_Type;
5054 -- Climb up derivation chain to generic actual subtype
5056 while not Is_Generic_Actual_Type (Par) loop
5057 if Ekind (Par) = E_Record_Type then
5058 Par := Parent_Subtype (Par);
5061 exit when Par = Etype (Par);
5066 if Present (Par) and then Is_Generic_Actual_Type (Par) then
5068 -- Now look for component in ancestor types
5070 Par := Generic_Parent_Type (Declaration_Node (Par));
5072 Find_Component_In_Instance (Par);
5073 exit when Present (Entity (Sel))
5074 or else Par = Etype (Par);
5078 -- Another special case: the type is an extension of a private
5079 -- type T, is an actual in an instance, and we are in the body
5080 -- of the instance, so the generic body had a full view of the
5081 -- type declaration for T or of some ancestor that defines the
5082 -- component in question.
5084 elsif Is_Derived_Type (Type_To_Use)
5085 and then Used_As_Generic_Actual (Type_To_Use)
5086 and then In_Instance_Body
5088 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5090 -- In ASIS mode the generic parent type may be absent. Examine
5091 -- the parent type directly for a component that may have been
5092 -- visible in a parent generic unit.
5094 elsif Is_Derived_Type (Prefix_Type) then
5095 Par := Etype (Prefix_Type);
5096 Find_Component_In_Instance (Par);
5100 -- The search above must have eventually succeeded, since the
5101 -- selected component was legal in the generic.
5103 if No (Entity (Sel)) then
5104 raise Program_Error;
5109 -- Component not found, specialize error message when appropriate
5112 if Ekind (Prefix_Type) = E_Record_Subtype then
5114 -- Check whether this is a component of the base type which
5115 -- is absent from a statically constrained subtype. This will
5116 -- raise constraint error at run time, but is not a compile-
5117 -- time error. When the selector is illegal for base type as
5118 -- well fall through and generate a compilation error anyway.
5120 Comp := First_Component (Base_Type (Prefix_Type));
5121 while Present (Comp) loop
5122 if Chars (Comp) = Chars (Sel)
5123 and then Is_Visible_Component (Comp)
5125 Set_Entity_With_Checks (Sel, Comp);
5126 Generate_Reference (Comp, Sel);
5127 Set_Etype (Sel, Etype (Comp));
5128 Set_Etype (N, Etype (Comp));
5130 -- Emit appropriate message. The node will be replaced
5131 -- by an appropriate raise statement.
5133 -- Note that in SPARK mode, as with all calls to apply a
5134 -- compile time constraint error, this will be made into
5135 -- an error to simplify the processing of the formal
5136 -- verification backend.
5138 Apply_Compile_Time_Constraint_Error
5139 (N, "component not present in }??",
5140 CE_Discriminant_Check_Failed,
5141 Ent => Prefix_Type, Rep => False);
5143 Set_Raises_Constraint_Error (N);
5147 Next_Component (Comp);
5152 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5153 Error_Msg_NE ("no selector& for}", N, Sel);
5155 -- Add information in the case of an incomplete prefix
5157 if Is_Incomplete_Type (Type_To_Use) then
5159 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5162 if From_Limited_With (Scope (Type_To_Use)) then
5164 ("\limited view of& has no components", N, Inc);
5168 ("\premature usage of incomplete type&", N, Inc);
5170 if Nkind (Parent (Inc)) =
5171 N_Incomplete_Type_Declaration
5173 -- Record location of premature use in entity so that
5174 -- a continuation message is generated when the
5175 -- completion is seen.
5177 Set_Premature_Use (Parent (Inc), N);
5183 Check_Misspelled_Selector (Type_To_Use, Sel);
5186 Set_Entity (Sel, Any_Id);
5187 Set_Etype (Sel, Any_Type);
5189 end Analyze_Selected_Component;
5191 ---------------------------
5192 -- Analyze_Short_Circuit --
5193 ---------------------------
5195 procedure Analyze_Short_Circuit (N : Node_Id) is
5196 L : constant Node_Id := Left_Opnd (N);
5197 R : constant Node_Id := Right_Opnd (N);
5202 Analyze_Expression (L);
5203 Analyze_Expression (R);
5204 Set_Etype (N, Any_Type);
5206 if not Is_Overloaded (L) then
5207 if Root_Type (Etype (L)) = Standard_Boolean
5208 and then Has_Compatible_Type (R, Etype (L))
5210 Add_One_Interp (N, Etype (L), Etype (L));
5214 Get_First_Interp (L, Ind, It);
5215 while Present (It.Typ) loop
5216 if Root_Type (It.Typ) = Standard_Boolean
5217 and then Has_Compatible_Type (R, It.Typ)
5219 Add_One_Interp (N, It.Typ, It.Typ);
5222 Get_Next_Interp (Ind, It);
5226 -- Here we have failed to find an interpretation. Clearly we know that
5227 -- it is not the case that both operands can have an interpretation of
5228 -- Boolean, but this is by far the most likely intended interpretation.
5229 -- So we simply resolve both operands as Booleans, and at least one of
5230 -- these resolutions will generate an error message, and we do not need
5231 -- to give another error message on the short circuit operation itself.
5233 if Etype (N) = Any_Type then
5234 Resolve (L, Standard_Boolean);
5235 Resolve (R, Standard_Boolean);
5236 Set_Etype (N, Standard_Boolean);
5238 end Analyze_Short_Circuit;
5244 procedure Analyze_Slice (N : Node_Id) is
5245 D : constant Node_Id := Discrete_Range (N);
5246 P : constant Node_Id := Prefix (N);
5247 Array_Type : Entity_Id;
5248 Index_Type : Entity_Id;
5250 procedure Analyze_Overloaded_Slice;
5251 -- If the prefix is overloaded, select those interpretations that
5252 -- yield a one-dimensional array type.
5254 ------------------------------
5255 -- Analyze_Overloaded_Slice --
5256 ------------------------------
5258 procedure Analyze_Overloaded_Slice is
5264 Set_Etype (N, Any_Type);
5266 Get_First_Interp (P, I, It);
5267 while Present (It.Nam) loop
5270 if Is_Access_Type (Typ) then
5271 Typ := Designated_Type (Typ);
5273 (Warn_On_Dereference, "?d?implicit dereference", N);
5276 if Is_Array_Type (Typ)
5277 and then Number_Dimensions (Typ) = 1
5278 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5280 Add_One_Interp (N, Typ, Typ);
5283 Get_Next_Interp (I, It);
5286 if Etype (N) = Any_Type then
5287 Error_Msg_N ("expect array type in prefix of slice", N);
5289 end Analyze_Overloaded_Slice;
5291 -- Start of processing for Analyze_Slice
5294 if Comes_From_Source (N) then
5295 Check_SPARK_05_Restriction ("slice is not allowed", N);
5301 if Is_Overloaded (P) then
5302 Analyze_Overloaded_Slice;
5305 Array_Type := Etype (P);
5306 Set_Etype (N, Any_Type);
5308 if Is_Access_Type (Array_Type) then
5309 Array_Type := Designated_Type (Array_Type);
5310 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5313 if not Is_Array_Type (Array_Type) then
5314 Wrong_Type (P, Any_Array);
5316 elsif Number_Dimensions (Array_Type) > 1 then
5318 ("type is not one-dimensional array in slice prefix", N);
5321 if Ekind (Array_Type) = E_String_Literal_Subtype then
5322 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5324 Index_Type := Etype (First_Index (Array_Type));
5327 if not Has_Compatible_Type (D, Index_Type) then
5328 Wrong_Type (D, Index_Type);
5330 Set_Etype (N, Array_Type);
5336 -----------------------------
5337 -- Analyze_Type_Conversion --
5338 -----------------------------
5340 procedure Analyze_Type_Conversion (N : Node_Id) is
5341 Expr : constant Node_Id := Expression (N);
5345 -- If Conversion_OK is set, then the Etype is already set, and the only
5346 -- processing required is to analyze the expression. This is used to
5347 -- construct certain "illegal" conversions which are not allowed by Ada
5348 -- semantics, but can be handled by Gigi, see Sinfo for further details.
5350 if Conversion_OK (N) then
5355 -- Otherwise full type analysis is required, as well as some semantic
5356 -- checks to make sure the argument of the conversion is appropriate.
5358 Find_Type (Subtype_Mark (N));
5359 Typ := Entity (Subtype_Mark (N));
5361 Check_Fully_Declared (Typ, N);
5362 Analyze_Expression (Expr);
5363 Validate_Remote_Type_Type_Conversion (N);
5365 -- Only remaining step is validity checks on the argument. These
5366 -- are skipped if the conversion does not come from the source.
5368 if not Comes_From_Source (N) then
5371 -- If there was an error in a generic unit, no need to replicate the
5372 -- error message. Conversely, constant-folding in the generic may
5373 -- transform the argument of a conversion into a string literal, which
5374 -- is legal. Therefore the following tests are not performed in an
5375 -- instance. The same applies to an inlined body.
5377 elsif In_Instance or In_Inlined_Body then
5380 elsif Nkind (Expr) = N_Null then
5381 Error_Msg_N ("argument of conversion cannot be null", N);
5382 Error_Msg_N ("\use qualified expression instead", N);
5383 Set_Etype (N, Any_Type);
5385 elsif Nkind (Expr) = N_Aggregate then
5386 Error_Msg_N ("argument of conversion cannot be aggregate", N);
5387 Error_Msg_N ("\use qualified expression instead", N);
5389 elsif Nkind (Expr) = N_Allocator then
5390 Error_Msg_N ("argument of conversion cannot be an allocator", N);
5391 Error_Msg_N ("\use qualified expression instead", N);
5393 elsif Nkind (Expr) = N_String_Literal then
5394 Error_Msg_N ("argument of conversion cannot be string literal", N);
5395 Error_Msg_N ("\use qualified expression instead", N);
5397 elsif Nkind (Expr) = N_Character_Literal then
5398 if Ada_Version = Ada_83 then
5399 Resolve (Expr, Typ);
5401 Error_Msg_N ("argument of conversion cannot be character literal",
5403 Error_Msg_N ("\use qualified expression instead", N);
5406 elsif Nkind (Expr) = N_Attribute_Reference
5407 and then Nam_In (Attribute_Name (Expr), Name_Access,
5408 Name_Unchecked_Access,
5409 Name_Unrestricted_Access)
5411 Error_Msg_N ("argument of conversion cannot be access", N);
5412 Error_Msg_N ("\use qualified expression instead", N);
5415 -- A formal parameter of a specific tagged type whose related subprogram
5416 -- is subject to pragma Extensions_Visible with value "False" cannot
5417 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5418 -- internally generated expressions.
5420 if Is_Class_Wide_Type (Typ)
5421 and then Comes_From_Source (Expr)
5422 and then Is_EVF_Expression (Expr)
5425 ("formal parameter cannot be converted to class-wide type when "
5426 & "Extensions_Visible is False", Expr);
5428 end Analyze_Type_Conversion;
5430 ----------------------
5431 -- Analyze_Unary_Op --
5432 ----------------------
5434 procedure Analyze_Unary_Op (N : Node_Id) is
5435 R : constant Node_Id := Right_Opnd (N);
5436 Op_Id : Entity_Id := Entity (N);
5439 Set_Etype (N, Any_Type);
5440 Candidate_Type := Empty;
5442 Analyze_Expression (R);
5444 if Present (Op_Id) then
5445 if Ekind (Op_Id) = E_Operator then
5446 Find_Unary_Types (R, Op_Id, N);
5448 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5452 Op_Id := Get_Name_Entity_Id (Chars (N));
5453 while Present (Op_Id) loop
5454 if Ekind (Op_Id) = E_Operator then
5455 if No (Next_Entity (First_Entity (Op_Id))) then
5456 Find_Unary_Types (R, Op_Id, N);
5459 elsif Is_Overloadable (Op_Id) then
5460 Analyze_User_Defined_Unary_Op (N, Op_Id);
5463 Op_Id := Homonym (Op_Id);
5468 end Analyze_Unary_Op;
5470 ----------------------------------
5471 -- Analyze_Unchecked_Expression --
5472 ----------------------------------
5474 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5476 Analyze (Expression (N), Suppress => All_Checks);
5477 Set_Etype (N, Etype (Expression (N)));
5478 Save_Interps (Expression (N), N);
5479 end Analyze_Unchecked_Expression;
5481 ---------------------------------------
5482 -- Analyze_Unchecked_Type_Conversion --
5483 ---------------------------------------
5485 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5487 Find_Type (Subtype_Mark (N));
5488 Analyze_Expression (Expression (N));
5489 Set_Etype (N, Entity (Subtype_Mark (N)));
5490 end Analyze_Unchecked_Type_Conversion;
5492 ------------------------------------
5493 -- Analyze_User_Defined_Binary_Op --
5494 ------------------------------------
5496 procedure Analyze_User_Defined_Binary_Op
5501 -- Only do analysis if the operator Comes_From_Source, since otherwise
5502 -- the operator was generated by the expander, and all such operators
5503 -- always refer to the operators in package Standard.
5505 if Comes_From_Source (N) then
5507 F1 : constant Entity_Id := First_Formal (Op_Id);
5508 F2 : constant Entity_Id := Next_Formal (F1);
5511 -- Verify that Op_Id is a visible binary function. Note that since
5512 -- we know Op_Id is overloaded, potentially use visible means use
5513 -- visible for sure (RM 9.4(11)).
5515 if Ekind (Op_Id) = E_Function
5516 and then Present (F2)
5517 and then (Is_Immediately_Visible (Op_Id)
5518 or else Is_Potentially_Use_Visible (Op_Id))
5519 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5520 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5522 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5524 -- If the left operand is overloaded, indicate that the current
5525 -- type is a viable candidate. This is redundant in most cases,
5526 -- but for equality and comparison operators where the context
5527 -- does not impose a type on the operands, setting the proper
5528 -- type is necessary to avoid subsequent ambiguities during
5529 -- resolution, when both user-defined and predefined operators
5530 -- may be candidates.
5532 if Is_Overloaded (Left_Opnd (N)) then
5533 Set_Etype (Left_Opnd (N), Etype (F1));
5536 if Debug_Flag_E then
5537 Write_Str ("user defined operator ");
5538 Write_Name (Chars (Op_Id));
5539 Write_Str (" on node ");
5540 Write_Int (Int (N));
5546 end Analyze_User_Defined_Binary_Op;
5548 -----------------------------------
5549 -- Analyze_User_Defined_Unary_Op --
5550 -----------------------------------
5552 procedure Analyze_User_Defined_Unary_Op
5557 -- Only do analysis if the operator Comes_From_Source, since otherwise
5558 -- the operator was generated by the expander, and all such operators
5559 -- always refer to the operators in package Standard.
5561 if Comes_From_Source (N) then
5563 F : constant Entity_Id := First_Formal (Op_Id);
5566 -- Verify that Op_Id is a visible unary function. Note that since
5567 -- we know Op_Id is overloaded, potentially use visible means use
5568 -- visible for sure (RM 9.4(11)).
5570 if Ekind (Op_Id) = E_Function
5571 and then No (Next_Formal (F))
5572 and then (Is_Immediately_Visible (Op_Id)
5573 or else Is_Potentially_Use_Visible (Op_Id))
5574 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5576 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5580 end Analyze_User_Defined_Unary_Op;
5582 ---------------------------
5583 -- Check_Arithmetic_Pair --
5584 ---------------------------
5586 procedure Check_Arithmetic_Pair
5587 (T1, T2 : Entity_Id;
5591 Op_Name : constant Name_Id := Chars (Op_Id);
5593 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5594 -- Check whether the fixed-point type Typ has a user-defined operator
5595 -- (multiplication or division) that should hide the corresponding
5596 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5597 -- such operators more visible and therefore useful.
5599 -- If the name of the operation is an expanded name with prefix
5600 -- Standard, the predefined universal fixed operator is available,
5601 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5603 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5604 -- Get specific type (i.e. non-universal type if there is one)
5610 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5611 Bas : constant Entity_Id := Base_Type (Typ);
5617 -- If the universal_fixed operation is given explicitly the rule
5618 -- concerning primitive operations of the type do not apply.
5620 if Nkind (N) = N_Function_Call
5621 and then Nkind (Name (N)) = N_Expanded_Name
5622 and then Entity (Prefix (Name (N))) = Standard_Standard
5627 -- The operation is treated as primitive if it is declared in the
5628 -- same scope as the type, and therefore on the same entity chain.
5630 Ent := Next_Entity (Typ);
5631 while Present (Ent) loop
5632 if Chars (Ent) = Chars (Op) then
5633 F1 := First_Formal (Ent);
5634 F2 := Next_Formal (F1);
5636 -- The operation counts as primitive if either operand or
5637 -- result are of the given base type, and both operands are
5638 -- fixed point types.
5640 if (Base_Type (Etype (F1)) = Bas
5641 and then Is_Fixed_Point_Type (Etype (F2)))
5644 (Base_Type (Etype (F2)) = Bas
5645 and then Is_Fixed_Point_Type (Etype (F1)))
5648 (Base_Type (Etype (Ent)) = Bas
5649 and then Is_Fixed_Point_Type (Etype (F1))
5650 and then Is_Fixed_Point_Type (Etype (F2)))
5666 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5668 if T1 = Universal_Integer or else T1 = Universal_Real then
5669 return Base_Type (T2);
5671 return Base_Type (T1);
5675 -- Start of processing for Check_Arithmetic_Pair
5678 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5679 if Is_Numeric_Type (T1)
5680 and then Is_Numeric_Type (T2)
5681 and then (Covers (T1 => T1, T2 => T2)
5683 Covers (T1 => T2, T2 => T1))
5685 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5688 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5689 if Is_Fixed_Point_Type (T1)
5690 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5692 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5693 -- and no further processing is required (this is the case of an
5694 -- operator constructed by Exp_Fixd for a fixed point operation)
5695 -- Otherwise add one interpretation with universal fixed result
5696 -- If the operator is given in functional notation, it comes
5697 -- from source and Fixed_As_Integer cannot apply.
5699 if (Nkind (N) not in N_Op
5700 or else not Treat_Fixed_As_Integer (N))
5702 (not Has_Fixed_Op (T1, Op_Id)
5703 or else Nkind (Parent (N)) = N_Type_Conversion)
5705 Add_One_Interp (N, Op_Id, Universal_Fixed);
5708 elsif Is_Fixed_Point_Type (T2)
5709 and then (Nkind (N) not in N_Op
5710 or else not Treat_Fixed_As_Integer (N))
5711 and then T1 = Universal_Real
5713 (not Has_Fixed_Op (T1, Op_Id)
5714 or else Nkind (Parent (N)) = N_Type_Conversion)
5716 Add_One_Interp (N, Op_Id, Universal_Fixed);
5718 elsif Is_Numeric_Type (T1)
5719 and then Is_Numeric_Type (T2)
5720 and then (Covers (T1 => T1, T2 => T2)
5722 Covers (T1 => T2, T2 => T1))
5724 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5726 elsif Is_Fixed_Point_Type (T1)
5727 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5728 or else T2 = Universal_Integer)
5730 Add_One_Interp (N, Op_Id, T1);
5732 elsif T2 = Universal_Real
5733 and then Base_Type (T1) = Base_Type (Standard_Integer)
5734 and then Op_Name = Name_Op_Multiply
5736 Add_One_Interp (N, Op_Id, Any_Fixed);
5738 elsif T1 = Universal_Real
5739 and then Base_Type (T2) = Base_Type (Standard_Integer)
5741 Add_One_Interp (N, Op_Id, Any_Fixed);
5743 elsif Is_Fixed_Point_Type (T2)
5744 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5745 or else T1 = Universal_Integer)
5746 and then Op_Name = Name_Op_Multiply
5748 Add_One_Interp (N, Op_Id, T2);
5750 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5751 Add_One_Interp (N, Op_Id, T1);
5753 elsif T2 = Universal_Real
5754 and then T1 = Universal_Integer
5755 and then Op_Name = Name_Op_Multiply
5757 Add_One_Interp (N, Op_Id, T2);
5760 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5762 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5763 -- set does not require any special processing, since the Etype is
5764 -- already set (case of operation constructed by Exp_Fixed).
5766 if Is_Integer_Type (T1)
5767 and then (Covers (T1 => T1, T2 => T2)
5769 Covers (T1 => T2, T2 => T1))
5771 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5774 elsif Op_Name = Name_Op_Expon then
5775 if Is_Numeric_Type (T1)
5776 and then not Is_Fixed_Point_Type (T1)
5777 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5778 or else T2 = Universal_Integer)
5780 Add_One_Interp (N, Op_Id, Base_Type (T1));
5783 else pragma Assert (Nkind (N) in N_Op_Shift);
5785 -- If not one of the predefined operators, the node may be one
5786 -- of the intrinsic functions. Its kind is always specific, and
5787 -- we can use it directly, rather than the name of the operation.
5789 if Is_Integer_Type (T1)
5790 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5791 or else T2 = Universal_Integer)
5793 Add_One_Interp (N, Op_Id, Base_Type (T1));
5796 end Check_Arithmetic_Pair;
5798 -------------------------------
5799 -- Check_Misspelled_Selector --
5800 -------------------------------
5802 procedure Check_Misspelled_Selector
5803 (Prefix : Entity_Id;
5806 Max_Suggestions : constant := 2;
5807 Nr_Of_Suggestions : Natural := 0;
5809 Suggestion_1 : Entity_Id := Empty;
5810 Suggestion_2 : Entity_Id := Empty;
5815 -- All the components of the prefix of selector Sel are matched against
5816 -- Sel and a count is maintained of possible misspellings. When at
5817 -- the end of the analysis there are one or two (not more) possible
5818 -- misspellings, these misspellings will be suggested as possible
5821 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5823 -- Concurrent types should be handled as well ???
5828 Comp := First_Entity (Prefix);
5829 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5830 if Is_Visible_Component (Comp) then
5831 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5832 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5834 case Nr_Of_Suggestions is
5835 when 1 => Suggestion_1 := Comp;
5836 when 2 => Suggestion_2 := Comp;
5837 when others => null;
5842 Comp := Next_Entity (Comp);
5845 -- Report at most two suggestions
5847 if Nr_Of_Suggestions = 1 then
5848 Error_Msg_NE -- CODEFIX
5849 ("\possible misspelling of&", Sel, Suggestion_1);
5851 elsif Nr_Of_Suggestions = 2 then
5852 Error_Msg_Node_2 := Suggestion_2;
5853 Error_Msg_NE -- CODEFIX
5854 ("\possible misspelling of& or&", Sel, Suggestion_1);
5856 end Check_Misspelled_Selector;
5858 ----------------------
5859 -- Defined_In_Scope --
5860 ----------------------
5862 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5864 S1 : constant Entity_Id := Scope (Base_Type (T));
5867 or else (S1 = System_Aux_Id and then S = Scope (S1));
5868 end Defined_In_Scope;
5874 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5880 Void_Interp_Seen : Boolean := False;
5883 pragma Warnings (Off, Boolean);
5886 if Ada_Version >= Ada_2005 then
5887 Actual := First_Actual (N);
5888 while Present (Actual) loop
5890 -- Ada 2005 (AI-50217): Post an error in case of premature
5891 -- usage of an entity from the limited view.
5893 if not Analyzed (Etype (Actual))
5894 and then From_Limited_With (Etype (Actual))
5896 Error_Msg_Qual_Level := 1;
5898 ("missing with_clause for scope of imported type&",
5899 Actual, Etype (Actual));
5900 Error_Msg_Qual_Level := 0;
5903 Next_Actual (Actual);
5907 -- Before listing the possible candidates, check whether this is
5908 -- a prefix of a selected component that has been rewritten as a
5909 -- parameterless function call because there is a callable candidate
5910 -- interpretation. If there is a hidden package in the list of homonyms
5911 -- of the function name (bad programming style in any case) suggest that
5912 -- this is the intended entity.
5914 if No (Parameter_Associations (N))
5915 and then Nkind (Parent (N)) = N_Selected_Component
5916 and then Nkind (Parent (Parent (N))) in N_Declaration
5917 and then Is_Overloaded (Nam)
5923 Ent := Current_Entity (Nam);
5924 while Present (Ent) loop
5925 if Ekind (Ent) = E_Package then
5927 ("no legal interpretations as function call,!", Nam);
5928 Error_Msg_NE ("\package& is not visible", N, Ent);
5930 Rewrite (Parent (N),
5931 New_Occurrence_Of (Any_Type, Sloc (N)));
5935 Ent := Homonym (Ent);
5940 -- Analyze each candidate call again, with full error reporting for
5944 ("no candidate interpretations match the actuals:!", Nam);
5945 Err_Mode := All_Errors_Mode;
5946 All_Errors_Mode := True;
5948 -- If this is a call to an operation of a concurrent type,
5949 -- the failed interpretations have been removed from the
5950 -- name. Recover them to provide full diagnostics.
5952 if Nkind (Parent (Nam)) = N_Selected_Component then
5953 Set_Entity (Nam, Empty);
5954 New_Nam := New_Copy_Tree (Parent (Nam));
5955 Set_Is_Overloaded (New_Nam, False);
5956 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5957 Set_Parent (New_Nam, Parent (Parent (Nam)));
5958 Analyze_Selected_Component (New_Nam);
5959 Get_First_Interp (Selector_Name (New_Nam), X, It);
5961 Get_First_Interp (Nam, X, It);
5964 while Present (It.Nam) loop
5965 if Etype (It.Nam) = Standard_Void_Type then
5966 Void_Interp_Seen := True;
5969 Analyze_One_Call (N, It.Nam, True, Success);
5970 Get_Next_Interp (X, It);
5973 if Nkind (N) = N_Function_Call then
5974 Get_First_Interp (Nam, X, It);
5975 while Present (It.Nam) loop
5976 if Ekind_In (It.Nam, E_Function, E_Operator) then
5979 Get_Next_Interp (X, It);
5983 -- If all interpretations are procedures, this deserves a
5984 -- more precise message. Ditto if this appears as the prefix
5985 -- of a selected component, which may be a lexical error.
5988 ("\context requires function call, found procedure name", Nam);
5990 if Nkind (Parent (N)) = N_Selected_Component
5991 and then N = Prefix (Parent (N))
5993 Error_Msg_N -- CODEFIX
5994 ("\period should probably be semicolon", Parent (N));
5997 elsif Nkind (N) = N_Procedure_Call_Statement
5998 and then not Void_Interp_Seen
6001 "\function name found in procedure call", Nam);
6004 All_Errors_Mode := Err_Mode;
6007 ---------------------------
6008 -- Find_Arithmetic_Types --
6009 ---------------------------
6011 procedure Find_Arithmetic_Types
6016 Index1 : Interp_Index;
6017 Index2 : Interp_Index;
6021 procedure Check_Right_Argument (T : Entity_Id);
6022 -- Check right operand of operator
6024 --------------------------
6025 -- Check_Right_Argument --
6026 --------------------------
6028 procedure Check_Right_Argument (T : Entity_Id) is
6030 if not Is_Overloaded (R) then
6031 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6033 Get_First_Interp (R, Index2, It2);
6034 while Present (It2.Typ) loop
6035 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
6036 Get_Next_Interp (Index2, It2);
6039 end Check_Right_Argument;
6041 -- Start of processing for Find_Arithmetic_Types
6044 if not Is_Overloaded (L) then
6045 Check_Right_Argument (Etype (L));
6048 Get_First_Interp (L, Index1, It1);
6049 while Present (It1.Typ) loop
6050 Check_Right_Argument (It1.Typ);
6051 Get_Next_Interp (Index1, It1);
6055 end Find_Arithmetic_Types;
6057 ------------------------
6058 -- Find_Boolean_Types --
6059 ------------------------
6061 procedure Find_Boolean_Types
6066 Index : Interp_Index;
6069 procedure Check_Numeric_Argument (T : Entity_Id);
6070 -- Special case for logical operations one of whose operands is an
6071 -- integer literal. If both are literal the result is any modular type.
6073 ----------------------------
6074 -- Check_Numeric_Argument --
6075 ----------------------------
6077 procedure Check_Numeric_Argument (T : Entity_Id) is
6079 if T = Universal_Integer then
6080 Add_One_Interp (N, Op_Id, Any_Modular);
6082 elsif Is_Modular_Integer_Type (T) then
6083 Add_One_Interp (N, Op_Id, T);
6085 end Check_Numeric_Argument;
6087 -- Start of processing for Find_Boolean_Types
6090 if not Is_Overloaded (L) then
6091 if Etype (L) = Universal_Integer
6092 or else Etype (L) = Any_Modular
6094 if not Is_Overloaded (R) then
6095 Check_Numeric_Argument (Etype (R));
6098 Get_First_Interp (R, Index, It);
6099 while Present (It.Typ) loop
6100 Check_Numeric_Argument (It.Typ);
6101 Get_Next_Interp (Index, It);
6105 -- If operands are aggregates, we must assume that they may be
6106 -- boolean arrays, and leave disambiguation for the second pass.
6107 -- If only one is an aggregate, verify that the other one has an
6108 -- interpretation as a boolean array
6110 elsif Nkind (L) = N_Aggregate then
6111 if Nkind (R) = N_Aggregate then
6112 Add_One_Interp (N, Op_Id, Etype (L));
6114 elsif not Is_Overloaded (R) then
6115 if Valid_Boolean_Arg (Etype (R)) then
6116 Add_One_Interp (N, Op_Id, Etype (R));
6120 Get_First_Interp (R, Index, It);
6121 while Present (It.Typ) loop
6122 if Valid_Boolean_Arg (It.Typ) then
6123 Add_One_Interp (N, Op_Id, It.Typ);
6126 Get_Next_Interp (Index, It);
6130 elsif Valid_Boolean_Arg (Etype (L))
6131 and then Has_Compatible_Type (R, Etype (L))
6133 Add_One_Interp (N, Op_Id, Etype (L));
6137 Get_First_Interp (L, Index, It);
6138 while Present (It.Typ) loop
6139 if Valid_Boolean_Arg (It.Typ)
6140 and then Has_Compatible_Type (R, It.Typ)
6142 Add_One_Interp (N, Op_Id, It.Typ);
6145 Get_Next_Interp (Index, It);
6148 end Find_Boolean_Types;
6150 ---------------------------
6151 -- Find_Comparison_Types --
6152 ---------------------------
6154 procedure Find_Comparison_Types
6159 Index : Interp_Index;
6161 Found : Boolean := False;
6164 Scop : Entity_Id := Empty;
6166 procedure Try_One_Interp (T1 : Entity_Id);
6167 -- Routine to try one proposed interpretation. Note that the context
6168 -- of the operator plays no role in resolving the arguments, so that
6169 -- if there is more than one interpretation of the operands that is
6170 -- compatible with comparison, the operation is ambiguous.
6172 --------------------
6173 -- Try_One_Interp --
6174 --------------------
6176 procedure Try_One_Interp (T1 : Entity_Id) is
6179 -- If the operator is an expanded name, then the type of the operand
6180 -- must be defined in the corresponding scope. If the type is
6181 -- universal, the context will impose the correct type.
6184 and then not Defined_In_Scope (T1, Scop)
6185 and then T1 /= Universal_Integer
6186 and then T1 /= Universal_Real
6187 and then T1 /= Any_String
6188 and then T1 /= Any_Composite
6193 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6194 if Found and then Base_Type (T1) /= Base_Type (T_F) then
6195 It := Disambiguate (L, I_F, Index, Any_Type);
6197 if It = No_Interp then
6198 Ambiguous_Operands (N);
6199 Set_Etype (L, Any_Type);
6213 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6218 -- Start of processing for Find_Comparison_Types
6221 -- If left operand is aggregate, the right operand has to
6222 -- provide a usable type for it.
6224 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
6225 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6229 if Nkind (N) = N_Function_Call
6230 and then Nkind (Name (N)) = N_Expanded_Name
6232 Scop := Entity (Prefix (Name (N)));
6234 -- The prefix may be a package renaming, and the subsequent test
6235 -- requires the original package.
6237 if Ekind (Scop) = E_Package
6238 and then Present (Renamed_Entity (Scop))
6240 Scop := Renamed_Entity (Scop);
6241 Set_Entity (Prefix (Name (N)), Scop);
6245 if not Is_Overloaded (L) then
6246 Try_One_Interp (Etype (L));
6249 Get_First_Interp (L, Index, It);
6250 while Present (It.Typ) loop
6251 Try_One_Interp (It.Typ);
6252 Get_Next_Interp (Index, It);
6255 end Find_Comparison_Types;
6257 ----------------------------------------
6258 -- Find_Non_Universal_Interpretations --
6259 ----------------------------------------
6261 procedure Find_Non_Universal_Interpretations
6267 Index : Interp_Index;
6271 if T1 = Universal_Integer or else T1 = Universal_Real
6273 -- If the left operand of an equality operator is null, the visibility
6274 -- of the operator must be determined from the interpretation of the
6275 -- right operand. This processing must be done for Any_Access, which
6276 -- is the internal representation of the type of the literal null.
6278 or else T1 = Any_Access
6280 if not Is_Overloaded (R) then
6281 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
6283 Get_First_Interp (R, Index, It);
6284 while Present (It.Typ) loop
6285 if Covers (It.Typ, T1) then
6287 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6290 Get_Next_Interp (Index, It);
6294 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6296 end Find_Non_Universal_Interpretations;
6298 ------------------------------
6299 -- Find_Concatenation_Types --
6300 ------------------------------
6302 procedure Find_Concatenation_Types
6307 Op_Type : constant Entity_Id := Etype (Op_Id);
6310 if Is_Array_Type (Op_Type)
6311 and then not Is_Limited_Type (Op_Type)
6313 and then (Has_Compatible_Type (L, Op_Type)
6315 Has_Compatible_Type (L, Component_Type (Op_Type)))
6317 and then (Has_Compatible_Type (R, Op_Type)
6319 Has_Compatible_Type (R, Component_Type (Op_Type)))
6321 Add_One_Interp (N, Op_Id, Op_Type);
6323 end Find_Concatenation_Types;
6325 -------------------------
6326 -- Find_Equality_Types --
6327 -------------------------
6329 procedure Find_Equality_Types
6334 Index : Interp_Index;
6336 Found : Boolean := False;
6339 Scop : Entity_Id := Empty;
6341 procedure Try_One_Interp (T1 : Entity_Id);
6342 -- The context of the equality operator plays no role in resolving the
6343 -- arguments, so that if there is more than one interpretation of the
6344 -- operands that is compatible with equality, the construct is ambiguous
6345 -- and an error can be emitted now, after trying to disambiguate, i.e.
6346 -- applying preference rules.
6348 --------------------
6349 -- Try_One_Interp --
6350 --------------------
6352 procedure Try_One_Interp (T1 : Entity_Id) is
6353 Bas : constant Entity_Id := Base_Type (T1);
6356 -- If the operator is an expanded name, then the type of the operand
6357 -- must be defined in the corresponding scope. If the type is
6358 -- universal, the context will impose the correct type. An anonymous
6359 -- type for a 'Access reference is also universal in this sense, as
6360 -- the actual type is obtained from context.
6362 -- In Ada 2005, the equality operator for anonymous access types
6363 -- is declared in Standard, and preference rules apply to it.
6365 if Present (Scop) then
6366 if Defined_In_Scope (T1, Scop)
6367 or else T1 = Universal_Integer
6368 or else T1 = Universal_Real
6369 or else T1 = Any_Access
6370 or else T1 = Any_String
6371 or else T1 = Any_Composite
6372 or else (Ekind (T1) = E_Access_Subprogram_Type
6373 and then not Comes_From_Source (T1))
6377 elsif Ekind (T1) = E_Anonymous_Access_Type
6378 and then Scop = Standard_Standard
6383 -- The scope does not contain an operator for the type
6388 -- If we have infix notation, the operator must be usable. Within
6389 -- an instance, if the type is already established we know it is
6390 -- correct. If an operand is universal it is compatible with any
6393 elsif In_Open_Scopes (Scope (Bas))
6394 or else Is_Potentially_Use_Visible (Bas)
6395 or else In_Use (Bas)
6396 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
6398 -- In an instance, the type may have been immediately visible.
6399 -- Either the types are compatible, or one operand is universal
6400 -- (numeric or null).
6402 or else (In_Instance
6404 (First_Subtype (T1) = First_Subtype (Etype (R))
6405 or else Nkind (R) = N_Null
6407 (Is_Numeric_Type (T1)
6408 and then Is_Universal_Numeric_Type (Etype (R)))))
6410 -- In Ada 2005, the equality on anonymous access types is declared
6411 -- in Standard, and is always visible.
6413 or else Ekind (T1) = E_Anonymous_Access_Type
6418 -- Save candidate type for subsequent error message, if any
6420 if not Is_Limited_Type (T1) then
6421 Candidate_Type := T1;
6427 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6428 -- Do not allow anonymous access types in equality operators.
6430 if Ada_Version < Ada_2005
6431 and then Ekind (T1) = E_Anonymous_Access_Type
6436 -- If the right operand has a type compatible with T1, check for an
6437 -- acceptable interpretation, unless T1 is limited (no predefined
6438 -- equality available), or this is use of a "/=" for a tagged type.
6439 -- In the latter case, possible interpretations of equality need
6440 -- to be considered, we don't want the default inequality declared
6441 -- in Standard to be chosen, and the "/=" will be rewritten as a
6442 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
6443 -- that rewriting happens during analysis rather than being
6444 -- delayed until expansion (this is needed for ASIS, which only sees
6445 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
6446 -- is Name_Op_Eq then we still proceed with the interpretation,
6447 -- because that indicates the potential rewriting case where the
6448 -- interpretation to consider is actually "=" and the node may be
6449 -- about to be rewritten by Analyze_Equality_Op.
6451 if T1 /= Standard_Void_Type
6452 and then Has_Compatible_Type (R, T1)
6455 ((not Is_Limited_Type (T1)
6456 and then not Is_Limited_Composite (T1))
6460 and then not Is_Limited_Type (Component_Type (T1))
6461 and then Available_Full_View_Of_Component (T1)))
6464 (Nkind (N) /= N_Op_Ne
6465 or else not Is_Tagged_Type (T1)
6466 or else Chars (Op_Id) = Name_Op_Eq)
6469 and then Base_Type (T1) /= Base_Type (T_F)
6471 It := Disambiguate (L, I_F, Index, Any_Type);
6473 if It = No_Interp then
6474 Ambiguous_Operands (N);
6475 Set_Etype (L, Any_Type);
6488 if not Analyzed (L) then
6492 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6494 -- Case of operator was not visible, Etype still set to Any_Type
6496 if Etype (N) = Any_Type then
6500 elsif Scop = Standard_Standard
6501 and then Ekind (T1) = E_Anonymous_Access_Type
6507 -- Start of processing for Find_Equality_Types
6510 -- If left operand is aggregate, the right operand has to
6511 -- provide a usable type for it.
6513 if Nkind (L) = N_Aggregate
6514 and then Nkind (R) /= N_Aggregate
6516 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6520 if Nkind (N) = N_Function_Call
6521 and then Nkind (Name (N)) = N_Expanded_Name
6523 Scop := Entity (Prefix (Name (N)));
6525 -- The prefix may be a package renaming, and the subsequent test
6526 -- requires the original package.
6528 if Ekind (Scop) = E_Package
6529 and then Present (Renamed_Entity (Scop))
6531 Scop := Renamed_Entity (Scop);
6532 Set_Entity (Prefix (Name (N)), Scop);
6536 if not Is_Overloaded (L) then
6537 Try_One_Interp (Etype (L));
6540 Get_First_Interp (L, Index, It);
6541 while Present (It.Typ) loop
6542 Try_One_Interp (It.Typ);
6543 Get_Next_Interp (Index, It);
6546 end Find_Equality_Types;
6548 -------------------------
6549 -- Find_Negation_Types --
6550 -------------------------
6552 procedure Find_Negation_Types
6557 Index : Interp_Index;
6561 if not Is_Overloaded (R) then
6562 if Etype (R) = Universal_Integer then
6563 Add_One_Interp (N, Op_Id, Any_Modular);
6564 elsif Valid_Boolean_Arg (Etype (R)) then
6565 Add_One_Interp (N, Op_Id, Etype (R));
6569 Get_First_Interp (R, Index, It);
6570 while Present (It.Typ) loop
6571 if Valid_Boolean_Arg (It.Typ) then
6572 Add_One_Interp (N, Op_Id, It.Typ);
6575 Get_Next_Interp (Index, It);
6578 end Find_Negation_Types;
6580 ------------------------------
6581 -- Find_Primitive_Operation --
6582 ------------------------------
6584 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6585 Obj : constant Node_Id := Prefix (N);
6586 Op : constant Node_Id := Selector_Name (N);
6593 Set_Etype (Op, Any_Type);
6595 if Is_Access_Type (Etype (Obj)) then
6596 Typ := Designated_Type (Etype (Obj));
6601 if Is_Class_Wide_Type (Typ) then
6602 Typ := Root_Type (Typ);
6605 Prims := Primitive_Operations (Typ);
6607 Prim := First_Elmt (Prims);
6608 while Present (Prim) loop
6609 if Chars (Node (Prim)) = Chars (Op) then
6610 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6611 Set_Etype (N, Etype (Node (Prim)));
6617 -- Now look for class-wide operations of the type or any of its
6618 -- ancestors by iterating over the homonyms of the selector.
6621 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6625 Hom := Current_Entity (Op);
6626 while Present (Hom) loop
6627 if (Ekind (Hom) = E_Procedure
6629 Ekind (Hom) = E_Function)
6630 and then Scope (Hom) = Scope (Typ)
6631 and then Present (First_Formal (Hom))
6633 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6635 (Is_Access_Type (Etype (First_Formal (Hom)))
6637 Ekind (Etype (First_Formal (Hom))) =
6638 E_Anonymous_Access_Type
6641 (Designated_Type (Etype (First_Formal (Hom)))) =
6644 Add_One_Interp (Op, Hom, Etype (Hom));
6645 Set_Etype (N, Etype (Hom));
6648 Hom := Homonym (Hom);
6652 return Etype (Op) /= Any_Type;
6653 end Find_Primitive_Operation;
6655 ----------------------
6656 -- Find_Unary_Types --
6657 ----------------------
6659 procedure Find_Unary_Types
6664 Index : Interp_Index;
6668 if not Is_Overloaded (R) then
6669 if Is_Numeric_Type (Etype (R)) then
6671 -- In an instance a generic actual may be a numeric type even if
6672 -- the formal in the generic unit was not. In that case, the
6673 -- predefined operator was not a possible interpretation in the
6674 -- generic, and cannot be one in the instance, unless the operator
6675 -- is an actual of an instance.
6679 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6683 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6688 Get_First_Interp (R, Index, It);
6689 while Present (It.Typ) loop
6690 if Is_Numeric_Type (It.Typ) then
6694 (Corresponding_Generic_Type (Etype (It.Typ)))
6699 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6703 Get_Next_Interp (Index, It);
6706 end Find_Unary_Types;
6712 function Junk_Operand (N : Node_Id) return Boolean is
6716 if Error_Posted (N) then
6720 -- Get entity to be tested
6722 if Is_Entity_Name (N)
6723 and then Present (Entity (N))
6727 -- An odd case, a procedure name gets converted to a very peculiar
6728 -- function call, and here is where we detect this happening.
6730 elsif Nkind (N) = N_Function_Call
6731 and then Is_Entity_Name (Name (N))
6732 and then Present (Entity (Name (N)))
6736 -- Another odd case, there are at least some cases of selected
6737 -- components where the selected component is not marked as having
6738 -- an entity, even though the selector does have an entity
6740 elsif Nkind (N) = N_Selected_Component
6741 and then Present (Entity (Selector_Name (N)))
6743 Enode := Selector_Name (N);
6749 -- Now test the entity we got to see if it is a bad case
6751 case Ekind (Entity (Enode)) is
6754 ("package name cannot be used as operand", Enode);
6756 when Generic_Unit_Kind =>
6758 ("generic unit name cannot be used as operand", Enode);
6762 ("subtype name cannot be used as operand", Enode);
6766 ("entry name cannot be used as operand", Enode);
6770 ("procedure name cannot be used as operand", Enode);
6774 ("exception name cannot be used as operand", Enode);
6781 ("label name cannot be used as operand", Enode);
6790 --------------------
6791 -- Operator_Check --
6792 --------------------
6794 procedure Operator_Check (N : Node_Id) is
6796 Remove_Abstract_Operations (N);
6798 -- Test for case of no interpretation found for operator
6800 if Etype (N) = Any_Type then
6804 Op_Id : Entity_Id := Empty;
6807 R := Right_Opnd (N);
6809 if Nkind (N) in N_Binary_Op then
6815 -- If either operand has no type, then don't complain further,
6816 -- since this simply means that we have a propagated error.
6819 or else Etype (R) = Any_Type
6820 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6822 -- For the rather unusual case where one of the operands is
6823 -- a Raise_Expression, whose initial type is Any_Type, use
6824 -- the type of the other operand.
6826 if Nkind (L) = N_Raise_Expression then
6827 Set_Etype (L, Etype (R));
6828 Set_Etype (N, Etype (R));
6830 elsif Nkind (R) = N_Raise_Expression then
6831 Set_Etype (R, Etype (L));
6832 Set_Etype (N, Etype (L));
6837 -- We explicitly check for the case of concatenation of component
6838 -- with component to avoid reporting spurious matching array types
6839 -- that might happen to be lurking in distant packages (such as
6840 -- run-time packages). This also prevents inconsistencies in the
6841 -- messages for certain ACVC B tests, which can vary depending on
6842 -- types declared in run-time interfaces. Another improvement when
6843 -- aggregates are present is to look for a well-typed operand.
6845 elsif Present (Candidate_Type)
6846 and then (Nkind (N) /= N_Op_Concat
6847 or else Is_Array_Type (Etype (L))
6848 or else Is_Array_Type (Etype (R)))
6850 if Nkind (N) = N_Op_Concat then
6851 if Etype (L) /= Any_Composite
6852 and then Is_Array_Type (Etype (L))
6854 Candidate_Type := Etype (L);
6856 elsif Etype (R) /= Any_Composite
6857 and then Is_Array_Type (Etype (R))
6859 Candidate_Type := Etype (R);
6863 Error_Msg_NE -- CODEFIX
6864 ("operator for} is not directly visible!",
6865 N, First_Subtype (Candidate_Type));
6868 U : constant Node_Id :=
6869 Cunit (Get_Source_Unit (Candidate_Type));
6871 if Unit_Is_Visible (U) then
6872 Error_Msg_N -- CODEFIX
6873 ("use clause would make operation legal!", N);
6875 Error_Msg_NE -- CODEFIX
6876 ("add with_clause and use_clause for&!",
6877 N, Defining_Entity (Unit (U)));
6882 -- If either operand is a junk operand (e.g. package name), then
6883 -- post appropriate error messages, but do not complain further.
6885 -- Note that the use of OR in this test instead of OR ELSE is
6886 -- quite deliberate, we may as well check both operands in the
6887 -- binary operator case.
6889 elsif Junk_Operand (R)
6890 or -- really mean OR here and not OR ELSE, see above
6891 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6895 -- If we have a logical operator, one of whose operands is
6896 -- Boolean, then we know that the other operand cannot resolve to
6897 -- Boolean (since we got no interpretations), but in that case we
6898 -- pretty much know that the other operand should be Boolean, so
6899 -- resolve it that way (generating an error).
6901 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6902 if Etype (L) = Standard_Boolean then
6903 Resolve (R, Standard_Boolean);
6905 elsif Etype (R) = Standard_Boolean then
6906 Resolve (L, Standard_Boolean);
6910 -- For an arithmetic operator or comparison operator, if one
6911 -- of the operands is numeric, then we know the other operand
6912 -- is not the same numeric type. If it is a non-numeric type,
6913 -- then probably it is intended to match the other operand.
6915 elsif Nkind_In (N, N_Op_Add,
6921 Nkind_In (N, N_Op_Lt,
6927 -- If Allow_Integer_Address is active, check whether the
6928 -- operation becomes legal after converting an operand.
6930 if Is_Numeric_Type (Etype (L))
6931 and then not Is_Numeric_Type (Etype (R))
6933 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6935 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6937 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6938 Analyze_Comparison_Op (N);
6940 Analyze_Arithmetic_Op (N);
6943 Resolve (R, Etype (L));
6948 elsif Is_Numeric_Type (Etype (R))
6949 and then not Is_Numeric_Type (Etype (L))
6951 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
6953 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
6955 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6956 Analyze_Comparison_Op (N);
6958 Analyze_Arithmetic_Op (N);
6964 Resolve (L, Etype (R));
6969 elsif Allow_Integer_Address
6970 and then Is_Descendant_Of_Address (Etype (L))
6971 and then Is_Descendant_Of_Address (Etype (R))
6972 and then not Error_Posted (N)
6975 Addr_Type : constant Entity_Id := Etype (L);
6979 Unchecked_Convert_To (
6980 Standard_Integer, Relocate_Node (L)));
6982 Unchecked_Convert_To (
6983 Standard_Integer, Relocate_Node (R)));
6985 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6986 Analyze_Comparison_Op (N);
6988 Analyze_Arithmetic_Op (N);
6991 -- If this is an operand in an enclosing arithmetic
6992 -- operation, Convert the result as an address so that
6993 -- arithmetic folding of address can continue.
6995 if Nkind (Parent (N)) in N_Op then
6997 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7003 -- Under relaxed RM semantics silently replace occurrences of
7004 -- null by System.Address_Null.
7006 elsif Null_To_Null_Address_Convert_OK (N) then
7007 Replace_Null_By_Null_Address (N);
7009 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7010 Analyze_Comparison_Op (N);
7012 Analyze_Arithmetic_Op (N);
7018 -- Comparisons on A'Access are common enough to deserve a
7021 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
7022 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7023 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7026 ("two access attributes cannot be compared directly", N);
7028 ("\use qualified expression for one of the operands",
7032 -- Another one for C programmers
7034 elsif Nkind (N) = N_Op_Concat
7035 and then Valid_Boolean_Arg (Etype (L))
7036 and then Valid_Boolean_Arg (Etype (R))
7038 Error_Msg_N ("invalid operands for concatenation", N);
7039 Error_Msg_N -- CODEFIX
7040 ("\maybe AND was meant", N);
7043 -- A special case for comparison of access parameter with null
7045 elsif Nkind (N) = N_Op_Eq
7046 and then Is_Entity_Name (L)
7047 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7048 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7050 and then Nkind (R) = N_Null
7052 Error_Msg_N ("access parameter is not allowed to be null", L);
7053 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7056 -- Another special case for exponentiation, where the right
7057 -- operand must be Natural, independently of the base.
7059 elsif Nkind (N) = N_Op_Expon
7060 and then Is_Numeric_Type (Etype (L))
7061 and then not Is_Overloaded (R)
7063 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7064 and then Base_Type (Etype (R)) /= Universal_Integer
7066 if Ada_Version >= Ada_2012
7067 and then Has_Dimension_System (Etype (L))
7070 ("exponent for dimensioned type must be a rational" &
7071 ", found}", R, Etype (R));
7074 ("exponent must be of type Natural, found}", R, Etype (R));
7079 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
7080 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7082 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7083 Analyze_Equality_Op (N);
7086 -- Under relaxed RM semantics silently replace occurrences of
7087 -- null by System.Address_Null.
7089 elsif Null_To_Null_Address_Convert_OK (N) then
7090 Replace_Null_By_Null_Address (N);
7091 Analyze_Equality_Op (N);
7096 -- If we fall through then just give general message. Note that in
7097 -- the following messages, if the operand is overloaded we choose
7098 -- an arbitrary type to complain about, but that is probably more
7099 -- useful than not giving a type at all.
7101 if Nkind (N) in N_Unary_Op then
7102 Error_Msg_Node_2 := Etype (R);
7103 Error_Msg_N ("operator& not defined for}", N);
7107 if Nkind (N) in N_Binary_Op then
7108 if not Is_Overloaded (L)
7109 and then not Is_Overloaded (R)
7110 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7112 Error_Msg_Node_2 := First_Subtype (Etype (R));
7113 Error_Msg_N ("there is no applicable operator& for}", N);
7116 -- Another attempt to find a fix: one of the candidate
7117 -- interpretations may not be use-visible. This has
7118 -- already been checked for predefined operators, so
7119 -- we examine only user-defined functions.
7121 Op_Id := Get_Name_Entity_Id (Chars (N));
7123 while Present (Op_Id) loop
7124 if Ekind (Op_Id) /= E_Operator
7125 and then Is_Overloadable (Op_Id)
7127 if not Is_Immediately_Visible (Op_Id)
7128 and then not In_Use (Scope (Op_Id))
7129 and then not Is_Abstract_Subprogram (Op_Id)
7130 and then not Is_Hidden (Op_Id)
7131 and then Ekind (Scope (Op_Id)) = E_Package
7134 (L, Etype (First_Formal (Op_Id)))
7136 (Next_Formal (First_Formal (Op_Id)))
7140 Etype (Next_Formal (First_Formal (Op_Id))))
7143 ("No legal interpretation for operator&", N);
7145 ("\use clause on& would make operation legal",
7151 Op_Id := Homonym (Op_Id);
7155 Error_Msg_N ("invalid operand types for operator&", N);
7157 if Nkind (N) /= N_Op_Concat then
7158 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7159 Error_Msg_NE ("\right operand has}!", N, Etype (R));
7161 -- For concatenation operators it is more difficult to
7162 -- determine which is the wrong operand. It is worth
7163 -- flagging explicitly an access type, for those who
7164 -- might think that a dereference happens here.
7166 elsif Is_Access_Type (Etype (L)) then
7167 Error_Msg_N ("\left operand is access type", N);
7169 elsif Is_Access_Type (Etype (R)) then
7170 Error_Msg_N ("\right operand is access type", N);
7180 -----------------------------------------
7181 -- Process_Implicit_Dereference_Prefix --
7182 -----------------------------------------
7184 function Process_Implicit_Dereference_Prefix
7186 P : Entity_Id) return Entity_Id
7189 Typ : constant Entity_Id := Designated_Type (Etype (P));
7193 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7195 -- We create a dummy reference to E to ensure that the reference is
7196 -- not considered as part of an assignment (an implicit dereference
7197 -- can never assign to its prefix). The Comes_From_Source attribute
7198 -- needs to be propagated for accurate warnings.
7200 Ref := New_Occurrence_Of (E, Sloc (P));
7201 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7202 Generate_Reference (E, Ref);
7205 -- An implicit dereference is a legal occurrence of an incomplete type
7206 -- imported through a limited_with clause, if the full view is visible.
7208 if From_Limited_With (Typ)
7209 and then not From_Limited_With (Scope (Typ))
7211 (Is_Immediately_Visible (Scope (Typ))
7213 (Is_Child_Unit (Scope (Typ))
7214 and then Is_Visible_Lib_Unit (Scope (Typ))))
7216 return Available_View (Typ);
7220 end Process_Implicit_Dereference_Prefix;
7222 --------------------------------
7223 -- Remove_Abstract_Operations --
7224 --------------------------------
7226 procedure Remove_Abstract_Operations (N : Node_Id) is
7227 Abstract_Op : Entity_Id := Empty;
7228 Address_Descendant : Boolean := False;
7232 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7233 -- activate this if either extensions are enabled, or if the abstract
7234 -- operation in question comes from a predefined file. This latter test
7235 -- allows us to use abstract to make operations invisible to users. In
7236 -- particular, if type Address is non-private and abstract subprograms
7237 -- are used to hide its operators, they will be truly hidden.
7239 type Operand_Position is (First_Op, Second_Op);
7240 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
7242 procedure Remove_Address_Interpretations (Op : Operand_Position);
7243 -- Ambiguities may arise when the operands are literal and the address
7244 -- operations in s-auxdec are visible. In that case, remove the
7245 -- interpretation of a literal as Address, to retain the semantics
7246 -- of Address as a private type.
7248 ------------------------------------
7249 -- Remove_Address_Interpretations --
7250 ------------------------------------
7252 procedure Remove_Address_Interpretations (Op : Operand_Position) is
7256 if Is_Overloaded (N) then
7257 Get_First_Interp (N, I, It);
7258 while Present (It.Nam) loop
7259 Formal := First_Entity (It.Nam);
7261 if Op = Second_Op then
7262 Formal := Next_Entity (Formal);
7265 if Is_Descendant_Of_Address (Etype (Formal)) then
7266 Address_Descendant := True;
7270 Get_Next_Interp (I, It);
7273 end Remove_Address_Interpretations;
7275 -- Start of processing for Remove_Abstract_Operations
7278 if Is_Overloaded (N) then
7279 if Debug_Flag_V then
7280 Write_Str ("Remove_Abstract_Operations: ");
7281 Write_Overloads (N);
7284 Get_First_Interp (N, I, It);
7286 while Present (It.Nam) loop
7287 if Is_Overloadable (It.Nam)
7288 and then Is_Abstract_Subprogram (It.Nam)
7289 and then not Is_Dispatching_Operation (It.Nam)
7291 Abstract_Op := It.Nam;
7293 if Is_Descendant_Of_Address (It.Typ) then
7294 Address_Descendant := True;
7298 -- In Ada 2005, this operation does not participate in overload
7299 -- resolution. If the operation is defined in a predefined
7300 -- unit, it is one of the operations declared abstract in some
7301 -- variants of System, and it must be removed as well.
7303 elsif Ada_Version >= Ada_2005
7304 or else Is_Predefined_File_Name
7305 (Unit_File_Name (Get_Source_Unit (It.Nam)))
7312 Get_Next_Interp (I, It);
7315 if No (Abstract_Op) then
7317 -- If some interpretation yields an integer type, it is still
7318 -- possible that there are address interpretations. Remove them
7319 -- if one operand is a literal, to avoid spurious ambiguities
7320 -- on systems where Address is a visible integer type.
7322 if Is_Overloaded (N)
7323 and then Nkind (N) in N_Op
7324 and then Is_Integer_Type (Etype (N))
7326 if Nkind (N) in N_Binary_Op then
7327 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7328 Remove_Address_Interpretations (Second_Op);
7330 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
7331 Remove_Address_Interpretations (First_Op);
7336 elsif Nkind (N) in N_Op then
7338 -- Remove interpretations that treat literals as addresses. This
7339 -- is never appropriate, even when Address is defined as a visible
7340 -- Integer type. The reason is that we would really prefer Address
7341 -- to behave as a private type, even in this case. If Address is a
7342 -- visible integer type, we get lots of overload ambiguities.
7344 if Nkind (N) in N_Binary_Op then
7346 U1 : constant Boolean :=
7347 Present (Universal_Interpretation (Right_Opnd (N)));
7348 U2 : constant Boolean :=
7349 Present (Universal_Interpretation (Left_Opnd (N)));
7353 Remove_Address_Interpretations (Second_Op);
7357 Remove_Address_Interpretations (First_Op);
7360 if not (U1 and U2) then
7362 -- Remove corresponding predefined operator, which is
7363 -- always added to the overload set.
7365 Get_First_Interp (N, I, It);
7366 while Present (It.Nam) loop
7367 if Scope (It.Nam) = Standard_Standard
7368 and then Base_Type (It.Typ) =
7369 Base_Type (Etype (Abstract_Op))
7374 Get_Next_Interp (I, It);
7377 elsif Is_Overloaded (N)
7378 and then Present (Univ_Type)
7380 -- If both operands have a universal interpretation,
7381 -- it is still necessary to remove interpretations that
7382 -- yield Address. Any remaining ambiguities will be
7383 -- removed in Disambiguate.
7385 Get_First_Interp (N, I, It);
7386 while Present (It.Nam) loop
7387 if Is_Descendant_Of_Address (It.Typ) then
7390 elsif not Is_Type (It.Nam) then
7391 Set_Entity (N, It.Nam);
7394 Get_Next_Interp (I, It);
7400 elsif Nkind (N) = N_Function_Call
7402 (Nkind (Name (N)) = N_Operator_Symbol
7404 (Nkind (Name (N)) = N_Expanded_Name
7406 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7410 Arg1 : constant Node_Id := First (Parameter_Associations (N));
7411 U1 : constant Boolean :=
7412 Present (Universal_Interpretation (Arg1));
7413 U2 : constant Boolean :=
7414 Present (Next (Arg1)) and then
7415 Present (Universal_Interpretation (Next (Arg1)));
7419 Remove_Address_Interpretations (First_Op);
7423 Remove_Address_Interpretations (Second_Op);
7426 if not (U1 and U2) then
7427 Get_First_Interp (N, I, It);
7428 while Present (It.Nam) loop
7429 if Scope (It.Nam) = Standard_Standard
7430 and then It.Typ = Base_Type (Etype (Abstract_Op))
7435 Get_Next_Interp (I, It);
7441 -- If the removal has left no valid interpretations, emit an error
7442 -- message now and label node as illegal.
7444 if Present (Abstract_Op) then
7445 Get_First_Interp (N, I, It);
7449 -- Removal of abstract operation left no viable candidate
7451 Set_Etype (N, Any_Type);
7452 Error_Msg_Sloc := Sloc (Abstract_Op);
7454 ("cannot call abstract operation& declared#", N, Abstract_Op);
7456 -- In Ada 2005, an abstract operation may disable predefined
7457 -- operators. Since the context is not yet known, we mark the
7458 -- predefined operators as potentially hidden. Do not include
7459 -- predefined operators when addresses are involved since this
7460 -- case is handled separately.
7462 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
7463 while Present (It.Nam) loop
7464 if Is_Numeric_Type (It.Typ)
7465 and then Scope (It.Typ) = Standard_Standard
7467 Set_Abstract_Op (I, Abstract_Op);
7470 Get_Next_Interp (I, It);
7475 if Debug_Flag_V then
7476 Write_Str ("Remove_Abstract_Operations done: ");
7477 Write_Overloads (N);
7480 end Remove_Abstract_Operations;
7482 ----------------------------
7483 -- Try_Container_Indexing --
7484 ----------------------------
7486 function Try_Container_Indexing
7489 Exprs : List_Id) return Boolean
7491 Pref_Typ : constant Entity_Id := Etype (Prefix);
7493 function Constant_Indexing_OK return Boolean;
7494 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7495 -- for the type, or else node not a target of assignment, or an actual
7496 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7498 function Find_Indexing_Operations
7501 Is_Constant : Boolean) return Node_Id;
7502 -- Return a reference to the primitive operation of type T denoted by
7503 -- name Nam. If the operation is overloaded, the reference carries all
7504 -- interpretations. Flag Is_Constant should be set when the context is
7505 -- constant indexing.
7507 --------------------------
7508 -- Constant_Indexing_OK --
7509 --------------------------
7511 function Constant_Indexing_OK return Boolean is
7515 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
7518 elsif not Is_Variable (Prefix) then
7523 while Present (Par) loop
7524 if Nkind (Parent (Par)) = N_Assignment_Statement
7525 and then Par = Name (Parent (Par))
7529 -- The call may be overloaded, in which case we assume that its
7530 -- resolution does not depend on the type of the parameter that
7531 -- includes the indexing operation.
7533 elsif Nkind_In (Parent (Par), N_Function_Call,
7534 N_Procedure_Call_Statement)
7535 and then Is_Entity_Name (Name (Parent (Par)))
7543 -- We should look for an interpretation with the proper
7544 -- number of formals, and determine whether it is an
7545 -- In_Parameter, but for now we examine the formal that
7546 -- corresponds to the indexing, and assume that variable
7547 -- indexing is required if some interpretation has an
7548 -- assignable formal at that position. Still does not
7549 -- cover the most complex cases ???
7551 if Is_Overloaded (Name (Parent (Par))) then
7553 Proc : constant Node_Id := Name (Parent (Par));
7560 Get_First_Interp (Proc, I, It);
7561 while Present (It.Nam) loop
7562 F := First_Formal (It.Nam);
7563 A := First (Parameter_Associations (Parent (Par)));
7565 while Present (F) and then Present (A) loop
7567 if Ekind (F) /= E_In_Parameter then
7570 exit; -- interpretation is safe
7578 Get_Next_Interp (I, It);
7585 Proc := Entity (Name (Parent (Par)));
7587 -- If this is an indirect call, get formals from
7590 if Is_Access_Subprogram_Type (Etype (Proc)) then
7591 Proc := Designated_Type (Etype (Proc));
7595 Formal := First_Formal (Proc);
7596 Actual := First_Actual (Parent (Par));
7598 -- Find corresponding actual
7600 while Present (Actual) loop
7601 exit when Actual = Par;
7602 Next_Actual (Actual);
7604 if Present (Formal) then
7605 Next_Formal (Formal);
7607 -- Otherwise this is a parameter mismatch, the error is
7608 -- reported elsewhere.
7615 return Ekind (Formal) = E_In_Parameter;
7618 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7621 -- If the indexed component is a prefix it may be the first actual
7622 -- of a prefixed call. Retrieve the called entity, if any, and
7623 -- check its first formal. Determine if the context is a procedure
7624 -- or function call.
7626 elsif Nkind (Parent (Par)) = N_Selected_Component then
7628 Sel : constant Node_Id := Selector_Name (Parent (Par));
7629 Nam : constant Entity_Id := Current_Entity (Sel);
7632 if Present (Nam) and then Is_Overloadable (Nam) then
7633 if Nkind (Parent (Parent (Par))) =
7634 N_Procedure_Call_Statement
7638 elsif Ekind (Nam) = E_Function
7639 and then Present (First_Formal (Nam))
7641 return Ekind (First_Formal (Nam)) = E_In_Parameter;
7646 elsif Nkind (Par) in N_Op then
7650 Par := Parent (Par);
7653 -- In all other cases, constant indexing is legal
7656 end Constant_Indexing_OK;
7658 ------------------------------
7659 -- Find_Indexing_Operations --
7660 ------------------------------
7662 function Find_Indexing_Operations
7665 Is_Constant : Boolean) return Node_Id
7667 procedure Inspect_Declarations
7669 Ref : in out Node_Id);
7670 -- Traverse the declarative list where type Typ resides and collect
7671 -- all suitable interpretations in node Ref.
7673 procedure Inspect_Primitives
7675 Ref : in out Node_Id);
7676 -- Traverse the list of primitive operations of type Typ and collect
7677 -- all suitable interpretations in node Ref.
7679 function Is_OK_Candidate
7680 (Subp_Id : Entity_Id;
7681 Typ : Entity_Id) return Boolean;
7682 -- Determine whether subprogram Subp_Id is a suitable indexing
7683 -- operation for type Typ. To qualify as such, the subprogram must
7684 -- be a function, have at least two parameters, and the type of the
7685 -- first parameter must be either Typ, or Typ'Class, or access [to
7686 -- constant] with designated type Typ or Typ'Class.
7688 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7689 -- Store subprogram Subp_Id as an interpretation in node Ref
7691 --------------------------
7692 -- Inspect_Declarations --
7693 --------------------------
7695 procedure Inspect_Declarations
7697 Ref : in out Node_Id)
7699 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
7701 Subp_Id : Entity_Id;
7704 -- Ensure that the routine is not called with itypes, which lack a
7705 -- declarative node.
7707 pragma Assert (Present (Typ_Decl));
7708 pragma Assert (Is_List_Member (Typ_Decl));
7710 Decl := First (List_Containing (Typ_Decl));
7711 while Present (Decl) loop
7712 if Nkind (Decl) = N_Subprogram_Declaration then
7713 Subp_Id := Defining_Entity (Decl);
7715 if Is_OK_Candidate (Subp_Id, Typ) then
7716 Record_Interp (Subp_Id, Ref);
7722 end Inspect_Declarations;
7724 ------------------------
7725 -- Inspect_Primitives --
7726 ------------------------
7728 procedure Inspect_Primitives
7730 Ref : in out Node_Id)
7732 Prim_Elmt : Elmt_Id;
7733 Prim_Id : Entity_Id;
7736 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
7737 while Present (Prim_Elmt) loop
7738 Prim_Id := Node (Prim_Elmt);
7740 if Is_OK_Candidate (Prim_Id, Typ) then
7741 Record_Interp (Prim_Id, Ref);
7744 Next_Elmt (Prim_Elmt);
7746 end Inspect_Primitives;
7748 ---------------------
7749 -- Is_OK_Candidate --
7750 ---------------------
7752 function Is_OK_Candidate
7753 (Subp_Id : Entity_Id;
7754 Typ : Entity_Id) return Boolean
7757 Formal_Typ : Entity_Id;
7758 Param_Typ : Node_Id;
7761 -- To classify as a suitable candidate, the subprogram must be a
7762 -- function whose name matches the argument of aspect Constant or
7763 -- Variable_Indexing.
7765 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
7766 Formal := First_Formal (Subp_Id);
7768 -- The candidate requires at least two parameters
7770 if Present (Formal) and then Present (Next_Formal (Formal)) then
7771 Formal_Typ := Empty;
7772 Param_Typ := Parameter_Type (Parent (Formal));
7774 -- Use the designated type when the first parameter is of an
7777 if Nkind (Param_Typ) = N_Access_Definition
7778 and then Present (Subtype_Mark (Param_Typ))
7780 -- When the context is a constant indexing, the access
7781 -- definition must be access-to-constant. This does not
7782 -- apply to variable indexing.
7785 or else Constant_Present (Param_Typ)
7787 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
7790 -- Otherwise use the parameter type
7793 Formal_Typ := Etype (Param_Typ);
7796 if Present (Formal_Typ) then
7798 -- Use the specific type when the parameter type is
7801 if Is_Class_Wide_Type (Formal_Typ) then
7802 Formal_Typ := Etype (Base_Type (Formal_Typ));
7805 -- Use the full view when the parameter type is private
7808 if Is_Incomplete_Or_Private_Type (Formal_Typ)
7809 and then Present (Full_View (Formal_Typ))
7811 Formal_Typ := Full_View (Formal_Typ);
7814 -- The type of the first parameter must denote the type
7815 -- of the container or acts as its ancestor type.
7819 or else Is_Ancestor (Formal_Typ, Typ);
7825 end Is_OK_Candidate;
7831 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
7833 if Present (Ref) then
7834 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
7836 -- Otherwise this is the first interpretation. Create a reference
7837 -- where all remaining interpretations will be collected.
7840 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
7849 -- Start of processing for Find_Indexing_Operations
7854 -- Use the specific type when the parameter type is class-wide
7856 if Is_Class_Wide_Type (Typ) then
7857 Typ := Root_Type (Typ);
7861 Typ := Underlying_Type (Base_Type (Typ));
7863 Inspect_Primitives (Typ, Ref);
7865 -- Now look for explicit declarations of an indexing operation.
7866 -- If the type is private the operation may be declared in the
7867 -- visible part that contains the partial view.
7869 if Is_Private_Type (T) then
7870 Inspect_Declarations (T, Ref);
7873 Inspect_Declarations (Typ, Ref);
7876 end Find_Indexing_Operations;
7880 Loc : constant Source_Ptr := Sloc (N);
7884 Func_Name : Node_Id;
7887 Is_Constant_Indexing : Boolean := False;
7888 -- This flag reflects the nature of the container indexing. Note that
7889 -- the context may be suited for constant indexing, but the type may
7890 -- lack a Constant_Indexing annotation.
7892 -- Start of processing for Try_Container_Indexing
7895 -- Node may have been analyzed already when testing for a prefixed
7896 -- call, in which case do not redo analysis.
7898 if Present (Generalized_Indexing (N)) then
7904 -- If indexing a class-wide container, obtain indexing primitive from
7907 if Is_Class_Wide_Type (C_Type) then
7908 C_Type := Etype (Base_Type (C_Type));
7911 -- Check whether the type has a specified indexing aspect
7915 -- The context is suitable for constant indexing, so obtain the name of
7916 -- the indexing function from aspect Constant_Indexing.
7918 if Constant_Indexing_OK then
7920 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
7923 if Present (Func_Name) then
7924 Is_Constant_Indexing := True;
7926 -- Otherwise attempt variable indexing
7930 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
7933 -- The type is not subject to either form of indexing, therefore the
7934 -- indexed component does not denote container indexing. If this is a
7935 -- true error, it is diagnosed by the caller.
7937 if No (Func_Name) then
7939 -- The prefix itself may be an indexing of a container. Rewrite it
7940 -- as such and retry.
7942 if Has_Implicit_Dereference (Pref_Typ) then
7943 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
7944 return Try_Container_Indexing (N, Prefix, Exprs);
7946 -- Otherwise this is definitely not container indexing
7952 -- If the container type is derived from another container type, the
7953 -- value of the inherited aspect is the Reference operation declared
7954 -- for the parent type.
7956 -- However, Reference is also a primitive operation of the type, and the
7957 -- inherited operation has a different signature. We retrieve the right
7958 -- ones (the function may be overloaded) from the list of primitive
7959 -- operations of the derived type.
7961 -- Note that predefined containers are typically all derived from one of
7962 -- the Controlled types. The code below is motivated by containers that
7963 -- are derived from other types with a Reference aspect.
7965 elsif Is_Derived_Type (C_Type)
7966 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
7969 Find_Indexing_Operations
7971 Nam => Chars (Func_Name),
7972 Is_Constant => Is_Constant_Indexing);
7975 Assoc := New_List (Relocate_Node (Prefix));
7977 -- A generalized indexing may have nore than one index expression, so
7978 -- transfer all of them to the argument list to be used in the call.
7979 -- Note that there may be named associations, in which case the node
7980 -- was rewritten earlier as a call, and has been transformed back into
7981 -- an indexed expression to share the following processing.
7983 -- The generalized indexing node is the one on which analysis and
7984 -- resolution take place. Before expansion the original node is replaced
7985 -- with the generalized indexing node, which is a call, possibly with a
7986 -- dereference operation.
7988 if Comes_From_Source (N) then
7989 Check_Compiler_Unit ("generalized indexing", N);
7992 -- Create argument list for function call that represents generalized
7993 -- indexing. Note that indices (i.e. actuals) may themselves be
8001 Arg := First (Exprs);
8002 while Present (Arg) loop
8003 New_Arg := Relocate_Node (Arg);
8005 -- The arguments can be parameter associations, in which case the
8006 -- explicit actual parameter carries the overloadings.
8008 if Nkind (New_Arg) /= N_Parameter_Association then
8009 Save_Interps (Arg, New_Arg);
8012 Append (New_Arg, Assoc);
8017 if not Is_Overloaded (Func_Name) then
8018 Func := Entity (Func_Name);
8021 Make_Function_Call (Loc,
8022 Name => New_Occurrence_Of (Func, Loc),
8023 Parameter_Associations => Assoc);
8025 Set_Parent (Indexing, Parent (N));
8026 Set_Generalized_Indexing (N, Indexing);
8028 Set_Etype (N, Etype (Indexing));
8030 -- If the return type of the indexing function is a reference type,
8031 -- add the dereference as a possible interpretation. Note that the
8032 -- indexing aspect may be a function that returns the element type
8033 -- with no intervening implicit dereference, and that the reference
8034 -- discriminant is not the first discriminant.
8036 if Has_Discriminants (Etype (Func)) then
8037 Check_Implicit_Dereference (N, Etype (Func));
8041 -- If there are multiple indexing functions, build a function call
8042 -- and analyze it for each of the possible interpretations.
8045 Make_Function_Call (Loc,
8047 Make_Identifier (Loc, Chars (Func_Name)),
8048 Parameter_Associations => Assoc);
8049 Set_Parent (Indexing, Parent (N));
8050 Set_Generalized_Indexing (N, Indexing);
8051 Set_Etype (N, Any_Type);
8052 Set_Etype (Name (Indexing), Any_Type);
8060 Get_First_Interp (Func_Name, I, It);
8061 Set_Etype (Indexing, Any_Type);
8063 -- Analyze each candidate function with the given actuals
8065 while Present (It.Nam) loop
8066 Analyze_One_Call (Indexing, It.Nam, False, Success);
8067 Get_Next_Interp (I, It);
8070 -- If there are several successful candidates, resolution will
8071 -- be by result. Mark the interpretations of the function name
8074 if Is_Overloaded (Indexing) then
8075 Get_First_Interp (Indexing, I, It);
8077 while Present (It.Nam) loop
8078 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
8079 Get_Next_Interp (I, It);
8083 Set_Etype (Name (Indexing), Etype (Indexing));
8086 -- Now add the candidate interpretations to the indexing node
8087 -- itself, to be replaced later by the function call.
8089 if Is_Overloaded (Name (Indexing)) then
8090 Get_First_Interp (Name (Indexing), I, It);
8092 while Present (It.Nam) loop
8093 Add_One_Interp (N, It.Nam, It.Typ);
8095 -- Add dereference interpretation if the result type has
8096 -- implicit reference discriminants.
8098 if Has_Discriminants (Etype (It.Nam)) then
8099 Check_Implicit_Dereference (N, Etype (It.Nam));
8102 Get_Next_Interp (I, It);
8106 Set_Etype (N, Etype (Name (Indexing)));
8107 if Has_Discriminants (Etype (N)) then
8108 Check_Implicit_Dereference (N, Etype (N));
8114 if Etype (Indexing) = Any_Type then
8116 ("container cannot be indexed with&", N, Etype (First (Exprs)));
8117 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
8121 end Try_Container_Indexing;
8123 -----------------------
8124 -- Try_Indirect_Call --
8125 -----------------------
8127 function Try_Indirect_Call
8130 Typ : Entity_Id) return Boolean
8136 pragma Warnings (Off, Call_OK);
8139 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
8141 Actual := First_Actual (N);
8142 Formal := First_Formal (Designated_Type (Typ));
8143 while Present (Actual) and then Present (Formal) loop
8144 if not Has_Compatible_Type (Actual, Etype (Formal)) then
8149 Next_Formal (Formal);
8152 if No (Actual) and then No (Formal) then
8153 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
8155 -- Nam is a candidate interpretation for the name in the call,
8156 -- if it is not an indirect call.
8158 if not Is_Type (Nam)
8159 and then Is_Entity_Name (Name (N))
8161 Set_Entity (Name (N), Nam);
8169 end Try_Indirect_Call;
8171 ----------------------
8172 -- Try_Indexed_Call --
8173 ----------------------
8175 function Try_Indexed_Call
8179 Skip_First : Boolean) return Boolean
8181 Loc : constant Source_Ptr := Sloc (N);
8182 Actuals : constant List_Id := Parameter_Associations (N);
8187 Actual := First (Actuals);
8189 -- If the call was originally written in prefix form, skip the first
8190 -- actual, which is obviously not defaulted.
8196 Index := First_Index (Typ);
8197 while Present (Actual) and then Present (Index) loop
8199 -- If the parameter list has a named association, the expression
8200 -- is definitely a call and not an indexed component.
8202 if Nkind (Actual) = N_Parameter_Association then
8206 if Is_Entity_Name (Actual)
8207 and then Is_Type (Entity (Actual))
8208 and then No (Next (Actual))
8210 -- A single actual that is a type name indicates a slice if the
8211 -- type is discrete, and an error otherwise.
8213 if Is_Discrete_Type (Entity (Actual)) then
8217 Make_Function_Call (Loc,
8218 Name => Relocate_Node (Name (N))),
8220 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8225 Error_Msg_N ("invalid use of type in expression", Actual);
8226 Set_Etype (N, Any_Type);
8231 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
8239 if No (Actual) and then No (Index) then
8240 Add_One_Interp (N, Nam, Component_Type (Typ));
8242 -- Nam is a candidate interpretation for the name in the call,
8243 -- if it is not an indirect call.
8245 if not Is_Type (Nam)
8246 and then Is_Entity_Name (Name (N))
8248 Set_Entity (Name (N), Nam);
8255 end Try_Indexed_Call;
8257 --------------------------
8258 -- Try_Object_Operation --
8259 --------------------------
8261 function Try_Object_Operation
8262 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8264 K : constant Node_Kind := Nkind (Parent (N));
8265 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
8266 Loc : constant Source_Ptr := Sloc (N);
8267 Obj : constant Node_Id := Prefix (N);
8269 Subprog : constant Node_Id :=
8270 Make_Identifier (Sloc (Selector_Name (N)),
8271 Chars => Chars (Selector_Name (N)));
8272 -- Identifier on which possible interpretations will be collected
8274 Report_Error : Boolean := False;
8275 -- If no candidate interpretation matches the context, redo analysis
8276 -- with Report_Error True to provide additional information.
8279 Candidate : Entity_Id := Empty;
8280 New_Call_Node : Node_Id := Empty;
8281 Node_To_Replace : Node_Id;
8282 Obj_Type : Entity_Id := Etype (Obj);
8283 Success : Boolean := False;
8285 function Valid_Candidate
8288 Subp : Entity_Id) return Entity_Id;
8289 -- If the subprogram is a valid interpretation, record it, and add
8290 -- to the list of interpretations of Subprog. Otherwise return Empty.
8292 procedure Complete_Object_Operation
8293 (Call_Node : Node_Id;
8294 Node_To_Replace : Node_Id);
8295 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8296 -- Call_Node, insert the object (or its dereference) as the first actual
8297 -- in the call, and complete the analysis of the call.
8299 procedure Report_Ambiguity (Op : Entity_Id);
8300 -- If a prefixed procedure call is ambiguous, indicate whether the
8301 -- call includes an implicit dereference or an implicit 'Access.
8303 procedure Transform_Object_Operation
8304 (Call_Node : out Node_Id;
8305 Node_To_Replace : out Node_Id);
8306 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
8307 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8308 -- either N or the parent of N, and Subprog is a reference to the
8309 -- subprogram we are trying to match.
8311 function Try_Class_Wide_Operation
8312 (Call_Node : Node_Id;
8313 Node_To_Replace : Node_Id) return Boolean;
8314 -- Traverse all ancestor types looking for a class-wide subprogram
8315 -- for which the current operation is a valid non-dispatching call.
8317 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8318 -- If prefix is overloaded, its interpretation may include different
8319 -- tagged types, and we must examine the primitive operations and
8320 -- the class-wide operations of each in order to find candidate
8321 -- interpretations for the call as a whole.
8323 function Try_Primitive_Operation
8324 (Call_Node : Node_Id;
8325 Node_To_Replace : Node_Id) return Boolean;
8326 -- Traverse the list of primitive subprograms looking for a dispatching
8327 -- operation for which the current node is a valid call .
8329 ---------------------
8330 -- Valid_Candidate --
8331 ---------------------
8333 function Valid_Candidate
8336 Subp : Entity_Id) return Entity_Id
8338 Arr_Type : Entity_Id;
8339 Comp_Type : Entity_Id;
8342 -- If the subprogram is a valid interpretation, record it in global
8343 -- variable Subprog, to collect all possible overloadings.
8346 if Subp /= Entity (Subprog) then
8347 Add_One_Interp (Subprog, Subp, Etype (Subp));
8351 -- If the call may be an indexed call, retrieve component type of
8352 -- resulting expression, and add possible interpretation.
8357 if Nkind (Call) = N_Function_Call
8358 and then Nkind (Parent (N)) = N_Indexed_Component
8359 and then Needs_One_Actual (Subp)
8361 if Is_Array_Type (Etype (Subp)) then
8362 Arr_Type := Etype (Subp);
8364 elsif Is_Access_Type (Etype (Subp))
8365 and then Is_Array_Type (Designated_Type (Etype (Subp)))
8367 Arr_Type := Designated_Type (Etype (Subp));
8371 if Present (Arr_Type) then
8373 -- Verify that the actuals (excluding the object) match the types
8381 Actual := Next (First_Actual (Call));
8382 Index := First_Index (Arr_Type);
8383 while Present (Actual) and then Present (Index) loop
8384 if not Has_Compatible_Type (Actual, Etype (Index)) then
8389 Next_Actual (Actual);
8395 and then Present (Arr_Type)
8397 Comp_Type := Component_Type (Arr_Type);
8401 if Present (Comp_Type)
8402 and then Etype (Subprog) /= Comp_Type
8404 Add_One_Interp (Subprog, Subp, Comp_Type);
8408 if Etype (Call) /= Any_Type then
8413 end Valid_Candidate;
8415 -------------------------------
8416 -- Complete_Object_Operation --
8417 -------------------------------
8419 procedure Complete_Object_Operation
8420 (Call_Node : Node_Id;
8421 Node_To_Replace : Node_Id)
8423 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8424 Formal_Type : constant Entity_Id := Etype (Control);
8425 First_Actual : Node_Id;
8428 -- Place the name of the operation, with its interpretations,
8429 -- on the rewritten call.
8431 Set_Name (Call_Node, Subprog);
8433 First_Actual := First (Parameter_Associations (Call_Node));
8435 -- For cross-reference purposes, treat the new node as being in the
8436 -- source if the original one is. Set entity and type, even though
8437 -- they may be overwritten during resolution if overloaded.
8439 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8440 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8442 if Nkind (N) = N_Selected_Component
8443 and then not Inside_A_Generic
8445 Set_Entity (Selector_Name (N), Entity (Subprog));
8446 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
8449 -- If need be, rewrite first actual as an explicit dereference. If
8450 -- the call is overloaded, the rewriting can only be done once the
8451 -- primitive operation is identified.
8453 if Is_Overloaded (Subprog) then
8455 -- The prefix itself may be overloaded, and its interpretations
8456 -- must be propagated to the new actual in the call.
8458 if Is_Overloaded (Obj) then
8459 Save_Interps (Obj, First_Actual);
8462 Rewrite (First_Actual, Obj);
8464 elsif not Is_Access_Type (Formal_Type)
8465 and then Is_Access_Type (Etype (Obj))
8467 Rewrite (First_Actual,
8468 Make_Explicit_Dereference (Sloc (Obj), Obj));
8469 Analyze (First_Actual);
8471 -- If we need to introduce an explicit dereference, verify that
8472 -- the resulting actual is compatible with the mode of the formal.
8474 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8475 and then Is_Access_Constant (Etype (Obj))
8478 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8481 -- Conversely, if the formal is an access parameter and the object
8482 -- is not, replace the actual with a 'Access reference. Its analysis
8483 -- will check that the object is aliased.
8485 elsif Is_Access_Type (Formal_Type)
8486 and then not Is_Access_Type (Etype (Obj))
8488 -- A special case: A.all'access is illegal if A is an access to a
8489 -- constant and the context requires an access to a variable.
8491 if not Is_Access_Constant (Formal_Type) then
8492 if (Nkind (Obj) = N_Explicit_Dereference
8493 and then Is_Access_Constant (Etype (Prefix (Obj))))
8494 or else not Is_Variable (Obj)
8497 ("actual for & must be a variable", Obj, Control);
8501 Rewrite (First_Actual,
8502 Make_Attribute_Reference (Loc,
8503 Attribute_Name => Name_Access,
8504 Prefix => Relocate_Node (Obj)));
8506 if not Is_Aliased_View (Obj) then
8508 ("object in prefixed call to & must be aliased "
8509 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
8512 Analyze (First_Actual);
8515 if Is_Overloaded (Obj) then
8516 Save_Interps (Obj, First_Actual);
8519 Rewrite (First_Actual, Obj);
8522 -- The operation is obtained from the dispatch table and not by
8523 -- visibility, and may be declared in a unit that is not explicitly
8524 -- referenced in the source, but is nevertheless required in the
8525 -- context of the current unit. Indicate that operation and its scope
8526 -- are referenced, to prevent spurious and misleading warnings. If
8527 -- the operation is overloaded, all primitives are in the same scope
8528 -- and we can use any of them.
8530 Set_Referenced (Entity (Subprog), True);
8531 Set_Referenced (Scope (Entity (Subprog)), True);
8533 Rewrite (Node_To_Replace, Call_Node);
8535 -- Propagate the interpretations collected in subprog to the new
8536 -- function call node, to be resolved from context.
8538 if Is_Overloaded (Subprog) then
8539 Save_Interps (Subprog, Node_To_Replace);
8542 -- The type of the subprogram may be a limited view obtained
8543 -- transitively from another unit. If full view is available,
8544 -- use it to analyze call.
8547 T : constant Entity_Id := Etype (Subprog);
8549 if From_Limited_With (T) then
8550 Set_Etype (Entity (Subprog), Available_View (T));
8554 Analyze (Node_To_Replace);
8556 -- If the operation has been rewritten into a call, which may get
8557 -- subsequently an explicit dereference, preserve the type on the
8558 -- original node (selected component or indexed component) for
8559 -- subsequent legality tests, e.g. Is_Variable. which examines
8560 -- the original node.
8562 if Nkind (Node_To_Replace) = N_Function_Call then
8564 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8567 end Complete_Object_Operation;
8569 ----------------------
8570 -- Report_Ambiguity --
8571 ----------------------
8573 procedure Report_Ambiguity (Op : Entity_Id) is
8574 Access_Actual : constant Boolean :=
8575 Is_Access_Type (Etype (Prefix (N)));
8576 Access_Formal : Boolean := False;
8579 Error_Msg_Sloc := Sloc (Op);
8581 if Present (First_Formal (Op)) then
8582 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8585 if Access_Formal and then not Access_Actual then
8586 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8588 ("\possible interpretation "
8589 & "(inherited, with implicit 'Access) #", N);
8592 ("\possible interpretation (with implicit 'Access) #", N);
8595 elsif not Access_Formal and then Access_Actual then
8596 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8598 ("\possible interpretation "
8599 & "(inherited, with implicit dereference) #", N);
8602 ("\possible interpretation (with implicit dereference) #", N);
8606 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8607 Error_Msg_N ("\possible interpretation (inherited)#", N);
8609 Error_Msg_N -- CODEFIX
8610 ("\possible interpretation#", N);
8613 end Report_Ambiguity;
8615 --------------------------------
8616 -- Transform_Object_Operation --
8617 --------------------------------
8619 procedure Transform_Object_Operation
8620 (Call_Node : out Node_Id;
8621 Node_To_Replace : out Node_Id)
8623 Dummy : constant Node_Id := New_Copy (Obj);
8624 -- Placeholder used as a first parameter in the call, replaced
8625 -- eventually by the proper object.
8627 Parent_Node : constant Node_Id := Parent (N);
8633 -- Common case covering 1) Call to a procedure and 2) Call to a
8634 -- function that has some additional actuals.
8636 if Nkind (Parent_Node) in N_Subprogram_Call
8638 -- N is a selected component node containing the name of the
8639 -- subprogram. If N is not the name of the parent node we must
8640 -- not replace the parent node by the new construct. This case
8641 -- occurs when N is a parameterless call to a subprogram that
8642 -- is an actual parameter of a call to another subprogram. For
8644 -- Some_Subprogram (..., Obj.Operation, ...)
8646 and then Name (Parent_Node) = N
8648 Node_To_Replace := Parent_Node;
8650 Actuals := Parameter_Associations (Parent_Node);
8652 if Present (Actuals) then
8653 Prepend (Dummy, Actuals);
8655 Actuals := New_List (Dummy);
8658 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8660 Make_Procedure_Call_Statement (Loc,
8661 Name => New_Copy (Subprog),
8662 Parameter_Associations => Actuals);
8666 Make_Function_Call (Loc,
8667 Name => New_Copy (Subprog),
8668 Parameter_Associations => Actuals);
8671 -- Before analysis, a function call appears as an indexed component
8672 -- if there are no named associations.
8674 elsif Nkind (Parent_Node) = N_Indexed_Component
8675 and then N = Prefix (Parent_Node)
8677 Node_To_Replace := Parent_Node;
8678 Actuals := Expressions (Parent_Node);
8680 Actual := First (Actuals);
8681 while Present (Actual) loop
8686 Prepend (Dummy, Actuals);
8689 Make_Function_Call (Loc,
8690 Name => New_Copy (Subprog),
8691 Parameter_Associations => Actuals);
8693 -- Parameterless call: Obj.F is rewritten as F (Obj)
8696 Node_To_Replace := N;
8699 Make_Function_Call (Loc,
8700 Name => New_Copy (Subprog),
8701 Parameter_Associations => New_List (Dummy));
8703 end Transform_Object_Operation;
8705 ------------------------------
8706 -- Try_Class_Wide_Operation --
8707 ------------------------------
8709 function Try_Class_Wide_Operation
8710 (Call_Node : Node_Id;
8711 Node_To_Replace : Node_Id) return Boolean
8713 Anc_Type : Entity_Id;
8714 Matching_Op : Entity_Id := Empty;
8717 procedure Traverse_Homonyms
8718 (Anc_Type : Entity_Id;
8719 Error : out Boolean);
8720 -- Traverse the homonym chain of the subprogram searching for those
8721 -- homonyms whose first formal has the Anc_Type's class-wide type,
8722 -- or an anonymous access type designating the class-wide type. If
8723 -- an ambiguity is detected, then Error is set to True.
8725 procedure Traverse_Interfaces
8726 (Anc_Type : Entity_Id;
8727 Error : out Boolean);
8728 -- Traverse the list of interfaces, if any, associated with Anc_Type
8729 -- and search for acceptable class-wide homonyms associated with each
8730 -- interface. If an ambiguity is detected, then Error is set to True.
8732 -----------------------
8733 -- Traverse_Homonyms --
8734 -----------------------
8736 procedure Traverse_Homonyms
8737 (Anc_Type : Entity_Id;
8738 Error : out Boolean)
8740 Cls_Type : Entity_Id;
8748 Cls_Type := Class_Wide_Type (Anc_Type);
8750 Hom := Current_Entity (Subprog);
8752 -- Find a non-hidden operation whose first parameter is of the
8753 -- class-wide type, a subtype thereof, or an anonymous access
8754 -- to same. If in an instance, the operation can be considered
8755 -- even if hidden (it may be hidden because the instantiation
8756 -- is expanded after the containing package has been analyzed).
8758 while Present (Hom) loop
8759 if Ekind_In (Hom, E_Procedure, E_Function)
8760 and then (not Is_Hidden (Hom) or else In_Instance)
8761 and then Scope (Hom) = Scope (Anc_Type)
8762 and then Present (First_Formal (Hom))
8764 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
8766 (Is_Access_Type (Etype (First_Formal (Hom)))
8768 Ekind (Etype (First_Formal (Hom))) =
8769 E_Anonymous_Access_Type
8772 (Designated_Type (Etype (First_Formal (Hom)))) =
8775 -- If the context is a procedure call, ignore functions
8776 -- in the name of the call.
8778 if Ekind (Hom) = E_Function
8779 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
8780 and then N = Name (Parent (N))
8784 -- If the context is a function call, ignore procedures
8785 -- in the name of the call.
8787 elsif Ekind (Hom) = E_Procedure
8788 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
8793 Set_Etype (Call_Node, Any_Type);
8794 Set_Is_Overloaded (Call_Node, False);
8797 if No (Matching_Op) then
8798 Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog));
8799 Set_Etype (Call_Node, Any_Type);
8800 Set_Parent (Call_Node, Parent (Node_To_Replace));
8802 Set_Name (Call_Node, Hom_Ref);
8807 Report => Report_Error,
8809 Skip_First => True);
8812 Valid_Candidate (Success, Call_Node, Hom);
8818 Report => Report_Error,
8820 Skip_First => True);
8822 if Present (Valid_Candidate (Success, Call_Node, Hom))
8823 and then Nkind (Call_Node) /= N_Function_Call
8825 Error_Msg_NE ("ambiguous call to&", N, Hom);
8826 Report_Ambiguity (Matching_Op);
8827 Report_Ambiguity (Hom);
8835 Hom := Homonym (Hom);
8837 end Traverse_Homonyms;
8839 -------------------------
8840 -- Traverse_Interfaces --
8841 -------------------------
8843 procedure Traverse_Interfaces
8844 (Anc_Type : Entity_Id;
8845 Error : out Boolean)
8847 Intface_List : constant List_Id :=
8848 Abstract_Interface_List (Anc_Type);
8854 if Is_Non_Empty_List (Intface_List) then
8855 Intface := First (Intface_List);
8856 while Present (Intface) loop
8858 -- Look for acceptable class-wide homonyms associated with
8861 Traverse_Homonyms (Etype (Intface), Error);
8867 -- Continue the search by looking at each of the interface's
8868 -- associated interface ancestors.
8870 Traverse_Interfaces (Etype (Intface), Error);
8879 end Traverse_Interfaces;
8881 -- Start of processing for Try_Class_Wide_Operation
8884 -- If we are searching only for conflicting class-wide subprograms
8885 -- then initialize directly Matching_Op with the target entity.
8887 if CW_Test_Only then
8888 Matching_Op := Entity (Selector_Name (N));
8891 -- Loop through ancestor types (including interfaces), traversing
8892 -- the homonym chain of the subprogram, trying out those homonyms
8893 -- whose first formal has the class-wide type of the ancestor, or
8894 -- an anonymous access type designating the class-wide type.
8896 Anc_Type := Obj_Type;
8898 -- Look for a match among homonyms associated with the ancestor
8900 Traverse_Homonyms (Anc_Type, Error);
8906 -- Continue the search for matches among homonyms associated with
8907 -- any interfaces implemented by the ancestor.
8909 Traverse_Interfaces (Anc_Type, Error);
8915 exit when Etype (Anc_Type) = Anc_Type;
8916 Anc_Type := Etype (Anc_Type);
8919 if Present (Matching_Op) then
8920 Set_Etype (Call_Node, Etype (Matching_Op));
8923 return Present (Matching_Op);
8924 end Try_Class_Wide_Operation;
8926 -----------------------------------
8927 -- Try_One_Prefix_Interpretation --
8928 -----------------------------------
8930 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
8932 -- If the interpretation does not have a valid candidate type,
8933 -- preserve current value of Obj_Type for subsequent errors.
8935 Prev_Obj_Type : constant Entity_Id := Obj_Type;
8940 if Is_Access_Type (Obj_Type) then
8941 Obj_Type := Designated_Type (Obj_Type);
8944 if Ekind (Obj_Type) = E_Private_Subtype then
8945 Obj_Type := Base_Type (Obj_Type);
8948 if Is_Class_Wide_Type (Obj_Type) then
8949 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
8952 -- The type may have be obtained through a limited_with clause,
8953 -- in which case the primitive operations are available on its
8954 -- non-limited view. If still incomplete, retrieve full view.
8956 if Ekind (Obj_Type) = E_Incomplete_Type
8957 and then From_Limited_With (Obj_Type)
8958 and then Has_Non_Limited_View (Obj_Type)
8960 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
8963 -- If the object is not tagged, or the type is still an incomplete
8964 -- type, this is not a prefixed call.
8966 if not Is_Tagged_Type (Obj_Type)
8967 or else Is_Incomplete_Type (Obj_Type)
8970 -- Restore previous type if current one is not legal candidate
8972 Obj_Type := Prev_Obj_Type;
8977 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
8978 CW_Result : Boolean;
8979 Prim_Result : Boolean;
8980 pragma Unreferenced (CW_Result);
8983 if not CW_Test_Only then
8985 Try_Primitive_Operation
8986 (Call_Node => New_Call_Node,
8987 Node_To_Replace => Node_To_Replace);
8990 -- Check if there is a class-wide subprogram covering the
8991 -- primitive. This check must be done even if a candidate
8992 -- was found in order to report ambiguous calls.
8994 if not (Prim_Result) then
8996 Try_Class_Wide_Operation
8997 (Call_Node => New_Call_Node,
8998 Node_To_Replace => Node_To_Replace);
9000 -- If we found a primitive we search for class-wide subprograms
9001 -- using a duplicate of the call node (done to avoid missing its
9002 -- decoration if there is no ambiguity).
9006 Try_Class_Wide_Operation
9007 (Call_Node => Dup_Call_Node,
9008 Node_To_Replace => Node_To_Replace);
9011 end Try_One_Prefix_Interpretation;
9013 -----------------------------
9014 -- Try_Primitive_Operation --
9015 -----------------------------
9017 function Try_Primitive_Operation
9018 (Call_Node : Node_Id;
9019 Node_To_Replace : Node_Id) return Boolean
9022 Prim_Op : Entity_Id;
9023 Matching_Op : Entity_Id := Empty;
9024 Prim_Op_Ref : Node_Id := Empty;
9026 Corr_Type : Entity_Id := Empty;
9027 -- If the prefix is a synchronized type, the controlling type of
9028 -- the primitive operation is the corresponding record type, else
9029 -- this is the object type itself.
9031 Success : Boolean := False;
9033 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9034 -- For tagged types the candidate interpretations are found in
9035 -- the list of primitive operations of the type and its ancestors.
9036 -- For formal tagged types we have to find the operations declared
9037 -- in the same scope as the type (including in the generic formal
9038 -- part) because the type itself carries no primitive operations,
9039 -- except for formal derived types that inherit the operations of
9040 -- the parent and progenitors.
9042 -- If the context is a generic subprogram body, the generic formals
9043 -- are visible by name, but are not in the entity list of the
9044 -- subprogram because that list starts with the subprogram formals.
9045 -- We retrieve the candidate operations from the generic declaration.
9047 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9048 -- Prefix notation can also be used on operations that are not
9049 -- primitives of the type, but are declared in the same immediate
9050 -- declarative part, which can only mean the corresponding package
9051 -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the
9052 -- list of primitives with body operations with the same name that
9053 -- may be candidates, so that Try_Primitive_Operations can examine
9054 -- them if no real primitive is found.
9056 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
9057 -- An operation that overrides an inherited operation in the private
9058 -- part of its package may be hidden, but if the inherited operation
9059 -- is visible a direct call to it will dispatch to the private one,
9060 -- which is therefore a valid candidate.
9062 function Names_Match
9063 (Obj_Type : Entity_Id;
9064 Prim_Op : Entity_Id;
9065 Subprog : Entity_Id) return Boolean;
9066 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
9067 -- is a protected type then compare also the original name of Prim_Op
9068 -- with the name of Subprog (since the expander may have added a
9069 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
9071 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
9072 -- Verify that the prefix, dereferenced if need be, is a valid
9073 -- controlling argument in a call to Op. The remaining actuals
9074 -- are checked in the subsequent call to Analyze_One_Call.
9076 ------------------------------
9077 -- Collect_Generic_Type_Ops --
9078 ------------------------------
9080 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
9081 Bas : constant Entity_Id := Base_Type (T);
9082 Candidates : constant Elist_Id := New_Elmt_List;
9086 procedure Check_Candidate;
9087 -- The operation is a candidate if its first parameter is a
9088 -- controlling operand of the desired type.
9090 -----------------------
9091 -- Check_Candidate; --
9092 -----------------------
9094 procedure Check_Candidate is
9096 Formal := First_Formal (Subp);
9099 and then Is_Controlling_Formal (Formal)
9101 (Base_Type (Etype (Formal)) = Bas
9103 (Is_Access_Type (Etype (Formal))
9104 and then Designated_Type (Etype (Formal)) = Bas))
9106 Append_Elmt (Subp, Candidates);
9108 end Check_Candidate;
9110 -- Start of processing for Collect_Generic_Type_Ops
9113 if Is_Derived_Type (T) then
9114 return Primitive_Operations (T);
9116 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
9118 -- Scan the list of generic formals to find subprograms
9119 -- that may have a first controlling formal of the type.
9121 if Nkind (Unit_Declaration_Node (Scope (T))) =
9122 N_Generic_Subprogram_Declaration
9129 First (Generic_Formal_Declarations
9130 (Unit_Declaration_Node (Scope (T))));
9131 while Present (Decl) loop
9132 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
9133 Subp := Defining_Entity (Decl);
9144 -- Scan the list of entities declared in the same scope as
9145 -- the type. In general this will be an open scope, given that
9146 -- the call we are analyzing can only appear within a generic
9147 -- declaration or body (either the one that declares T, or a
9150 -- For a subtype representing a generic actual type, go to the
9153 if Is_Generic_Actual_Type (T) then
9154 Subp := First_Entity (Scope (Base_Type (T)));
9156 Subp := First_Entity (Scope (T));
9159 while Present (Subp) loop
9160 if Is_Overloadable (Subp) then
9169 end Collect_Generic_Type_Ops;
9171 ----------------------------
9172 -- Extended_Primitive_Ops --
9173 ----------------------------
9175 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
9176 Type_Scope : constant Entity_Id := Scope (T);
9178 Body_Decls : List_Id;
9184 Op_List := Primitive_Operations (T);
9186 if Ekind (Type_Scope) = E_Package
9187 and then In_Package_Body (Type_Scope)
9188 and then In_Open_Scopes (Type_Scope)
9190 -- Retrieve list of declarations of package body.
9194 (Unit_Declaration_Node
9196 (Unit_Declaration_Node (Type_Scope))));
9198 Op := Current_Entity (Subprog);
9200 while Present (Op) loop
9201 if Comes_From_Source (Op)
9202 and then Is_Overloadable (Op)
9204 -- Exclude overriding primitive operations of a type
9205 -- extension declared in the package body, to prevent
9206 -- duplicates in extended list.
9208 and then not Is_Primitive (Op)
9209 and then Is_List_Member (Unit_Declaration_Node (Op))
9210 and then List_Containing (Unit_Declaration_Node (Op)) =
9213 if not Op_Found then
9215 -- Copy list of primitives so it is not affected for
9218 Op_List := New_Copy_Elist (Op_List);
9222 Append_Elmt (Op, Op_List);
9230 end Extended_Primitive_Ops;
9232 ---------------------------
9233 -- Is_Private_Overriding --
9234 ---------------------------
9236 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
9237 Visible_Op : constant Entity_Id := Homonym (Op);
9240 return Present (Visible_Op)
9241 and then Scope (Op) = Scope (Visible_Op)
9242 and then not Comes_From_Source (Visible_Op)
9243 and then Alias (Visible_Op) = Op
9244 and then not Is_Hidden (Visible_Op);
9245 end Is_Private_Overriding;
9251 function Names_Match
9252 (Obj_Type : Entity_Id;
9253 Prim_Op : Entity_Id;
9254 Subprog : Entity_Id) return Boolean is
9256 -- Common case: exact match
9258 if Chars (Prim_Op) = Chars (Subprog) then
9261 -- For protected type primitives the expander may have built the
9262 -- name of the dispatching primitive prepending the type name to
9263 -- avoid conflicts with the name of the protected subprogram (see
9264 -- Exp_Ch9.Build_Selected_Name).
9266 elsif Is_Protected_Type (Obj_Type) then
9268 Present (Original_Protected_Subprogram (Prim_Op))
9269 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9276 -----------------------------
9277 -- Valid_First_Argument_Of --
9278 -----------------------------
9280 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9281 Typ : Entity_Id := Etype (First_Formal (Op));
9284 if Is_Concurrent_Type (Typ)
9285 and then Present (Corresponding_Record_Type (Typ))
9287 Typ := Corresponding_Record_Type (Typ);
9290 -- Simple case. Object may be a subtype of the tagged type or
9291 -- may be the corresponding record of a synchronized type.
9293 return Obj_Type = Typ
9294 or else Base_Type (Obj_Type) = Typ
9295 or else Corr_Type = Typ
9297 -- Prefix can be dereferenced
9300 (Is_Access_Type (Corr_Type)
9301 and then Designated_Type (Corr_Type) = Typ)
9303 -- Formal is an access parameter, for which the object
9304 -- can provide an access.
9307 (Ekind (Typ) = E_Anonymous_Access_Type
9309 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
9310 end Valid_First_Argument_Of;
9312 -- Start of processing for Try_Primitive_Operation
9315 -- Look for subprograms in the list of primitive operations. The name
9316 -- must be identical, and the kind of call indicates the expected
9317 -- kind of operation (function or procedure). If the type is a
9318 -- (tagged) synchronized type, the primitive ops are attached to the
9319 -- corresponding record (base) type.
9321 if Is_Concurrent_Type (Obj_Type) then
9322 if Present (Corresponding_Record_Type (Obj_Type)) then
9323 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
9324 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
9326 Corr_Type := Obj_Type;
9327 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9330 elsif not Is_Generic_Type (Obj_Type) then
9331 Corr_Type := Obj_Type;
9332 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
9335 Corr_Type := Obj_Type;
9336 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9339 while Present (Elmt) loop
9340 Prim_Op := Node (Elmt);
9342 if Names_Match (Obj_Type, Prim_Op, Subprog)
9343 and then Present (First_Formal (Prim_Op))
9344 and then Valid_First_Argument_Of (Prim_Op)
9346 (Nkind (Call_Node) = N_Function_Call)
9348 (Ekind (Prim_Op) = E_Function)
9350 -- Ada 2005 (AI-251): If this primitive operation corresponds
9351 -- to an immediate ancestor interface there is no need to add
9352 -- it to the list of interpretations; the corresponding aliased
9353 -- primitive is also in this list of primitive operations and
9354 -- will be used instead.
9356 if (Present (Interface_Alias (Prim_Op))
9357 and then Is_Ancestor (Find_Dispatching_Type
9358 (Alias (Prim_Op)), Corr_Type))
9360 -- Do not consider hidden primitives unless the type is in an
9361 -- open scope or we are within an instance, where visibility
9362 -- is known to be correct, or else if this is an overriding
9363 -- operation in the private part for an inherited operation.
9365 or else (Is_Hidden (Prim_Op)
9366 and then not Is_Immediately_Visible (Obj_Type)
9367 and then not In_Instance
9368 and then not Is_Private_Overriding (Prim_Op))
9373 Set_Etype (Call_Node, Any_Type);
9374 Set_Is_Overloaded (Call_Node, False);
9376 if No (Matching_Op) then
9377 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
9378 Candidate := Prim_Op;
9380 Set_Parent (Call_Node, Parent (Node_To_Replace));
9382 Set_Name (Call_Node, Prim_Op_Ref);
9388 Report => Report_Error,
9390 Skip_First => True);
9392 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
9394 -- More than one interpretation, collect for subsequent
9395 -- disambiguation. If this is a procedure call and there
9396 -- is another match, report ambiguity now.
9402 Report => Report_Error,
9404 Skip_First => True);
9406 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9407 and then Nkind (Call_Node) /= N_Function_Call
9409 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
9410 Report_Ambiguity (Matching_Op);
9411 Report_Ambiguity (Prim_Op);
9421 if Present (Matching_Op) then
9422 Set_Etype (Call_Node, Etype (Matching_Op));
9425 return Present (Matching_Op);
9426 end Try_Primitive_Operation;
9428 -- Start of processing for Try_Object_Operation
9431 Analyze_Expression (Obj);
9433 -- Analyze the actuals if node is known to be a subprogram call
9435 if Is_Subprg_Call and then N = Name (Parent (N)) then
9436 Actual := First (Parameter_Associations (Parent (N)));
9437 while Present (Actual) loop
9438 Analyze_Expression (Actual);
9443 -- Build a subprogram call node, using a copy of Obj as its first
9444 -- actual. This is a placeholder, to be replaced by an explicit
9445 -- dereference when needed.
9447 Transform_Object_Operation
9448 (Call_Node => New_Call_Node,
9449 Node_To_Replace => Node_To_Replace);
9451 Set_Etype (New_Call_Node, Any_Type);
9452 Set_Etype (Subprog, Any_Type);
9453 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
9455 if not Is_Overloaded (Obj) then
9456 Try_One_Prefix_Interpretation (Obj_Type);
9463 Get_First_Interp (Obj, I, It);
9464 while Present (It.Nam) loop
9465 Try_One_Prefix_Interpretation (It.Typ);
9466 Get_Next_Interp (I, It);
9471 if Etype (New_Call_Node) /= Any_Type then
9473 -- No need to complete the tree transformations if we are only
9474 -- searching for conflicting class-wide subprograms
9476 if CW_Test_Only then
9479 Complete_Object_Operation
9480 (Call_Node => New_Call_Node,
9481 Node_To_Replace => Node_To_Replace);
9485 elsif Present (Candidate) then
9487 -- The argument list is not type correct. Re-analyze with error
9488 -- reporting enabled, and use one of the possible candidates.
9489 -- In All_Errors_Mode, re-analyze all failed interpretations.
9491 if All_Errors_Mode then
9492 Report_Error := True;
9493 if Try_Primitive_Operation
9494 (Call_Node => New_Call_Node,
9495 Node_To_Replace => Node_To_Replace)
9498 Try_Class_Wide_Operation
9499 (Call_Node => New_Call_Node,
9500 Node_To_Replace => Node_To_Replace)
9507 (N => New_Call_Node,
9511 Skip_First => True);
9514 -- No need for further errors
9519 -- There was no candidate operation, so report it as an error
9520 -- in the caller: Analyze_Selected_Component.
9524 end Try_Object_Operation;
9530 procedure wpo (T : Entity_Id) is
9535 if not Is_Tagged_Type (T) then
9539 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9540 while Present (E) loop
9542 Write_Int (Int (Op));
9543 Write_Str (" === ");
9544 Write_Name (Chars (Op));
9546 Write_Name (Chars (Scope (Op)));