1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Namet.Sp; use Namet.Sp;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch5; use Sem_Ch5;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Tbuild; use Tbuild;
66 package body Sem_Ch4 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Concatenation_Rest (N : Node_Id);
73 -- Does the "rest" of the work of Analyze_Concatenation, after the left
74 -- operand has been analyzed. See Analyze_Concatenation for details.
76 procedure Analyze_Expression (N : Node_Id);
77 -- For expressions that are not names, this is just a call to analyze.
78 -- If the expression is a name, it may be a call to a parameterless
79 -- function, and if so must be converted into an explicit call node
80 -- and analyzed as such. This deproceduring must be done during the first
81 -- pass of overload resolution, because otherwise a procedure call with
82 -- overloaded actuals may fail to resolve.
84 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
85 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
86 -- is an operator name or an expanded name whose selector is an operator
87 -- name, and one possible interpretation is as a predefined operator.
89 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
90 -- If the prefix of a selected_component is overloaded, the proper
91 -- interpretation that yields a record type with the proper selector
92 -- name must be selected.
94 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
95 -- Procedure to analyze a user defined binary operator, which is resolved
96 -- like a function, but instead of a list of actuals it is presented
97 -- with the left and right operands of an operator node.
99 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
100 -- Procedure to analyze a user defined unary operator, which is resolved
101 -- like a function, but instead of a list of actuals, it is presented with
102 -- the operand of the operator node.
104 procedure Ambiguous_Operands (N : Node_Id);
105 -- For equality, membership, and comparison operators with overloaded
106 -- arguments, list possible interpretations.
108 procedure Analyze_One_Call
112 Success : out Boolean;
113 Skip_First : Boolean := False);
114 -- Check one interpretation of an overloaded subprogram name for
115 -- compatibility with the types of the actuals in a call. If there is a
116 -- single interpretation which does not match, post error if Report is
119 -- Nam is the entity that provides the formals against which the actuals
120 -- are checked. Nam is either the name of a subprogram, or the internal
121 -- subprogram type constructed for an access_to_subprogram. If the actuals
122 -- are compatible with Nam, then Nam is added to the list of candidate
123 -- interpretations for N, and Success is set to True.
125 -- The flag Skip_First is used when analyzing a call that was rewritten
126 -- from object notation. In this case the first actual may have to receive
127 -- an explicit dereference, depending on the first formal of the operation
128 -- being called. The caller will have verified that the object is legal
129 -- for the call. If the remaining parameters match, the first parameter
130 -- will rewritten as a dereference if needed, prior to completing analysis.
132 procedure Check_Misspelled_Selector
135 -- Give possible misspelling diagnostic if Sel is likely to be a mis-
136 -- spelling of one of the selectors of the Prefix. This is called by
137 -- Analyze_Selected_Component after producing an invalid selector error
140 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
141 -- Verify that type T is declared in scope S. Used to find interpretations
142 -- for operators given by expanded names. This is abstracted as a separate
143 -- function to handle extensions to System, where S is System, but T is
144 -- declared in the extension.
146 procedure Find_Arithmetic_Types
150 -- L and R are the operands of an arithmetic operator. Find
151 -- consistent pairs of interpretations for L and R that have a
152 -- numeric type consistent with the semantics of the operator.
154 procedure Find_Comparison_Types
158 -- L and R are operands of a comparison operator. Find consistent
159 -- pairs of interpretations for L and R.
161 procedure Find_Concatenation_Types
165 -- For the four varieties of concatenation
167 procedure Find_Equality_Types
171 -- Ditto for equality operators
173 procedure Find_Boolean_Types
177 -- Ditto for binary logical operations
179 procedure Find_Negation_Types
183 -- Find consistent interpretation for operand of negation operator
185 procedure Find_Non_Universal_Interpretations
190 -- For equality and comparison operators, the result is always boolean,
191 -- and the legality of the operation is determined from the visibility
192 -- of the operand types. If one of the operands has a universal interpre-
193 -- tation, the legality check uses some compatible non-universal
194 -- interpretation of the other operand. N can be an operator node, or
195 -- a function call whose name is an operator designator.
197 function Find_Primitive_Operation (N : Node_Id) return Boolean;
198 -- Find candidate interpretations for the name Obj.Proc when it appears
199 -- in a subprogram renaming declaration.
201 procedure Find_Unary_Types
205 -- Unary arithmetic types: plus, minus, abs
207 procedure Check_Arithmetic_Pair
211 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
212 -- types for left and right operand. Determine whether they constitute
213 -- a valid pair for the given operator, and record the corresponding
214 -- interpretation of the operator node. The node N may be an operator
215 -- node (the usual case) or a function call whose prefix is an operator
216 -- designator. In both cases Op_Id is the operator name itself.
218 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
219 -- Give detailed information on overloaded call where none of the
220 -- interpretations match. N is the call node, Nam the designator for
221 -- the overloaded entity being called.
223 function Junk_Operand (N : Node_Id) return Boolean;
224 -- Test for an operand that is an inappropriate entity (e.g. a package
225 -- name or a label). If so, issue an error message and return True. If
226 -- the operand is not an inappropriate entity kind, return False.
228 procedure Operator_Check (N : Node_Id);
229 -- Verify that an operator has received some valid interpretation. If none
230 -- was found, determine whether a use clause would make the operation
231 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
232 -- every type compatible with the operator, even if the operator for the
233 -- type is not directly visible. The routine uses this type to emit a more
234 -- informative message.
236 function Process_Implicit_Dereference_Prefix
238 P : Node_Id) return Entity_Id;
239 -- Called when P is the prefix of an implicit dereference, denoting an
240 -- object E. The function returns the designated type of the prefix, taking
241 -- into account that the designated type of an anonymous access type may be
242 -- a limited view, when the non-limited view is visible.
243 -- If in semantics only mode (-gnatc or generic), the function also records
244 -- that the prefix is a reference to E, if any. Normally, such a reference
245 -- is generated only when the implicit dereference is expanded into an
246 -- explicit one, but for consistency we must generate the reference when
247 -- expansion is disabled as well.
249 procedure Remove_Abstract_Operations (N : Node_Id);
250 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
251 -- operation is not a candidate interpretation.
253 function Try_Container_Indexing
256 Expr : Node_Id) return Boolean;
257 -- AI05-0139: Generalized indexing to support iterators over containers
259 function Try_Indexed_Call
263 Skip_First : Boolean) return Boolean;
264 -- If a function has defaults for all its actuals, a call to it may in fact
265 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
266 -- interpretation as an indexing, prior to analysis as a call. If both are
267 -- possible, the node is overloaded with both interpretations (same symbol
268 -- but two different types). If the call is written in prefix form, the
269 -- prefix becomes the first parameter in the call, and only the remaining
270 -- actuals must be checked for the presence of defaults.
272 function Try_Indirect_Call
275 Typ : Entity_Id) return Boolean;
276 -- Similarly, a function F that needs no actuals can return an access to a
277 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
278 -- the call may be overloaded with both interpretations.
280 function Try_Object_Operation
282 CW_Test_Only : Boolean := False) return Boolean;
283 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
284 -- is a call in this notation, it is transformed into a normal subprogram
285 -- call where the prefix is a parameter, and True is returned. If node
286 -- N is not of this form, it is unchanged, and False is returned. if
287 -- CW_Test_Only is true then N is an N_Selected_Component node which
288 -- is part of a call to an entry or procedure of a tagged concurrent
289 -- type and this routine is invoked to search for class-wide subprograms
290 -- conflicting with the target entity.
292 procedure wpo (T : Entity_Id);
293 pragma Warnings (Off, wpo);
294 -- Used for debugging: obtain list of primitive operations even if
295 -- type is not frozen and dispatch table is not built yet.
297 ------------------------
298 -- Ambiguous_Operands --
299 ------------------------
301 procedure Ambiguous_Operands (N : Node_Id) is
302 procedure List_Operand_Interps (Opnd : Node_Id);
304 --------------------------
305 -- List_Operand_Interps --
306 --------------------------
308 procedure List_Operand_Interps (Opnd : Node_Id) is
313 if Is_Overloaded (Opnd) then
314 if Nkind (Opnd) in N_Op then
316 elsif Nkind (Opnd) = N_Function_Call then
318 elsif Ada_Version >= Ada_2012 then
324 Get_First_Interp (Opnd, I, It);
325 while Present (It.Nam) loop
326 if Has_Implicit_Dereference (It.Typ) then
328 ("can be interpreted as implicit dereference", Opnd);
332 Get_Next_Interp (I, It);
343 if Opnd = Left_Opnd (N) then
344 Error_Msg_N ("\left operand has the following interpretations", N);
347 ("\right operand has the following interpretations", N);
351 List_Interps (Nam, Err);
352 end List_Operand_Interps;
354 -- Start of processing for Ambiguous_Operands
357 if Nkind (N) in N_Membership_Test then
358 Error_Msg_N ("ambiguous operands for membership", N);
360 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
361 Error_Msg_N ("ambiguous operands for equality", N);
364 Error_Msg_N ("ambiguous operands for comparison", N);
367 if All_Errors_Mode then
368 List_Operand_Interps (Left_Opnd (N));
369 List_Operand_Interps (Right_Opnd (N));
371 Error_Msg_N ("\use -gnatf switch for details", N);
373 end Ambiguous_Operands;
375 -----------------------
376 -- Analyze_Aggregate --
377 -----------------------
379 -- Most of the analysis of Aggregates requires that the type be known,
380 -- and is therefore put off until resolution.
382 procedure Analyze_Aggregate (N : Node_Id) is
384 if No (Etype (N)) then
385 Set_Etype (N, Any_Composite);
387 end Analyze_Aggregate;
389 -----------------------
390 -- Analyze_Allocator --
391 -----------------------
393 procedure Analyze_Allocator (N : Node_Id) is
394 Loc : constant Source_Ptr := Sloc (N);
395 Sav_Errs : constant Nat := Serious_Errors_Detected;
396 E : Node_Id := Expression (N);
397 Acc_Type : Entity_Id;
403 Check_SPARK_Restriction ("allocator is not allowed", N);
405 -- Deal with allocator restrictions
407 -- In accordance with H.4(7), the No_Allocators restriction only applies
408 -- to user-written allocators. The same consideration applies to the
409 -- No_Allocators_Before_Elaboration restriction.
411 if Comes_From_Source (N) then
412 Check_Restriction (No_Allocators, N);
414 -- Processing for No_Allocators_After_Elaboration, loop to look at
415 -- enclosing context, checking task case and main subprogram case.
419 while Present (P) loop
421 -- In both cases we need a handled sequence of statements, where
422 -- the occurrence of the allocator is within the statements.
424 if Nkind (P) = N_Handled_Sequence_Of_Statements
425 and then Is_List_Member (C)
426 and then List_Containing (C) = Statements (P)
428 -- Check for allocator within task body, this is a definite
429 -- violation of No_Allocators_After_Elaboration we can detect.
431 if Nkind (Original_Node (Parent (P))) = N_Task_Body then
432 Check_Restriction (No_Allocators_After_Elaboration, N);
436 -- The other case is appearance in a subprogram body. This may
437 -- be a violation if this is a library level subprogram, and it
438 -- turns out to be used as the main program, but only the
439 -- binder knows that, so just record the occurrence.
441 if Nkind (Original_Node (Parent (P))) = N_Subprogram_Body
442 and then Nkind (Parent (Parent (P))) = N_Compilation_Unit
444 Set_Has_Allocator (Current_Sem_Unit);
453 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
454 -- any. The expected type for the name is any type. A non-overloading
455 -- rule then requires it to be of a type descended from
456 -- System.Storage_Pools.Subpools.Subpool_Handle.
458 -- This isn't exactly what the AI says, but it seems to be the right
459 -- rule. The AI should be fixed.???
462 Subpool : constant Node_Id := Subpool_Handle_Name (N);
465 if Present (Subpool) then
468 if Is_Overloaded (Subpool) then
469 Error_Msg_N ("ambiguous subpool handle", Subpool);
472 -- Check that Etype (Subpool) is descended from Subpool_Handle
478 -- Analyze the qualified expression or subtype indication
480 if Nkind (E) = N_Qualified_Expression then
481 Acc_Type := Create_Itype (E_Allocator_Type, N);
482 Set_Etype (Acc_Type, Acc_Type);
483 Find_Type (Subtype_Mark (E));
485 -- Analyze the qualified expression, and apply the name resolution
486 -- rule given in 4.7(3).
489 Type_Id := Etype (E);
490 Set_Directly_Designated_Type (Acc_Type, Type_Id);
492 Resolve (Expression (E), Type_Id);
494 -- Allocators generated by the build-in-place expansion mechanism
495 -- are explicitly marked as coming from source but do not need to be
496 -- checked for limited initialization. To exclude this case, ensure
497 -- that the parent of the allocator is a source node.
499 if Is_Limited_Type (Type_Id)
500 and then Comes_From_Source (N)
501 and then Comes_From_Source (Parent (N))
502 and then not In_Instance_Body
504 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
505 Error_Msg_N ("initialization not allowed for limited types", N);
506 Explain_Limited_Type (Type_Id, N);
510 -- A qualified expression requires an exact match of the type,
511 -- class-wide matching is not allowed.
513 -- if Is_Class_Wide_Type (Type_Id)
514 -- and then Base_Type
515 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
517 -- Wrong_Type (Expression (E), Type_Id);
520 Check_Non_Static_Context (Expression (E));
522 -- We don't analyze the qualified expression itself because it's
523 -- part of the allocator
525 Set_Etype (E, Type_Id);
527 -- Case where allocator has a subtype indication
532 Base_Typ : Entity_Id;
535 -- If the allocator includes a N_Subtype_Indication then a
536 -- constraint is present, otherwise the node is a subtype mark.
537 -- Introduce an explicit subtype declaration into the tree
538 -- defining some anonymous subtype and rewrite the allocator to
539 -- use this subtype rather than the subtype indication.
541 -- It is important to introduce the explicit subtype declaration
542 -- so that the bounds of the subtype indication are attached to
543 -- the tree in case the allocator is inside a generic unit.
545 if Nkind (E) = N_Subtype_Indication then
547 -- A constraint is only allowed for a composite type in Ada
548 -- 95. In Ada 83, a constraint is also allowed for an
549 -- access-to-composite type, but the constraint is ignored.
551 Find_Type (Subtype_Mark (E));
552 Base_Typ := Entity (Subtype_Mark (E));
554 if Is_Elementary_Type (Base_Typ) then
555 if not (Ada_Version = Ada_83
556 and then Is_Access_Type (Base_Typ))
558 Error_Msg_N ("constraint not allowed here", E);
560 if Nkind (Constraint (E)) =
561 N_Index_Or_Discriminant_Constraint
563 Error_Msg_N -- CODEFIX
564 ("\if qualified expression was meant, " &
565 "use apostrophe", Constraint (E));
569 -- Get rid of the bogus constraint:
571 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
572 Analyze_Allocator (N);
575 -- Ada 2005, AI-363: if the designated type has a constrained
576 -- partial view, it cannot receive a discriminant constraint,
577 -- and the allocated object is unconstrained.
579 elsif Ada_Version >= Ada_2005
580 and then Effectively_Has_Constrained_Partial_View
582 Scop => Current_Scope)
585 ("constraint not allowed when type " &
586 "has a constrained partial view", Constraint (E));
589 if Expander_Active then
590 Def_Id := Make_Temporary (Loc, 'S');
593 Make_Subtype_Declaration (Loc,
594 Defining_Identifier => Def_Id,
595 Subtype_Indication => Relocate_Node (E)));
597 if Sav_Errs /= Serious_Errors_Detected
598 and then Nkind (Constraint (E)) =
599 N_Index_Or_Discriminant_Constraint
601 Error_Msg_N -- CODEFIX
602 ("if qualified expression was meant, " &
603 "use apostrophe!", Constraint (E));
606 E := New_Occurrence_Of (Def_Id, Loc);
607 Rewrite (Expression (N), E);
611 Type_Id := Process_Subtype (E, N);
612 Acc_Type := Create_Itype (E_Allocator_Type, N);
613 Set_Etype (Acc_Type, Acc_Type);
614 Set_Directly_Designated_Type (Acc_Type, Type_Id);
615 Check_Fully_Declared (Type_Id, N);
617 -- Ada 2005 (AI-231): If the designated type is itself an access
618 -- type that excludes null, its default initialization will
619 -- be a null object, and we can insert an unconditional raise
620 -- before the allocator.
622 -- Ada 2012 (AI-104): A not null indication here is altogether
625 if Can_Never_Be_Null (Type_Id) then
627 Not_Null_Check : constant Node_Id :=
628 Make_Raise_Constraint_Error (Sloc (E),
629 Reason => CE_Null_Not_Allowed);
632 if Ada_Version >= Ada_2012 then
634 ("an uninitialized allocator cannot have"
635 & " a null exclusion", N);
637 elsif Expander_Active then
638 Insert_Action (N, Not_Null_Check);
639 Analyze (Not_Null_Check);
642 Error_Msg_N ("null value not allowed here?", E);
647 -- Check restriction against dynamically allocated protected
648 -- objects. Note that when limited aggregates are supported,
649 -- a similar test should be applied to an allocator with a
650 -- qualified expression ???
652 if Is_Protected_Type (Type_Id) then
653 Check_Restriction (No_Protected_Type_Allocators, N);
656 -- Check for missing initialization. Skip this check if we already
657 -- had errors on analyzing the allocator, since in that case these
658 -- are probably cascaded errors.
660 if Is_Indefinite_Subtype (Type_Id)
661 and then Serious_Errors_Detected = Sav_Errs
663 if Is_Class_Wide_Type (Type_Id) then
665 ("initialization required in class-wide allocation", N);
667 if Ada_Version < Ada_2005
668 and then Is_Limited_Type (Type_Id)
670 Error_Msg_N ("unconstrained allocation not allowed", N);
672 if Is_Array_Type (Type_Id) then
674 ("\constraint with array bounds required", N);
676 elsif Has_Unknown_Discriminants (Type_Id) then
679 else pragma Assert (Has_Discriminants (Type_Id));
681 ("\constraint with discriminant values required", N);
684 -- Limited Ada 2005 and general non-limited case
688 ("uninitialized unconstrained allocation not allowed",
691 if Is_Array_Type (Type_Id) then
693 ("\qualified expression or constraint with " &
694 "array bounds required", N);
696 elsif Has_Unknown_Discriminants (Type_Id) then
697 Error_Msg_N ("\qualified expression required", N);
699 else pragma Assert (Has_Discriminants (Type_Id));
701 ("\qualified expression or constraint with " &
702 "discriminant values required", N);
710 if Is_Abstract_Type (Type_Id) then
711 Error_Msg_N ("cannot allocate abstract object", E);
714 if Has_Task (Designated_Type (Acc_Type)) then
715 Check_Restriction (No_Tasking, N);
716 Check_Restriction (Max_Tasks, N);
717 Check_Restriction (No_Task_Allocators, N);
720 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
721 -- type is nested, and the designated type needs finalization. The rule
722 -- is conservative in that class-wide types need finalization.
724 if Needs_Finalization (Designated_Type (Acc_Type))
725 and then not Is_Library_Level_Entity (Acc_Type)
727 Check_Restriction (No_Nested_Finalization, N);
730 -- Check that an allocator of a nested access type doesn't create a
731 -- protected object when restriction No_Local_Protected_Objects applies.
732 -- We don't have an equivalent to Has_Task for protected types, so only
733 -- cases where the designated type itself is a protected type are
734 -- currently checked. ???
736 if Is_Protected_Type (Designated_Type (Acc_Type))
737 and then not Is_Library_Level_Entity (Acc_Type)
739 Check_Restriction (No_Local_Protected_Objects, N);
742 -- If the No_Streams restriction is set, check that the type of the
743 -- object is not, and does not contain, any subtype derived from
744 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
745 -- Has_Stream just for efficiency reasons. There is no point in
746 -- spending time on a Has_Stream check if the restriction is not set.
748 if Restriction_Check_Required (No_Streams) then
749 if Has_Stream (Designated_Type (Acc_Type)) then
750 Check_Restriction (No_Streams, N);
754 Set_Etype (N, Acc_Type);
756 if not Is_Library_Level_Entity (Acc_Type) then
757 Check_Restriction (No_Local_Allocators, N);
760 if Serious_Errors_Detected > Sav_Errs then
761 Set_Error_Posted (N);
762 Set_Etype (N, Any_Type);
764 end Analyze_Allocator;
766 ---------------------------
767 -- Analyze_Arithmetic_Op --
768 ---------------------------
770 procedure Analyze_Arithmetic_Op (N : Node_Id) is
771 L : constant Node_Id := Left_Opnd (N);
772 R : constant Node_Id := Right_Opnd (N);
776 Candidate_Type := Empty;
777 Analyze_Expression (L);
778 Analyze_Expression (R);
780 -- If the entity is already set, the node is the instantiation of a
781 -- generic node with a non-local reference, or was manufactured by a
782 -- call to Make_Op_xxx. In either case the entity is known to be valid,
783 -- and we do not need to collect interpretations, instead we just get
784 -- the single possible interpretation.
788 if Present (Op_Id) then
789 if Ekind (Op_Id) = E_Operator then
791 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
792 and then Treat_Fixed_As_Integer (N)
796 Set_Etype (N, Any_Type);
797 Find_Arithmetic_Types (L, R, Op_Id, N);
801 Set_Etype (N, Any_Type);
802 Add_One_Interp (N, Op_Id, Etype (Op_Id));
805 -- Entity is not already set, so we do need to collect interpretations
808 Op_Id := Get_Name_Entity_Id (Chars (N));
809 Set_Etype (N, Any_Type);
811 while Present (Op_Id) loop
812 if Ekind (Op_Id) = E_Operator
813 and then Present (Next_Entity (First_Entity (Op_Id)))
815 Find_Arithmetic_Types (L, R, Op_Id, N);
817 -- The following may seem superfluous, because an operator cannot
818 -- be generic, but this ignores the cleverness of the author of
821 elsif Is_Overloadable (Op_Id) then
822 Analyze_User_Defined_Binary_Op (N, Op_Id);
825 Op_Id := Homonym (Op_Id);
830 end Analyze_Arithmetic_Op;
836 -- Function, procedure, and entry calls are checked here. The Name in
837 -- the call may be overloaded. The actuals have been analyzed and may
838 -- themselves be overloaded. On exit from this procedure, the node N
839 -- may have zero, one or more interpretations. In the first case an
840 -- error message is produced. In the last case, the node is flagged
841 -- as overloaded and the interpretations are collected in All_Interp.
843 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
844 -- the type-checking is similar to that of other calls.
846 procedure Analyze_Call (N : Node_Id) is
847 Actuals : constant List_Id := Parameter_Associations (N);
852 Success : Boolean := False;
854 Deref : Boolean := False;
855 -- Flag indicates whether an interpretation of the prefix is a
856 -- parameterless call that returns an access_to_subprogram.
858 procedure Check_Mixed_Parameter_And_Named_Associations;
859 -- Check that parameter and named associations are not mixed. This is
860 -- a restriction in SPARK mode.
862 function Name_Denotes_Function return Boolean;
863 -- If the type of the name is an access to subprogram, this may be the
864 -- type of a name, or the return type of the function being called. If
865 -- the name is not an entity then it can denote a protected function.
866 -- Until we distinguish Etype from Return_Type, we must use this routine
867 -- to resolve the meaning of the name in the call.
869 procedure No_Interpretation;
870 -- Output error message when no valid interpretation exists
872 --------------------------------------------------
873 -- Check_Mixed_Parameter_And_Named_Associations --
874 --------------------------------------------------
876 procedure Check_Mixed_Parameter_And_Named_Associations is
878 Named_Seen : Boolean;
883 Actual := First (Actuals);
884 while Present (Actual) loop
885 case Nkind (Actual) is
886 when N_Parameter_Association =>
888 Check_SPARK_Restriction
889 ("named association cannot follow positional one",
899 end Check_Mixed_Parameter_And_Named_Associations;
901 ---------------------------
902 -- Name_Denotes_Function --
903 ---------------------------
905 function Name_Denotes_Function return Boolean is
907 if Is_Entity_Name (Nam) then
908 return Ekind (Entity (Nam)) = E_Function;
910 elsif Nkind (Nam) = N_Selected_Component then
911 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
916 end Name_Denotes_Function;
918 -----------------------
919 -- No_Interpretation --
920 -----------------------
922 procedure No_Interpretation is
923 L : constant Boolean := Is_List_Member (N);
924 K : constant Node_Kind := Nkind (Parent (N));
927 -- If the node is in a list whose parent is not an expression then it
928 -- must be an attempted procedure call.
930 if L and then K not in N_Subexpr then
931 if Ekind (Entity (Nam)) = E_Generic_Procedure then
933 ("must instantiate generic procedure& before call",
937 ("procedure or entry name expected", Nam);
940 -- Check for tasking cases where only an entry call will do
943 and then Nkind_In (K, N_Entry_Call_Alternative,
944 N_Triggering_Alternative)
946 Error_Msg_N ("entry name expected", Nam);
948 -- Otherwise give general error message
951 Error_Msg_N ("invalid prefix in call", Nam);
953 end No_Interpretation;
955 -- Start of processing for Analyze_Call
958 if Restriction_Check_Required (SPARK) then
959 Check_Mixed_Parameter_And_Named_Associations;
962 -- Initialize the type of the result of the call to the error type,
963 -- which will be reset if the type is successfully resolved.
965 Set_Etype (N, Any_Type);
969 if not Is_Overloaded (Nam) then
971 -- Only one interpretation to check
973 if Ekind (Etype (Nam)) = E_Subprogram_Type then
974 Nam_Ent := Etype (Nam);
976 -- If the prefix is an access_to_subprogram, this may be an indirect
977 -- call. This is the case if the name in the call is not an entity
978 -- name, or if it is a function name in the context of a procedure
979 -- call. In this latter case, we have a call to a parameterless
980 -- function that returns a pointer_to_procedure which is the entity
981 -- being called. Finally, F (X) may be a call to a parameterless
982 -- function that returns a pointer to a function with parameters.
984 elsif Is_Access_Type (Etype (Nam))
985 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
987 (not Name_Denotes_Function
988 or else Nkind (N) = N_Procedure_Call_Statement
990 (Nkind (Parent (N)) /= N_Explicit_Dereference
991 and then Is_Entity_Name (Nam)
992 and then No (First_Formal (Entity (Nam)))
993 and then Present (Actuals)))
995 Nam_Ent := Designated_Type (Etype (Nam));
996 Insert_Explicit_Dereference (Nam);
998 -- Selected component case. Simple entry or protected operation,
999 -- where the entry name is given by the selector name.
1001 elsif Nkind (Nam) = N_Selected_Component then
1002 Nam_Ent := Entity (Selector_Name (Nam));
1004 if not Ekind_In (Nam_Ent, E_Entry,
1009 Error_Msg_N ("name in call is not a callable entity", Nam);
1010 Set_Etype (N, Any_Type);
1014 -- If the name is an Indexed component, it can be a call to a member
1015 -- of an entry family. The prefix must be a selected component whose
1016 -- selector is the entry. Analyze_Procedure_Call normalizes several
1017 -- kinds of call into this form.
1019 elsif Nkind (Nam) = N_Indexed_Component then
1020 if Nkind (Prefix (Nam)) = N_Selected_Component then
1021 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1023 Error_Msg_N ("name in call is not a callable entity", Nam);
1024 Set_Etype (N, Any_Type);
1028 elsif not Is_Entity_Name (Nam) then
1029 Error_Msg_N ("name in call is not a callable entity", Nam);
1030 Set_Etype (N, Any_Type);
1034 Nam_Ent := Entity (Nam);
1036 -- If no interpretations, give error message
1038 if not Is_Overloadable (Nam_Ent) then
1044 -- Operations generated for RACW stub types are called only through
1045 -- dispatching, and can never be the static interpretation of a call.
1047 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1052 Analyze_One_Call (N, Nam_Ent, True, Success);
1054 -- If this is an indirect call, the return type of the access_to
1055 -- subprogram may be an incomplete type. At the point of the call,
1056 -- use the full type if available, and at the same time update the
1057 -- return type of the access_to_subprogram.
1060 and then Nkind (Nam) = N_Explicit_Dereference
1061 and then Ekind (Etype (N)) = E_Incomplete_Type
1062 and then Present (Full_View (Etype (N)))
1064 Set_Etype (N, Full_View (Etype (N)));
1065 Set_Etype (Nam_Ent, Etype (N));
1069 -- An overloaded selected component must denote overloaded operations
1070 -- of a concurrent type. The interpretations are attached to the
1071 -- simple name of those operations.
1073 if Nkind (Nam) = N_Selected_Component then
1074 Nam := Selector_Name (Nam);
1077 Get_First_Interp (Nam, X, It);
1079 while Present (It.Nam) loop
1083 -- Name may be call that returns an access to subprogram, or more
1084 -- generally an overloaded expression one of whose interpretations
1085 -- yields an access to subprogram. If the name is an entity, we do
1086 -- not dereference, because the node is a call that returns the
1087 -- access type: note difference between f(x), where the call may
1088 -- return an access subprogram type, and f(x)(y), where the type
1089 -- returned by the call to f is implicitly dereferenced to analyze
1092 if Is_Access_Type (Nam_Ent) then
1093 Nam_Ent := Designated_Type (Nam_Ent);
1095 elsif Is_Access_Type (Etype (Nam_Ent))
1097 (not Is_Entity_Name (Nam)
1098 or else Nkind (N) = N_Procedure_Call_Statement)
1099 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1102 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1104 if Is_Entity_Name (Nam) then
1109 -- If the call has been rewritten from a prefixed call, the first
1110 -- parameter has been analyzed, but may need a subsequent
1111 -- dereference, so skip its analysis now.
1113 if N /= Original_Node (N)
1114 and then Nkind (Original_Node (N)) = Nkind (N)
1115 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1116 and then Present (Parameter_Associations (N))
1117 and then Present (Etype (First (Parameter_Associations (N))))
1120 (N, Nam_Ent, False, Success, Skip_First => True);
1122 Analyze_One_Call (N, Nam_Ent, False, Success);
1125 -- If the interpretation succeeds, mark the proper type of the
1126 -- prefix (any valid candidate will do). If not, remove the
1127 -- candidate interpretation. This only needs to be done for
1128 -- overloaded protected operations, for other entities disambi-
1129 -- guation is done directly in Resolve.
1133 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1135 Set_Entity (Nam, It.Nam);
1136 Insert_Explicit_Dereference (Nam);
1137 Set_Etype (Nam, Nam_Ent);
1140 Set_Etype (Nam, It.Typ);
1143 elsif Nkind_In (Name (N), N_Selected_Component,
1149 Get_Next_Interp (X, It);
1152 -- If the name is the result of a function call, it can only
1153 -- be a call to a function returning an access to subprogram.
1154 -- Insert explicit dereference.
1156 if Nkind (Nam) = N_Function_Call then
1157 Insert_Explicit_Dereference (Nam);
1160 if Etype (N) = Any_Type then
1162 -- None of the interpretations is compatible with the actuals
1164 Diagnose_Call (N, Nam);
1166 -- Special checks for uninstantiated put routines
1168 if Nkind (N) = N_Procedure_Call_Statement
1169 and then Is_Entity_Name (Nam)
1170 and then Chars (Nam) = Name_Put
1171 and then List_Length (Actuals) = 1
1174 Arg : constant Node_Id := First (Actuals);
1178 if Nkind (Arg) = N_Parameter_Association then
1179 Typ := Etype (Explicit_Actual_Parameter (Arg));
1184 if Is_Signed_Integer_Type (Typ) then
1186 ("possible missing instantiation of " &
1187 "'Text_'I'O.'Integer_'I'O!", Nam);
1189 elsif Is_Modular_Integer_Type (Typ) then
1191 ("possible missing instantiation of " &
1192 "'Text_'I'O.'Modular_'I'O!", Nam);
1194 elsif Is_Floating_Point_Type (Typ) then
1196 ("possible missing instantiation of " &
1197 "'Text_'I'O.'Float_'I'O!", Nam);
1199 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1201 ("possible missing instantiation of " &
1202 "'Text_'I'O.'Fixed_'I'O!", Nam);
1204 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1206 ("possible missing instantiation of " &
1207 "'Text_'I'O.'Decimal_'I'O!", Nam);
1209 elsif Is_Enumeration_Type (Typ) then
1211 ("possible missing instantiation of " &
1212 "'Text_'I'O.'Enumeration_'I'O!", Nam);
1217 elsif not Is_Overloaded (N)
1218 and then Is_Entity_Name (Nam)
1220 -- Resolution yields a single interpretation. Verify that the
1221 -- reference has capitalization consistent with the declaration.
1223 Set_Entity_With_Style_Check (Nam, Entity (Nam));
1224 Generate_Reference (Entity (Nam), Nam);
1226 Set_Etype (Nam, Etype (Entity (Nam)));
1228 Remove_Abstract_Operations (N);
1235 -----------------------------
1236 -- Analyze_Case_Expression --
1237 -----------------------------
1239 procedure Analyze_Case_Expression (N : Node_Id) is
1240 Expr : constant Node_Id := Expression (N);
1241 FirstX : constant Node_Id := Expression (First (Alternatives (N)));
1243 Exp_Type : Entity_Id;
1244 Exp_Btype : Entity_Id;
1246 Dont_Care : Boolean;
1247 Others_Present : Boolean;
1249 procedure Non_Static_Choice_Error (Choice : Node_Id);
1250 -- Error routine invoked by the generic instantiation below when
1251 -- the case expression has a non static choice.
1253 package Case_Choices_Processing is new
1254 Generic_Choices_Processing
1255 (Get_Alternatives => Alternatives,
1256 Get_Choices => Discrete_Choices,
1257 Process_Empty_Choice => No_OP,
1258 Process_Non_Static_Choice => Non_Static_Choice_Error,
1259 Process_Associated_Node => No_OP);
1260 use Case_Choices_Processing;
1262 -----------------------------
1263 -- Non_Static_Choice_Error --
1264 -----------------------------
1266 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1268 Flag_Non_Static_Expr
1269 ("choice given in case expression is not static!", Choice);
1270 end Non_Static_Choice_Error;
1272 -- Start of processing for Analyze_Case_Expression
1275 if Comes_From_Source (N) then
1276 Check_Compiler_Unit (N);
1279 Analyze_And_Resolve (Expr, Any_Discrete);
1280 Check_Unset_Reference (Expr);
1281 Exp_Type := Etype (Expr);
1282 Exp_Btype := Base_Type (Exp_Type);
1284 Alt := First (Alternatives (N));
1285 while Present (Alt) loop
1286 Analyze (Expression (Alt));
1290 if not Is_Overloaded (FirstX) then
1291 Set_Etype (N, Etype (FirstX));
1299 Set_Etype (N, Any_Type);
1301 Get_First_Interp (FirstX, I, It);
1302 while Present (It.Nam) loop
1304 -- For each interpretation of the first expression, we only
1305 -- add the interpretation if every other expression in the
1306 -- case expression alternatives has a compatible type.
1308 Alt := Next (First (Alternatives (N)));
1309 while Present (Alt) loop
1310 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1315 Add_One_Interp (N, It.Typ, It.Typ);
1318 Get_Next_Interp (I, It);
1323 Exp_Btype := Base_Type (Exp_Type);
1325 -- The expression must be of a discrete type which must be determinable
1326 -- independently of the context in which the expression occurs, but
1327 -- using the fact that the expression must be of a discrete type.
1328 -- Moreover, the type this expression must not be a character literal
1329 -- (which is always ambiguous).
1331 -- If error already reported by Resolve, nothing more to do
1333 if Exp_Btype = Any_Discrete
1334 or else Exp_Btype = Any_Type
1338 elsif Exp_Btype = Any_Character then
1340 ("character literal as case expression is ambiguous", Expr);
1344 -- If the case expression is a formal object of mode in out, then
1345 -- treat it as having a nonstatic subtype by forcing use of the base
1346 -- type (which has to get passed to Check_Case_Choices below). Also
1347 -- use base type when the case expression is parenthesized.
1349 if Paren_Count (Expr) > 0
1350 or else (Is_Entity_Name (Expr)
1351 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1353 Exp_Type := Exp_Btype;
1356 -- Call instantiated Analyze_Choices which does the rest of the work
1358 Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1360 if Exp_Type = Universal_Integer and then not Others_Present then
1362 ("case on universal integer requires OTHERS choice", Expr);
1364 end Analyze_Case_Expression;
1366 ---------------------------
1367 -- Analyze_Comparison_Op --
1368 ---------------------------
1370 procedure Analyze_Comparison_Op (N : Node_Id) is
1371 L : constant Node_Id := Left_Opnd (N);
1372 R : constant Node_Id := Right_Opnd (N);
1373 Op_Id : Entity_Id := Entity (N);
1376 Set_Etype (N, Any_Type);
1377 Candidate_Type := Empty;
1379 Analyze_Expression (L);
1380 Analyze_Expression (R);
1382 if Present (Op_Id) then
1383 if Ekind (Op_Id) = E_Operator then
1384 Find_Comparison_Types (L, R, Op_Id, N);
1386 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1389 if Is_Overloaded (L) then
1390 Set_Etype (L, Intersect_Types (L, R));
1394 Op_Id := Get_Name_Entity_Id (Chars (N));
1395 while Present (Op_Id) loop
1396 if Ekind (Op_Id) = E_Operator then
1397 Find_Comparison_Types (L, R, Op_Id, N);
1399 Analyze_User_Defined_Binary_Op (N, Op_Id);
1402 Op_Id := Homonym (Op_Id);
1407 end Analyze_Comparison_Op;
1409 ---------------------------
1410 -- Analyze_Concatenation --
1411 ---------------------------
1413 procedure Analyze_Concatenation (N : Node_Id) is
1415 -- We wish to avoid deep recursion, because concatenations are often
1416 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1417 -- operands nonrecursively until we find something that is not a
1418 -- concatenation (A in this case), or has already been analyzed. We
1419 -- analyze that, and then walk back up the tree following Parent
1420 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1421 -- work at each level. The Parent pointers allow us to avoid recursion,
1422 -- and thus avoid running out of memory.
1428 Candidate_Type := Empty;
1430 -- The following code is equivalent to:
1432 -- Set_Etype (N, Any_Type);
1433 -- Analyze_Expression (Left_Opnd (N));
1434 -- Analyze_Concatenation_Rest (N);
1436 -- where the Analyze_Expression call recurses back here if the left
1437 -- operand is a concatenation.
1439 -- Walk down left operands
1442 Set_Etype (NN, Any_Type);
1443 L := Left_Opnd (NN);
1444 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1448 -- Now (given the above example) NN is A&B and L is A
1450 -- First analyze L ...
1452 Analyze_Expression (L);
1454 -- ... then walk NN back up until we reach N (where we started), calling
1455 -- Analyze_Concatenation_Rest along the way.
1458 Analyze_Concatenation_Rest (NN);
1462 end Analyze_Concatenation;
1464 --------------------------------
1465 -- Analyze_Concatenation_Rest --
1466 --------------------------------
1468 -- If the only one-dimensional array type in scope is String,
1469 -- this is the resulting type of the operation. Otherwise there
1470 -- will be a concatenation operation defined for each user-defined
1471 -- one-dimensional array.
1473 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1474 L : constant Node_Id := Left_Opnd (N);
1475 R : constant Node_Id := Right_Opnd (N);
1476 Op_Id : Entity_Id := Entity (N);
1481 Analyze_Expression (R);
1483 -- If the entity is present, the node appears in an instance, and
1484 -- denotes a predefined concatenation operation. The resulting type is
1485 -- obtained from the arguments when possible. If the arguments are
1486 -- aggregates, the array type and the concatenation type must be
1489 if Present (Op_Id) then
1490 if Ekind (Op_Id) = E_Operator then
1491 LT := Base_Type (Etype (L));
1492 RT := Base_Type (Etype (R));
1494 if Is_Array_Type (LT)
1495 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1497 Add_One_Interp (N, Op_Id, LT);
1499 elsif Is_Array_Type (RT)
1500 and then LT = Base_Type (Component_Type (RT))
1502 Add_One_Interp (N, Op_Id, RT);
1504 -- If one operand is a string type or a user-defined array type,
1505 -- and the other is a literal, result is of the specific type.
1508 (Root_Type (LT) = Standard_String
1509 or else Scope (LT) /= Standard_Standard)
1510 and then Etype (R) = Any_String
1512 Add_One_Interp (N, Op_Id, LT);
1515 (Root_Type (RT) = Standard_String
1516 or else Scope (RT) /= Standard_Standard)
1517 and then Etype (L) = Any_String
1519 Add_One_Interp (N, Op_Id, RT);
1521 elsif not Is_Generic_Type (Etype (Op_Id)) then
1522 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1525 -- Type and its operations must be visible
1527 Set_Entity (N, Empty);
1528 Analyze_Concatenation (N);
1532 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1536 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1537 while Present (Op_Id) loop
1538 if Ekind (Op_Id) = E_Operator then
1540 -- Do not consider operators declared in dead code, they can
1541 -- not be part of the resolution.
1543 if Is_Eliminated (Op_Id) then
1546 Find_Concatenation_Types (L, R, Op_Id, N);
1550 Analyze_User_Defined_Binary_Op (N, Op_Id);
1553 Op_Id := Homonym (Op_Id);
1558 end Analyze_Concatenation_Rest;
1560 ------------------------------------
1561 -- Analyze_Conditional_Expression --
1562 ------------------------------------
1564 procedure Analyze_Conditional_Expression (N : Node_Id) is
1565 Condition : constant Node_Id := First (Expressions (N));
1566 Then_Expr : constant Node_Id := Next (Condition);
1567 Else_Expr : Node_Id;
1570 -- Defend against error of missing expressions from previous error
1572 if No (Then_Expr) then
1576 Check_SPARK_Restriction ("conditional expression is not allowed", N);
1578 Else_Expr := Next (Then_Expr);
1580 if Comes_From_Source (N) then
1581 Check_Compiler_Unit (N);
1584 Analyze_Expression (Condition);
1585 Analyze_Expression (Then_Expr);
1587 if Present (Else_Expr) then
1588 Analyze_Expression (Else_Expr);
1591 -- If then expression not overloaded, then that decides the type
1593 if not Is_Overloaded (Then_Expr) then
1594 Set_Etype (N, Etype (Then_Expr));
1596 -- Case where then expression is overloaded
1604 Set_Etype (N, Any_Type);
1606 -- Shouldn't the following statement be down in the ELSE of the
1607 -- following loop? ???
1609 Get_First_Interp (Then_Expr, I, It);
1611 -- if no Else_Expression the conditional must be boolean
1613 if No (Else_Expr) then
1614 Set_Etype (N, Standard_Boolean);
1616 -- Else_Expression Present. For each possible intepretation of
1617 -- the Then_Expression, add it only if the Else_Expression has
1618 -- a compatible type.
1621 while Present (It.Nam) loop
1622 if Has_Compatible_Type (Else_Expr, It.Typ) then
1623 Add_One_Interp (N, It.Typ, It.Typ);
1626 Get_Next_Interp (I, It);
1631 end Analyze_Conditional_Expression;
1633 -------------------------
1634 -- Analyze_Equality_Op --
1635 -------------------------
1637 procedure Analyze_Equality_Op (N : Node_Id) is
1638 Loc : constant Source_Ptr := Sloc (N);
1639 L : constant Node_Id := Left_Opnd (N);
1640 R : constant Node_Id := Right_Opnd (N);
1644 Set_Etype (N, Any_Type);
1645 Candidate_Type := Empty;
1647 Analyze_Expression (L);
1648 Analyze_Expression (R);
1650 -- If the entity is set, the node is a generic instance with a non-local
1651 -- reference to the predefined operator or to a user-defined function.
1652 -- It can also be an inequality that is expanded into the negation of a
1653 -- call to a user-defined equality operator.
1655 -- For the predefined case, the result is Boolean, regardless of the
1656 -- type of the operands. The operands may even be limited, if they are
1657 -- generic actuals. If they are overloaded, label the left argument with
1658 -- the common type that must be present, or with the type of the formal
1659 -- of the user-defined function.
1661 if Present (Entity (N)) then
1662 Op_Id := Entity (N);
1664 if Ekind (Op_Id) = E_Operator then
1665 Add_One_Interp (N, Op_Id, Standard_Boolean);
1667 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1670 if Is_Overloaded (L) then
1671 if Ekind (Op_Id) = E_Operator then
1672 Set_Etype (L, Intersect_Types (L, R));
1674 Set_Etype (L, Etype (First_Formal (Op_Id)));
1679 Op_Id := Get_Name_Entity_Id (Chars (N));
1680 while Present (Op_Id) loop
1681 if Ekind (Op_Id) = E_Operator then
1682 Find_Equality_Types (L, R, Op_Id, N);
1684 Analyze_User_Defined_Binary_Op (N, Op_Id);
1687 Op_Id := Homonym (Op_Id);
1691 -- If there was no match, and the operator is inequality, this may
1692 -- be a case where inequality has not been made explicit, as for
1693 -- tagged types. Analyze the node as the negation of an equality
1694 -- operation. This cannot be done earlier, because before analysis
1695 -- we cannot rule out the presence of an explicit inequality.
1697 if Etype (N) = Any_Type
1698 and then Nkind (N) = N_Op_Ne
1700 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1701 while Present (Op_Id) loop
1702 if Ekind (Op_Id) = E_Operator then
1703 Find_Equality_Types (L, R, Op_Id, N);
1705 Analyze_User_Defined_Binary_Op (N, Op_Id);
1708 Op_Id := Homonym (Op_Id);
1711 if Etype (N) /= Any_Type then
1712 Op_Id := Entity (N);
1718 Left_Opnd => Left_Opnd (N),
1719 Right_Opnd => Right_Opnd (N))));
1721 Set_Entity (Right_Opnd (N), Op_Id);
1727 end Analyze_Equality_Op;
1729 ----------------------------------
1730 -- Analyze_Explicit_Dereference --
1731 ----------------------------------
1733 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1734 Loc : constant Source_Ptr := Sloc (N);
1735 P : constant Node_Id := Prefix (N);
1741 function Is_Function_Type return Boolean;
1742 -- Check whether node may be interpreted as an implicit function call
1744 ----------------------
1745 -- Is_Function_Type --
1746 ----------------------
1748 function Is_Function_Type return Boolean is
1753 if not Is_Overloaded (N) then
1754 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1755 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1758 Get_First_Interp (N, I, It);
1759 while Present (It.Nam) loop
1760 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1761 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1766 Get_Next_Interp (I, It);
1771 end Is_Function_Type;
1773 -- Start of processing for Analyze_Explicit_Dereference
1776 -- If source node, check SPARK restriction. We guard this with the
1777 -- source node check, because ???
1779 if Comes_From_Source (N) then
1780 Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1783 -- In formal verification mode, keep track of all reads and writes
1784 -- through explicit dereferences.
1787 Alfa.Generate_Dereference (N);
1791 Set_Etype (N, Any_Type);
1793 -- Test for remote access to subprogram type, and if so return
1794 -- after rewriting the original tree.
1796 if Remote_AST_E_Dereference (P) then
1800 -- Normal processing for other than remote access to subprogram type
1802 if not Is_Overloaded (P) then
1803 if Is_Access_Type (Etype (P)) then
1805 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1806 -- avoid other problems caused by the Private_Subtype and it is
1807 -- safe to go to the Base_Type because this is the same as
1808 -- converting the access value to its Base_Type.
1811 DT : Entity_Id := Designated_Type (Etype (P));
1814 if Ekind (DT) = E_Private_Subtype
1815 and then Is_For_Access_Subtype (DT)
1817 DT := Base_Type (DT);
1820 -- An explicit dereference is a legal occurrence of an
1821 -- incomplete type imported through a limited_with clause,
1822 -- if the full view is visible.
1824 if From_With_Type (DT)
1825 and then not From_With_Type (Scope (DT))
1827 (Is_Immediately_Visible (Scope (DT))
1829 (Is_Child_Unit (Scope (DT))
1830 and then Is_Visible_Child_Unit (Scope (DT))))
1832 Set_Etype (N, Available_View (DT));
1839 elsif Etype (P) /= Any_Type then
1840 Error_Msg_N ("prefix of dereference must be an access type", N);
1845 Get_First_Interp (P, I, It);
1846 while Present (It.Nam) loop
1849 if Is_Access_Type (T) then
1850 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1853 Get_Next_Interp (I, It);
1856 -- Error if no interpretation of the prefix has an access type
1858 if Etype (N) = Any_Type then
1860 ("access type required in prefix of explicit dereference", P);
1861 Set_Etype (N, Any_Type);
1867 and then Nkind (Parent (N)) /= N_Indexed_Component
1869 and then (Nkind (Parent (N)) /= N_Function_Call
1870 or else N /= Name (Parent (N)))
1872 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1873 or else N /= Name (Parent (N)))
1875 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1876 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1878 (Attribute_Name (Parent (N)) /= Name_Address
1880 Attribute_Name (Parent (N)) /= Name_Access))
1882 -- Name is a function call with no actuals, in a context that
1883 -- requires deproceduring (including as an actual in an enclosing
1884 -- function or procedure call). There are some pathological cases
1885 -- where the prefix might include functions that return access to
1886 -- subprograms and others that return a regular type. Disambiguation
1887 -- of those has to take place in Resolve.
1890 Make_Function_Call (Loc,
1891 Name => Make_Explicit_Dereference (Loc, P),
1892 Parameter_Associations => New_List);
1894 -- If the prefix is overloaded, remove operations that have formals,
1895 -- we know that this is a parameterless call.
1897 if Is_Overloaded (P) then
1898 Get_First_Interp (P, I, It);
1899 while Present (It.Nam) loop
1902 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1908 Get_Next_Interp (I, It);
1915 elsif not Is_Function_Type
1916 and then Is_Overloaded (N)
1918 -- The prefix may include access to subprograms and other access
1919 -- types. If the context selects the interpretation that is a
1920 -- function call (not a procedure call) we cannot rewrite the node
1921 -- yet, but we include the result of the call interpretation.
1923 Get_First_Interp (N, I, It);
1924 while Present (It.Nam) loop
1925 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1926 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1927 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1929 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1932 Get_Next_Interp (I, It);
1936 -- A value of remote access-to-class-wide must not be dereferenced
1939 Validate_Remote_Access_To_Class_Wide_Type (N);
1940 end Analyze_Explicit_Dereference;
1942 ------------------------
1943 -- Analyze_Expression --
1944 ------------------------
1946 procedure Analyze_Expression (N : Node_Id) is
1949 Check_Parameterless_Call (N);
1950 end Analyze_Expression;
1952 -------------------------------------
1953 -- Analyze_Expression_With_Actions --
1954 -------------------------------------
1956 procedure Analyze_Expression_With_Actions (N : Node_Id) is
1960 A := First (Actions (N));
1967 Analyze_Expression (Expression (N));
1968 Set_Etype (N, Etype (Expression (N)));
1969 end Analyze_Expression_With_Actions;
1971 ------------------------------------
1972 -- Analyze_Indexed_Component_Form --
1973 ------------------------------------
1975 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1976 P : constant Node_Id := Prefix (N);
1977 Exprs : constant List_Id := Expressions (N);
1983 procedure Process_Function_Call;
1984 -- Prefix in indexed component form is an overloadable entity,
1985 -- so the node is a function call. Reformat it as such.
1987 procedure Process_Indexed_Component;
1988 -- Prefix in indexed component form is actually an indexed component.
1989 -- This routine processes it, knowing that the prefix is already
1992 procedure Process_Indexed_Component_Or_Slice;
1993 -- An indexed component with a single index may designate a slice if
1994 -- the index is a subtype mark. This routine disambiguates these two
1995 -- cases by resolving the prefix to see if it is a subtype mark.
1997 procedure Process_Overloaded_Indexed_Component;
1998 -- If the prefix of an indexed component is overloaded, the proper
1999 -- interpretation is selected by the index types and the context.
2001 ---------------------------
2002 -- Process_Function_Call --
2003 ---------------------------
2005 procedure Process_Function_Call is
2009 Change_Node (N, N_Function_Call);
2011 Set_Parameter_Associations (N, Exprs);
2013 -- Analyze actuals prior to analyzing the call itself
2015 Actual := First (Parameter_Associations (N));
2016 while Present (Actual) loop
2018 Check_Parameterless_Call (Actual);
2020 -- Move to next actual. Note that we use Next, not Next_Actual
2021 -- here. The reason for this is a bit subtle. If a function call
2022 -- includes named associations, the parser recognizes the node as
2023 -- a call, and it is analyzed as such. If all associations are
2024 -- positional, the parser builds an indexed_component node, and
2025 -- it is only after analysis of the prefix that the construct
2026 -- is recognized as a call, in which case Process_Function_Call
2027 -- rewrites the node and analyzes the actuals. If the list of
2028 -- actuals is malformed, the parser may leave the node as an
2029 -- indexed component (despite the presence of named associations).
2030 -- The iterator Next_Actual is equivalent to Next if the list is
2031 -- positional, but follows the normalized chain of actuals when
2032 -- named associations are present. In this case normalization has
2033 -- not taken place, and actuals remain unanalyzed, which leads to
2034 -- subsequent crashes or loops if there is an attempt to continue
2035 -- analysis of the program.
2041 end Process_Function_Call;
2043 -------------------------------
2044 -- Process_Indexed_Component --
2045 -------------------------------
2047 procedure Process_Indexed_Component is
2049 Array_Type : Entity_Id;
2051 Pent : Entity_Id := Empty;
2054 Exp := First (Exprs);
2056 if Is_Overloaded (P) then
2057 Process_Overloaded_Indexed_Component;
2060 Array_Type := Etype (P);
2062 if Is_Entity_Name (P) then
2064 elsif Nkind (P) = N_Selected_Component
2065 and then Is_Entity_Name (Selector_Name (P))
2067 Pent := Entity (Selector_Name (P));
2070 -- Prefix must be appropriate for an array type, taking into
2071 -- account a possible implicit dereference.
2073 if Is_Access_Type (Array_Type) then
2074 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2075 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2078 if Is_Array_Type (Array_Type) then
2081 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2083 Set_Etype (N, Any_Type);
2085 if not Has_Compatible_Type
2086 (Exp, Entry_Index_Type (Pent))
2088 Error_Msg_N ("invalid index type in entry name", N);
2090 elsif Present (Next (Exp)) then
2091 Error_Msg_N ("too many subscripts in entry reference", N);
2094 Set_Etype (N, Etype (P));
2099 elsif Is_Record_Type (Array_Type)
2100 and then Remote_AST_I_Dereference (P)
2104 elsif Try_Container_Indexing (N, P, Exp) then
2107 elsif Array_Type = Any_Type then
2108 Set_Etype (N, Any_Type);
2110 -- In most cases the analysis of the prefix will have emitted
2111 -- an error already, but if the prefix may be interpreted as a
2112 -- call in prefixed notation, the report is left to the caller.
2113 -- To prevent cascaded errors, report only if no previous ones.
2115 if Serious_Errors_Detected = 0 then
2116 Error_Msg_N ("invalid prefix in indexed component", P);
2118 if Nkind (P) = N_Expanded_Name then
2119 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2125 -- Here we definitely have a bad indexing
2128 if Nkind (Parent (N)) = N_Requeue_Statement
2129 and then Present (Pent) and then Ekind (Pent) = E_Entry
2132 ("REQUEUE does not permit parameters", First (Exprs));
2134 elsif Is_Entity_Name (P)
2135 and then Etype (P) = Standard_Void_Type
2137 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2140 Error_Msg_N ("array type required in indexed component", P);
2143 Set_Etype (N, Any_Type);
2147 Index := First_Index (Array_Type);
2148 while Present (Index) and then Present (Exp) loop
2149 if not Has_Compatible_Type (Exp, Etype (Index)) then
2150 Wrong_Type (Exp, Etype (Index));
2151 Set_Etype (N, Any_Type);
2159 Set_Etype (N, Component_Type (Array_Type));
2160 Check_Implicit_Dereference (N, Etype (N));
2162 if Present (Index) then
2164 ("too few subscripts in array reference", First (Exprs));
2166 elsif Present (Exp) then
2167 Error_Msg_N ("too many subscripts in array reference", Exp);
2170 end Process_Indexed_Component;
2172 ----------------------------------------
2173 -- Process_Indexed_Component_Or_Slice --
2174 ----------------------------------------
2176 procedure Process_Indexed_Component_Or_Slice is
2178 Exp := First (Exprs);
2179 while Present (Exp) loop
2180 Analyze_Expression (Exp);
2184 Exp := First (Exprs);
2186 -- If one index is present, and it is a subtype name, then the
2187 -- node denotes a slice (note that the case of an explicit range
2188 -- for a slice was already built as an N_Slice node in the first
2189 -- place, so that case is not handled here).
2191 -- We use a replace rather than a rewrite here because this is one
2192 -- of the cases in which the tree built by the parser is plain wrong.
2195 and then Is_Entity_Name (Exp)
2196 and then Is_Type (Entity (Exp))
2199 Make_Slice (Sloc (N),
2201 Discrete_Range => New_Copy (Exp)));
2204 -- Otherwise (more than one index present, or single index is not
2205 -- a subtype name), then we have the indexed component case.
2208 Process_Indexed_Component;
2210 end Process_Indexed_Component_Or_Slice;
2212 ------------------------------------------
2213 -- Process_Overloaded_Indexed_Component --
2214 ------------------------------------------
2216 procedure Process_Overloaded_Indexed_Component is
2225 Set_Etype (N, Any_Type);
2227 Get_First_Interp (P, I, It);
2228 while Present (It.Nam) loop
2231 if Is_Access_Type (Typ) then
2232 Typ := Designated_Type (Typ);
2233 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2236 if Is_Array_Type (Typ) then
2238 -- Got a candidate: verify that index types are compatible
2240 Index := First_Index (Typ);
2242 Exp := First (Exprs);
2243 while Present (Index) and then Present (Exp) loop
2244 if Has_Compatible_Type (Exp, Etype (Index)) then
2256 if Found and then No (Index) and then No (Exp) then
2258 CT : constant Entity_Id :=
2259 Base_Type (Component_Type (Typ));
2261 Add_One_Interp (N, CT, CT);
2262 Check_Implicit_Dereference (N, CT);
2266 elsif Try_Container_Indexing (N, P, First (Exprs)) then
2271 Get_Next_Interp (I, It);
2274 if Etype (N) = Any_Type then
2275 Error_Msg_N ("no legal interpretation for indexed component", N);
2276 Set_Is_Overloaded (N, False);
2280 end Process_Overloaded_Indexed_Component;
2282 -- Start of processing for Analyze_Indexed_Component_Form
2285 -- Get name of array, function or type
2289 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) then
2291 -- If P is an explicit dereference whose prefix is of a
2292 -- remote access-to-subprogram type, then N has already
2293 -- been rewritten as a subprogram call and analyzed.
2298 pragma Assert (Nkind (N) = N_Indexed_Component);
2300 P_T := Base_Type (Etype (P));
2302 if Is_Entity_Name (P) and then Present (Entity (P)) then
2305 if Is_Type (U_N) then
2307 -- Reformat node as a type conversion
2309 E := Remove_Head (Exprs);
2311 if Present (First (Exprs)) then
2313 ("argument of type conversion must be single expression", N);
2316 Change_Node (N, N_Type_Conversion);
2317 Set_Subtype_Mark (N, P);
2319 Set_Expression (N, E);
2321 -- After changing the node, call for the specific Analysis
2322 -- routine directly, to avoid a double call to the expander.
2324 Analyze_Type_Conversion (N);
2328 if Is_Overloadable (U_N) then
2329 Process_Function_Call;
2331 elsif Ekind (Etype (P)) = E_Subprogram_Type
2332 or else (Is_Access_Type (Etype (P))
2334 Ekind (Designated_Type (Etype (P))) =
2337 -- Call to access_to-subprogram with possible implicit dereference
2339 Process_Function_Call;
2341 elsif Is_Generic_Subprogram (U_N) then
2343 -- A common beginner's (or C++ templates fan) error
2345 Error_Msg_N ("generic subprogram cannot be called", N);
2346 Set_Etype (N, Any_Type);
2350 Process_Indexed_Component_Or_Slice;
2353 -- If not an entity name, prefix is an expression that may denote
2354 -- an array or an access-to-subprogram.
2357 if Ekind (P_T) = E_Subprogram_Type
2358 or else (Is_Access_Type (P_T)
2360 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2362 Process_Function_Call;
2364 elsif Nkind (P) = N_Selected_Component
2365 and then Is_Overloadable (Entity (Selector_Name (P)))
2367 Process_Function_Call;
2370 -- Indexed component, slice, or a call to a member of a family
2371 -- entry, which will be converted to an entry call later.
2373 Process_Indexed_Component_Or_Slice;
2376 end Analyze_Indexed_Component_Form;
2378 ------------------------
2379 -- Analyze_Logical_Op --
2380 ------------------------
2382 procedure Analyze_Logical_Op (N : Node_Id) is
2383 L : constant Node_Id := Left_Opnd (N);
2384 R : constant Node_Id := Right_Opnd (N);
2385 Op_Id : Entity_Id := Entity (N);
2388 Set_Etype (N, Any_Type);
2389 Candidate_Type := Empty;
2391 Analyze_Expression (L);
2392 Analyze_Expression (R);
2394 if Present (Op_Id) then
2396 if Ekind (Op_Id) = E_Operator then
2397 Find_Boolean_Types (L, R, Op_Id, N);
2399 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2403 Op_Id := Get_Name_Entity_Id (Chars (N));
2404 while Present (Op_Id) loop
2405 if Ekind (Op_Id) = E_Operator then
2406 Find_Boolean_Types (L, R, Op_Id, N);
2408 Analyze_User_Defined_Binary_Op (N, Op_Id);
2411 Op_Id := Homonym (Op_Id);
2416 end Analyze_Logical_Op;
2418 ---------------------------
2419 -- Analyze_Membership_Op --
2420 ---------------------------
2422 procedure Analyze_Membership_Op (N : Node_Id) is
2423 Loc : constant Source_Ptr := Sloc (N);
2424 L : constant Node_Id := Left_Opnd (N);
2425 R : constant Node_Id := Right_Opnd (N);
2427 Index : Interp_Index;
2429 Found : Boolean := False;
2433 procedure Try_One_Interp (T1 : Entity_Id);
2434 -- Routine to try one proposed interpretation. Note that the context
2435 -- of the operation plays no role in resolving the arguments, so that
2436 -- if there is more than one interpretation of the operands that is
2437 -- compatible with a membership test, the operation is ambiguous.
2439 --------------------
2440 -- Try_One_Interp --
2441 --------------------
2443 procedure Try_One_Interp (T1 : Entity_Id) is
2445 if Has_Compatible_Type (R, T1) then
2447 and then Base_Type (T1) /= Base_Type (T_F)
2449 It := Disambiguate (L, I_F, Index, Any_Type);
2451 if It = No_Interp then
2452 Ambiguous_Operands (N);
2453 Set_Etype (L, Any_Type);
2470 procedure Analyze_Set_Membership;
2471 -- If a set of alternatives is present, analyze each and find the
2472 -- common type to which they must all resolve.
2474 ----------------------------
2475 -- Analyze_Set_Membership --
2476 ----------------------------
2478 procedure Analyze_Set_Membership is
2480 Index : Interp_Index;
2482 Candidate_Interps : Node_Id;
2483 Common_Type : Entity_Id := Empty;
2487 Candidate_Interps := L;
2489 if not Is_Overloaded (L) then
2490 Common_Type := Etype (L);
2492 Alt := First (Alternatives (N));
2493 while Present (Alt) loop
2496 if not Has_Compatible_Type (Alt, Common_Type) then
2497 Wrong_Type (Alt, Common_Type);
2504 Alt := First (Alternatives (N));
2505 while Present (Alt) loop
2507 if not Is_Overloaded (Alt) then
2508 Common_Type := Etype (Alt);
2511 Get_First_Interp (Alt, Index, It);
2512 while Present (It.Typ) loop
2514 Has_Compatible_Type (Candidate_Interps, It.Typ)
2516 Remove_Interp (Index);
2519 Get_Next_Interp (Index, It);
2522 Get_First_Interp (Alt, Index, It);
2525 Error_Msg_N ("alternative has no legal type", Alt);
2529 -- If alternative is not overloaded, we have a unique type
2532 Set_Etype (Alt, It.Typ);
2533 Get_Next_Interp (Index, It);
2536 Set_Is_Overloaded (Alt, False);
2537 Common_Type := Etype (Alt);
2540 Candidate_Interps := Alt;
2547 Set_Etype (N, Standard_Boolean);
2549 if Present (Common_Type) then
2550 Set_Etype (L, Common_Type);
2551 Set_Is_Overloaded (L, False);
2554 Error_Msg_N ("cannot resolve membership operation", N);
2556 end Analyze_Set_Membership;
2558 -- Start of processing for Analyze_Membership_Op
2561 Analyze_Expression (L);
2564 and then Ada_Version >= Ada_2012
2566 Analyze_Set_Membership;
2570 if Nkind (R) = N_Range
2571 or else (Nkind (R) = N_Attribute_Reference
2572 and then Attribute_Name (R) = Name_Range)
2576 if not Is_Overloaded (L) then
2577 Try_One_Interp (Etype (L));
2580 Get_First_Interp (L, Index, It);
2581 while Present (It.Typ) loop
2582 Try_One_Interp (It.Typ);
2583 Get_Next_Interp (Index, It);
2587 -- If not a range, it can be a subtype mark, or else it is a degenerate
2588 -- membership test with a singleton value, i.e. a test for equality,
2589 -- if the types are compatible.
2594 if Is_Entity_Name (R)
2595 and then Is_Type (Entity (R))
2598 Check_Fully_Declared (Entity (R), R);
2600 elsif Ada_Version >= Ada_2012
2601 and then Has_Compatible_Type (R, Etype (L))
2603 if Nkind (N) = N_In then
2619 -- In all versions of the language, if we reach this point there
2620 -- is a previous error that will be diagnosed below.
2626 -- Compatibility between expression and subtype mark or range is
2627 -- checked during resolution. The result of the operation is Boolean
2630 Set_Etype (N, Standard_Boolean);
2632 if Comes_From_Source (N)
2633 and then Present (Right_Opnd (N))
2634 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2636 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2638 end Analyze_Membership_Op;
2640 ----------------------
2641 -- Analyze_Negation --
2642 ----------------------
2644 procedure Analyze_Negation (N : Node_Id) is
2645 R : constant Node_Id := Right_Opnd (N);
2646 Op_Id : Entity_Id := Entity (N);
2649 Set_Etype (N, Any_Type);
2650 Candidate_Type := Empty;
2652 Analyze_Expression (R);
2654 if Present (Op_Id) then
2655 if Ekind (Op_Id) = E_Operator then
2656 Find_Negation_Types (R, Op_Id, N);
2658 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2662 Op_Id := Get_Name_Entity_Id (Chars (N));
2663 while Present (Op_Id) loop
2664 if Ekind (Op_Id) = E_Operator then
2665 Find_Negation_Types (R, Op_Id, N);
2667 Analyze_User_Defined_Unary_Op (N, Op_Id);
2670 Op_Id := Homonym (Op_Id);
2675 end Analyze_Negation;
2681 procedure Analyze_Null (N : Node_Id) is
2683 Check_SPARK_Restriction ("null is not allowed", N);
2685 Set_Etype (N, Any_Access);
2688 ----------------------
2689 -- Analyze_One_Call --
2690 ----------------------
2692 procedure Analyze_One_Call
2696 Success : out Boolean;
2697 Skip_First : Boolean := False)
2699 Actuals : constant List_Id := Parameter_Associations (N);
2700 Prev_T : constant Entity_Id := Etype (N);
2702 Must_Skip : constant Boolean := Skip_First
2703 or else Nkind (Original_Node (N)) = N_Selected_Component
2705 (Nkind (Original_Node (N)) = N_Indexed_Component
2706 and then Nkind (Prefix (Original_Node (N)))
2707 = N_Selected_Component);
2708 -- The first formal must be omitted from the match when trying to find
2709 -- a primitive operation that is a possible interpretation, and also
2710 -- after the call has been rewritten, because the corresponding actual
2711 -- is already known to be compatible, and because this may be an
2712 -- indexing of a call with default parameters.
2716 Is_Indexed : Boolean := False;
2717 Is_Indirect : Boolean := False;
2718 Subp_Type : constant Entity_Id := Etype (Nam);
2721 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2722 -- There may be a user-defined operator that hides the current
2723 -- interpretation. We must check for this independently of the
2724 -- analysis of the call with the user-defined operation, because
2725 -- the parameter names may be wrong and yet the hiding takes place.
2726 -- This fixes a problem with ACATS test B34014O.
2728 -- When the type Address is a visible integer type, and the DEC
2729 -- system extension is visible, the predefined operator may be
2730 -- hidden as well, by one of the address operations in auxdec.
2731 -- Finally, The abstract operations on address do not hide the
2732 -- predefined operator (this is the purpose of making them abstract).
2734 procedure Indicate_Name_And_Type;
2735 -- If candidate interpretation matches, indicate name and type of
2736 -- result on call node.
2738 ----------------------------
2739 -- Indicate_Name_And_Type --
2740 ----------------------------
2742 procedure Indicate_Name_And_Type is
2744 Add_One_Interp (N, Nam, Etype (Nam));
2745 Check_Implicit_Dereference (N, Etype (Nam));
2748 -- If the prefix of the call is a name, indicate the entity
2749 -- being called. If it is not a name, it is an expression that
2750 -- denotes an access to subprogram or else an entry or family. In
2751 -- the latter case, the name is a selected component, and the entity
2752 -- being called is noted on the selector.
2754 if not Is_Type (Nam) then
2755 if Is_Entity_Name (Name (N)) then
2756 Set_Entity (Name (N), Nam);
2758 elsif Nkind (Name (N)) = N_Selected_Component then
2759 Set_Entity (Selector_Name (Name (N)), Nam);
2763 if Debug_Flag_E and not Report then
2764 Write_Str (" Overloaded call ");
2765 Write_Int (Int (N));
2766 Write_Str (" compatible with ");
2767 Write_Int (Int (Nam));
2770 end Indicate_Name_And_Type;
2772 ------------------------
2773 -- Operator_Hidden_By --
2774 ------------------------
2776 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2777 Act1 : constant Node_Id := First_Actual (N);
2778 Act2 : constant Node_Id := Next_Actual (Act1);
2779 Form1 : constant Entity_Id := First_Formal (Fun);
2780 Form2 : constant Entity_Id := Next_Formal (Form1);
2783 if Ekind (Fun) /= E_Function
2784 or else Is_Abstract_Subprogram (Fun)
2788 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2791 elsif Present (Form2) then
2793 No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2798 elsif Present (Act2) then
2802 -- Now we know that the arity of the operator matches the function,
2803 -- and the function call is a valid interpretation. The function
2804 -- hides the operator if it has the right signature, or if one of
2805 -- its operands is a non-abstract operation on Address when this is
2806 -- a visible integer type.
2808 return Hides_Op (Fun, Nam)
2809 or else Is_Descendent_Of_Address (Etype (Form1))
2812 and then Is_Descendent_Of_Address (Etype (Form2)));
2813 end Operator_Hidden_By;
2815 -- Start of processing for Analyze_One_Call
2820 -- If the subprogram has no formals or if all the formals have defaults,
2821 -- and the return type is an array type, the node may denote an indexing
2822 -- of the result of a parameterless call. In Ada 2005, the subprogram
2823 -- may have one non-defaulted formal, and the call may have been written
2824 -- in prefix notation, so that the rebuilt parameter list has more than
2827 if not Is_Overloadable (Nam)
2828 and then Ekind (Nam) /= E_Subprogram_Type
2829 and then Ekind (Nam) /= E_Entry_Family
2834 -- An indexing requires at least one actual
2836 if not Is_Empty_List (Actuals)
2838 (Needs_No_Actuals (Nam)
2840 (Needs_One_Actual (Nam)
2841 and then Present (Next_Actual (First (Actuals)))))
2843 if Is_Array_Type (Subp_Type) then
2844 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2846 elsif Is_Access_Type (Subp_Type)
2847 and then Is_Array_Type (Designated_Type (Subp_Type))
2851 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2853 -- The prefix can also be a parameterless function that returns an
2854 -- access to subprogram, in which case this is an indirect call.
2855 -- If this succeeds, an explicit dereference is added later on,
2856 -- in Analyze_Call or Resolve_Call.
2858 elsif Is_Access_Type (Subp_Type)
2859 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2861 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
2866 -- If the call has been transformed into a slice, it is of the form
2867 -- F (Subtype) where F is parameterless. The node has been rewritten in
2868 -- Try_Indexed_Call and there is nothing else to do.
2871 and then Nkind (N) = N_Slice
2877 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
2881 -- If an indirect call is a possible interpretation, indicate
2882 -- success to the caller.
2888 -- Mismatch in number or names of parameters
2890 elsif Debug_Flag_E then
2891 Write_Str (" normalization fails in call ");
2892 Write_Int (Int (N));
2893 Write_Str (" with subprogram ");
2894 Write_Int (Int (Nam));
2898 -- If the context expects a function call, discard any interpretation
2899 -- that is a procedure. If the node is not overloaded, leave as is for
2900 -- better error reporting when type mismatch is found.
2902 elsif Nkind (N) = N_Function_Call
2903 and then Is_Overloaded (Name (N))
2904 and then Ekind (Nam) = E_Procedure
2908 -- Ditto for function calls in a procedure context
2910 elsif Nkind (N) = N_Procedure_Call_Statement
2911 and then Is_Overloaded (Name (N))
2912 and then Etype (Nam) /= Standard_Void_Type
2916 elsif No (Actuals) then
2918 -- If Normalize succeeds, then there are default parameters for
2921 Indicate_Name_And_Type;
2923 elsif Ekind (Nam) = E_Operator then
2924 if Nkind (N) = N_Procedure_Call_Statement then
2928 -- This can occur when the prefix of the call is an operator
2929 -- name or an expanded name whose selector is an operator name.
2931 Analyze_Operator_Call (N, Nam);
2933 if Etype (N) /= Prev_T then
2935 -- Check that operator is not hidden by a function interpretation
2937 if Is_Overloaded (Name (N)) then
2943 Get_First_Interp (Name (N), I, It);
2944 while Present (It.Nam) loop
2945 if Operator_Hidden_By (It.Nam) then
2946 Set_Etype (N, Prev_T);
2950 Get_Next_Interp (I, It);
2955 -- If operator matches formals, record its name on the call.
2956 -- If the operator is overloaded, Resolve will select the
2957 -- correct one from the list of interpretations. The call
2958 -- node itself carries the first candidate.
2960 Set_Entity (Name (N), Nam);
2963 elsif Report and then Etype (N) = Any_Type then
2964 Error_Msg_N ("incompatible arguments for operator", N);
2968 -- Normalize_Actuals has chained the named associations in the
2969 -- correct order of the formals.
2971 Actual := First_Actual (N);
2972 Formal := First_Formal (Nam);
2974 -- If we are analyzing a call rewritten from object notation, skip
2975 -- first actual, which may be rewritten later as an explicit
2979 Next_Actual (Actual);
2980 Next_Formal (Formal);
2983 while Present (Actual) and then Present (Formal) loop
2984 if Nkind (Parent (Actual)) /= N_Parameter_Association
2985 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2987 -- The actual can be compatible with the formal, but we must
2988 -- also check that the context is not an address type that is
2989 -- visibly an integer type, as is the case in VMS_64. In this
2990 -- case the use of literals is illegal, except in the body of
2991 -- descendents of system, where arithmetic operations on
2992 -- address are of course used.
2994 if Has_Compatible_Type (Actual, Etype (Formal))
2996 (Etype (Actual) /= Universal_Integer
2997 or else not Is_Descendent_Of_Address (Etype (Formal))
2999 Is_Predefined_File_Name
3000 (Unit_File_Name (Get_Source_Unit (N))))
3002 Next_Actual (Actual);
3003 Next_Formal (Formal);
3006 if Debug_Flag_E then
3007 Write_Str (" type checking fails in call ");
3008 Write_Int (Int (N));
3009 Write_Str (" with formal ");
3010 Write_Int (Int (Formal));
3011 Write_Str (" in subprogram ");
3012 Write_Int (Int (Nam));
3016 if Report and not Is_Indexed and not Is_Indirect then
3018 -- Ada 2005 (AI-251): Complete the error notification
3019 -- to help new Ada 2005 users.
3021 if Is_Class_Wide_Type (Etype (Formal))
3022 and then Is_Interface (Etype (Etype (Formal)))
3023 and then not Interface_Present_In_Ancestor
3024 (Typ => Etype (Actual),
3025 Iface => Etype (Etype (Formal)))
3028 ("(Ada 2005) does not implement interface }",
3029 Actual, Etype (Etype (Formal)));
3032 Wrong_Type (Actual, Etype (Formal));
3034 if Nkind (Actual) = N_Op_Eq
3035 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3037 Formal := First_Formal (Nam);
3038 while Present (Formal) loop
3039 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3040 Error_Msg_N -- CODEFIX
3041 ("possible misspelling of `='>`!", Actual);
3045 Next_Formal (Formal);
3049 if All_Errors_Mode then
3050 Error_Msg_Sloc := Sloc (Nam);
3052 if Etype (Formal) = Any_Type then
3054 ("there is no legal actual parameter", Actual);
3057 if Is_Overloadable (Nam)
3058 and then Present (Alias (Nam))
3059 and then not Comes_From_Source (Nam)
3062 ("\\ =='> in call to inherited operation & #!",
3065 elsif Ekind (Nam) = E_Subprogram_Type then
3067 Access_To_Subprogram_Typ :
3068 constant Entity_Id :=
3070 (Associated_Node_For_Itype (Nam));
3073 "\\ =='> in call to dereference of &#!",
3074 Actual, Access_To_Subprogram_Typ);
3079 ("\\ =='> in call to &#!", Actual, Nam);
3089 -- Normalize_Actuals has verified that a default value exists
3090 -- for this formal. Current actual names a subsequent formal.
3092 Next_Formal (Formal);
3096 -- On exit, all actuals match
3098 Indicate_Name_And_Type;
3100 end Analyze_One_Call;
3102 ---------------------------
3103 -- Analyze_Operator_Call --
3104 ---------------------------
3106 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3107 Op_Name : constant Name_Id := Chars (Op_Id);
3108 Act1 : constant Node_Id := First_Actual (N);
3109 Act2 : constant Node_Id := Next_Actual (Act1);
3112 -- Binary operator case
3114 if Present (Act2) then
3116 -- If more than two operands, then not binary operator after all
3118 if Present (Next_Actual (Act2)) then
3122 -- Otherwise action depends on operator
3132 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3137 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3143 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3147 Find_Equality_Types (Act1, Act2, Op_Id, N);
3149 when Name_Op_Concat =>
3150 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3152 -- Is this when others, or should it be an abort???
3158 -- Unary operator case
3162 when Name_Op_Subtract |
3165 Find_Unary_Types (Act1, Op_Id, N);
3168 Find_Negation_Types (Act1, Op_Id, N);
3170 -- Is this when others correct, or should it be an abort???
3176 end Analyze_Operator_Call;
3178 -------------------------------------------
3179 -- Analyze_Overloaded_Selected_Component --
3180 -------------------------------------------
3182 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3183 Nam : constant Node_Id := Prefix (N);
3184 Sel : constant Node_Id := Selector_Name (N);
3191 Set_Etype (Sel, Any_Type);
3193 Get_First_Interp (Nam, I, It);
3194 while Present (It.Typ) loop
3195 if Is_Access_Type (It.Typ) then
3196 T := Designated_Type (It.Typ);
3197 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3202 -- Locate the component. For a private prefix the selector can denote
3205 if Is_Record_Type (T) or else Is_Private_Type (T) then
3207 -- If the prefix is a class-wide type, the visible components are
3208 -- those of the base type.
3210 if Is_Class_Wide_Type (T) then
3214 Comp := First_Entity (T);
3215 while Present (Comp) loop
3216 if Chars (Comp) = Chars (Sel)
3217 and then Is_Visible_Component (Comp)
3220 -- AI05-105: if the context is an object renaming with
3221 -- an anonymous access type, the expected type of the
3222 -- object must be anonymous. This is a name resolution rule.
3224 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3225 or else No (Access_Definition (Parent (N)))
3226 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3228 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3230 Set_Entity (Sel, Comp);
3231 Set_Etype (Sel, Etype (Comp));
3232 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3233 Check_Implicit_Dereference (N, Etype (Comp));
3235 -- This also specifies a candidate to resolve the name.
3236 -- Further overloading will be resolved from context.
3237 -- The selector name itself does not carry overloading
3240 Set_Etype (Nam, It.Typ);
3243 -- Named access type in the context of a renaming
3244 -- declaration with an access definition. Remove
3245 -- inapplicable candidate.
3254 elsif Is_Concurrent_Type (T) then
3255 Comp := First_Entity (T);
3256 while Present (Comp)
3257 and then Comp /= First_Private_Entity (T)
3259 if Chars (Comp) = Chars (Sel) then
3260 if Is_Overloadable (Comp) then
3261 Add_One_Interp (Sel, Comp, Etype (Comp));
3263 Set_Entity_With_Style_Check (Sel, Comp);
3264 Generate_Reference (Comp, Sel);
3267 Set_Etype (Sel, Etype (Comp));
3268 Set_Etype (N, Etype (Comp));
3269 Set_Etype (Nam, It.Typ);
3271 -- For access type case, introduce explicit dereference for
3272 -- more uniform treatment of entry calls. Do this only once
3273 -- if several interpretations yield an access type.
3275 if Is_Access_Type (Etype (Nam))
3276 and then Nkind (Nam) /= N_Explicit_Dereference
3278 Insert_Explicit_Dereference (Nam);
3280 (Warn_On_Dereference, "?implicit dereference", N);
3287 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3290 Get_Next_Interp (I, It);
3293 if Etype (N) = Any_Type
3294 and then not Try_Object_Operation (N)
3296 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3297 Set_Entity (Sel, Any_Id);
3298 Set_Etype (Sel, Any_Type);
3300 end Analyze_Overloaded_Selected_Component;
3302 ----------------------------------
3303 -- Analyze_Qualified_Expression --
3304 ----------------------------------
3306 procedure Analyze_Qualified_Expression (N : Node_Id) is
3307 Mark : constant Entity_Id := Subtype_Mark (N);
3308 Expr : constant Node_Id := Expression (N);
3314 Analyze_Expression (Expr);
3316 Set_Etype (N, Any_Type);
3321 if T = Any_Type then
3325 Check_Fully_Declared (T, N);
3327 -- If expected type is class-wide, check for exact match before
3328 -- expansion, because if the expression is a dispatching call it
3329 -- may be rewritten as explicit dereference with class-wide result.
3330 -- If expression is overloaded, retain only interpretations that
3331 -- will yield exact matches.
3333 if Is_Class_Wide_Type (T) then
3334 if not Is_Overloaded (Expr) then
3335 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3336 if Nkind (Expr) = N_Aggregate then
3337 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3339 Wrong_Type (Expr, T);
3344 Get_First_Interp (Expr, I, It);
3346 while Present (It.Nam) loop
3347 if Base_Type (It.Typ) /= Base_Type (T) then
3351 Get_Next_Interp (I, It);
3357 end Analyze_Qualified_Expression;
3359 -----------------------------------
3360 -- Analyze_Quantified_Expression --
3361 -----------------------------------
3363 procedure Analyze_Quantified_Expression (N : Node_Id) is
3364 Loc : constant Source_Ptr := Sloc (N);
3365 Ent : constant Entity_Id :=
3367 (E_Loop, Current_Scope, Sloc (N), 'L');
3372 Set_Etype (Ent, Standard_Void_Type);
3373 Set_Scope (Ent, Current_Scope);
3374 Set_Parent (Ent, N);
3376 Check_SPARK_Restriction ("quantified expression is not allowed", N);
3378 -- If expansion is enabled (and not in Alfa mode), the condition is
3379 -- analyzed after rewritten as a loop. So we only need to set the type.
3381 if Operating_Mode /= Check_Semantics
3382 and then not Alfa_Mode
3384 Set_Etype (N, Standard_Boolean);
3388 if Present (Loop_Parameter_Specification (N)) then
3390 Make_Iteration_Scheme (Loc,
3391 Loop_Parameter_Specification =>
3392 Loop_Parameter_Specification (N));
3395 Make_Iteration_Scheme (Loc,
3396 Iterator_Specification =>
3397 Iterator_Specification (N));
3401 Set_Parent (Iterator, N);
3402 Analyze_Iteration_Scheme (Iterator);
3404 -- The loop specification may have been converted into an iterator
3405 -- specification during its analysis. Update the quantified node
3408 if Present (Iterator_Specification (Iterator)) then
3409 Set_Iterator_Specification
3410 (N, Iterator_Specification (Iterator));
3411 Set_Loop_Parameter_Specification (N, Empty);
3414 Analyze (Condition (N));
3416 Set_Etype (N, Standard_Boolean);
3417 end Analyze_Quantified_Expression;
3423 procedure Analyze_Range (N : Node_Id) is
3424 L : constant Node_Id := Low_Bound (N);
3425 H : constant Node_Id := High_Bound (N);
3426 I1, I2 : Interp_Index;
3429 procedure Check_Common_Type (T1, T2 : Entity_Id);
3430 -- Verify the compatibility of two types, and choose the
3431 -- non universal one if the other is universal.
3433 procedure Check_High_Bound (T : Entity_Id);
3434 -- Test one interpretation of the low bound against all those
3435 -- of the high bound.
3437 procedure Check_Universal_Expression (N : Node_Id);
3438 -- In Ada 83, reject bounds of a universal range that are not literals
3441 -----------------------
3442 -- Check_Common_Type --
3443 -----------------------
3445 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3447 if Covers (T1 => T1, T2 => T2)
3449 Covers (T1 => T2, T2 => T1)
3451 if T1 = Universal_Integer
3452 or else T1 = Universal_Real
3453 or else T1 = Any_Character
3455 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3458 Add_One_Interp (N, T1, T1);
3461 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3464 end Check_Common_Type;
3466 ----------------------
3467 -- Check_High_Bound --
3468 ----------------------
3470 procedure Check_High_Bound (T : Entity_Id) is
3472 if not Is_Overloaded (H) then
3473 Check_Common_Type (T, Etype (H));
3475 Get_First_Interp (H, I2, It2);
3476 while Present (It2.Typ) loop
3477 Check_Common_Type (T, It2.Typ);
3478 Get_Next_Interp (I2, It2);
3481 end Check_High_Bound;
3483 -----------------------------
3484 -- Is_Universal_Expression --
3485 -----------------------------
3487 procedure Check_Universal_Expression (N : Node_Id) is
3489 if Etype (N) = Universal_Integer
3490 and then Nkind (N) /= N_Integer_Literal
3491 and then not Is_Entity_Name (N)
3492 and then Nkind (N) /= N_Attribute_Reference
3494 Error_Msg_N ("illegal bound in discrete range", N);
3496 end Check_Universal_Expression;
3498 -- Start of processing for Analyze_Range
3501 Set_Etype (N, Any_Type);
3502 Analyze_Expression (L);
3503 Analyze_Expression (H);
3505 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3509 if not Is_Overloaded (L) then
3510 Check_High_Bound (Etype (L));
3512 Get_First_Interp (L, I1, It1);
3513 while Present (It1.Typ) loop
3514 Check_High_Bound (It1.Typ);
3515 Get_Next_Interp (I1, It1);
3519 -- If result is Any_Type, then we did not find a compatible pair
3521 if Etype (N) = Any_Type then
3522 Error_Msg_N ("incompatible types in range ", N);
3526 if Ada_Version = Ada_83
3528 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3529 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3531 Check_Universal_Expression (L);
3532 Check_Universal_Expression (H);
3536 -----------------------
3537 -- Analyze_Reference --
3538 -----------------------
3540 procedure Analyze_Reference (N : Node_Id) is
3541 P : constant Node_Id := Prefix (N);
3544 Acc_Type : Entity_Id;
3549 -- An interesting error check, if we take the 'Reference of an object
3550 -- for which a pragma Atomic or Volatile has been given, and the type
3551 -- of the object is not Atomic or Volatile, then we are in trouble. The
3552 -- problem is that no trace of the atomic/volatile status will remain
3553 -- for the backend to respect when it deals with the resulting pointer,
3554 -- since the pointer type will not be marked atomic (it is a pointer to
3555 -- the base type of the object).
3557 -- It is not clear if that can ever occur, but in case it does, we will
3558 -- generate an error message. Not clear if this message can ever be
3559 -- generated, and pretty clear that it represents a bug if it is, still
3560 -- seems worth checking, except in CodePeer mode where we do not really
3561 -- care and don't want to bother the user.
3565 if Is_Entity_Name (P)
3566 and then Is_Object_Reference (P)
3567 and then not CodePeer_Mode
3572 if (Has_Atomic_Components (E)
3573 and then not Has_Atomic_Components (T))
3575 (Has_Volatile_Components (E)
3576 and then not Has_Volatile_Components (T))
3577 or else (Is_Atomic (E) and then not Is_Atomic (T))
3578 or else (Is_Volatile (E) and then not Is_Volatile (T))
3580 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3584 -- Carry on with normal processing
3586 Acc_Type := Create_Itype (E_Allocator_Type, N);
3587 Set_Etype (Acc_Type, Acc_Type);
3588 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3589 Set_Etype (N, Acc_Type);
3590 end Analyze_Reference;
3592 --------------------------------
3593 -- Analyze_Selected_Component --
3594 --------------------------------
3596 -- Prefix is a record type or a task or protected type. In the latter case,
3597 -- the selector must denote a visible entry.
3599 procedure Analyze_Selected_Component (N : Node_Id) is
3600 Name : constant Node_Id := Prefix (N);
3601 Sel : constant Node_Id := Selector_Name (N);
3604 Has_Candidate : Boolean := False;
3607 Pent : Entity_Id := Empty;
3608 Prefix_Type : Entity_Id;
3610 Type_To_Use : Entity_Id;
3611 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3612 -- a class-wide type, we use its root type, whose components are
3613 -- present in the class-wide type.
3615 Is_Single_Concurrent_Object : Boolean;
3616 -- Set True if the prefix is a single task or a single protected object
3618 procedure Find_Component_In_Instance (Rec : Entity_Id);
3619 -- In an instance, a component of a private extension may not be visible
3620 -- while it was visible in the generic. Search candidate scope for a
3621 -- component with the proper identifier. This is only done if all other
3622 -- searches have failed. When the match is found (it always will be),
3623 -- the Etype of both N and Sel are set from this component, and the
3624 -- entity of Sel is set to reference this component.
3626 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3627 -- It is known that the parent of N denotes a subprogram call. Comp
3628 -- is an overloadable component of the concurrent type of the prefix.
3629 -- Determine whether all formals of the parent of N and Comp are mode
3630 -- conformant. If the parent node is not analyzed yet it may be an
3631 -- indexed component rather than a function call.
3633 --------------------------------
3634 -- Find_Component_In_Instance --
3635 --------------------------------
3637 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3641 Comp := First_Component (Rec);
3642 while Present (Comp) loop
3643 if Chars (Comp) = Chars (Sel) then
3644 Set_Entity_With_Style_Check (Sel, Comp);
3645 Set_Etype (Sel, Etype (Comp));
3646 Set_Etype (N, Etype (Comp));
3650 Next_Component (Comp);
3653 -- This must succeed because code was legal in the generic
3655 raise Program_Error;
3656 end Find_Component_In_Instance;
3658 ------------------------------
3659 -- Has_Mode_Conformant_Spec --
3660 ------------------------------
3662 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3663 Comp_Param : Entity_Id;
3665 Param_Typ : Entity_Id;
3668 Comp_Param := First_Formal (Comp);
3670 if Nkind (Parent (N)) = N_Indexed_Component then
3671 Param := First (Expressions (Parent (N)));
3673 Param := First (Parameter_Associations (Parent (N)));
3676 while Present (Comp_Param)
3677 and then Present (Param)
3679 Param_Typ := Find_Parameter_Type (Param);
3681 if Present (Param_Typ)
3683 not Conforming_Types
3684 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
3689 Next_Formal (Comp_Param);
3693 -- One of the specs has additional formals
3695 if Present (Comp_Param) or else Present (Param) then
3700 end Has_Mode_Conformant_Spec;
3702 -- Start of processing for Analyze_Selected_Component
3705 Set_Etype (N, Any_Type);
3707 if Is_Overloaded (Name) then
3708 Analyze_Overloaded_Selected_Component (N);
3711 elsif Etype (Name) = Any_Type then
3712 Set_Entity (Sel, Any_Id);
3713 Set_Etype (Sel, Any_Type);
3717 Prefix_Type := Etype (Name);
3720 if Is_Access_Type (Prefix_Type) then
3722 -- A RACW object can never be used as prefix of a selected component
3723 -- since that means it is dereferenced without being a controlling
3724 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
3725 -- reporting an error, we must check whether this is actually a
3726 -- dispatching call in prefix form.
3728 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
3729 and then Comes_From_Source (N)
3731 if Try_Object_Operation (N) then
3735 ("invalid dereference of a remote access-to-class-wide value",
3739 -- Normal case of selected component applied to access type
3742 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3744 if Is_Entity_Name (Name) then
3745 Pent := Entity (Name);
3746 elsif Nkind (Name) = N_Selected_Component
3747 and then Is_Entity_Name (Selector_Name (Name))
3749 Pent := Entity (Selector_Name (Name));
3752 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3755 -- If we have an explicit dereference of a remote access-to-class-wide
3756 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
3757 -- have to check for the case of a prefix that is a controlling operand
3758 -- of a prefixed dispatching call, as the dereference is legal in that
3759 -- case. Normally this condition is checked in Validate_Remote_Access_
3760 -- To_Class_Wide_Type, but we have to defer the checking for selected
3761 -- component prefixes because of the prefixed dispatching call case.
3762 -- Note that implicit dereferences are checked for this just above.
3764 elsif Nkind (Name) = N_Explicit_Dereference
3765 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3766 and then Comes_From_Source (N)
3768 if Try_Object_Operation (N) then
3772 ("invalid dereference of a remote access-to-class-wide value",
3777 -- (Ada 2005): if the prefix is the limited view of a type, and
3778 -- the context already includes the full view, use the full view
3779 -- in what follows, either to retrieve a component of to find
3780 -- a primitive operation. If the prefix is an explicit dereference,
3781 -- set the type of the prefix to reflect this transformation.
3782 -- If the non-limited view is itself an incomplete type, get the
3783 -- full view if available.
3785 if Is_Incomplete_Type (Prefix_Type)
3786 and then From_With_Type (Prefix_Type)
3787 and then Present (Non_Limited_View (Prefix_Type))
3789 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3791 if Nkind (N) = N_Explicit_Dereference then
3792 Set_Etype (Prefix (N), Prefix_Type);
3795 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3796 and then From_With_Type (Prefix_Type)
3797 and then Present (Non_Limited_View (Etype (Prefix_Type)))
3800 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3802 if Nkind (N) = N_Explicit_Dereference then
3803 Set_Etype (Prefix (N), Prefix_Type);
3807 if Ekind (Prefix_Type) = E_Private_Subtype then
3808 Prefix_Type := Base_Type (Prefix_Type);
3811 Type_To_Use := Prefix_Type;
3813 -- For class-wide types, use the entity list of the root type. This
3814 -- indirection is specially important for private extensions because
3815 -- only the root type get switched (not the class-wide type).
3817 if Is_Class_Wide_Type (Prefix_Type) then
3818 Type_To_Use := Root_Type (Prefix_Type);
3821 -- If the prefix is a single concurrent object, use its name in error
3822 -- messages, rather than that of its anonymous type.
3824 Is_Single_Concurrent_Object :=
3825 Is_Concurrent_Type (Prefix_Type)
3826 and then Is_Internal_Name (Chars (Prefix_Type))
3827 and then not Is_Derived_Type (Prefix_Type)
3828 and then Is_Entity_Name (Name);
3830 Comp := First_Entity (Type_To_Use);
3832 -- If the selector has an original discriminant, the node appears in
3833 -- an instance. Replace the discriminant with the corresponding one
3834 -- in the current discriminated type. For nested generics, this must
3835 -- be done transitively, so note the new original discriminant.
3837 if Nkind (Sel) = N_Identifier
3838 and then In_Instance
3839 and then Present (Original_Discriminant (Sel))
3841 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3843 -- Mark entity before rewriting, for completeness and because
3844 -- subsequent semantic checks might examine the original node.
3846 Set_Entity (Sel, Comp);
3847 Rewrite (Selector_Name (N),
3848 New_Occurrence_Of (Comp, Sloc (N)));
3849 Set_Original_Discriminant (Selector_Name (N), Comp);
3850 Set_Etype (N, Etype (Comp));
3851 Check_Implicit_Dereference (N, Etype (Comp));
3853 if Is_Access_Type (Etype (Name)) then
3854 Insert_Explicit_Dereference (Name);
3855 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3858 elsif Is_Record_Type (Prefix_Type) then
3860 -- Find component with given name
3862 while Present (Comp) loop
3863 if Chars (Comp) = Chars (Sel)
3864 and then Is_Visible_Component (Comp)
3866 Set_Entity_With_Style_Check (Sel, Comp);
3867 Set_Etype (Sel, Etype (Comp));
3869 if Ekind (Comp) = E_Discriminant then
3870 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3872 ("cannot reference discriminant of Unchecked_Union",
3876 if Is_Generic_Type (Prefix_Type)
3878 Is_Generic_Type (Root_Type (Prefix_Type))
3880 Set_Original_Discriminant (Sel, Comp);
3884 -- Resolve the prefix early otherwise it is not possible to
3885 -- build the actual subtype of the component: it may need
3886 -- to duplicate this prefix and duplication is only allowed
3887 -- on fully resolved expressions.
3891 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
3892 -- subtypes in a package specification.
3895 -- limited with Pkg;
3897 -- type Acc_Inc is access Pkg.T;
3899 -- N : Natural := X.all.Comp; -- ERROR, limited view
3900 -- end Pkg; -- Comp is not visible
3902 if Nkind (Name) = N_Explicit_Dereference
3903 and then From_With_Type (Etype (Prefix (Name)))
3904 and then not Is_Potentially_Use_Visible (Etype (Name))
3905 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3906 N_Package_Specification
3909 ("premature usage of incomplete}", Prefix (Name),
3910 Etype (Prefix (Name)));
3913 -- We never need an actual subtype for the case of a selection
3914 -- for a indexed component of a non-packed array, since in
3915 -- this case gigi generates all the checks and can find the
3916 -- necessary bounds information.
3918 -- We also do not need an actual subtype for the case of a
3919 -- first, last, length, or range attribute applied to a
3920 -- non-packed array, since gigi can again get the bounds in
3921 -- these cases (gigi cannot handle the packed case, since it
3922 -- has the bounds of the packed array type, not the original
3923 -- bounds of the type). However, if the prefix is itself a
3924 -- selected component, as in a.b.c (i), gigi may regard a.b.c
3925 -- as a dynamic-sized temporary, so we do generate an actual
3926 -- subtype for this case.
3928 Parent_N := Parent (N);
3930 if not Is_Packed (Etype (Comp))
3932 ((Nkind (Parent_N) = N_Indexed_Component
3933 and then Nkind (Name) /= N_Selected_Component)
3935 (Nkind (Parent_N) = N_Attribute_Reference
3936 and then (Attribute_Name (Parent_N) = Name_First
3938 Attribute_Name (Parent_N) = Name_Last
3940 Attribute_Name (Parent_N) = Name_Length
3942 Attribute_Name (Parent_N) = Name_Range)))
3944 Set_Etype (N, Etype (Comp));
3946 -- If full analysis is not enabled, we do not generate an
3947 -- actual subtype, because in the absence of expansion
3948 -- reference to a formal of a protected type, for example,
3949 -- will not be properly transformed, and will lead to
3950 -- out-of-scope references in gigi.
3952 -- In all other cases, we currently build an actual subtype.
3953 -- It seems likely that many of these cases can be avoided,
3954 -- but right now, the front end makes direct references to the
3955 -- bounds (e.g. in generating a length check), and if we do
3956 -- not make an actual subtype, we end up getting a direct
3957 -- reference to a discriminant, which will not do.
3959 elsif Full_Analysis then
3961 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3962 Insert_Action (N, Act_Decl);
3964 if No (Act_Decl) then
3965 Set_Etype (N, Etype (Comp));
3968 -- Component type depends on discriminants. Enter the
3969 -- main attributes of the subtype.
3972 Subt : constant Entity_Id :=
3973 Defining_Identifier (Act_Decl);
3976 Set_Etype (Subt, Base_Type (Etype (Comp)));
3977 Set_Ekind (Subt, Ekind (Etype (Comp)));
3978 Set_Etype (N, Subt);
3982 -- If Full_Analysis not enabled, just set the Etype
3985 Set_Etype (N, Etype (Comp));
3988 Check_Implicit_Dereference (N, Etype (N));
3992 -- If the prefix is a private extension, check only the visible
3993 -- components of the partial view. This must include the tag,
3994 -- which can appear in expanded code in a tag check.
3996 if Ekind (Type_To_Use) = E_Record_Type_With_Private
3997 and then Chars (Selector_Name (N)) /= Name_uTag
3999 exit when Comp = Last_Entity (Type_To_Use);
4005 -- Ada 2005 (AI-252): The selected component can be interpreted as
4006 -- a prefixed view of a subprogram. Depending on the context, this is
4007 -- either a name that can appear in a renaming declaration, or part
4008 -- of an enclosing call given in prefix form.
4010 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4011 -- selected component should resolve to a name.
4013 if Ada_Version >= Ada_2005
4014 and then Is_Tagged_Type (Prefix_Type)
4015 and then not Is_Concurrent_Type (Prefix_Type)
4017 if Nkind (Parent (N)) = N_Generic_Association
4018 or else Nkind (Parent (N)) = N_Requeue_Statement
4019 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4021 if Find_Primitive_Operation (N) then
4025 elsif Try_Object_Operation (N) then
4029 -- If the transformation fails, it will be necessary to redo the
4030 -- analysis with all errors enabled, to indicate candidate
4031 -- interpretations and reasons for each failure ???
4035 elsif Is_Private_Type (Prefix_Type) then
4037 -- Allow access only to discriminants of the type. If the type has
4038 -- no full view, gigi uses the parent type for the components, so we
4039 -- do the same here.
4041 if No (Full_View (Prefix_Type)) then
4042 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4043 Comp := First_Entity (Type_To_Use);
4046 while Present (Comp) loop
4047 if Chars (Comp) = Chars (Sel) then
4048 if Ekind (Comp) = E_Discriminant then
4049 Set_Entity_With_Style_Check (Sel, Comp);
4050 Generate_Reference (Comp, Sel);
4052 Set_Etype (Sel, Etype (Comp));
4053 Set_Etype (N, Etype (Comp));
4054 Check_Implicit_Dereference (N, Etype (N));
4056 if Is_Generic_Type (Prefix_Type)
4057 or else Is_Generic_Type (Root_Type (Prefix_Type))
4059 Set_Original_Discriminant (Sel, Comp);
4062 -- Before declaring an error, check whether this is tagged
4063 -- private type and a call to a primitive operation.
4065 elsif Ada_Version >= Ada_2005
4066 and then Is_Tagged_Type (Prefix_Type)
4067 and then Try_Object_Operation (N)
4072 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4073 Error_Msg_NE ("invisible selector& for }", N, Sel);
4074 Set_Entity (Sel, Any_Id);
4075 Set_Etype (N, Any_Type);
4084 elsif Is_Concurrent_Type (Prefix_Type) then
4086 -- Find visible operation with given name. For a protected type,
4087 -- the possible candidates are discriminants, entries or protected
4088 -- procedures. For a task type, the set can only include entries or
4089 -- discriminants if the task type is not an enclosing scope. If it
4090 -- is an enclosing scope (e.g. in an inner task) then all entities
4091 -- are visible, but the prefix must denote the enclosing scope, i.e.
4092 -- can only be a direct name or an expanded name.
4094 Set_Etype (Sel, Any_Type);
4095 In_Scope := In_Open_Scopes (Prefix_Type);
4097 while Present (Comp) loop
4098 if Chars (Comp) = Chars (Sel) then
4099 if Is_Overloadable (Comp) then
4100 Add_One_Interp (Sel, Comp, Etype (Comp));
4102 -- If the prefix is tagged, the correct interpretation may
4103 -- lie in the primitive or class-wide operations of the
4104 -- type. Perform a simple conformance check to determine
4105 -- whether Try_Object_Operation should be invoked even if
4106 -- a visible entity is found.
4108 if Is_Tagged_Type (Prefix_Type)
4110 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4112 N_Indexed_Component)
4113 and then Has_Mode_Conformant_Spec (Comp)
4115 Has_Candidate := True;
4118 -- Note: a selected component may not denote a component of a
4119 -- protected type (4.1.3(7)).
4121 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4123 and then not Is_Protected_Type (Prefix_Type)
4124 and then Is_Entity_Name (Name))
4126 Set_Entity_With_Style_Check (Sel, Comp);
4127 Generate_Reference (Comp, Sel);
4129 -- The selector is not overloadable, so we have a candidate
4132 Has_Candidate := True;
4138 Set_Etype (Sel, Etype (Comp));
4139 Set_Etype (N, Etype (Comp));
4141 if Ekind (Comp) = E_Discriminant then
4142 Set_Original_Discriminant (Sel, Comp);
4145 -- For access type case, introduce explicit dereference for
4146 -- more uniform treatment of entry calls.
4148 if Is_Access_Type (Etype (Name)) then
4149 Insert_Explicit_Dereference (Name);
4151 (Warn_On_Dereference, "?implicit dereference", N);
4157 exit when not In_Scope
4159 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4162 -- If there is no visible entity with the given name or none of the
4163 -- visible entities are plausible interpretations, check whether
4164 -- there is some other primitive operation with that name.
4166 if Ada_Version >= Ada_2005
4167 and then Is_Tagged_Type (Prefix_Type)
4169 if (Etype (N) = Any_Type
4170 or else not Has_Candidate)
4171 and then Try_Object_Operation (N)
4175 -- If the context is not syntactically a procedure call, it
4176 -- may be a call to a primitive function declared outside of
4177 -- the synchronized type.
4179 -- If the context is a procedure call, there might still be
4180 -- an overloading between an entry and a primitive procedure
4181 -- declared outside of the synchronized type, called in prefix
4182 -- notation. This is harder to disambiguate because in one case
4183 -- the controlling formal is implicit ???
4185 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4186 and then Nkind (Parent (N)) /= N_Indexed_Component
4187 and then Try_Object_Operation (N)
4192 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4193 -- entry or procedure of a tagged concurrent type we must check
4194 -- if there are class-wide subprograms covering the primitive. If
4195 -- true then Try_Object_Operation reports the error.
4198 and then Is_Concurrent_Type (Prefix_Type)
4199 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4201 -- Duplicate the call. This is required to avoid problems with
4202 -- the tree transformations performed by Try_Object_Operation.
4205 Try_Object_Operation
4206 (N => Sinfo.Name (New_Copy_Tree (Parent (N))),
4207 CW_Test_Only => True)
4213 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4215 -- Case of a prefix of a protected type: selector might denote
4216 -- an invisible private component.
4218 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4219 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4223 if Present (Comp) then
4224 if Is_Single_Concurrent_Object then
4225 Error_Msg_Node_2 := Entity (Name);
4226 Error_Msg_NE ("invisible selector& for &", N, Sel);
4229 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4230 Error_Msg_NE ("invisible selector& for }", N, Sel);
4236 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4241 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4244 -- If N still has no type, the component is not defined in the prefix
4246 if Etype (N) = Any_Type then
4248 if Is_Single_Concurrent_Object then
4249 Error_Msg_Node_2 := Entity (Name);
4250 Error_Msg_NE ("no selector& for&", N, Sel);
4252 Check_Misspelled_Selector (Type_To_Use, Sel);
4254 elsif Is_Generic_Type (Prefix_Type)
4255 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4256 and then Prefix_Type /= Etype (Prefix_Type)
4257 and then Is_Record_Type (Etype (Prefix_Type))
4259 -- If this is a derived formal type, the parent may have
4260 -- different visibility at this point. Try for an inherited
4261 -- component before reporting an error.
4263 Set_Etype (Prefix (N), Etype (Prefix_Type));
4264 Analyze_Selected_Component (N);
4267 -- Similarly, if this is the actual for a formal derived type, the
4268 -- component inherited from the generic parent may not be visible
4269 -- in the actual, but the selected component is legal.
4271 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4272 and then Is_Generic_Actual_Type (Prefix_Type)
4273 and then Present (Full_View (Prefix_Type))
4276 Find_Component_In_Instance
4277 (Generic_Parent_Type (Parent (Prefix_Type)));
4280 -- Finally, the formal and the actual may be private extensions,
4281 -- but the generic is declared in a child unit of the parent, and
4282 -- an additional step is needed to retrieve the proper scope.
4285 and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4287 Find_Component_In_Instance
4288 (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4291 -- Component not found, specialize error message when appropriate
4294 if Ekind (Prefix_Type) = E_Record_Subtype then
4296 -- Check whether this is a component of the base type which
4297 -- is absent from a statically constrained subtype. This will
4298 -- raise constraint error at run time, but is not a compile-
4299 -- time error. When the selector is illegal for base type as
4300 -- well fall through and generate a compilation error anyway.
4302 Comp := First_Component (Base_Type (Prefix_Type));
4303 while Present (Comp) loop
4304 if Chars (Comp) = Chars (Sel)
4305 and then Is_Visible_Component (Comp)
4307 Set_Entity_With_Style_Check (Sel, Comp);
4308 Generate_Reference (Comp, Sel);
4309 Set_Etype (Sel, Etype (Comp));
4310 Set_Etype (N, Etype (Comp));
4312 -- Emit appropriate message. Gigi will replace the
4313 -- node subsequently with the appropriate Raise.
4315 Apply_Compile_Time_Constraint_Error
4316 (N, "component not present in }?",
4317 CE_Discriminant_Check_Failed,
4318 Ent => Prefix_Type, Rep => False);
4319 Set_Raises_Constraint_Error (N);
4323 Next_Component (Comp);
4328 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4329 Error_Msg_NE ("no selector& for}", N, Sel);
4331 -- Add information in the case of an incomplete prefix
4333 if Is_Incomplete_Type (Type_To_Use) then
4335 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4338 if From_With_Type (Scope (Type_To_Use)) then
4340 ("\limited view of& has no components", N, Inc);
4344 ("\premature usage of incomplete type&", N, Inc);
4346 if Nkind (Parent (Inc)) =
4347 N_Incomplete_Type_Declaration
4349 -- Record location of premature use in entity so that
4350 -- a continuation message is generated when the
4351 -- completion is seen.
4353 Set_Premature_Use (Parent (Inc), N);
4359 Check_Misspelled_Selector (Type_To_Use, Sel);
4362 Set_Entity (Sel, Any_Id);
4363 Set_Etype (Sel, Any_Type);
4365 end Analyze_Selected_Component;
4367 ---------------------------
4368 -- Analyze_Short_Circuit --
4369 ---------------------------
4371 procedure Analyze_Short_Circuit (N : Node_Id) is
4372 L : constant Node_Id := Left_Opnd (N);
4373 R : constant Node_Id := Right_Opnd (N);
4378 Analyze_Expression (L);
4379 Analyze_Expression (R);
4380 Set_Etype (N, Any_Type);
4382 if not Is_Overloaded (L) then
4383 if Root_Type (Etype (L)) = Standard_Boolean
4384 and then Has_Compatible_Type (R, Etype (L))
4386 Add_One_Interp (N, Etype (L), Etype (L));
4390 Get_First_Interp (L, Ind, It);
4391 while Present (It.Typ) loop
4392 if Root_Type (It.Typ) = Standard_Boolean
4393 and then Has_Compatible_Type (R, It.Typ)
4395 Add_One_Interp (N, It.Typ, It.Typ);
4398 Get_Next_Interp (Ind, It);
4402 -- Here we have failed to find an interpretation. Clearly we know that
4403 -- it is not the case that both operands can have an interpretation of
4404 -- Boolean, but this is by far the most likely intended interpretation.
4405 -- So we simply resolve both operands as Booleans, and at least one of
4406 -- these resolutions will generate an error message, and we do not need
4407 -- to give another error message on the short circuit operation itself.
4409 if Etype (N) = Any_Type then
4410 Resolve (L, Standard_Boolean);
4411 Resolve (R, Standard_Boolean);
4412 Set_Etype (N, Standard_Boolean);
4414 end Analyze_Short_Circuit;
4420 procedure Analyze_Slice (N : Node_Id) is
4421 P : constant Node_Id := Prefix (N);
4422 D : constant Node_Id := Discrete_Range (N);
4423 Array_Type : Entity_Id;
4425 procedure Analyze_Overloaded_Slice;
4426 -- If the prefix is overloaded, select those interpretations that
4427 -- yield a one-dimensional array type.
4429 ------------------------------
4430 -- Analyze_Overloaded_Slice --
4431 ------------------------------
4433 procedure Analyze_Overloaded_Slice is
4439 Set_Etype (N, Any_Type);
4441 Get_First_Interp (P, I, It);
4442 while Present (It.Nam) loop
4445 if Is_Access_Type (Typ) then
4446 Typ := Designated_Type (Typ);
4447 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4450 if Is_Array_Type (Typ)
4451 and then Number_Dimensions (Typ) = 1
4452 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4454 Add_One_Interp (N, Typ, Typ);
4457 Get_Next_Interp (I, It);
4460 if Etype (N) = Any_Type then
4461 Error_Msg_N ("expect array type in prefix of slice", N);
4463 end Analyze_Overloaded_Slice;
4465 -- Start of processing for Analyze_Slice
4468 if Comes_From_Source (N) then
4469 Check_SPARK_Restriction ("slice is not allowed", N);
4475 if Is_Overloaded (P) then
4476 Analyze_Overloaded_Slice;
4479 Array_Type := Etype (P);
4480 Set_Etype (N, Any_Type);
4482 if Is_Access_Type (Array_Type) then
4483 Array_Type := Designated_Type (Array_Type);
4484 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4487 if not Is_Array_Type (Array_Type) then
4488 Wrong_Type (P, Any_Array);
4490 elsif Number_Dimensions (Array_Type) > 1 then
4492 ("type is not one-dimensional array in slice prefix", N);
4495 Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
4497 Wrong_Type (D, Etype (First_Index (Array_Type)));
4500 Set_Etype (N, Array_Type);
4505 -----------------------------
4506 -- Analyze_Type_Conversion --
4507 -----------------------------
4509 procedure Analyze_Type_Conversion (N : Node_Id) is
4510 Expr : constant Node_Id := Expression (N);
4514 -- If Conversion_OK is set, then the Etype is already set, and the
4515 -- only processing required is to analyze the expression. This is
4516 -- used to construct certain "illegal" conversions which are not
4517 -- allowed by Ada semantics, but can be handled OK by Gigi, see
4518 -- Sinfo for further details.
4520 if Conversion_OK (N) then
4525 -- Otherwise full type analysis is required, as well as some semantic
4526 -- checks to make sure the argument of the conversion is appropriate.
4528 Find_Type (Subtype_Mark (N));
4529 T := Entity (Subtype_Mark (N));
4531 Check_Fully_Declared (T, N);
4532 Analyze_Expression (Expr);
4533 Validate_Remote_Type_Type_Conversion (N);
4535 -- Only remaining step is validity checks on the argument. These
4536 -- are skipped if the conversion does not come from the source.
4538 if not Comes_From_Source (N) then
4541 -- If there was an error in a generic unit, no need to replicate the
4542 -- error message. Conversely, constant-folding in the generic may
4543 -- transform the argument of a conversion into a string literal, which
4544 -- is legal. Therefore the following tests are not performed in an
4547 elsif In_Instance then
4550 elsif Nkind (Expr) = N_Null then
4551 Error_Msg_N ("argument of conversion cannot be null", N);
4552 Error_Msg_N ("\use qualified expression instead", N);
4553 Set_Etype (N, Any_Type);
4555 elsif Nkind (Expr) = N_Aggregate then
4556 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4557 Error_Msg_N ("\use qualified expression instead", N);
4559 elsif Nkind (Expr) = N_Allocator then
4560 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4561 Error_Msg_N ("\use qualified expression instead", N);
4563 elsif Nkind (Expr) = N_String_Literal then
4564 Error_Msg_N ("argument of conversion cannot be string literal", N);
4565 Error_Msg_N ("\use qualified expression instead", N);
4567 elsif Nkind (Expr) = N_Character_Literal then
4568 if Ada_Version = Ada_83 then
4571 Error_Msg_N ("argument of conversion cannot be character literal",
4573 Error_Msg_N ("\use qualified expression instead", N);
4576 elsif Nkind (Expr) = N_Attribute_Reference
4578 (Attribute_Name (Expr) = Name_Access or else
4579 Attribute_Name (Expr) = Name_Unchecked_Access or else
4580 Attribute_Name (Expr) = Name_Unrestricted_Access)
4582 Error_Msg_N ("argument of conversion cannot be access", N);
4583 Error_Msg_N ("\use qualified expression instead", N);
4585 end Analyze_Type_Conversion;
4587 ----------------------
4588 -- Analyze_Unary_Op --
4589 ----------------------
4591 procedure Analyze_Unary_Op (N : Node_Id) is
4592 R : constant Node_Id := Right_Opnd (N);
4593 Op_Id : Entity_Id := Entity (N);
4596 Set_Etype (N, Any_Type);
4597 Candidate_Type := Empty;
4599 Analyze_Expression (R);
4601 if Present (Op_Id) then
4602 if Ekind (Op_Id) = E_Operator then
4603 Find_Unary_Types (R, Op_Id, N);
4605 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4609 Op_Id := Get_Name_Entity_Id (Chars (N));
4610 while Present (Op_Id) loop
4611 if Ekind (Op_Id) = E_Operator then
4612 if No (Next_Entity (First_Entity (Op_Id))) then
4613 Find_Unary_Types (R, Op_Id, N);
4616 elsif Is_Overloadable (Op_Id) then
4617 Analyze_User_Defined_Unary_Op (N, Op_Id);
4620 Op_Id := Homonym (Op_Id);
4625 end Analyze_Unary_Op;
4627 ----------------------------------
4628 -- Analyze_Unchecked_Expression --
4629 ----------------------------------
4631 procedure Analyze_Unchecked_Expression (N : Node_Id) is
4633 Analyze (Expression (N), Suppress => All_Checks);
4634 Set_Etype (N, Etype (Expression (N)));
4635 Save_Interps (Expression (N), N);
4636 end Analyze_Unchecked_Expression;
4638 ---------------------------------------
4639 -- Analyze_Unchecked_Type_Conversion --
4640 ---------------------------------------
4642 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
4644 Find_Type (Subtype_Mark (N));
4645 Analyze_Expression (Expression (N));
4646 Set_Etype (N, Entity (Subtype_Mark (N)));
4647 end Analyze_Unchecked_Type_Conversion;
4649 ------------------------------------
4650 -- Analyze_User_Defined_Binary_Op --
4651 ------------------------------------
4653 procedure Analyze_User_Defined_Binary_Op
4658 -- Only do analysis if the operator Comes_From_Source, since otherwise
4659 -- the operator was generated by the expander, and all such operators
4660 -- always refer to the operators in package Standard.
4662 if Comes_From_Source (N) then
4664 F1 : constant Entity_Id := First_Formal (Op_Id);
4665 F2 : constant Entity_Id := Next_Formal (F1);
4668 -- Verify that Op_Id is a visible binary function. Note that since
4669 -- we know Op_Id is overloaded, potentially use visible means use
4670 -- visible for sure (RM 9.4(11)).
4672 if Ekind (Op_Id) = E_Function
4673 and then Present (F2)
4674 and then (Is_Immediately_Visible (Op_Id)
4675 or else Is_Potentially_Use_Visible (Op_Id))
4676 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
4677 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
4679 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4681 -- If the left operand is overloaded, indicate that the
4682 -- current type is a viable candidate. This is redundant
4683 -- in most cases, but for equality and comparison operators
4684 -- where the context does not impose a type on the operands,
4685 -- setting the proper type is necessary to avoid subsequent
4686 -- ambiguities during resolution, when both user-defined and
4687 -- predefined operators may be candidates.
4689 if Is_Overloaded (Left_Opnd (N)) then
4690 Set_Etype (Left_Opnd (N), Etype (F1));
4693 if Debug_Flag_E then
4694 Write_Str ("user defined operator ");
4695 Write_Name (Chars (Op_Id));
4696 Write_Str (" on node ");
4697 Write_Int (Int (N));
4703 end Analyze_User_Defined_Binary_Op;
4705 -----------------------------------
4706 -- Analyze_User_Defined_Unary_Op --
4707 -----------------------------------
4709 procedure Analyze_User_Defined_Unary_Op
4714 -- Only do analysis if the operator Comes_From_Source, since otherwise
4715 -- the operator was generated by the expander, and all such operators
4716 -- always refer to the operators in package Standard.
4718 if Comes_From_Source (N) then
4720 F : constant Entity_Id := First_Formal (Op_Id);
4723 -- Verify that Op_Id is a visible unary function. Note that since
4724 -- we know Op_Id is overloaded, potentially use visible means use
4725 -- visible for sure (RM 9.4(11)).
4727 if Ekind (Op_Id) = E_Function
4728 and then No (Next_Formal (F))
4729 and then (Is_Immediately_Visible (Op_Id)
4730 or else Is_Potentially_Use_Visible (Op_Id))
4731 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
4733 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4737 end Analyze_User_Defined_Unary_Op;
4739 ---------------------------
4740 -- Check_Arithmetic_Pair --
4741 ---------------------------
4743 procedure Check_Arithmetic_Pair
4744 (T1, T2 : Entity_Id;
4748 Op_Name : constant Name_Id := Chars (Op_Id);
4750 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
4751 -- Check whether the fixed-point type Typ has a user-defined operator
4752 -- (multiplication or division) that should hide the corresponding
4753 -- predefined operator. Used to implement Ada 2005 AI-264, to make
4754 -- such operators more visible and therefore useful.
4756 -- If the name of the operation is an expanded name with prefix
4757 -- Standard, the predefined universal fixed operator is available,
4758 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
4760 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
4761 -- Get specific type (i.e. non-universal type if there is one)
4767 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
4768 Bas : constant Entity_Id := Base_Type (Typ);
4774 -- If the universal_fixed operation is given explicitly the rule
4775 -- concerning primitive operations of the type do not apply.
4777 if Nkind (N) = N_Function_Call
4778 and then Nkind (Name (N)) = N_Expanded_Name
4779 and then Entity (Prefix (Name (N))) = Standard_Standard
4784 -- The operation is treated as primitive if it is declared in the
4785 -- same scope as the type, and therefore on the same entity chain.
4787 Ent := Next_Entity (Typ);
4788 while Present (Ent) loop
4789 if Chars (Ent) = Chars (Op) then
4790 F1 := First_Formal (Ent);
4791 F2 := Next_Formal (F1);
4793 -- The operation counts as primitive if either operand or
4794 -- result are of the given base type, and both operands are
4795 -- fixed point types.
4797 if (Base_Type (Etype (F1)) = Bas
4798 and then Is_Fixed_Point_Type (Etype (F2)))
4801 (Base_Type (Etype (F2)) = Bas
4802 and then Is_Fixed_Point_Type (Etype (F1)))
4805 (Base_Type (Etype (Ent)) = Bas
4806 and then Is_Fixed_Point_Type (Etype (F1))
4807 and then Is_Fixed_Point_Type (Etype (F2)))
4823 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
4825 if T1 = Universal_Integer or else T1 = Universal_Real then
4826 return Base_Type (T2);
4828 return Base_Type (T1);
4832 -- Start of processing for Check_Arithmetic_Pair
4835 if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
4837 if Is_Numeric_Type (T1)
4838 and then Is_Numeric_Type (T2)
4839 and then (Covers (T1 => T1, T2 => T2)
4841 Covers (T1 => T2, T2 => T1))
4843 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4846 elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4848 if Is_Fixed_Point_Type (T1)
4849 and then (Is_Fixed_Point_Type (T2)
4850 or else T2 = Universal_Real)
4852 -- If Treat_Fixed_As_Integer is set then the Etype is already set
4853 -- and no further processing is required (this is the case of an
4854 -- operator constructed by Exp_Fixd for a fixed point operation)
4855 -- Otherwise add one interpretation with universal fixed result
4856 -- If the operator is given in functional notation, it comes
4857 -- from source and Fixed_As_Integer cannot apply.
4859 if (Nkind (N) not in N_Op
4860 or else not Treat_Fixed_As_Integer (N))
4862 (not Has_Fixed_Op (T1, Op_Id)
4863 or else Nkind (Parent (N)) = N_Type_Conversion)
4865 Add_One_Interp (N, Op_Id, Universal_Fixed);
4868 elsif Is_Fixed_Point_Type (T2)
4869 and then (Nkind (N) not in N_Op
4870 or else not Treat_Fixed_As_Integer (N))
4871 and then T1 = Universal_Real
4873 (not Has_Fixed_Op (T1, Op_Id)
4874 or else Nkind (Parent (N)) = N_Type_Conversion)
4876 Add_One_Interp (N, Op_Id, Universal_Fixed);
4878 elsif Is_Numeric_Type (T1)
4879 and then Is_Numeric_Type (T2)
4880 and then (Covers (T1 => T1, T2 => T2)
4882 Covers (T1 => T2, T2 => T1))
4884 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4886 elsif Is_Fixed_Point_Type (T1)
4887 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4888 or else T2 = Universal_Integer)
4890 Add_One_Interp (N, Op_Id, T1);
4892 elsif T2 = Universal_Real
4893 and then Base_Type (T1) = Base_Type (Standard_Integer)
4894 and then Op_Name = Name_Op_Multiply
4896 Add_One_Interp (N, Op_Id, Any_Fixed);
4898 elsif T1 = Universal_Real
4899 and then Base_Type (T2) = Base_Type (Standard_Integer)
4901 Add_One_Interp (N, Op_Id, Any_Fixed);
4903 elsif Is_Fixed_Point_Type (T2)
4904 and then (Base_Type (T1) = Base_Type (Standard_Integer)
4905 or else T1 = Universal_Integer)
4906 and then Op_Name = Name_Op_Multiply
4908 Add_One_Interp (N, Op_Id, T2);
4910 elsif T1 = Universal_Real and then T2 = Universal_Integer then
4911 Add_One_Interp (N, Op_Id, T1);
4913 elsif T2 = Universal_Real
4914 and then T1 = Universal_Integer
4915 and then Op_Name = Name_Op_Multiply
4917 Add_One_Interp (N, Op_Id, T2);
4920 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4922 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
4923 -- set does not require any special processing, since the Etype is
4924 -- already set (case of operation constructed by Exp_Fixed).
4926 if Is_Integer_Type (T1)
4927 and then (Covers (T1 => T1, T2 => T2)
4929 Covers (T1 => T2, T2 => T1))
4931 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4934 elsif Op_Name = Name_Op_Expon then
4935 if Is_Numeric_Type (T1)
4936 and then not Is_Fixed_Point_Type (T1)
4937 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4938 or else T2 = Universal_Integer)
4940 Add_One_Interp (N, Op_Id, Base_Type (T1));
4943 else pragma Assert (Nkind (N) in N_Op_Shift);
4945 -- If not one of the predefined operators, the node may be one
4946 -- of the intrinsic functions. Its kind is always specific, and
4947 -- we can use it directly, rather than the name of the operation.
4949 if Is_Integer_Type (T1)
4950 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4951 or else T2 = Universal_Integer)
4953 Add_One_Interp (N, Op_Id, Base_Type (T1));
4956 end Check_Arithmetic_Pair;
4958 -------------------------------
4959 -- Check_Misspelled_Selector --
4960 -------------------------------
4962 procedure Check_Misspelled_Selector
4963 (Prefix : Entity_Id;
4966 Max_Suggestions : constant := 2;
4967 Nr_Of_Suggestions : Natural := 0;
4969 Suggestion_1 : Entity_Id := Empty;
4970 Suggestion_2 : Entity_Id := Empty;
4975 -- All the components of the prefix of selector Sel are matched
4976 -- against Sel and a count is maintained of possible misspellings.
4977 -- When at the end of the analysis there are one or two (not more!)
4978 -- possible misspellings, these misspellings will be suggested as
4979 -- possible correction.
4981 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
4983 -- Concurrent types should be handled as well ???
4988 Comp := First_Entity (Prefix);
4989 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
4990 if Is_Visible_Component (Comp) then
4991 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
4992 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
4994 case Nr_Of_Suggestions is
4995 when 1 => Suggestion_1 := Comp;
4996 when 2 => Suggestion_2 := Comp;
4997 when others => exit;
5002 Comp := Next_Entity (Comp);
5005 -- Report at most two suggestions
5007 if Nr_Of_Suggestions = 1 then
5008 Error_Msg_NE -- CODEFIX
5009 ("\possible misspelling of&", Sel, Suggestion_1);
5011 elsif Nr_Of_Suggestions = 2 then
5012 Error_Msg_Node_2 := Suggestion_2;
5013 Error_Msg_NE -- CODEFIX
5014 ("\possible misspelling of& or&", Sel, Suggestion_1);
5016 end Check_Misspelled_Selector;
5018 ----------------------
5019 -- Defined_In_Scope --
5020 ----------------------
5022 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5024 S1 : constant Entity_Id := Scope (Base_Type (T));
5027 or else (S1 = System_Aux_Id and then S = Scope (S1));
5028 end Defined_In_Scope;
5034 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5040 Void_Interp_Seen : Boolean := False;
5043 pragma Warnings (Off, Boolean);
5046 if Ada_Version >= Ada_2005 then
5047 Actual := First_Actual (N);
5048 while Present (Actual) loop
5050 -- Ada 2005 (AI-50217): Post an error in case of premature
5051 -- usage of an entity from the limited view.
5053 if not Analyzed (Etype (Actual))
5054 and then From_With_Type (Etype (Actual))
5056 Error_Msg_Qual_Level := 1;
5058 ("missing with_clause for scope of imported type&",
5059 Actual, Etype (Actual));
5060 Error_Msg_Qual_Level := 0;
5063 Next_Actual (Actual);
5067 -- Analyze each candidate call again, with full error reporting
5071 ("no candidate interpretations match the actuals:!", Nam);
5072 Err_Mode := All_Errors_Mode;
5073 All_Errors_Mode := True;
5075 -- If this is a call to an operation of a concurrent type,
5076 -- the failed interpretations have been removed from the
5077 -- name. Recover them to provide full diagnostics.
5079 if Nkind (Parent (Nam)) = N_Selected_Component then
5080 Set_Entity (Nam, Empty);
5081 New_Nam := New_Copy_Tree (Parent (Nam));
5082 Set_Is_Overloaded (New_Nam, False);
5083 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5084 Set_Parent (New_Nam, Parent (Parent (Nam)));
5085 Analyze_Selected_Component (New_Nam);
5086 Get_First_Interp (Selector_Name (New_Nam), X, It);
5088 Get_First_Interp (Nam, X, It);
5091 while Present (It.Nam) loop
5092 if Etype (It.Nam) = Standard_Void_Type then
5093 Void_Interp_Seen := True;
5096 Analyze_One_Call (N, It.Nam, True, Success);
5097 Get_Next_Interp (X, It);
5100 if Nkind (N) = N_Function_Call then
5101 Get_First_Interp (Nam, X, It);
5102 while Present (It.Nam) loop
5103 if Ekind_In (It.Nam, E_Function, E_Operator) then
5106 Get_Next_Interp (X, It);
5110 -- If all interpretations are procedures, this deserves a
5111 -- more precise message. Ditto if this appears as the prefix
5112 -- of a selected component, which may be a lexical error.
5115 ("\context requires function call, found procedure name", Nam);
5117 if Nkind (Parent (N)) = N_Selected_Component
5118 and then N = Prefix (Parent (N))
5120 Error_Msg_N -- CODEFIX
5121 ("\period should probably be semicolon", Parent (N));
5124 elsif Nkind (N) = N_Procedure_Call_Statement
5125 and then not Void_Interp_Seen
5128 "\function name found in procedure call", Nam);
5131 All_Errors_Mode := Err_Mode;
5134 ---------------------------
5135 -- Find_Arithmetic_Types --
5136 ---------------------------
5138 procedure Find_Arithmetic_Types
5143 Index1 : Interp_Index;
5144 Index2 : Interp_Index;
5148 procedure Check_Right_Argument (T : Entity_Id);
5149 -- Check right operand of operator
5151 --------------------------
5152 -- Check_Right_Argument --
5153 --------------------------
5155 procedure Check_Right_Argument (T : Entity_Id) is
5157 if not Is_Overloaded (R) then
5158 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5160 Get_First_Interp (R, Index2, It2);
5161 while Present (It2.Typ) loop
5162 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5163 Get_Next_Interp (Index2, It2);
5166 end Check_Right_Argument;
5168 -- Start of processing for Find_Arithmetic_Types
5171 if not Is_Overloaded (L) then
5172 Check_Right_Argument (Etype (L));
5175 Get_First_Interp (L, Index1, It1);
5176 while Present (It1.Typ) loop
5177 Check_Right_Argument (It1.Typ);
5178 Get_Next_Interp (Index1, It1);
5182 end Find_Arithmetic_Types;
5184 ------------------------
5185 -- Find_Boolean_Types --
5186 ------------------------
5188 procedure Find_Boolean_Types
5193 Index : Interp_Index;
5196 procedure Check_Numeric_Argument (T : Entity_Id);
5197 -- Special case for logical operations one of whose operands is an
5198 -- integer literal. If both are literal the result is any modular type.
5200 ----------------------------
5201 -- Check_Numeric_Argument --
5202 ----------------------------
5204 procedure Check_Numeric_Argument (T : Entity_Id) is
5206 if T = Universal_Integer then
5207 Add_One_Interp (N, Op_Id, Any_Modular);
5209 elsif Is_Modular_Integer_Type (T) then
5210 Add_One_Interp (N, Op_Id, T);
5212 end Check_Numeric_Argument;
5214 -- Start of processing for Find_Boolean_Types
5217 if not Is_Overloaded (L) then
5218 if Etype (L) = Universal_Integer
5219 or else Etype (L) = Any_Modular
5221 if not Is_Overloaded (R) then
5222 Check_Numeric_Argument (Etype (R));
5225 Get_First_Interp (R, Index, It);
5226 while Present (It.Typ) loop
5227 Check_Numeric_Argument (It.Typ);
5228 Get_Next_Interp (Index, It);
5232 -- If operands are aggregates, we must assume that they may be
5233 -- boolean arrays, and leave disambiguation for the second pass.
5234 -- If only one is an aggregate, verify that the other one has an
5235 -- interpretation as a boolean array
5237 elsif Nkind (L) = N_Aggregate then
5238 if Nkind (R) = N_Aggregate then
5239 Add_One_Interp (N, Op_Id, Etype (L));
5241 elsif not Is_Overloaded (R) then
5242 if Valid_Boolean_Arg (Etype (R)) then
5243 Add_One_Interp (N, Op_Id, Etype (R));
5247 Get_First_Interp (R, Index, It);
5248 while Present (It.Typ) loop
5249 if Valid_Boolean_Arg (It.Typ) then
5250 Add_One_Interp (N, Op_Id, It.Typ);
5253 Get_Next_Interp (Index, It);
5257 elsif Valid_Boolean_Arg (Etype (L))
5258 and then Has_Compatible_Type (R, Etype (L))
5260 Add_One_Interp (N, Op_Id, Etype (L));
5264 Get_First_Interp (L, Index, It);
5265 while Present (It.Typ) loop
5266 if Valid_Boolean_Arg (It.Typ)
5267 and then Has_Compatible_Type (R, It.Typ)
5269 Add_One_Interp (N, Op_Id, It.Typ);
5272 Get_Next_Interp (Index, It);
5275 end Find_Boolean_Types;
5277 ---------------------------
5278 -- Find_Comparison_Types --
5279 ---------------------------
5281 procedure Find_Comparison_Types
5286 Index : Interp_Index;
5288 Found : Boolean := False;
5291 Scop : Entity_Id := Empty;
5293 procedure Try_One_Interp (T1 : Entity_Id);
5294 -- Routine to try one proposed interpretation. Note that the context
5295 -- of the operator plays no role in resolving the arguments, so that
5296 -- if there is more than one interpretation of the operands that is
5297 -- compatible with comparison, the operation is ambiguous.
5299 --------------------
5300 -- Try_One_Interp --
5301 --------------------
5303 procedure Try_One_Interp (T1 : Entity_Id) is
5306 -- If the operator is an expanded name, then the type of the operand
5307 -- must be defined in the corresponding scope. If the type is
5308 -- universal, the context will impose the correct type.
5311 and then not Defined_In_Scope (T1, Scop)
5312 and then T1 /= Universal_Integer
5313 and then T1 /= Universal_Real
5314 and then T1 /= Any_String
5315 and then T1 /= Any_Composite
5320 if Valid_Comparison_Arg (T1)
5321 and then Has_Compatible_Type (R, T1)
5324 and then Base_Type (T1) /= Base_Type (T_F)
5326 It := Disambiguate (L, I_F, Index, Any_Type);
5328 if It = No_Interp then
5329 Ambiguous_Operands (N);
5330 Set_Etype (L, Any_Type);
5344 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5349 -- Start of processing for Find_Comparison_Types
5352 -- If left operand is aggregate, the right operand has to
5353 -- provide a usable type for it.
5355 if Nkind (L) = N_Aggregate
5356 and then Nkind (R) /= N_Aggregate
5358 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5362 if Nkind (N) = N_Function_Call
5363 and then Nkind (Name (N)) = N_Expanded_Name
5365 Scop := Entity (Prefix (Name (N)));
5367 -- The prefix may be a package renaming, and the subsequent test
5368 -- requires the original package.
5370 if Ekind (Scop) = E_Package
5371 and then Present (Renamed_Entity (Scop))
5373 Scop := Renamed_Entity (Scop);
5374 Set_Entity (Prefix (Name (N)), Scop);
5378 if not Is_Overloaded (L) then
5379 Try_One_Interp (Etype (L));
5382 Get_First_Interp (L, Index, It);
5383 while Present (It.Typ) loop
5384 Try_One_Interp (It.Typ);
5385 Get_Next_Interp (Index, It);
5388 end Find_Comparison_Types;
5390 ----------------------------------------
5391 -- Find_Non_Universal_Interpretations --
5392 ----------------------------------------
5394 procedure Find_Non_Universal_Interpretations
5400 Index : Interp_Index;
5404 if T1 = Universal_Integer
5405 or else T1 = Universal_Real
5407 if not Is_Overloaded (R) then
5409 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5411 Get_First_Interp (R, Index, It);
5412 while Present (It.Typ) loop
5413 if Covers (It.Typ, T1) then
5415 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5418 Get_Next_Interp (Index, It);
5422 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5424 end Find_Non_Universal_Interpretations;
5426 ------------------------------
5427 -- Find_Concatenation_Types --
5428 ------------------------------
5430 procedure Find_Concatenation_Types
5435 Op_Type : constant Entity_Id := Etype (Op_Id);
5438 if Is_Array_Type (Op_Type)
5439 and then not Is_Limited_Type (Op_Type)
5441 and then (Has_Compatible_Type (L, Op_Type)
5443 Has_Compatible_Type (L, Component_Type (Op_Type)))
5445 and then (Has_Compatible_Type (R, Op_Type)
5447 Has_Compatible_Type (R, Component_Type (Op_Type)))
5449 Add_One_Interp (N, Op_Id, Op_Type);
5451 end Find_Concatenation_Types;
5453 -------------------------
5454 -- Find_Equality_Types --
5455 -------------------------
5457 procedure Find_Equality_Types
5462 Index : Interp_Index;
5464 Found : Boolean := False;
5467 Scop : Entity_Id := Empty;
5469 procedure Try_One_Interp (T1 : Entity_Id);
5470 -- The context of the equality operator plays no role in resolving the
5471 -- arguments, so that if there is more than one interpretation of the
5472 -- operands that is compatible with equality, the construct is ambiguous
5473 -- and an error can be emitted now, after trying to disambiguate, i.e.
5474 -- applying preference rules.
5476 --------------------
5477 -- Try_One_Interp --
5478 --------------------
5480 procedure Try_One_Interp (T1 : Entity_Id) is
5481 Bas : constant Entity_Id := Base_Type (T1);
5484 -- If the operator is an expanded name, then the type of the operand
5485 -- must be defined in the corresponding scope. If the type is
5486 -- universal, the context will impose the correct type. An anonymous
5487 -- type for a 'Access reference is also universal in this sense, as
5488 -- the actual type is obtained from context.
5489 -- In Ada 2005, the equality operator for anonymous access types
5490 -- is declared in Standard, and preference rules apply to it.
5492 if Present (Scop) then
5493 if Defined_In_Scope (T1, Scop)
5494 or else T1 = Universal_Integer
5495 or else T1 = Universal_Real
5496 or else T1 = Any_Access
5497 or else T1 = Any_String
5498 or else T1 = Any_Composite
5499 or else (Ekind (T1) = E_Access_Subprogram_Type
5500 and then not Comes_From_Source (T1))
5504 elsif Ekind (T1) = E_Anonymous_Access_Type
5505 and then Scop = Standard_Standard
5510 -- The scope does not contain an operator for the type
5515 -- If we have infix notation, the operator must be usable.
5516 -- Within an instance, if the type is already established we
5517 -- know it is correct.
5518 -- In Ada 2005, the equality on anonymous access types is declared
5519 -- in Standard, and is always visible.
5521 elsif In_Open_Scopes (Scope (Bas))
5522 or else Is_Potentially_Use_Visible (Bas)
5523 or else In_Use (Bas)
5524 or else (In_Use (Scope (Bas))
5525 and then not Is_Hidden (Bas))
5526 or else (In_Instance
5527 and then First_Subtype (T1) = First_Subtype (Etype (R)))
5528 or else Ekind (T1) = E_Anonymous_Access_Type
5533 -- Save candidate type for subsequent error message, if any
5535 if not Is_Limited_Type (T1) then
5536 Candidate_Type := T1;
5542 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5543 -- Do not allow anonymous access types in equality operators.
5545 if Ada_Version < Ada_2005
5546 and then Ekind (T1) = E_Anonymous_Access_Type
5551 if T1 /= Standard_Void_Type
5552 and then Has_Compatible_Type (R, T1)
5554 ((not Is_Limited_Type (T1)
5555 and then not Is_Limited_Composite (T1))
5559 and then not Is_Limited_Type (Component_Type (T1))
5560 and then Available_Full_View_Of_Component (T1)))
5563 and then Base_Type (T1) /= Base_Type (T_F)
5565 It := Disambiguate (L, I_F, Index, Any_Type);
5567 if It = No_Interp then
5568 Ambiguous_Operands (N);
5569 Set_Etype (L, Any_Type);
5582 if not Analyzed (L) then
5586 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5588 -- Case of operator was not visible, Etype still set to Any_Type
5590 if Etype (N) = Any_Type then
5594 elsif Scop = Standard_Standard
5595 and then Ekind (T1) = E_Anonymous_Access_Type
5601 -- Start of processing for Find_Equality_Types
5604 -- If left operand is aggregate, the right operand has to
5605 -- provide a usable type for it.
5607 if Nkind (L) = N_Aggregate
5608 and then Nkind (R) /= N_Aggregate
5610 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5614 if Nkind (N) = N_Function_Call
5615 and then Nkind (Name (N)) = N_Expanded_Name
5617 Scop := Entity (Prefix (Name (N)));
5619 -- The prefix may be a package renaming, and the subsequent test
5620 -- requires the original package.
5622 if Ekind (Scop) = E_Package
5623 and then Present (Renamed_Entity (Scop))
5625 Scop := Renamed_Entity (Scop);
5626 Set_Entity (Prefix (Name (N)), Scop);
5630 if not Is_Overloaded (L) then
5631 Try_One_Interp (Etype (L));
5634 Get_First_Interp (L, Index, It);
5635 while Present (It.Typ) loop
5636 Try_One_Interp (It.Typ);
5637 Get_Next_Interp (Index, It);
5640 end Find_Equality_Types;
5642 -------------------------
5643 -- Find_Negation_Types --
5644 -------------------------
5646 procedure Find_Negation_Types
5651 Index : Interp_Index;
5655 if not Is_Overloaded (R) then
5656 if Etype (R) = Universal_Integer then
5657 Add_One_Interp (N, Op_Id, Any_Modular);
5658 elsif Valid_Boolean_Arg (Etype (R)) then
5659 Add_One_Interp (N, Op_Id, Etype (R));
5663 Get_First_Interp (R, Index, It);
5664 while Present (It.Typ) loop
5665 if Valid_Boolean_Arg (It.Typ) then
5666 Add_One_Interp (N, Op_Id, It.Typ);
5669 Get_Next_Interp (Index, It);
5672 end Find_Negation_Types;
5674 ------------------------------
5675 -- Find_Primitive_Operation --
5676 ------------------------------
5678 function Find_Primitive_Operation (N : Node_Id) return Boolean is
5679 Obj : constant Node_Id := Prefix (N);
5680 Op : constant Node_Id := Selector_Name (N);
5687 Set_Etype (Op, Any_Type);
5689 if Is_Access_Type (Etype (Obj)) then
5690 Typ := Designated_Type (Etype (Obj));
5695 if Is_Class_Wide_Type (Typ) then
5696 Typ := Root_Type (Typ);
5699 Prims := Primitive_Operations (Typ);
5701 Prim := First_Elmt (Prims);
5702 while Present (Prim) loop
5703 if Chars (Node (Prim)) = Chars (Op) then
5704 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
5705 Set_Etype (N, Etype (Node (Prim)));
5711 -- Now look for class-wide operations of the type or any of its
5712 -- ancestors by iterating over the homonyms of the selector.
5715 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
5719 Hom := Current_Entity (Op);
5720 while Present (Hom) loop
5721 if (Ekind (Hom) = E_Procedure
5723 Ekind (Hom) = E_Function)
5724 and then Scope (Hom) = Scope (Typ)
5725 and then Present (First_Formal (Hom))
5727 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
5729 (Is_Access_Type (Etype (First_Formal (Hom)))
5731 Ekind (Etype (First_Formal (Hom))) =
5732 E_Anonymous_Access_Type
5735 (Designated_Type (Etype (First_Formal (Hom)))) =
5738 Add_One_Interp (Op, Hom, Etype (Hom));
5739 Set_Etype (N, Etype (Hom));
5742 Hom := Homonym (Hom);
5746 return Etype (Op) /= Any_Type;
5747 end Find_Primitive_Operation;
5749 ----------------------
5750 -- Find_Unary_Types --
5751 ----------------------
5753 procedure Find_Unary_Types
5758 Index : Interp_Index;
5762 if not Is_Overloaded (R) then
5763 if Is_Numeric_Type (Etype (R)) then
5764 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
5768 Get_First_Interp (R, Index, It);
5769 while Present (It.Typ) loop
5770 if Is_Numeric_Type (It.Typ) then
5771 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
5774 Get_Next_Interp (Index, It);
5777 end Find_Unary_Types;
5783 function Junk_Operand (N : Node_Id) return Boolean is
5787 if Error_Posted (N) then
5791 -- Get entity to be tested
5793 if Is_Entity_Name (N)
5794 and then Present (Entity (N))
5798 -- An odd case, a procedure name gets converted to a very peculiar
5799 -- function call, and here is where we detect this happening.
5801 elsif Nkind (N) = N_Function_Call
5802 and then Is_Entity_Name (Name (N))
5803 and then Present (Entity (Name (N)))
5807 -- Another odd case, there are at least some cases of selected
5808 -- components where the selected component is not marked as having
5809 -- an entity, even though the selector does have an entity
5811 elsif Nkind (N) = N_Selected_Component
5812 and then Present (Entity (Selector_Name (N)))
5814 Enode := Selector_Name (N);
5820 -- Now test the entity we got to see if it is a bad case
5822 case Ekind (Entity (Enode)) is
5826 ("package name cannot be used as operand", Enode);
5828 when Generic_Unit_Kind =>
5830 ("generic unit name cannot be used as operand", Enode);
5834 ("subtype name cannot be used as operand", Enode);
5838 ("entry name cannot be used as operand", Enode);
5842 ("procedure name cannot be used as operand", Enode);
5846 ("exception name cannot be used as operand", Enode);
5848 when E_Block | E_Label | E_Loop =>
5850 ("label name cannot be used as operand", Enode);
5860 --------------------
5861 -- Operator_Check --
5862 --------------------
5864 procedure Operator_Check (N : Node_Id) is
5866 Remove_Abstract_Operations (N);
5868 -- Test for case of no interpretation found for operator
5870 if Etype (N) = Any_Type then
5874 Op_Id : Entity_Id := Empty;
5877 R := Right_Opnd (N);
5879 if Nkind (N) in N_Binary_Op then
5885 -- If either operand has no type, then don't complain further,
5886 -- since this simply means that we have a propagated error.
5889 or else Etype (R) = Any_Type
5890 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5894 -- We explicitly check for the case of concatenation of component
5895 -- with component to avoid reporting spurious matching array types
5896 -- that might happen to be lurking in distant packages (such as
5897 -- run-time packages). This also prevents inconsistencies in the
5898 -- messages for certain ACVC B tests, which can vary depending on
5899 -- types declared in run-time interfaces. Another improvement when
5900 -- aggregates are present is to look for a well-typed operand.
5902 elsif Present (Candidate_Type)
5903 and then (Nkind (N) /= N_Op_Concat
5904 or else Is_Array_Type (Etype (L))
5905 or else Is_Array_Type (Etype (R)))
5907 if Nkind (N) = N_Op_Concat then
5908 if Etype (L) /= Any_Composite
5909 and then Is_Array_Type (Etype (L))
5911 Candidate_Type := Etype (L);
5913 elsif Etype (R) /= Any_Composite
5914 and then Is_Array_Type (Etype (R))
5916 Candidate_Type := Etype (R);
5920 Error_Msg_NE -- CODEFIX
5921 ("operator for} is not directly visible!",
5922 N, First_Subtype (Candidate_Type));
5925 U : constant Node_Id :=
5926 Cunit (Get_Source_Unit (Candidate_Type));
5928 if Unit_Is_Visible (U) then
5929 Error_Msg_N -- CODEFIX
5930 ("use clause would make operation legal!", N);
5932 Error_Msg_NE -- CODEFIX
5933 ("add with_clause and use_clause for&!",
5934 N, Defining_Entity (Unit (U)));
5939 -- If either operand is a junk operand (e.g. package name), then
5940 -- post appropriate error messages, but do not complain further.
5942 -- Note that the use of OR in this test instead of OR ELSE is
5943 -- quite deliberate, we may as well check both operands in the
5944 -- binary operator case.
5946 elsif Junk_Operand (R)
5947 or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
5951 -- If we have a logical operator, one of whose operands is
5952 -- Boolean, then we know that the other operand cannot resolve to
5953 -- Boolean (since we got no interpretations), but in that case we
5954 -- pretty much know that the other operand should be Boolean, so
5955 -- resolve it that way (generating an error)
5957 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
5958 if Etype (L) = Standard_Boolean then
5959 Resolve (R, Standard_Boolean);
5961 elsif Etype (R) = Standard_Boolean then
5962 Resolve (L, Standard_Boolean);
5966 -- For an arithmetic operator or comparison operator, if one
5967 -- of the operands is numeric, then we know the other operand
5968 -- is not the same numeric type. If it is a non-numeric type,
5969 -- then probably it is intended to match the other operand.
5971 elsif Nkind_In (N, N_Op_Add,
5977 Nkind_In (N, N_Op_Lt,
5983 if Is_Numeric_Type (Etype (L))
5984 and then not Is_Numeric_Type (Etype (R))
5986 Resolve (R, Etype (L));
5989 elsif Is_Numeric_Type (Etype (R))
5990 and then not Is_Numeric_Type (Etype (L))
5992 Resolve (L, Etype (R));
5996 -- Comparisons on A'Access are common enough to deserve a
5999 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6000 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6001 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6004 ("two access attributes cannot be compared directly", N);
6006 ("\use qualified expression for one of the operands",
6010 -- Another one for C programmers
6012 elsif Nkind (N) = N_Op_Concat
6013 and then Valid_Boolean_Arg (Etype (L))
6014 and then Valid_Boolean_Arg (Etype (R))
6016 Error_Msg_N ("invalid operands for concatenation", N);
6017 Error_Msg_N -- CODEFIX
6018 ("\maybe AND was meant", N);
6021 -- A special case for comparison of access parameter with null
6023 elsif Nkind (N) = N_Op_Eq
6024 and then Is_Entity_Name (L)
6025 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6026 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6028 and then Nkind (R) = N_Null
6030 Error_Msg_N ("access parameter is not allowed to be null", L);
6031 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6034 -- Another special case for exponentiation, where the right
6035 -- operand must be Natural, independently of the base.
6037 elsif Nkind (N) = N_Op_Expon
6038 and then Is_Numeric_Type (Etype (L))
6039 and then not Is_Overloaded (R)
6041 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6042 and then Base_Type (Etype (R)) /= Universal_Integer
6044 if Ada_Version >= Ada_2012
6045 and then Is_Dimensioned_Type (Etype (L))
6048 ("exponent for dimensioned type must be a rational" &
6049 ", found}", R, Etype (R));
6052 ("exponent must be of type Natural, found}", R, Etype (R));
6058 -- If we fall through then just give general message. Note that in
6059 -- the following messages, if the operand is overloaded we choose
6060 -- an arbitrary type to complain about, but that is probably more
6061 -- useful than not giving a type at all.
6063 if Nkind (N) in N_Unary_Op then
6064 Error_Msg_Node_2 := Etype (R);
6065 Error_Msg_N ("operator& not defined for}", N);
6069 if Nkind (N) in N_Binary_Op then
6070 if not Is_Overloaded (L)
6071 and then not Is_Overloaded (R)
6072 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6074 Error_Msg_Node_2 := First_Subtype (Etype (R));
6075 Error_Msg_N ("there is no applicable operator& for}", N);
6078 -- Another attempt to find a fix: one of the candidate
6079 -- interpretations may not be use-visible. This has
6080 -- already been checked for predefined operators, so
6081 -- we examine only user-defined functions.
6083 Op_Id := Get_Name_Entity_Id (Chars (N));
6085 while Present (Op_Id) loop
6086 if Ekind (Op_Id) /= E_Operator
6087 and then Is_Overloadable (Op_Id)
6089 if not Is_Immediately_Visible (Op_Id)
6090 and then not In_Use (Scope (Op_Id))
6091 and then not Is_Abstract_Subprogram (Op_Id)
6092 and then not Is_Hidden (Op_Id)
6093 and then Ekind (Scope (Op_Id)) = E_Package
6096 (L, Etype (First_Formal (Op_Id)))
6098 (Next_Formal (First_Formal (Op_Id)))
6102 Etype (Next_Formal (First_Formal (Op_Id))))
6105 ("No legal interpretation for operator&", N);
6107 ("\use clause on& would make operation legal",
6113 Op_Id := Homonym (Op_Id);
6117 Error_Msg_N ("invalid operand types for operator&", N);
6119 if Nkind (N) /= N_Op_Concat then
6120 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6121 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6131 -----------------------------------------
6132 -- Process_Implicit_Dereference_Prefix --
6133 -----------------------------------------
6135 function Process_Implicit_Dereference_Prefix
6137 P : Entity_Id) return Entity_Id
6140 Typ : constant Entity_Id := Designated_Type (Etype (P));
6144 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6146 -- We create a dummy reference to E to ensure that the reference
6147 -- is not considered as part of an assignment (an implicit
6148 -- dereference can never assign to its prefix). The Comes_From_Source
6149 -- attribute needs to be propagated for accurate warnings.
6151 Ref := New_Reference_To (E, Sloc (P));
6152 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6153 Generate_Reference (E, Ref);
6156 -- An implicit dereference is a legal occurrence of an
6157 -- incomplete type imported through a limited_with clause,
6158 -- if the full view is visible.
6160 if From_With_Type (Typ)
6161 and then not From_With_Type (Scope (Typ))
6163 (Is_Immediately_Visible (Scope (Typ))
6165 (Is_Child_Unit (Scope (Typ))
6166 and then Is_Visible_Child_Unit (Scope (Typ))))
6168 return Available_View (Typ);
6173 end Process_Implicit_Dereference_Prefix;
6175 --------------------------------
6176 -- Remove_Abstract_Operations --
6177 --------------------------------
6179 procedure Remove_Abstract_Operations (N : Node_Id) is
6180 Abstract_Op : Entity_Id := Empty;
6181 Address_Kludge : Boolean := False;
6185 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6186 -- activate this if either extensions are enabled, or if the abstract
6187 -- operation in question comes from a predefined file. This latter test
6188 -- allows us to use abstract to make operations invisible to users. In
6189 -- particular, if type Address is non-private and abstract subprograms
6190 -- are used to hide its operators, they will be truly hidden.
6192 type Operand_Position is (First_Op, Second_Op);
6193 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6195 procedure Remove_Address_Interpretations (Op : Operand_Position);
6196 -- Ambiguities may arise when the operands are literal and the address
6197 -- operations in s-auxdec are visible. In that case, remove the
6198 -- interpretation of a literal as Address, to retain the semantics of
6199 -- Address as a private type.
6201 ------------------------------------
6202 -- Remove_Address_Interpretations --
6203 ------------------------------------
6205 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6209 if Is_Overloaded (N) then
6210 Get_First_Interp (N, I, It);
6211 while Present (It.Nam) loop
6212 Formal := First_Entity (It.Nam);
6214 if Op = Second_Op then
6215 Formal := Next_Entity (Formal);
6218 if Is_Descendent_Of_Address (Etype (Formal)) then
6219 Address_Kludge := True;
6223 Get_Next_Interp (I, It);
6226 end Remove_Address_Interpretations;
6228 -- Start of processing for Remove_Abstract_Operations
6231 if Is_Overloaded (N) then
6232 if Debug_Flag_V then
6233 Write_Str ("Remove_Abstract_Operations: ");
6234 Write_Overloads (N);
6237 Get_First_Interp (N, I, It);
6239 while Present (It.Nam) loop
6240 if Is_Overloadable (It.Nam)
6241 and then Is_Abstract_Subprogram (It.Nam)
6242 and then not Is_Dispatching_Operation (It.Nam)
6244 Abstract_Op := It.Nam;
6246 if Is_Descendent_Of_Address (It.Typ) then
6247 Address_Kludge := True;
6251 -- In Ada 2005, this operation does not participate in overload
6252 -- resolution. If the operation is defined in a predefined
6253 -- unit, it is one of the operations declared abstract in some
6254 -- variants of System, and it must be removed as well.
6256 elsif Ada_Version >= Ada_2005
6257 or else Is_Predefined_File_Name
6258 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6265 Get_Next_Interp (I, It);
6268 if No (Abstract_Op) then
6270 -- If some interpretation yields an integer type, it is still
6271 -- possible that there are address interpretations. Remove them
6272 -- if one operand is a literal, to avoid spurious ambiguities
6273 -- on systems where Address is a visible integer type.
6275 if Is_Overloaded (N)
6276 and then Nkind (N) in N_Op
6277 and then Is_Integer_Type (Etype (N))
6279 if Nkind (N) in N_Binary_Op then
6280 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6281 Remove_Address_Interpretations (Second_Op);
6283 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6284 Remove_Address_Interpretations (First_Op);
6289 elsif Nkind (N) in N_Op then
6291 -- Remove interpretations that treat literals as addresses. This
6292 -- is never appropriate, even when Address is defined as a visible
6293 -- Integer type. The reason is that we would really prefer Address
6294 -- to behave as a private type, even in this case, which is there
6295 -- only to accommodate oddities of VMS address sizes. If Address
6296 -- is a visible integer type, we get lots of overload ambiguities.
6298 if Nkind (N) in N_Binary_Op then
6300 U1 : constant Boolean :=
6301 Present (Universal_Interpretation (Right_Opnd (N)));
6302 U2 : constant Boolean :=
6303 Present (Universal_Interpretation (Left_Opnd (N)));
6307 Remove_Address_Interpretations (Second_Op);
6311 Remove_Address_Interpretations (First_Op);
6314 if not (U1 and U2) then
6316 -- Remove corresponding predefined operator, which is
6317 -- always added to the overload set.
6319 Get_First_Interp (N, I, It);
6320 while Present (It.Nam) loop
6321 if Scope (It.Nam) = Standard_Standard
6322 and then Base_Type (It.Typ) =
6323 Base_Type (Etype (Abstract_Op))
6328 Get_Next_Interp (I, It);
6331 elsif Is_Overloaded (N)
6332 and then Present (Univ_Type)
6334 -- If both operands have a universal interpretation,
6335 -- it is still necessary to remove interpretations that
6336 -- yield Address. Any remaining ambiguities will be
6337 -- removed in Disambiguate.
6339 Get_First_Interp (N, I, It);
6340 while Present (It.Nam) loop
6341 if Is_Descendent_Of_Address (It.Typ) then
6344 elsif not Is_Type (It.Nam) then
6345 Set_Entity (N, It.Nam);
6348 Get_Next_Interp (I, It);
6354 elsif Nkind (N) = N_Function_Call
6356 (Nkind (Name (N)) = N_Operator_Symbol
6358 (Nkind (Name (N)) = N_Expanded_Name
6360 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6364 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6365 U1 : constant Boolean :=
6366 Present (Universal_Interpretation (Arg1));
6367 U2 : constant Boolean :=
6368 Present (Next (Arg1)) and then
6369 Present (Universal_Interpretation (Next (Arg1)));
6373 Remove_Address_Interpretations (First_Op);
6377 Remove_Address_Interpretations (Second_Op);
6380 if not (U1 and U2) then
6381 Get_First_Interp (N, I, It);
6382 while Present (It.Nam) loop
6383 if Scope (It.Nam) = Standard_Standard
6384 and then It.Typ = Base_Type (Etype (Abstract_Op))
6389 Get_Next_Interp (I, It);
6395 -- If the removal has left no valid interpretations, emit an error
6396 -- message now and label node as illegal.
6398 if Present (Abstract_Op) then
6399 Get_First_Interp (N, I, It);
6403 -- Removal of abstract operation left no viable candidate
6405 Set_Etype (N, Any_Type);
6406 Error_Msg_Sloc := Sloc (Abstract_Op);
6408 ("cannot call abstract operation& declared#", N, Abstract_Op);
6410 -- In Ada 2005, an abstract operation may disable predefined
6411 -- operators. Since the context is not yet known, we mark the
6412 -- predefined operators as potentially hidden. Do not include
6413 -- predefined operators when addresses are involved since this
6414 -- case is handled separately.
6416 elsif Ada_Version >= Ada_2005
6417 and then not Address_Kludge
6419 while Present (It.Nam) loop
6420 if Is_Numeric_Type (It.Typ)
6421 and then Scope (It.Typ) = Standard_Standard
6423 Set_Abstract_Op (I, Abstract_Op);
6426 Get_Next_Interp (I, It);
6431 if Debug_Flag_V then
6432 Write_Str ("Remove_Abstract_Operations done: ");
6433 Write_Overloads (N);
6436 end Remove_Abstract_Operations;
6438 ----------------------------
6439 -- Try_Container_Indexing --
6440 ----------------------------
6442 function Try_Container_Indexing
6445 Expr : Node_Id) return Boolean
6447 Loc : constant Source_Ptr := Sloc (N);
6450 Func_Name : Node_Id;
6455 -- Check whether type has a specified indexing aspect
6459 if Is_Variable (Prefix) then
6460 Func_Name := Find_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
6463 if No (Func_Name) then
6464 Func_Name := Find_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
6467 -- If aspect does not exist the expression is illegal. Error is
6468 -- diagnosed in caller.
6470 if No (Func_Name) then
6472 -- The prefix itself may be an indexing of a container
6473 -- rewrite as such and re-analyze.
6475 if Has_Implicit_Dereference (Etype (Prefix)) then
6476 Build_Explicit_Dereference
6477 (Prefix, First_Discriminant (Etype (Prefix)));
6478 return Try_Container_Indexing (N, Prefix, Expr);
6485 if not Is_Overloaded (Func_Name) then
6486 Func := Entity (Func_Name);
6487 Indexing := Make_Function_Call (Loc,
6488 Name => New_Occurrence_Of (Func, Loc),
6489 Parameter_Associations =>
6490 New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6491 Rewrite (N, Indexing);
6494 -- If the return type of the indexing function is a reference type,
6495 -- add the dereference as a possible interpretation. Note that the
6496 -- indexing aspect may be a function that returns the element type
6497 -- with no intervening implicit dereference.
6499 if Has_Discriminants (Etype (Func)) then
6500 Disc := First_Discriminant (Etype (Func));
6501 while Present (Disc) loop
6502 if Has_Implicit_Dereference (Disc) then
6503 Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
6507 Next_Discriminant (Disc);
6512 Indexing := Make_Function_Call (Loc,
6513 Name => Make_Identifier (Loc, Chars (Func_Name)),
6514 Parameter_Associations =>
6515 New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6517 Rewrite (N, Indexing);
6525 Get_First_Interp (Func_Name, I, It);
6526 Set_Etype (N, Any_Type);
6527 while Present (It.Nam) loop
6528 Analyze_One_Call (N, It.Nam, False, Success);
6530 Set_Etype (Name (N), It.Typ);
6531 Set_Entity (Name (N), It.Nam);
6533 -- Add implicit dereference interpretation
6535 if Has_Discriminants (Etype (It.Nam)) then
6536 Disc := First_Discriminant (Etype (It.Nam));
6537 while Present (Disc) loop
6538 if Has_Implicit_Dereference (Disc) then
6540 (N, Disc, Designated_Type (Etype (Disc)));
6544 Next_Discriminant (Disc);
6550 Get_Next_Interp (I, It);
6555 if Etype (N) = Any_Type then
6556 Error_Msg_NE ("container cannot be indexed with&", N, Etype (Expr));
6557 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
6563 end Try_Container_Indexing;
6565 -----------------------
6566 -- Try_Indirect_Call --
6567 -----------------------
6569 function Try_Indirect_Call
6572 Typ : Entity_Id) return Boolean
6578 pragma Warnings (Off, Call_OK);
6581 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
6583 Actual := First_Actual (N);
6584 Formal := First_Formal (Designated_Type (Typ));
6585 while Present (Actual) and then Present (Formal) loop
6586 if not Has_Compatible_Type (Actual, Etype (Formal)) then
6591 Next_Formal (Formal);
6594 if No (Actual) and then No (Formal) then
6595 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
6597 -- Nam is a candidate interpretation for the name in the call,
6598 -- if it is not an indirect call.
6600 if not Is_Type (Nam)
6601 and then Is_Entity_Name (Name (N))
6603 Set_Entity (Name (N), Nam);
6610 end Try_Indirect_Call;
6612 ----------------------
6613 -- Try_Indexed_Call --
6614 ----------------------
6616 function Try_Indexed_Call
6620 Skip_First : Boolean) return Boolean
6622 Loc : constant Source_Ptr := Sloc (N);
6623 Actuals : constant List_Id := Parameter_Associations (N);
6628 Actual := First (Actuals);
6630 -- If the call was originally written in prefix form, skip the first
6631 -- actual, which is obviously not defaulted.
6637 Index := First_Index (Typ);
6638 while Present (Actual) and then Present (Index) loop
6640 -- If the parameter list has a named association, the expression
6641 -- is definitely a call and not an indexed component.
6643 if Nkind (Actual) = N_Parameter_Association then
6647 if Is_Entity_Name (Actual)
6648 and then Is_Type (Entity (Actual))
6649 and then No (Next (Actual))
6651 -- A single actual that is a type name indicates a slice if the
6652 -- type is discrete, and an error otherwise.
6654 if Is_Discrete_Type (Entity (Actual)) then
6658 Make_Function_Call (Loc,
6659 Name => Relocate_Node (Name (N))),
6661 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
6666 Error_Msg_N ("invalid use of type in expression", Actual);
6667 Set_Etype (N, Any_Type);
6672 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
6680 if No (Actual) and then No (Index) then
6681 Add_One_Interp (N, Nam, Component_Type (Typ));
6683 -- Nam is a candidate interpretation for the name in the call,
6684 -- if it is not an indirect call.
6686 if not Is_Type (Nam)
6687 and then Is_Entity_Name (Name (N))
6689 Set_Entity (Name (N), Nam);
6696 end Try_Indexed_Call;
6698 --------------------------
6699 -- Try_Object_Operation --
6700 --------------------------
6702 function Try_Object_Operation
6703 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
6705 K : constant Node_Kind := Nkind (Parent (N));
6706 Is_Subprg_Call : constant Boolean := Nkind_In
6707 (K, N_Procedure_Call_Statement,
6709 Loc : constant Source_Ptr := Sloc (N);
6710 Obj : constant Node_Id := Prefix (N);
6712 Subprog : constant Node_Id :=
6713 Make_Identifier (Sloc (Selector_Name (N)),
6714 Chars => Chars (Selector_Name (N)));
6715 -- Identifier on which possible interpretations will be collected
6717 Report_Error : Boolean := False;
6718 -- If no candidate interpretation matches the context, redo the
6719 -- analysis with error enabled to provide additional information.
6722 Candidate : Entity_Id := Empty;
6723 New_Call_Node : Node_Id := Empty;
6724 Node_To_Replace : Node_Id;
6725 Obj_Type : Entity_Id := Etype (Obj);
6726 Success : Boolean := False;
6728 function Valid_Candidate
6731 Subp : Entity_Id) return Entity_Id;
6732 -- If the subprogram is a valid interpretation, record it, and add
6733 -- to the list of interpretations of Subprog. Otherwise return Empty.
6735 procedure Complete_Object_Operation
6736 (Call_Node : Node_Id;
6737 Node_To_Replace : Node_Id);
6738 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
6739 -- Call_Node, insert the object (or its dereference) as the first actual
6740 -- in the call, and complete the analysis of the call.
6742 procedure Report_Ambiguity (Op : Entity_Id);
6743 -- If a prefixed procedure call is ambiguous, indicate whether the
6744 -- call includes an implicit dereference or an implicit 'Access.
6746 procedure Transform_Object_Operation
6747 (Call_Node : out Node_Id;
6748 Node_To_Replace : out Node_Id);
6749 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
6750 -- Call_Node is the resulting subprogram call, Node_To_Replace is
6751 -- either N or the parent of N, and Subprog is a reference to the
6752 -- subprogram we are trying to match.
6754 function Try_Class_Wide_Operation
6755 (Call_Node : Node_Id;
6756 Node_To_Replace : Node_Id) return Boolean;
6757 -- Traverse all ancestor types looking for a class-wide subprogram
6758 -- for which the current operation is a valid non-dispatching call.
6760 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
6761 -- If prefix is overloaded, its interpretation may include different
6762 -- tagged types, and we must examine the primitive operations and
6763 -- the class-wide operations of each in order to find candidate
6764 -- interpretations for the call as a whole.
6766 function Try_Primitive_Operation
6767 (Call_Node : Node_Id;
6768 Node_To_Replace : Node_Id) return Boolean;
6769 -- Traverse the list of primitive subprograms looking for a dispatching
6770 -- operation for which the current node is a valid call .
6772 ---------------------
6773 -- Valid_Candidate --
6774 ---------------------
6776 function Valid_Candidate
6779 Subp : Entity_Id) return Entity_Id
6781 Arr_Type : Entity_Id;
6782 Comp_Type : Entity_Id;
6785 -- If the subprogram is a valid interpretation, record it in global
6786 -- variable Subprog, to collect all possible overloadings.
6789 if Subp /= Entity (Subprog) then
6790 Add_One_Interp (Subprog, Subp, Etype (Subp));
6794 -- If the call may be an indexed call, retrieve component type of
6795 -- resulting expression, and add possible interpretation.
6800 if Nkind (Call) = N_Function_Call
6801 and then Nkind (Parent (N)) = N_Indexed_Component
6802 and then Needs_One_Actual (Subp)
6804 if Is_Array_Type (Etype (Subp)) then
6805 Arr_Type := Etype (Subp);
6807 elsif Is_Access_Type (Etype (Subp))
6808 and then Is_Array_Type (Designated_Type (Etype (Subp)))
6810 Arr_Type := Designated_Type (Etype (Subp));
6814 if Present (Arr_Type) then
6816 -- Verify that the actuals (excluding the object) match the types
6824 Actual := Next (First_Actual (Call));
6825 Index := First_Index (Arr_Type);
6826 while Present (Actual) and then Present (Index) loop
6827 if not Has_Compatible_Type (Actual, Etype (Index)) then
6832 Next_Actual (Actual);
6838 and then Present (Arr_Type)
6840 Comp_Type := Component_Type (Arr_Type);
6844 if Present (Comp_Type)
6845 and then Etype (Subprog) /= Comp_Type
6847 Add_One_Interp (Subprog, Subp, Comp_Type);
6851 if Etype (Call) /= Any_Type then
6856 end Valid_Candidate;
6858 -------------------------------
6859 -- Complete_Object_Operation --
6860 -------------------------------
6862 procedure Complete_Object_Operation
6863 (Call_Node : Node_Id;
6864 Node_To_Replace : Node_Id)
6866 Control : constant Entity_Id := First_Formal (Entity (Subprog));
6867 Formal_Type : constant Entity_Id := Etype (Control);
6868 First_Actual : Node_Id;
6871 -- Place the name of the operation, with its interpretations,
6872 -- on the rewritten call.
6874 Set_Name (Call_Node, Subprog);
6876 First_Actual := First (Parameter_Associations (Call_Node));
6878 -- For cross-reference purposes, treat the new node as being in
6879 -- the source if the original one is. Set entity and type, even
6880 -- though they may be overwritten during resolution if overloaded.
6882 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
6883 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
6885 if Nkind (N) = N_Selected_Component
6886 and then not Inside_A_Generic
6888 Set_Entity (Selector_Name (N), Entity (Subprog));
6889 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
6892 -- If need be, rewrite first actual as an explicit dereference
6893 -- If the call is overloaded, the rewriting can only be done
6894 -- once the primitive operation is identified.
6896 if Is_Overloaded (Subprog) then
6898 -- The prefix itself may be overloaded, and its interpretations
6899 -- must be propagated to the new actual in the call.
6901 if Is_Overloaded (Obj) then
6902 Save_Interps (Obj, First_Actual);
6905 Rewrite (First_Actual, Obj);
6907 elsif not Is_Access_Type (Formal_Type)
6908 and then Is_Access_Type (Etype (Obj))
6910 Rewrite (First_Actual,
6911 Make_Explicit_Dereference (Sloc (Obj), Obj));
6912 Analyze (First_Actual);
6914 -- If we need to introduce an explicit dereference, verify that
6915 -- the resulting actual is compatible with the mode of the formal.
6917 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
6918 and then Is_Access_Constant (Etype (Obj))
6921 ("expect variable in call to&", Prefix (N), Entity (Subprog));
6924 -- Conversely, if the formal is an access parameter and the object
6925 -- is not, replace the actual with a 'Access reference. Its analysis
6926 -- will check that the object is aliased.
6928 elsif Is_Access_Type (Formal_Type)
6929 and then not Is_Access_Type (Etype (Obj))
6931 -- A special case: A.all'access is illegal if A is an access to a
6932 -- constant and the context requires an access to a variable.
6934 if not Is_Access_Constant (Formal_Type) then
6935 if (Nkind (Obj) = N_Explicit_Dereference
6936 and then Is_Access_Constant (Etype (Prefix (Obj))))
6937 or else not Is_Variable (Obj)
6940 ("actual for& must be a variable", Obj, Control);
6944 Rewrite (First_Actual,
6945 Make_Attribute_Reference (Loc,
6946 Attribute_Name => Name_Access,
6947 Prefix => Relocate_Node (Obj)));
6949 if not Is_Aliased_View (Obj) then
6951 ("object in prefixed call to& must be aliased"
6952 & " (RM-2005 4.3.1 (13))",
6953 Prefix (First_Actual), Subprog);
6956 Analyze (First_Actual);
6959 if Is_Overloaded (Obj) then
6960 Save_Interps (Obj, First_Actual);
6963 Rewrite (First_Actual, Obj);
6966 Rewrite (Node_To_Replace, Call_Node);
6968 -- Propagate the interpretations collected in subprog to the new
6969 -- function call node, to be resolved from context.
6971 if Is_Overloaded (Subprog) then
6972 Save_Interps (Subprog, Node_To_Replace);
6975 Analyze (Node_To_Replace);
6977 -- If the operation has been rewritten into a call, which may get
6978 -- subsequently an explicit dereference, preserve the type on the
6979 -- original node (selected component or indexed component) for
6980 -- subsequent legality tests, e.g. Is_Variable. which examines
6981 -- the original node.
6983 if Nkind (Node_To_Replace) = N_Function_Call then
6985 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
6988 end Complete_Object_Operation;
6990 ----------------------
6991 -- Report_Ambiguity --
6992 ----------------------
6994 procedure Report_Ambiguity (Op : Entity_Id) is
6995 Access_Actual : constant Boolean :=
6996 Is_Access_Type (Etype (Prefix (N)));
6997 Access_Formal : Boolean := False;
7000 Error_Msg_Sloc := Sloc (Op);
7002 if Present (First_Formal (Op)) then
7003 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7006 if Access_Formal and then not Access_Actual then
7007 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7009 ("\possible interpretation"
7010 & " (inherited, with implicit 'Access) #", N);
7013 ("\possible interpretation (with implicit 'Access) #", N);
7016 elsif not Access_Formal and then Access_Actual then
7017 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7019 ("\possible interpretation"
7020 & " ( inherited, with implicit dereference) #", N);
7023 ("\possible interpretation (with implicit dereference) #", N);
7027 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7028 Error_Msg_N ("\possible interpretation (inherited)#", N);
7030 Error_Msg_N -- CODEFIX
7031 ("\possible interpretation#", N);
7034 end Report_Ambiguity;
7036 --------------------------------
7037 -- Transform_Object_Operation --
7038 --------------------------------
7040 procedure Transform_Object_Operation
7041 (Call_Node : out Node_Id;
7042 Node_To_Replace : out Node_Id)
7044 Dummy : constant Node_Id := New_Copy (Obj);
7045 -- Placeholder used as a first parameter in the call, replaced
7046 -- eventually by the proper object.
7048 Parent_Node : constant Node_Id := Parent (N);
7054 -- Common case covering 1) Call to a procedure and 2) Call to a
7055 -- function that has some additional actuals.
7057 if Nkind_In (Parent_Node, N_Function_Call,
7058 N_Procedure_Call_Statement)
7060 -- N is a selected component node containing the name of the
7061 -- subprogram. If N is not the name of the parent node we must
7062 -- not replace the parent node by the new construct. This case
7063 -- occurs when N is a parameterless call to a subprogram that
7064 -- is an actual parameter of a call to another subprogram. For
7066 -- Some_Subprogram (..., Obj.Operation, ...)
7068 and then Name (Parent_Node) = N
7070 Node_To_Replace := Parent_Node;
7072 Actuals := Parameter_Associations (Parent_Node);
7074 if Present (Actuals) then
7075 Prepend (Dummy, Actuals);
7077 Actuals := New_List (Dummy);
7080 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7082 Make_Procedure_Call_Statement (Loc,
7083 Name => New_Copy (Subprog),
7084 Parameter_Associations => Actuals);
7088 Make_Function_Call (Loc,
7089 Name => New_Copy (Subprog),
7090 Parameter_Associations => Actuals);
7094 -- Before analysis, a function call appears as an indexed component
7095 -- if there are no named associations.
7097 elsif Nkind (Parent_Node) = N_Indexed_Component
7098 and then N = Prefix (Parent_Node)
7100 Node_To_Replace := Parent_Node;
7101 Actuals := Expressions (Parent_Node);
7103 Actual := First (Actuals);
7104 while Present (Actual) loop
7109 Prepend (Dummy, Actuals);
7112 Make_Function_Call (Loc,
7113 Name => New_Copy (Subprog),
7114 Parameter_Associations => Actuals);
7116 -- Parameterless call: Obj.F is rewritten as F (Obj)
7119 Node_To_Replace := N;
7122 Make_Function_Call (Loc,
7123 Name => New_Copy (Subprog),
7124 Parameter_Associations => New_List (Dummy));
7126 end Transform_Object_Operation;
7128 ------------------------------
7129 -- Try_Class_Wide_Operation --
7130 ------------------------------
7132 function Try_Class_Wide_Operation
7133 (Call_Node : Node_Id;
7134 Node_To_Replace : Node_Id) return Boolean
7136 Anc_Type : Entity_Id;
7137 Matching_Op : Entity_Id := Empty;
7140 procedure Traverse_Homonyms
7141 (Anc_Type : Entity_Id;
7142 Error : out Boolean);
7143 -- Traverse the homonym chain of the subprogram searching for those
7144 -- homonyms whose first formal has the Anc_Type's class-wide type,
7145 -- or an anonymous access type designating the class-wide type. If
7146 -- an ambiguity is detected, then Error is set to True.
7148 procedure Traverse_Interfaces
7149 (Anc_Type : Entity_Id;
7150 Error : out Boolean);
7151 -- Traverse the list of interfaces, if any, associated with Anc_Type
7152 -- and search for acceptable class-wide homonyms associated with each
7153 -- interface. If an ambiguity is detected, then Error is set to True.
7155 -----------------------
7156 -- Traverse_Homonyms --
7157 -----------------------
7159 procedure Traverse_Homonyms
7160 (Anc_Type : Entity_Id;
7161 Error : out Boolean)
7163 Cls_Type : Entity_Id;
7171 Cls_Type := Class_Wide_Type (Anc_Type);
7173 Hom := Current_Entity (Subprog);
7175 -- Find a non-hidden operation whose first parameter is of the
7176 -- class-wide type, a subtype thereof, or an anonymous access
7177 -- to same. If in an instance, the operation can be considered
7178 -- even if hidden (it may be hidden because the instantiation is
7179 -- expanded after the containing package has been analyzed).
7181 while Present (Hom) loop
7182 if Ekind_In (Hom, E_Procedure, E_Function)
7183 and then (not Is_Hidden (Hom) or else In_Instance)
7184 and then Scope (Hom) = Scope (Anc_Type)
7185 and then Present (First_Formal (Hom))
7187 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7189 (Is_Access_Type (Etype (First_Formal (Hom)))
7191 Ekind (Etype (First_Formal (Hom))) =
7192 E_Anonymous_Access_Type
7195 (Designated_Type (Etype (First_Formal (Hom)))) =
7198 -- If the context is a procedure call, ignore functions
7199 -- in the name of the call.
7201 if Ekind (Hom) = E_Function
7202 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7203 and then N = Name (Parent (N))
7207 -- If the context is a function call, ignore procedures
7208 -- in the name of the call.
7210 elsif Ekind (Hom) = E_Procedure
7211 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7216 Set_Etype (Call_Node, Any_Type);
7217 Set_Is_Overloaded (Call_Node, False);
7220 if No (Matching_Op) then
7221 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
7222 Set_Etype (Call_Node, Any_Type);
7223 Set_Parent (Call_Node, Parent (Node_To_Replace));
7225 Set_Name (Call_Node, Hom_Ref);
7230 Report => Report_Error,
7232 Skip_First => True);
7235 Valid_Candidate (Success, Call_Node, Hom);
7241 Report => Report_Error,
7243 Skip_First => True);
7245 if Present (Valid_Candidate (Success, Call_Node, Hom))
7246 and then Nkind (Call_Node) /= N_Function_Call
7248 Error_Msg_NE ("ambiguous call to&", N, Hom);
7249 Report_Ambiguity (Matching_Op);
7250 Report_Ambiguity (Hom);
7258 Hom := Homonym (Hom);
7260 end Traverse_Homonyms;
7262 -------------------------
7263 -- Traverse_Interfaces --
7264 -------------------------
7266 procedure Traverse_Interfaces
7267 (Anc_Type : Entity_Id;
7268 Error : out Boolean)
7270 Intface_List : constant List_Id :=
7271 Abstract_Interface_List (Anc_Type);
7277 if Is_Non_Empty_List (Intface_List) then
7278 Intface := First (Intface_List);
7279 while Present (Intface) loop
7281 -- Look for acceptable class-wide homonyms associated with
7284 Traverse_Homonyms (Etype (Intface), Error);
7290 -- Continue the search by looking at each of the interface's
7291 -- associated interface ancestors.
7293 Traverse_Interfaces (Etype (Intface), Error);
7302 end Traverse_Interfaces;
7304 -- Start of processing for Try_Class_Wide_Operation
7307 -- If we are searching only for conflicting class-wide subprograms
7308 -- then initialize directly Matching_Op with the target entity.
7310 if CW_Test_Only then
7311 Matching_Op := Entity (Selector_Name (N));
7314 -- Loop through ancestor types (including interfaces), traversing
7315 -- the homonym chain of the subprogram, trying out those homonyms
7316 -- whose first formal has the class-wide type of the ancestor, or
7317 -- an anonymous access type designating the class-wide type.
7319 Anc_Type := Obj_Type;
7321 -- Look for a match among homonyms associated with the ancestor
7323 Traverse_Homonyms (Anc_Type, Error);
7329 -- Continue the search for matches among homonyms associated with
7330 -- any interfaces implemented by the ancestor.
7332 Traverse_Interfaces (Anc_Type, Error);
7338 exit when Etype (Anc_Type) = Anc_Type;
7339 Anc_Type := Etype (Anc_Type);
7342 if Present (Matching_Op) then
7343 Set_Etype (Call_Node, Etype (Matching_Op));
7346 return Present (Matching_Op);
7347 end Try_Class_Wide_Operation;
7349 -----------------------------------
7350 -- Try_One_Prefix_Interpretation --
7351 -----------------------------------
7353 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7357 if Is_Access_Type (Obj_Type) then
7358 Obj_Type := Designated_Type (Obj_Type);
7361 if Ekind (Obj_Type) = E_Private_Subtype then
7362 Obj_Type := Base_Type (Obj_Type);
7365 if Is_Class_Wide_Type (Obj_Type) then
7366 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7369 -- The type may have be obtained through a limited_with clause,
7370 -- in which case the primitive operations are available on its
7371 -- non-limited view. If still incomplete, retrieve full view.
7373 if Ekind (Obj_Type) = E_Incomplete_Type
7374 and then From_With_Type (Obj_Type)
7376 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7379 -- If the object is not tagged, or the type is still an incomplete
7380 -- type, this is not a prefixed call.
7382 if not Is_Tagged_Type (Obj_Type)
7383 or else Is_Incomplete_Type (Obj_Type)
7389 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7390 CW_Result : Boolean;
7391 Prim_Result : Boolean;
7392 pragma Unreferenced (CW_Result);
7395 if not CW_Test_Only then
7397 Try_Primitive_Operation
7398 (Call_Node => New_Call_Node,
7399 Node_To_Replace => Node_To_Replace);
7402 -- Check if there is a class-wide subprogram covering the
7403 -- primitive. This check must be done even if a candidate
7404 -- was found in order to report ambiguous calls.
7406 if not (Prim_Result) then
7408 Try_Class_Wide_Operation
7409 (Call_Node => New_Call_Node,
7410 Node_To_Replace => Node_To_Replace);
7412 -- If we found a primitive we search for class-wide subprograms
7413 -- using a duplicate of the call node (done to avoid missing its
7414 -- decoration if there is no ambiguity).
7418 Try_Class_Wide_Operation
7419 (Call_Node => Dup_Call_Node,
7420 Node_To_Replace => Node_To_Replace);
7423 end Try_One_Prefix_Interpretation;
7425 -----------------------------
7426 -- Try_Primitive_Operation --
7427 -----------------------------
7429 function Try_Primitive_Operation
7430 (Call_Node : Node_Id;
7431 Node_To_Replace : Node_Id) return Boolean
7434 Prim_Op : Entity_Id;
7435 Matching_Op : Entity_Id := Empty;
7436 Prim_Op_Ref : Node_Id := Empty;
7438 Corr_Type : Entity_Id := Empty;
7439 -- If the prefix is a synchronized type, the controlling type of
7440 -- the primitive operation is the corresponding record type, else
7441 -- this is the object type itself.
7443 Success : Boolean := False;
7445 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7446 -- For tagged types the candidate interpretations are found in
7447 -- the list of primitive operations of the type and its ancestors.
7448 -- For formal tagged types we have to find the operations declared
7449 -- in the same scope as the type (including in the generic formal
7450 -- part) because the type itself carries no primitive operations,
7451 -- except for formal derived types that inherit the operations of
7452 -- the parent and progenitors.
7453 -- If the context is a generic subprogram body, the generic formals
7454 -- are visible by name, but are not in the entity list of the
7455 -- subprogram because that list starts with the subprogram formals.
7456 -- We retrieve the candidate operations from the generic declaration.
7458 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7459 -- An operation that overrides an inherited operation in the private
7460 -- part of its package may be hidden, but if the inherited operation
7461 -- is visible a direct call to it will dispatch to the private one,
7462 -- which is therefore a valid candidate.
7464 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7465 -- Verify that the prefix, dereferenced if need be, is a valid
7466 -- controlling argument in a call to Op. The remaining actuals
7467 -- are checked in the subsequent call to Analyze_One_Call.
7469 ------------------------------
7470 -- Collect_Generic_Type_Ops --
7471 ------------------------------
7473 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7474 Bas : constant Entity_Id := Base_Type (T);
7475 Candidates : constant Elist_Id := New_Elmt_List;
7479 procedure Check_Candidate;
7480 -- The operation is a candidate if its first parameter is a
7481 -- controlling operand of the desired type.
7483 -----------------------
7484 -- Check_Candidate; --
7485 -----------------------
7487 procedure Check_Candidate is
7489 Formal := First_Formal (Subp);
7492 and then Is_Controlling_Formal (Formal)
7494 (Base_Type (Etype (Formal)) = Bas
7496 (Is_Access_Type (Etype (Formal))
7497 and then Designated_Type (Etype (Formal)) = Bas))
7499 Append_Elmt (Subp, Candidates);
7501 end Check_Candidate;
7503 -- Start of processing for Collect_Generic_Type_Ops
7506 if Is_Derived_Type (T) then
7507 return Primitive_Operations (T);
7509 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7511 -- Scan the list of generic formals to find subprograms
7512 -- that may have a first controlling formal of the type.
7514 if Nkind (Unit_Declaration_Node (Scope (T)))
7515 = N_Generic_Subprogram_Declaration
7522 First (Generic_Formal_Declarations
7523 (Unit_Declaration_Node (Scope (T))));
7524 while Present (Decl) loop
7525 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7526 Subp := Defining_Entity (Decl);
7537 -- Scan the list of entities declared in the same scope as
7538 -- the type. In general this will be an open scope, given that
7539 -- the call we are analyzing can only appear within a generic
7540 -- declaration or body (either the one that declares T, or a
7543 -- For a subtype representing a generic actual type, go to the
7546 if Is_Generic_Actual_Type (T) then
7547 Subp := First_Entity (Scope (Base_Type (T)));
7549 Subp := First_Entity (Scope (T));
7552 while Present (Subp) loop
7553 if Is_Overloadable (Subp) then
7562 end Collect_Generic_Type_Ops;
7564 ---------------------------
7565 -- Is_Private_Overriding --
7566 ---------------------------
7568 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
7569 Visible_Op : constant Entity_Id := Homonym (Op);
7572 return Present (Visible_Op)
7573 and then Scope (Op) = Scope (Visible_Op)
7574 and then not Comes_From_Source (Visible_Op)
7575 and then Alias (Visible_Op) = Op
7576 and then not Is_Hidden (Visible_Op);
7577 end Is_Private_Overriding;
7579 -----------------------------
7580 -- Valid_First_Argument_Of --
7581 -----------------------------
7583 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
7584 Typ : Entity_Id := Etype (First_Formal (Op));
7587 if Is_Concurrent_Type (Typ)
7588 and then Present (Corresponding_Record_Type (Typ))
7590 Typ := Corresponding_Record_Type (Typ);
7593 -- Simple case. Object may be a subtype of the tagged type or
7594 -- may be the corresponding record of a synchronized type.
7596 return Obj_Type = Typ
7597 or else Base_Type (Obj_Type) = Typ
7598 or else Corr_Type = Typ
7600 -- Prefix can be dereferenced
7603 (Is_Access_Type (Corr_Type)
7604 and then Designated_Type (Corr_Type) = Typ)
7606 -- Formal is an access parameter, for which the object
7607 -- can provide an access.
7610 (Ekind (Typ) = E_Anonymous_Access_Type
7612 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
7613 end Valid_First_Argument_Of;
7615 -- Start of processing for Try_Primitive_Operation
7618 -- Look for subprograms in the list of primitive operations. The name
7619 -- must be identical, and the kind of call indicates the expected
7620 -- kind of operation (function or procedure). If the type is a
7621 -- (tagged) synchronized type, the primitive ops are attached to the
7622 -- corresponding record (base) type.
7624 if Is_Concurrent_Type (Obj_Type) then
7625 if Present (Corresponding_Record_Type (Obj_Type)) then
7626 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
7627 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
7629 Corr_Type := Obj_Type;
7630 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7633 elsif not Is_Generic_Type (Obj_Type) then
7634 Corr_Type := Obj_Type;
7635 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
7638 Corr_Type := Obj_Type;
7639 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7642 while Present (Elmt) loop
7643 Prim_Op := Node (Elmt);
7645 if Chars (Prim_Op) = Chars (Subprog)
7646 and then Present (First_Formal (Prim_Op))
7647 and then Valid_First_Argument_Of (Prim_Op)
7649 (Nkind (Call_Node) = N_Function_Call)
7650 = (Ekind (Prim_Op) = E_Function)
7652 -- Ada 2005 (AI-251): If this primitive operation corresponds
7653 -- with an immediate ancestor interface there is no need to add
7654 -- it to the list of interpretations; the corresponding aliased
7655 -- primitive is also in this list of primitive operations and
7656 -- will be used instead.
7658 if (Present (Interface_Alias (Prim_Op))
7659 and then Is_Ancestor (Find_Dispatching_Type
7660 (Alias (Prim_Op)), Corr_Type))
7662 -- Do not consider hidden primitives unless the type is in an
7663 -- open scope or we are within an instance, where visibility
7664 -- is known to be correct, or else if this is an overriding
7665 -- operation in the private part for an inherited operation.
7667 or else (Is_Hidden (Prim_Op)
7668 and then not Is_Immediately_Visible (Obj_Type)
7669 and then not In_Instance
7670 and then not Is_Private_Overriding (Prim_Op))
7675 Set_Etype (Call_Node, Any_Type);
7676 Set_Is_Overloaded (Call_Node, False);
7678 if No (Matching_Op) then
7679 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
7680 Candidate := Prim_Op;
7682 Set_Parent (Call_Node, Parent (Node_To_Replace));
7684 Set_Name (Call_Node, Prim_Op_Ref);
7690 Report => Report_Error,
7692 Skip_First => True);
7694 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
7696 -- More than one interpretation, collect for subsequent
7697 -- disambiguation. If this is a procedure call and there
7698 -- is another match, report ambiguity now.
7704 Report => Report_Error,
7706 Skip_First => True);
7708 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
7709 and then Nkind (Call_Node) /= N_Function_Call
7711 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
7712 Report_Ambiguity (Matching_Op);
7713 Report_Ambiguity (Prim_Op);
7723 if Present (Matching_Op) then
7724 Set_Etype (Call_Node, Etype (Matching_Op));
7727 return Present (Matching_Op);
7728 end Try_Primitive_Operation;
7730 -- Start of processing for Try_Object_Operation
7733 Analyze_Expression (Obj);
7735 -- Analyze the actuals if node is known to be a subprogram call
7737 if Is_Subprg_Call and then N = Name (Parent (N)) then
7738 Actual := First (Parameter_Associations (Parent (N)));
7739 while Present (Actual) loop
7740 Analyze_Expression (Actual);
7745 -- Build a subprogram call node, using a copy of Obj as its first
7746 -- actual. This is a placeholder, to be replaced by an explicit
7747 -- dereference when needed.
7749 Transform_Object_Operation
7750 (Call_Node => New_Call_Node,
7751 Node_To_Replace => Node_To_Replace);
7753 Set_Etype (New_Call_Node, Any_Type);
7754 Set_Etype (Subprog, Any_Type);
7755 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
7757 if not Is_Overloaded (Obj) then
7758 Try_One_Prefix_Interpretation (Obj_Type);
7765 Get_First_Interp (Obj, I, It);
7766 while Present (It.Nam) loop
7767 Try_One_Prefix_Interpretation (It.Typ);
7768 Get_Next_Interp (I, It);
7773 if Etype (New_Call_Node) /= Any_Type then
7775 -- No need to complete the tree transformations if we are only
7776 -- searching for conflicting class-wide subprograms
7778 if CW_Test_Only then
7781 Complete_Object_Operation
7782 (Call_Node => New_Call_Node,
7783 Node_To_Replace => Node_To_Replace);
7787 elsif Present (Candidate) then
7789 -- The argument list is not type correct. Re-analyze with error
7790 -- reporting enabled, and use one of the possible candidates.
7791 -- In All_Errors_Mode, re-analyze all failed interpretations.
7793 if All_Errors_Mode then
7794 Report_Error := True;
7795 if Try_Primitive_Operation
7796 (Call_Node => New_Call_Node,
7797 Node_To_Replace => Node_To_Replace)
7800 Try_Class_Wide_Operation
7801 (Call_Node => New_Call_Node,
7802 Node_To_Replace => Node_To_Replace)
7809 (N => New_Call_Node,
7813 Skip_First => True);
7816 -- No need for further errors
7821 -- There was no candidate operation, so report it as an error
7822 -- in the caller: Analyze_Selected_Component.
7826 end Try_Object_Operation;
7832 procedure wpo (T : Entity_Id) is
7837 if not Is_Tagged_Type (T) then
7841 E := First_Elmt (Primitive_Operations (Base_Type (T)));
7842 while Present (E) loop
7844 Write_Int (Int (Op));
7845 Write_Str (" === ");
7846 Write_Name (Chars (Op));
7848 Write_Name (Chars (Scope (Op)));