* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob8801fb750bad15b54eb0600d40686c7b204035d7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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 Itypes; use Itypes;
34 with Lib; use Lib;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Namet.Sp; use Namet.Sp;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Output; use Output;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Case; use Sem_Case;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Dist; use Sem_Dist;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Sem_Ch4 is
67 -- Tables which speed up the identification of dangerous calls to Ada 2012
68 -- functions with writable actuals (AI05-0144).
70 -- The following table enumerates the Ada constructs which may evaluate in
71 -- arbitrary order. It does not cover all the language constructs which can
72 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
74 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
75 (N_Aggregate => True,
76 N_Assignment_Statement => True,
77 N_Entry_Call_Statement => True,
78 N_Extension_Aggregate => True,
79 N_Full_Type_Declaration => True,
80 N_Indexed_Component => True,
81 N_Object_Declaration => True,
82 N_Pragma => True,
83 N_Range => True,
84 N_Slice => True,
85 N_Array_Type_Definition => True,
86 N_Membership_Test => True,
87 N_Binary_Op => True,
88 N_Subprogram_Call => True,
89 others => False);
91 -- The following table enumerates the nodes on which we stop climbing when
92 -- locating the outermost Ada construct that can be evaluated in arbitrary
93 -- order.
95 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
96 (N_Aggregate => True,
97 N_Assignment_Statement => True,
98 N_Entry_Call_Statement => True,
99 N_Extended_Return_Statement => True,
100 N_Extension_Aggregate => True,
101 N_Full_Type_Declaration => True,
102 N_Object_Declaration => True,
103 N_Object_Renaming_Declaration => True,
104 N_Package_Specification => True,
105 N_Pragma => True,
106 N_Procedure_Call_Statement => True,
107 N_Simple_Return_Statement => True,
108 N_Has_Condition => True,
109 others => False);
111 -----------------------
112 -- Local Subprograms --
113 -----------------------
115 procedure Analyze_Concatenation_Rest (N : Node_Id);
116 -- Does the "rest" of the work of Analyze_Concatenation, after the left
117 -- operand has been analyzed. See Analyze_Concatenation for details.
119 procedure Analyze_Expression (N : Node_Id);
120 -- For expressions that are not names, this is just a call to analyze. If
121 -- the expression is a name, it may be a call to a parameterless function,
122 -- and if so must be converted into an explicit call node and analyzed as
123 -- such. This deproceduring must be done during the first pass of overload
124 -- resolution, because otherwise a procedure call with overloaded actuals
125 -- may fail to resolve.
127 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
128 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
129 -- operator name or an expanded name whose selector is an operator name,
130 -- and one possible interpretation is as a predefined operator.
132 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
133 -- If the prefix of a selected_component is overloaded, the proper
134 -- interpretation that yields a record type with the proper selector
135 -- name must be selected.
137 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
138 -- Procedure to analyze a user defined binary operator, which is resolved
139 -- like a function, but instead of a list of actuals it is presented
140 -- with the left and right operands of an operator node.
142 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
143 -- Procedure to analyze a user defined unary operator, which is resolved
144 -- like a function, but instead of a list of actuals, it is presented with
145 -- the operand of the operator node.
147 procedure Ambiguous_Operands (N : Node_Id);
148 -- For equality, membership, and comparison operators with overloaded
149 -- arguments, list possible interpretations.
151 procedure Analyze_One_Call
152 (N : Node_Id;
153 Nam : Entity_Id;
154 Report : Boolean;
155 Success : out Boolean;
156 Skip_First : Boolean := False);
157 -- Check one interpretation of an overloaded subprogram name for
158 -- compatibility with the types of the actuals in a call. If there is a
159 -- single interpretation which does not match, post error if Report is
160 -- set to True.
162 -- Nam is the entity that provides the formals against which the actuals
163 -- are checked. Nam is either the name of a subprogram, or the internal
164 -- subprogram type constructed for an access_to_subprogram. If the actuals
165 -- are compatible with Nam, then Nam is added to the list of candidate
166 -- interpretations for N, and Success is set to True.
168 -- The flag Skip_First is used when analyzing a call that was rewritten
169 -- from object notation. In this case the first actual may have to receive
170 -- an explicit dereference, depending on the first formal of the operation
171 -- being called. The caller will have verified that the object is legal
172 -- for the call. If the remaining parameters match, the first parameter
173 -- will rewritten as a dereference if needed, prior to completing analysis.
175 procedure Check_Misspelled_Selector
176 (Prefix : Entity_Id;
177 Sel : Node_Id);
178 -- Give possible misspelling message if Sel seems likely to be a mis-
179 -- spelling of one of the selectors of the Prefix. This is called by
180 -- Analyze_Selected_Component after producing an invalid selector error
181 -- message.
183 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
184 -- Verify that type T is declared in scope S. Used to find interpretations
185 -- for operators given by expanded names. This is abstracted as a separate
186 -- function to handle extensions to System, where S is System, but T is
187 -- declared in the extension.
189 procedure Find_Arithmetic_Types
190 (L, R : Node_Id;
191 Op_Id : Entity_Id;
192 N : Node_Id);
193 -- L and R are the operands of an arithmetic operator. Find consistent
194 -- pairs of interpretations for L and R that have a numeric type consistent
195 -- with the semantics of the operator.
197 procedure Find_Comparison_Types
198 (L, R : Node_Id;
199 Op_Id : Entity_Id;
200 N : Node_Id);
201 -- L and R are operands of a comparison operator. Find consistent pairs of
202 -- interpretations for L and R.
204 procedure Find_Concatenation_Types
205 (L, R : Node_Id;
206 Op_Id : Entity_Id;
207 N : Node_Id);
208 -- For the four varieties of concatenation
210 procedure Find_Equality_Types
211 (L, R : Node_Id;
212 Op_Id : Entity_Id;
213 N : Node_Id);
214 -- Ditto for equality operators
216 procedure Find_Boolean_Types
217 (L, R : Node_Id;
218 Op_Id : Entity_Id;
219 N : Node_Id);
220 -- Ditto for binary logical operations
222 procedure Find_Negation_Types
223 (R : Node_Id;
224 Op_Id : Entity_Id;
225 N : Node_Id);
226 -- Find consistent interpretation for operand of negation operator
228 procedure Find_Non_Universal_Interpretations
229 (N : Node_Id;
230 R : Node_Id;
231 Op_Id : Entity_Id;
232 T1 : Entity_Id);
233 -- For equality and comparison operators, the result is always boolean, and
234 -- the legality of the operation is determined from the visibility of the
235 -- operand types. If one of the operands has a universal interpretation,
236 -- the legality check uses some compatible non-universal interpretation of
237 -- the other operand. N can be an operator node, or a function call whose
238 -- name is an operator designator. Any_Access, which is the initial type of
239 -- the literal NULL, is a universal type for the purpose of this routine.
241 function Find_Primitive_Operation (N : Node_Id) return Boolean;
242 -- Find candidate interpretations for the name Obj.Proc when it appears in
243 -- a subprogram renaming declaration.
245 procedure Find_Unary_Types
246 (R : Node_Id;
247 Op_Id : Entity_Id;
248 N : Node_Id);
249 -- Unary arithmetic types: plus, minus, abs
251 procedure Check_Arithmetic_Pair
252 (T1, T2 : Entity_Id;
253 Op_Id : Entity_Id;
254 N : Node_Id);
255 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
256 -- for left and right operand. Determine whether they constitute a valid
257 -- pair for the given operator, and record the corresponding interpretation
258 -- of the operator node. The node N may be an operator node (the usual
259 -- case) or a function call whose prefix is an operator designator. In
260 -- both cases Op_Id is the operator name itself.
262 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
263 -- Give detailed information on overloaded call where none of the
264 -- interpretations match. N is the call node, Nam the designator for
265 -- the overloaded entity being called.
267 function Junk_Operand (N : Node_Id) return Boolean;
268 -- Test for an operand that is an inappropriate entity (e.g. a package
269 -- name or a label). If so, issue an error message and return True. If
270 -- the operand is not an inappropriate entity kind, return False.
272 procedure Operator_Check (N : Node_Id);
273 -- Verify that an operator has received some valid interpretation. If none
274 -- was found, determine whether a use clause would make the operation
275 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
276 -- every type compatible with the operator, even if the operator for the
277 -- type is not directly visible. The routine uses this type to emit a more
278 -- informative message.
280 function Process_Implicit_Dereference_Prefix
281 (E : Entity_Id;
282 P : Node_Id) return Entity_Id;
283 -- Called when P is the prefix of an implicit dereference, denoting an
284 -- object E. The function returns the designated type of the prefix, taking
285 -- into account that the designated type of an anonymous access type may be
286 -- a limited view, when the nonlimited view is visible.
288 -- If in semantics only mode (-gnatc or generic), the function also records
289 -- that the prefix is a reference to E, if any. Normally, such a reference
290 -- is generated only when the implicit dereference is expanded into an
291 -- explicit one, but for consistency we must generate the reference when
292 -- expansion is disabled as well.
294 procedure Remove_Abstract_Operations (N : Node_Id);
295 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
296 -- operation is not a candidate interpretation.
298 function Try_Container_Indexing
299 (N : Node_Id;
300 Prefix : Node_Id;
301 Exprs : List_Id) return Boolean;
302 -- AI05-0139: Generalized indexing to support iterators over containers
304 function Try_Indexed_Call
305 (N : Node_Id;
306 Nam : Entity_Id;
307 Typ : Entity_Id;
308 Skip_First : Boolean) return Boolean;
309 -- If a function has defaults for all its actuals, a call to it may in fact
310 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
311 -- interpretation as an indexing, prior to analysis as a call. If both are
312 -- possible, the node is overloaded with both interpretations (same symbol
313 -- but two different types). If the call is written in prefix form, the
314 -- prefix becomes the first parameter in the call, and only the remaining
315 -- actuals must be checked for the presence of defaults.
317 function Try_Indirect_Call
318 (N : Node_Id;
319 Nam : Entity_Id;
320 Typ : Entity_Id) return Boolean;
321 -- Similarly, a function F that needs no actuals can return an access to a
322 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
323 -- the call may be overloaded with both interpretations.
325 procedure wpo (T : Entity_Id);
326 pragma Warnings (Off, wpo);
327 -- Used for debugging: obtain list of primitive operations even if
328 -- type is not frozen and dispatch table is not built yet.
330 ------------------------
331 -- Ambiguous_Operands --
332 ------------------------
334 procedure Ambiguous_Operands (N : Node_Id) is
335 procedure List_Operand_Interps (Opnd : Node_Id);
337 --------------------------
338 -- List_Operand_Interps --
339 --------------------------
341 procedure List_Operand_Interps (Opnd : Node_Id) is
342 Nam : Node_Id;
343 pragma Warnings (Off, Nam);
344 Err : Node_Id := N;
346 begin
347 if Is_Overloaded (Opnd) then
348 if Nkind (Opnd) in N_Op then
349 Nam := Opnd;
351 elsif Nkind (Opnd) = N_Function_Call then
352 Nam := Name (Opnd);
354 elsif Ada_Version >= Ada_2012 then
355 declare
356 It : Interp;
357 I : Interp_Index;
359 begin
360 Get_First_Interp (Opnd, I, It);
361 while Present (It.Nam) loop
362 if Has_Implicit_Dereference (It.Typ) then
363 Error_Msg_N
364 ("can be interpreted as implicit dereference", Opnd);
365 return;
366 end if;
368 Get_Next_Interp (I, It);
369 end loop;
370 end;
372 return;
373 end if;
375 else
376 return;
377 end if;
379 if Opnd = Left_Opnd (N) then
380 Error_Msg_N
381 ("\left operand has the following interpretations", N);
382 else
383 Error_Msg_N
384 ("\right operand has the following interpretations", N);
385 Err := Opnd;
386 end if;
388 List_Interps (Nam, Err);
389 end List_Operand_Interps;
391 -- Start of processing for Ambiguous_Operands
393 begin
394 if Nkind (N) in N_Membership_Test then
395 Error_Msg_N ("ambiguous operands for membership", N);
397 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
398 Error_Msg_N ("ambiguous operands for equality", N);
400 else
401 Error_Msg_N ("ambiguous operands for comparison", N);
402 end if;
404 if All_Errors_Mode then
405 List_Operand_Interps (Left_Opnd (N));
406 List_Operand_Interps (Right_Opnd (N));
407 else
408 Error_Msg_N ("\use -gnatf switch for details", N);
409 end if;
410 end Ambiguous_Operands;
412 -----------------------
413 -- Analyze_Aggregate --
414 -----------------------
416 -- Most of the analysis of Aggregates requires that the type be known,
417 -- and is therefore put off until resolution.
419 procedure Analyze_Aggregate (N : Node_Id) is
420 begin
421 if No (Etype (N)) then
422 Set_Etype (N, Any_Composite);
423 end if;
424 end Analyze_Aggregate;
426 -----------------------
427 -- Analyze_Allocator --
428 -----------------------
430 procedure Analyze_Allocator (N : Node_Id) is
431 Loc : constant Source_Ptr := Sloc (N);
432 Sav_Errs : constant Nat := Serious_Errors_Detected;
433 E : Node_Id := Expression (N);
434 Acc_Type : Entity_Id;
435 Type_Id : Entity_Id;
436 P : Node_Id;
437 C : Node_Id;
438 Onode : Node_Id;
440 begin
441 Check_SPARK_05_Restriction ("allocator is not allowed", N);
443 -- Deal with allocator restrictions
445 -- In accordance with H.4(7), the No_Allocators restriction only applies
446 -- to user-written allocators. The same consideration applies to the
447 -- No_Standard_Allocators_Before_Elaboration restriction.
449 if Comes_From_Source (N) then
450 Check_Restriction (No_Allocators, N);
452 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
453 -- look at enclosing context, checking task/main subprogram case.
455 C := N;
456 P := Parent (C);
457 while Present (P) loop
459 -- For the task case we need a handled sequence of statements,
460 -- where the occurrence of the allocator is within the statements
461 -- and the parent is a task body
463 if Nkind (P) = N_Handled_Sequence_Of_Statements
464 and then Is_List_Member (C)
465 and then List_Containing (C) = Statements (P)
466 then
467 Onode := Original_Node (Parent (P));
469 -- Check for allocator within task body, this is a definite
470 -- violation of No_Allocators_After_Elaboration we can detect
471 -- at compile time.
473 if Nkind (Onode) = N_Task_Body then
474 Check_Restriction
475 (No_Standard_Allocators_After_Elaboration, N);
476 exit;
477 end if;
478 end if;
480 -- The other case is appearance in a subprogram body. This is
481 -- a violation if this is a library level subprogram with no
482 -- parameters. Note that this is now a static error even if the
483 -- subprogram is not the main program (this is a change, in an
484 -- earlier version only the main program was affected, and the
485 -- check had to be done in the binder.
487 if Nkind (P) = N_Subprogram_Body
488 and then Nkind (Parent (P)) = N_Compilation_Unit
489 and then No (Parameter_Specifications (Specification (P)))
490 then
491 Check_Restriction
492 (No_Standard_Allocators_After_Elaboration, N);
493 end if;
495 C := P;
496 P := Parent (C);
497 end loop;
498 end if;
500 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
501 -- any. The expected type for the name is any type. A non-overloading
502 -- rule then requires it to be of a type descended from
503 -- System.Storage_Pools.Subpools.Subpool_Handle.
505 -- This isn't exactly what the AI says, but it seems to be the right
506 -- rule. The AI should be fixed.???
508 declare
509 Subpool : constant Node_Id := Subpool_Handle_Name (N);
511 begin
512 if Present (Subpool) then
513 Analyze (Subpool);
515 if Is_Overloaded (Subpool) then
516 Error_Msg_N ("ambiguous subpool handle", Subpool);
517 end if;
519 -- Check that Etype (Subpool) is descended from Subpool_Handle
521 Resolve (Subpool);
522 end if;
523 end;
525 -- Analyze the qualified expression or subtype indication
527 if Nkind (E) = N_Qualified_Expression then
528 Acc_Type := Create_Itype (E_Allocator_Type, N);
529 Set_Etype (Acc_Type, Acc_Type);
530 Find_Type (Subtype_Mark (E));
532 -- Analyze the qualified expression, and apply the name resolution
533 -- rule given in 4.7(3).
535 Analyze (E);
536 Type_Id := Etype (E);
537 Set_Directly_Designated_Type (Acc_Type, Type_Id);
539 -- A qualified expression requires an exact match of the type,
540 -- class-wide matching is not allowed.
542 -- if Is_Class_Wide_Type (Type_Id)
543 -- and then Base_Type
544 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
545 -- then
546 -- Wrong_Type (Expression (E), Type_Id);
547 -- end if;
549 -- We don't analyze the qualified expression itself because it's
550 -- part of the allocator. It is fully analyzed and resolved when
551 -- the allocator is resolved with the context type.
553 Set_Etype (E, Type_Id);
555 -- Case where allocator has a subtype indication
557 else
558 declare
559 Def_Id : Entity_Id;
560 Base_Typ : Entity_Id;
562 begin
563 -- If the allocator includes a N_Subtype_Indication then a
564 -- constraint is present, otherwise the node is a subtype mark.
565 -- Introduce an explicit subtype declaration into the tree
566 -- defining some anonymous subtype and rewrite the allocator to
567 -- use this subtype rather than the subtype indication.
569 -- It is important to introduce the explicit subtype declaration
570 -- so that the bounds of the subtype indication are attached to
571 -- the tree in case the allocator is inside a generic unit.
573 -- Finally, if there is no subtype indication and the type is
574 -- a tagged unconstrained type with discriminants, the designated
575 -- object is constrained by their default values, and it is
576 -- simplest to introduce an explicit constraint now. In some cases
577 -- this is done during expansion, but freeze actions are certain
578 -- to be emitted in the proper order if constraint is explicit.
580 if Is_Entity_Name (E) and then Expander_Active then
581 Find_Type (E);
582 Type_Id := Entity (E);
584 if Is_Tagged_Type (Type_Id)
585 and then Has_Discriminants (Type_Id)
586 and then not Is_Constrained (Type_Id)
587 and then
588 Present
589 (Discriminant_Default_Value
590 (First_Discriminant (Type_Id)))
591 then
592 declare
593 Constr : constant List_Id := New_List;
594 Loc : constant Source_Ptr := Sloc (E);
595 Discr : Entity_Id := First_Discriminant (Type_Id);
597 begin
598 if Present (Discriminant_Default_Value (Discr)) then
599 while Present (Discr) loop
600 Append (Discriminant_Default_Value (Discr), Constr);
601 Next_Discriminant (Discr);
602 end loop;
604 Rewrite (E,
605 Make_Subtype_Indication (Loc,
606 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
607 Constraint =>
608 Make_Index_Or_Discriminant_Constraint (Loc,
609 Constraints => Constr)));
610 end if;
611 end;
612 end if;
613 end if;
615 if Nkind (E) = N_Subtype_Indication then
617 -- A constraint is only allowed for a composite type in Ada
618 -- 95. In Ada 83, a constraint is also allowed for an
619 -- access-to-composite type, but the constraint is ignored.
621 Find_Type (Subtype_Mark (E));
622 Base_Typ := Entity (Subtype_Mark (E));
624 if Is_Elementary_Type (Base_Typ) then
625 if not (Ada_Version = Ada_83
626 and then Is_Access_Type (Base_Typ))
627 then
628 Error_Msg_N ("constraint not allowed here", E);
630 if Nkind (Constraint (E)) =
631 N_Index_Or_Discriminant_Constraint
632 then
633 Error_Msg_N -- CODEFIX
634 ("\if qualified expression was meant, " &
635 "use apostrophe", Constraint (E));
636 end if;
637 end if;
639 -- Get rid of the bogus constraint:
641 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
642 Analyze_Allocator (N);
643 return;
644 end if;
646 if Expander_Active then
647 Def_Id := Make_Temporary (Loc, 'S');
649 Insert_Action (E,
650 Make_Subtype_Declaration (Loc,
651 Defining_Identifier => Def_Id,
652 Subtype_Indication => Relocate_Node (E)));
654 if Sav_Errs /= Serious_Errors_Detected
655 and then Nkind (Constraint (E)) =
656 N_Index_Or_Discriminant_Constraint
657 then
658 Error_Msg_N -- CODEFIX
659 ("if qualified expression was meant, "
660 & "use apostrophe!", Constraint (E));
661 end if;
663 E := New_Occurrence_Of (Def_Id, Loc);
664 Rewrite (Expression (N), E);
665 end if;
666 end if;
668 Type_Id := Process_Subtype (E, N);
669 Acc_Type := Create_Itype (E_Allocator_Type, N);
670 Set_Etype (Acc_Type, Acc_Type);
671 Set_Directly_Designated_Type (Acc_Type, Type_Id);
672 Check_Fully_Declared (Type_Id, N);
674 -- Ada 2005 (AI-231): If the designated type is itself an access
675 -- type that excludes null, its default initialization will
676 -- be a null object, and we can insert an unconditional raise
677 -- before the allocator.
679 -- Ada 2012 (AI-104): A not null indication here is altogether
680 -- illegal.
682 if Can_Never_Be_Null (Type_Id) then
683 declare
684 Not_Null_Check : constant Node_Id :=
685 Make_Raise_Constraint_Error (Sloc (E),
686 Reason => CE_Null_Not_Allowed);
688 begin
689 if Expander_Active then
690 Insert_Action (N, Not_Null_Check);
691 Analyze (Not_Null_Check);
693 elsif Warn_On_Ada_2012_Compatibility then
694 Error_Msg_N
695 ("null value not allowed here in Ada 2012?y?", E);
696 end if;
697 end;
698 end if;
700 -- Check for missing initialization. Skip this check if we already
701 -- had errors on analyzing the allocator, since in that case these
702 -- are probably cascaded errors.
704 if not Is_Definite_Subtype (Type_Id)
705 and then Serious_Errors_Detected = Sav_Errs
706 then
707 -- The build-in-place machinery may produce an allocator when
708 -- the designated type is indefinite but the underlying type is
709 -- not. In this case the unknown discriminants are meaningless
710 -- and should not trigger error messages. Check the parent node
711 -- because the allocator is marked as coming from source.
713 if Present (Underlying_Type (Type_Id))
714 and then Is_Definite_Subtype (Underlying_Type (Type_Id))
715 and then not Comes_From_Source (Parent (N))
716 then
717 null;
719 -- An unusual case arises when the parent of a derived type is
720 -- a limited record extension with unknown discriminants, and
721 -- its full view has no discriminants.
723 -- A more general fix might be to create the proper underlying
724 -- type for such a derived type, but it is a record type with
725 -- no private attributes, so this required extending the
726 -- meaning of this attribute. ???
728 elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private
729 and then Present (Underlying_Type (Etype (Type_Id)))
730 and then
731 not Has_Discriminants (Underlying_Type (Etype (Type_Id)))
732 and then not Comes_From_Source (Parent (N))
733 then
734 null;
736 elsif Is_Class_Wide_Type (Type_Id) then
737 Error_Msg_N
738 ("initialization required in class-wide allocation", N);
740 else
741 if Ada_Version < Ada_2005
742 and then Is_Limited_Type (Type_Id)
743 then
744 Error_Msg_N ("unconstrained allocation not allowed", N);
746 if Is_Array_Type (Type_Id) then
747 Error_Msg_N
748 ("\constraint with array bounds required", N);
750 elsif Has_Unknown_Discriminants (Type_Id) then
751 null;
753 else pragma Assert (Has_Discriminants (Type_Id));
754 Error_Msg_N
755 ("\constraint with discriminant values required", N);
756 end if;
758 -- Limited Ada 2005 and general nonlimited case
760 else
761 Error_Msg_N
762 ("uninitialized unconstrained allocation not "
763 & "allowed", N);
765 if Is_Array_Type (Type_Id) then
766 Error_Msg_N
767 ("\qualified expression or constraint with "
768 & "array bounds required", N);
770 elsif Has_Unknown_Discriminants (Type_Id) then
771 Error_Msg_N ("\qualified expression required", N);
773 else pragma Assert (Has_Discriminants (Type_Id));
774 Error_Msg_N
775 ("\qualified expression or constraint with "
776 & "discriminant values required", N);
777 end if;
778 end if;
779 end if;
780 end if;
781 end;
782 end if;
784 if Is_Abstract_Type (Type_Id) then
785 Error_Msg_N ("cannot allocate abstract object", E);
786 end if;
788 if Has_Task (Designated_Type (Acc_Type)) then
789 Check_Restriction (No_Tasking, N);
790 Check_Restriction (Max_Tasks, N);
791 Check_Restriction (No_Task_Allocators, N);
792 end if;
794 -- Check restriction against dynamically allocated protected objects
796 if Has_Protected (Designated_Type (Acc_Type)) then
797 Check_Restriction (No_Protected_Type_Allocators, N);
798 end if;
800 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
801 -- type is nested, and the designated type needs finalization. The rule
802 -- is conservative in that class-wide types need finalization.
804 if Needs_Finalization (Designated_Type (Acc_Type))
805 and then not Is_Library_Level_Entity (Acc_Type)
806 then
807 Check_Restriction (No_Nested_Finalization, N);
808 end if;
810 -- Check that an allocator of a nested access type doesn't create a
811 -- protected object when restriction No_Local_Protected_Objects applies.
813 if Has_Protected (Designated_Type (Acc_Type))
814 and then not Is_Library_Level_Entity (Acc_Type)
815 then
816 Check_Restriction (No_Local_Protected_Objects, N);
817 end if;
819 -- Likewise for No_Local_Timing_Events
821 if Has_Timing_Event (Designated_Type (Acc_Type))
822 and then not Is_Library_Level_Entity (Acc_Type)
823 then
824 Check_Restriction (No_Local_Timing_Events, N);
825 end if;
827 -- If the No_Streams restriction is set, check that the type of the
828 -- object is not, and does not contain, any subtype derived from
829 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
830 -- Has_Stream just for efficiency reasons. There is no point in
831 -- spending time on a Has_Stream check if the restriction is not set.
833 if Restriction_Check_Required (No_Streams) then
834 if Has_Stream (Designated_Type (Acc_Type)) then
835 Check_Restriction (No_Streams, N);
836 end if;
837 end if;
839 Set_Etype (N, Acc_Type);
841 if not Is_Library_Level_Entity (Acc_Type) then
842 Check_Restriction (No_Local_Allocators, N);
843 end if;
845 if Serious_Errors_Detected > Sav_Errs then
846 Set_Error_Posted (N);
847 Set_Etype (N, Any_Type);
848 end if;
849 end Analyze_Allocator;
851 ---------------------------
852 -- Analyze_Arithmetic_Op --
853 ---------------------------
855 procedure Analyze_Arithmetic_Op (N : Node_Id) is
856 L : constant Node_Id := Left_Opnd (N);
857 R : constant Node_Id := Right_Opnd (N);
858 Op_Id : Entity_Id;
860 begin
861 Candidate_Type := Empty;
862 Analyze_Expression (L);
863 Analyze_Expression (R);
865 -- If the entity is already set, the node is the instantiation of a
866 -- generic node with a non-local reference, or was manufactured by a
867 -- call to Make_Op_xxx. In either case the entity is known to be valid,
868 -- and we do not need to collect interpretations, instead we just get
869 -- the single possible interpretation.
871 Op_Id := Entity (N);
873 if Present (Op_Id) then
874 if Ekind (Op_Id) = E_Operator then
876 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
877 and then Treat_Fixed_As_Integer (N)
878 then
879 null;
880 else
881 Set_Etype (N, Any_Type);
882 Find_Arithmetic_Types (L, R, Op_Id, N);
883 end if;
885 else
886 Set_Etype (N, Any_Type);
887 Add_One_Interp (N, Op_Id, Etype (Op_Id));
888 end if;
890 -- Entity is not already set, so we do need to collect interpretations
892 else
893 Set_Etype (N, Any_Type);
895 Op_Id := Get_Name_Entity_Id (Chars (N));
896 while Present (Op_Id) loop
897 if Ekind (Op_Id) = E_Operator
898 and then Present (Next_Entity (First_Entity (Op_Id)))
899 then
900 Find_Arithmetic_Types (L, R, Op_Id, N);
902 -- The following may seem superfluous, because an operator cannot
903 -- be generic, but this ignores the cleverness of the author of
904 -- ACVC bc1013a.
906 elsif Is_Overloadable (Op_Id) then
907 Analyze_User_Defined_Binary_Op (N, Op_Id);
908 end if;
910 Op_Id := Homonym (Op_Id);
911 end loop;
912 end if;
914 Operator_Check (N);
915 Check_Function_Writable_Actuals (N);
916 end Analyze_Arithmetic_Op;
918 ------------------
919 -- Analyze_Call --
920 ------------------
922 -- Function, procedure, and entry calls are checked here. The Name in
923 -- the call may be overloaded. The actuals have been analyzed and may
924 -- themselves be overloaded. On exit from this procedure, the node N
925 -- may have zero, one or more interpretations. In the first case an
926 -- error message is produced. In the last case, the node is flagged
927 -- as overloaded and the interpretations are collected in All_Interp.
929 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
930 -- the type-checking is similar to that of other calls.
932 procedure Analyze_Call (N : Node_Id) is
933 Actuals : constant List_Id := Parameter_Associations (N);
934 Loc : constant Source_Ptr := Sloc (N);
935 Nam : Node_Id;
936 X : Interp_Index;
937 It : Interp;
938 Nam_Ent : Entity_Id;
939 Success : Boolean := False;
941 Deref : Boolean := False;
942 -- Flag indicates whether an interpretation of the prefix is a
943 -- parameterless call that returns an access_to_subprogram.
945 procedure Check_Mixed_Parameter_And_Named_Associations;
946 -- Check that parameter and named associations are not mixed. This is
947 -- a restriction in SPARK mode.
949 procedure Check_Writable_Actuals (N : Node_Id);
950 -- If the call has out or in-out parameters then mark its outermost
951 -- enclosing construct as a node on which the writable actuals check
952 -- must be performed.
954 function Name_Denotes_Function return Boolean;
955 -- If the type of the name is an access to subprogram, this may be the
956 -- type of a name, or the return type of the function being called. If
957 -- the name is not an entity then it can denote a protected function.
958 -- Until we distinguish Etype from Return_Type, we must use this routine
959 -- to resolve the meaning of the name in the call.
961 procedure No_Interpretation;
962 -- Output error message when no valid interpretation exists
964 --------------------------------------------------
965 -- Check_Mixed_Parameter_And_Named_Associations --
966 --------------------------------------------------
968 procedure Check_Mixed_Parameter_And_Named_Associations is
969 Actual : Node_Id;
970 Named_Seen : Boolean;
972 begin
973 Named_Seen := False;
975 Actual := First (Actuals);
976 while Present (Actual) loop
977 case Nkind (Actual) is
978 when N_Parameter_Association =>
979 if Named_Seen then
980 Check_SPARK_05_Restriction
981 ("named association cannot follow positional one",
982 Actual);
983 exit;
984 end if;
986 when others =>
987 Named_Seen := True;
988 end case;
990 Next (Actual);
991 end loop;
992 end Check_Mixed_Parameter_And_Named_Associations;
994 ----------------------------
995 -- Check_Writable_Actuals --
996 ----------------------------
998 -- The identification of conflicts in calls to functions with writable
999 -- actuals is performed in the analysis phase of the front end to ensure
1000 -- that it reports exactly the same errors compiling with and without
1001 -- expansion enabled. It is performed in two stages:
1003 -- 1) When a call to a function with out-mode parameters is found,
1004 -- we climb to the outermost enclosing construct that can be
1005 -- evaluated in arbitrary order and we mark it with the flag
1006 -- Check_Actuals.
1008 -- 2) When the analysis of the marked node is complete, we traverse
1009 -- its decorated subtree searching for conflicts (see function
1010 -- Sem_Util.Check_Function_Writable_Actuals).
1012 -- The unique exception to this general rule is for aggregates, since
1013 -- their analysis is performed by the front end in the resolution
1014 -- phase. For aggregates we do not climb to their enclosing construct:
1015 -- we restrict the analysis to the subexpressions initializing the
1016 -- aggregate components.
1018 -- This implies that the analysis of expressions containing aggregates
1019 -- is not complete, since there may be conflicts on writable actuals
1020 -- involving subexpressions of the enclosing logical or arithmetic
1021 -- expressions. However, we cannot wait and perform the analysis when
1022 -- the whole subtree is resolved, since the subtrees may be transformed,
1023 -- thus adding extra complexity and computation cost to identify and
1024 -- report exactly the same errors compiling with and without expansion
1025 -- enabled.
1027 procedure Check_Writable_Actuals (N : Node_Id) is
1028 begin
1029 if Comes_From_Source (N)
1030 and then Present (Get_Subprogram_Entity (N))
1031 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1032 then
1033 -- For procedures and entries there is no need to climb since
1034 -- we only need to check if the actuals of this call invoke
1035 -- functions whose out-mode parameters overlap.
1037 if Nkind (N) /= N_Function_Call then
1038 Set_Check_Actuals (N);
1040 -- For calls to functions we climb to the outermost enclosing
1041 -- construct where the out-mode actuals of this function may
1042 -- introduce conflicts.
1044 else
1045 declare
1046 Outermost : Node_Id;
1047 P : Node_Id := N;
1049 begin
1050 while Present (P) loop
1052 -- For object declarations we can climb to the node from
1053 -- its object definition branch or from its initializing
1054 -- expression. We prefer to mark the child node as the
1055 -- outermost construct to avoid adding further complexity
1056 -- to the routine that will later take care of
1057 -- performing the writable actuals check.
1059 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1060 and then not Nkind_In (P, N_Assignment_Statement,
1061 N_Object_Declaration)
1062 then
1063 Outermost := P;
1064 end if;
1066 -- Avoid climbing more than needed!
1068 exit when Stop_Subtree_Climbing (Nkind (P))
1069 or else (Nkind (P) = N_Range
1070 and then not
1071 Nkind_In (Parent (P), N_In, N_Not_In));
1073 P := Parent (P);
1074 end loop;
1076 Set_Check_Actuals (Outermost);
1077 end;
1078 end if;
1079 end if;
1080 end Check_Writable_Actuals;
1082 ---------------------------
1083 -- Name_Denotes_Function --
1084 ---------------------------
1086 function Name_Denotes_Function return Boolean is
1087 begin
1088 if Is_Entity_Name (Nam) then
1089 return Ekind (Entity (Nam)) = E_Function;
1090 elsif Nkind (Nam) = N_Selected_Component then
1091 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1092 else
1093 return False;
1094 end if;
1095 end Name_Denotes_Function;
1097 -----------------------
1098 -- No_Interpretation --
1099 -----------------------
1101 procedure No_Interpretation is
1102 L : constant Boolean := Is_List_Member (N);
1103 K : constant Node_Kind := Nkind (Parent (N));
1105 begin
1106 -- If the node is in a list whose parent is not an expression then it
1107 -- must be an attempted procedure call.
1109 if L and then K not in N_Subexpr then
1110 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1111 Error_Msg_NE
1112 ("must instantiate generic procedure& before call",
1113 Nam, Entity (Nam));
1114 else
1115 Error_Msg_N ("procedure or entry name expected", Nam);
1116 end if;
1118 -- Check for tasking cases where only an entry call will do
1120 elsif not L
1121 and then Nkind_In (K, N_Entry_Call_Alternative,
1122 N_Triggering_Alternative)
1123 then
1124 Error_Msg_N ("entry name expected", Nam);
1126 -- Otherwise give general error message
1128 else
1129 Error_Msg_N ("invalid prefix in call", Nam);
1130 end if;
1131 end No_Interpretation;
1133 -- Start of processing for Analyze_Call
1135 begin
1136 if Restriction_Check_Required (SPARK_05) then
1137 Check_Mixed_Parameter_And_Named_Associations;
1138 end if;
1140 -- Initialize the type of the result of the call to the error type,
1141 -- which will be reset if the type is successfully resolved.
1143 Set_Etype (N, Any_Type);
1145 Nam := Name (N);
1147 if not Is_Overloaded (Nam) then
1149 -- Only one interpretation to check
1151 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1152 Nam_Ent := Etype (Nam);
1154 -- If the prefix is an access_to_subprogram, this may be an indirect
1155 -- call. This is the case if the name in the call is not an entity
1156 -- name, or if it is a function name in the context of a procedure
1157 -- call. In this latter case, we have a call to a parameterless
1158 -- function that returns a pointer_to_procedure which is the entity
1159 -- being called. Finally, F (X) may be a call to a parameterless
1160 -- function that returns a pointer to a function with parameters.
1161 -- Note that if F returns an access-to-subprogram whose designated
1162 -- type is an array, F (X) cannot be interpreted as an indirect call
1163 -- through the result of the call to F.
1165 elsif Is_Access_Type (Etype (Nam))
1166 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1167 and then
1168 (not Name_Denotes_Function
1169 or else Nkind (N) = N_Procedure_Call_Statement
1170 or else
1171 (Nkind (Parent (N)) /= N_Explicit_Dereference
1172 and then Is_Entity_Name (Nam)
1173 and then No (First_Formal (Entity (Nam)))
1174 and then not
1175 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1176 and then Present (Actuals)))
1177 then
1178 Nam_Ent := Designated_Type (Etype (Nam));
1179 Insert_Explicit_Dereference (Nam);
1181 -- Selected component case. Simple entry or protected operation,
1182 -- where the entry name is given by the selector name.
1184 elsif Nkind (Nam) = N_Selected_Component then
1185 Nam_Ent := Entity (Selector_Name (Nam));
1187 if not Ekind_In (Nam_Ent, E_Entry,
1188 E_Entry_Family,
1189 E_Function,
1190 E_Procedure)
1191 then
1192 Error_Msg_N ("name in call is not a callable entity", Nam);
1193 Set_Etype (N, Any_Type);
1194 return;
1195 end if;
1197 -- If the name is an Indexed component, it can be a call to a member
1198 -- of an entry family. The prefix must be a selected component whose
1199 -- selector is the entry. Analyze_Procedure_Call normalizes several
1200 -- kinds of call into this form.
1202 elsif Nkind (Nam) = N_Indexed_Component then
1203 if Nkind (Prefix (Nam)) = N_Selected_Component then
1204 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1205 else
1206 Error_Msg_N ("name in call is not a callable entity", Nam);
1207 Set_Etype (N, Any_Type);
1208 return;
1209 end if;
1211 elsif not Is_Entity_Name (Nam) then
1212 Error_Msg_N ("name in call is not a callable entity", Nam);
1213 Set_Etype (N, Any_Type);
1214 return;
1216 else
1217 Nam_Ent := Entity (Nam);
1219 -- If not overloadable, this may be a generalized indexing
1220 -- operation with named associations. Rewrite again as an
1221 -- indexed component and analyze as container indexing.
1223 if not Is_Overloadable (Nam_Ent) then
1224 if Present
1225 (Find_Value_Of_Aspect
1226 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1227 then
1228 Replace (N,
1229 Make_Indexed_Component (Sloc (N),
1230 Prefix => Nam,
1231 Expressions => Parameter_Associations (N)));
1233 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1234 return;
1235 else
1236 No_Interpretation;
1237 end if;
1239 else
1240 No_Interpretation;
1241 end if;
1243 return;
1244 end if;
1245 end if;
1247 -- Operations generated for RACW stub types are called only through
1248 -- dispatching, and can never be the static interpretation of a call.
1250 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1251 No_Interpretation;
1252 return;
1253 end if;
1255 Analyze_One_Call (N, Nam_Ent, True, Success);
1257 -- If this is an indirect call, the return type of the access_to
1258 -- subprogram may be an incomplete type. At the point of the call,
1259 -- use the full type if available, and at the same time update the
1260 -- return type of the access_to_subprogram.
1262 if Success
1263 and then Nkind (Nam) = N_Explicit_Dereference
1264 and then Ekind (Etype (N)) = E_Incomplete_Type
1265 and then Present (Full_View (Etype (N)))
1266 then
1267 Set_Etype (N, Full_View (Etype (N)));
1268 Set_Etype (Nam_Ent, Etype (N));
1269 end if;
1271 -- Overloaded call
1273 else
1274 -- An overloaded selected component must denote overloaded operations
1275 -- of a concurrent type. The interpretations are attached to the
1276 -- simple name of those operations.
1278 if Nkind (Nam) = N_Selected_Component then
1279 Nam := Selector_Name (Nam);
1280 end if;
1282 Get_First_Interp (Nam, X, It);
1283 while Present (It.Nam) loop
1284 Nam_Ent := It.Nam;
1285 Deref := False;
1287 -- Name may be call that returns an access to subprogram, or more
1288 -- generally an overloaded expression one of whose interpretations
1289 -- yields an access to subprogram. If the name is an entity, we do
1290 -- not dereference, because the node is a call that returns the
1291 -- access type: note difference between f(x), where the call may
1292 -- return an access subprogram type, and f(x)(y), where the type
1293 -- returned by the call to f is implicitly dereferenced to analyze
1294 -- the outer call.
1296 if Is_Access_Type (Nam_Ent) then
1297 Nam_Ent := Designated_Type (Nam_Ent);
1299 elsif Is_Access_Type (Etype (Nam_Ent))
1300 and then
1301 (not Is_Entity_Name (Nam)
1302 or else Nkind (N) = N_Procedure_Call_Statement)
1303 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1304 = E_Subprogram_Type
1305 then
1306 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1308 if Is_Entity_Name (Nam) then
1309 Deref := True;
1310 end if;
1311 end if;
1313 -- If the call has been rewritten from a prefixed call, the first
1314 -- parameter has been analyzed, but may need a subsequent
1315 -- dereference, so skip its analysis now.
1317 if N /= Original_Node (N)
1318 and then Nkind (Original_Node (N)) = Nkind (N)
1319 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1320 and then Present (Parameter_Associations (N))
1321 and then Present (Etype (First (Parameter_Associations (N))))
1322 then
1323 Analyze_One_Call
1324 (N, Nam_Ent, False, Success, Skip_First => True);
1325 else
1326 Analyze_One_Call (N, Nam_Ent, False, Success);
1327 end if;
1329 -- If the interpretation succeeds, mark the proper type of the
1330 -- prefix (any valid candidate will do). If not, remove the
1331 -- candidate interpretation. If this is a parameterless call
1332 -- on an anonymous access to subprogram, X is a variable with
1333 -- an access discriminant D, the entity in the interpretation is
1334 -- D, so rewrite X as X.D.all.
1336 if Success then
1337 if Deref
1338 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1339 then
1340 if Ekind (It.Nam) = E_Discriminant
1341 and then Has_Implicit_Dereference (It.Nam)
1342 then
1343 Rewrite (Name (N),
1344 Make_Explicit_Dereference (Loc,
1345 Prefix =>
1346 Make_Selected_Component (Loc,
1347 Prefix =>
1348 New_Occurrence_Of (Entity (Nam), Loc),
1349 Selector_Name =>
1350 New_Occurrence_Of (It.Nam, Loc))));
1352 Analyze (N);
1353 return;
1355 else
1356 Set_Entity (Nam, It.Nam);
1357 Insert_Explicit_Dereference (Nam);
1358 Set_Etype (Nam, Nam_Ent);
1359 end if;
1361 else
1362 Set_Etype (Nam, It.Typ);
1363 end if;
1365 elsif Nkind_In (Name (N), N_Function_Call, N_Selected_Component)
1366 then
1367 Remove_Interp (X);
1368 end if;
1370 Get_Next_Interp (X, It);
1371 end loop;
1373 -- If the name is the result of a function call, it can only be a
1374 -- call to a function returning an access to subprogram. Insert
1375 -- explicit dereference.
1377 if Nkind (Nam) = N_Function_Call then
1378 Insert_Explicit_Dereference (Nam);
1379 end if;
1381 if Etype (N) = Any_Type then
1383 -- None of the interpretations is compatible with the actuals
1385 Diagnose_Call (N, Nam);
1387 -- Special checks for uninstantiated put routines
1389 if Nkind (N) = N_Procedure_Call_Statement
1390 and then Is_Entity_Name (Nam)
1391 and then Chars (Nam) = Name_Put
1392 and then List_Length (Actuals) = 1
1393 then
1394 declare
1395 Arg : constant Node_Id := First (Actuals);
1396 Typ : Entity_Id;
1398 begin
1399 if Nkind (Arg) = N_Parameter_Association then
1400 Typ := Etype (Explicit_Actual_Parameter (Arg));
1401 else
1402 Typ := Etype (Arg);
1403 end if;
1405 if Is_Signed_Integer_Type (Typ) then
1406 Error_Msg_N
1407 ("possible missing instantiation of "
1408 & "'Text_'I'O.'Integer_'I'O!", Nam);
1410 elsif Is_Modular_Integer_Type (Typ) then
1411 Error_Msg_N
1412 ("possible missing instantiation of "
1413 & "'Text_'I'O.'Modular_'I'O!", Nam);
1415 elsif Is_Floating_Point_Type (Typ) then
1416 Error_Msg_N
1417 ("possible missing instantiation of "
1418 & "'Text_'I'O.'Float_'I'O!", Nam);
1420 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1421 Error_Msg_N
1422 ("possible missing instantiation of "
1423 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1425 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1426 Error_Msg_N
1427 ("possible missing instantiation of "
1428 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1430 elsif Is_Enumeration_Type (Typ) then
1431 Error_Msg_N
1432 ("possible missing instantiation of "
1433 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1434 end if;
1435 end;
1436 end if;
1438 elsif not Is_Overloaded (N)
1439 and then Is_Entity_Name (Nam)
1440 then
1441 -- Resolution yields a single interpretation. Verify that the
1442 -- reference has capitalization consistent with the declaration.
1444 Set_Entity_With_Checks (Nam, Entity (Nam));
1445 Generate_Reference (Entity (Nam), Nam);
1447 Set_Etype (Nam, Etype (Entity (Nam)));
1448 else
1449 Remove_Abstract_Operations (N);
1450 end if;
1452 End_Interp_List;
1453 end if;
1455 if Ada_Version >= Ada_2012 then
1457 -- Check if the call contains a function with writable actuals
1459 Check_Writable_Actuals (N);
1461 -- If found and the outermost construct that can be evaluated in
1462 -- an arbitrary order is precisely this call, then check all its
1463 -- actuals.
1465 Check_Function_Writable_Actuals (N);
1467 -- The return type of the function may be incomplete. This can be
1468 -- the case if the type is a generic formal, or a limited view. It
1469 -- can also happen when the function declaration appears before the
1470 -- full view of the type (which is legal in Ada 2012) and the call
1471 -- appears in a different unit, in which case the incomplete view
1472 -- must be replaced with the full view (or the nonlimited view)
1473 -- to prevent subsequent type errors. Note that the usual install/
1474 -- removal of limited_with clauses is not sufficient to handle this
1475 -- case, because the limited view may have been captured in another
1476 -- compilation unit that defines the current function.
1478 if Is_Incomplete_Type (Etype (N)) then
1479 if Present (Full_View (Etype (N))) then
1480 if Is_Entity_Name (Nam) then
1481 Set_Etype (Nam, Full_View (Etype (N)));
1482 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1483 end if;
1485 Set_Etype (N, Full_View (Etype (N)));
1487 elsif From_Limited_With (Etype (N))
1488 and then Present (Non_Limited_View (Etype (N)))
1489 then
1490 Set_Etype (N, Non_Limited_View (Etype (N)));
1491 end if;
1492 end if;
1493 end if;
1494 end Analyze_Call;
1496 -----------------------------
1497 -- Analyze_Case_Expression --
1498 -----------------------------
1500 procedure Analyze_Case_Expression (N : Node_Id) is
1501 procedure Non_Static_Choice_Error (Choice : Node_Id);
1502 -- Error routine invoked by the generic instantiation below when
1503 -- the case expression has a non static choice.
1505 package Case_Choices_Analysis is new
1506 Generic_Analyze_Choices
1507 (Process_Associated_Node => No_OP);
1508 use Case_Choices_Analysis;
1510 package Case_Choices_Checking is new
1511 Generic_Check_Choices
1512 (Process_Empty_Choice => No_OP,
1513 Process_Non_Static_Choice => Non_Static_Choice_Error,
1514 Process_Associated_Node => No_OP);
1515 use Case_Choices_Checking;
1517 -----------------------------
1518 -- Non_Static_Choice_Error --
1519 -----------------------------
1521 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1522 begin
1523 Flag_Non_Static_Expr
1524 ("choice given in case expression is not static!", Choice);
1525 end Non_Static_Choice_Error;
1527 -- Local variables
1529 Expr : constant Node_Id := Expression (N);
1530 Alt : Node_Id;
1531 Exp_Type : Entity_Id;
1532 Exp_Btype : Entity_Id;
1534 FirstX : Node_Id := Empty;
1535 -- First expression in the case for which there is some type information
1536 -- available, i.e. it is not Any_Type, which can happen because of some
1537 -- error, or from the use of e.g. raise Constraint_Error.
1539 Others_Present : Boolean;
1540 -- Indicates if Others was present
1542 Wrong_Alt : Node_Id := Empty;
1543 -- For error reporting
1545 -- Start of processing for Analyze_Case_Expression
1547 begin
1548 if Comes_From_Source (N) then
1549 Check_Compiler_Unit ("case expression", N);
1550 end if;
1552 Analyze_And_Resolve (Expr, Any_Discrete);
1553 Check_Unset_Reference (Expr);
1554 Exp_Type := Etype (Expr);
1555 Exp_Btype := Base_Type (Exp_Type);
1557 Alt := First (Alternatives (N));
1558 while Present (Alt) loop
1559 if Error_Posted (Expression (Alt)) then
1560 return;
1561 end if;
1563 Analyze (Expression (Alt));
1565 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1566 FirstX := Expression (Alt);
1567 end if;
1569 Next (Alt);
1570 end loop;
1572 -- Get our initial type from the first expression for which we got some
1573 -- useful type information from the expression.
1575 if No (FirstX) then
1576 return;
1577 end if;
1579 if not Is_Overloaded (FirstX) then
1580 Set_Etype (N, Etype (FirstX));
1582 else
1583 declare
1584 I : Interp_Index;
1585 It : Interp;
1587 begin
1588 Set_Etype (N, Any_Type);
1590 Get_First_Interp (FirstX, I, It);
1591 while Present (It.Nam) loop
1593 -- For each interpretation of the first expression, we only
1594 -- add the interpretation if every other expression in the
1595 -- case expression alternatives has a compatible type.
1597 Alt := Next (First (Alternatives (N)));
1598 while Present (Alt) loop
1599 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1600 Next (Alt);
1601 end loop;
1603 if No (Alt) then
1604 Add_One_Interp (N, It.Typ, It.Typ);
1605 else
1606 Wrong_Alt := Alt;
1607 end if;
1609 Get_Next_Interp (I, It);
1610 end loop;
1611 end;
1612 end if;
1614 Exp_Btype := Base_Type (Exp_Type);
1616 -- The expression must be of a discrete type which must be determinable
1617 -- independently of the context in which the expression occurs, but
1618 -- using the fact that the expression must be of a discrete type.
1619 -- Moreover, the type this expression must not be a character literal
1620 -- (which is always ambiguous).
1622 -- If error already reported by Resolve, nothing more to do
1624 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1625 return;
1627 -- Special casee message for character literal
1629 elsif Exp_Btype = Any_Character then
1630 Error_Msg_N
1631 ("character literal as case expression is ambiguous", Expr);
1632 return;
1633 end if;
1635 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1636 Error_Msg_N
1637 ("type incompatible with that of previous alternatives",
1638 Expression (Wrong_Alt));
1639 return;
1640 end if;
1642 -- If the case expression is a formal object of mode in out, then
1643 -- treat it as having a nonstatic subtype by forcing use of the base
1644 -- type (which has to get passed to Check_Case_Choices below). Also
1645 -- use base type when the case expression is parenthesized.
1647 if Paren_Count (Expr) > 0
1648 or else (Is_Entity_Name (Expr)
1649 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1650 then
1651 Exp_Type := Exp_Btype;
1652 end if;
1654 -- The case expression alternatives cover the range of a static subtype
1655 -- subject to aspect Static_Predicate. Do not check the choices when the
1656 -- case expression has not been fully analyzed yet because this may lead
1657 -- to bogus errors.
1659 if Is_OK_Static_Subtype (Exp_Type)
1660 and then Has_Static_Predicate_Aspect (Exp_Type)
1661 and then In_Spec_Expression
1662 then
1663 null;
1665 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1667 else
1668 Analyze_Choices (Alternatives (N), Exp_Type);
1669 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1670 end if;
1672 if Exp_Type = Universal_Integer and then not Others_Present then
1673 Error_Msg_N
1674 ("case on universal integer requires OTHERS choice", Expr);
1675 end if;
1676 end Analyze_Case_Expression;
1678 ---------------------------
1679 -- Analyze_Comparison_Op --
1680 ---------------------------
1682 procedure Analyze_Comparison_Op (N : Node_Id) is
1683 L : constant Node_Id := Left_Opnd (N);
1684 R : constant Node_Id := Right_Opnd (N);
1685 Op_Id : Entity_Id := Entity (N);
1687 begin
1688 Set_Etype (N, Any_Type);
1689 Candidate_Type := Empty;
1691 Analyze_Expression (L);
1692 Analyze_Expression (R);
1694 if Present (Op_Id) then
1695 if Ekind (Op_Id) = E_Operator then
1696 Find_Comparison_Types (L, R, Op_Id, N);
1697 else
1698 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1699 end if;
1701 if Is_Overloaded (L) then
1702 Set_Etype (L, Intersect_Types (L, R));
1703 end if;
1705 else
1706 Op_Id := Get_Name_Entity_Id (Chars (N));
1707 while Present (Op_Id) loop
1708 if Ekind (Op_Id) = E_Operator then
1709 Find_Comparison_Types (L, R, Op_Id, N);
1710 else
1711 Analyze_User_Defined_Binary_Op (N, Op_Id);
1712 end if;
1714 Op_Id := Homonym (Op_Id);
1715 end loop;
1716 end if;
1718 Operator_Check (N);
1719 Check_Function_Writable_Actuals (N);
1720 end Analyze_Comparison_Op;
1722 ---------------------------
1723 -- Analyze_Concatenation --
1724 ---------------------------
1726 procedure Analyze_Concatenation (N : Node_Id) is
1728 -- We wish to avoid deep recursion, because concatenations are often
1729 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1730 -- operands nonrecursively until we find something that is not a
1731 -- concatenation (A in this case), or has already been analyzed. We
1732 -- analyze that, and then walk back up the tree following Parent
1733 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1734 -- work at each level. The Parent pointers allow us to avoid recursion,
1735 -- and thus avoid running out of memory.
1737 NN : Node_Id := N;
1738 L : Node_Id;
1740 begin
1741 Candidate_Type := Empty;
1743 -- The following code is equivalent to:
1745 -- Set_Etype (N, Any_Type);
1746 -- Analyze_Expression (Left_Opnd (N));
1747 -- Analyze_Concatenation_Rest (N);
1749 -- where the Analyze_Expression call recurses back here if the left
1750 -- operand is a concatenation.
1752 -- Walk down left operands
1754 loop
1755 Set_Etype (NN, Any_Type);
1756 L := Left_Opnd (NN);
1757 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1758 NN := L;
1759 end loop;
1761 -- Now (given the above example) NN is A&B and L is A
1763 -- First analyze L ...
1765 Analyze_Expression (L);
1767 -- ... then walk NN back up until we reach N (where we started), calling
1768 -- Analyze_Concatenation_Rest along the way.
1770 loop
1771 Analyze_Concatenation_Rest (NN);
1772 exit when NN = N;
1773 NN := Parent (NN);
1774 end loop;
1775 end Analyze_Concatenation;
1777 --------------------------------
1778 -- Analyze_Concatenation_Rest --
1779 --------------------------------
1781 -- If the only one-dimensional array type in scope is String,
1782 -- this is the resulting type of the operation. Otherwise there
1783 -- will be a concatenation operation defined for each user-defined
1784 -- one-dimensional array.
1786 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1787 L : constant Node_Id := Left_Opnd (N);
1788 R : constant Node_Id := Right_Opnd (N);
1789 Op_Id : Entity_Id := Entity (N);
1790 LT : Entity_Id;
1791 RT : Entity_Id;
1793 begin
1794 Analyze_Expression (R);
1796 -- If the entity is present, the node appears in an instance, and
1797 -- denotes a predefined concatenation operation. The resulting type is
1798 -- obtained from the arguments when possible. If the arguments are
1799 -- aggregates, the array type and the concatenation type must be
1800 -- visible.
1802 if Present (Op_Id) then
1803 if Ekind (Op_Id) = E_Operator then
1804 LT := Base_Type (Etype (L));
1805 RT := Base_Type (Etype (R));
1807 if Is_Array_Type (LT)
1808 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1809 then
1810 Add_One_Interp (N, Op_Id, LT);
1812 elsif Is_Array_Type (RT)
1813 and then LT = Base_Type (Component_Type (RT))
1814 then
1815 Add_One_Interp (N, Op_Id, RT);
1817 -- If one operand is a string type or a user-defined array type,
1818 -- and the other is a literal, result is of the specific type.
1820 elsif
1821 (Root_Type (LT) = Standard_String
1822 or else Scope (LT) /= Standard_Standard)
1823 and then Etype (R) = Any_String
1824 then
1825 Add_One_Interp (N, Op_Id, LT);
1827 elsif
1828 (Root_Type (RT) = Standard_String
1829 or else Scope (RT) /= Standard_Standard)
1830 and then Etype (L) = Any_String
1831 then
1832 Add_One_Interp (N, Op_Id, RT);
1834 elsif not Is_Generic_Type (Etype (Op_Id)) then
1835 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1837 else
1838 -- Type and its operations must be visible
1840 Set_Entity (N, Empty);
1841 Analyze_Concatenation (N);
1842 end if;
1844 else
1845 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1846 end if;
1848 else
1849 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1850 while Present (Op_Id) loop
1851 if Ekind (Op_Id) = E_Operator then
1853 -- Do not consider operators declared in dead code, they can
1854 -- not be part of the resolution.
1856 if Is_Eliminated (Op_Id) then
1857 null;
1858 else
1859 Find_Concatenation_Types (L, R, Op_Id, N);
1860 end if;
1862 else
1863 Analyze_User_Defined_Binary_Op (N, Op_Id);
1864 end if;
1866 Op_Id := Homonym (Op_Id);
1867 end loop;
1868 end if;
1870 Operator_Check (N);
1871 end Analyze_Concatenation_Rest;
1873 -------------------------
1874 -- Analyze_Equality_Op --
1875 -------------------------
1877 procedure Analyze_Equality_Op (N : Node_Id) is
1878 Loc : constant Source_Ptr := Sloc (N);
1879 L : constant Node_Id := Left_Opnd (N);
1880 R : constant Node_Id := Right_Opnd (N);
1881 Op_Id : Entity_Id;
1883 begin
1884 Set_Etype (N, Any_Type);
1885 Candidate_Type := Empty;
1887 Analyze_Expression (L);
1888 Analyze_Expression (R);
1890 -- If the entity is set, the node is a generic instance with a non-local
1891 -- reference to the predefined operator or to a user-defined function.
1892 -- It can also be an inequality that is expanded into the negation of a
1893 -- call to a user-defined equality operator.
1895 -- For the predefined case, the result is Boolean, regardless of the
1896 -- type of the operands. The operands may even be limited, if they are
1897 -- generic actuals. If they are overloaded, label the left argument with
1898 -- the common type that must be present, or with the type of the formal
1899 -- of the user-defined function.
1901 if Present (Entity (N)) then
1902 Op_Id := Entity (N);
1904 if Ekind (Op_Id) = E_Operator then
1905 Add_One_Interp (N, Op_Id, Standard_Boolean);
1906 else
1907 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1908 end if;
1910 if Is_Overloaded (L) then
1911 if Ekind (Op_Id) = E_Operator then
1912 Set_Etype (L, Intersect_Types (L, R));
1913 else
1914 Set_Etype (L, Etype (First_Formal (Op_Id)));
1915 end if;
1916 end if;
1918 else
1919 Op_Id := Get_Name_Entity_Id (Chars (N));
1920 while Present (Op_Id) loop
1921 if Ekind (Op_Id) = E_Operator then
1922 Find_Equality_Types (L, R, Op_Id, N);
1923 else
1924 Analyze_User_Defined_Binary_Op (N, Op_Id);
1925 end if;
1927 Op_Id := Homonym (Op_Id);
1928 end loop;
1929 end if;
1931 -- If there was no match, and the operator is inequality, this may be
1932 -- a case where inequality has not been made explicit, as for tagged
1933 -- types. Analyze the node as the negation of an equality operation.
1934 -- This cannot be done earlier, because before analysis we cannot rule
1935 -- out the presence of an explicit inequality.
1937 if Etype (N) = Any_Type
1938 and then Nkind (N) = N_Op_Ne
1939 then
1940 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1941 while Present (Op_Id) loop
1942 if Ekind (Op_Id) = E_Operator then
1943 Find_Equality_Types (L, R, Op_Id, N);
1944 else
1945 Analyze_User_Defined_Binary_Op (N, Op_Id);
1946 end if;
1948 Op_Id := Homonym (Op_Id);
1949 end loop;
1951 if Etype (N) /= Any_Type then
1952 Op_Id := Entity (N);
1954 Rewrite (N,
1955 Make_Op_Not (Loc,
1956 Right_Opnd =>
1957 Make_Op_Eq (Loc,
1958 Left_Opnd => Left_Opnd (N),
1959 Right_Opnd => Right_Opnd (N))));
1961 Set_Entity (Right_Opnd (N), Op_Id);
1962 Analyze (N);
1963 end if;
1964 end if;
1966 Operator_Check (N);
1967 Check_Function_Writable_Actuals (N);
1968 end Analyze_Equality_Op;
1970 ----------------------------------
1971 -- Analyze_Explicit_Dereference --
1972 ----------------------------------
1974 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1975 Loc : constant Source_Ptr := Sloc (N);
1976 P : constant Node_Id := Prefix (N);
1977 T : Entity_Id;
1978 I : Interp_Index;
1979 It : Interp;
1980 New_N : Node_Id;
1982 function Is_Function_Type return Boolean;
1983 -- Check whether node may be interpreted as an implicit function call
1985 ----------------------
1986 -- Is_Function_Type --
1987 ----------------------
1989 function Is_Function_Type return Boolean is
1990 I : Interp_Index;
1991 It : Interp;
1993 begin
1994 if not Is_Overloaded (N) then
1995 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1996 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1998 else
1999 Get_First_Interp (N, I, It);
2000 while Present (It.Nam) loop
2001 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2002 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2003 then
2004 return False;
2005 end if;
2007 Get_Next_Interp (I, It);
2008 end loop;
2010 return True;
2011 end if;
2012 end Is_Function_Type;
2014 -- Start of processing for Analyze_Explicit_Dereference
2016 begin
2017 -- If source node, check SPARK restriction. We guard this with the
2018 -- source node check, because ???
2020 if Comes_From_Source (N) then
2021 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
2022 end if;
2024 -- In formal verification mode, keep track of all reads and writes
2025 -- through explicit dereferences.
2027 if GNATprove_Mode then
2028 SPARK_Specific.Generate_Dereference (N);
2029 end if;
2031 Analyze (P);
2032 Set_Etype (N, Any_Type);
2034 -- Test for remote access to subprogram type, and if so return
2035 -- after rewriting the original tree.
2037 if Remote_AST_E_Dereference (P) then
2038 return;
2039 end if;
2041 -- Normal processing for other than remote access to subprogram type
2043 if not Is_Overloaded (P) then
2044 if Is_Access_Type (Etype (P)) then
2046 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
2047 -- avoid other problems caused by the Private_Subtype and it is
2048 -- safe to go to the Base_Type because this is the same as
2049 -- converting the access value to its Base_Type.
2051 declare
2052 DT : Entity_Id := Designated_Type (Etype (P));
2054 begin
2055 if Ekind (DT) = E_Private_Subtype
2056 and then Is_For_Access_Subtype (DT)
2057 then
2058 DT := Base_Type (DT);
2059 end if;
2061 -- An explicit dereference is a legal occurrence of an
2062 -- incomplete type imported through a limited_with clause, if
2063 -- the full view is visible, or if we are within an instance
2064 -- body, where the enclosing body has a regular with_clause
2065 -- on the unit.
2067 if From_Limited_With (DT)
2068 and then not From_Limited_With (Scope (DT))
2069 and then
2070 (Is_Immediately_Visible (Scope (DT))
2071 or else
2072 (Is_Child_Unit (Scope (DT))
2073 and then Is_Visible_Lib_Unit (Scope (DT)))
2074 or else In_Instance_Body)
2075 then
2076 Set_Etype (N, Available_View (DT));
2078 else
2079 Set_Etype (N, DT);
2080 end if;
2081 end;
2083 elsif Etype (P) /= Any_Type then
2084 Error_Msg_N ("prefix of dereference must be an access type", N);
2085 return;
2086 end if;
2088 else
2089 Get_First_Interp (P, I, It);
2090 while Present (It.Nam) loop
2091 T := It.Typ;
2093 if Is_Access_Type (T) then
2094 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2095 end if;
2097 Get_Next_Interp (I, It);
2098 end loop;
2100 -- Error if no interpretation of the prefix has an access type
2102 if Etype (N) = Any_Type then
2103 Error_Msg_N
2104 ("access type required in prefix of explicit dereference", P);
2105 Set_Etype (N, Any_Type);
2106 return;
2107 end if;
2108 end if;
2110 if Is_Function_Type
2111 and then Nkind (Parent (N)) /= N_Indexed_Component
2113 and then (Nkind (Parent (N)) /= N_Function_Call
2114 or else N /= Name (Parent (N)))
2116 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2117 or else N /= Name (Parent (N)))
2119 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2120 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2121 or else
2122 (Attribute_Name (Parent (N)) /= Name_Address
2123 and then
2124 Attribute_Name (Parent (N)) /= Name_Access))
2125 then
2126 -- Name is a function call with no actuals, in a context that
2127 -- requires deproceduring (including as an actual in an enclosing
2128 -- function or procedure call). There are some pathological cases
2129 -- where the prefix might include functions that return access to
2130 -- subprograms and others that return a regular type. Disambiguation
2131 -- of those has to take place in Resolve.
2133 New_N :=
2134 Make_Function_Call (Loc,
2135 Name => Make_Explicit_Dereference (Loc, P),
2136 Parameter_Associations => New_List);
2138 -- If the prefix is overloaded, remove operations that have formals,
2139 -- we know that this is a parameterless call.
2141 if Is_Overloaded (P) then
2142 Get_First_Interp (P, I, It);
2143 while Present (It.Nam) loop
2144 T := It.Typ;
2146 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2147 Set_Etype (P, T);
2148 else
2149 Remove_Interp (I);
2150 end if;
2152 Get_Next_Interp (I, It);
2153 end loop;
2154 end if;
2156 Rewrite (N, New_N);
2157 Analyze (N);
2159 elsif not Is_Function_Type
2160 and then Is_Overloaded (N)
2161 then
2162 -- The prefix may include access to subprograms and other access
2163 -- types. If the context selects the interpretation that is a
2164 -- function call (not a procedure call) we cannot rewrite the node
2165 -- yet, but we include the result of the call interpretation.
2167 Get_First_Interp (N, I, It);
2168 while Present (It.Nam) loop
2169 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2170 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2171 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2172 then
2173 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2174 end if;
2176 Get_Next_Interp (I, It);
2177 end loop;
2178 end if;
2180 -- A value of remote access-to-class-wide must not be dereferenced
2181 -- (RM E.2.2(16)).
2183 Validate_Remote_Access_To_Class_Wide_Type (N);
2184 end Analyze_Explicit_Dereference;
2186 ------------------------
2187 -- Analyze_Expression --
2188 ------------------------
2190 procedure Analyze_Expression (N : Node_Id) is
2191 begin
2193 -- If the expression is an indexed component that will be rewritten
2194 -- as a container indexing, it has already been analyzed.
2196 if Nkind (N) = N_Indexed_Component
2197 and then Present (Generalized_Indexing (N))
2198 then
2199 null;
2201 else
2202 Analyze (N);
2203 Check_Parameterless_Call (N);
2204 end if;
2205 end Analyze_Expression;
2207 -------------------------------------
2208 -- Analyze_Expression_With_Actions --
2209 -------------------------------------
2211 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2212 A : Node_Id;
2214 begin
2215 A := First (Actions (N));
2216 while Present (A) loop
2217 Analyze (A);
2218 Next (A);
2219 end loop;
2221 Analyze_Expression (Expression (N));
2222 Set_Etype (N, Etype (Expression (N)));
2223 end Analyze_Expression_With_Actions;
2225 ---------------------------
2226 -- Analyze_If_Expression --
2227 ---------------------------
2229 procedure Analyze_If_Expression (N : Node_Id) is
2230 Condition : constant Node_Id := First (Expressions (N));
2231 Then_Expr : Node_Id;
2232 Else_Expr : Node_Id;
2234 begin
2235 -- Defend against error of missing expressions from previous error
2237 if No (Condition) then
2238 Check_Error_Detected;
2239 return;
2240 end if;
2242 Then_Expr := Next (Condition);
2244 if No (Then_Expr) then
2245 Check_Error_Detected;
2246 return;
2247 end if;
2249 Else_Expr := Next (Then_Expr);
2251 if Comes_From_Source (N) then
2252 Check_SPARK_05_Restriction ("if expression is not allowed", N);
2253 end if;
2255 if Comes_From_Source (N) then
2256 Check_Compiler_Unit ("if expression", N);
2257 end if;
2259 -- Analyze and resolve the condition. We need to resolve this now so
2260 -- that it gets folded to True/False if possible, before we analyze
2261 -- the THEN/ELSE branches, because when analyzing these branches, we
2262 -- may call Is_Statically_Unevaluated, which expects the condition of
2263 -- an enclosing IF to have been analyze/resolved/evaluated.
2265 Analyze_Expression (Condition);
2266 Resolve (Condition, Any_Boolean);
2268 -- Analyze THEN expression and (if present) ELSE expression. For those
2269 -- we delay resolution in the normal manner, because of overloading etc.
2271 Analyze_Expression (Then_Expr);
2273 if Present (Else_Expr) then
2274 Analyze_Expression (Else_Expr);
2275 end if;
2277 -- If then expression not overloaded, then that decides the type
2279 if not Is_Overloaded (Then_Expr) then
2280 Set_Etype (N, Etype (Then_Expr));
2282 -- Case where then expression is overloaded
2284 else
2285 declare
2286 I : Interp_Index;
2287 It : Interp;
2289 begin
2290 Set_Etype (N, Any_Type);
2292 -- Loop through interpretations of Then_Expr
2294 Get_First_Interp (Then_Expr, I, It);
2295 while Present (It.Nam) loop
2297 -- Add possible interpretation of Then_Expr if no Else_Expr, or
2298 -- Else_Expr is present and has a compatible type.
2300 if No (Else_Expr)
2301 or else Has_Compatible_Type (Else_Expr, It.Typ)
2302 then
2303 Add_One_Interp (N, It.Typ, It.Typ);
2304 end if;
2306 Get_Next_Interp (I, It);
2307 end loop;
2309 -- If no valid interpretation has been found, then the type of the
2310 -- ELSE expression does not match any interpretation of the THEN
2311 -- expression.
2313 if Etype (N) = Any_Type then
2314 Error_Msg_N
2315 ("type incompatible with that of `THEN` expression",
2316 Else_Expr);
2317 return;
2318 end if;
2319 end;
2320 end if;
2321 end Analyze_If_Expression;
2323 ------------------------------------
2324 -- Analyze_Indexed_Component_Form --
2325 ------------------------------------
2327 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2328 P : constant Node_Id := Prefix (N);
2329 Exprs : constant List_Id := Expressions (N);
2330 Exp : Node_Id;
2331 P_T : Entity_Id;
2332 E : Node_Id;
2333 U_N : Entity_Id;
2335 procedure Process_Function_Call;
2336 -- Prefix in indexed component form is an overloadable entity, so the
2337 -- node is a function call. Reformat it as such.
2339 procedure Process_Indexed_Component;
2340 -- Prefix in indexed component form is actually an indexed component.
2341 -- This routine processes it, knowing that the prefix is already
2342 -- resolved.
2344 procedure Process_Indexed_Component_Or_Slice;
2345 -- An indexed component with a single index may designate a slice if
2346 -- the index is a subtype mark. This routine disambiguates these two
2347 -- cases by resolving the prefix to see if it is a subtype mark.
2349 procedure Process_Overloaded_Indexed_Component;
2350 -- If the prefix of an indexed component is overloaded, the proper
2351 -- interpretation is selected by the index types and the context.
2353 ---------------------------
2354 -- Process_Function_Call --
2355 ---------------------------
2357 procedure Process_Function_Call is
2358 Loc : constant Source_Ptr := Sloc (N);
2359 Actual : Node_Id;
2361 begin
2362 Change_Node (N, N_Function_Call);
2363 Set_Name (N, P);
2364 Set_Parameter_Associations (N, Exprs);
2366 -- Analyze actuals prior to analyzing the call itself
2368 Actual := First (Parameter_Associations (N));
2369 while Present (Actual) loop
2370 Analyze (Actual);
2371 Check_Parameterless_Call (Actual);
2373 -- Move to next actual. Note that we use Next, not Next_Actual
2374 -- here. The reason for this is a bit subtle. If a function call
2375 -- includes named associations, the parser recognizes the node
2376 -- as a call, and it is analyzed as such. If all associations are
2377 -- positional, the parser builds an indexed_component node, and
2378 -- it is only after analysis of the prefix that the construct
2379 -- is recognized as a call, in which case Process_Function_Call
2380 -- rewrites the node and analyzes the actuals. If the list of
2381 -- actuals is malformed, the parser may leave the node as an
2382 -- indexed component (despite the presence of named associations).
2383 -- The iterator Next_Actual is equivalent to Next if the list is
2384 -- positional, but follows the normalized chain of actuals when
2385 -- named associations are present. In this case normalization has
2386 -- not taken place, and actuals remain unanalyzed, which leads to
2387 -- subsequent crashes or loops if there is an attempt to continue
2388 -- analysis of the program.
2390 -- IF there is a single actual and it is a type name, the node
2391 -- can only be interpreted as a slice of a parameterless call.
2392 -- Rebuild the node as such and analyze.
2394 if No (Next (Actual))
2395 and then Is_Entity_Name (Actual)
2396 and then Is_Type (Entity (Actual))
2397 and then Is_Discrete_Type (Entity (Actual))
2398 then
2399 Replace (N,
2400 Make_Slice (Loc,
2401 Prefix => P,
2402 Discrete_Range =>
2403 New_Occurrence_Of (Entity (Actual), Loc)));
2404 Analyze (N);
2405 return;
2407 else
2408 Next (Actual);
2409 end if;
2410 end loop;
2412 Analyze_Call (N);
2413 end Process_Function_Call;
2415 -------------------------------
2416 -- Process_Indexed_Component --
2417 -------------------------------
2419 procedure Process_Indexed_Component is
2420 Exp : Node_Id;
2421 Array_Type : Entity_Id;
2422 Index : Node_Id;
2423 Pent : Entity_Id := Empty;
2425 begin
2426 Exp := First (Exprs);
2428 if Is_Overloaded (P) then
2429 Process_Overloaded_Indexed_Component;
2431 else
2432 Array_Type := Etype (P);
2434 if Is_Entity_Name (P) then
2435 Pent := Entity (P);
2436 elsif Nkind (P) = N_Selected_Component
2437 and then Is_Entity_Name (Selector_Name (P))
2438 then
2439 Pent := Entity (Selector_Name (P));
2440 end if;
2442 -- Prefix must be appropriate for an array type, taking into
2443 -- account a possible implicit dereference.
2445 if Is_Access_Type (Array_Type) then
2446 Error_Msg_NW
2447 (Warn_On_Dereference, "?d?implicit dereference", N);
2448 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2449 end if;
2451 if Is_Array_Type (Array_Type) then
2453 -- In order to correctly access First_Index component later,
2454 -- replace string literal subtype by its parent type.
2456 if Ekind (Array_Type) = E_String_Literal_Subtype then
2457 Array_Type := Etype (Array_Type);
2458 end if;
2460 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2461 Analyze (Exp);
2462 Set_Etype (N, Any_Type);
2464 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
2465 Error_Msg_N ("invalid index type in entry name", N);
2467 elsif Present (Next (Exp)) then
2468 Error_Msg_N ("too many subscripts in entry reference", N);
2470 else
2471 Set_Etype (N, Etype (P));
2472 end if;
2474 return;
2476 elsif Is_Record_Type (Array_Type)
2477 and then Remote_AST_I_Dereference (P)
2478 then
2479 return;
2481 elsif Try_Container_Indexing (N, P, Exprs) then
2482 return;
2484 elsif Array_Type = Any_Type then
2485 Set_Etype (N, Any_Type);
2487 -- In most cases the analysis of the prefix will have emitted
2488 -- an error already, but if the prefix may be interpreted as a
2489 -- call in prefixed notation, the report is left to the caller.
2490 -- To prevent cascaded errors, report only if no previous ones.
2492 if Serious_Errors_Detected = 0 then
2493 Error_Msg_N ("invalid prefix in indexed component", P);
2495 if Nkind (P) = N_Expanded_Name then
2496 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2497 end if;
2498 end if;
2500 return;
2502 -- Here we definitely have a bad indexing
2504 else
2505 if Nkind (Parent (N)) = N_Requeue_Statement
2506 and then Present (Pent) and then Ekind (Pent) = E_Entry
2507 then
2508 Error_Msg_N
2509 ("REQUEUE does not permit parameters", First (Exprs));
2511 elsif Is_Entity_Name (P)
2512 and then Etype (P) = Standard_Void_Type
2513 then
2514 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2516 else
2517 Error_Msg_N ("array type required in indexed component", P);
2518 end if;
2520 Set_Etype (N, Any_Type);
2521 return;
2522 end if;
2524 Index := First_Index (Array_Type);
2525 while Present (Index) and then Present (Exp) loop
2526 if not Has_Compatible_Type (Exp, Etype (Index)) then
2527 Wrong_Type (Exp, Etype (Index));
2528 Set_Etype (N, Any_Type);
2529 return;
2530 end if;
2532 Next_Index (Index);
2533 Next (Exp);
2534 end loop;
2536 Set_Etype (N, Component_Type (Array_Type));
2537 Check_Implicit_Dereference (N, Etype (N));
2539 if Present (Index) then
2540 Error_Msg_N
2541 ("too few subscripts in array reference", First (Exprs));
2543 elsif Present (Exp) then
2544 Error_Msg_N ("too many subscripts in array reference", Exp);
2545 end if;
2546 end if;
2547 end Process_Indexed_Component;
2549 ----------------------------------------
2550 -- Process_Indexed_Component_Or_Slice --
2551 ----------------------------------------
2553 procedure Process_Indexed_Component_Or_Slice is
2554 begin
2555 Exp := First (Exprs);
2556 while Present (Exp) loop
2557 Analyze_Expression (Exp);
2558 Next (Exp);
2559 end loop;
2561 Exp := First (Exprs);
2563 -- If one index is present, and it is a subtype name, then the node
2564 -- denotes a slice (note that the case of an explicit range for a
2565 -- slice was already built as an N_Slice node in the first place,
2566 -- so that case is not handled here).
2568 -- We use a replace rather than a rewrite here because this is one
2569 -- of the cases in which the tree built by the parser is plain wrong.
2571 if No (Next (Exp))
2572 and then Is_Entity_Name (Exp)
2573 and then Is_Type (Entity (Exp))
2574 then
2575 Replace (N,
2576 Make_Slice (Sloc (N),
2577 Prefix => P,
2578 Discrete_Range => New_Copy (Exp)));
2579 Analyze (N);
2581 -- Otherwise (more than one index present, or single index is not
2582 -- a subtype name), then we have the indexed component case.
2584 else
2585 Process_Indexed_Component;
2586 end if;
2587 end Process_Indexed_Component_Or_Slice;
2589 ------------------------------------------
2590 -- Process_Overloaded_Indexed_Component --
2591 ------------------------------------------
2593 procedure Process_Overloaded_Indexed_Component is
2594 Exp : Node_Id;
2595 I : Interp_Index;
2596 It : Interp;
2597 Typ : Entity_Id;
2598 Index : Node_Id;
2599 Found : Boolean;
2601 begin
2602 Set_Etype (N, Any_Type);
2604 Get_First_Interp (P, I, It);
2605 while Present (It.Nam) loop
2606 Typ := It.Typ;
2608 if Is_Access_Type (Typ) then
2609 Typ := Designated_Type (Typ);
2610 Error_Msg_NW
2611 (Warn_On_Dereference, "?d?implicit dereference", N);
2612 end if;
2614 if Is_Array_Type (Typ) then
2616 -- Got a candidate: verify that index types are compatible
2618 Index := First_Index (Typ);
2619 Found := True;
2620 Exp := First (Exprs);
2621 while Present (Index) and then Present (Exp) loop
2622 if Has_Compatible_Type (Exp, Etype (Index)) then
2623 null;
2624 else
2625 Found := False;
2626 Remove_Interp (I);
2627 exit;
2628 end if;
2630 Next_Index (Index);
2631 Next (Exp);
2632 end loop;
2634 if Found and then No (Index) and then No (Exp) then
2635 declare
2636 CT : constant Entity_Id :=
2637 Base_Type (Component_Type (Typ));
2638 begin
2639 Add_One_Interp (N, CT, CT);
2640 Check_Implicit_Dereference (N, CT);
2641 end;
2642 end if;
2644 elsif Try_Container_Indexing (N, P, Exprs) then
2645 return;
2647 end if;
2649 Get_Next_Interp (I, It);
2650 end loop;
2652 if Etype (N) = Any_Type then
2653 Error_Msg_N ("no legal interpretation for indexed component", N);
2654 Set_Is_Overloaded (N, False);
2655 end if;
2657 End_Interp_List;
2658 end Process_Overloaded_Indexed_Component;
2660 -- Start of processing for Analyze_Indexed_Component_Form
2662 begin
2663 -- Get name of array, function or type
2665 Analyze (P);
2667 -- If P is an explicit dereference whose prefix is of a remote access-
2668 -- to-subprogram type, then N has already been rewritten as a subprogram
2669 -- call and analyzed.
2671 if Nkind (N) in N_Subprogram_Call then
2672 return;
2674 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2675 -- the indexed component denotes a loop name, the indexed form is turned
2676 -- into an attribute reference.
2678 elsif Nkind (N) = N_Attribute_Reference
2679 and then Attribute_Name (N) = Name_Loop_Entry
2680 then
2681 return;
2682 end if;
2684 pragma Assert (Nkind (N) = N_Indexed_Component);
2686 P_T := Base_Type (Etype (P));
2688 if Is_Entity_Name (P) and then Present (Entity (P)) then
2689 U_N := Entity (P);
2691 if Is_Type (U_N) then
2693 -- Reformat node as a type conversion
2695 E := Remove_Head (Exprs);
2697 if Present (First (Exprs)) then
2698 Error_Msg_N
2699 ("argument of type conversion must be single expression", N);
2700 end if;
2702 Change_Node (N, N_Type_Conversion);
2703 Set_Subtype_Mark (N, P);
2704 Set_Etype (N, U_N);
2705 Set_Expression (N, E);
2707 -- After changing the node, call for the specific Analysis
2708 -- routine directly, to avoid a double call to the expander.
2710 Analyze_Type_Conversion (N);
2711 return;
2712 end if;
2714 if Is_Overloadable (U_N) then
2715 Process_Function_Call;
2717 elsif Ekind (Etype (P)) = E_Subprogram_Type
2718 or else (Is_Access_Type (Etype (P))
2719 and then
2720 Ekind (Designated_Type (Etype (P))) =
2721 E_Subprogram_Type)
2722 then
2723 -- Call to access_to-subprogram with possible implicit dereference
2725 Process_Function_Call;
2727 elsif Is_Generic_Subprogram (U_N) then
2729 -- A common beginner's (or C++ templates fan) error
2731 Error_Msg_N ("generic subprogram cannot be called", N);
2732 Set_Etype (N, Any_Type);
2733 return;
2735 else
2736 Process_Indexed_Component_Or_Slice;
2737 end if;
2739 -- If not an entity name, prefix is an expression that may denote
2740 -- an array or an access-to-subprogram.
2742 else
2743 if Ekind (P_T) = E_Subprogram_Type
2744 or else (Is_Access_Type (P_T)
2745 and then
2746 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2747 then
2748 Process_Function_Call;
2750 elsif Nkind (P) = N_Selected_Component
2751 and then Present (Entity (Selector_Name (P)))
2752 and then Is_Overloadable (Entity (Selector_Name (P)))
2753 then
2754 Process_Function_Call;
2756 -- In ASIS mode within a generic, a prefixed call is analyzed and
2757 -- partially rewritten but the original indexed component has not
2758 -- yet been rewritten as a call. Perform the replacement now.
2760 elsif Nkind (P) = N_Selected_Component
2761 and then Nkind (Parent (P)) = N_Function_Call
2762 and then ASIS_Mode
2763 then
2764 Rewrite (N, Parent (P));
2765 Analyze (N);
2767 else
2768 -- Indexed component, slice, or a call to a member of a family
2769 -- entry, which will be converted to an entry call later.
2771 Process_Indexed_Component_Or_Slice;
2772 end if;
2773 end if;
2775 Analyze_Dimension (N);
2776 end Analyze_Indexed_Component_Form;
2778 ------------------------
2779 -- Analyze_Logical_Op --
2780 ------------------------
2782 procedure Analyze_Logical_Op (N : Node_Id) is
2783 L : constant Node_Id := Left_Opnd (N);
2784 R : constant Node_Id := Right_Opnd (N);
2785 Op_Id : Entity_Id := Entity (N);
2787 begin
2788 Set_Etype (N, Any_Type);
2789 Candidate_Type := Empty;
2791 Analyze_Expression (L);
2792 Analyze_Expression (R);
2794 if Present (Op_Id) then
2796 if Ekind (Op_Id) = E_Operator then
2797 Find_Boolean_Types (L, R, Op_Id, N);
2798 else
2799 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2800 end if;
2802 else
2803 Op_Id := Get_Name_Entity_Id (Chars (N));
2804 while Present (Op_Id) loop
2805 if Ekind (Op_Id) = E_Operator then
2806 Find_Boolean_Types (L, R, Op_Id, N);
2807 else
2808 Analyze_User_Defined_Binary_Op (N, Op_Id);
2809 end if;
2811 Op_Id := Homonym (Op_Id);
2812 end loop;
2813 end if;
2815 Operator_Check (N);
2816 Check_Function_Writable_Actuals (N);
2817 end Analyze_Logical_Op;
2819 ---------------------------
2820 -- Analyze_Membership_Op --
2821 ---------------------------
2823 procedure Analyze_Membership_Op (N : Node_Id) is
2824 Loc : constant Source_Ptr := Sloc (N);
2825 L : constant Node_Id := Left_Opnd (N);
2826 R : constant Node_Id := Right_Opnd (N);
2828 Index : Interp_Index;
2829 It : Interp;
2830 Found : Boolean := False;
2831 I_F : Interp_Index;
2832 T_F : Entity_Id;
2834 procedure Try_One_Interp (T1 : Entity_Id);
2835 -- Routine to try one proposed interpretation. Note that the context
2836 -- of the operation plays no role in resolving the arguments, so that
2837 -- if there is more than one interpretation of the operands that is
2838 -- compatible with a membership test, the operation is ambiguous.
2840 --------------------
2841 -- Try_One_Interp --
2842 --------------------
2844 procedure Try_One_Interp (T1 : Entity_Id) is
2845 begin
2846 if Has_Compatible_Type (R, T1) then
2847 if Found
2848 and then Base_Type (T1) /= Base_Type (T_F)
2849 then
2850 It := Disambiguate (L, I_F, Index, Any_Type);
2852 if It = No_Interp then
2853 Ambiguous_Operands (N);
2854 Set_Etype (L, Any_Type);
2855 return;
2857 else
2858 T_F := It.Typ;
2859 end if;
2861 else
2862 Found := True;
2863 T_F := T1;
2864 I_F := Index;
2865 end if;
2867 Set_Etype (L, T_F);
2868 end if;
2869 end Try_One_Interp;
2871 procedure Analyze_Set_Membership;
2872 -- If a set of alternatives is present, analyze each and find the
2873 -- common type to which they must all resolve.
2875 ----------------------------
2876 -- Analyze_Set_Membership --
2877 ----------------------------
2879 procedure Analyze_Set_Membership is
2880 Alt : Node_Id;
2881 Index : Interp_Index;
2882 It : Interp;
2883 Candidate_Interps : Node_Id;
2884 Common_Type : Entity_Id := Empty;
2886 begin
2887 if Comes_From_Source (N) then
2888 Check_Compiler_Unit ("set membership", N);
2889 end if;
2891 Analyze (L);
2892 Candidate_Interps := L;
2894 if not Is_Overloaded (L) then
2895 Common_Type := Etype (L);
2897 Alt := First (Alternatives (N));
2898 while Present (Alt) loop
2899 Analyze (Alt);
2901 if not Has_Compatible_Type (Alt, Common_Type) then
2902 Wrong_Type (Alt, Common_Type);
2903 end if;
2905 Next (Alt);
2906 end loop;
2908 else
2909 Alt := First (Alternatives (N));
2910 while Present (Alt) loop
2911 Analyze (Alt);
2912 if not Is_Overloaded (Alt) then
2913 Common_Type := Etype (Alt);
2915 else
2916 Get_First_Interp (Alt, Index, It);
2917 while Present (It.Typ) loop
2918 if not
2919 Has_Compatible_Type (Candidate_Interps, It.Typ)
2920 then
2921 Remove_Interp (Index);
2922 end if;
2924 Get_Next_Interp (Index, It);
2925 end loop;
2927 Get_First_Interp (Alt, Index, It);
2929 if No (It.Typ) then
2930 Error_Msg_N ("alternative has no legal type", Alt);
2931 return;
2932 end if;
2934 -- If alternative is not overloaded, we have a unique type
2935 -- for all of them.
2937 Set_Etype (Alt, It.Typ);
2939 -- If the alternative is an enumeration literal, use the one
2940 -- for this interpretation.
2942 if Is_Entity_Name (Alt) then
2943 Set_Entity (Alt, It.Nam);
2944 end if;
2946 Get_Next_Interp (Index, It);
2948 if No (It.Typ) then
2949 Set_Is_Overloaded (Alt, False);
2950 Common_Type := Etype (Alt);
2951 end if;
2953 Candidate_Interps := Alt;
2954 end if;
2956 Next (Alt);
2957 end loop;
2958 end if;
2960 Set_Etype (N, Standard_Boolean);
2962 if Present (Common_Type) then
2963 Set_Etype (L, Common_Type);
2965 -- The left operand may still be overloaded, to be resolved using
2966 -- the Common_Type.
2968 else
2969 Error_Msg_N ("cannot resolve membership operation", N);
2970 end if;
2971 end Analyze_Set_Membership;
2973 -- Start of processing for Analyze_Membership_Op
2975 begin
2976 Analyze_Expression (L);
2978 if No (R) and then Ada_Version >= Ada_2012 then
2979 Analyze_Set_Membership;
2980 Check_Function_Writable_Actuals (N);
2982 return;
2983 end if;
2985 if Nkind (R) = N_Range
2986 or else (Nkind (R) = N_Attribute_Reference
2987 and then Attribute_Name (R) = Name_Range)
2988 then
2989 Analyze (R);
2991 if not Is_Overloaded (L) then
2992 Try_One_Interp (Etype (L));
2994 else
2995 Get_First_Interp (L, Index, It);
2996 while Present (It.Typ) loop
2997 Try_One_Interp (It.Typ);
2998 Get_Next_Interp (Index, It);
2999 end loop;
3000 end if;
3002 -- If not a range, it can be a subtype mark, or else it is a degenerate
3003 -- membership test with a singleton value, i.e. a test for equality,
3004 -- if the types are compatible.
3006 else
3007 Analyze (R);
3009 if Is_Entity_Name (R)
3010 and then Is_Type (Entity (R))
3011 then
3012 Find_Type (R);
3013 Check_Fully_Declared (Entity (R), R);
3015 elsif Ada_Version >= Ada_2012
3016 and then Has_Compatible_Type (R, Etype (L))
3017 then
3018 if Nkind (N) = N_In then
3019 Rewrite (N,
3020 Make_Op_Eq (Loc,
3021 Left_Opnd => L,
3022 Right_Opnd => R));
3023 else
3024 Rewrite (N,
3025 Make_Op_Ne (Loc,
3026 Left_Opnd => L,
3027 Right_Opnd => R));
3028 end if;
3030 Analyze (N);
3031 return;
3033 else
3034 -- In all versions of the language, if we reach this point there
3035 -- is a previous error that will be diagnosed below.
3037 Find_Type (R);
3038 end if;
3039 end if;
3041 -- Compatibility between expression and subtype mark or range is
3042 -- checked during resolution. The result of the operation is Boolean
3043 -- in any case.
3045 Set_Etype (N, Standard_Boolean);
3047 if Comes_From_Source (N)
3048 and then Present (Right_Opnd (N))
3049 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3050 then
3051 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3052 end if;
3054 Check_Function_Writable_Actuals (N);
3055 end Analyze_Membership_Op;
3057 -----------------
3058 -- Analyze_Mod --
3059 -----------------
3061 procedure Analyze_Mod (N : Node_Id) is
3062 begin
3063 -- A special warning check, if we have an expression of the form:
3064 -- expr mod 2 * literal
3065 -- where literal is 64 or less, then probably what was meant was
3066 -- expr mod 2 ** literal
3067 -- so issue an appropriate warning.
3069 if Warn_On_Suspicious_Modulus_Value
3070 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3071 and then Intval (Right_Opnd (N)) = Uint_2
3072 and then Nkind (Parent (N)) = N_Op_Multiply
3073 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3074 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
3075 then
3076 Error_Msg_N
3077 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
3078 end if;
3080 -- Remaining processing is same as for other arithmetic operators
3082 Analyze_Arithmetic_Op (N);
3083 end Analyze_Mod;
3085 ----------------------
3086 -- Analyze_Negation --
3087 ----------------------
3089 procedure Analyze_Negation (N : Node_Id) is
3090 R : constant Node_Id := Right_Opnd (N);
3091 Op_Id : Entity_Id := Entity (N);
3093 begin
3094 Set_Etype (N, Any_Type);
3095 Candidate_Type := Empty;
3097 Analyze_Expression (R);
3099 if Present (Op_Id) then
3100 if Ekind (Op_Id) = E_Operator then
3101 Find_Negation_Types (R, Op_Id, N);
3102 else
3103 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3104 end if;
3106 else
3107 Op_Id := Get_Name_Entity_Id (Chars (N));
3108 while Present (Op_Id) loop
3109 if Ekind (Op_Id) = E_Operator then
3110 Find_Negation_Types (R, Op_Id, N);
3111 else
3112 Analyze_User_Defined_Unary_Op (N, Op_Id);
3113 end if;
3115 Op_Id := Homonym (Op_Id);
3116 end loop;
3117 end if;
3119 Operator_Check (N);
3120 end Analyze_Negation;
3122 ------------------
3123 -- Analyze_Null --
3124 ------------------
3126 procedure Analyze_Null (N : Node_Id) is
3127 begin
3128 Check_SPARK_05_Restriction ("null is not allowed", N);
3130 Set_Etype (N, Any_Access);
3131 end Analyze_Null;
3133 ----------------------
3134 -- Analyze_One_Call --
3135 ----------------------
3137 procedure Analyze_One_Call
3138 (N : Node_Id;
3139 Nam : Entity_Id;
3140 Report : Boolean;
3141 Success : out Boolean;
3142 Skip_First : Boolean := False)
3144 Actuals : constant List_Id := Parameter_Associations (N);
3145 Prev_T : constant Entity_Id := Etype (N);
3147 Must_Skip : constant Boolean := Skip_First
3148 or else Nkind (Original_Node (N)) = N_Selected_Component
3149 or else
3150 (Nkind (Original_Node (N)) = N_Indexed_Component
3151 and then Nkind (Prefix (Original_Node (N)))
3152 = N_Selected_Component);
3153 -- The first formal must be omitted from the match when trying to find
3154 -- a primitive operation that is a possible interpretation, and also
3155 -- after the call has been rewritten, because the corresponding actual
3156 -- is already known to be compatible, and because this may be an
3157 -- indexing of a call with default parameters.
3159 Formal : Entity_Id;
3160 Actual : Node_Id;
3161 Is_Indexed : Boolean := False;
3162 Is_Indirect : Boolean := False;
3163 Subp_Type : constant Entity_Id := Etype (Nam);
3164 Norm_OK : Boolean;
3166 function Compatible_Types_In_Predicate
3167 (T1 : Entity_Id;
3168 T2 : Entity_Id) return Boolean;
3169 -- For an Ada 2012 predicate or invariant, a call may mention an
3170 -- incomplete type, while resolution of the corresponding predicate
3171 -- function may see the full view, as a consequence of the delayed
3172 -- resolution of the corresponding expressions. This may occur in
3173 -- the body of a predicate function, or in a call to such. Anomalies
3174 -- involving private and full views can also happen. In each case,
3175 -- rewrite node or add conversions to remove spurious type errors.
3177 procedure Indicate_Name_And_Type;
3178 -- If candidate interpretation matches, indicate name and type of result
3179 -- on call node.
3181 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3182 -- There may be a user-defined operator that hides the current
3183 -- interpretation. We must check for this independently of the
3184 -- analysis of the call with the user-defined operation, because
3185 -- the parameter names may be wrong and yet the hiding takes place.
3186 -- This fixes a problem with ACATS test B34014O.
3188 -- When the type Address is a visible integer type, and the DEC
3189 -- system extension is visible, the predefined operator may be
3190 -- hidden as well, by one of the address operations in auxdec.
3191 -- Finally, The abstract operations on address do not hide the
3192 -- predefined operator (this is the purpose of making them abstract).
3194 -----------------------------------
3195 -- Compatible_Types_In_Predicate --
3196 -----------------------------------
3198 function Compatible_Types_In_Predicate
3199 (T1 : Entity_Id;
3200 T2 : Entity_Id) return Boolean
3202 function Common_Type (T : Entity_Id) return Entity_Id;
3203 -- Find non-private full view if any, without going to ancestor type
3204 -- (as opposed to Underlying_Type).
3206 -----------------
3207 -- Common_Type --
3208 -----------------
3210 function Common_Type (T : Entity_Id) return Entity_Id is
3211 begin
3212 if Is_Private_Type (T) and then Present (Full_View (T)) then
3213 return Base_Type (Full_View (T));
3214 else
3215 return Base_Type (T);
3216 end if;
3217 end Common_Type;
3219 -- Start of processing for Compatible_Types_In_Predicate
3221 begin
3222 if (Ekind (Current_Scope) = E_Function
3223 and then Is_Predicate_Function (Current_Scope))
3224 or else
3225 (Ekind (Nam) = E_Function
3226 and then Is_Predicate_Function (Nam))
3227 then
3228 if Is_Incomplete_Type (T1)
3229 and then Present (Full_View (T1))
3230 and then Full_View (T1) = T2
3231 then
3232 Set_Etype (Formal, Etype (Actual));
3233 return True;
3235 elsif Common_Type (T1) = Common_Type (T2) then
3236 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3237 return True;
3239 else
3240 return False;
3241 end if;
3243 else
3244 return False;
3245 end if;
3246 end Compatible_Types_In_Predicate;
3248 ----------------------------
3249 -- Indicate_Name_And_Type --
3250 ----------------------------
3252 procedure Indicate_Name_And_Type is
3253 begin
3254 Add_One_Interp (N, Nam, Etype (Nam));
3255 Check_Implicit_Dereference (N, Etype (Nam));
3256 Success := True;
3258 -- If the prefix of the call is a name, indicate the entity
3259 -- being called. If it is not a name, it is an expression that
3260 -- denotes an access to subprogram or else an entry or family. In
3261 -- the latter case, the name is a selected component, and the entity
3262 -- being called is noted on the selector.
3264 if not Is_Type (Nam) then
3265 if Is_Entity_Name (Name (N)) then
3266 Set_Entity (Name (N), Nam);
3267 Set_Etype (Name (N), Etype (Nam));
3269 elsif Nkind (Name (N)) = N_Selected_Component then
3270 Set_Entity (Selector_Name (Name (N)), Nam);
3271 end if;
3272 end if;
3274 if Debug_Flag_E and not Report then
3275 Write_Str (" Overloaded call ");
3276 Write_Int (Int (N));
3277 Write_Str (" compatible with ");
3278 Write_Int (Int (Nam));
3279 Write_Eol;
3280 end if;
3281 end Indicate_Name_And_Type;
3283 ------------------------
3284 -- Operator_Hidden_By --
3285 ------------------------
3287 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3288 Act1 : constant Node_Id := First_Actual (N);
3289 Act2 : constant Node_Id := Next_Actual (Act1);
3290 Form1 : constant Entity_Id := First_Formal (Fun);
3291 Form2 : constant Entity_Id := Next_Formal (Form1);
3293 begin
3294 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3295 return False;
3297 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3298 return False;
3300 elsif Present (Form2) then
3301 if No (Act2)
3302 or else not Has_Compatible_Type (Act2, Etype (Form2))
3303 then
3304 return False;
3305 end if;
3307 elsif Present (Act2) then
3308 return False;
3309 end if;
3311 -- Now we know that the arity of the operator matches the function,
3312 -- and the function call is a valid interpretation. The function
3313 -- hides the operator if it has the right signature, or if one of
3314 -- its operands is a non-abstract operation on Address when this is
3315 -- a visible integer type.
3317 return Hides_Op (Fun, Nam)
3318 or else Is_Descendant_Of_Address (Etype (Form1))
3319 or else
3320 (Present (Form2)
3321 and then Is_Descendant_Of_Address (Etype (Form2)));
3322 end Operator_Hidden_By;
3324 -- Start of processing for Analyze_One_Call
3326 begin
3327 Success := False;
3329 -- If the subprogram has no formals or if all the formals have defaults,
3330 -- and the return type is an array type, the node may denote an indexing
3331 -- of the result of a parameterless call. In Ada 2005, the subprogram
3332 -- may have one non-defaulted formal, and the call may have been written
3333 -- in prefix notation, so that the rebuilt parameter list has more than
3334 -- one actual.
3336 if not Is_Overloadable (Nam)
3337 and then Ekind (Nam) /= E_Subprogram_Type
3338 and then Ekind (Nam) /= E_Entry_Family
3339 then
3340 return;
3341 end if;
3343 -- An indexing requires at least one actual. The name of the call cannot
3344 -- be an implicit indirect call, so it cannot be a generated explicit
3345 -- dereference.
3347 if not Is_Empty_List (Actuals)
3348 and then
3349 (Needs_No_Actuals (Nam)
3350 or else
3351 (Needs_One_Actual (Nam)
3352 and then Present (Next_Actual (First (Actuals)))))
3353 then
3354 if Is_Array_Type (Subp_Type)
3355 and then
3356 (Nkind (Name (N)) /= N_Explicit_Dereference
3357 or else Comes_From_Source (Name (N)))
3358 then
3359 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3361 elsif Is_Access_Type (Subp_Type)
3362 and then Is_Array_Type (Designated_Type (Subp_Type))
3363 then
3364 Is_Indexed :=
3365 Try_Indexed_Call
3366 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3368 -- The prefix can also be a parameterless function that returns an
3369 -- access to subprogram, in which case this is an indirect call.
3370 -- If this succeeds, an explicit dereference is added later on,
3371 -- in Analyze_Call or Resolve_Call.
3373 elsif Is_Access_Type (Subp_Type)
3374 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3375 then
3376 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3377 end if;
3379 end if;
3381 -- If the call has been transformed into a slice, it is of the form
3382 -- F (Subtype) where F is parameterless. The node has been rewritten in
3383 -- Try_Indexed_Call and there is nothing else to do.
3385 if Is_Indexed
3386 and then Nkind (N) = N_Slice
3387 then
3388 return;
3389 end if;
3391 Normalize_Actuals
3392 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3394 if not Norm_OK then
3396 -- If an indirect call is a possible interpretation, indicate
3397 -- success to the caller. This may be an indexing of an explicit
3398 -- dereference of a call that returns an access type (see above).
3400 if Is_Indirect
3401 or else (Is_Indexed
3402 and then Nkind (Name (N)) = N_Explicit_Dereference
3403 and then Comes_From_Source (Name (N)))
3404 then
3405 Success := True;
3406 return;
3408 -- Mismatch in number or names of parameters
3410 elsif Debug_Flag_E then
3411 Write_Str (" normalization fails in call ");
3412 Write_Int (Int (N));
3413 Write_Str (" with subprogram ");
3414 Write_Int (Int (Nam));
3415 Write_Eol;
3416 end if;
3418 -- If the context expects a function call, discard any interpretation
3419 -- that is a procedure. If the node is not overloaded, leave as is for
3420 -- better error reporting when type mismatch is found.
3422 elsif Nkind (N) = N_Function_Call
3423 and then Is_Overloaded (Name (N))
3424 and then Ekind (Nam) = E_Procedure
3425 then
3426 return;
3428 -- Ditto for function calls in a procedure context
3430 elsif Nkind (N) = N_Procedure_Call_Statement
3431 and then Is_Overloaded (Name (N))
3432 and then Etype (Nam) /= Standard_Void_Type
3433 then
3434 return;
3436 elsif No (Actuals) then
3438 -- If Normalize succeeds, then there are default parameters for
3439 -- all formals.
3441 Indicate_Name_And_Type;
3443 elsif Ekind (Nam) = E_Operator then
3444 if Nkind (N) = N_Procedure_Call_Statement then
3445 return;
3446 end if;
3448 -- This can occur when the prefix of the call is an operator
3449 -- name or an expanded name whose selector is an operator name.
3451 Analyze_Operator_Call (N, Nam);
3453 if Etype (N) /= Prev_T then
3455 -- Check that operator is not hidden by a function interpretation
3457 if Is_Overloaded (Name (N)) then
3458 declare
3459 I : Interp_Index;
3460 It : Interp;
3462 begin
3463 Get_First_Interp (Name (N), I, It);
3464 while Present (It.Nam) loop
3465 if Operator_Hidden_By (It.Nam) then
3466 Set_Etype (N, Prev_T);
3467 return;
3468 end if;
3470 Get_Next_Interp (I, It);
3471 end loop;
3472 end;
3473 end if;
3475 -- If operator matches formals, record its name on the call.
3476 -- If the operator is overloaded, Resolve will select the
3477 -- correct one from the list of interpretations. The call
3478 -- node itself carries the first candidate.
3480 Set_Entity (Name (N), Nam);
3481 Success := True;
3483 elsif Report and then Etype (N) = Any_Type then
3484 Error_Msg_N ("incompatible arguments for operator", N);
3485 end if;
3487 else
3488 -- Normalize_Actuals has chained the named associations in the
3489 -- correct order of the formals.
3491 Actual := First_Actual (N);
3492 Formal := First_Formal (Nam);
3494 -- If we are analyzing a call rewritten from object notation, skip
3495 -- first actual, which may be rewritten later as an explicit
3496 -- dereference.
3498 if Must_Skip then
3499 Next_Actual (Actual);
3500 Next_Formal (Formal);
3501 end if;
3503 while Present (Actual) and then Present (Formal) loop
3504 if Nkind (Parent (Actual)) /= N_Parameter_Association
3505 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3506 then
3507 -- The actual can be compatible with the formal, but we must
3508 -- also check that the context is not an address type that is
3509 -- visibly an integer type. In this case the use of literals is
3510 -- illegal, except in the body of descendants of system, where
3511 -- arithmetic operations on address are of course used.
3513 if Has_Compatible_Type (Actual, Etype (Formal))
3514 and then
3515 (Etype (Actual) /= Universal_Integer
3516 or else not Is_Descendant_Of_Address (Etype (Formal))
3517 or else In_Predefined_Unit (N))
3518 then
3519 Next_Actual (Actual);
3520 Next_Formal (Formal);
3522 -- In Allow_Integer_Address mode, we allow an actual integer to
3523 -- match a formal address type and vice versa. We only do this
3524 -- if we are certain that an error will otherwise be issued
3526 elsif Address_Integer_Convert_OK
3527 (Etype (Actual), Etype (Formal))
3528 and then (Report and not Is_Indexed and not Is_Indirect)
3529 then
3530 -- Handle this case by introducing an unchecked conversion
3532 Rewrite (Actual,
3533 Unchecked_Convert_To (Etype (Formal),
3534 Relocate_Node (Actual)));
3535 Analyze_And_Resolve (Actual, Etype (Formal));
3536 Next_Actual (Actual);
3537 Next_Formal (Formal);
3539 -- Under relaxed RM semantics silently replace occurrences of
3540 -- null by System.Address_Null. We only do this if we know that
3541 -- an error will otherwise be issued.
3543 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3544 and then (Report and not Is_Indexed and not Is_Indirect)
3545 then
3546 Replace_Null_By_Null_Address (Actual);
3547 Analyze_And_Resolve (Actual, Etype (Formal));
3548 Next_Actual (Actual);
3549 Next_Formal (Formal);
3551 elsif Compatible_Types_In_Predicate
3552 (Etype (Formal), Etype (Actual))
3553 then
3554 Next_Actual (Actual);
3555 Next_Formal (Formal);
3557 -- In a complex case where an enclosing generic and a nested
3558 -- generic package, both declared with partially parameterized
3559 -- formal subprograms with the same names, are instantiated
3560 -- with the same type, the types of the actual parameter and
3561 -- that of the formal may appear incompatible at first sight.
3563 -- generic
3564 -- type Outer_T is private;
3565 -- with function Func (Formal : Outer_T)
3566 -- return ... is <>;
3568 -- package Outer_Gen is
3569 -- generic
3570 -- type Inner_T is private;
3571 -- with function Func (Formal : Inner_T) -- (1)
3572 -- return ... is <>;
3574 -- package Inner_Gen is
3575 -- function Inner_Func (Formal : Inner_T) -- (2)
3576 -- return ... is (Func (Formal));
3577 -- end Inner_Gen;
3578 -- end Outer_Generic;
3580 -- package Outer_Inst is new Outer_Gen (Actual_T);
3581 -- package Inner_Inst is new Outer_Inst.Inner_Gen (Actual_T);
3583 -- In the example above, the type of parameter
3584 -- Inner_Func.Formal at (2) is incompatible with the type of
3585 -- Func.Formal at (1) in the context of instantiations
3586 -- Outer_Inst and Inner_Inst. In reality both types are generic
3587 -- actual subtypes renaming base type Actual_T as part of the
3588 -- generic prologues for the instantiations.
3590 -- Recognize this case and add a type conversion to allow this
3591 -- kind of generic actual subtype conformance. Note that this
3592 -- is done only when the call is non-overloaded because the
3593 -- resolution mechanism already has the means to disambiguate
3594 -- similar cases.
3596 elsif not Is_Overloaded (Name (N))
3597 and then Is_Type (Etype (Actual))
3598 and then Is_Type (Etype (Formal))
3599 and then Is_Generic_Actual_Type (Etype (Actual))
3600 and then Is_Generic_Actual_Type (Etype (Formal))
3601 and then Base_Type (Etype (Actual)) =
3602 Base_Type (Etype (Formal))
3603 then
3604 Rewrite (Actual,
3605 Convert_To (Etype (Formal), Relocate_Node (Actual)));
3606 Analyze_And_Resolve (Actual, Etype (Formal));
3607 Next_Actual (Actual);
3608 Next_Formal (Formal);
3610 -- Handle failed type check
3612 else
3613 if Debug_Flag_E then
3614 Write_Str (" type checking fails in call ");
3615 Write_Int (Int (N));
3616 Write_Str (" with formal ");
3617 Write_Int (Int (Formal));
3618 Write_Str (" in subprogram ");
3619 Write_Int (Int (Nam));
3620 Write_Eol;
3621 end if;
3623 -- Comment needed on the following test???
3625 if Report and not Is_Indexed and not Is_Indirect then
3627 -- Ada 2005 (AI-251): Complete the error notification
3628 -- to help new Ada 2005 users.
3630 if Is_Class_Wide_Type (Etype (Formal))
3631 and then Is_Interface (Etype (Etype (Formal)))
3632 and then not Interface_Present_In_Ancestor
3633 (Typ => Etype (Actual),
3634 Iface => Etype (Etype (Formal)))
3635 then
3636 Error_Msg_NE
3637 ("(Ada 2005) does not implement interface }",
3638 Actual, Etype (Etype (Formal)));
3639 end if;
3641 Wrong_Type (Actual, Etype (Formal));
3643 if Nkind (Actual) = N_Op_Eq
3644 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3645 then
3646 Formal := First_Formal (Nam);
3647 while Present (Formal) loop
3648 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3649 Error_Msg_N -- CODEFIX
3650 ("possible misspelling of `='>`!", Actual);
3651 exit;
3652 end if;
3654 Next_Formal (Formal);
3655 end loop;
3656 end if;
3658 if All_Errors_Mode then
3659 Error_Msg_Sloc := Sloc (Nam);
3661 if Etype (Formal) = Any_Type then
3662 Error_Msg_N
3663 ("there is no legal actual parameter", Actual);
3664 end if;
3666 if Is_Overloadable (Nam)
3667 and then Present (Alias (Nam))
3668 and then not Comes_From_Source (Nam)
3669 then
3670 Error_Msg_NE
3671 ("\\ =='> in call to inherited operation & #!",
3672 Actual, Nam);
3674 elsif Ekind (Nam) = E_Subprogram_Type then
3675 declare
3676 Access_To_Subprogram_Typ :
3677 constant Entity_Id :=
3678 Defining_Identifier
3679 (Associated_Node_For_Itype (Nam));
3680 begin
3681 Error_Msg_NE
3682 ("\\ =='> in call to dereference of &#!",
3683 Actual, Access_To_Subprogram_Typ);
3684 end;
3686 else
3687 Error_Msg_NE
3688 ("\\ =='> in call to &#!", Actual, Nam);
3690 end if;
3691 end if;
3692 end if;
3694 return;
3695 end if;
3697 else
3698 -- Normalize_Actuals has verified that a default value exists
3699 -- for this formal. Current actual names a subsequent formal.
3701 Next_Formal (Formal);
3702 end if;
3703 end loop;
3705 -- On exit, all actuals match
3707 Indicate_Name_And_Type;
3708 end if;
3709 end Analyze_One_Call;
3711 ---------------------------
3712 -- Analyze_Operator_Call --
3713 ---------------------------
3715 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3716 Op_Name : constant Name_Id := Chars (Op_Id);
3717 Act1 : constant Node_Id := First_Actual (N);
3718 Act2 : constant Node_Id := Next_Actual (Act1);
3720 begin
3721 -- Binary operator case
3723 if Present (Act2) then
3725 -- If more than two operands, then not binary operator after all
3727 if Present (Next_Actual (Act2)) then
3728 return;
3729 end if;
3731 -- Otherwise action depends on operator
3733 case Op_Name is
3734 when Name_Op_Add
3735 | Name_Op_Divide
3736 | Name_Op_Expon
3737 | Name_Op_Mod
3738 | Name_Op_Multiply
3739 | Name_Op_Rem
3740 | Name_Op_Subtract
3742 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3744 when Name_Op_And
3745 | Name_Op_Or
3746 | Name_Op_Xor
3748 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3750 when Name_Op_Ge
3751 | Name_Op_Gt
3752 | Name_Op_Le
3753 | Name_Op_Lt
3755 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3757 when Name_Op_Eq
3758 | Name_Op_Ne
3760 Find_Equality_Types (Act1, Act2, Op_Id, N);
3762 when Name_Op_Concat =>
3763 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3765 -- Is this when others, or should it be an abort???
3767 when others =>
3768 null;
3769 end case;
3771 -- Unary operator case
3773 else
3774 case Op_Name is
3775 when Name_Op_Abs
3776 | Name_Op_Add
3777 | Name_Op_Subtract
3779 Find_Unary_Types (Act1, Op_Id, N);
3781 when Name_Op_Not =>
3782 Find_Negation_Types (Act1, Op_Id, N);
3784 -- Is this when others correct, or should it be an abort???
3786 when others =>
3787 null;
3788 end case;
3789 end if;
3790 end Analyze_Operator_Call;
3792 -------------------------------------------
3793 -- Analyze_Overloaded_Selected_Component --
3794 -------------------------------------------
3796 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3797 Nam : constant Node_Id := Prefix (N);
3798 Sel : constant Node_Id := Selector_Name (N);
3799 Comp : Entity_Id;
3800 I : Interp_Index;
3801 It : Interp;
3802 T : Entity_Id;
3804 begin
3805 Set_Etype (Sel, Any_Type);
3807 Get_First_Interp (Nam, I, It);
3808 while Present (It.Typ) loop
3809 if Is_Access_Type (It.Typ) then
3810 T := Designated_Type (It.Typ);
3811 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3812 else
3813 T := It.Typ;
3814 end if;
3816 -- Locate the component. For a private prefix the selector can denote
3817 -- a discriminant.
3819 if Is_Record_Type (T) or else Is_Private_Type (T) then
3821 -- If the prefix is a class-wide type, the visible components are
3822 -- those of the base type.
3824 if Is_Class_Wide_Type (T) then
3825 T := Etype (T);
3826 end if;
3828 Comp := First_Entity (T);
3829 while Present (Comp) loop
3830 if Chars (Comp) = Chars (Sel)
3831 and then Is_Visible_Component (Comp)
3832 then
3834 -- AI05-105: if the context is an object renaming with
3835 -- an anonymous access type, the expected type of the
3836 -- object must be anonymous. This is a name resolution rule.
3838 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3839 or else No (Access_Definition (Parent (N)))
3840 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3841 or else
3842 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3843 then
3844 Set_Entity (Sel, Comp);
3845 Set_Etype (Sel, Etype (Comp));
3846 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3847 Check_Implicit_Dereference (N, Etype (Comp));
3849 -- This also specifies a candidate to resolve the name.
3850 -- Further overloading will be resolved from context.
3851 -- The selector name itself does not carry overloading
3852 -- information.
3854 Set_Etype (Nam, It.Typ);
3856 else
3857 -- Named access type in the context of a renaming
3858 -- declaration with an access definition. Remove
3859 -- inapplicable candidate.
3861 Remove_Interp (I);
3862 end if;
3863 end if;
3865 Next_Entity (Comp);
3866 end loop;
3868 elsif Is_Concurrent_Type (T) then
3869 Comp := First_Entity (T);
3870 while Present (Comp)
3871 and then Comp /= First_Private_Entity (T)
3872 loop
3873 if Chars (Comp) = Chars (Sel) then
3874 if Is_Overloadable (Comp) then
3875 Add_One_Interp (Sel, Comp, Etype (Comp));
3876 else
3877 Set_Entity_With_Checks (Sel, Comp);
3878 Generate_Reference (Comp, Sel);
3879 end if;
3881 Set_Etype (Sel, Etype (Comp));
3882 Set_Etype (N, Etype (Comp));
3883 Set_Etype (Nam, It.Typ);
3885 -- For access type case, introduce explicit dereference for
3886 -- more uniform treatment of entry calls. Do this only once
3887 -- if several interpretations yield an access type.
3889 if Is_Access_Type (Etype (Nam))
3890 and then Nkind (Nam) /= N_Explicit_Dereference
3891 then
3892 Insert_Explicit_Dereference (Nam);
3893 Error_Msg_NW
3894 (Warn_On_Dereference, "?d?implicit dereference", N);
3895 end if;
3896 end if;
3898 Next_Entity (Comp);
3899 end loop;
3901 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3902 end if;
3904 Get_Next_Interp (I, It);
3905 end loop;
3907 if Etype (N) = Any_Type
3908 and then not Try_Object_Operation (N)
3909 then
3910 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3911 Set_Entity (Sel, Any_Id);
3912 Set_Etype (Sel, Any_Type);
3913 end if;
3914 end Analyze_Overloaded_Selected_Component;
3916 ----------------------------------
3917 -- Analyze_Qualified_Expression --
3918 ----------------------------------
3920 procedure Analyze_Qualified_Expression (N : Node_Id) is
3921 Mark : constant Entity_Id := Subtype_Mark (N);
3922 Expr : constant Node_Id := Expression (N);
3923 I : Interp_Index;
3924 It : Interp;
3925 T : Entity_Id;
3927 begin
3928 Analyze_Expression (Expr);
3930 Set_Etype (N, Any_Type);
3931 Find_Type (Mark);
3932 T := Entity (Mark);
3934 if Nkind_In (Enclosing_Declaration (N), N_Formal_Type_Declaration,
3935 N_Full_Type_Declaration,
3936 N_Incomplete_Type_Declaration,
3937 N_Protected_Type_Declaration,
3938 N_Private_Extension_Declaration,
3939 N_Private_Type_Declaration,
3940 N_Subtype_Declaration,
3941 N_Task_Type_Declaration)
3942 and then T = Defining_Identifier (Enclosing_Declaration (N))
3943 then
3944 Error_Msg_N ("current instance not allowed", Mark);
3945 T := Any_Type;
3946 end if;
3948 Set_Etype (N, T);
3950 if T = Any_Type then
3951 return;
3952 end if;
3954 Check_Fully_Declared (T, N);
3956 -- If expected type is class-wide, check for exact match before
3957 -- expansion, because if the expression is a dispatching call it
3958 -- may be rewritten as explicit dereference with class-wide result.
3959 -- If expression is overloaded, retain only interpretations that
3960 -- will yield exact matches.
3962 if Is_Class_Wide_Type (T) then
3963 if not Is_Overloaded (Expr) then
3964 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3965 if Nkind (Expr) = N_Aggregate then
3966 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3967 else
3968 Wrong_Type (Expr, T);
3969 end if;
3970 end if;
3972 else
3973 Get_First_Interp (Expr, I, It);
3975 while Present (It.Nam) loop
3976 if Base_Type (It.Typ) /= Base_Type (T) then
3977 Remove_Interp (I);
3978 end if;
3980 Get_Next_Interp (I, It);
3981 end loop;
3982 end if;
3983 end if;
3985 Set_Etype (N, T);
3986 end Analyze_Qualified_Expression;
3988 -----------------------------------
3989 -- Analyze_Quantified_Expression --
3990 -----------------------------------
3992 procedure Analyze_Quantified_Expression (N : Node_Id) is
3993 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3994 -- If the iterator is part of a quantified expression, and the range is
3995 -- known to be statically empty, emit a warning and replace expression
3996 -- with its static value. Returns True if the replacement occurs.
3998 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3999 -- Determine whether if expression If_Expr lacks an else part or if it
4000 -- has one, it evaluates to True.
4002 --------------------
4003 -- Is_Empty_Range --
4004 --------------------
4006 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4007 Loc : constant Source_Ptr := Sloc (N);
4009 begin
4010 if Is_Array_Type (Typ)
4011 and then Compile_Time_Known_Bounds (Typ)
4012 and then
4013 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4014 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
4015 then
4016 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4018 if All_Present (N) then
4019 Error_Msg_N
4020 ("??quantified expression with ALL "
4021 & "over a null range has value True", N);
4022 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4024 else
4025 Error_Msg_N
4026 ("??quantified expression with SOME "
4027 & "over a null range has value False", N);
4028 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4029 end if;
4031 Analyze (N);
4032 return True;
4034 else
4035 return False;
4036 end if;
4037 end Is_Empty_Range;
4039 -----------------------------
4040 -- No_Else_Or_Trivial_True --
4041 -----------------------------
4043 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4044 Else_Expr : constant Node_Id :=
4045 Next (Next (First (Expressions (If_Expr))));
4046 begin
4047 return
4048 No (Else_Expr)
4049 or else (Compile_Time_Known_Value (Else_Expr)
4050 and then Is_True (Expr_Value (Else_Expr)));
4051 end No_Else_Or_Trivial_True;
4053 -- Local variables
4055 Cond : constant Node_Id := Condition (N);
4056 Loop_Id : Entity_Id;
4057 QE_Scop : Entity_Id;
4059 -- Start of processing for Analyze_Quantified_Expression
4061 begin
4062 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
4064 -- Create a scope to emulate the loop-like behavior of the quantified
4065 -- expression. The scope is needed to provide proper visibility of the
4066 -- loop variable.
4068 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
4069 Set_Etype (QE_Scop, Standard_Void_Type);
4070 Set_Scope (QE_Scop, Current_Scope);
4071 Set_Parent (QE_Scop, N);
4073 Push_Scope (QE_Scop);
4075 -- All constituents are preanalyzed and resolved to avoid untimely
4076 -- generation of various temporaries and types. Full analysis and
4077 -- expansion is carried out when the quantified expression is
4078 -- transformed into an expression with actions.
4080 if Present (Iterator_Specification (N)) then
4081 Preanalyze (Iterator_Specification (N));
4083 -- Do not proceed with the analysis when the range of iteration is
4084 -- empty. The appropriate error is issued by Is_Empty_Range.
4086 if Is_Entity_Name (Name (Iterator_Specification (N)))
4087 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4088 then
4089 return;
4090 end if;
4092 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4093 declare
4094 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4096 begin
4097 Preanalyze (Loop_Par);
4099 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4100 and then Parent (Loop_Par) /= N
4101 then
4102 -- The parser cannot distinguish between a loop specification
4103 -- and an iterator specification. If after pre-analysis the
4104 -- proper form has been recognized, rewrite the expression to
4105 -- reflect the right kind. This is needed for proper ASIS
4106 -- navigation. If expansion is enabled, the transformation is
4107 -- performed when the expression is rewritten as a loop.
4109 Set_Iterator_Specification (N,
4110 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4112 Set_Defining_Identifier (Iterator_Specification (N),
4113 Relocate_Node (Defining_Identifier (Loop_Par)));
4114 Set_Name (Iterator_Specification (N),
4115 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4116 Set_Comes_From_Source (Iterator_Specification (N),
4117 Comes_From_Source (Loop_Parameter_Specification (N)));
4118 Set_Loop_Parameter_Specification (N, Empty);
4119 end if;
4120 end;
4121 end if;
4123 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4125 End_Scope;
4126 Set_Etype (N, Standard_Boolean);
4128 -- Verify that the loop variable is used within the condition of the
4129 -- quantified expression.
4131 if Present (Iterator_Specification (N)) then
4132 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4133 else
4134 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4135 end if;
4137 if Warn_On_Suspicious_Contract
4138 and then not Referenced (Loop_Id, Cond)
4139 then
4140 -- Generating C, this check causes spurious warnings on inlined
4141 -- postconditions; we can safely disable it because this check
4142 -- was previously performed when analyzing the internally built
4143 -- postconditions procedure.
4145 if Modify_Tree_For_C and then In_Inlined_Body then
4146 null;
4147 else
4148 Error_Msg_N ("?T?unused variable &", Loop_Id);
4149 end if;
4150 end if;
4152 -- Diagnose a possible misuse of the SOME existential quantifier. When
4153 -- we have a quantified expression of the form:
4155 -- for some X => (if P then Q [else True])
4157 -- any value for X that makes P False results in the if expression being
4158 -- trivially True, and so also results in the quantified expression
4159 -- being trivially True.
4161 if Warn_On_Suspicious_Contract
4162 and then not All_Present (N)
4163 and then Nkind (Cond) = N_If_Expression
4164 and then No_Else_Or_Trivial_True (Cond)
4165 then
4166 Error_Msg_N ("?T?suspicious expression", N);
4167 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4168 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4169 end if;
4170 end Analyze_Quantified_Expression;
4172 -------------------
4173 -- Analyze_Range --
4174 -------------------
4176 procedure Analyze_Range (N : Node_Id) is
4177 L : constant Node_Id := Low_Bound (N);
4178 H : constant Node_Id := High_Bound (N);
4179 I1, I2 : Interp_Index;
4180 It1, It2 : Interp;
4182 procedure Check_Common_Type (T1, T2 : Entity_Id);
4183 -- Verify the compatibility of two types, and choose the
4184 -- non universal one if the other is universal.
4186 procedure Check_High_Bound (T : Entity_Id);
4187 -- Test one interpretation of the low bound against all those
4188 -- of the high bound.
4190 procedure Check_Universal_Expression (N : Node_Id);
4191 -- In Ada 83, reject bounds of a universal range that are not literals
4192 -- or entity names.
4194 -----------------------
4195 -- Check_Common_Type --
4196 -----------------------
4198 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4199 begin
4200 if Covers (T1 => T1, T2 => T2)
4201 or else
4202 Covers (T1 => T2, T2 => T1)
4203 then
4204 if T1 = Universal_Integer
4205 or else T1 = Universal_Real
4206 or else T1 = Any_Character
4207 then
4208 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4210 elsif T1 = T2 then
4211 Add_One_Interp (N, T1, T1);
4213 else
4214 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4215 end if;
4216 end if;
4217 end Check_Common_Type;
4219 ----------------------
4220 -- Check_High_Bound --
4221 ----------------------
4223 procedure Check_High_Bound (T : Entity_Id) is
4224 begin
4225 if not Is_Overloaded (H) then
4226 Check_Common_Type (T, Etype (H));
4227 else
4228 Get_First_Interp (H, I2, It2);
4229 while Present (It2.Typ) loop
4230 Check_Common_Type (T, It2.Typ);
4231 Get_Next_Interp (I2, It2);
4232 end loop;
4233 end if;
4234 end Check_High_Bound;
4236 -----------------------------
4237 -- Is_Universal_Expression --
4238 -----------------------------
4240 procedure Check_Universal_Expression (N : Node_Id) is
4241 begin
4242 if Etype (N) = Universal_Integer
4243 and then Nkind (N) /= N_Integer_Literal
4244 and then not Is_Entity_Name (N)
4245 and then Nkind (N) /= N_Attribute_Reference
4246 then
4247 Error_Msg_N ("illegal bound in discrete range", N);
4248 end if;
4249 end Check_Universal_Expression;
4251 -- Start of processing for Analyze_Range
4253 begin
4254 Set_Etype (N, Any_Type);
4255 Analyze_Expression (L);
4256 Analyze_Expression (H);
4258 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4259 return;
4261 else
4262 if not Is_Overloaded (L) then
4263 Check_High_Bound (Etype (L));
4264 else
4265 Get_First_Interp (L, I1, It1);
4266 while Present (It1.Typ) loop
4267 Check_High_Bound (It1.Typ);
4268 Get_Next_Interp (I1, It1);
4269 end loop;
4270 end if;
4272 -- If result is Any_Type, then we did not find a compatible pair
4274 if Etype (N) = Any_Type then
4275 Error_Msg_N ("incompatible types in range ", N);
4276 end if;
4277 end if;
4279 if Ada_Version = Ada_83
4280 and then
4281 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4282 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4283 then
4284 Check_Universal_Expression (L);
4285 Check_Universal_Expression (H);
4286 end if;
4288 Check_Function_Writable_Actuals (N);
4289 end Analyze_Range;
4291 -----------------------
4292 -- Analyze_Reference --
4293 -----------------------
4295 procedure Analyze_Reference (N : Node_Id) is
4296 P : constant Node_Id := Prefix (N);
4297 E : Entity_Id;
4298 T : Entity_Id;
4299 Acc_Type : Entity_Id;
4301 begin
4302 Analyze (P);
4304 -- An interesting error check, if we take the 'Ref of an object for
4305 -- which a pragma Atomic or Volatile has been given, and the type of the
4306 -- object is not Atomic or Volatile, then we are in trouble. The problem
4307 -- is that no trace of the atomic/volatile status will remain for the
4308 -- backend to respect when it deals with the resulting pointer, since
4309 -- the pointer type will not be marked atomic (it is a pointer to the
4310 -- base type of the object).
4312 -- It is not clear if that can ever occur, but in case it does, we will
4313 -- generate an error message. Not clear if this message can ever be
4314 -- generated, and pretty clear that it represents a bug if it is, still
4315 -- seems worth checking, except in CodePeer mode where we do not really
4316 -- care and don't want to bother the user.
4318 T := Etype (P);
4320 if Is_Entity_Name (P)
4321 and then Is_Object_Reference (P)
4322 and then not CodePeer_Mode
4323 then
4324 E := Entity (P);
4325 T := Etype (P);
4327 if (Has_Atomic_Components (E)
4328 and then not Has_Atomic_Components (T))
4329 or else
4330 (Has_Volatile_Components (E)
4331 and then not Has_Volatile_Components (T))
4332 or else (Is_Atomic (E) and then not Is_Atomic (T))
4333 or else (Is_Volatile (E) and then not Is_Volatile (T))
4334 then
4335 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4336 end if;
4337 end if;
4339 -- Carry on with normal processing
4341 Acc_Type := Create_Itype (E_Allocator_Type, N);
4342 Set_Etype (Acc_Type, Acc_Type);
4343 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4344 Set_Etype (N, Acc_Type);
4345 end Analyze_Reference;
4347 --------------------------------
4348 -- Analyze_Selected_Component --
4349 --------------------------------
4351 -- Prefix is a record type or a task or protected type. In the latter case,
4352 -- the selector must denote a visible entry.
4354 procedure Analyze_Selected_Component (N : Node_Id) is
4355 Name : constant Node_Id := Prefix (N);
4356 Sel : constant Node_Id := Selector_Name (N);
4357 Act_Decl : Node_Id;
4358 Comp : Entity_Id;
4359 Has_Candidate : Boolean := False;
4360 Hidden_Comp : Entity_Id;
4361 In_Scope : Boolean;
4362 Is_Private_Op : Boolean;
4363 Parent_N : Node_Id;
4364 Pent : Entity_Id := Empty;
4365 Prefix_Type : Entity_Id;
4367 Type_To_Use : Entity_Id;
4368 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4369 -- a class-wide type, we use its root type, whose components are
4370 -- present in the class-wide type.
4372 Is_Single_Concurrent_Object : Boolean;
4373 -- Set True if the prefix is a single task or a single protected object
4375 procedure Find_Component_In_Instance (Rec : Entity_Id);
4376 -- In an instance, a component of a private extension may not be visible
4377 -- while it was visible in the generic. Search candidate scope for a
4378 -- component with the proper identifier. This is only done if all other
4379 -- searches have failed. If a match is found, the Etype of both N and
4380 -- Sel are set from this component, and the entity of Sel is set to
4381 -- reference this component. If no match is found, Entity (Sel) remains
4382 -- unset. For a derived type that is an actual of the instance, the
4383 -- desired component may be found in any ancestor.
4385 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4386 -- It is known that the parent of N denotes a subprogram call. Comp
4387 -- is an overloadable component of the concurrent type of the prefix.
4388 -- Determine whether all formals of the parent of N and Comp are mode
4389 -- conformant. If the parent node is not analyzed yet it may be an
4390 -- indexed component rather than a function call.
4392 function Has_Dereference (Nod : Node_Id) return Boolean;
4393 -- Check whether prefix includes a dereference at any level.
4395 --------------------------------
4396 -- Find_Component_In_Instance --
4397 --------------------------------
4399 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4400 Comp : Entity_Id;
4401 Typ : Entity_Id;
4403 begin
4404 Typ := Rec;
4405 while Present (Typ) loop
4406 Comp := First_Component (Typ);
4407 while Present (Comp) loop
4408 if Chars (Comp) = Chars (Sel) then
4409 Set_Entity_With_Checks (Sel, Comp);
4410 Set_Etype (Sel, Etype (Comp));
4411 Set_Etype (N, Etype (Comp));
4412 return;
4413 end if;
4415 Next_Component (Comp);
4416 end loop;
4418 -- If not found, the component may be declared in the parent
4419 -- type or its full view, if any.
4421 if Is_Derived_Type (Typ) then
4422 Typ := Etype (Typ);
4424 if Is_Private_Type (Typ) then
4425 Typ := Full_View (Typ);
4426 end if;
4428 else
4429 return;
4430 end if;
4431 end loop;
4433 -- If we fall through, no match, so no changes made
4435 return;
4436 end Find_Component_In_Instance;
4438 ------------------------------
4439 -- Has_Mode_Conformant_Spec --
4440 ------------------------------
4442 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4443 Comp_Param : Entity_Id;
4444 Param : Node_Id;
4445 Param_Typ : Entity_Id;
4447 begin
4448 Comp_Param := First_Formal (Comp);
4450 if Nkind (Parent (N)) = N_Indexed_Component then
4451 Param := First (Expressions (Parent (N)));
4452 else
4453 Param := First (Parameter_Associations (Parent (N)));
4454 end if;
4456 while Present (Comp_Param)
4457 and then Present (Param)
4458 loop
4459 Param_Typ := Find_Parameter_Type (Param);
4461 if Present (Param_Typ)
4462 and then
4463 not Conforming_Types
4464 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4465 then
4466 return False;
4467 end if;
4469 Next_Formal (Comp_Param);
4470 Next (Param);
4471 end loop;
4473 -- One of the specs has additional formals; there is no match, unless
4474 -- this may be an indexing of a parameterless call.
4476 -- Note that when expansion is disabled, the corresponding record
4477 -- type of synchronized types is not constructed, so that there is
4478 -- no point is attempting an interpretation as a prefixed call, as
4479 -- this is bound to fail because the primitive operations will not
4480 -- be properly located.
4482 if Present (Comp_Param) or else Present (Param) then
4483 if Needs_No_Actuals (Comp)
4484 and then Is_Array_Type (Etype (Comp))
4485 and then not Expander_Active
4486 then
4487 return True;
4488 else
4489 return False;
4490 end if;
4491 end if;
4493 return True;
4494 end Has_Mode_Conformant_Spec;
4496 ---------------------
4497 -- Has_Dereference --
4498 ---------------------
4500 function Has_Dereference (Nod : Node_Id) return Boolean is
4501 begin
4502 if Nkind (Nod) = N_Explicit_Dereference then
4503 return True;
4505 -- When expansion is disabled an explicit dereference may not have
4506 -- been inserted, but if this is an access type the indirection makes
4507 -- the call safe.
4509 elsif Is_Access_Type (Etype (Nod)) then
4510 return True;
4512 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4513 return Has_Dereference (Prefix (Nod));
4515 else
4516 return False;
4517 end if;
4518 end Has_Dereference;
4520 -- Start of processing for Analyze_Selected_Component
4522 begin
4523 Set_Etype (N, Any_Type);
4525 if Is_Overloaded (Name) then
4526 Analyze_Overloaded_Selected_Component (N);
4527 return;
4529 elsif Etype (Name) = Any_Type then
4530 Set_Entity (Sel, Any_Id);
4531 Set_Etype (Sel, Any_Type);
4532 return;
4534 else
4535 Prefix_Type := Etype (Name);
4536 end if;
4538 if Is_Access_Type (Prefix_Type) then
4540 -- A RACW object can never be used as prefix of a selected component
4541 -- since that means it is dereferenced without being a controlling
4542 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4543 -- reporting an error, we must check whether this is actually a
4544 -- dispatching call in prefix form.
4546 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4547 and then Comes_From_Source (N)
4548 then
4549 if Try_Object_Operation (N) then
4550 return;
4551 else
4552 Error_Msg_N
4553 ("invalid dereference of a remote access-to-class-wide value",
4555 end if;
4557 -- Normal case of selected component applied to access type
4559 else
4560 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4562 if Is_Entity_Name (Name) then
4563 Pent := Entity (Name);
4564 elsif Nkind (Name) = N_Selected_Component
4565 and then Is_Entity_Name (Selector_Name (Name))
4566 then
4567 Pent := Entity (Selector_Name (Name));
4568 end if;
4570 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4571 end if;
4573 -- If we have an explicit dereference of a remote access-to-class-wide
4574 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4575 -- have to check for the case of a prefix that is a controlling operand
4576 -- of a prefixed dispatching call, as the dereference is legal in that
4577 -- case. Normally this condition is checked in Validate_Remote_Access_
4578 -- To_Class_Wide_Type, but we have to defer the checking for selected
4579 -- component prefixes because of the prefixed dispatching call case.
4580 -- Note that implicit dereferences are checked for this just above.
4582 elsif Nkind (Name) = N_Explicit_Dereference
4583 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4584 and then Comes_From_Source (N)
4585 then
4586 if Try_Object_Operation (N) then
4587 return;
4588 else
4589 Error_Msg_N
4590 ("invalid dereference of a remote access-to-class-wide value",
4592 end if;
4593 end if;
4595 -- (Ada 2005): if the prefix is the limited view of a type, and
4596 -- the context already includes the full view, use the full view
4597 -- in what follows, either to retrieve a component of to find
4598 -- a primitive operation. If the prefix is an explicit dereference,
4599 -- set the type of the prefix to reflect this transformation.
4600 -- If the nonlimited view is itself an incomplete type, get the
4601 -- full view if available.
4603 if From_Limited_With (Prefix_Type)
4604 and then Has_Non_Limited_View (Prefix_Type)
4605 then
4606 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4608 if Nkind (N) = N_Explicit_Dereference then
4609 Set_Etype (Prefix (N), Prefix_Type);
4610 end if;
4611 end if;
4613 if Ekind (Prefix_Type) = E_Private_Subtype then
4614 Prefix_Type := Base_Type (Prefix_Type);
4615 end if;
4617 Type_To_Use := Prefix_Type;
4619 -- For class-wide types, use the entity list of the root type. This
4620 -- indirection is specially important for private extensions because
4621 -- only the root type get switched (not the class-wide type).
4623 if Is_Class_Wide_Type (Prefix_Type) then
4624 Type_To_Use := Root_Type (Prefix_Type);
4625 end if;
4627 -- If the prefix is a single concurrent object, use its name in error
4628 -- messages, rather than that of its anonymous type.
4630 Is_Single_Concurrent_Object :=
4631 Is_Concurrent_Type (Prefix_Type)
4632 and then Is_Internal_Name (Chars (Prefix_Type))
4633 and then not Is_Derived_Type (Prefix_Type)
4634 and then Is_Entity_Name (Name);
4636 Comp := First_Entity (Type_To_Use);
4638 -- If the selector has an original discriminant, the node appears in
4639 -- an instance. Replace the discriminant with the corresponding one
4640 -- in the current discriminated type. For nested generics, this must
4641 -- be done transitively, so note the new original discriminant.
4643 if Nkind (Sel) = N_Identifier
4644 and then In_Instance
4645 and then Present (Original_Discriminant (Sel))
4646 then
4647 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4649 -- Mark entity before rewriting, for completeness and because
4650 -- subsequent semantic checks might examine the original node.
4652 Set_Entity (Sel, Comp);
4653 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4654 Set_Original_Discriminant (Selector_Name (N), Comp);
4655 Set_Etype (N, Etype (Comp));
4656 Check_Implicit_Dereference (N, Etype (Comp));
4658 if Is_Access_Type (Etype (Name)) then
4659 Insert_Explicit_Dereference (Name);
4660 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4661 end if;
4663 elsif Is_Record_Type (Prefix_Type) then
4665 -- Find component with given name. In an instance, if the node is
4666 -- known as a prefixed call, do not examine components whose
4667 -- visibility may be accidental.
4669 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4670 if Chars (Comp) = Chars (Sel)
4671 and then Is_Visible_Component (Comp, N)
4672 then
4673 Set_Entity_With_Checks (Sel, Comp);
4674 Set_Etype (Sel, Etype (Comp));
4676 if Ekind (Comp) = E_Discriminant then
4677 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4678 Error_Msg_N
4679 ("cannot reference discriminant of unchecked union",
4680 Sel);
4681 end if;
4683 if Is_Generic_Type (Prefix_Type)
4684 or else
4685 Is_Generic_Type (Root_Type (Prefix_Type))
4686 then
4687 Set_Original_Discriminant (Sel, Comp);
4688 end if;
4689 end if;
4691 -- Resolve the prefix early otherwise it is not possible to
4692 -- build the actual subtype of the component: it may need
4693 -- to duplicate this prefix and duplication is only allowed
4694 -- on fully resolved expressions.
4696 Resolve (Name);
4698 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4699 -- subtypes in a package specification.
4700 -- Example:
4702 -- limited with Pkg;
4703 -- package Pkg is
4704 -- type Acc_Inc is access Pkg.T;
4705 -- X : Acc_Inc;
4706 -- N : Natural := X.all.Comp; -- ERROR, limited view
4707 -- end Pkg; -- Comp is not visible
4709 if Nkind (Name) = N_Explicit_Dereference
4710 and then From_Limited_With (Etype (Prefix (Name)))
4711 and then not Is_Potentially_Use_Visible (Etype (Name))
4712 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4713 N_Package_Specification
4714 then
4715 Error_Msg_NE
4716 ("premature usage of incomplete}", Prefix (Name),
4717 Etype (Prefix (Name)));
4718 end if;
4720 -- We never need an actual subtype for the case of a selection
4721 -- for a indexed component of a non-packed array, since in
4722 -- this case gigi generates all the checks and can find the
4723 -- necessary bounds information.
4725 -- We also do not need an actual subtype for the case of a
4726 -- first, last, length, or range attribute applied to a
4727 -- non-packed array, since gigi can again get the bounds in
4728 -- these cases (gigi cannot handle the packed case, since it
4729 -- has the bounds of the packed array type, not the original
4730 -- bounds of the type). However, if the prefix is itself a
4731 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4732 -- as a dynamic-sized temporary, so we do generate an actual
4733 -- subtype for this case.
4735 Parent_N := Parent (N);
4737 if not Is_Packed (Etype (Comp))
4738 and then
4739 ((Nkind (Parent_N) = N_Indexed_Component
4740 and then Nkind (Name) /= N_Selected_Component)
4741 or else
4742 (Nkind (Parent_N) = N_Attribute_Reference
4743 and then
4744 Nam_In (Attribute_Name (Parent_N), Name_First,
4745 Name_Last,
4746 Name_Length,
4747 Name_Range)))
4748 then
4749 Set_Etype (N, Etype (Comp));
4751 -- If full analysis is not enabled, we do not generate an
4752 -- actual subtype, because in the absence of expansion
4753 -- reference to a formal of a protected type, for example,
4754 -- will not be properly transformed, and will lead to
4755 -- out-of-scope references in gigi.
4757 -- In all other cases, we currently build an actual subtype.
4758 -- It seems likely that many of these cases can be avoided,
4759 -- but right now, the front end makes direct references to the
4760 -- bounds (e.g. in generating a length check), and if we do
4761 -- not make an actual subtype, we end up getting a direct
4762 -- reference to a discriminant, which will not do.
4764 elsif Full_Analysis then
4765 Act_Decl :=
4766 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4767 Insert_Action (N, Act_Decl);
4769 if No (Act_Decl) then
4770 Set_Etype (N, Etype (Comp));
4772 else
4773 -- Component type depends on discriminants. Enter the
4774 -- main attributes of the subtype.
4776 declare
4777 Subt : constant Entity_Id :=
4778 Defining_Identifier (Act_Decl);
4780 begin
4781 Set_Etype (Subt, Base_Type (Etype (Comp)));
4782 Set_Ekind (Subt, Ekind (Etype (Comp)));
4783 Set_Etype (N, Subt);
4784 end;
4785 end if;
4787 -- If Full_Analysis not enabled, just set the Etype
4789 else
4790 Set_Etype (N, Etype (Comp));
4791 end if;
4793 Check_Implicit_Dereference (N, Etype (N));
4794 return;
4795 end if;
4797 -- If the prefix is a private extension, check only the visible
4798 -- components of the partial view. This must include the tag,
4799 -- which can appear in expanded code in a tag check.
4801 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4802 and then Chars (Selector_Name (N)) /= Name_uTag
4803 then
4804 exit when Comp = Last_Entity (Type_To_Use);
4805 end if;
4807 Next_Entity (Comp);
4808 end loop;
4810 -- Ada 2005 (AI-252): The selected component can be interpreted as
4811 -- a prefixed view of a subprogram. Depending on the context, this is
4812 -- either a name that can appear in a renaming declaration, or part
4813 -- of an enclosing call given in prefix form.
4815 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4816 -- selected component should resolve to a name.
4818 if Ada_Version >= Ada_2005
4819 and then Is_Tagged_Type (Prefix_Type)
4820 and then not Is_Concurrent_Type (Prefix_Type)
4821 then
4822 if Nkind (Parent (N)) = N_Generic_Association
4823 or else Nkind (Parent (N)) = N_Requeue_Statement
4824 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4825 then
4826 if Find_Primitive_Operation (N) then
4827 return;
4828 end if;
4830 elsif Try_Object_Operation (N) then
4831 return;
4832 end if;
4834 -- If the transformation fails, it will be necessary to redo the
4835 -- analysis with all errors enabled, to indicate candidate
4836 -- interpretations and reasons for each failure ???
4838 end if;
4840 elsif Is_Private_Type (Prefix_Type) then
4842 -- Allow access only to discriminants of the type. If the type has
4843 -- no full view, gigi uses the parent type for the components, so we
4844 -- do the same here.
4846 if No (Full_View (Prefix_Type)) then
4847 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4848 Comp := First_Entity (Type_To_Use);
4849 end if;
4851 while Present (Comp) loop
4852 if Chars (Comp) = Chars (Sel) then
4853 if Ekind (Comp) = E_Discriminant then
4854 Set_Entity_With_Checks (Sel, Comp);
4855 Generate_Reference (Comp, Sel);
4857 Set_Etype (Sel, Etype (Comp));
4858 Set_Etype (N, Etype (Comp));
4859 Check_Implicit_Dereference (N, Etype (N));
4861 if Is_Generic_Type (Prefix_Type)
4862 or else Is_Generic_Type (Root_Type (Prefix_Type))
4863 then
4864 Set_Original_Discriminant (Sel, Comp);
4865 end if;
4867 -- Before declaring an error, check whether this is tagged
4868 -- private type and a call to a primitive operation.
4870 elsif Ada_Version >= Ada_2005
4871 and then Is_Tagged_Type (Prefix_Type)
4872 and then Try_Object_Operation (N)
4873 then
4874 return;
4876 else
4877 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4878 Error_Msg_NE ("invisible selector& for }", N, Sel);
4879 Set_Entity (Sel, Any_Id);
4880 Set_Etype (N, Any_Type);
4881 end if;
4883 return;
4884 end if;
4886 Next_Entity (Comp);
4887 end loop;
4889 elsif Is_Concurrent_Type (Prefix_Type) then
4891 -- Find visible operation with given name. For a protected type,
4892 -- the possible candidates are discriminants, entries or protected
4893 -- subprograms. For a task type, the set can only include entries or
4894 -- discriminants if the task type is not an enclosing scope. If it
4895 -- is an enclosing scope (e.g. in an inner task) then all entities
4896 -- are visible, but the prefix must denote the enclosing scope, i.e.
4897 -- can only be a direct name or an expanded name.
4899 Set_Etype (Sel, Any_Type);
4900 Hidden_Comp := Empty;
4901 In_Scope := In_Open_Scopes (Prefix_Type);
4902 Is_Private_Op := False;
4904 while Present (Comp) loop
4906 -- Do not examine private operations of the type if not within
4907 -- its scope.
4909 if Chars (Comp) = Chars (Sel) then
4910 if Is_Overloadable (Comp)
4911 and then (In_Scope
4912 or else Comp /= First_Private_Entity (Type_To_Use))
4913 then
4914 Add_One_Interp (Sel, Comp, Etype (Comp));
4915 if Comp = First_Private_Entity (Type_To_Use) then
4916 Is_Private_Op := True;
4917 end if;
4919 -- If the prefix is tagged, the correct interpretation may
4920 -- lie in the primitive or class-wide operations of the
4921 -- type. Perform a simple conformance check to determine
4922 -- whether Try_Object_Operation should be invoked even if
4923 -- a visible entity is found.
4925 if Is_Tagged_Type (Prefix_Type)
4926 and then Nkind_In (Parent (N), N_Function_Call,
4927 N_Indexed_Component,
4928 N_Procedure_Call_Statement)
4929 and then Has_Mode_Conformant_Spec (Comp)
4930 then
4931 Has_Candidate := True;
4932 end if;
4934 -- Note: a selected component may not denote a component of a
4935 -- protected type (4.1.3(7)).
4937 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4938 or else (In_Scope
4939 and then not Is_Protected_Type (Prefix_Type)
4940 and then Is_Entity_Name (Name))
4941 then
4942 Set_Entity_With_Checks (Sel, Comp);
4943 Generate_Reference (Comp, Sel);
4945 -- The selector is not overloadable, so we have a candidate
4946 -- interpretation.
4948 Has_Candidate := True;
4950 else
4951 if Ekind (Comp) = E_Component then
4952 Hidden_Comp := Comp;
4953 end if;
4955 goto Next_Comp;
4956 end if;
4958 Set_Etype (Sel, Etype (Comp));
4959 Set_Etype (N, Etype (Comp));
4961 if Ekind (Comp) = E_Discriminant then
4962 Set_Original_Discriminant (Sel, Comp);
4963 end if;
4965 -- For access type case, introduce explicit dereference for
4966 -- more uniform treatment of entry calls.
4968 if Is_Access_Type (Etype (Name)) then
4969 Insert_Explicit_Dereference (Name);
4970 Error_Msg_NW
4971 (Warn_On_Dereference, "?d?implicit dereference", N);
4972 end if;
4973 end if;
4975 <<Next_Comp>>
4976 if Comp = First_Private_Entity (Type_To_Use) then
4977 if Etype (Sel) /= Any_Type then
4979 -- We have a candiate
4981 exit;
4983 else
4984 -- Indicate that subsequent operations are private,
4985 -- for better error reporting.
4987 Is_Private_Op := True;
4988 end if;
4989 end if;
4991 Next_Entity (Comp);
4992 exit when not In_Scope
4993 and then
4994 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4995 end loop;
4997 -- If the scope is a current instance, the prefix cannot be an
4998 -- expression of the same type, unless the selector designates a
4999 -- public operation (otherwise that would represent an attempt to
5000 -- reach an internal entity of another synchronized object).
5002 -- This is legal if prefix is an access to such type and there is
5003 -- a dereference, or is a component with a dereferenced prefix.
5004 -- It is also legal if the prefix is a component of a task type,
5005 -- and the selector is one of the task operations.
5007 if In_Scope
5008 and then not Is_Entity_Name (Name)
5009 and then not Has_Dereference (Name)
5010 then
5011 if Is_Task_Type (Prefix_Type)
5012 and then Present (Entity (Sel))
5013 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
5014 then
5015 null;
5017 elsif Is_Protected_Type (Prefix_Type)
5018 and then Is_Overloadable (Entity (Sel))
5019 and then not Is_Private_Op
5020 then
5021 null;
5023 else
5024 Error_Msg_NE
5025 ("invalid reference to internal operation of some object of "
5026 & "type &", N, Type_To_Use);
5027 Set_Entity (Sel, Any_Id);
5028 Set_Etype (Sel, Any_Type);
5029 return;
5030 end if;
5032 -- Another special case: the prefix may denote an object of the type
5033 -- (but not a type) in which case this is an external call and the
5034 -- operation must be public.
5036 elsif In_Scope
5037 and then Is_Object_Reference (Original_Node (Prefix (N)))
5038 and then Comes_From_Source (N)
5039 and then Is_Private_Op
5040 then
5041 if Present (Hidden_Comp) then
5042 Error_Msg_NE
5043 ("invalid reference to private component of object of type "
5044 & "&", N, Type_To_Use);
5046 else
5047 Error_Msg_NE
5048 ("invalid reference to private operation of some object of "
5049 & "type &", N, Type_To_Use);
5050 end if;
5052 Set_Entity (Sel, Any_Id);
5053 Set_Etype (Sel, Any_Type);
5054 return;
5055 end if;
5057 -- If there is no visible entity with the given name or none of the
5058 -- visible entities are plausible interpretations, check whether
5059 -- there is some other primitive operation with that name.
5061 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
5062 if (Etype (N) = Any_Type
5063 or else not Has_Candidate)
5064 and then Try_Object_Operation (N)
5065 then
5066 return;
5068 -- If the context is not syntactically a procedure call, it
5069 -- may be a call to a primitive function declared outside of
5070 -- the synchronized type.
5072 -- If the context is a procedure call, there might still be
5073 -- an overloading between an entry and a primitive procedure
5074 -- declared outside of the synchronized type, called in prefix
5075 -- notation. This is harder to disambiguate because in one case
5076 -- the controlling formal is implicit ???
5078 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
5079 and then Nkind (Parent (N)) /= N_Indexed_Component
5080 and then Try_Object_Operation (N)
5081 then
5082 return;
5083 end if;
5085 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5086 -- entry or procedure of a tagged concurrent type we must check
5087 -- if there are class-wide subprograms covering the primitive. If
5088 -- true then Try_Object_Operation reports the error.
5090 if Has_Candidate
5091 and then Is_Concurrent_Type (Prefix_Type)
5092 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
5093 then
5094 -- Duplicate the call. This is required to avoid problems with
5095 -- the tree transformations performed by Try_Object_Operation.
5096 -- Set properly the parent of the copied call, because it is
5097 -- about to be reanalyzed.
5099 declare
5100 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5102 begin
5103 Set_Parent (Par, Parent (Parent (N)));
5105 if Try_Object_Operation
5106 (Sinfo.Name (Par), CW_Test_Only => True)
5107 then
5108 return;
5109 end if;
5110 end;
5111 end if;
5112 end if;
5114 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
5116 -- Case of a prefix of a protected type: selector might denote
5117 -- an invisible private component.
5119 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5120 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5121 Next_Entity (Comp);
5122 end loop;
5124 if Present (Comp) then
5125 if Is_Single_Concurrent_Object then
5126 Error_Msg_Node_2 := Entity (Name);
5127 Error_Msg_NE ("invisible selector& for &", N, Sel);
5129 else
5130 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5131 Error_Msg_NE ("invisible selector& for }", N, Sel);
5132 end if;
5133 return;
5134 end if;
5135 end if;
5137 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5139 else
5140 -- Invalid prefix
5142 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5143 end if;
5145 -- If N still has no type, the component is not defined in the prefix
5147 if Etype (N) = Any_Type then
5149 if Is_Single_Concurrent_Object then
5150 Error_Msg_Node_2 := Entity (Name);
5151 Error_Msg_NE ("no selector& for&", N, Sel);
5153 Check_Misspelled_Selector (Type_To_Use, Sel);
5155 -- If this is a derived formal type, the parent may have different
5156 -- visibility at this point. Try for an inherited component before
5157 -- reporting an error.
5159 elsif Is_Generic_Type (Prefix_Type)
5160 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5161 and then Prefix_Type /= Etype (Prefix_Type)
5162 and then Is_Record_Type (Etype (Prefix_Type))
5163 then
5164 Set_Etype (Prefix (N), Etype (Prefix_Type));
5165 Analyze_Selected_Component (N);
5166 return;
5168 -- Similarly, if this is the actual for a formal derived type, or
5169 -- a derived type thereof, the component inherited from the generic
5170 -- parent may not be visible in the actual, but the selected
5171 -- component is legal. Climb up the derivation chain of the generic
5172 -- parent type until we find the proper ancestor type.
5174 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5175 declare
5176 Par : Entity_Id := Prefix_Type;
5177 begin
5178 -- Climb up derivation chain to generic actual subtype
5180 while not Is_Generic_Actual_Type (Par) loop
5181 if Ekind (Par) = E_Record_Type then
5182 Par := Parent_Subtype (Par);
5183 exit when No (Par);
5184 else
5185 exit when Par = Etype (Par);
5186 Par := Etype (Par);
5187 end if;
5188 end loop;
5190 if Present (Par) and then Is_Generic_Actual_Type (Par) then
5192 -- Now look for component in ancestor types
5194 Par := Generic_Parent_Type (Declaration_Node (Par));
5195 loop
5196 Find_Component_In_Instance (Par);
5197 exit when Present (Entity (Sel))
5198 or else Par = Etype (Par);
5199 Par := Etype (Par);
5200 end loop;
5202 -- Another special case: the type is an extension of a private
5203 -- type T, is an actual in an instance, and we are in the body
5204 -- of the instance, so the generic body had a full view of the
5205 -- type declaration for T or of some ancestor that defines the
5206 -- component in question.
5208 elsif Is_Derived_Type (Type_To_Use)
5209 and then Used_As_Generic_Actual (Type_To_Use)
5210 and then In_Instance_Body
5211 then
5212 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5214 -- In ASIS mode the generic parent type may be absent. Examine
5215 -- the parent type directly for a component that may have been
5216 -- visible in a parent generic unit.
5218 elsif Is_Derived_Type (Prefix_Type) then
5219 Par := Etype (Prefix_Type);
5220 Find_Component_In_Instance (Par);
5221 end if;
5222 end;
5224 -- The search above must have eventually succeeded, since the
5225 -- selected component was legal in the generic.
5227 if No (Entity (Sel)) then
5228 raise Program_Error;
5229 end if;
5231 return;
5233 -- Component not found, specialize error message when appropriate
5235 else
5236 if Ekind (Prefix_Type) = E_Record_Subtype then
5238 -- Check whether this is a component of the base type which
5239 -- is absent from a statically constrained subtype. This will
5240 -- raise constraint error at run time, but is not a compile-
5241 -- time error. When the selector is illegal for base type as
5242 -- well fall through and generate a compilation error anyway.
5244 Comp := First_Component (Base_Type (Prefix_Type));
5245 while Present (Comp) loop
5246 if Chars (Comp) = Chars (Sel)
5247 and then Is_Visible_Component (Comp)
5248 then
5249 Set_Entity_With_Checks (Sel, Comp);
5250 Generate_Reference (Comp, Sel);
5251 Set_Etype (Sel, Etype (Comp));
5252 Set_Etype (N, Etype (Comp));
5254 -- Emit appropriate message. The node will be replaced
5255 -- by an appropriate raise statement.
5257 -- Note that in SPARK mode, as with all calls to apply a
5258 -- compile time constraint error, this will be made into
5259 -- an error to simplify the processing of the formal
5260 -- verification backend.
5262 Apply_Compile_Time_Constraint_Error
5263 (N, "component not present in }??",
5264 CE_Discriminant_Check_Failed,
5265 Ent => Prefix_Type, Rep => False);
5267 Set_Raises_Constraint_Error (N);
5268 return;
5269 end if;
5271 Next_Component (Comp);
5272 end loop;
5274 end if;
5276 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5277 Error_Msg_NE ("no selector& for}", N, Sel);
5279 -- Add information in the case of an incomplete prefix
5281 if Is_Incomplete_Type (Type_To_Use) then
5282 declare
5283 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5285 begin
5286 if From_Limited_With (Scope (Type_To_Use)) then
5287 Error_Msg_NE
5288 ("\limited view of& has no components", N, Inc);
5290 else
5291 Error_Msg_NE
5292 ("\premature usage of incomplete type&", N, Inc);
5294 if Nkind (Parent (Inc)) =
5295 N_Incomplete_Type_Declaration
5296 then
5297 -- Record location of premature use in entity so that
5298 -- a continuation message is generated when the
5299 -- completion is seen.
5301 Set_Premature_Use (Parent (Inc), N);
5302 end if;
5303 end if;
5304 end;
5305 end if;
5307 Check_Misspelled_Selector (Type_To_Use, Sel);
5308 end if;
5310 Set_Entity (Sel, Any_Id);
5311 Set_Etype (Sel, Any_Type);
5312 end if;
5313 end Analyze_Selected_Component;
5315 ---------------------------
5316 -- Analyze_Short_Circuit --
5317 ---------------------------
5319 procedure Analyze_Short_Circuit (N : Node_Id) is
5320 L : constant Node_Id := Left_Opnd (N);
5321 R : constant Node_Id := Right_Opnd (N);
5322 Ind : Interp_Index;
5323 It : Interp;
5325 begin
5326 Analyze_Expression (L);
5327 Analyze_Expression (R);
5328 Set_Etype (N, Any_Type);
5330 if not Is_Overloaded (L) then
5331 if Root_Type (Etype (L)) = Standard_Boolean
5332 and then Has_Compatible_Type (R, Etype (L))
5333 then
5334 Add_One_Interp (N, Etype (L), Etype (L));
5335 end if;
5337 else
5338 Get_First_Interp (L, Ind, It);
5339 while Present (It.Typ) loop
5340 if Root_Type (It.Typ) = Standard_Boolean
5341 and then Has_Compatible_Type (R, It.Typ)
5342 then
5343 Add_One_Interp (N, It.Typ, It.Typ);
5344 end if;
5346 Get_Next_Interp (Ind, It);
5347 end loop;
5348 end if;
5350 -- Here we have failed to find an interpretation. Clearly we know that
5351 -- it is not the case that both operands can have an interpretation of
5352 -- Boolean, but this is by far the most likely intended interpretation.
5353 -- So we simply resolve both operands as Booleans, and at least one of
5354 -- these resolutions will generate an error message, and we do not need
5355 -- to give another error message on the short circuit operation itself.
5357 if Etype (N) = Any_Type then
5358 Resolve (L, Standard_Boolean);
5359 Resolve (R, Standard_Boolean);
5360 Set_Etype (N, Standard_Boolean);
5361 end if;
5362 end Analyze_Short_Circuit;
5364 -------------------
5365 -- Analyze_Slice --
5366 -------------------
5368 procedure Analyze_Slice (N : Node_Id) is
5369 D : constant Node_Id := Discrete_Range (N);
5370 P : constant Node_Id := Prefix (N);
5371 Array_Type : Entity_Id;
5372 Index_Type : Entity_Id;
5374 procedure Analyze_Overloaded_Slice;
5375 -- If the prefix is overloaded, select those interpretations that
5376 -- yield a one-dimensional array type.
5378 ------------------------------
5379 -- Analyze_Overloaded_Slice --
5380 ------------------------------
5382 procedure Analyze_Overloaded_Slice is
5383 I : Interp_Index;
5384 It : Interp;
5385 Typ : Entity_Id;
5387 begin
5388 Set_Etype (N, Any_Type);
5390 Get_First_Interp (P, I, It);
5391 while Present (It.Nam) loop
5392 Typ := It.Typ;
5394 if Is_Access_Type (Typ) then
5395 Typ := Designated_Type (Typ);
5396 Error_Msg_NW
5397 (Warn_On_Dereference, "?d?implicit dereference", N);
5398 end if;
5400 if Is_Array_Type (Typ)
5401 and then Number_Dimensions (Typ) = 1
5402 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5403 then
5404 Add_One_Interp (N, Typ, Typ);
5405 end if;
5407 Get_Next_Interp (I, It);
5408 end loop;
5410 if Etype (N) = Any_Type then
5411 Error_Msg_N ("expect array type in prefix of slice", N);
5412 end if;
5413 end Analyze_Overloaded_Slice;
5415 -- Start of processing for Analyze_Slice
5417 begin
5418 if Comes_From_Source (N) then
5419 Check_SPARK_05_Restriction ("slice is not allowed", N);
5420 end if;
5422 Analyze (P);
5423 Analyze (D);
5425 if Is_Overloaded (P) then
5426 Analyze_Overloaded_Slice;
5428 else
5429 Array_Type := Etype (P);
5430 Set_Etype (N, Any_Type);
5432 if Is_Access_Type (Array_Type) then
5433 Array_Type := Designated_Type (Array_Type);
5434 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5435 end if;
5437 if not Is_Array_Type (Array_Type) then
5438 Wrong_Type (P, Any_Array);
5440 elsif Number_Dimensions (Array_Type) > 1 then
5441 Error_Msg_N
5442 ("type is not one-dimensional array in slice prefix", N);
5444 else
5445 if Ekind (Array_Type) = E_String_Literal_Subtype then
5446 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5447 else
5448 Index_Type := Etype (First_Index (Array_Type));
5449 end if;
5451 if not Has_Compatible_Type (D, Index_Type) then
5452 Wrong_Type (D, Index_Type);
5453 else
5454 Set_Etype (N, Array_Type);
5455 end if;
5456 end if;
5457 end if;
5458 end Analyze_Slice;
5460 -----------------------------
5461 -- Analyze_Type_Conversion --
5462 -----------------------------
5464 procedure Analyze_Type_Conversion (N : Node_Id) is
5465 Expr : constant Node_Id := Expression (N);
5466 Typ : Entity_Id;
5468 begin
5469 -- If Conversion_OK is set, then the Etype is already set, and the only
5470 -- processing required is to analyze the expression. This is used to
5471 -- construct certain "illegal" conversions which are not allowed by Ada
5472 -- semantics, but can be handled by Gigi, see Sinfo for further details.
5474 if Conversion_OK (N) then
5475 Analyze (Expr);
5476 return;
5477 end if;
5479 -- Otherwise full type analysis is required, as well as some semantic
5480 -- checks to make sure the argument of the conversion is appropriate.
5482 Find_Type (Subtype_Mark (N));
5483 Typ := Entity (Subtype_Mark (N));
5484 Set_Etype (N, Typ);
5485 Check_Fully_Declared (Typ, N);
5486 Analyze_Expression (Expr);
5487 Validate_Remote_Type_Type_Conversion (N);
5489 -- Only remaining step is validity checks on the argument. These
5490 -- are skipped if the conversion does not come from the source.
5492 if not Comes_From_Source (N) then
5493 return;
5495 -- If there was an error in a generic unit, no need to replicate the
5496 -- error message. Conversely, constant-folding in the generic may
5497 -- transform the argument of a conversion into a string literal, which
5498 -- is legal. Therefore the following tests are not performed in an
5499 -- instance. The same applies to an inlined body.
5501 elsif In_Instance or In_Inlined_Body then
5502 return;
5504 elsif Nkind (Expr) = N_Null then
5505 Error_Msg_N ("argument of conversion cannot be null", N);
5506 Error_Msg_N ("\use qualified expression instead", N);
5507 Set_Etype (N, Any_Type);
5509 elsif Nkind (Expr) = N_Aggregate then
5510 Error_Msg_N ("argument of conversion cannot be aggregate", N);
5511 Error_Msg_N ("\use qualified expression instead", N);
5513 elsif Nkind (Expr) = N_Allocator then
5514 Error_Msg_N ("argument of conversion cannot be an allocator", N);
5515 Error_Msg_N ("\use qualified expression instead", N);
5517 elsif Nkind (Expr) = N_String_Literal then
5518 Error_Msg_N ("argument of conversion cannot be string literal", N);
5519 Error_Msg_N ("\use qualified expression instead", N);
5521 elsif Nkind (Expr) = N_Character_Literal then
5522 if Ada_Version = Ada_83 then
5523 Resolve (Expr, Typ);
5524 else
5525 Error_Msg_N ("argument of conversion cannot be character literal",
5527 Error_Msg_N ("\use qualified expression instead", N);
5528 end if;
5530 elsif Nkind (Expr) = N_Attribute_Reference
5531 and then Nam_In (Attribute_Name (Expr), Name_Access,
5532 Name_Unchecked_Access,
5533 Name_Unrestricted_Access)
5534 then
5535 Error_Msg_N ("argument of conversion cannot be access", N);
5536 Error_Msg_N ("\use qualified expression instead", N);
5537 end if;
5539 -- A formal parameter of a specific tagged type whose related subprogram
5540 -- is subject to pragma Extensions_Visible with value "False" cannot
5541 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5542 -- internally generated expressions.
5544 if Is_Class_Wide_Type (Typ)
5545 and then Comes_From_Source (Expr)
5546 and then Is_EVF_Expression (Expr)
5547 then
5548 Error_Msg_N
5549 ("formal parameter cannot be converted to class-wide type when "
5550 & "Extensions_Visible is False", Expr);
5551 end if;
5552 end Analyze_Type_Conversion;
5554 ----------------------
5555 -- Analyze_Unary_Op --
5556 ----------------------
5558 procedure Analyze_Unary_Op (N : Node_Id) is
5559 R : constant Node_Id := Right_Opnd (N);
5560 Op_Id : Entity_Id := Entity (N);
5562 begin
5563 Set_Etype (N, Any_Type);
5564 Candidate_Type := Empty;
5566 Analyze_Expression (R);
5568 if Present (Op_Id) then
5569 if Ekind (Op_Id) = E_Operator then
5570 Find_Unary_Types (R, Op_Id, N);
5571 else
5572 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5573 end if;
5575 else
5576 Op_Id := Get_Name_Entity_Id (Chars (N));
5577 while Present (Op_Id) loop
5578 if Ekind (Op_Id) = E_Operator then
5579 if No (Next_Entity (First_Entity (Op_Id))) then
5580 Find_Unary_Types (R, Op_Id, N);
5581 end if;
5583 elsif Is_Overloadable (Op_Id) then
5584 Analyze_User_Defined_Unary_Op (N, Op_Id);
5585 end if;
5587 Op_Id := Homonym (Op_Id);
5588 end loop;
5589 end if;
5591 Operator_Check (N);
5592 end Analyze_Unary_Op;
5594 ----------------------------------
5595 -- Analyze_Unchecked_Expression --
5596 ----------------------------------
5598 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5599 begin
5600 Analyze (Expression (N), Suppress => All_Checks);
5601 Set_Etype (N, Etype (Expression (N)));
5602 Save_Interps (Expression (N), N);
5603 end Analyze_Unchecked_Expression;
5605 ---------------------------------------
5606 -- Analyze_Unchecked_Type_Conversion --
5607 ---------------------------------------
5609 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5610 begin
5611 Find_Type (Subtype_Mark (N));
5612 Analyze_Expression (Expression (N));
5613 Set_Etype (N, Entity (Subtype_Mark (N)));
5614 end Analyze_Unchecked_Type_Conversion;
5616 ------------------------------------
5617 -- Analyze_User_Defined_Binary_Op --
5618 ------------------------------------
5620 procedure Analyze_User_Defined_Binary_Op
5621 (N : Node_Id;
5622 Op_Id : Entity_Id)
5624 begin
5625 -- Only do analysis if the operator Comes_From_Source, since otherwise
5626 -- the operator was generated by the expander, and all such operators
5627 -- always refer to the operators in package Standard.
5629 if Comes_From_Source (N) then
5630 declare
5631 F1 : constant Entity_Id := First_Formal (Op_Id);
5632 F2 : constant Entity_Id := Next_Formal (F1);
5634 begin
5635 -- Verify that Op_Id is a visible binary function. Note that since
5636 -- we know Op_Id is overloaded, potentially use visible means use
5637 -- visible for sure (RM 9.4(11)).
5639 if Ekind (Op_Id) = E_Function
5640 and then Present (F2)
5641 and then (Is_Immediately_Visible (Op_Id)
5642 or else Is_Potentially_Use_Visible (Op_Id))
5643 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5644 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5645 then
5646 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5648 -- If the left operand is overloaded, indicate that the current
5649 -- type is a viable candidate. This is redundant in most cases,
5650 -- but for equality and comparison operators where the context
5651 -- does not impose a type on the operands, setting the proper
5652 -- type is necessary to avoid subsequent ambiguities during
5653 -- resolution, when both user-defined and predefined operators
5654 -- may be candidates.
5656 if Is_Overloaded (Left_Opnd (N)) then
5657 Set_Etype (Left_Opnd (N), Etype (F1));
5658 end if;
5660 if Debug_Flag_E then
5661 Write_Str ("user defined operator ");
5662 Write_Name (Chars (Op_Id));
5663 Write_Str (" on node ");
5664 Write_Int (Int (N));
5665 Write_Eol;
5666 end if;
5667 end if;
5668 end;
5669 end if;
5670 end Analyze_User_Defined_Binary_Op;
5672 -----------------------------------
5673 -- Analyze_User_Defined_Unary_Op --
5674 -----------------------------------
5676 procedure Analyze_User_Defined_Unary_Op
5677 (N : Node_Id;
5678 Op_Id : Entity_Id)
5680 begin
5681 -- Only do analysis if the operator Comes_From_Source, since otherwise
5682 -- the operator was generated by the expander, and all such operators
5683 -- always refer to the operators in package Standard.
5685 if Comes_From_Source (N) then
5686 declare
5687 F : constant Entity_Id := First_Formal (Op_Id);
5689 begin
5690 -- Verify that Op_Id is a visible unary function. Note that since
5691 -- we know Op_Id is overloaded, potentially use visible means use
5692 -- visible for sure (RM 9.4(11)).
5694 if Ekind (Op_Id) = E_Function
5695 and then No (Next_Formal (F))
5696 and then (Is_Immediately_Visible (Op_Id)
5697 or else Is_Potentially_Use_Visible (Op_Id))
5698 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5699 then
5700 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5701 end if;
5702 end;
5703 end if;
5704 end Analyze_User_Defined_Unary_Op;
5706 ---------------------------
5707 -- Check_Arithmetic_Pair --
5708 ---------------------------
5710 procedure Check_Arithmetic_Pair
5711 (T1, T2 : Entity_Id;
5712 Op_Id : Entity_Id;
5713 N : Node_Id)
5715 Op_Name : constant Name_Id := Chars (Op_Id);
5717 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5718 -- Check whether the fixed-point type Typ has a user-defined operator
5719 -- (multiplication or division) that should hide the corresponding
5720 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5721 -- such operators more visible and therefore useful.
5723 -- If the name of the operation is an expanded name with prefix
5724 -- Standard, the predefined universal fixed operator is available,
5725 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5727 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5728 -- Get specific type (i.e. non-universal type if there is one)
5730 ------------------
5731 -- Has_Fixed_Op --
5732 ------------------
5734 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5735 Bas : constant Entity_Id := Base_Type (Typ);
5736 Ent : Entity_Id;
5737 F1 : Entity_Id;
5738 F2 : Entity_Id;
5740 begin
5741 -- If the universal_fixed operation is given explicitly the rule
5742 -- concerning primitive operations of the type do not apply.
5744 if Nkind (N) = N_Function_Call
5745 and then Nkind (Name (N)) = N_Expanded_Name
5746 and then Entity (Prefix (Name (N))) = Standard_Standard
5747 then
5748 return False;
5749 end if;
5751 -- The operation is treated as primitive if it is declared in the
5752 -- same scope as the type, and therefore on the same entity chain.
5754 Ent := Next_Entity (Typ);
5755 while Present (Ent) loop
5756 if Chars (Ent) = Chars (Op) then
5757 F1 := First_Formal (Ent);
5758 F2 := Next_Formal (F1);
5760 -- The operation counts as primitive if either operand or
5761 -- result are of the given base type, and both operands are
5762 -- fixed point types.
5764 if (Base_Type (Etype (F1)) = Bas
5765 and then Is_Fixed_Point_Type (Etype (F2)))
5767 or else
5768 (Base_Type (Etype (F2)) = Bas
5769 and then Is_Fixed_Point_Type (Etype (F1)))
5771 or else
5772 (Base_Type (Etype (Ent)) = Bas
5773 and then Is_Fixed_Point_Type (Etype (F1))
5774 and then Is_Fixed_Point_Type (Etype (F2)))
5775 then
5776 return True;
5777 end if;
5778 end if;
5780 Next_Entity (Ent);
5781 end loop;
5783 return False;
5784 end Has_Fixed_Op;
5786 -------------------
5787 -- Specific_Type --
5788 -------------------
5790 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5791 begin
5792 if T1 = Universal_Integer or else T1 = Universal_Real then
5793 return Base_Type (T2);
5794 else
5795 return Base_Type (T1);
5796 end if;
5797 end Specific_Type;
5799 -- Start of processing for Check_Arithmetic_Pair
5801 begin
5802 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5803 if Is_Numeric_Type (T1)
5804 and then Is_Numeric_Type (T2)
5805 and then (Covers (T1 => T1, T2 => T2)
5806 or else
5807 Covers (T1 => T2, T2 => T1))
5808 then
5809 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5810 end if;
5812 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5813 if Is_Fixed_Point_Type (T1)
5814 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5815 then
5816 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5817 -- and no further processing is required (this is the case of an
5818 -- operator constructed by Exp_Fixd for a fixed point operation)
5819 -- Otherwise add one interpretation with universal fixed result
5820 -- If the operator is given in functional notation, it comes
5821 -- from source and Fixed_As_Integer cannot apply.
5823 if (Nkind (N) not in N_Op
5824 or else not Treat_Fixed_As_Integer (N))
5825 and then
5826 (not Has_Fixed_Op (T1, Op_Id)
5827 or else Nkind (Parent (N)) = N_Type_Conversion)
5828 then
5829 Add_One_Interp (N, Op_Id, Universal_Fixed);
5830 end if;
5832 elsif Is_Fixed_Point_Type (T2)
5833 and then (Nkind (N) not in N_Op
5834 or else not Treat_Fixed_As_Integer (N))
5835 and then T1 = Universal_Real
5836 and then
5837 (not Has_Fixed_Op (T1, Op_Id)
5838 or else Nkind (Parent (N)) = N_Type_Conversion)
5839 then
5840 Add_One_Interp (N, Op_Id, Universal_Fixed);
5842 elsif Is_Numeric_Type (T1)
5843 and then Is_Numeric_Type (T2)
5844 and then (Covers (T1 => T1, T2 => T2)
5845 or else
5846 Covers (T1 => T2, T2 => T1))
5847 then
5848 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5850 elsif Is_Fixed_Point_Type (T1)
5851 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5852 or else T2 = Universal_Integer)
5853 then
5854 Add_One_Interp (N, Op_Id, T1);
5856 elsif T2 = Universal_Real
5857 and then Base_Type (T1) = Base_Type (Standard_Integer)
5858 and then Op_Name = Name_Op_Multiply
5859 then
5860 Add_One_Interp (N, Op_Id, Any_Fixed);
5862 elsif T1 = Universal_Real
5863 and then Base_Type (T2) = Base_Type (Standard_Integer)
5864 then
5865 Add_One_Interp (N, Op_Id, Any_Fixed);
5867 elsif Is_Fixed_Point_Type (T2)
5868 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5869 or else T1 = Universal_Integer)
5870 and then Op_Name = Name_Op_Multiply
5871 then
5872 Add_One_Interp (N, Op_Id, T2);
5874 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5875 Add_One_Interp (N, Op_Id, T1);
5877 elsif T2 = Universal_Real
5878 and then T1 = Universal_Integer
5879 and then Op_Name = Name_Op_Multiply
5880 then
5881 Add_One_Interp (N, Op_Id, T2);
5882 end if;
5884 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5886 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5887 -- set does not require any special processing, since the Etype is
5888 -- already set (case of operation constructed by Exp_Fixed).
5890 if Is_Integer_Type (T1)
5891 and then (Covers (T1 => T1, T2 => T2)
5892 or else
5893 Covers (T1 => T2, T2 => T1))
5894 then
5895 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5896 end if;
5898 elsif Op_Name = Name_Op_Expon then
5899 if Is_Numeric_Type (T1)
5900 and then not Is_Fixed_Point_Type (T1)
5901 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5902 or else T2 = Universal_Integer)
5903 then
5904 Add_One_Interp (N, Op_Id, Base_Type (T1));
5905 end if;
5907 else pragma Assert (Nkind (N) in N_Op_Shift);
5909 -- If not one of the predefined operators, the node may be one
5910 -- of the intrinsic functions. Its kind is always specific, and
5911 -- we can use it directly, rather than the name of the operation.
5913 if Is_Integer_Type (T1)
5914 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5915 or else T2 = Universal_Integer)
5916 then
5917 Add_One_Interp (N, Op_Id, Base_Type (T1));
5918 end if;
5919 end if;
5920 end Check_Arithmetic_Pair;
5922 -------------------------------
5923 -- Check_Misspelled_Selector --
5924 -------------------------------
5926 procedure Check_Misspelled_Selector
5927 (Prefix : Entity_Id;
5928 Sel : Node_Id)
5930 Max_Suggestions : constant := 2;
5931 Nr_Of_Suggestions : Natural := 0;
5933 Suggestion_1 : Entity_Id := Empty;
5934 Suggestion_2 : Entity_Id := Empty;
5936 Comp : Entity_Id;
5938 begin
5939 -- All the components of the prefix of selector Sel are matched against
5940 -- Sel and a count is maintained of possible misspellings. When at
5941 -- the end of the analysis there are one or two (not more) possible
5942 -- misspellings, these misspellings will be suggested as possible
5943 -- correction.
5945 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5947 -- Concurrent types should be handled as well ???
5949 return;
5950 end if;
5952 Comp := First_Entity (Prefix);
5953 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5954 if Is_Visible_Component (Comp) then
5955 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5956 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5958 case Nr_Of_Suggestions is
5959 when 1 => Suggestion_1 := Comp;
5960 when 2 => Suggestion_2 := Comp;
5961 when others => null;
5962 end case;
5963 end if;
5964 end if;
5966 Comp := Next_Entity (Comp);
5967 end loop;
5969 -- Report at most two suggestions
5971 if Nr_Of_Suggestions = 1 then
5972 Error_Msg_NE -- CODEFIX
5973 ("\possible misspelling of&", Sel, Suggestion_1);
5975 elsif Nr_Of_Suggestions = 2 then
5976 Error_Msg_Node_2 := Suggestion_2;
5977 Error_Msg_NE -- CODEFIX
5978 ("\possible misspelling of& or&", Sel, Suggestion_1);
5979 end if;
5980 end Check_Misspelled_Selector;
5982 ----------------------
5983 -- Defined_In_Scope --
5984 ----------------------
5986 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5988 S1 : constant Entity_Id := Scope (Base_Type (T));
5989 begin
5990 return S1 = S
5991 or else (S1 = System_Aux_Id and then S = Scope (S1));
5992 end Defined_In_Scope;
5994 -------------------
5995 -- Diagnose_Call --
5996 -------------------
5998 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5999 Actual : Node_Id;
6000 X : Interp_Index;
6001 It : Interp;
6002 Err_Mode : Boolean;
6003 New_Nam : Node_Id;
6004 Void_Interp_Seen : Boolean := False;
6006 Success : Boolean;
6007 pragma Warnings (Off, Boolean);
6009 begin
6010 if Ada_Version >= Ada_2005 then
6011 Actual := First_Actual (N);
6012 while Present (Actual) loop
6014 -- Ada 2005 (AI-50217): Post an error in case of premature
6015 -- usage of an entity from the limited view.
6017 if not Analyzed (Etype (Actual))
6018 and then From_Limited_With (Etype (Actual))
6019 then
6020 Error_Msg_Qual_Level := 1;
6021 Error_Msg_NE
6022 ("missing with_clause for scope of imported type&",
6023 Actual, Etype (Actual));
6024 Error_Msg_Qual_Level := 0;
6025 end if;
6027 Next_Actual (Actual);
6028 end loop;
6029 end if;
6031 -- Before listing the possible candidates, check whether this is
6032 -- a prefix of a selected component that has been rewritten as a
6033 -- parameterless function call because there is a callable candidate
6034 -- interpretation. If there is a hidden package in the list of homonyms
6035 -- of the function name (bad programming style in any case) suggest that
6036 -- this is the intended entity.
6038 if No (Parameter_Associations (N))
6039 and then Nkind (Parent (N)) = N_Selected_Component
6040 and then Nkind (Parent (Parent (N))) in N_Declaration
6041 and then Is_Overloaded (Nam)
6042 then
6043 declare
6044 Ent : Entity_Id;
6046 begin
6047 Ent := Current_Entity (Nam);
6048 while Present (Ent) loop
6049 if Ekind (Ent) = E_Package then
6050 Error_Msg_N
6051 ("no legal interpretations as function call,!", Nam);
6052 Error_Msg_NE ("\package& is not visible", N, Ent);
6054 Rewrite (Parent (N),
6055 New_Occurrence_Of (Any_Type, Sloc (N)));
6056 return;
6057 end if;
6059 Ent := Homonym (Ent);
6060 end loop;
6061 end;
6062 end if;
6064 -- Analyze each candidate call again, with full error reporting for
6065 -- each.
6067 Error_Msg_N
6068 ("no candidate interpretations match the actuals:!", Nam);
6069 Err_Mode := All_Errors_Mode;
6070 All_Errors_Mode := True;
6072 -- If this is a call to an operation of a concurrent type,
6073 -- the failed interpretations have been removed from the
6074 -- name. Recover them to provide full diagnostics.
6076 if Nkind (Parent (Nam)) = N_Selected_Component then
6077 Set_Entity (Nam, Empty);
6078 New_Nam := New_Copy_Tree (Parent (Nam));
6079 Set_Is_Overloaded (New_Nam, False);
6080 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6081 Set_Parent (New_Nam, Parent (Parent (Nam)));
6082 Analyze_Selected_Component (New_Nam);
6083 Get_First_Interp (Selector_Name (New_Nam), X, It);
6084 else
6085 Get_First_Interp (Nam, X, It);
6086 end if;
6088 while Present (It.Nam) loop
6089 if Etype (It.Nam) = Standard_Void_Type then
6090 Void_Interp_Seen := True;
6091 end if;
6093 Analyze_One_Call (N, It.Nam, True, Success);
6094 Get_Next_Interp (X, It);
6095 end loop;
6097 if Nkind (N) = N_Function_Call then
6098 Get_First_Interp (Nam, X, It);
6099 while Present (It.Nam) loop
6100 if Ekind_In (It.Nam, E_Function, E_Operator) then
6101 return;
6102 else
6103 Get_Next_Interp (X, It);
6104 end if;
6105 end loop;
6107 -- If all interpretations are procedures, this deserves a
6108 -- more precise message. Ditto if this appears as the prefix
6109 -- of a selected component, which may be a lexical error.
6111 Error_Msg_N
6112 ("\context requires function call, found procedure name", Nam);
6114 if Nkind (Parent (N)) = N_Selected_Component
6115 and then N = Prefix (Parent (N))
6116 then
6117 Error_Msg_N -- CODEFIX
6118 ("\period should probably be semicolon", Parent (N));
6119 end if;
6121 elsif Nkind (N) = N_Procedure_Call_Statement
6122 and then not Void_Interp_Seen
6123 then
6124 Error_Msg_N (
6125 "\function name found in procedure call", Nam);
6126 end if;
6128 All_Errors_Mode := Err_Mode;
6129 end Diagnose_Call;
6131 ---------------------------
6132 -- Find_Arithmetic_Types --
6133 ---------------------------
6135 procedure Find_Arithmetic_Types
6136 (L, R : Node_Id;
6137 Op_Id : Entity_Id;
6138 N : Node_Id)
6140 Index1 : Interp_Index;
6141 Index2 : Interp_Index;
6142 It1 : Interp;
6143 It2 : Interp;
6145 procedure Check_Right_Argument (T : Entity_Id);
6146 -- Check right operand of operator
6148 --------------------------
6149 -- Check_Right_Argument --
6150 --------------------------
6152 procedure Check_Right_Argument (T : Entity_Id) is
6153 begin
6154 if not Is_Overloaded (R) then
6155 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6156 else
6157 Get_First_Interp (R, Index2, It2);
6158 while Present (It2.Typ) loop
6159 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
6160 Get_Next_Interp (Index2, It2);
6161 end loop;
6162 end if;
6163 end Check_Right_Argument;
6165 -- Start of processing for Find_Arithmetic_Types
6167 begin
6168 if not Is_Overloaded (L) then
6169 Check_Right_Argument (Etype (L));
6171 else
6172 Get_First_Interp (L, Index1, It1);
6173 while Present (It1.Typ) loop
6174 Check_Right_Argument (It1.Typ);
6175 Get_Next_Interp (Index1, It1);
6176 end loop;
6177 end if;
6179 end Find_Arithmetic_Types;
6181 ------------------------
6182 -- Find_Boolean_Types --
6183 ------------------------
6185 procedure Find_Boolean_Types
6186 (L, R : Node_Id;
6187 Op_Id : Entity_Id;
6188 N : Node_Id)
6190 Index : Interp_Index;
6191 It : Interp;
6193 procedure Check_Numeric_Argument (T : Entity_Id);
6194 -- Special case for logical operations one of whose operands is an
6195 -- integer literal. If both are literal the result is any modular type.
6197 ----------------------------
6198 -- Check_Numeric_Argument --
6199 ----------------------------
6201 procedure Check_Numeric_Argument (T : Entity_Id) is
6202 begin
6203 if T = Universal_Integer then
6204 Add_One_Interp (N, Op_Id, Any_Modular);
6206 elsif Is_Modular_Integer_Type (T) then
6207 Add_One_Interp (N, Op_Id, T);
6208 end if;
6209 end Check_Numeric_Argument;
6211 -- Start of processing for Find_Boolean_Types
6213 begin
6214 if not Is_Overloaded (L) then
6215 if Etype (L) = Universal_Integer
6216 or else Etype (L) = Any_Modular
6217 then
6218 if not Is_Overloaded (R) then
6219 Check_Numeric_Argument (Etype (R));
6221 else
6222 Get_First_Interp (R, Index, It);
6223 while Present (It.Typ) loop
6224 Check_Numeric_Argument (It.Typ);
6225 Get_Next_Interp (Index, It);
6226 end loop;
6227 end if;
6229 -- If operands are aggregates, we must assume that they may be
6230 -- boolean arrays, and leave disambiguation for the second pass.
6231 -- If only one is an aggregate, verify that the other one has an
6232 -- interpretation as a boolean array
6234 elsif Nkind (L) = N_Aggregate then
6235 if Nkind (R) = N_Aggregate then
6236 Add_One_Interp (N, Op_Id, Etype (L));
6238 elsif not Is_Overloaded (R) then
6239 if Valid_Boolean_Arg (Etype (R)) then
6240 Add_One_Interp (N, Op_Id, Etype (R));
6241 end if;
6243 else
6244 Get_First_Interp (R, Index, It);
6245 while Present (It.Typ) loop
6246 if Valid_Boolean_Arg (It.Typ) then
6247 Add_One_Interp (N, Op_Id, It.Typ);
6248 end if;
6250 Get_Next_Interp (Index, It);
6251 end loop;
6252 end if;
6254 elsif Valid_Boolean_Arg (Etype (L))
6255 and then Has_Compatible_Type (R, Etype (L))
6256 then
6257 Add_One_Interp (N, Op_Id, Etype (L));
6258 end if;
6260 else
6261 Get_First_Interp (L, Index, It);
6262 while Present (It.Typ) loop
6263 if Valid_Boolean_Arg (It.Typ)
6264 and then Has_Compatible_Type (R, It.Typ)
6265 then
6266 Add_One_Interp (N, Op_Id, It.Typ);
6267 end if;
6269 Get_Next_Interp (Index, It);
6270 end loop;
6271 end if;
6272 end Find_Boolean_Types;
6274 ---------------------------
6275 -- Find_Comparison_Types --
6276 ---------------------------
6278 procedure Find_Comparison_Types
6279 (L, R : Node_Id;
6280 Op_Id : Entity_Id;
6281 N : Node_Id)
6283 Index : Interp_Index;
6284 It : Interp;
6285 Found : Boolean := False;
6286 I_F : Interp_Index;
6287 T_F : Entity_Id;
6288 Scop : Entity_Id := Empty;
6290 procedure Try_One_Interp (T1 : Entity_Id);
6291 -- Routine to try one proposed interpretation. Note that the context
6292 -- of the operator plays no role in resolving the arguments, so that
6293 -- if there is more than one interpretation of the operands that is
6294 -- compatible with comparison, the operation is ambiguous.
6296 --------------------
6297 -- Try_One_Interp --
6298 --------------------
6300 procedure Try_One_Interp (T1 : Entity_Id) is
6301 begin
6302 -- If the operator is an expanded name, then the type of the operand
6303 -- must be defined in the corresponding scope. If the type is
6304 -- universal, the context will impose the correct type. Note that we
6305 -- also avoid returning if we are currently within a generic instance
6306 -- due to the fact that the generic package declaration has already
6307 -- been successfully analyzed and Defined_In_Scope expects the base
6308 -- type to be defined within the instance which will never be the
6309 -- case.
6311 if Present (Scop)
6312 and then not Defined_In_Scope (T1, Scop)
6313 and then not In_Instance
6314 and then T1 /= Universal_Integer
6315 and then T1 /= Universal_Real
6316 and then T1 /= Any_String
6317 and then T1 /= Any_Composite
6318 then
6319 return;
6320 end if;
6322 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6323 if Found and then Base_Type (T1) /= Base_Type (T_F) then
6324 It := Disambiguate (L, I_F, Index, Any_Type);
6326 if It = No_Interp then
6327 Ambiguous_Operands (N);
6328 Set_Etype (L, Any_Type);
6329 return;
6331 else
6332 T_F := It.Typ;
6333 end if;
6334 else
6335 Found := True;
6336 T_F := T1;
6337 I_F := Index;
6338 end if;
6340 Set_Etype (L, T_F);
6341 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6342 end if;
6343 end Try_One_Interp;
6345 -- Start of processing for Find_Comparison_Types
6347 begin
6348 -- If left operand is aggregate, the right operand has to
6349 -- provide a usable type for it.
6351 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
6352 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6353 return;
6354 end if;
6356 if Nkind (N) = N_Function_Call
6357 and then Nkind (Name (N)) = N_Expanded_Name
6358 then
6359 Scop := Entity (Prefix (Name (N)));
6361 -- The prefix may be a package renaming, and the subsequent test
6362 -- requires the original package.
6364 if Ekind (Scop) = E_Package
6365 and then Present (Renamed_Entity (Scop))
6366 then
6367 Scop := Renamed_Entity (Scop);
6368 Set_Entity (Prefix (Name (N)), Scop);
6369 end if;
6370 end if;
6372 if not Is_Overloaded (L) then
6373 Try_One_Interp (Etype (L));
6375 else
6376 Get_First_Interp (L, Index, It);
6377 while Present (It.Typ) loop
6378 Try_One_Interp (It.Typ);
6379 Get_Next_Interp (Index, It);
6380 end loop;
6381 end if;
6382 end Find_Comparison_Types;
6384 ----------------------------------------
6385 -- Find_Non_Universal_Interpretations --
6386 ----------------------------------------
6388 procedure Find_Non_Universal_Interpretations
6389 (N : Node_Id;
6390 R : Node_Id;
6391 Op_Id : Entity_Id;
6392 T1 : Entity_Id)
6394 Index : Interp_Index;
6395 It : Interp;
6397 begin
6398 if T1 = Universal_Integer or else T1 = Universal_Real
6400 -- If the left operand of an equality operator is null, the visibility
6401 -- of the operator must be determined from the interpretation of the
6402 -- right operand. This processing must be done for Any_Access, which
6403 -- is the internal representation of the type of the literal null.
6405 or else T1 = Any_Access
6406 then
6407 if not Is_Overloaded (R) then
6408 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
6409 else
6410 Get_First_Interp (R, Index, It);
6411 while Present (It.Typ) loop
6412 if Covers (It.Typ, T1) then
6413 Add_One_Interp
6414 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6415 end if;
6417 Get_Next_Interp (Index, It);
6418 end loop;
6419 end if;
6420 else
6421 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6422 end if;
6423 end Find_Non_Universal_Interpretations;
6425 ------------------------------
6426 -- Find_Concatenation_Types --
6427 ------------------------------
6429 procedure Find_Concatenation_Types
6430 (L, R : Node_Id;
6431 Op_Id : Entity_Id;
6432 N : Node_Id)
6434 Op_Type : constant Entity_Id := Etype (Op_Id);
6436 begin
6437 if Is_Array_Type (Op_Type)
6438 and then not Is_Limited_Type (Op_Type)
6440 and then (Has_Compatible_Type (L, Op_Type)
6441 or else
6442 Has_Compatible_Type (L, Component_Type (Op_Type)))
6444 and then (Has_Compatible_Type (R, Op_Type)
6445 or else
6446 Has_Compatible_Type (R, Component_Type (Op_Type)))
6447 then
6448 Add_One_Interp (N, Op_Id, Op_Type);
6449 end if;
6450 end Find_Concatenation_Types;
6452 -------------------------
6453 -- Find_Equality_Types --
6454 -------------------------
6456 procedure Find_Equality_Types
6457 (L, R : Node_Id;
6458 Op_Id : Entity_Id;
6459 N : Node_Id)
6461 Index : Interp_Index;
6462 It : Interp;
6463 Found : Boolean := False;
6464 I_F : Interp_Index;
6465 T_F : Entity_Id;
6466 Scop : Entity_Id := Empty;
6468 procedure Try_One_Interp (T1 : Entity_Id);
6469 -- The context of the equality operator plays no role in resolving the
6470 -- arguments, so that if there is more than one interpretation of the
6471 -- operands that is compatible with equality, the construct is ambiguous
6472 -- and an error can be emitted now, after trying to disambiguate, i.e.
6473 -- applying preference rules.
6475 --------------------
6476 -- Try_One_Interp --
6477 --------------------
6479 procedure Try_One_Interp (T1 : Entity_Id) is
6480 Bas : Entity_Id;
6482 begin
6483 -- Perform a sanity check in case of previous errors
6485 if No (T1) then
6486 return;
6487 end if;
6489 Bas := Base_Type (T1);
6491 -- If the operator is an expanded name, then the type of the operand
6492 -- must be defined in the corresponding scope. If the type is
6493 -- universal, the context will impose the correct type. An anonymous
6494 -- type for a 'Access reference is also universal in this sense, as
6495 -- the actual type is obtained from context.
6497 -- In Ada 2005, the equality operator for anonymous access types
6498 -- is declared in Standard, and preference rules apply to it.
6500 if Present (Scop) then
6502 -- Note that we avoid returning if we are currently within a
6503 -- generic instance due to the fact that the generic package
6504 -- declaration has already been successfully analyzed and
6505 -- Defined_In_Scope expects the base type to be defined within
6506 -- the instance which will never be the case.
6508 if Defined_In_Scope (T1, Scop)
6509 or else In_Instance
6510 or else T1 = Universal_Integer
6511 or else T1 = Universal_Real
6512 or else T1 = Any_Access
6513 or else T1 = Any_String
6514 or else T1 = Any_Composite
6515 or else (Ekind (T1) = E_Access_Subprogram_Type
6516 and then not Comes_From_Source (T1))
6517 then
6518 null;
6520 elsif Ekind (T1) = E_Anonymous_Access_Type
6521 and then Scop = Standard_Standard
6522 then
6523 null;
6525 else
6526 -- The scope does not contain an operator for the type
6528 return;
6529 end if;
6531 -- If we have infix notation, the operator must be usable. Within
6532 -- an instance, if the type is already established we know it is
6533 -- correct. If an operand is universal it is compatible with any
6534 -- numeric type.
6536 elsif In_Open_Scopes (Scope (Bas))
6537 or else Is_Potentially_Use_Visible (Bas)
6538 or else In_Use (Bas)
6539 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
6541 -- In an instance, the type may have been immediately visible.
6542 -- Either the types are compatible, or one operand is universal
6543 -- (numeric or null).
6545 or else
6546 ((In_Instance or else In_Inlined_Body)
6547 and then
6548 (First_Subtype (T1) = First_Subtype (Etype (R))
6549 or else Nkind (R) = N_Null
6550 or else
6551 (Is_Numeric_Type (T1)
6552 and then Is_Universal_Numeric_Type (Etype (R)))))
6554 -- In Ada 2005, the equality on anonymous access types is declared
6555 -- in Standard, and is always visible.
6557 or else Ekind (T1) = E_Anonymous_Access_Type
6558 then
6559 null;
6561 else
6562 -- Save candidate type for subsequent error message, if any
6564 if not Is_Limited_Type (T1) then
6565 Candidate_Type := T1;
6566 end if;
6568 return;
6569 end if;
6571 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6572 -- Do not allow anonymous access types in equality operators.
6574 if Ada_Version < Ada_2005
6575 and then Ekind (T1) = E_Anonymous_Access_Type
6576 then
6577 return;
6578 end if;
6580 -- If the right operand has a type compatible with T1, check for an
6581 -- acceptable interpretation, unless T1 is limited (no predefined
6582 -- equality available), or this is use of a "/=" for a tagged type.
6583 -- In the latter case, possible interpretations of equality need
6584 -- to be considered, we don't want the default inequality declared
6585 -- in Standard to be chosen, and the "/=" will be rewritten as a
6586 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
6587 -- that rewriting happens during analysis rather than being
6588 -- delayed until expansion (this is needed for ASIS, which only sees
6589 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
6590 -- is Name_Op_Eq then we still proceed with the interpretation,
6591 -- because that indicates the potential rewriting case where the
6592 -- interpretation to consider is actually "=" and the node may be
6593 -- about to be rewritten by Analyze_Equality_Op.
6595 if T1 /= Standard_Void_Type
6596 and then Has_Compatible_Type (R, T1)
6598 and then
6599 ((not Is_Limited_Type (T1)
6600 and then not Is_Limited_Composite (T1))
6602 or else
6603 (Is_Array_Type (T1)
6604 and then not Is_Limited_Type (Component_Type (T1))
6605 and then Available_Full_View_Of_Component (T1)))
6607 and then
6608 (Nkind (N) /= N_Op_Ne
6609 or else not Is_Tagged_Type (T1)
6610 or else Chars (Op_Id) = Name_Op_Eq)
6611 then
6612 if Found
6613 and then Base_Type (T1) /= Base_Type (T_F)
6614 then
6615 It := Disambiguate (L, I_F, Index, Any_Type);
6617 if It = No_Interp then
6618 Ambiguous_Operands (N);
6619 Set_Etype (L, Any_Type);
6620 return;
6622 else
6623 T_F := It.Typ;
6624 end if;
6626 else
6627 Found := True;
6628 T_F := T1;
6629 I_F := Index;
6630 end if;
6632 if not Analyzed (L) then
6633 Set_Etype (L, T_F);
6634 end if;
6636 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6638 -- Case of operator was not visible, Etype still set to Any_Type
6640 if Etype (N) = Any_Type then
6641 Found := False;
6642 end if;
6644 elsif Scop = Standard_Standard
6645 and then Ekind (T1) = E_Anonymous_Access_Type
6646 then
6647 Found := True;
6648 end if;
6649 end Try_One_Interp;
6651 -- Start of processing for Find_Equality_Types
6653 begin
6654 -- If left operand is aggregate, the right operand has to
6655 -- provide a usable type for it.
6657 if Nkind (L) = N_Aggregate
6658 and then Nkind (R) /= N_Aggregate
6659 then
6660 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6661 return;
6662 end if;
6664 if Nkind (N) = N_Function_Call
6665 and then Nkind (Name (N)) = N_Expanded_Name
6666 then
6667 Scop := Entity (Prefix (Name (N)));
6669 -- The prefix may be a package renaming, and the subsequent test
6670 -- requires the original package.
6672 if Ekind (Scop) = E_Package
6673 and then Present (Renamed_Entity (Scop))
6674 then
6675 Scop := Renamed_Entity (Scop);
6676 Set_Entity (Prefix (Name (N)), Scop);
6677 end if;
6678 end if;
6680 if not Is_Overloaded (L) then
6681 Try_One_Interp (Etype (L));
6683 else
6684 Get_First_Interp (L, Index, It);
6685 while Present (It.Typ) loop
6686 Try_One_Interp (It.Typ);
6687 Get_Next_Interp (Index, It);
6688 end loop;
6689 end if;
6690 end Find_Equality_Types;
6692 -------------------------
6693 -- Find_Negation_Types --
6694 -------------------------
6696 procedure Find_Negation_Types
6697 (R : Node_Id;
6698 Op_Id : Entity_Id;
6699 N : Node_Id)
6701 Index : Interp_Index;
6702 It : Interp;
6704 begin
6705 if not Is_Overloaded (R) then
6706 if Etype (R) = Universal_Integer then
6707 Add_One_Interp (N, Op_Id, Any_Modular);
6708 elsif Valid_Boolean_Arg (Etype (R)) then
6709 Add_One_Interp (N, Op_Id, Etype (R));
6710 end if;
6712 else
6713 Get_First_Interp (R, Index, It);
6714 while Present (It.Typ) loop
6715 if Valid_Boolean_Arg (It.Typ) then
6716 Add_One_Interp (N, Op_Id, It.Typ);
6717 end if;
6719 Get_Next_Interp (Index, It);
6720 end loop;
6721 end if;
6722 end Find_Negation_Types;
6724 ------------------------------
6725 -- Find_Primitive_Operation --
6726 ------------------------------
6728 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6729 Obj : constant Node_Id := Prefix (N);
6730 Op : constant Node_Id := Selector_Name (N);
6732 Prim : Elmt_Id;
6733 Prims : Elist_Id;
6734 Typ : Entity_Id;
6736 begin
6737 Set_Etype (Op, Any_Type);
6739 if Is_Access_Type (Etype (Obj)) then
6740 Typ := Designated_Type (Etype (Obj));
6741 else
6742 Typ := Etype (Obj);
6743 end if;
6745 if Is_Class_Wide_Type (Typ) then
6746 Typ := Root_Type (Typ);
6747 end if;
6749 Prims := Primitive_Operations (Typ);
6751 Prim := First_Elmt (Prims);
6752 while Present (Prim) loop
6753 if Chars (Node (Prim)) = Chars (Op) then
6754 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6755 Set_Etype (N, Etype (Node (Prim)));
6756 end if;
6758 Next_Elmt (Prim);
6759 end loop;
6761 -- Now look for class-wide operations of the type or any of its
6762 -- ancestors by iterating over the homonyms of the selector.
6764 declare
6765 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6766 Hom : Entity_Id;
6768 begin
6769 Hom := Current_Entity (Op);
6770 while Present (Hom) loop
6771 if (Ekind (Hom) = E_Procedure
6772 or else
6773 Ekind (Hom) = E_Function)
6774 and then Scope (Hom) = Scope (Typ)
6775 and then Present (First_Formal (Hom))
6776 and then
6777 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6778 or else
6779 (Is_Access_Type (Etype (First_Formal (Hom)))
6780 and then
6781 Ekind (Etype (First_Formal (Hom))) =
6782 E_Anonymous_Access_Type
6783 and then
6784 Base_Type
6785 (Designated_Type (Etype (First_Formal (Hom)))) =
6786 Cls_Type))
6787 then
6788 Add_One_Interp (Op, Hom, Etype (Hom));
6789 Set_Etype (N, Etype (Hom));
6790 end if;
6792 Hom := Homonym (Hom);
6793 end loop;
6794 end;
6796 return Etype (Op) /= Any_Type;
6797 end Find_Primitive_Operation;
6799 ----------------------
6800 -- Find_Unary_Types --
6801 ----------------------
6803 procedure Find_Unary_Types
6804 (R : Node_Id;
6805 Op_Id : Entity_Id;
6806 N : Node_Id)
6808 Index : Interp_Index;
6809 It : Interp;
6811 begin
6812 if not Is_Overloaded (R) then
6813 if Is_Numeric_Type (Etype (R)) then
6815 -- In an instance a generic actual may be a numeric type even if
6816 -- the formal in the generic unit was not. In that case, the
6817 -- predefined operator was not a possible interpretation in the
6818 -- generic, and cannot be one in the instance, unless the operator
6819 -- is an actual of an instance.
6821 if In_Instance
6822 and then
6823 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6824 then
6825 null;
6826 else
6827 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6828 end if;
6829 end if;
6831 else
6832 Get_First_Interp (R, Index, It);
6833 while Present (It.Typ) loop
6834 if Is_Numeric_Type (It.Typ) then
6835 if In_Instance
6836 and then
6837 not Is_Numeric_Type
6838 (Corresponding_Generic_Type (Etype (It.Typ)))
6839 then
6840 null;
6842 else
6843 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6844 end if;
6845 end if;
6847 Get_Next_Interp (Index, It);
6848 end loop;
6849 end if;
6850 end Find_Unary_Types;
6852 ------------------
6853 -- Junk_Operand --
6854 ------------------
6856 function Junk_Operand (N : Node_Id) return Boolean is
6857 Enode : Node_Id;
6859 begin
6860 if Error_Posted (N) then
6861 return False;
6862 end if;
6864 -- Get entity to be tested
6866 if Is_Entity_Name (N)
6867 and then Present (Entity (N))
6868 then
6869 Enode := N;
6871 -- An odd case, a procedure name gets converted to a very peculiar
6872 -- function call, and here is where we detect this happening.
6874 elsif Nkind (N) = N_Function_Call
6875 and then Is_Entity_Name (Name (N))
6876 and then Present (Entity (Name (N)))
6877 then
6878 Enode := Name (N);
6880 -- Another odd case, there are at least some cases of selected
6881 -- components where the selected component is not marked as having
6882 -- an entity, even though the selector does have an entity
6884 elsif Nkind (N) = N_Selected_Component
6885 and then Present (Entity (Selector_Name (N)))
6886 then
6887 Enode := Selector_Name (N);
6889 else
6890 return False;
6891 end if;
6893 -- Now test the entity we got to see if it is a bad case
6895 case Ekind (Entity (Enode)) is
6896 when E_Package =>
6897 Error_Msg_N
6898 ("package name cannot be used as operand", Enode);
6900 when Generic_Unit_Kind =>
6901 Error_Msg_N
6902 ("generic unit name cannot be used as operand", Enode);
6904 when Type_Kind =>
6905 Error_Msg_N
6906 ("subtype name cannot be used as operand", Enode);
6908 when Entry_Kind =>
6909 Error_Msg_N
6910 ("entry name cannot be used as operand", Enode);
6912 when E_Procedure =>
6913 Error_Msg_N
6914 ("procedure name cannot be used as operand", Enode);
6916 when E_Exception =>
6917 Error_Msg_N
6918 ("exception name cannot be used as operand", Enode);
6920 when E_Block
6921 | E_Label
6922 | E_Loop
6924 Error_Msg_N
6925 ("label name cannot be used as operand", Enode);
6927 when others =>
6928 return False;
6929 end case;
6931 return True;
6932 end Junk_Operand;
6934 --------------------
6935 -- Operator_Check --
6936 --------------------
6938 procedure Operator_Check (N : Node_Id) is
6939 begin
6940 Remove_Abstract_Operations (N);
6942 -- Test for case of no interpretation found for operator
6944 if Etype (N) = Any_Type then
6945 declare
6946 L : Node_Id;
6947 R : Node_Id;
6948 Op_Id : Entity_Id := Empty;
6950 begin
6951 R := Right_Opnd (N);
6953 if Nkind (N) in N_Binary_Op then
6954 L := Left_Opnd (N);
6955 else
6956 L := Empty;
6957 end if;
6959 -- If either operand has no type, then don't complain further,
6960 -- since this simply means that we have a propagated error.
6962 if R = Error
6963 or else Etype (R) = Any_Type
6964 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6965 then
6966 -- For the rather unusual case where one of the operands is
6967 -- a Raise_Expression, whose initial type is Any_Type, use
6968 -- the type of the other operand.
6970 if Nkind (L) = N_Raise_Expression then
6971 Set_Etype (L, Etype (R));
6972 Set_Etype (N, Etype (R));
6974 elsif Nkind (R) = N_Raise_Expression then
6975 Set_Etype (R, Etype (L));
6976 Set_Etype (N, Etype (L));
6977 end if;
6979 return;
6981 -- We explicitly check for the case of concatenation of component
6982 -- with component to avoid reporting spurious matching array types
6983 -- that might happen to be lurking in distant packages (such as
6984 -- run-time packages). This also prevents inconsistencies in the
6985 -- messages for certain ACVC B tests, which can vary depending on
6986 -- types declared in run-time interfaces. Another improvement when
6987 -- aggregates are present is to look for a well-typed operand.
6989 elsif Present (Candidate_Type)
6990 and then (Nkind (N) /= N_Op_Concat
6991 or else Is_Array_Type (Etype (L))
6992 or else Is_Array_Type (Etype (R)))
6993 then
6994 if Nkind (N) = N_Op_Concat then
6995 if Etype (L) /= Any_Composite
6996 and then Is_Array_Type (Etype (L))
6997 then
6998 Candidate_Type := Etype (L);
7000 elsif Etype (R) /= Any_Composite
7001 and then Is_Array_Type (Etype (R))
7002 then
7003 Candidate_Type := Etype (R);
7004 end if;
7005 end if;
7007 Error_Msg_NE -- CODEFIX
7008 ("operator for} is not directly visible!",
7009 N, First_Subtype (Candidate_Type));
7011 declare
7012 U : constant Node_Id :=
7013 Cunit (Get_Source_Unit (Candidate_Type));
7014 begin
7015 if Unit_Is_Visible (U) then
7016 Error_Msg_N -- CODEFIX
7017 ("use clause would make operation legal!", N);
7018 else
7019 Error_Msg_NE -- CODEFIX
7020 ("add with_clause and use_clause for&!",
7021 N, Defining_Entity (Unit (U)));
7022 end if;
7023 end;
7024 return;
7026 -- If either operand is a junk operand (e.g. package name), then
7027 -- post appropriate error messages, but do not complain further.
7029 -- Note that the use of OR in this test instead of OR ELSE is
7030 -- quite deliberate, we may as well check both operands in the
7031 -- binary operator case.
7033 elsif Junk_Operand (R)
7034 or -- really mean OR here and not OR ELSE, see above
7035 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
7036 then
7037 return;
7039 -- If we have a logical operator, one of whose operands is
7040 -- Boolean, then we know that the other operand cannot resolve to
7041 -- Boolean (since we got no interpretations), but in that case we
7042 -- pretty much know that the other operand should be Boolean, so
7043 -- resolve it that way (generating an error).
7045 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
7046 if Etype (L) = Standard_Boolean then
7047 Resolve (R, Standard_Boolean);
7048 return;
7049 elsif Etype (R) = Standard_Boolean then
7050 Resolve (L, Standard_Boolean);
7051 return;
7052 end if;
7054 -- For an arithmetic operator or comparison operator, if one
7055 -- of the operands is numeric, then we know the other operand
7056 -- is not the same numeric type. If it is a non-numeric type,
7057 -- then probably it is intended to match the other operand.
7059 elsif Nkind_In (N, N_Op_Add,
7060 N_Op_Divide,
7061 N_Op_Ge,
7062 N_Op_Gt,
7063 N_Op_Le)
7064 or else
7065 Nkind_In (N, N_Op_Lt,
7066 N_Op_Mod,
7067 N_Op_Multiply,
7068 N_Op_Rem,
7069 N_Op_Subtract)
7070 then
7071 -- If Allow_Integer_Address is active, check whether the
7072 -- operation becomes legal after converting an operand.
7074 if Is_Numeric_Type (Etype (L))
7075 and then not Is_Numeric_Type (Etype (R))
7076 then
7077 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7078 Rewrite (R,
7079 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7081 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7082 Analyze_Comparison_Op (N);
7083 else
7084 Analyze_Arithmetic_Op (N);
7085 end if;
7086 else
7087 Resolve (R, Etype (L));
7088 end if;
7090 return;
7092 elsif Is_Numeric_Type (Etype (R))
7093 and then not Is_Numeric_Type (Etype (L))
7094 then
7095 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7096 Rewrite (L,
7097 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
7099 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7100 Analyze_Comparison_Op (N);
7101 else
7102 Analyze_Arithmetic_Op (N);
7103 end if;
7105 return;
7107 else
7108 Resolve (L, Etype (R));
7109 end if;
7111 return;
7113 elsif Allow_Integer_Address
7114 and then Is_Descendant_Of_Address (Etype (L))
7115 and then Is_Descendant_Of_Address (Etype (R))
7116 and then not Error_Posted (N)
7117 then
7118 declare
7119 Addr_Type : constant Entity_Id := Etype (L);
7121 begin
7122 Rewrite (L,
7123 Unchecked_Convert_To (
7124 Standard_Integer, Relocate_Node (L)));
7125 Rewrite (R,
7126 Unchecked_Convert_To (
7127 Standard_Integer, Relocate_Node (R)));
7129 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7130 Analyze_Comparison_Op (N);
7131 else
7132 Analyze_Arithmetic_Op (N);
7133 end if;
7135 -- If this is an operand in an enclosing arithmetic
7136 -- operation, Convert the result as an address so that
7137 -- arithmetic folding of address can continue.
7139 if Nkind (Parent (N)) in N_Op then
7140 Rewrite (N,
7141 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7142 end if;
7144 return;
7145 end;
7147 -- Under relaxed RM semantics silently replace occurrences of
7148 -- null by System.Address_Null.
7150 elsif Null_To_Null_Address_Convert_OK (N) then
7151 Replace_Null_By_Null_Address (N);
7153 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7154 Analyze_Comparison_Op (N);
7155 else
7156 Analyze_Arithmetic_Op (N);
7157 end if;
7159 return;
7160 end if;
7162 -- Comparisons on A'Access are common enough to deserve a
7163 -- special message.
7165 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
7166 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7167 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7168 then
7169 Error_Msg_N
7170 ("two access attributes cannot be compared directly", N);
7171 Error_Msg_N
7172 ("\use qualified expression for one of the operands",
7174 return;
7176 -- Another one for C programmers
7178 elsif Nkind (N) = N_Op_Concat
7179 and then Valid_Boolean_Arg (Etype (L))
7180 and then Valid_Boolean_Arg (Etype (R))
7181 then
7182 Error_Msg_N ("invalid operands for concatenation", N);
7183 Error_Msg_N -- CODEFIX
7184 ("\maybe AND was meant", N);
7185 return;
7187 -- A special case for comparison of access parameter with null
7189 elsif Nkind (N) = N_Op_Eq
7190 and then Is_Entity_Name (L)
7191 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7192 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7193 N_Access_Definition
7194 and then Nkind (R) = N_Null
7195 then
7196 Error_Msg_N ("access parameter is not allowed to be null", L);
7197 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7198 return;
7200 -- Another special case for exponentiation, where the right
7201 -- operand must be Natural, independently of the base.
7203 elsif Nkind (N) = N_Op_Expon
7204 and then Is_Numeric_Type (Etype (L))
7205 and then not Is_Overloaded (R)
7206 and then
7207 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7208 and then Base_Type (Etype (R)) /= Universal_Integer
7209 then
7210 if Ada_Version >= Ada_2012
7211 and then Has_Dimension_System (Etype (L))
7212 then
7213 Error_Msg_NE
7214 ("exponent for dimensioned type must be a rational" &
7215 ", found}", R, Etype (R));
7216 else
7217 Error_Msg_NE
7218 ("exponent must be of type Natural, found}", R, Etype (R));
7219 end if;
7221 return;
7223 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
7224 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7225 Rewrite (R,
7226 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7227 Analyze_Equality_Op (N);
7228 return;
7230 -- Under relaxed RM semantics silently replace occurrences of
7231 -- null by System.Address_Null.
7233 elsif Null_To_Null_Address_Convert_OK (N) then
7234 Replace_Null_By_Null_Address (N);
7235 Analyze_Equality_Op (N);
7236 return;
7237 end if;
7238 end if;
7240 -- If we fall through then just give general message. Note that in
7241 -- the following messages, if the operand is overloaded we choose
7242 -- an arbitrary type to complain about, but that is probably more
7243 -- useful than not giving a type at all.
7245 if Nkind (N) in N_Unary_Op then
7246 Error_Msg_Node_2 := Etype (R);
7247 Error_Msg_N ("operator& not defined for}", N);
7248 return;
7250 else
7251 if Nkind (N) in N_Binary_Op then
7252 if not Is_Overloaded (L)
7253 and then not Is_Overloaded (R)
7254 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7255 then
7256 Error_Msg_Node_2 := First_Subtype (Etype (R));
7257 Error_Msg_N ("there is no applicable operator& for}", N);
7259 else
7260 -- Another attempt to find a fix: one of the candidate
7261 -- interpretations may not be use-visible. This has
7262 -- already been checked for predefined operators, so
7263 -- we examine only user-defined functions.
7265 Op_Id := Get_Name_Entity_Id (Chars (N));
7267 while Present (Op_Id) loop
7268 if Ekind (Op_Id) /= E_Operator
7269 and then Is_Overloadable (Op_Id)
7270 then
7271 if not Is_Immediately_Visible (Op_Id)
7272 and then not In_Use (Scope (Op_Id))
7273 and then not Is_Abstract_Subprogram (Op_Id)
7274 and then not Is_Hidden (Op_Id)
7275 and then Ekind (Scope (Op_Id)) = E_Package
7276 and then
7277 Has_Compatible_Type
7278 (L, Etype (First_Formal (Op_Id)))
7279 and then Present
7280 (Next_Formal (First_Formal (Op_Id)))
7281 and then
7282 Has_Compatible_Type
7284 Etype (Next_Formal (First_Formal (Op_Id))))
7285 then
7286 Error_Msg_N
7287 ("No legal interpretation for operator&", N);
7288 Error_Msg_NE
7289 ("\use clause on& would make operation legal",
7290 N, Scope (Op_Id));
7291 exit;
7292 end if;
7293 end if;
7295 Op_Id := Homonym (Op_Id);
7296 end loop;
7298 if No (Op_Id) then
7299 Error_Msg_N ("invalid operand types for operator&", N);
7301 if Nkind (N) /= N_Op_Concat then
7302 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7303 Error_Msg_NE ("\right operand has}!", N, Etype (R));
7305 -- For concatenation operators it is more difficult to
7306 -- determine which is the wrong operand. It is worth
7307 -- flagging explicitly an access type, for those who
7308 -- might think that a dereference happens here.
7310 elsif Is_Access_Type (Etype (L)) then
7311 Error_Msg_N ("\left operand is access type", N);
7313 elsif Is_Access_Type (Etype (R)) then
7314 Error_Msg_N ("\right operand is access type", N);
7315 end if;
7316 end if;
7317 end if;
7318 end if;
7319 end if;
7320 end;
7321 end if;
7322 end Operator_Check;
7324 -----------------------------------------
7325 -- Process_Implicit_Dereference_Prefix --
7326 -----------------------------------------
7328 function Process_Implicit_Dereference_Prefix
7329 (E : Entity_Id;
7330 P : Entity_Id) return Entity_Id
7332 Ref : Node_Id;
7333 Typ : constant Entity_Id := Designated_Type (Etype (P));
7335 begin
7336 if Present (E)
7337 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7338 then
7339 -- We create a dummy reference to E to ensure that the reference is
7340 -- not considered as part of an assignment (an implicit dereference
7341 -- can never assign to its prefix). The Comes_From_Source attribute
7342 -- needs to be propagated for accurate warnings.
7344 Ref := New_Occurrence_Of (E, Sloc (P));
7345 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7346 Generate_Reference (E, Ref);
7347 end if;
7349 -- An implicit dereference is a legal occurrence of an incomplete type
7350 -- imported through a limited_with clause, if the full view is visible.
7352 if From_Limited_With (Typ)
7353 and then not From_Limited_With (Scope (Typ))
7354 and then
7355 (Is_Immediately_Visible (Scope (Typ))
7356 or else
7357 (Is_Child_Unit (Scope (Typ))
7358 and then Is_Visible_Lib_Unit (Scope (Typ))))
7359 then
7360 return Available_View (Typ);
7361 else
7362 return Typ;
7363 end if;
7364 end Process_Implicit_Dereference_Prefix;
7366 --------------------------------
7367 -- Remove_Abstract_Operations --
7368 --------------------------------
7370 procedure Remove_Abstract_Operations (N : Node_Id) is
7371 Abstract_Op : Entity_Id := Empty;
7372 Address_Descendant : Boolean := False;
7373 I : Interp_Index;
7374 It : Interp;
7376 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7377 -- activate this if either extensions are enabled, or if the abstract
7378 -- operation in question comes from a predefined file. This latter test
7379 -- allows us to use abstract to make operations invisible to users. In
7380 -- particular, if type Address is non-private and abstract subprograms
7381 -- are used to hide its operators, they will be truly hidden.
7383 type Operand_Position is (First_Op, Second_Op);
7384 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
7386 procedure Remove_Address_Interpretations (Op : Operand_Position);
7387 -- Ambiguities may arise when the operands are literal and the address
7388 -- operations in s-auxdec are visible. In that case, remove the
7389 -- interpretation of a literal as Address, to retain the semantics
7390 -- of Address as a private type.
7392 ------------------------------------
7393 -- Remove_Address_Interpretations --
7394 ------------------------------------
7396 procedure Remove_Address_Interpretations (Op : Operand_Position) is
7397 Formal : Entity_Id;
7399 begin
7400 if Is_Overloaded (N) then
7401 Get_First_Interp (N, I, It);
7402 while Present (It.Nam) loop
7403 Formal := First_Entity (It.Nam);
7405 if Op = Second_Op then
7406 Formal := Next_Entity (Formal);
7407 end if;
7409 if Is_Descendant_Of_Address (Etype (Formal)) then
7410 Address_Descendant := True;
7411 Remove_Interp (I);
7412 end if;
7414 Get_Next_Interp (I, It);
7415 end loop;
7416 end if;
7417 end Remove_Address_Interpretations;
7419 -- Start of processing for Remove_Abstract_Operations
7421 begin
7422 if Is_Overloaded (N) then
7423 if Debug_Flag_V then
7424 Write_Str ("Remove_Abstract_Operations: ");
7425 Write_Overloads (N);
7426 end if;
7428 Get_First_Interp (N, I, It);
7430 while Present (It.Nam) loop
7431 if Is_Overloadable (It.Nam)
7432 and then Is_Abstract_Subprogram (It.Nam)
7433 and then not Is_Dispatching_Operation (It.Nam)
7434 then
7435 Abstract_Op := It.Nam;
7437 if Is_Descendant_Of_Address (It.Typ) then
7438 Address_Descendant := True;
7439 Remove_Interp (I);
7440 exit;
7442 -- In Ada 2005, this operation does not participate in overload
7443 -- resolution. If the operation is defined in a predefined
7444 -- unit, it is one of the operations declared abstract in some
7445 -- variants of System, and it must be removed as well.
7447 elsif Ada_Version >= Ada_2005
7448 or else In_Predefined_Unit (It.Nam)
7449 then
7450 Remove_Interp (I);
7451 exit;
7452 end if;
7453 end if;
7455 Get_Next_Interp (I, It);
7456 end loop;
7458 if No (Abstract_Op) then
7460 -- If some interpretation yields an integer type, it is still
7461 -- possible that there are address interpretations. Remove them
7462 -- if one operand is a literal, to avoid spurious ambiguities
7463 -- on systems where Address is a visible integer type.
7465 if Is_Overloaded (N)
7466 and then Nkind (N) in N_Op
7467 and then Is_Integer_Type (Etype (N))
7468 then
7469 if Nkind (N) in N_Binary_Op then
7470 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7471 Remove_Address_Interpretations (Second_Op);
7473 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
7474 Remove_Address_Interpretations (First_Op);
7475 end if;
7476 end if;
7477 end if;
7479 elsif Nkind (N) in N_Op then
7481 -- Remove interpretations that treat literals as addresses. This
7482 -- is never appropriate, even when Address is defined as a visible
7483 -- Integer type. The reason is that we would really prefer Address
7484 -- to behave as a private type, even in this case. If Address is a
7485 -- visible integer type, we get lots of overload ambiguities.
7487 if Nkind (N) in N_Binary_Op then
7488 declare
7489 U1 : constant Boolean :=
7490 Present (Universal_Interpretation (Right_Opnd (N)));
7491 U2 : constant Boolean :=
7492 Present (Universal_Interpretation (Left_Opnd (N)));
7494 begin
7495 if U1 then
7496 Remove_Address_Interpretations (Second_Op);
7497 end if;
7499 if U2 then
7500 Remove_Address_Interpretations (First_Op);
7501 end if;
7503 if not (U1 and U2) then
7505 -- Remove corresponding predefined operator, which is
7506 -- always added to the overload set.
7508 Get_First_Interp (N, I, It);
7509 while Present (It.Nam) loop
7510 if Scope (It.Nam) = Standard_Standard
7511 and then Base_Type (It.Typ) =
7512 Base_Type (Etype (Abstract_Op))
7513 then
7514 Remove_Interp (I);
7515 end if;
7517 Get_Next_Interp (I, It);
7518 end loop;
7520 elsif Is_Overloaded (N)
7521 and then Present (Univ_Type)
7522 then
7523 -- If both operands have a universal interpretation,
7524 -- it is still necessary to remove interpretations that
7525 -- yield Address. Any remaining ambiguities will be
7526 -- removed in Disambiguate.
7528 Get_First_Interp (N, I, It);
7529 while Present (It.Nam) loop
7530 if Is_Descendant_Of_Address (It.Typ) then
7531 Remove_Interp (I);
7533 elsif not Is_Type (It.Nam) then
7534 Set_Entity (N, It.Nam);
7535 end if;
7537 Get_Next_Interp (I, It);
7538 end loop;
7539 end if;
7540 end;
7541 end if;
7543 elsif Nkind (N) = N_Function_Call
7544 and then
7545 (Nkind (Name (N)) = N_Operator_Symbol
7546 or else
7547 (Nkind (Name (N)) = N_Expanded_Name
7548 and then
7549 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7550 then
7552 declare
7553 Arg1 : constant Node_Id := First (Parameter_Associations (N));
7554 U1 : constant Boolean :=
7555 Present (Universal_Interpretation (Arg1));
7556 U2 : constant Boolean :=
7557 Present (Next (Arg1)) and then
7558 Present (Universal_Interpretation (Next (Arg1)));
7560 begin
7561 if U1 then
7562 Remove_Address_Interpretations (First_Op);
7563 end if;
7565 if U2 then
7566 Remove_Address_Interpretations (Second_Op);
7567 end if;
7569 if not (U1 and U2) then
7570 Get_First_Interp (N, I, It);
7571 while Present (It.Nam) loop
7572 if Scope (It.Nam) = Standard_Standard
7573 and then It.Typ = Base_Type (Etype (Abstract_Op))
7574 then
7575 Remove_Interp (I);
7576 end if;
7578 Get_Next_Interp (I, It);
7579 end loop;
7580 end if;
7581 end;
7582 end if;
7584 -- If the removal has left no valid interpretations, emit an error
7585 -- message now and label node as illegal.
7587 if Present (Abstract_Op) then
7588 Get_First_Interp (N, I, It);
7590 if No (It.Nam) then
7592 -- Removal of abstract operation left no viable candidate
7594 Set_Etype (N, Any_Type);
7595 Error_Msg_Sloc := Sloc (Abstract_Op);
7596 Error_Msg_NE
7597 ("cannot call abstract operation& declared#", N, Abstract_Op);
7599 -- In Ada 2005, an abstract operation may disable predefined
7600 -- operators. Since the context is not yet known, we mark the
7601 -- predefined operators as potentially hidden. Do not include
7602 -- predefined operators when addresses are involved since this
7603 -- case is handled separately.
7605 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
7606 while Present (It.Nam) loop
7607 if Is_Numeric_Type (It.Typ)
7608 and then Scope (It.Typ) = Standard_Standard
7609 then
7610 Set_Abstract_Op (I, Abstract_Op);
7611 end if;
7613 Get_Next_Interp (I, It);
7614 end loop;
7615 end if;
7616 end if;
7618 if Debug_Flag_V then
7619 Write_Str ("Remove_Abstract_Operations done: ");
7620 Write_Overloads (N);
7621 end if;
7622 end if;
7623 end Remove_Abstract_Operations;
7625 ----------------------------
7626 -- Try_Container_Indexing --
7627 ----------------------------
7629 function Try_Container_Indexing
7630 (N : Node_Id;
7631 Prefix : Node_Id;
7632 Exprs : List_Id) return Boolean
7634 Pref_Typ : constant Entity_Id := Etype (Prefix);
7636 function Constant_Indexing_OK return Boolean;
7637 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7638 -- for the type, or else node not a target of assignment, or an actual
7639 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7641 function Expr_Matches_In_Formal
7642 (Subp : Entity_Id;
7643 Par : Node_Id) return Boolean;
7644 -- Find formal corresponding to given indexed component that is an
7645 -- actual in a call. Note that the enclosing subprogram call has not
7646 -- been analyzed yet, and the parameter list is not normalized, so
7647 -- that if the argument is a parameter association we must match it
7648 -- by name and not by position.
7650 function Find_Indexing_Operations
7651 (T : Entity_Id;
7652 Nam : Name_Id;
7653 Is_Constant : Boolean) return Node_Id;
7654 -- Return a reference to the primitive operation of type T denoted by
7655 -- name Nam. If the operation is overloaded, the reference carries all
7656 -- interpretations. Flag Is_Constant should be set when the context is
7657 -- constant indexing.
7659 --------------------------
7660 -- Constant_Indexing_OK --
7661 --------------------------
7663 function Constant_Indexing_OK return Boolean is
7664 Par : Node_Id;
7666 begin
7667 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
7668 return True;
7670 elsif not Is_Variable (Prefix) then
7671 return True;
7672 end if;
7674 Par := N;
7675 while Present (Par) loop
7676 if Nkind (Parent (Par)) = N_Assignment_Statement
7677 and then Par = Name (Parent (Par))
7678 then
7679 return False;
7681 -- The call may be overloaded, in which case we assume that its
7682 -- resolution does not depend on the type of the parameter that
7683 -- includes the indexing operation.
7685 elsif Nkind_In (Parent (Par), N_Function_Call,
7686 N_Procedure_Call_Statement)
7687 and then Is_Entity_Name (Name (Parent (Par)))
7688 then
7689 declare
7690 Proc : Entity_Id;
7692 begin
7693 -- We should look for an interpretation with the proper
7694 -- number of formals, and determine whether it is an
7695 -- In_Parameter, but for now we examine the formal that
7696 -- corresponds to the indexing, and assume that variable
7697 -- indexing is required if some interpretation has an
7698 -- assignable formal at that position. Still does not
7699 -- cover the most complex cases ???
7701 if Is_Overloaded (Name (Parent (Par))) then
7702 declare
7703 Proc : constant Node_Id := Name (Parent (Par));
7704 I : Interp_Index;
7705 It : Interp;
7707 begin
7708 Get_First_Interp (Proc, I, It);
7709 while Present (It.Nam) loop
7710 if not Expr_Matches_In_Formal (It.Nam, Par) then
7711 return False;
7712 end if;
7714 Get_Next_Interp (I, It);
7715 end loop;
7716 end;
7718 -- All interpretations have a matching in-mode formal
7720 return True;
7722 else
7723 Proc := Entity (Name (Parent (Par)));
7725 -- If this is an indirect call, get formals from
7726 -- designated type.
7728 if Is_Access_Subprogram_Type (Etype (Proc)) then
7729 Proc := Designated_Type (Etype (Proc));
7730 end if;
7731 end if;
7733 return Expr_Matches_In_Formal (Proc, Par);
7734 end;
7736 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7737 return False;
7739 -- If the indexed component is a prefix it may be the first actual
7740 -- of a prefixed call. Retrieve the called entity, if any, and
7741 -- check its first formal. Determine if the context is a procedure
7742 -- or function call.
7744 elsif Nkind (Parent (Par)) = N_Selected_Component then
7745 declare
7746 Sel : constant Node_Id := Selector_Name (Parent (Par));
7747 Nam : constant Entity_Id := Current_Entity (Sel);
7749 begin
7750 if Present (Nam) and then Is_Overloadable (Nam) then
7751 if Nkind (Parent (Parent (Par))) =
7752 N_Procedure_Call_Statement
7753 then
7754 return False;
7756 elsif Ekind (Nam) = E_Function
7757 and then Present (First_Formal (Nam))
7758 then
7759 return Ekind (First_Formal (Nam)) = E_In_Parameter;
7760 end if;
7761 end if;
7762 end;
7764 elsif Nkind (Par) in N_Op then
7765 return True;
7766 end if;
7768 Par := Parent (Par);
7769 end loop;
7771 -- In all other cases, constant indexing is legal
7773 return True;
7774 end Constant_Indexing_OK;
7776 ----------------------------
7777 -- Expr_Matches_In_Formal --
7778 ----------------------------
7780 function Expr_Matches_In_Formal
7781 (Subp : Entity_Id;
7782 Par : Node_Id) return Boolean
7784 Actual : Node_Id;
7785 Formal : Node_Id;
7787 begin
7788 Formal := First_Formal (Subp);
7789 Actual := First (Parameter_Associations ((Parent (Par))));
7791 if Nkind (Par) /= N_Parameter_Association then
7793 -- Match by position
7795 while Present (Actual) and then Present (Formal) loop
7796 exit when Actual = Par;
7797 Next (Actual);
7799 if Present (Formal) then
7800 Next_Formal (Formal);
7802 -- Otherwise this is a parameter mismatch, the error is
7803 -- reported elsewhere, or else variable indexing is implied.
7805 else
7806 return False;
7807 end if;
7808 end loop;
7810 else
7811 -- Match by name
7813 while Present (Formal) loop
7814 exit when Chars (Formal) = Chars (Selector_Name (Par));
7815 Next_Formal (Formal);
7817 if No (Formal) then
7818 return False;
7819 end if;
7820 end loop;
7821 end if;
7823 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
7824 end Expr_Matches_In_Formal;
7826 ------------------------------
7827 -- Find_Indexing_Operations --
7828 ------------------------------
7830 function Find_Indexing_Operations
7831 (T : Entity_Id;
7832 Nam : Name_Id;
7833 Is_Constant : Boolean) return Node_Id
7835 procedure Inspect_Declarations
7836 (Typ : Entity_Id;
7837 Ref : in out Node_Id);
7838 -- Traverse the declarative list where type Typ resides and collect
7839 -- all suitable interpretations in node Ref.
7841 procedure Inspect_Primitives
7842 (Typ : Entity_Id;
7843 Ref : in out Node_Id);
7844 -- Traverse the list of primitive operations of type Typ and collect
7845 -- all suitable interpretations in node Ref.
7847 function Is_OK_Candidate
7848 (Subp_Id : Entity_Id;
7849 Typ : Entity_Id) return Boolean;
7850 -- Determine whether subprogram Subp_Id is a suitable indexing
7851 -- operation for type Typ. To qualify as such, the subprogram must
7852 -- be a function, have at least two parameters, and the type of the
7853 -- first parameter must be either Typ, or Typ'Class, or access [to
7854 -- constant] with designated type Typ or Typ'Class.
7856 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7857 -- Store subprogram Subp_Id as an interpretation in node Ref
7859 --------------------------
7860 -- Inspect_Declarations --
7861 --------------------------
7863 procedure Inspect_Declarations
7864 (Typ : Entity_Id;
7865 Ref : in out Node_Id)
7867 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
7868 Decl : Node_Id;
7869 Subp_Id : Entity_Id;
7871 begin
7872 -- Ensure that the routine is not called with itypes, which lack a
7873 -- declarative node.
7875 pragma Assert (Present (Typ_Decl));
7876 pragma Assert (Is_List_Member (Typ_Decl));
7878 Decl := First (List_Containing (Typ_Decl));
7879 while Present (Decl) loop
7880 if Nkind (Decl) = N_Subprogram_Declaration then
7881 Subp_Id := Defining_Entity (Decl);
7883 if Is_OK_Candidate (Subp_Id, Typ) then
7884 Record_Interp (Subp_Id, Ref);
7885 end if;
7886 end if;
7888 Next (Decl);
7889 end loop;
7890 end Inspect_Declarations;
7892 ------------------------
7893 -- Inspect_Primitives --
7894 ------------------------
7896 procedure Inspect_Primitives
7897 (Typ : Entity_Id;
7898 Ref : in out Node_Id)
7900 Prim_Elmt : Elmt_Id;
7901 Prim_Id : Entity_Id;
7903 begin
7904 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
7905 while Present (Prim_Elmt) loop
7906 Prim_Id := Node (Prim_Elmt);
7908 if Is_OK_Candidate (Prim_Id, Typ) then
7909 Record_Interp (Prim_Id, Ref);
7910 end if;
7912 Next_Elmt (Prim_Elmt);
7913 end loop;
7914 end Inspect_Primitives;
7916 ---------------------
7917 -- Is_OK_Candidate --
7918 ---------------------
7920 function Is_OK_Candidate
7921 (Subp_Id : Entity_Id;
7922 Typ : Entity_Id) return Boolean
7924 Formal : Entity_Id;
7925 Formal_Typ : Entity_Id;
7926 Param_Typ : Node_Id;
7928 begin
7929 -- To classify as a suitable candidate, the subprogram must be a
7930 -- function whose name matches the argument of aspect Constant or
7931 -- Variable_Indexing.
7933 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
7934 Formal := First_Formal (Subp_Id);
7936 -- The candidate requires at least two parameters
7938 if Present (Formal) and then Present (Next_Formal (Formal)) then
7939 Formal_Typ := Empty;
7940 Param_Typ := Parameter_Type (Parent (Formal));
7942 -- Use the designated type when the first parameter is of an
7943 -- access type.
7945 if Nkind (Param_Typ) = N_Access_Definition
7946 and then Present (Subtype_Mark (Param_Typ))
7947 then
7948 -- When the context is a constant indexing, the access
7949 -- definition must be access-to-constant. This does not
7950 -- apply to variable indexing.
7952 if not Is_Constant
7953 or else Constant_Present (Param_Typ)
7954 then
7955 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
7956 end if;
7958 -- Otherwise use the parameter type
7960 else
7961 Formal_Typ := Etype (Param_Typ);
7962 end if;
7964 if Present (Formal_Typ) then
7966 -- Use the specific type when the parameter type is
7967 -- class-wide.
7969 if Is_Class_Wide_Type (Formal_Typ) then
7970 Formal_Typ := Etype (Base_Type (Formal_Typ));
7971 end if;
7973 -- Use the full view when the parameter type is private
7974 -- or incomplete.
7976 if Is_Incomplete_Or_Private_Type (Formal_Typ)
7977 and then Present (Full_View (Formal_Typ))
7978 then
7979 Formal_Typ := Full_View (Formal_Typ);
7980 end if;
7982 -- The type of the first parameter must denote the type
7983 -- of the container or acts as its ancestor type.
7985 return
7986 Formal_Typ = Typ
7987 or else Is_Ancestor (Formal_Typ, Typ);
7988 end if;
7989 end if;
7990 end if;
7992 return False;
7993 end Is_OK_Candidate;
7995 -------------------
7996 -- Record_Interp --
7997 -------------------
7999 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8000 begin
8001 if Present (Ref) then
8002 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8004 -- Otherwise this is the first interpretation. Create a reference
8005 -- where all remaining interpretations will be collected.
8007 else
8008 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8009 end if;
8010 end Record_Interp;
8012 -- Local variables
8014 Ref : Node_Id;
8015 Typ : Entity_Id;
8017 -- Start of processing for Find_Indexing_Operations
8019 begin
8020 Typ := T;
8022 -- Use the specific type when the parameter type is class-wide
8024 if Is_Class_Wide_Type (Typ) then
8025 Typ := Root_Type (Typ);
8026 end if;
8028 Ref := Empty;
8029 Typ := Underlying_Type (Base_Type (Typ));
8031 Inspect_Primitives (Typ, Ref);
8033 -- Now look for explicit declarations of an indexing operation.
8034 -- If the type is private the operation may be declared in the
8035 -- visible part that contains the partial view.
8037 if Is_Private_Type (T) then
8038 Inspect_Declarations (T, Ref);
8039 end if;
8041 Inspect_Declarations (Typ, Ref);
8043 return Ref;
8044 end Find_Indexing_Operations;
8046 -- Local variables
8048 Loc : constant Source_Ptr := Sloc (N);
8049 Assoc : List_Id;
8050 C_Type : Entity_Id;
8051 Func : Entity_Id;
8052 Func_Name : Node_Id;
8053 Indexing : Node_Id;
8055 Is_Constant_Indexing : Boolean := False;
8056 -- This flag reflects the nature of the container indexing. Note that
8057 -- the context may be suited for constant indexing, but the type may
8058 -- lack a Constant_Indexing annotation.
8060 -- Start of processing for Try_Container_Indexing
8062 begin
8063 -- Node may have been analyzed already when testing for a prefixed
8064 -- call, in which case do not redo analysis.
8066 if Present (Generalized_Indexing (N)) then
8067 return True;
8068 end if;
8070 C_Type := Pref_Typ;
8072 -- If indexing a class-wide container, obtain indexing primitive from
8073 -- specific type.
8075 if Is_Class_Wide_Type (C_Type) then
8076 C_Type := Etype (Base_Type (C_Type));
8077 end if;
8079 -- Check whether the type has a specified indexing aspect
8081 Func_Name := Empty;
8083 -- The context is suitable for constant indexing, so obtain the name of
8084 -- the indexing function from aspect Constant_Indexing.
8086 if Constant_Indexing_OK then
8087 Func_Name :=
8088 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
8089 end if;
8091 if Present (Func_Name) then
8092 Is_Constant_Indexing := True;
8094 -- Otherwise attempt variable indexing
8096 else
8097 Func_Name :=
8098 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
8099 end if;
8101 -- The type is not subject to either form of indexing, therefore the
8102 -- indexed component does not denote container indexing. If this is a
8103 -- true error, it is diagnosed by the caller.
8105 if No (Func_Name) then
8107 -- The prefix itself may be an indexing of a container. Rewrite it
8108 -- as such and retry.
8110 if Has_Implicit_Dereference (Pref_Typ) then
8111 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
8112 return Try_Container_Indexing (N, Prefix, Exprs);
8114 -- Otherwise this is definitely not container indexing
8116 else
8117 return False;
8118 end if;
8120 -- If the container type is derived from another container type, the
8121 -- value of the inherited aspect is the Reference operation declared
8122 -- for the parent type.
8124 -- However, Reference is also a primitive operation of the type, and the
8125 -- inherited operation has a different signature. We retrieve the right
8126 -- ones (the function may be overloaded) from the list of primitive
8127 -- operations of the derived type.
8129 -- Note that predefined containers are typically all derived from one of
8130 -- the Controlled types. The code below is motivated by containers that
8131 -- are derived from other types with a Reference aspect.
8133 elsif Is_Derived_Type (C_Type)
8134 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
8135 then
8136 Func_Name :=
8137 Find_Indexing_Operations
8138 (T => C_Type,
8139 Nam => Chars (Func_Name),
8140 Is_Constant => Is_Constant_Indexing);
8141 end if;
8143 Assoc := New_List (Relocate_Node (Prefix));
8145 -- A generalized indexing may have nore than one index expression, so
8146 -- transfer all of them to the argument list to be used in the call.
8147 -- Note that there may be named associations, in which case the node
8148 -- was rewritten earlier as a call, and has been transformed back into
8149 -- an indexed expression to share the following processing.
8151 -- The generalized indexing node is the one on which analysis and
8152 -- resolution take place. Before expansion the original node is replaced
8153 -- with the generalized indexing node, which is a call, possibly with a
8154 -- dereference operation.
8156 if Comes_From_Source (N) then
8157 Check_Compiler_Unit ("generalized indexing", N);
8158 end if;
8160 -- Create argument list for function call that represents generalized
8161 -- indexing. Note that indices (i.e. actuals) may themselves be
8162 -- overloaded.
8164 declare
8165 Arg : Node_Id;
8166 New_Arg : Node_Id;
8168 begin
8169 Arg := First (Exprs);
8170 while Present (Arg) loop
8171 New_Arg := Relocate_Node (Arg);
8173 -- The arguments can be parameter associations, in which case the
8174 -- explicit actual parameter carries the overloadings.
8176 if Nkind (New_Arg) /= N_Parameter_Association then
8177 Save_Interps (Arg, New_Arg);
8178 end if;
8180 Append (New_Arg, Assoc);
8181 Next (Arg);
8182 end loop;
8183 end;
8185 if not Is_Overloaded (Func_Name) then
8186 Func := Entity (Func_Name);
8188 Indexing :=
8189 Make_Function_Call (Loc,
8190 Name => New_Occurrence_Of (Func, Loc),
8191 Parameter_Associations => Assoc);
8193 Set_Parent (Indexing, Parent (N));
8194 Set_Generalized_Indexing (N, Indexing);
8195 Analyze (Indexing);
8196 Set_Etype (N, Etype (Indexing));
8198 -- If the return type of the indexing function is a reference type,
8199 -- add the dereference as a possible interpretation. Note that the
8200 -- indexing aspect may be a function that returns the element type
8201 -- with no intervening implicit dereference, and that the reference
8202 -- discriminant is not the first discriminant.
8204 if Has_Discriminants (Etype (Func)) then
8205 Check_Implicit_Dereference (N, Etype (Func));
8206 end if;
8208 else
8209 -- If there are multiple indexing functions, build a function call
8210 -- and analyze it for each of the possible interpretations.
8212 Indexing :=
8213 Make_Function_Call (Loc,
8214 Name =>
8215 Make_Identifier (Loc, Chars (Func_Name)),
8216 Parameter_Associations => Assoc);
8217 Set_Parent (Indexing, Parent (N));
8218 Set_Generalized_Indexing (N, Indexing);
8219 Set_Etype (N, Any_Type);
8220 Set_Etype (Name (Indexing), Any_Type);
8222 declare
8223 I : Interp_Index;
8224 It : Interp;
8225 Success : Boolean;
8227 begin
8228 Get_First_Interp (Func_Name, I, It);
8229 Set_Etype (Indexing, Any_Type);
8231 -- Analyze each candidate function with the given actuals
8233 while Present (It.Nam) loop
8234 Analyze_One_Call (Indexing, It.Nam, False, Success);
8235 Get_Next_Interp (I, It);
8236 end loop;
8238 -- If there are several successful candidates, resolution will
8239 -- be by result. Mark the interpretations of the function name
8240 -- itself.
8242 if Is_Overloaded (Indexing) then
8243 Get_First_Interp (Indexing, I, It);
8245 while Present (It.Nam) loop
8246 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
8247 Get_Next_Interp (I, It);
8248 end loop;
8250 else
8251 Set_Etype (Name (Indexing), Etype (Indexing));
8252 end if;
8254 -- Now add the candidate interpretations to the indexing node
8255 -- itself, to be replaced later by the function call.
8257 if Is_Overloaded (Name (Indexing)) then
8258 Get_First_Interp (Name (Indexing), I, It);
8260 while Present (It.Nam) loop
8261 Add_One_Interp (N, It.Nam, It.Typ);
8263 -- Add dereference interpretation if the result type has
8264 -- implicit reference discriminants.
8266 if Has_Discriminants (Etype (It.Nam)) then
8267 Check_Implicit_Dereference (N, Etype (It.Nam));
8268 end if;
8270 Get_Next_Interp (I, It);
8271 end loop;
8273 else
8274 Set_Etype (N, Etype (Name (Indexing)));
8275 if Has_Discriminants (Etype (N)) then
8276 Check_Implicit_Dereference (N, Etype (N));
8277 end if;
8278 end if;
8279 end;
8280 end if;
8282 if Etype (Indexing) = Any_Type then
8283 Error_Msg_NE
8284 ("container cannot be indexed with&", N, Etype (First (Exprs)));
8285 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
8286 end if;
8288 return True;
8289 end Try_Container_Indexing;
8291 -----------------------
8292 -- Try_Indirect_Call --
8293 -----------------------
8295 function Try_Indirect_Call
8296 (N : Node_Id;
8297 Nam : Entity_Id;
8298 Typ : Entity_Id) return Boolean
8300 Actual : Node_Id;
8301 Formal : Entity_Id;
8303 Call_OK : Boolean;
8304 pragma Warnings (Off, Call_OK);
8306 begin
8307 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
8309 Actual := First_Actual (N);
8310 Formal := First_Formal (Designated_Type (Typ));
8311 while Present (Actual) and then Present (Formal) loop
8312 if not Has_Compatible_Type (Actual, Etype (Formal)) then
8313 return False;
8314 end if;
8316 Next (Actual);
8317 Next_Formal (Formal);
8318 end loop;
8320 if No (Actual) and then No (Formal) then
8321 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
8323 -- Nam is a candidate interpretation for the name in the call,
8324 -- if it is not an indirect call.
8326 if not Is_Type (Nam)
8327 and then Is_Entity_Name (Name (N))
8328 then
8329 Set_Entity (Name (N), Nam);
8330 end if;
8332 return True;
8334 else
8335 return False;
8336 end if;
8337 end Try_Indirect_Call;
8339 ----------------------
8340 -- Try_Indexed_Call --
8341 ----------------------
8343 function Try_Indexed_Call
8344 (N : Node_Id;
8345 Nam : Entity_Id;
8346 Typ : Entity_Id;
8347 Skip_First : Boolean) return Boolean
8349 Loc : constant Source_Ptr := Sloc (N);
8350 Actuals : constant List_Id := Parameter_Associations (N);
8351 Actual : Node_Id;
8352 Index : Entity_Id;
8354 begin
8355 Actual := First (Actuals);
8357 -- If the call was originally written in prefix form, skip the first
8358 -- actual, which is obviously not defaulted.
8360 if Skip_First then
8361 Next (Actual);
8362 end if;
8364 Index := First_Index (Typ);
8365 while Present (Actual) and then Present (Index) loop
8367 -- If the parameter list has a named association, the expression
8368 -- is definitely a call and not an indexed component.
8370 if Nkind (Actual) = N_Parameter_Association then
8371 return False;
8372 end if;
8374 if Is_Entity_Name (Actual)
8375 and then Is_Type (Entity (Actual))
8376 and then No (Next (Actual))
8377 then
8378 -- A single actual that is a type name indicates a slice if the
8379 -- type is discrete, and an error otherwise.
8381 if Is_Discrete_Type (Entity (Actual)) then
8382 Rewrite (N,
8383 Make_Slice (Loc,
8384 Prefix =>
8385 Make_Function_Call (Loc,
8386 Name => Relocate_Node (Name (N))),
8387 Discrete_Range =>
8388 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8390 Analyze (N);
8392 else
8393 Error_Msg_N ("invalid use of type in expression", Actual);
8394 Set_Etype (N, Any_Type);
8395 end if;
8397 return True;
8399 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
8400 return False;
8401 end if;
8403 Next (Actual);
8404 Next_Index (Index);
8405 end loop;
8407 if No (Actual) and then No (Index) then
8408 Add_One_Interp (N, Nam, Component_Type (Typ));
8410 -- Nam is a candidate interpretation for the name in the call,
8411 -- if it is not an indirect call.
8413 if not Is_Type (Nam)
8414 and then Is_Entity_Name (Name (N))
8415 then
8416 Set_Entity (Name (N), Nam);
8417 end if;
8419 return True;
8420 else
8421 return False;
8422 end if;
8423 end Try_Indexed_Call;
8425 --------------------------
8426 -- Try_Object_Operation --
8427 --------------------------
8429 function Try_Object_Operation
8430 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8432 K : constant Node_Kind := Nkind (Parent (N));
8433 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
8434 Loc : constant Source_Ptr := Sloc (N);
8435 Obj : constant Node_Id := Prefix (N);
8437 Subprog : constant Node_Id :=
8438 Make_Identifier (Sloc (Selector_Name (N)),
8439 Chars => Chars (Selector_Name (N)));
8440 -- Identifier on which possible interpretations will be collected
8442 Report_Error : Boolean := False;
8443 -- If no candidate interpretation matches the context, redo analysis
8444 -- with Report_Error True to provide additional information.
8446 Actual : Node_Id;
8447 Candidate : Entity_Id := Empty;
8448 New_Call_Node : Node_Id := Empty;
8449 Node_To_Replace : Node_Id;
8450 Obj_Type : Entity_Id := Etype (Obj);
8451 Success : Boolean := False;
8453 procedure Complete_Object_Operation
8454 (Call_Node : Node_Id;
8455 Node_To_Replace : Node_Id);
8456 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8457 -- Call_Node, insert the object (or its dereference) as the first actual
8458 -- in the call, and complete the analysis of the call.
8460 procedure Report_Ambiguity (Op : Entity_Id);
8461 -- If a prefixed procedure call is ambiguous, indicate whether the call
8462 -- includes an implicit dereference or an implicit 'Access.
8464 procedure Transform_Object_Operation
8465 (Call_Node : out Node_Id;
8466 Node_To_Replace : out Node_Id);
8467 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
8468 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8469 -- either N or the parent of N, and Subprog is a reference to the
8470 -- subprogram we are trying to match.
8472 function Try_Class_Wide_Operation
8473 (Call_Node : Node_Id;
8474 Node_To_Replace : Node_Id) return Boolean;
8475 -- Traverse all ancestor types looking for a class-wide subprogram for
8476 -- which the current operation is a valid non-dispatching call.
8478 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8479 -- If prefix is overloaded, its interpretation may include different
8480 -- tagged types, and we must examine the primitive operations and the
8481 -- class-wide operations of each in order to find candidate
8482 -- interpretations for the call as a whole.
8484 function Try_Primitive_Operation
8485 (Call_Node : Node_Id;
8486 Node_To_Replace : Node_Id) return Boolean;
8487 -- Traverse the list of primitive subprograms looking for a dispatching
8488 -- operation for which the current node is a valid call.
8490 function Valid_Candidate
8491 (Success : Boolean;
8492 Call : Node_Id;
8493 Subp : Entity_Id) return Entity_Id;
8494 -- If the subprogram is a valid interpretation, record it, and add to
8495 -- the list of interpretations of Subprog. Otherwise return Empty.
8497 -------------------------------
8498 -- Complete_Object_Operation --
8499 -------------------------------
8501 procedure Complete_Object_Operation
8502 (Call_Node : Node_Id;
8503 Node_To_Replace : Node_Id)
8505 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8506 Formal_Type : constant Entity_Id := Etype (Control);
8507 First_Actual : Node_Id;
8509 begin
8510 -- Place the name of the operation, with its interpretations,
8511 -- on the rewritten call.
8513 Set_Name (Call_Node, Subprog);
8515 First_Actual := First (Parameter_Associations (Call_Node));
8517 -- For cross-reference purposes, treat the new node as being in the
8518 -- source if the original one is. Set entity and type, even though
8519 -- they may be overwritten during resolution if overloaded.
8521 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8522 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8524 if Nkind (N) = N_Selected_Component
8525 and then not Inside_A_Generic
8526 then
8527 Set_Entity (Selector_Name (N), Entity (Subprog));
8528 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
8529 end if;
8531 -- If need be, rewrite first actual as an explicit dereference. If
8532 -- the call is overloaded, the rewriting can only be done once the
8533 -- primitive operation is identified.
8535 if Is_Overloaded (Subprog) then
8537 -- The prefix itself may be overloaded, and its interpretations
8538 -- must be propagated to the new actual in the call.
8540 if Is_Overloaded (Obj) then
8541 Save_Interps (Obj, First_Actual);
8542 end if;
8544 Rewrite (First_Actual, Obj);
8546 elsif not Is_Access_Type (Formal_Type)
8547 and then Is_Access_Type (Etype (Obj))
8548 then
8549 Rewrite (First_Actual,
8550 Make_Explicit_Dereference (Sloc (Obj), Obj));
8551 Analyze (First_Actual);
8553 -- If we need to introduce an explicit dereference, verify that
8554 -- the resulting actual is compatible with the mode of the formal.
8556 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8557 and then Is_Access_Constant (Etype (Obj))
8558 then
8559 Error_Msg_NE
8560 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8561 end if;
8563 -- Conversely, if the formal is an access parameter and the object is
8564 -- not an access type or a reference type (i.e. a type with the
8565 -- Implicit_Dereference aspect specified), replace the actual with a
8566 -- 'Access reference. Its analysis will check that the object is
8567 -- aliased.
8569 elsif Is_Access_Type (Formal_Type)
8570 and then not Is_Access_Type (Etype (Obj))
8571 and then
8572 (not Has_Implicit_Dereference (Etype (Obj))
8573 or else
8574 not Is_Access_Type (Designated_Type (Etype
8575 (Get_Reference_Discriminant (Etype (Obj))))))
8576 then
8577 -- A special case: A.all'Access is illegal if A is an access to a
8578 -- constant and the context requires an access to a variable.
8580 if not Is_Access_Constant (Formal_Type) then
8581 if (Nkind (Obj) = N_Explicit_Dereference
8582 and then Is_Access_Constant (Etype (Prefix (Obj))))
8583 or else not Is_Variable (Obj)
8584 then
8585 Error_Msg_NE
8586 ("actual for & must be a variable", Obj, Control);
8587 end if;
8588 end if;
8590 Rewrite (First_Actual,
8591 Make_Attribute_Reference (Loc,
8592 Attribute_Name => Name_Access,
8593 Prefix => Relocate_Node (Obj)));
8595 -- If the object is not overloaded verify that taking access of
8596 -- it is legal. Otherwise check is made during resolution.
8598 if not Is_Overloaded (Obj)
8599 and then not Is_Aliased_View (Obj)
8600 then
8601 Error_Msg_NE
8602 ("object in prefixed call to & must be aliased "
8603 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
8604 end if;
8606 Analyze (First_Actual);
8608 else
8609 if Is_Overloaded (Obj) then
8610 Save_Interps (Obj, First_Actual);
8611 end if;
8613 Rewrite (First_Actual, Obj);
8614 end if;
8616 -- The operation is obtained from the dispatch table and not by
8617 -- visibility, and may be declared in a unit that is not explicitly
8618 -- referenced in the source, but is nevertheless required in the
8619 -- context of the current unit. Indicate that operation and its scope
8620 -- are referenced, to prevent spurious and misleading warnings. If
8621 -- the operation is overloaded, all primitives are in the same scope
8622 -- and we can use any of them.
8624 Set_Referenced (Entity (Subprog), True);
8625 Set_Referenced (Scope (Entity (Subprog)), True);
8627 Rewrite (Node_To_Replace, Call_Node);
8629 -- Propagate the interpretations collected in subprog to the new
8630 -- function call node, to be resolved from context.
8632 if Is_Overloaded (Subprog) then
8633 Save_Interps (Subprog, Node_To_Replace);
8635 else
8636 -- The type of the subprogram may be a limited view obtained
8637 -- transitively from another unit. If full view is available,
8638 -- use it to analyze call.
8640 declare
8641 T : constant Entity_Id := Etype (Subprog);
8642 begin
8643 if From_Limited_With (T) then
8644 Set_Etype (Entity (Subprog), Available_View (T));
8645 end if;
8646 end;
8648 Analyze (Node_To_Replace);
8650 -- If the operation has been rewritten into a call, which may get
8651 -- subsequently an explicit dereference, preserve the type on the
8652 -- original node (selected component or indexed component) for
8653 -- subsequent legality tests, e.g. Is_Variable. which examines
8654 -- the original node.
8656 if Nkind (Node_To_Replace) = N_Function_Call then
8657 Set_Etype
8658 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8659 end if;
8660 end if;
8661 end Complete_Object_Operation;
8663 ----------------------
8664 -- Report_Ambiguity --
8665 ----------------------
8667 procedure Report_Ambiguity (Op : Entity_Id) is
8668 Access_Actual : constant Boolean :=
8669 Is_Access_Type (Etype (Prefix (N)));
8670 Access_Formal : Boolean := False;
8672 begin
8673 Error_Msg_Sloc := Sloc (Op);
8675 if Present (First_Formal (Op)) then
8676 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8677 end if;
8679 if Access_Formal and then not Access_Actual then
8680 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8681 Error_Msg_N
8682 ("\possible interpretation "
8683 & "(inherited, with implicit 'Access) #", N);
8684 else
8685 Error_Msg_N
8686 ("\possible interpretation (with implicit 'Access) #", N);
8687 end if;
8689 elsif not Access_Formal and then Access_Actual then
8690 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8691 Error_Msg_N
8692 ("\possible interpretation "
8693 & "(inherited, with implicit dereference) #", N);
8694 else
8695 Error_Msg_N
8696 ("\possible interpretation (with implicit dereference) #", N);
8697 end if;
8699 else
8700 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8701 Error_Msg_N ("\possible interpretation (inherited)#", N);
8702 else
8703 Error_Msg_N -- CODEFIX
8704 ("\possible interpretation#", N);
8705 end if;
8706 end if;
8707 end Report_Ambiguity;
8709 --------------------------------
8710 -- Transform_Object_Operation --
8711 --------------------------------
8713 procedure Transform_Object_Operation
8714 (Call_Node : out Node_Id;
8715 Node_To_Replace : out Node_Id)
8717 Dummy : constant Node_Id := New_Copy (Obj);
8718 -- Placeholder used as a first parameter in the call, replaced
8719 -- eventually by the proper object.
8721 Parent_Node : constant Node_Id := Parent (N);
8723 Actual : Node_Id;
8724 Actuals : List_Id;
8726 begin
8727 -- Obj may already have been rewritten if it involves an implicit
8728 -- dereference (e.g. if it is an access to a limited view). Preserve
8729 -- a link to the original node for ASIS use.
8731 if not Comes_From_Source (Obj) then
8732 Set_Original_Node (Dummy, Original_Node (Obj));
8733 end if;
8735 -- Common case covering 1) Call to a procedure and 2) Call to a
8736 -- function that has some additional actuals.
8738 if Nkind (Parent_Node) in N_Subprogram_Call
8740 -- N is a selected component node containing the name of the
8741 -- subprogram. If N is not the name of the parent node we must
8742 -- not replace the parent node by the new construct. This case
8743 -- occurs when N is a parameterless call to a subprogram that
8744 -- is an actual parameter of a call to another subprogram. For
8745 -- example:
8746 -- Some_Subprogram (..., Obj.Operation, ...)
8748 and then Name (Parent_Node) = N
8749 then
8750 Node_To_Replace := Parent_Node;
8752 Actuals := Parameter_Associations (Parent_Node);
8754 if Present (Actuals) then
8755 Prepend (Dummy, Actuals);
8756 else
8757 Actuals := New_List (Dummy);
8758 end if;
8760 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8761 Call_Node :=
8762 Make_Procedure_Call_Statement (Loc,
8763 Name => New_Copy (Subprog),
8764 Parameter_Associations => Actuals);
8766 else
8767 Call_Node :=
8768 Make_Function_Call (Loc,
8769 Name => New_Copy (Subprog),
8770 Parameter_Associations => Actuals);
8771 end if;
8773 -- Before analysis, a function call appears as an indexed component
8774 -- if there are no named associations.
8776 elsif Nkind (Parent_Node) = N_Indexed_Component
8777 and then N = Prefix (Parent_Node)
8778 then
8779 Node_To_Replace := Parent_Node;
8780 Actuals := Expressions (Parent_Node);
8782 Actual := First (Actuals);
8783 while Present (Actual) loop
8784 Analyze (Actual);
8785 Next (Actual);
8786 end loop;
8788 Prepend (Dummy, Actuals);
8790 Call_Node :=
8791 Make_Function_Call (Loc,
8792 Name => New_Copy (Subprog),
8793 Parameter_Associations => Actuals);
8795 -- Parameterless call: Obj.F is rewritten as F (Obj)
8797 else
8798 Node_To_Replace := N;
8800 Call_Node :=
8801 Make_Function_Call (Loc,
8802 Name => New_Copy (Subprog),
8803 Parameter_Associations => New_List (Dummy));
8804 end if;
8805 end Transform_Object_Operation;
8807 ------------------------------
8808 -- Try_Class_Wide_Operation --
8809 ------------------------------
8811 function Try_Class_Wide_Operation
8812 (Call_Node : Node_Id;
8813 Node_To_Replace : Node_Id) return Boolean
8815 Anc_Type : Entity_Id;
8816 Matching_Op : Entity_Id := Empty;
8817 Error : Boolean;
8819 procedure Traverse_Homonyms
8820 (Anc_Type : Entity_Id;
8821 Error : out Boolean);
8822 -- Traverse the homonym chain of the subprogram searching for those
8823 -- homonyms whose first formal has the Anc_Type's class-wide type,
8824 -- or an anonymous access type designating the class-wide type. If
8825 -- an ambiguity is detected, then Error is set to True.
8827 procedure Traverse_Interfaces
8828 (Anc_Type : Entity_Id;
8829 Error : out Boolean);
8830 -- Traverse the list of interfaces, if any, associated with Anc_Type
8831 -- and search for acceptable class-wide homonyms associated with each
8832 -- interface. If an ambiguity is detected, then Error is set to True.
8834 -----------------------
8835 -- Traverse_Homonyms --
8836 -----------------------
8838 procedure Traverse_Homonyms
8839 (Anc_Type : Entity_Id;
8840 Error : out Boolean)
8842 Cls_Type : Entity_Id;
8843 Hom : Entity_Id;
8844 Hom_Ref : Node_Id;
8845 Success : Boolean;
8847 begin
8848 Error := False;
8850 Cls_Type := Class_Wide_Type (Anc_Type);
8852 Hom := Current_Entity (Subprog);
8854 -- Find a non-hidden operation whose first parameter is of the
8855 -- class-wide type, a subtype thereof, or an anonymous access
8856 -- to same. If in an instance, the operation can be considered
8857 -- even if hidden (it may be hidden because the instantiation
8858 -- is expanded after the containing package has been analyzed).
8860 while Present (Hom) loop
8861 if Ekind_In (Hom, E_Procedure, E_Function)
8862 and then (not Is_Hidden (Hom) or else In_Instance)
8863 and then Scope (Hom) = Scope (Anc_Type)
8864 and then Present (First_Formal (Hom))
8865 and then
8866 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
8867 or else
8868 (Is_Access_Type (Etype (First_Formal (Hom)))
8869 and then
8870 Ekind (Etype (First_Formal (Hom))) =
8871 E_Anonymous_Access_Type
8872 and then
8873 Base_Type
8874 (Designated_Type (Etype (First_Formal (Hom)))) =
8875 Cls_Type))
8876 then
8877 -- If the context is a procedure call, ignore functions
8878 -- in the name of the call.
8880 if Ekind (Hom) = E_Function
8881 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
8882 and then N = Name (Parent (N))
8883 then
8884 goto Next_Hom;
8886 -- If the context is a function call, ignore procedures
8887 -- in the name of the call.
8889 elsif Ekind (Hom) = E_Procedure
8890 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
8891 then
8892 goto Next_Hom;
8893 end if;
8895 Set_Etype (Call_Node, Any_Type);
8896 Set_Is_Overloaded (Call_Node, False);
8897 Success := False;
8899 if No (Matching_Op) then
8900 Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog));
8901 Set_Etype (Call_Node, Any_Type);
8902 Set_Parent (Call_Node, Parent (Node_To_Replace));
8904 Set_Name (Call_Node, Hom_Ref);
8906 Analyze_One_Call
8907 (N => Call_Node,
8908 Nam => Hom,
8909 Report => Report_Error,
8910 Success => Success,
8911 Skip_First => True);
8913 Matching_Op :=
8914 Valid_Candidate (Success, Call_Node, Hom);
8916 else
8917 Analyze_One_Call
8918 (N => Call_Node,
8919 Nam => Hom,
8920 Report => Report_Error,
8921 Success => Success,
8922 Skip_First => True);
8924 if Present (Valid_Candidate (Success, Call_Node, Hom))
8925 and then Nkind (Call_Node) /= N_Function_Call
8926 then
8927 Error_Msg_NE ("ambiguous call to&", N, Hom);
8928 Report_Ambiguity (Matching_Op);
8929 Report_Ambiguity (Hom);
8930 Error := True;
8931 return;
8932 end if;
8933 end if;
8934 end if;
8936 <<Next_Hom>>
8937 Hom := Homonym (Hom);
8938 end loop;
8939 end Traverse_Homonyms;
8941 -------------------------
8942 -- Traverse_Interfaces --
8943 -------------------------
8945 procedure Traverse_Interfaces
8946 (Anc_Type : Entity_Id;
8947 Error : out Boolean)
8949 Intface_List : constant List_Id :=
8950 Abstract_Interface_List (Anc_Type);
8951 Intface : Node_Id;
8953 begin
8954 Error := False;
8956 if Is_Non_Empty_List (Intface_List) then
8957 Intface := First (Intface_List);
8958 while Present (Intface) loop
8960 -- Look for acceptable class-wide homonyms associated with
8961 -- the interface.
8963 Traverse_Homonyms (Etype (Intface), Error);
8965 if Error then
8966 return;
8967 end if;
8969 -- Continue the search by looking at each of the interface's
8970 -- associated interface ancestors.
8972 Traverse_Interfaces (Etype (Intface), Error);
8974 if Error then
8975 return;
8976 end if;
8978 Next (Intface);
8979 end loop;
8980 end if;
8981 end Traverse_Interfaces;
8983 -- Start of processing for Try_Class_Wide_Operation
8985 begin
8986 -- If we are searching only for conflicting class-wide subprograms
8987 -- then initialize directly Matching_Op with the target entity.
8989 if CW_Test_Only then
8990 Matching_Op := Entity (Selector_Name (N));
8991 end if;
8993 -- Loop through ancestor types (including interfaces), traversing
8994 -- the homonym chain of the subprogram, trying out those homonyms
8995 -- whose first formal has the class-wide type of the ancestor, or
8996 -- an anonymous access type designating the class-wide type.
8998 Anc_Type := Obj_Type;
8999 loop
9000 -- Look for a match among homonyms associated with the ancestor
9002 Traverse_Homonyms (Anc_Type, Error);
9004 if Error then
9005 return True;
9006 end if;
9008 -- Continue the search for matches among homonyms associated with
9009 -- any interfaces implemented by the ancestor.
9011 Traverse_Interfaces (Anc_Type, Error);
9013 if Error then
9014 return True;
9015 end if;
9017 exit when Etype (Anc_Type) = Anc_Type;
9018 Anc_Type := Etype (Anc_Type);
9019 end loop;
9021 if Present (Matching_Op) then
9022 Set_Etype (Call_Node, Etype (Matching_Op));
9023 end if;
9025 return Present (Matching_Op);
9026 end Try_Class_Wide_Operation;
9028 -----------------------------------
9029 -- Try_One_Prefix_Interpretation --
9030 -----------------------------------
9032 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
9033 Prev_Obj_Type : constant Entity_Id := Obj_Type;
9034 -- If the interpretation does not have a valid candidate type,
9035 -- preserve current value of Obj_Type for subsequent errors.
9037 begin
9038 Obj_Type := T;
9040 if Is_Access_Type (Obj_Type) then
9041 Obj_Type := Designated_Type (Obj_Type);
9042 end if;
9044 if Ekind_In (Obj_Type, E_Private_Subtype,
9045 E_Record_Subtype_With_Private)
9046 then
9047 Obj_Type := Base_Type (Obj_Type);
9048 end if;
9050 if Is_Class_Wide_Type (Obj_Type) then
9051 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9052 end if;
9054 -- The type may have be obtained through a limited_with clause,
9055 -- in which case the primitive operations are available on its
9056 -- nonlimited view. If still incomplete, retrieve full view.
9058 if Ekind (Obj_Type) = E_Incomplete_Type
9059 and then From_Limited_With (Obj_Type)
9060 and then Has_Non_Limited_View (Obj_Type)
9061 then
9062 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
9063 end if;
9065 -- If the object is not tagged, or the type is still an incomplete
9066 -- type, this is not a prefixed call. Restore the previous type as
9067 -- the current one is not a legal candidate.
9069 if not Is_Tagged_Type (Obj_Type)
9070 or else Is_Incomplete_Type (Obj_Type)
9071 then
9072 Obj_Type := Prev_Obj_Type;
9073 return;
9074 end if;
9076 declare
9077 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
9078 CW_Result : Boolean;
9079 Prim_Result : Boolean;
9080 pragma Unreferenced (CW_Result);
9082 begin
9083 if not CW_Test_Only then
9084 Prim_Result :=
9085 Try_Primitive_Operation
9086 (Call_Node => New_Call_Node,
9087 Node_To_Replace => Node_To_Replace);
9088 end if;
9090 -- Check if there is a class-wide subprogram covering the
9091 -- primitive. This check must be done even if a candidate
9092 -- was found in order to report ambiguous calls.
9094 if not Prim_Result then
9095 CW_Result :=
9096 Try_Class_Wide_Operation
9097 (Call_Node => New_Call_Node,
9098 Node_To_Replace => Node_To_Replace);
9100 -- If we found a primitive we search for class-wide subprograms
9101 -- using a duplicate of the call node (done to avoid missing its
9102 -- decoration if there is no ambiguity).
9104 else
9105 CW_Result :=
9106 Try_Class_Wide_Operation
9107 (Call_Node => Dup_Call_Node,
9108 Node_To_Replace => Node_To_Replace);
9109 end if;
9110 end;
9111 end Try_One_Prefix_Interpretation;
9113 -----------------------------
9114 -- Try_Primitive_Operation --
9115 -----------------------------
9117 function Try_Primitive_Operation
9118 (Call_Node : Node_Id;
9119 Node_To_Replace : Node_Id) return Boolean
9121 Elmt : Elmt_Id;
9122 Prim_Op : Entity_Id;
9123 Matching_Op : Entity_Id := Empty;
9124 Prim_Op_Ref : Node_Id := Empty;
9126 Corr_Type : Entity_Id := Empty;
9127 -- If the prefix is a synchronized type, the controlling type of
9128 -- the primitive operation is the corresponding record type, else
9129 -- this is the object type itself.
9131 Success : Boolean := False;
9133 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9134 -- For tagged types the candidate interpretations are found in
9135 -- the list of primitive operations of the type and its ancestors.
9136 -- For formal tagged types we have to find the operations declared
9137 -- in the same scope as the type (including in the generic formal
9138 -- part) because the type itself carries no primitive operations,
9139 -- except for formal derived types that inherit the operations of
9140 -- the parent and progenitors.
9142 -- If the context is a generic subprogram body, the generic formals
9143 -- are visible by name, but are not in the entity list of the
9144 -- subprogram because that list starts with the subprogram formals.
9145 -- We retrieve the candidate operations from the generic declaration.
9147 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9148 -- Prefix notation can also be used on operations that are not
9149 -- primitives of the type, but are declared in the same immediate
9150 -- declarative part, which can only mean the corresponding package
9151 -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the
9152 -- list of primitives with body operations with the same name that
9153 -- may be candidates, so that Try_Primitive_Operations can examine
9154 -- them if no real primitive is found.
9156 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
9157 -- An operation that overrides an inherited operation in the private
9158 -- part of its package may be hidden, but if the inherited operation
9159 -- is visible a direct call to it will dispatch to the private one,
9160 -- which is therefore a valid candidate.
9162 function Names_Match
9163 (Obj_Type : Entity_Id;
9164 Prim_Op : Entity_Id;
9165 Subprog : Entity_Id) return Boolean;
9166 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
9167 -- is a protected type then compare also the original name of Prim_Op
9168 -- with the name of Subprog (since the expander may have added a
9169 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
9171 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
9172 -- Verify that the prefix, dereferenced if need be, is a valid
9173 -- controlling argument in a call to Op. The remaining actuals
9174 -- are checked in the subsequent call to Analyze_One_Call.
9176 ------------------------------
9177 -- Collect_Generic_Type_Ops --
9178 ------------------------------
9180 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
9181 Bas : constant Entity_Id := Base_Type (T);
9182 Candidates : constant Elist_Id := New_Elmt_List;
9183 Subp : Entity_Id;
9184 Formal : Entity_Id;
9186 procedure Check_Candidate;
9187 -- The operation is a candidate if its first parameter is a
9188 -- controlling operand of the desired type.
9190 -----------------------
9191 -- Check_Candidate; --
9192 -----------------------
9194 procedure Check_Candidate is
9195 begin
9196 Formal := First_Formal (Subp);
9198 if Present (Formal)
9199 and then Is_Controlling_Formal (Formal)
9200 and then
9201 (Base_Type (Etype (Formal)) = Bas
9202 or else
9203 (Is_Access_Type (Etype (Formal))
9204 and then Designated_Type (Etype (Formal)) = Bas))
9205 then
9206 Append_Elmt (Subp, Candidates);
9207 end if;
9208 end Check_Candidate;
9210 -- Start of processing for Collect_Generic_Type_Ops
9212 begin
9213 if Is_Derived_Type (T) then
9214 return Primitive_Operations (T);
9216 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
9218 -- Scan the list of generic formals to find subprograms
9219 -- that may have a first controlling formal of the type.
9221 if Nkind (Unit_Declaration_Node (Scope (T))) =
9222 N_Generic_Subprogram_Declaration
9223 then
9224 declare
9225 Decl : Node_Id;
9227 begin
9228 Decl :=
9229 First (Generic_Formal_Declarations
9230 (Unit_Declaration_Node (Scope (T))));
9231 while Present (Decl) loop
9232 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
9233 Subp := Defining_Entity (Decl);
9234 Check_Candidate;
9235 end if;
9237 Next (Decl);
9238 end loop;
9239 end;
9240 end if;
9241 return Candidates;
9243 else
9244 -- Scan the list of entities declared in the same scope as
9245 -- the type. In general this will be an open scope, given that
9246 -- the call we are analyzing can only appear within a generic
9247 -- declaration or body (either the one that declares T, or a
9248 -- child unit).
9250 -- For a subtype representing a generic actual type, go to the
9251 -- base type.
9253 if Is_Generic_Actual_Type (T) then
9254 Subp := First_Entity (Scope (Base_Type (T)));
9255 else
9256 Subp := First_Entity (Scope (T));
9257 end if;
9259 while Present (Subp) loop
9260 if Is_Overloadable (Subp) then
9261 Check_Candidate;
9262 end if;
9264 Next_Entity (Subp);
9265 end loop;
9267 return Candidates;
9268 end if;
9269 end Collect_Generic_Type_Ops;
9271 ----------------------------
9272 -- Extended_Primitive_Ops --
9273 ----------------------------
9275 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
9276 Type_Scope : constant Entity_Id := Scope (T);
9278 Body_Decls : List_Id;
9279 Op_Found : Boolean;
9280 Op : Entity_Id;
9281 Op_List : Elist_Id;
9283 begin
9284 Op_List := Primitive_Operations (T);
9286 if Ekind (Type_Scope) = E_Package
9287 and then In_Package_Body (Type_Scope)
9288 and then In_Open_Scopes (Type_Scope)
9289 then
9290 -- Retrieve list of declarations of package body.
9292 Body_Decls :=
9293 Declarations
9294 (Unit_Declaration_Node
9295 (Corresponding_Body
9296 (Unit_Declaration_Node (Type_Scope))));
9298 Op := Current_Entity (Subprog);
9299 Op_Found := False;
9300 while Present (Op) loop
9301 if Comes_From_Source (Op)
9302 and then Is_Overloadable (Op)
9304 -- Exclude overriding primitive operations of a type
9305 -- extension declared in the package body, to prevent
9306 -- duplicates in extended list.
9308 and then not Is_Primitive (Op)
9309 and then Is_List_Member (Unit_Declaration_Node (Op))
9310 and then List_Containing (Unit_Declaration_Node (Op)) =
9311 Body_Decls
9312 then
9313 if not Op_Found then
9315 -- Copy list of primitives so it is not affected for
9316 -- other uses.
9318 Op_List := New_Copy_Elist (Op_List);
9319 Op_Found := True;
9320 end if;
9322 Append_Elmt (Op, Op_List);
9323 end if;
9325 Op := Homonym (Op);
9326 end loop;
9327 end if;
9329 return Op_List;
9330 end Extended_Primitive_Ops;
9332 ---------------------------
9333 -- Is_Private_Overriding --
9334 ---------------------------
9336 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
9337 Visible_Op : constant Entity_Id := Homonym (Op);
9339 begin
9340 return Present (Visible_Op)
9341 and then Scope (Op) = Scope (Visible_Op)
9342 and then not Comes_From_Source (Visible_Op)
9343 and then Alias (Visible_Op) = Op
9344 and then not Is_Hidden (Visible_Op);
9345 end Is_Private_Overriding;
9347 -----------------
9348 -- Names_Match --
9349 -----------------
9351 function Names_Match
9352 (Obj_Type : Entity_Id;
9353 Prim_Op : Entity_Id;
9354 Subprog : Entity_Id) return Boolean is
9355 begin
9356 -- Common case: exact match
9358 if Chars (Prim_Op) = Chars (Subprog) then
9359 return True;
9361 -- For protected type primitives the expander may have built the
9362 -- name of the dispatching primitive prepending the type name to
9363 -- avoid conflicts with the name of the protected subprogram (see
9364 -- Exp_Ch9.Build_Selected_Name).
9366 elsif Is_Protected_Type (Obj_Type) then
9367 return
9368 Present (Original_Protected_Subprogram (Prim_Op))
9369 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9370 Chars (Subprog);
9371 end if;
9373 return False;
9374 end Names_Match;
9376 -----------------------------
9377 -- Valid_First_Argument_Of --
9378 -----------------------------
9380 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9381 Typ : Entity_Id := Etype (First_Formal (Op));
9383 begin
9384 if Is_Concurrent_Type (Typ)
9385 and then Present (Corresponding_Record_Type (Typ))
9386 then
9387 Typ := Corresponding_Record_Type (Typ);
9388 end if;
9390 -- Simple case. Object may be a subtype of the tagged type or may
9391 -- be the corresponding record of a synchronized type.
9393 return Obj_Type = Typ
9394 or else Base_Type (Obj_Type) = Typ
9395 or else Corr_Type = Typ
9397 -- Object may be of a derived type whose parent has unknown
9398 -- discriminants, in which case the type matches the underlying
9399 -- record view of its base.
9401 or else
9402 (Has_Unknown_Discriminants (Typ)
9403 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
9405 -- Prefix can be dereferenced
9407 or else
9408 (Is_Access_Type (Corr_Type)
9409 and then Designated_Type (Corr_Type) = Typ)
9411 -- Formal is an access parameter, for which the object can
9412 -- provide an access.
9414 or else
9415 (Ekind (Typ) = E_Anonymous_Access_Type
9416 and then
9417 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
9418 end Valid_First_Argument_Of;
9420 -- Start of processing for Try_Primitive_Operation
9422 begin
9423 -- Look for subprograms in the list of primitive operations. The name
9424 -- must be identical, and the kind of call indicates the expected
9425 -- kind of operation (function or procedure). If the type is a
9426 -- (tagged) synchronized type, the primitive ops are attached to the
9427 -- corresponding record (base) type.
9429 if Is_Concurrent_Type (Obj_Type) then
9430 if Present (Corresponding_Record_Type (Obj_Type)) then
9431 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
9432 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
9433 else
9434 Corr_Type := Obj_Type;
9435 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9436 end if;
9438 elsif not Is_Generic_Type (Obj_Type) then
9439 Corr_Type := Obj_Type;
9440 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
9442 else
9443 Corr_Type := Obj_Type;
9444 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9445 end if;
9447 while Present (Elmt) loop
9448 Prim_Op := Node (Elmt);
9450 if Names_Match (Obj_Type, Prim_Op, Subprog)
9451 and then Present (First_Formal (Prim_Op))
9452 and then Valid_First_Argument_Of (Prim_Op)
9453 and then
9454 (Nkind (Call_Node) = N_Function_Call)
9456 (Ekind (Prim_Op) = E_Function)
9457 then
9458 -- Ada 2005 (AI-251): If this primitive operation corresponds
9459 -- to an immediate ancestor interface there is no need to add
9460 -- it to the list of interpretations; the corresponding aliased
9461 -- primitive is also in this list of primitive operations and
9462 -- will be used instead.
9464 if (Present (Interface_Alias (Prim_Op))
9465 and then Is_Ancestor (Find_Dispatching_Type
9466 (Alias (Prim_Op)), Corr_Type))
9468 -- Do not consider hidden primitives unless the type is in an
9469 -- open scope or we are within an instance, where visibility
9470 -- is known to be correct, or else if this is an overriding
9471 -- operation in the private part for an inherited operation.
9473 or else (Is_Hidden (Prim_Op)
9474 and then not Is_Immediately_Visible (Obj_Type)
9475 and then not In_Instance
9476 and then not Is_Private_Overriding (Prim_Op))
9477 then
9478 goto Continue;
9479 end if;
9481 Set_Etype (Call_Node, Any_Type);
9482 Set_Is_Overloaded (Call_Node, False);
9484 if No (Matching_Op) then
9485 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
9486 Candidate := Prim_Op;
9488 Set_Parent (Call_Node, Parent (Node_To_Replace));
9490 Set_Name (Call_Node, Prim_Op_Ref);
9491 Success := False;
9493 Analyze_One_Call
9494 (N => Call_Node,
9495 Nam => Prim_Op,
9496 Report => Report_Error,
9497 Success => Success,
9498 Skip_First => True);
9500 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
9502 -- More than one interpretation, collect for subsequent
9503 -- disambiguation. If this is a procedure call and there
9504 -- is another match, report ambiguity now.
9506 else
9507 Analyze_One_Call
9508 (N => Call_Node,
9509 Nam => Prim_Op,
9510 Report => Report_Error,
9511 Success => Success,
9512 Skip_First => True);
9514 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9515 and then Nkind (Call_Node) /= N_Function_Call
9516 then
9517 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
9518 Report_Ambiguity (Matching_Op);
9519 Report_Ambiguity (Prim_Op);
9520 return True;
9521 end if;
9522 end if;
9523 end if;
9525 <<Continue>>
9526 Next_Elmt (Elmt);
9527 end loop;
9529 if Present (Matching_Op) then
9530 Set_Etype (Call_Node, Etype (Matching_Op));
9531 end if;
9533 return Present (Matching_Op);
9534 end Try_Primitive_Operation;
9536 ---------------------
9537 -- Valid_Candidate --
9538 ---------------------
9540 function Valid_Candidate
9541 (Success : Boolean;
9542 Call : Node_Id;
9543 Subp : Entity_Id) return Entity_Id
9545 Arr_Type : Entity_Id;
9546 Comp_Type : Entity_Id;
9548 begin
9549 -- If the subprogram is a valid interpretation, record it in global
9550 -- variable Subprog, to collect all possible overloadings.
9552 if Success then
9553 if Subp /= Entity (Subprog) then
9554 Add_One_Interp (Subprog, Subp, Etype (Subp));
9555 end if;
9556 end if;
9558 -- If the call may be an indexed call, retrieve component type of
9559 -- resulting expression, and add possible interpretation.
9561 Arr_Type := Empty;
9562 Comp_Type := Empty;
9564 if Nkind (Call) = N_Function_Call
9565 and then Nkind (Parent (N)) = N_Indexed_Component
9566 and then Needs_One_Actual (Subp)
9567 then
9568 if Is_Array_Type (Etype (Subp)) then
9569 Arr_Type := Etype (Subp);
9571 elsif Is_Access_Type (Etype (Subp))
9572 and then Is_Array_Type (Designated_Type (Etype (Subp)))
9573 then
9574 Arr_Type := Designated_Type (Etype (Subp));
9575 end if;
9576 end if;
9578 if Present (Arr_Type) then
9580 -- Verify that the actuals (excluding the object) match the types
9581 -- of the indexes.
9583 declare
9584 Actual : Node_Id;
9585 Index : Node_Id;
9587 begin
9588 Actual := Next (First_Actual (Call));
9589 Index := First_Index (Arr_Type);
9590 while Present (Actual) and then Present (Index) loop
9591 if not Has_Compatible_Type (Actual, Etype (Index)) then
9592 Arr_Type := Empty;
9593 exit;
9594 end if;
9596 Next_Actual (Actual);
9597 Next_Index (Index);
9598 end loop;
9600 if No (Actual)
9601 and then No (Index)
9602 and then Present (Arr_Type)
9603 then
9604 Comp_Type := Component_Type (Arr_Type);
9605 end if;
9606 end;
9608 if Present (Comp_Type)
9609 and then Etype (Subprog) /= Comp_Type
9610 then
9611 Add_One_Interp (Subprog, Subp, Comp_Type);
9612 end if;
9613 end if;
9615 if Etype (Call) /= Any_Type then
9616 return Subp;
9617 else
9618 return Empty;
9619 end if;
9620 end Valid_Candidate;
9622 -- Start of processing for Try_Object_Operation
9624 begin
9625 Analyze_Expression (Obj);
9627 -- Analyze the actuals if node is known to be a subprogram call
9629 if Is_Subprg_Call and then N = Name (Parent (N)) then
9630 Actual := First (Parameter_Associations (Parent (N)));
9631 while Present (Actual) loop
9632 Analyze_Expression (Actual);
9633 Next (Actual);
9634 end loop;
9635 end if;
9637 -- Build a subprogram call node, using a copy of Obj as its first
9638 -- actual. This is a placeholder, to be replaced by an explicit
9639 -- dereference when needed.
9641 Transform_Object_Operation
9642 (Call_Node => New_Call_Node,
9643 Node_To_Replace => Node_To_Replace);
9645 Set_Etype (New_Call_Node, Any_Type);
9646 Set_Etype (Subprog, Any_Type);
9647 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
9649 if not Is_Overloaded (Obj) then
9650 Try_One_Prefix_Interpretation (Obj_Type);
9652 else
9653 declare
9654 I : Interp_Index;
9655 It : Interp;
9656 begin
9657 Get_First_Interp (Obj, I, It);
9658 while Present (It.Nam) loop
9659 Try_One_Prefix_Interpretation (It.Typ);
9660 Get_Next_Interp (I, It);
9661 end loop;
9662 end;
9663 end if;
9665 if Etype (New_Call_Node) /= Any_Type then
9667 -- No need to complete the tree transformations if we are only
9668 -- searching for conflicting class-wide subprograms
9670 if CW_Test_Only then
9671 return False;
9672 else
9673 Complete_Object_Operation
9674 (Call_Node => New_Call_Node,
9675 Node_To_Replace => Node_To_Replace);
9676 return True;
9677 end if;
9679 elsif Present (Candidate) then
9681 -- The argument list is not type correct. Re-analyze with error
9682 -- reporting enabled, and use one of the possible candidates.
9683 -- In All_Errors_Mode, re-analyze all failed interpretations.
9685 if All_Errors_Mode then
9686 Report_Error := True;
9687 if Try_Primitive_Operation
9688 (Call_Node => New_Call_Node,
9689 Node_To_Replace => Node_To_Replace)
9691 or else
9692 Try_Class_Wide_Operation
9693 (Call_Node => New_Call_Node,
9694 Node_To_Replace => Node_To_Replace)
9695 then
9696 null;
9697 end if;
9699 else
9700 Analyze_One_Call
9701 (N => New_Call_Node,
9702 Nam => Candidate,
9703 Report => True,
9704 Success => Success,
9705 Skip_First => True);
9706 end if;
9708 -- No need for further errors
9710 return True;
9712 else
9713 -- There was no candidate operation, so report it as an error
9714 -- in the caller: Analyze_Selected_Component.
9716 return False;
9717 end if;
9718 end Try_Object_Operation;
9720 ---------
9721 -- wpo --
9722 ---------
9724 procedure wpo (T : Entity_Id) is
9725 Op : Entity_Id;
9726 E : Elmt_Id;
9728 begin
9729 if not Is_Tagged_Type (T) then
9730 return;
9731 end if;
9733 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9734 while Present (E) loop
9735 Op := Node (E);
9736 Write_Int (Int (Op));
9737 Write_Str (" === ");
9738 Write_Name (Chars (Op));
9739 Write_Str (" in ");
9740 Write_Name (Chars (Scope (Op)));
9741 Next_Elmt (E);
9742 Write_Eol;
9743 end loop;
9744 end wpo;
9746 end Sem_Ch4;