PR tree-optimization/36329
[official-gcc.git] / gcc / ada / sem_ch4.adb
blobb59cd4b5186160bdc68fb9db5046854c8dfbdafc
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-2008, 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 Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Fname; use Fname;
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_Cat; use Sem_Cat;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Disp; use Sem_Disp;
50 with Sem_Dist; use Sem_Dist;
51 with Sem_Eval; use Sem_Eval;
52 with Sem_Res; use Sem_Res;
53 with Sem_Util; use Sem_Util;
54 with Sem_Type; use Sem_Type;
55 with Stand; use Stand;
56 with Sinfo; use Sinfo;
57 with Snames; use Snames;
58 with Tbuild; use Tbuild;
60 package body Sem_Ch4 is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Analyze_Concatenation_Rest (N : Node_Id);
67 -- Does the "rest" of the work of Analyze_Concatenation, after the left
68 -- operand has been analyzed. See Analyze_Concatenation for details.
70 procedure Analyze_Expression (N : Node_Id);
71 -- For expressions that are not names, this is just a call to analyze.
72 -- If the expression is a name, it may be a call to a parameterless
73 -- function, and if so must be converted into an explicit call node
74 -- and analyzed as such. This deproceduring must be done during the first
75 -- pass of overload resolution, because otherwise a procedure call with
76 -- overloaded actuals may fail to resolve.
78 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
79 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
80 -- is an operator name or an expanded name whose selector is an operator
81 -- name, and one possible interpretation is as a predefined operator.
83 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
84 -- If the prefix of a selected_component is overloaded, the proper
85 -- interpretation that yields a record type with the proper selector
86 -- name must be selected.
88 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
89 -- Procedure to analyze a user defined binary operator, which is resolved
90 -- like a function, but instead of a list of actuals it is presented
91 -- with the left and right operands of an operator node.
93 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
94 -- Procedure to analyze a user defined unary operator, which is resolved
95 -- like a function, but instead of a list of actuals, it is presented with
96 -- the operand of the operator node.
98 procedure Ambiguous_Operands (N : Node_Id);
99 -- for equality, membership, and comparison operators with overloaded
100 -- arguments, list possible interpretations.
102 procedure Analyze_One_Call
103 (N : Node_Id;
104 Nam : Entity_Id;
105 Report : Boolean;
106 Success : out Boolean;
107 Skip_First : Boolean := False);
108 -- Check one interpretation of an overloaded subprogram name for
109 -- compatibility with the types of the actuals in a call. If there is a
110 -- single interpretation which does not match, post error if Report is
111 -- set to True.
113 -- Nam is the entity that provides the formals against which the actuals
114 -- are checked. Nam is either the name of a subprogram, or the internal
115 -- subprogram type constructed for an access_to_subprogram. If the actuals
116 -- are compatible with Nam, then Nam is added to the list of candidate
117 -- interpretations for N, and Success is set to True.
119 -- The flag Skip_First is used when analyzing a call that was rewritten
120 -- from object notation. In this case the first actual may have to receive
121 -- an explicit dereference, depending on the first formal of the operation
122 -- being called. The caller will have verified that the object is legal
123 -- for the call. If the remaining parameters match, the first parameter
124 -- will rewritten as a dereference if needed, prior to completing analysis.
126 procedure Check_Misspelled_Selector
127 (Prefix : Entity_Id;
128 Sel : Node_Id);
129 -- Give possible misspelling diagnostic if Sel is likely to be
130 -- a misspelling of one of the selectors of the Prefix.
131 -- This is called by Analyze_Selected_Component after producing
132 -- an invalid selector error message.
134 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
135 -- Verify that type T is declared in scope S. Used to find interpretations
136 -- for operators given by expanded names. This is abstracted as a separate
137 -- function to handle extensions to System, where S is System, but T is
138 -- declared in the extension.
140 procedure Find_Arithmetic_Types
141 (L, R : Node_Id;
142 Op_Id : Entity_Id;
143 N : Node_Id);
144 -- L and R are the operands of an arithmetic operator. Find
145 -- consistent pairs of interpretations for L and R that have a
146 -- numeric type consistent with the semantics of the operator.
148 procedure Find_Comparison_Types
149 (L, R : Node_Id;
150 Op_Id : Entity_Id;
151 N : Node_Id);
152 -- L and R are operands of a comparison operator. Find consistent
153 -- pairs of interpretations for L and R.
155 procedure Find_Concatenation_Types
156 (L, R : Node_Id;
157 Op_Id : Entity_Id;
158 N : Node_Id);
159 -- For the four varieties of concatenation
161 procedure Find_Equality_Types
162 (L, R : Node_Id;
163 Op_Id : Entity_Id;
164 N : Node_Id);
165 -- Ditto for equality operators
167 procedure Find_Boolean_Types
168 (L, R : Node_Id;
169 Op_Id : Entity_Id;
170 N : Node_Id);
171 -- Ditto for binary logical operations
173 procedure Find_Negation_Types
174 (R : Node_Id;
175 Op_Id : Entity_Id;
176 N : Node_Id);
177 -- Find consistent interpretation for operand of negation operator
179 procedure Find_Non_Universal_Interpretations
180 (N : Node_Id;
181 R : Node_Id;
182 Op_Id : Entity_Id;
183 T1 : Entity_Id);
184 -- For equality and comparison operators, the result is always boolean,
185 -- and the legality of the operation is determined from the visibility
186 -- of the operand types. If one of the operands has a universal interpre-
187 -- tation, the legality check uses some compatible non-universal
188 -- interpretation of the other operand. N can be an operator node, or
189 -- a function call whose name is an operator designator.
191 function Find_Primitive_Operation (N : Node_Id) return Boolean;
192 -- Find candidate interpretations for the name Obj.Proc when it appears
193 -- in a subprogram renaming declaration.
195 procedure Find_Unary_Types
196 (R : Node_Id;
197 Op_Id : Entity_Id;
198 N : Node_Id);
199 -- Unary arithmetic types: plus, minus, abs
201 procedure Check_Arithmetic_Pair
202 (T1, T2 : Entity_Id;
203 Op_Id : Entity_Id;
204 N : Node_Id);
205 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
206 -- types for left and right operand. Determine whether they constitute
207 -- a valid pair for the given operator, and record the corresponding
208 -- interpretation of the operator node. The node N may be an operator
209 -- node (the usual case) or a function call whose prefix is an operator
210 -- designator. In both cases Op_Id is the operator name itself.
212 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
213 -- Give detailed information on overloaded call where none of the
214 -- interpretations match. N is the call node, Nam the designator for
215 -- the overloaded entity being called.
217 function Junk_Operand (N : Node_Id) return Boolean;
218 -- Test for an operand that is an inappropriate entity (e.g. a package
219 -- name or a label). If so, issue an error message and return True. If
220 -- the operand is not an inappropriate entity kind, return False.
222 procedure Operator_Check (N : Node_Id);
223 -- Verify that an operator has received some valid interpretation. If none
224 -- was found, determine whether a use clause would make the operation
225 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
226 -- every type compatible with the operator, even if the operator for the
227 -- type is not directly visible. The routine uses this type to emit a more
228 -- informative message.
230 function Process_Implicit_Dereference_Prefix
231 (E : Entity_Id;
232 P : Node_Id) return Entity_Id;
233 -- Called when P is the prefix of an implicit dereference, denoting an
234 -- object E. The function returns the designated type of the prefix, taking
235 -- into account that the designated type of an anonymous access type may be
236 -- a limited view, when the non-limited view is visible.
237 -- If in semantics only mode (-gnatc or generic), the function also records
238 -- that the prefix is a reference to E, if any. Normally, such a reference
239 -- is generated only when the implicit dereference is expanded into an
240 -- explicit one, but for consistency we must generate the reference when
241 -- expansion is disabled as well.
243 procedure Remove_Abstract_Operations (N : Node_Id);
244 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
245 -- operation is not a candidate interpretation.
247 function Try_Indexed_Call
248 (N : Node_Id;
249 Nam : Entity_Id;
250 Typ : Entity_Id;
251 Skip_First : Boolean) return Boolean;
252 -- If a function has defaults for all its actuals, a call to it may in fact
253 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
254 -- interpretation as an indexing, prior to analysis as a call. If both are
255 -- possible, the node is overloaded with both interpretations (same symbol
256 -- but two different types). If the call is written in prefix form, the
257 -- prefix becomes the first parameter in the call, and only the remaining
258 -- actuals must be checked for the presence of defaults.
260 function Try_Indirect_Call
261 (N : Node_Id;
262 Nam : Entity_Id;
263 Typ : Entity_Id) return Boolean;
264 -- Similarly, a function F that needs no actuals can return an access to a
265 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
266 -- the call may be overloaded with both interpretations.
268 function Try_Object_Operation (N : Node_Id) return Boolean;
269 -- Ada 2005 (AI-252): Support the object.operation notation
271 procedure wpo (T : Entity_Id);
272 pragma Warnings (Off, wpo);
273 -- Used for debugging: obtain list of primitive operations even if
274 -- type is not frozen and dispatch table is not built yet.
276 ------------------------
277 -- Ambiguous_Operands --
278 ------------------------
280 procedure Ambiguous_Operands (N : Node_Id) is
281 procedure List_Operand_Interps (Opnd : Node_Id);
283 --------------------------
284 -- List_Operand_Interps --
285 --------------------------
287 procedure List_Operand_Interps (Opnd : Node_Id) is
288 Nam : Node_Id;
289 Err : Node_Id := N;
291 begin
292 if Is_Overloaded (Opnd) then
293 if Nkind (Opnd) in N_Op then
294 Nam := Opnd;
295 elsif Nkind (Opnd) = N_Function_Call then
296 Nam := Name (Opnd);
297 else
298 return;
299 end if;
301 else
302 return;
303 end if;
305 if Opnd = Left_Opnd (N) then
306 Error_Msg_N
307 ("\left operand has the following interpretations", N);
308 else
309 Error_Msg_N
310 ("\right operand has the following interpretations", N);
311 Err := Opnd;
312 end if;
314 List_Interps (Nam, Err);
315 end List_Operand_Interps;
317 -- Start of processing for Ambiguous_Operands
319 begin
320 if Nkind (N) in N_Membership_Test then
321 Error_Msg_N ("ambiguous operands for membership", N);
323 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
324 Error_Msg_N ("ambiguous operands for equality", N);
326 else
327 Error_Msg_N ("ambiguous operands for comparison", N);
328 end if;
330 if All_Errors_Mode then
331 List_Operand_Interps (Left_Opnd (N));
332 List_Operand_Interps (Right_Opnd (N));
333 else
334 Error_Msg_N ("\use -gnatf switch for details", N);
335 end if;
336 end Ambiguous_Operands;
338 -----------------------
339 -- Analyze_Aggregate --
340 -----------------------
342 -- Most of the analysis of Aggregates requires that the type be known,
343 -- and is therefore put off until resolution.
345 procedure Analyze_Aggregate (N : Node_Id) is
346 begin
347 if No (Etype (N)) then
348 Set_Etype (N, Any_Composite);
349 end if;
350 end Analyze_Aggregate;
352 -----------------------
353 -- Analyze_Allocator --
354 -----------------------
356 procedure Analyze_Allocator (N : Node_Id) is
357 Loc : constant Source_Ptr := Sloc (N);
358 Sav_Errs : constant Nat := Serious_Errors_Detected;
359 E : Node_Id := Expression (N);
360 Acc_Type : Entity_Id;
361 Type_Id : Entity_Id;
363 begin
364 -- In accordance with H.4(7), the No_Allocators restriction only applies
365 -- to user-written allocators.
367 if Comes_From_Source (N) then
368 Check_Restriction (No_Allocators, N);
369 end if;
371 if Nkind (E) = N_Qualified_Expression then
372 Acc_Type := Create_Itype (E_Allocator_Type, N);
373 Set_Etype (Acc_Type, Acc_Type);
374 Find_Type (Subtype_Mark (E));
376 -- Analyze the qualified expression, and apply the name resolution
377 -- rule given in 4.7 (3).
379 Analyze (E);
380 Type_Id := Etype (E);
381 Set_Directly_Designated_Type (Acc_Type, Type_Id);
383 Resolve (Expression (E), Type_Id);
385 if Is_Limited_Type (Type_Id)
386 and then Comes_From_Source (N)
387 and then not In_Instance_Body
388 then
389 if not OK_For_Limited_Init (Expression (E)) then
390 Error_Msg_N ("initialization not allowed for limited types", N);
391 Explain_Limited_Type (Type_Id, N);
392 end if;
393 end if;
395 -- A qualified expression requires an exact match of the type,
396 -- class-wide matching is not allowed.
398 -- if Is_Class_Wide_Type (Type_Id)
399 -- and then Base_Type
400 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
401 -- then
402 -- Wrong_Type (Expression (E), Type_Id);
403 -- end if;
405 Check_Non_Static_Context (Expression (E));
407 -- We don't analyze the qualified expression itself because it's
408 -- part of the allocator
410 Set_Etype (E, Type_Id);
412 -- Case where allocator has a subtype indication
414 else
415 declare
416 Def_Id : Entity_Id;
417 Base_Typ : Entity_Id;
419 begin
420 -- If the allocator includes a N_Subtype_Indication then a
421 -- constraint is present, otherwise the node is a subtype mark.
422 -- Introduce an explicit subtype declaration into the tree
423 -- defining some anonymous subtype and rewrite the allocator to
424 -- use this subtype rather than the subtype indication.
426 -- It is important to introduce the explicit subtype declaration
427 -- so that the bounds of the subtype indication are attached to
428 -- the tree in case the allocator is inside a generic unit.
430 if Nkind (E) = N_Subtype_Indication then
432 -- A constraint is only allowed for a composite type in Ada
433 -- 95. In Ada 83, a constraint is also allowed for an
434 -- access-to-composite type, but the constraint is ignored.
436 Find_Type (Subtype_Mark (E));
437 Base_Typ := Entity (Subtype_Mark (E));
439 if Is_Elementary_Type (Base_Typ) then
440 if not (Ada_Version = Ada_83
441 and then Is_Access_Type (Base_Typ))
442 then
443 Error_Msg_N ("constraint not allowed here", E);
445 if Nkind (Constraint (E)) =
446 N_Index_Or_Discriminant_Constraint
447 then
448 Error_Msg_N
449 ("\if qualified expression was meant, " &
450 "use apostrophe", Constraint (E));
451 end if;
452 end if;
454 -- Get rid of the bogus constraint:
456 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
457 Analyze_Allocator (N);
458 return;
460 -- Ada 2005, AI-363: if the designated type has a constrained
461 -- partial view, it cannot receive a discriminant constraint,
462 -- and the allocated object is unconstrained.
464 elsif Ada_Version >= Ada_05
465 and then Has_Constrained_Partial_View (Base_Typ)
466 then
467 Error_Msg_N
468 ("constraint no allowed when type " &
469 "has a constrained partial view", Constraint (E));
470 end if;
472 if Expander_Active then
473 Def_Id :=
474 Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
476 Insert_Action (E,
477 Make_Subtype_Declaration (Loc,
478 Defining_Identifier => Def_Id,
479 Subtype_Indication => Relocate_Node (E)));
481 if Sav_Errs /= Serious_Errors_Detected
482 and then Nkind (Constraint (E)) =
483 N_Index_Or_Discriminant_Constraint
484 then
485 Error_Msg_N
486 ("if qualified expression was meant, " &
487 "use apostrophe!", Constraint (E));
488 end if;
490 E := New_Occurrence_Of (Def_Id, Loc);
491 Rewrite (Expression (N), E);
492 end if;
493 end if;
495 Type_Id := Process_Subtype (E, N);
496 Acc_Type := Create_Itype (E_Allocator_Type, N);
497 Set_Etype (Acc_Type, Acc_Type);
498 Set_Directly_Designated_Type (Acc_Type, Type_Id);
499 Check_Fully_Declared (Type_Id, N);
501 -- Ada 2005 (AI-231)
503 if Can_Never_Be_Null (Type_Id) then
504 Error_Msg_N ("(Ada 2005) qualified expression required",
505 Expression (N));
506 end if;
508 -- Check restriction against dynamically allocated protected
509 -- objects. Note that when limited aggregates are supported,
510 -- a similar test should be applied to an allocator with a
511 -- qualified expression ???
513 if Is_Protected_Type (Type_Id) then
514 Check_Restriction (No_Protected_Type_Allocators, N);
515 end if;
517 -- Check for missing initialization. Skip this check if we already
518 -- had errors on analyzing the allocator, since in that case these
519 -- are probably cascaded errors.
521 if Is_Indefinite_Subtype (Type_Id)
522 and then Serious_Errors_Detected = Sav_Errs
523 then
524 if Is_Class_Wide_Type (Type_Id) then
525 Error_Msg_N
526 ("initialization required in class-wide allocation", N);
527 else
528 if Ada_Version < Ada_05
529 and then Is_Limited_Type (Type_Id)
530 then
531 Error_Msg_N ("unconstrained allocation not allowed", N);
533 if Is_Array_Type (Type_Id) then
534 Error_Msg_N
535 ("\constraint with array bounds required", N);
537 elsif Has_Unknown_Discriminants (Type_Id) then
538 null;
540 else pragma Assert (Has_Discriminants (Type_Id));
541 Error_Msg_N
542 ("\constraint with discriminant values required", N);
543 end if;
545 -- Limited Ada 2005 and general non-limited case
547 else
548 Error_Msg_N
549 ("uninitialized unconstrained allocation not allowed",
552 if Is_Array_Type (Type_Id) then
553 Error_Msg_N
554 ("\qualified expression or constraint with " &
555 "array bounds required", N);
557 elsif Has_Unknown_Discriminants (Type_Id) then
558 Error_Msg_N ("\qualified expression required", N);
560 else pragma Assert (Has_Discriminants (Type_Id));
561 Error_Msg_N
562 ("\qualified expression or constraint with " &
563 "discriminant values required", N);
564 end if;
565 end if;
566 end if;
567 end if;
568 end;
569 end if;
571 if Is_Abstract_Type (Type_Id) then
572 Error_Msg_N ("cannot allocate abstract object", E);
573 end if;
575 if Has_Task (Designated_Type (Acc_Type)) then
576 Check_Restriction (No_Tasking, N);
577 Check_Restriction (Max_Tasks, N);
578 Check_Restriction (No_Task_Allocators, N);
579 end if;
581 -- If the No_Streams restriction is set, check that the type of the
582 -- object is not, and does not contain, any subtype derived from
583 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
584 -- Has_Stream just for efficiency reasons. There is no point in
585 -- spending time on a Has_Stream check if the restriction is not set.
587 if Restrictions.Set (No_Streams) then
588 if Has_Stream (Designated_Type (Acc_Type)) then
589 Check_Restriction (No_Streams, N);
590 end if;
591 end if;
593 Set_Etype (N, Acc_Type);
595 if not Is_Library_Level_Entity (Acc_Type) then
596 Check_Restriction (No_Local_Allocators, N);
597 end if;
599 if Serious_Errors_Detected > Sav_Errs then
600 Set_Error_Posted (N);
601 Set_Etype (N, Any_Type);
602 end if;
603 end Analyze_Allocator;
605 ---------------------------
606 -- Analyze_Arithmetic_Op --
607 ---------------------------
609 procedure Analyze_Arithmetic_Op (N : Node_Id) is
610 L : constant Node_Id := Left_Opnd (N);
611 R : constant Node_Id := Right_Opnd (N);
612 Op_Id : Entity_Id;
614 begin
615 Candidate_Type := Empty;
616 Analyze_Expression (L);
617 Analyze_Expression (R);
619 -- If the entity is already set, the node is the instantiation of a
620 -- generic node with a non-local reference, or was manufactured by a
621 -- call to Make_Op_xxx. In either case the entity is known to be valid,
622 -- and we do not need to collect interpretations, instead we just get
623 -- the single possible interpretation.
625 Op_Id := Entity (N);
627 if Present (Op_Id) then
628 if Ekind (Op_Id) = E_Operator then
630 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
631 and then Treat_Fixed_As_Integer (N)
632 then
633 null;
634 else
635 Set_Etype (N, Any_Type);
636 Find_Arithmetic_Types (L, R, Op_Id, N);
637 end if;
639 else
640 Set_Etype (N, Any_Type);
641 Add_One_Interp (N, Op_Id, Etype (Op_Id));
642 end if;
644 -- Entity is not already set, so we do need to collect interpretations
646 else
647 Op_Id := Get_Name_Entity_Id (Chars (N));
648 Set_Etype (N, Any_Type);
650 while Present (Op_Id) loop
651 if Ekind (Op_Id) = E_Operator
652 and then Present (Next_Entity (First_Entity (Op_Id)))
653 then
654 Find_Arithmetic_Types (L, R, Op_Id, N);
656 -- The following may seem superfluous, because an operator cannot
657 -- be generic, but this ignores the cleverness of the author of
658 -- ACVC bc1013a.
660 elsif Is_Overloadable (Op_Id) then
661 Analyze_User_Defined_Binary_Op (N, Op_Id);
662 end if;
664 Op_Id := Homonym (Op_Id);
665 end loop;
666 end if;
668 Operator_Check (N);
669 end Analyze_Arithmetic_Op;
671 ------------------
672 -- Analyze_Call --
673 ------------------
675 -- Function, procedure, and entry calls are checked here. The Name in
676 -- the call may be overloaded. The actuals have been analyzed and may
677 -- themselves be overloaded. On exit from this procedure, the node N
678 -- may have zero, one or more interpretations. In the first case an
679 -- error message is produced. In the last case, the node is flagged
680 -- as overloaded and the interpretations are collected in All_Interp.
682 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
683 -- the type-checking is similar to that of other calls.
685 procedure Analyze_Call (N : Node_Id) is
686 Actuals : constant List_Id := Parameter_Associations (N);
687 Nam : Node_Id := Name (N);
688 X : Interp_Index;
689 It : Interp;
690 Nam_Ent : Entity_Id;
691 Success : Boolean := False;
693 function Name_Denotes_Function return Boolean;
694 -- If the type of the name is an access to subprogram, this may be the
695 -- type of a name, or the return type of the function being called. If
696 -- the name is not an entity then it can denote a protected function.
697 -- Until we distinguish Etype from Return_Type, we must use this routine
698 -- to resolve the meaning of the name in the call.
700 procedure No_Interpretation;
701 -- Output error message when no valid interpretation exists
703 ---------------------------
704 -- Name_Denotes_Function --
705 ---------------------------
707 function Name_Denotes_Function return Boolean is
708 begin
709 if Is_Entity_Name (Nam) then
710 return Ekind (Entity (Nam)) = E_Function;
712 elsif Nkind (Nam) = N_Selected_Component then
713 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
715 else
716 return False;
717 end if;
718 end Name_Denotes_Function;
720 -----------------------
721 -- No_Interpretation --
722 -----------------------
724 procedure No_Interpretation is
725 L : constant Boolean := Is_List_Member (N);
726 K : constant Node_Kind := Nkind (Parent (N));
728 begin
729 -- If the node is in a list whose parent is not an expression then it
730 -- must be an attempted procedure call.
732 if L and then K not in N_Subexpr then
733 if Ekind (Entity (Nam)) = E_Generic_Procedure then
734 Error_Msg_NE
735 ("must instantiate generic procedure& before call",
736 Nam, Entity (Nam));
737 else
738 Error_Msg_N
739 ("procedure or entry name expected", Nam);
740 end if;
742 -- Check for tasking cases where only an entry call will do
744 elsif not L
745 and then Nkind_In (K, N_Entry_Call_Alternative,
746 N_Triggering_Alternative)
747 then
748 Error_Msg_N ("entry name expected", Nam);
750 -- Otherwise give general error message
752 else
753 Error_Msg_N ("invalid prefix in call", Nam);
754 end if;
755 end No_Interpretation;
757 -- Start of processing for Analyze_Call
759 begin
760 -- Initialize the type of the result of the call to the error type,
761 -- which will be reset if the type is successfully resolved.
763 Set_Etype (N, Any_Type);
765 if not Is_Overloaded (Nam) then
767 -- Only one interpretation to check
769 if Ekind (Etype (Nam)) = E_Subprogram_Type then
770 Nam_Ent := Etype (Nam);
772 -- If the prefix is an access_to_subprogram, this may be an indirect
773 -- call. This is the case if the name in the call is not an entity
774 -- name, or if it is a function name in the context of a procedure
775 -- call. In this latter case, we have a call to a parameterless
776 -- function that returns a pointer_to_procedure which is the entity
777 -- being called. Finally, F (X) may be a call to a parameterless
778 -- function that returns a pointer to a function with parameters.
780 elsif Is_Access_Type (Etype (Nam))
781 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
782 and then
783 (not Name_Denotes_Function
784 or else Nkind (N) = N_Procedure_Call_Statement
785 or else
786 (Nkind (Parent (N)) /= N_Explicit_Dereference
787 and then Is_Entity_Name (Nam)
788 and then No (First_Formal (Entity (Nam)))
789 and then Present (Actuals)))
790 then
791 Nam_Ent := Designated_Type (Etype (Nam));
792 Insert_Explicit_Dereference (Nam);
794 -- Selected component case. Simple entry or protected operation,
795 -- where the entry name is given by the selector name.
797 elsif Nkind (Nam) = N_Selected_Component then
798 Nam_Ent := Entity (Selector_Name (Nam));
800 if Ekind (Nam_Ent) /= E_Entry
801 and then Ekind (Nam_Ent) /= E_Entry_Family
802 and then Ekind (Nam_Ent) /= E_Function
803 and then Ekind (Nam_Ent) /= E_Procedure
804 then
805 Error_Msg_N ("name in call is not a callable entity", Nam);
806 Set_Etype (N, Any_Type);
807 return;
808 end if;
810 -- If the name is an Indexed component, it can be a call to a member
811 -- of an entry family. The prefix must be a selected component whose
812 -- selector is the entry. Analyze_Procedure_Call normalizes several
813 -- kinds of call into this form.
815 elsif Nkind (Nam) = N_Indexed_Component then
816 if Nkind (Prefix (Nam)) = N_Selected_Component then
817 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
818 else
819 Error_Msg_N ("name in call is not a callable entity", Nam);
820 Set_Etype (N, Any_Type);
821 return;
822 end if;
824 elsif not Is_Entity_Name (Nam) then
825 Error_Msg_N ("name in call is not a callable entity", Nam);
826 Set_Etype (N, Any_Type);
827 return;
829 else
830 Nam_Ent := Entity (Nam);
832 -- If no interpretations, give error message
834 if not Is_Overloadable (Nam_Ent) then
835 No_Interpretation;
836 return;
837 end if;
838 end if;
840 -- Operations generated for RACW stub types are called only through
841 -- dispatching, and can never be the static interpretation of a call.
843 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
844 No_Interpretation;
845 return;
846 end if;
848 Analyze_One_Call (N, Nam_Ent, True, Success);
850 -- If this is an indirect call, the return type of the access_to
851 -- subprogram may be an incomplete type. At the point of the call,
852 -- use the full type if available, and at the same time update
853 -- the return type of the access_to_subprogram.
855 if Success
856 and then Nkind (Nam) = N_Explicit_Dereference
857 and then Ekind (Etype (N)) = E_Incomplete_Type
858 and then Present (Full_View (Etype (N)))
859 then
860 Set_Etype (N, Full_View (Etype (N)));
861 Set_Etype (Nam_Ent, Etype (N));
862 end if;
864 else
865 -- An overloaded selected component must denote overloaded operations
866 -- of a concurrent type. The interpretations are attached to the
867 -- simple name of those operations.
869 if Nkind (Nam) = N_Selected_Component then
870 Nam := Selector_Name (Nam);
871 end if;
873 Get_First_Interp (Nam, X, It);
875 while Present (It.Nam) loop
876 Nam_Ent := It.Nam;
878 -- Name may be call that returns an access to subprogram, or more
879 -- generally an overloaded expression one of whose interpretations
880 -- yields an access to subprogram. If the name is an entity, we
881 -- do not dereference, because the node is a call that returns
882 -- the access type: note difference between f(x), where the call
883 -- may return an access subprogram type, and f(x)(y), where the
884 -- type returned by the call to f is implicitly dereferenced to
885 -- analyze the outer call.
887 if Is_Access_Type (Nam_Ent) then
888 Nam_Ent := Designated_Type (Nam_Ent);
890 elsif Is_Access_Type (Etype (Nam_Ent))
891 and then not Is_Entity_Name (Nam)
892 and then Ekind (Designated_Type (Etype (Nam_Ent)))
893 = E_Subprogram_Type
894 then
895 Nam_Ent := Designated_Type (Etype (Nam_Ent));
896 end if;
898 Analyze_One_Call (N, Nam_Ent, False, Success);
900 -- If the interpretation succeeds, mark the proper type of the
901 -- prefix (any valid candidate will do). If not, remove the
902 -- candidate interpretation. This only needs to be done for
903 -- overloaded protected operations, for other entities disambi-
904 -- guation is done directly in Resolve.
906 if Success then
907 Set_Etype (Nam, It.Typ);
909 elsif Nkind_In (Name (N), N_Selected_Component,
910 N_Function_Call)
911 then
912 Remove_Interp (X);
913 end if;
915 Get_Next_Interp (X, It);
916 end loop;
918 -- If the name is the result of a function call, it can only
919 -- be a call to a function returning an access to subprogram.
920 -- Insert explicit dereference.
922 if Nkind (Nam) = N_Function_Call then
923 Insert_Explicit_Dereference (Nam);
924 end if;
926 if Etype (N) = Any_Type then
928 -- None of the interpretations is compatible with the actuals
930 Diagnose_Call (N, Nam);
932 -- Special checks for uninstantiated put routines
934 if Nkind (N) = N_Procedure_Call_Statement
935 and then Is_Entity_Name (Nam)
936 and then Chars (Nam) = Name_Put
937 and then List_Length (Actuals) = 1
938 then
939 declare
940 Arg : constant Node_Id := First (Actuals);
941 Typ : Entity_Id;
943 begin
944 if Nkind (Arg) = N_Parameter_Association then
945 Typ := Etype (Explicit_Actual_Parameter (Arg));
946 else
947 Typ := Etype (Arg);
948 end if;
950 if Is_Signed_Integer_Type (Typ) then
951 Error_Msg_N
952 ("possible missing instantiation of " &
953 "'Text_'I'O.'Integer_'I'O!", Nam);
955 elsif Is_Modular_Integer_Type (Typ) then
956 Error_Msg_N
957 ("possible missing instantiation of " &
958 "'Text_'I'O.'Modular_'I'O!", Nam);
960 elsif Is_Floating_Point_Type (Typ) then
961 Error_Msg_N
962 ("possible missing instantiation of " &
963 "'Text_'I'O.'Float_'I'O!", Nam);
965 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
966 Error_Msg_N
967 ("possible missing instantiation of " &
968 "'Text_'I'O.'Fixed_'I'O!", Nam);
970 elsif Is_Decimal_Fixed_Point_Type (Typ) then
971 Error_Msg_N
972 ("possible missing instantiation of " &
973 "'Text_'I'O.'Decimal_'I'O!", Nam);
975 elsif Is_Enumeration_Type (Typ) then
976 Error_Msg_N
977 ("possible missing instantiation of " &
978 "'Text_'I'O.'Enumeration_'I'O!", Nam);
979 end if;
980 end;
981 end if;
983 elsif not Is_Overloaded (N)
984 and then Is_Entity_Name (Nam)
985 then
986 -- Resolution yields a single interpretation. Verify that the
987 -- reference has capitalization consistent with the declaration.
989 Set_Entity_With_Style_Check (Nam, Entity (Nam));
990 Generate_Reference (Entity (Nam), Nam);
992 Set_Etype (Nam, Etype (Entity (Nam)));
993 else
994 Remove_Abstract_Operations (N);
995 end if;
997 End_Interp_List;
998 end if;
999 end Analyze_Call;
1001 ---------------------------
1002 -- Analyze_Comparison_Op --
1003 ---------------------------
1005 procedure Analyze_Comparison_Op (N : Node_Id) is
1006 L : constant Node_Id := Left_Opnd (N);
1007 R : constant Node_Id := Right_Opnd (N);
1008 Op_Id : Entity_Id := Entity (N);
1010 begin
1011 Set_Etype (N, Any_Type);
1012 Candidate_Type := Empty;
1014 Analyze_Expression (L);
1015 Analyze_Expression (R);
1017 if Present (Op_Id) then
1018 if Ekind (Op_Id) = E_Operator then
1019 Find_Comparison_Types (L, R, Op_Id, N);
1020 else
1021 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1022 end if;
1024 if Is_Overloaded (L) then
1025 Set_Etype (L, Intersect_Types (L, R));
1026 end if;
1028 else
1029 Op_Id := Get_Name_Entity_Id (Chars (N));
1030 while Present (Op_Id) loop
1031 if Ekind (Op_Id) = E_Operator then
1032 Find_Comparison_Types (L, R, Op_Id, N);
1033 else
1034 Analyze_User_Defined_Binary_Op (N, Op_Id);
1035 end if;
1037 Op_Id := Homonym (Op_Id);
1038 end loop;
1039 end if;
1041 Operator_Check (N);
1042 end Analyze_Comparison_Op;
1044 ---------------------------
1045 -- Analyze_Concatenation --
1046 ---------------------------
1048 procedure Analyze_Concatenation (N : Node_Id) is
1050 -- We wish to avoid deep recursion, because concatenations are often
1051 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1052 -- operands nonrecursively until we find something that is not a
1053 -- concatenation (A in this case), or has already been analyzed. We
1054 -- analyze that, and then walk back up the tree following Parent
1055 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1056 -- work at each level. The Parent pointers allow us to avoid recursion,
1057 -- and thus avoid running out of memory.
1059 NN : Node_Id := N;
1060 L : Node_Id;
1062 begin
1063 Candidate_Type := Empty;
1065 -- The following code is equivalent to:
1067 -- Set_Etype (N, Any_Type);
1068 -- Analyze_Expression (Left_Opnd (N));
1069 -- Analyze_Concatenation_Rest (N);
1071 -- where the Analyze_Expression call recurses back here if the left
1072 -- operand is a concatenation.
1074 -- Walk down left operands
1076 loop
1077 Set_Etype (NN, Any_Type);
1078 L := Left_Opnd (NN);
1079 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1080 NN := L;
1081 end loop;
1083 -- Now (given the above example) NN is A&B and L is A
1085 -- First analyze L ...
1087 Analyze_Expression (L);
1089 -- ... then walk NN back up until we reach N (where we started), calling
1090 -- Analyze_Concatenation_Rest along the way.
1092 loop
1093 Analyze_Concatenation_Rest (NN);
1094 exit when NN = N;
1095 NN := Parent (NN);
1096 end loop;
1097 end Analyze_Concatenation;
1099 --------------------------------
1100 -- Analyze_Concatenation_Rest --
1101 --------------------------------
1103 -- If the only one-dimensional array type in scope is String,
1104 -- this is the resulting type of the operation. Otherwise there
1105 -- will be a concatenation operation defined for each user-defined
1106 -- one-dimensional array.
1108 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1109 L : constant Node_Id := Left_Opnd (N);
1110 R : constant Node_Id := Right_Opnd (N);
1111 Op_Id : Entity_Id := Entity (N);
1112 LT : Entity_Id;
1113 RT : Entity_Id;
1115 begin
1116 Analyze_Expression (R);
1118 -- If the entity is present, the node appears in an instance, and
1119 -- denotes a predefined concatenation operation. The resulting type is
1120 -- obtained from the arguments when possible. If the arguments are
1121 -- aggregates, the array type and the concatenation type must be
1122 -- visible.
1124 if Present (Op_Id) then
1125 if Ekind (Op_Id) = E_Operator then
1127 LT := Base_Type (Etype (L));
1128 RT := Base_Type (Etype (R));
1130 if Is_Array_Type (LT)
1131 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1132 then
1133 Add_One_Interp (N, Op_Id, LT);
1135 elsif Is_Array_Type (RT)
1136 and then LT = Base_Type (Component_Type (RT))
1137 then
1138 Add_One_Interp (N, Op_Id, RT);
1140 -- If one operand is a string type or a user-defined array type,
1141 -- and the other is a literal, result is of the specific type.
1143 elsif
1144 (Root_Type (LT) = Standard_String
1145 or else Scope (LT) /= Standard_Standard)
1146 and then Etype (R) = Any_String
1147 then
1148 Add_One_Interp (N, Op_Id, LT);
1150 elsif
1151 (Root_Type (RT) = Standard_String
1152 or else Scope (RT) /= Standard_Standard)
1153 and then Etype (L) = Any_String
1154 then
1155 Add_One_Interp (N, Op_Id, RT);
1157 elsif not Is_Generic_Type (Etype (Op_Id)) then
1158 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1160 else
1161 -- Type and its operations must be visible
1163 Set_Entity (N, Empty);
1164 Analyze_Concatenation (N);
1165 end if;
1167 else
1168 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1169 end if;
1171 else
1172 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1173 while Present (Op_Id) loop
1174 if Ekind (Op_Id) = E_Operator then
1176 -- Do not consider operators declared in dead code, they can
1177 -- not be part of the resolution.
1179 if Is_Eliminated (Op_Id) then
1180 null;
1181 else
1182 Find_Concatenation_Types (L, R, Op_Id, N);
1183 end if;
1185 else
1186 Analyze_User_Defined_Binary_Op (N, Op_Id);
1187 end if;
1189 Op_Id := Homonym (Op_Id);
1190 end loop;
1191 end if;
1193 Operator_Check (N);
1194 end Analyze_Concatenation_Rest;
1196 ------------------------------------
1197 -- Analyze_Conditional_Expression --
1198 ------------------------------------
1200 procedure Analyze_Conditional_Expression (N : Node_Id) is
1201 Condition : constant Node_Id := First (Expressions (N));
1202 Then_Expr : constant Node_Id := Next (Condition);
1203 Else_Expr : constant Node_Id := Next (Then_Expr);
1204 begin
1205 Analyze_Expression (Condition);
1206 Analyze_Expression (Then_Expr);
1207 Analyze_Expression (Else_Expr);
1208 Set_Etype (N, Etype (Then_Expr));
1209 end Analyze_Conditional_Expression;
1211 -------------------------
1212 -- Analyze_Equality_Op --
1213 -------------------------
1215 procedure Analyze_Equality_Op (N : Node_Id) is
1216 Loc : constant Source_Ptr := Sloc (N);
1217 L : constant Node_Id := Left_Opnd (N);
1218 R : constant Node_Id := Right_Opnd (N);
1219 Op_Id : Entity_Id;
1221 begin
1222 Set_Etype (N, Any_Type);
1223 Candidate_Type := Empty;
1225 Analyze_Expression (L);
1226 Analyze_Expression (R);
1228 -- If the entity is set, the node is a generic instance with a non-local
1229 -- reference to the predefined operator or to a user-defined function.
1230 -- It can also be an inequality that is expanded into the negation of a
1231 -- call to a user-defined equality operator.
1233 -- For the predefined case, the result is Boolean, regardless of the
1234 -- type of the operands. The operands may even be limited, if they are
1235 -- generic actuals. If they are overloaded, label the left argument with
1236 -- the common type that must be present, or with the type of the formal
1237 -- of the user-defined function.
1239 if Present (Entity (N)) then
1240 Op_Id := Entity (N);
1242 if Ekind (Op_Id) = E_Operator then
1243 Add_One_Interp (N, Op_Id, Standard_Boolean);
1244 else
1245 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1246 end if;
1248 if Is_Overloaded (L) then
1249 if Ekind (Op_Id) = E_Operator then
1250 Set_Etype (L, Intersect_Types (L, R));
1251 else
1252 Set_Etype (L, Etype (First_Formal (Op_Id)));
1253 end if;
1254 end if;
1256 else
1257 Op_Id := Get_Name_Entity_Id (Chars (N));
1258 while Present (Op_Id) loop
1259 if Ekind (Op_Id) = E_Operator then
1260 Find_Equality_Types (L, R, Op_Id, N);
1261 else
1262 Analyze_User_Defined_Binary_Op (N, Op_Id);
1263 end if;
1265 Op_Id := Homonym (Op_Id);
1266 end loop;
1267 end if;
1269 -- If there was no match, and the operator is inequality, this may
1270 -- be a case where inequality has not been made explicit, as for
1271 -- tagged types. Analyze the node as the negation of an equality
1272 -- operation. This cannot be done earlier, because before analysis
1273 -- we cannot rule out the presence of an explicit inequality.
1275 if Etype (N) = Any_Type
1276 and then Nkind (N) = N_Op_Ne
1277 then
1278 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1279 while Present (Op_Id) loop
1280 if Ekind (Op_Id) = E_Operator then
1281 Find_Equality_Types (L, R, Op_Id, N);
1282 else
1283 Analyze_User_Defined_Binary_Op (N, Op_Id);
1284 end if;
1286 Op_Id := Homonym (Op_Id);
1287 end loop;
1289 if Etype (N) /= Any_Type then
1290 Op_Id := Entity (N);
1292 Rewrite (N,
1293 Make_Op_Not (Loc,
1294 Right_Opnd =>
1295 Make_Op_Eq (Loc,
1296 Left_Opnd => Left_Opnd (N),
1297 Right_Opnd => Right_Opnd (N))));
1299 Set_Entity (Right_Opnd (N), Op_Id);
1300 Analyze (N);
1301 end if;
1302 end if;
1304 Operator_Check (N);
1305 end Analyze_Equality_Op;
1307 ----------------------------------
1308 -- Analyze_Explicit_Dereference --
1309 ----------------------------------
1311 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1312 Loc : constant Source_Ptr := Sloc (N);
1313 P : constant Node_Id := Prefix (N);
1314 T : Entity_Id;
1315 I : Interp_Index;
1316 It : Interp;
1317 New_N : Node_Id;
1319 function Is_Function_Type return Boolean;
1320 -- Check whether node may be interpreted as an implicit function call
1322 ----------------------
1323 -- Is_Function_Type --
1324 ----------------------
1326 function Is_Function_Type return Boolean is
1327 I : Interp_Index;
1328 It : Interp;
1330 begin
1331 if not Is_Overloaded (N) then
1332 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1333 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1335 else
1336 Get_First_Interp (N, I, It);
1337 while Present (It.Nam) loop
1338 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1339 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1340 then
1341 return False;
1342 end if;
1344 Get_Next_Interp (I, It);
1345 end loop;
1347 return True;
1348 end if;
1349 end Is_Function_Type;
1351 -- Start of processing for Analyze_Explicit_Dereference
1353 begin
1354 Analyze (P);
1355 Set_Etype (N, Any_Type);
1357 -- Test for remote access to subprogram type, and if so return
1358 -- after rewriting the original tree.
1360 if Remote_AST_E_Dereference (P) then
1361 return;
1362 end if;
1364 -- Normal processing for other than remote access to subprogram type
1366 if not Is_Overloaded (P) then
1367 if Is_Access_Type (Etype (P)) then
1369 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1370 -- avoid other problems caused by the Private_Subtype and it is
1371 -- safe to go to the Base_Type because this is the same as
1372 -- converting the access value to its Base_Type.
1374 declare
1375 DT : Entity_Id := Designated_Type (Etype (P));
1377 begin
1378 if Ekind (DT) = E_Private_Subtype
1379 and then Is_For_Access_Subtype (DT)
1380 then
1381 DT := Base_Type (DT);
1382 end if;
1384 -- An explicit dereference is a legal occurrence of an
1385 -- incomplete type imported through a limited_with clause,
1386 -- if the full view is visible.
1388 if From_With_Type (DT)
1389 and then not From_With_Type (Scope (DT))
1390 and then
1391 (Is_Immediately_Visible (Scope (DT))
1392 or else
1393 (Is_Child_Unit (Scope (DT))
1394 and then Is_Visible_Child_Unit (Scope (DT))))
1395 then
1396 Set_Etype (N, Available_View (DT));
1398 else
1399 Set_Etype (N, DT);
1400 end if;
1401 end;
1403 elsif Etype (P) /= Any_Type then
1404 Error_Msg_N ("prefix of dereference must be an access type", N);
1405 return;
1406 end if;
1408 else
1409 Get_First_Interp (P, I, It);
1410 while Present (It.Nam) loop
1411 T := It.Typ;
1413 if Is_Access_Type (T) then
1414 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1415 end if;
1417 Get_Next_Interp (I, It);
1418 end loop;
1420 -- Error if no interpretation of the prefix has an access type
1422 if Etype (N) = Any_Type then
1423 Error_Msg_N
1424 ("access type required in prefix of explicit dereference", P);
1425 Set_Etype (N, Any_Type);
1426 return;
1427 end if;
1428 end if;
1430 if Is_Function_Type
1431 and then Nkind (Parent (N)) /= N_Indexed_Component
1433 and then (Nkind (Parent (N)) /= N_Function_Call
1434 or else N /= Name (Parent (N)))
1436 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1437 or else N /= Name (Parent (N)))
1439 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1440 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1441 or else
1442 (Attribute_Name (Parent (N)) /= Name_Address
1443 and then
1444 Attribute_Name (Parent (N)) /= Name_Access))
1445 then
1446 -- Name is a function call with no actuals, in a context that
1447 -- requires deproceduring (including as an actual in an enclosing
1448 -- function or procedure call). There are some pathological cases
1449 -- where the prefix might include functions that return access to
1450 -- subprograms and others that return a regular type. Disambiguation
1451 -- of those has to take place in Resolve.
1453 New_N :=
1454 Make_Function_Call (Loc,
1455 Name => Make_Explicit_Dereference (Loc, P),
1456 Parameter_Associations => New_List);
1458 -- If the prefix is overloaded, remove operations that have formals,
1459 -- we know that this is a parameterless call.
1461 if Is_Overloaded (P) then
1462 Get_First_Interp (P, I, It);
1463 while Present (It.Nam) loop
1464 T := It.Typ;
1466 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1467 Set_Etype (P, T);
1468 else
1469 Remove_Interp (I);
1470 end if;
1472 Get_Next_Interp (I, It);
1473 end loop;
1474 end if;
1476 Rewrite (N, New_N);
1477 Analyze (N);
1479 elsif not Is_Function_Type
1480 and then Is_Overloaded (N)
1481 then
1482 -- The prefix may include access to subprograms and other access
1483 -- types. If the context selects the interpretation that is a call,
1484 -- we cannot rewrite the node yet, but we include the result of
1485 -- the call interpretation.
1487 Get_First_Interp (N, I, It);
1488 while Present (It.Nam) loop
1489 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1490 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1491 then
1492 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1493 end if;
1495 Get_Next_Interp (I, It);
1496 end loop;
1497 end if;
1499 -- A value of remote access-to-class-wide must not be dereferenced
1500 -- (RM E.2.2(16)).
1502 Validate_Remote_Access_To_Class_Wide_Type (N);
1503 end Analyze_Explicit_Dereference;
1505 ------------------------
1506 -- Analyze_Expression --
1507 ------------------------
1509 procedure Analyze_Expression (N : Node_Id) is
1510 begin
1511 Analyze (N);
1512 Check_Parameterless_Call (N);
1513 end Analyze_Expression;
1515 ------------------------------------
1516 -- Analyze_Indexed_Component_Form --
1517 ------------------------------------
1519 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1520 P : constant Node_Id := Prefix (N);
1521 Exprs : constant List_Id := Expressions (N);
1522 Exp : Node_Id;
1523 P_T : Entity_Id;
1524 E : Node_Id;
1525 U_N : Entity_Id;
1527 procedure Process_Function_Call;
1528 -- Prefix in indexed component form is an overloadable entity,
1529 -- so the node is a function call. Reformat it as such.
1531 procedure Process_Indexed_Component;
1532 -- Prefix in indexed component form is actually an indexed component.
1533 -- This routine processes it, knowing that the prefix is already
1534 -- resolved.
1536 procedure Process_Indexed_Component_Or_Slice;
1537 -- An indexed component with a single index may designate a slice if
1538 -- the index is a subtype mark. This routine disambiguates these two
1539 -- cases by resolving the prefix to see if it is a subtype mark.
1541 procedure Process_Overloaded_Indexed_Component;
1542 -- If the prefix of an indexed component is overloaded, the proper
1543 -- interpretation is selected by the index types and the context.
1545 ---------------------------
1546 -- Process_Function_Call --
1547 ---------------------------
1549 procedure Process_Function_Call is
1550 Actual : Node_Id;
1552 begin
1553 Change_Node (N, N_Function_Call);
1554 Set_Name (N, P);
1555 Set_Parameter_Associations (N, Exprs);
1557 -- Analyze actuals prior to analyzing the call itself
1559 Actual := First (Parameter_Associations (N));
1560 while Present (Actual) loop
1561 Analyze (Actual);
1562 Check_Parameterless_Call (Actual);
1564 -- Move to next actual. Note that we use Next, not Next_Actual
1565 -- here. The reason for this is a bit subtle. If a function call
1566 -- includes named associations, the parser recognizes the node as
1567 -- a call, and it is analyzed as such. If all associations are
1568 -- positional, the parser builds an indexed_component node, and
1569 -- it is only after analysis of the prefix that the construct
1570 -- is recognized as a call, in which case Process_Function_Call
1571 -- rewrites the node and analyzes the actuals. If the list of
1572 -- actuals is malformed, the parser may leave the node as an
1573 -- indexed component (despite the presence of named associations).
1574 -- The iterator Next_Actual is equivalent to Next if the list is
1575 -- positional, but follows the normalized chain of actuals when
1576 -- named associations are present. In this case normalization has
1577 -- not taken place, and actuals remain unanalyzed, which leads to
1578 -- subsequent crashes or loops if there is an attempt to continue
1579 -- analysis of the program.
1581 Next (Actual);
1582 end loop;
1584 Analyze_Call (N);
1585 end Process_Function_Call;
1587 -------------------------------
1588 -- Process_Indexed_Component --
1589 -------------------------------
1591 procedure Process_Indexed_Component is
1592 Exp : Node_Id;
1593 Array_Type : Entity_Id;
1594 Index : Node_Id;
1595 Pent : Entity_Id := Empty;
1597 begin
1598 Exp := First (Exprs);
1600 if Is_Overloaded (P) then
1601 Process_Overloaded_Indexed_Component;
1603 else
1604 Array_Type := Etype (P);
1606 if Is_Entity_Name (P) then
1607 Pent := Entity (P);
1608 elsif Nkind (P) = N_Selected_Component
1609 and then Is_Entity_Name (Selector_Name (P))
1610 then
1611 Pent := Entity (Selector_Name (P));
1612 end if;
1614 -- Prefix must be appropriate for an array type, taking into
1615 -- account a possible implicit dereference.
1617 if Is_Access_Type (Array_Type) then
1618 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1619 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
1620 end if;
1622 if Is_Array_Type (Array_Type) then
1623 null;
1625 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
1626 Analyze (Exp);
1627 Set_Etype (N, Any_Type);
1629 if not Has_Compatible_Type
1630 (Exp, Entry_Index_Type (Pent))
1631 then
1632 Error_Msg_N ("invalid index type in entry name", N);
1634 elsif Present (Next (Exp)) then
1635 Error_Msg_N ("too many subscripts in entry reference", N);
1637 else
1638 Set_Etype (N, Etype (P));
1639 end if;
1641 return;
1643 elsif Is_Record_Type (Array_Type)
1644 and then Remote_AST_I_Dereference (P)
1645 then
1646 return;
1648 elsif Array_Type = Any_Type then
1649 Set_Etype (N, Any_Type);
1650 return;
1652 -- Here we definitely have a bad indexing
1654 else
1655 if Nkind (Parent (N)) = N_Requeue_Statement
1656 and then Present (Pent) and then Ekind (Pent) = E_Entry
1657 then
1658 Error_Msg_N
1659 ("REQUEUE does not permit parameters", First (Exprs));
1661 elsif Is_Entity_Name (P)
1662 and then Etype (P) = Standard_Void_Type
1663 then
1664 Error_Msg_NE ("incorrect use of&", P, Entity (P));
1666 else
1667 Error_Msg_N ("array type required in indexed component", P);
1668 end if;
1670 Set_Etype (N, Any_Type);
1671 return;
1672 end if;
1674 Index := First_Index (Array_Type);
1675 while Present (Index) and then Present (Exp) loop
1676 if not Has_Compatible_Type (Exp, Etype (Index)) then
1677 Wrong_Type (Exp, Etype (Index));
1678 Set_Etype (N, Any_Type);
1679 return;
1680 end if;
1682 Next_Index (Index);
1683 Next (Exp);
1684 end loop;
1686 Set_Etype (N, Component_Type (Array_Type));
1688 if Present (Index) then
1689 Error_Msg_N
1690 ("too few subscripts in array reference", First (Exprs));
1692 elsif Present (Exp) then
1693 Error_Msg_N ("too many subscripts in array reference", Exp);
1694 end if;
1695 end if;
1696 end Process_Indexed_Component;
1698 ----------------------------------------
1699 -- Process_Indexed_Component_Or_Slice --
1700 ----------------------------------------
1702 procedure Process_Indexed_Component_Or_Slice is
1703 begin
1704 Exp := First (Exprs);
1705 while Present (Exp) loop
1706 Analyze_Expression (Exp);
1707 Next (Exp);
1708 end loop;
1710 Exp := First (Exprs);
1712 -- If one index is present, and it is a subtype name, then the
1713 -- node denotes a slice (note that the case of an explicit range
1714 -- for a slice was already built as an N_Slice node in the first
1715 -- place, so that case is not handled here).
1717 -- We use a replace rather than a rewrite here because this is one
1718 -- of the cases in which the tree built by the parser is plain wrong.
1720 if No (Next (Exp))
1721 and then Is_Entity_Name (Exp)
1722 and then Is_Type (Entity (Exp))
1723 then
1724 Replace (N,
1725 Make_Slice (Sloc (N),
1726 Prefix => P,
1727 Discrete_Range => New_Copy (Exp)));
1728 Analyze (N);
1730 -- Otherwise (more than one index present, or single index is not
1731 -- a subtype name), then we have the indexed component case.
1733 else
1734 Process_Indexed_Component;
1735 end if;
1736 end Process_Indexed_Component_Or_Slice;
1738 ------------------------------------------
1739 -- Process_Overloaded_Indexed_Component --
1740 ------------------------------------------
1742 procedure Process_Overloaded_Indexed_Component is
1743 Exp : Node_Id;
1744 I : Interp_Index;
1745 It : Interp;
1746 Typ : Entity_Id;
1747 Index : Node_Id;
1748 Found : Boolean;
1750 begin
1751 Set_Etype (N, Any_Type);
1753 Get_First_Interp (P, I, It);
1754 while Present (It.Nam) loop
1755 Typ := It.Typ;
1757 if Is_Access_Type (Typ) then
1758 Typ := Designated_Type (Typ);
1759 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1760 end if;
1762 if Is_Array_Type (Typ) then
1764 -- Got a candidate: verify that index types are compatible
1766 Index := First_Index (Typ);
1767 Found := True;
1768 Exp := First (Exprs);
1769 while Present (Index) and then Present (Exp) loop
1770 if Has_Compatible_Type (Exp, Etype (Index)) then
1771 null;
1772 else
1773 Found := False;
1774 Remove_Interp (I);
1775 exit;
1776 end if;
1778 Next_Index (Index);
1779 Next (Exp);
1780 end loop;
1782 if Found and then No (Index) and then No (Exp) then
1783 Add_One_Interp (N,
1784 Etype (Component_Type (Typ)),
1785 Etype (Component_Type (Typ)));
1786 end if;
1787 end if;
1789 Get_Next_Interp (I, It);
1790 end loop;
1792 if Etype (N) = Any_Type then
1793 Error_Msg_N ("no legal interpretation for indexed component", N);
1794 Set_Is_Overloaded (N, False);
1795 end if;
1797 End_Interp_List;
1798 end Process_Overloaded_Indexed_Component;
1800 -- Start of processing for Analyze_Indexed_Component_Form
1802 begin
1803 -- Get name of array, function or type
1805 Analyze (P);
1807 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) then
1809 -- If P is an explicit dereference whose prefix is of a
1810 -- remote access-to-subprogram type, then N has already
1811 -- been rewritten as a subprogram call and analyzed.
1813 return;
1814 end if;
1816 pragma Assert (Nkind (N) = N_Indexed_Component);
1818 P_T := Base_Type (Etype (P));
1820 if Is_Entity_Name (P)
1821 or else Nkind (P) = N_Operator_Symbol
1822 then
1823 U_N := Entity (P);
1825 if Is_Type (U_N) then
1827 -- Reformat node as a type conversion
1829 E := Remove_Head (Exprs);
1831 if Present (First (Exprs)) then
1832 Error_Msg_N
1833 ("argument of type conversion must be single expression", N);
1834 end if;
1836 Change_Node (N, N_Type_Conversion);
1837 Set_Subtype_Mark (N, P);
1838 Set_Etype (N, U_N);
1839 Set_Expression (N, E);
1841 -- After changing the node, call for the specific Analysis
1842 -- routine directly, to avoid a double call to the expander.
1844 Analyze_Type_Conversion (N);
1845 return;
1846 end if;
1848 if Is_Overloadable (U_N) then
1849 Process_Function_Call;
1851 elsif Ekind (Etype (P)) = E_Subprogram_Type
1852 or else (Is_Access_Type (Etype (P))
1853 and then
1854 Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type)
1855 then
1856 -- Call to access_to-subprogram with possible implicit dereference
1858 Process_Function_Call;
1860 elsif Is_Generic_Subprogram (U_N) then
1862 -- A common beginner's (or C++ templates fan) error
1864 Error_Msg_N ("generic subprogram cannot be called", N);
1865 Set_Etype (N, Any_Type);
1866 return;
1868 else
1869 Process_Indexed_Component_Or_Slice;
1870 end if;
1872 -- If not an entity name, prefix is an expression that may denote
1873 -- an array or an access-to-subprogram.
1875 else
1876 if Ekind (P_T) = E_Subprogram_Type
1877 or else (Is_Access_Type (P_T)
1878 and then
1879 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
1880 then
1881 Process_Function_Call;
1883 elsif Nkind (P) = N_Selected_Component
1884 and then Is_Overloadable (Entity (Selector_Name (P)))
1885 then
1886 Process_Function_Call;
1888 else
1889 -- Indexed component, slice, or a call to a member of a family
1890 -- entry, which will be converted to an entry call later.
1892 Process_Indexed_Component_Or_Slice;
1893 end if;
1894 end if;
1895 end Analyze_Indexed_Component_Form;
1897 ------------------------
1898 -- Analyze_Logical_Op --
1899 ------------------------
1901 procedure Analyze_Logical_Op (N : Node_Id) is
1902 L : constant Node_Id := Left_Opnd (N);
1903 R : constant Node_Id := Right_Opnd (N);
1904 Op_Id : Entity_Id := Entity (N);
1906 begin
1907 Set_Etype (N, Any_Type);
1908 Candidate_Type := Empty;
1910 Analyze_Expression (L);
1911 Analyze_Expression (R);
1913 if Present (Op_Id) then
1915 if Ekind (Op_Id) = E_Operator then
1916 Find_Boolean_Types (L, R, Op_Id, N);
1917 else
1918 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1919 end if;
1921 else
1922 Op_Id := Get_Name_Entity_Id (Chars (N));
1923 while Present (Op_Id) loop
1924 if Ekind (Op_Id) = E_Operator then
1925 Find_Boolean_Types (L, R, Op_Id, N);
1926 else
1927 Analyze_User_Defined_Binary_Op (N, Op_Id);
1928 end if;
1930 Op_Id := Homonym (Op_Id);
1931 end loop;
1932 end if;
1934 Operator_Check (N);
1935 end Analyze_Logical_Op;
1937 ---------------------------
1938 -- Analyze_Membership_Op --
1939 ---------------------------
1941 procedure Analyze_Membership_Op (N : Node_Id) is
1942 L : constant Node_Id := Left_Opnd (N);
1943 R : constant Node_Id := Right_Opnd (N);
1945 Index : Interp_Index;
1946 It : Interp;
1947 Found : Boolean := False;
1948 I_F : Interp_Index;
1949 T_F : Entity_Id;
1951 procedure Try_One_Interp (T1 : Entity_Id);
1952 -- Routine to try one proposed interpretation. Note that the context
1953 -- of the operation plays no role in resolving the arguments, so that
1954 -- if there is more than one interpretation of the operands that is
1955 -- compatible with a membership test, the operation is ambiguous.
1957 --------------------
1958 -- Try_One_Interp --
1959 --------------------
1961 procedure Try_One_Interp (T1 : Entity_Id) is
1962 begin
1963 if Has_Compatible_Type (R, T1) then
1964 if Found
1965 and then Base_Type (T1) /= Base_Type (T_F)
1966 then
1967 It := Disambiguate (L, I_F, Index, Any_Type);
1969 if It = No_Interp then
1970 Ambiguous_Operands (N);
1971 Set_Etype (L, Any_Type);
1972 return;
1974 else
1975 T_F := It.Typ;
1976 end if;
1978 else
1979 Found := True;
1980 T_F := T1;
1981 I_F := Index;
1982 end if;
1984 Set_Etype (L, T_F);
1985 end if;
1987 end Try_One_Interp;
1989 -- Start of processing for Analyze_Membership_Op
1991 begin
1992 Analyze_Expression (L);
1994 if Nkind (R) = N_Range
1995 or else (Nkind (R) = N_Attribute_Reference
1996 and then Attribute_Name (R) = Name_Range)
1997 then
1998 Analyze (R);
2000 if not Is_Overloaded (L) then
2001 Try_One_Interp (Etype (L));
2003 else
2004 Get_First_Interp (L, Index, It);
2005 while Present (It.Typ) loop
2006 Try_One_Interp (It.Typ);
2007 Get_Next_Interp (Index, It);
2008 end loop;
2009 end if;
2011 -- If not a range, it can only be a subtype mark, or else there
2012 -- is a more basic error, to be diagnosed in Find_Type.
2014 else
2015 Find_Type (R);
2017 if Is_Entity_Name (R) then
2018 Check_Fully_Declared (Entity (R), R);
2019 end if;
2020 end if;
2022 -- Compatibility between expression and subtype mark or range is
2023 -- checked during resolution. The result of the operation is Boolean
2024 -- in any case.
2026 Set_Etype (N, Standard_Boolean);
2028 if Comes_From_Source (N)
2029 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2030 then
2031 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2032 end if;
2033 end Analyze_Membership_Op;
2035 ----------------------
2036 -- Analyze_Negation --
2037 ----------------------
2039 procedure Analyze_Negation (N : Node_Id) is
2040 R : constant Node_Id := Right_Opnd (N);
2041 Op_Id : Entity_Id := Entity (N);
2043 begin
2044 Set_Etype (N, Any_Type);
2045 Candidate_Type := Empty;
2047 Analyze_Expression (R);
2049 if Present (Op_Id) then
2050 if Ekind (Op_Id) = E_Operator then
2051 Find_Negation_Types (R, Op_Id, N);
2052 else
2053 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2054 end if;
2056 else
2057 Op_Id := Get_Name_Entity_Id (Chars (N));
2058 while Present (Op_Id) loop
2059 if Ekind (Op_Id) = E_Operator then
2060 Find_Negation_Types (R, Op_Id, N);
2061 else
2062 Analyze_User_Defined_Unary_Op (N, Op_Id);
2063 end if;
2065 Op_Id := Homonym (Op_Id);
2066 end loop;
2067 end if;
2069 Operator_Check (N);
2070 end Analyze_Negation;
2072 ------------------
2073 -- Analyze_Null --
2074 ------------------
2076 procedure Analyze_Null (N : Node_Id) is
2077 begin
2078 Set_Etype (N, Any_Access);
2079 end Analyze_Null;
2081 ----------------------
2082 -- Analyze_One_Call --
2083 ----------------------
2085 procedure Analyze_One_Call
2086 (N : Node_Id;
2087 Nam : Entity_Id;
2088 Report : Boolean;
2089 Success : out Boolean;
2090 Skip_First : Boolean := False)
2092 Actuals : constant List_Id := Parameter_Associations (N);
2093 Prev_T : constant Entity_Id := Etype (N);
2095 Must_Skip : constant Boolean := Skip_First
2096 or else Nkind (Original_Node (N)) = N_Selected_Component
2097 or else
2098 (Nkind (Original_Node (N)) = N_Indexed_Component
2099 and then Nkind (Prefix (Original_Node (N)))
2100 = N_Selected_Component);
2101 -- The first formal must be omitted from the match when trying to find
2102 -- a primitive operation that is a possible interpretation, and also
2103 -- after the call has been rewritten, because the corresponding actual
2104 -- is already known to be compatible, and because this may be an
2105 -- indexing of a call with default parameters.
2107 Formal : Entity_Id;
2108 Actual : Node_Id;
2109 Is_Indexed : Boolean := False;
2110 Subp_Type : constant Entity_Id := Etype (Nam);
2111 Norm_OK : Boolean;
2113 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2114 -- There may be a user-defined operator that hides the current
2115 -- interpretation. We must check for this independently of the
2116 -- analysis of the call with the user-defined operation, because
2117 -- the parameter names may be wrong and yet the hiding takes place.
2118 -- This fixes a problem with ACATS test B34014O.
2120 -- When the type Address is a visible integer type, and the DEC
2121 -- system extension is visible, the predefined operator may be
2122 -- hidden as well, by one of the address operations in auxdec.
2123 -- Finally, The abstract operations on address do not hide the
2124 -- predefined operator (this is the purpose of making them abstract).
2126 procedure Indicate_Name_And_Type;
2127 -- If candidate interpretation matches, indicate name and type of
2128 -- result on call node.
2130 ----------------------------
2131 -- Indicate_Name_And_Type --
2132 ----------------------------
2134 procedure Indicate_Name_And_Type is
2135 begin
2136 Add_One_Interp (N, Nam, Etype (Nam));
2137 Success := True;
2139 -- If the prefix of the call is a name, indicate the entity
2140 -- being called. If it is not a name, it is an expression that
2141 -- denotes an access to subprogram or else an entry or family. In
2142 -- the latter case, the name is a selected component, and the entity
2143 -- being called is noted on the selector.
2145 if not Is_Type (Nam) then
2146 if Is_Entity_Name (Name (N))
2147 or else Nkind (Name (N)) = N_Operator_Symbol
2148 then
2149 Set_Entity (Name (N), Nam);
2151 elsif Nkind (Name (N)) = N_Selected_Component then
2152 Set_Entity (Selector_Name (Name (N)), Nam);
2153 end if;
2154 end if;
2156 if Debug_Flag_E and not Report then
2157 Write_Str (" Overloaded call ");
2158 Write_Int (Int (N));
2159 Write_Str (" compatible with ");
2160 Write_Int (Int (Nam));
2161 Write_Eol;
2162 end if;
2163 end Indicate_Name_And_Type;
2165 ------------------------
2166 -- Operator_Hidden_By --
2167 ------------------------
2169 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2170 Act1 : constant Node_Id := First_Actual (N);
2171 Act2 : constant Node_Id := Next_Actual (Act1);
2172 Form1 : constant Entity_Id := First_Formal (Fun);
2173 Form2 : constant Entity_Id := Next_Formal (Form1);
2175 begin
2176 if Ekind (Fun) /= E_Function
2177 or else Is_Abstract_Subprogram (Fun)
2178 then
2179 return False;
2181 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2182 return False;
2184 elsif Present (Form2) then
2186 No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2187 then
2188 return False;
2189 end if;
2191 elsif Present (Act2) then
2192 return False;
2193 end if;
2195 -- Now we know that the arity of the operator matches the function,
2196 -- and the function call is a valid interpretation. The function
2197 -- hides the operator if it has the right signature, or if one of
2198 -- its operands is a non-abstract operation on Address when this is
2199 -- a visible integer type.
2201 return Hides_Op (Fun, Nam)
2202 or else Is_Descendent_Of_Address (Etype (Form1))
2203 or else
2204 (Present (Form2)
2205 and then Is_Descendent_Of_Address (Etype (Form2)));
2206 end Operator_Hidden_By;
2208 -- Start of processing for Analyze_One_Call
2210 begin
2211 Success := False;
2213 -- If the subprogram has no formals or if all the formals have defaults,
2214 -- and the return type is an array type, the node may denote an indexing
2215 -- of the result of a parameterless call. In Ada 2005, the subprogram
2216 -- may have one non-defaulted formal, and the call may have been written
2217 -- in prefix notation, so that the rebuilt parameter list has more than
2218 -- one actual.
2220 if Present (Actuals)
2221 and then
2222 (Needs_No_Actuals (Nam)
2223 or else
2224 (Needs_One_Actual (Nam)
2225 and then Present (Next_Actual (First (Actuals)))))
2226 then
2227 if Is_Array_Type (Subp_Type) then
2228 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2230 elsif Is_Access_Type (Subp_Type)
2231 and then Is_Array_Type (Designated_Type (Subp_Type))
2232 then
2233 Is_Indexed :=
2234 Try_Indexed_Call
2235 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2237 -- The prefix can also be a parameterless function that returns an
2238 -- access to subprogram, in which case this is an indirect call.
2240 elsif Is_Access_Type (Subp_Type)
2241 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2242 then
2243 Is_Indexed := Try_Indirect_Call (N, Nam, Subp_Type);
2244 end if;
2246 end if;
2248 -- If the call has been transformed into a slice, it is of the form
2249 -- F (Subtype) where F is paramterless. The node has ben rewritten in
2250 -- Try_Indexed_Call and there is nothing else to do.
2252 if Is_Indexed
2253 and then Nkind (N) = N_Slice
2254 then
2255 return;
2256 end if;
2258 Normalize_Actuals (N, Nam, (Report and not Is_Indexed), Norm_OK);
2260 if not Norm_OK then
2262 -- Mismatch in number or names of parameters
2264 if Debug_Flag_E then
2265 Write_Str (" normalization fails in call ");
2266 Write_Int (Int (N));
2267 Write_Str (" with subprogram ");
2268 Write_Int (Int (Nam));
2269 Write_Eol;
2270 end if;
2272 -- If the context expects a function call, discard any interpretation
2273 -- that is a procedure. If the node is not overloaded, leave as is for
2274 -- better error reporting when type mismatch is found.
2276 elsif Nkind (N) = N_Function_Call
2277 and then Is_Overloaded (Name (N))
2278 and then Ekind (Nam) = E_Procedure
2279 then
2280 return;
2282 -- Ditto for function calls in a procedure context
2284 elsif Nkind (N) = N_Procedure_Call_Statement
2285 and then Is_Overloaded (Name (N))
2286 and then Etype (Nam) /= Standard_Void_Type
2287 then
2288 return;
2290 elsif No (Actuals) then
2292 -- If Normalize succeeds, then there are default parameters for
2293 -- all formals.
2295 Indicate_Name_And_Type;
2297 elsif Ekind (Nam) = E_Operator then
2298 if Nkind (N) = N_Procedure_Call_Statement then
2299 return;
2300 end if;
2302 -- This can occur when the prefix of the call is an operator
2303 -- name or an expanded name whose selector is an operator name.
2305 Analyze_Operator_Call (N, Nam);
2307 if Etype (N) /= Prev_T then
2309 -- Check that operator is not hidden by a function interpretation
2311 if Is_Overloaded (Name (N)) then
2312 declare
2313 I : Interp_Index;
2314 It : Interp;
2316 begin
2317 Get_First_Interp (Name (N), I, It);
2318 while Present (It.Nam) loop
2319 if Operator_Hidden_By (It.Nam) then
2320 Set_Etype (N, Prev_T);
2321 return;
2322 end if;
2324 Get_Next_Interp (I, It);
2325 end loop;
2326 end;
2327 end if;
2329 -- If operator matches formals, record its name on the call.
2330 -- If the operator is overloaded, Resolve will select the
2331 -- correct one from the list of interpretations. The call
2332 -- node itself carries the first candidate.
2334 Set_Entity (Name (N), Nam);
2335 Success := True;
2337 elsif Report and then Etype (N) = Any_Type then
2338 Error_Msg_N ("incompatible arguments for operator", N);
2339 end if;
2341 else
2342 -- Normalize_Actuals has chained the named associations in the
2343 -- correct order of the formals.
2345 Actual := First_Actual (N);
2346 Formal := First_Formal (Nam);
2348 -- If we are analyzing a call rewritten from object notation,
2349 -- skip first actual, which may be rewritten later as an
2350 -- explicit dereference.
2352 if Must_Skip then
2353 Next_Actual (Actual);
2354 Next_Formal (Formal);
2355 end if;
2357 while Present (Actual) and then Present (Formal) loop
2358 if Nkind (Parent (Actual)) /= N_Parameter_Association
2359 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2360 then
2361 -- The actual can be compatible with the formal, but we must
2362 -- also check that the context is not an address type that is
2363 -- visibly an integer type, as is the case in VMS_64. In this
2364 -- case the use of literals is illegal, except in the body of
2365 -- descendents of system, where arithmetic operations on
2366 -- address are of course used.
2368 if Has_Compatible_Type (Actual, Etype (Formal))
2369 and then
2370 (Etype (Actual) /= Universal_Integer
2371 or else not Is_Descendent_Of_Address (Etype (Formal))
2372 or else
2373 Is_Predefined_File_Name
2374 (Unit_File_Name (Get_Source_Unit (N))))
2375 then
2376 Next_Actual (Actual);
2377 Next_Formal (Formal);
2379 else
2380 if Debug_Flag_E then
2381 Write_Str (" type checking fails in call ");
2382 Write_Int (Int (N));
2383 Write_Str (" with formal ");
2384 Write_Int (Int (Formal));
2385 Write_Str (" in subprogram ");
2386 Write_Int (Int (Nam));
2387 Write_Eol;
2388 end if;
2390 if Report and not Is_Indexed then
2392 -- Ada 2005 (AI-251): Complete the error notification
2393 -- to help new Ada 2005 users
2395 if Is_Class_Wide_Type (Etype (Formal))
2396 and then Is_Interface (Etype (Etype (Formal)))
2397 and then not Interface_Present_In_Ancestor
2398 (Typ => Etype (Actual),
2399 Iface => Etype (Etype (Formal)))
2400 then
2401 Error_Msg_NE
2402 ("(Ada 2005) does not implement interface }",
2403 Actual, Etype (Etype (Formal)));
2404 end if;
2406 Wrong_Type (Actual, Etype (Formal));
2408 if Nkind (Actual) = N_Op_Eq
2409 and then Nkind (Left_Opnd (Actual)) = N_Identifier
2410 then
2411 Formal := First_Formal (Nam);
2412 while Present (Formal) loop
2413 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2414 Error_Msg_N
2415 ("possible misspelling of `='>`!", Actual);
2416 exit;
2417 end if;
2419 Next_Formal (Formal);
2420 end loop;
2421 end if;
2423 if All_Errors_Mode then
2424 Error_Msg_Sloc := Sloc (Nam);
2426 if Is_Overloadable (Nam)
2427 and then Present (Alias (Nam))
2428 and then not Comes_From_Source (Nam)
2429 then
2430 Error_Msg_NE
2431 ("\\ =='> in call to inherited operation & #!",
2432 Actual, Nam);
2434 elsif Ekind (Nam) = E_Subprogram_Type then
2435 declare
2436 Access_To_Subprogram_Typ :
2437 constant Entity_Id :=
2438 Defining_Identifier
2439 (Associated_Node_For_Itype (Nam));
2440 begin
2441 Error_Msg_NE (
2442 "\\ =='> in call to dereference of &#!",
2443 Actual, Access_To_Subprogram_Typ);
2444 end;
2446 else
2447 Error_Msg_NE
2448 ("\\ =='> in call to &#!", Actual, Nam);
2450 end if;
2451 end if;
2452 end if;
2454 return;
2455 end if;
2457 else
2458 -- Normalize_Actuals has verified that a default value exists
2459 -- for this formal. Current actual names a subsequent formal.
2461 Next_Formal (Formal);
2462 end if;
2463 end loop;
2465 -- On exit, all actuals match
2467 Indicate_Name_And_Type;
2468 end if;
2469 end Analyze_One_Call;
2471 ---------------------------
2472 -- Analyze_Operator_Call --
2473 ---------------------------
2475 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
2476 Op_Name : constant Name_Id := Chars (Op_Id);
2477 Act1 : constant Node_Id := First_Actual (N);
2478 Act2 : constant Node_Id := Next_Actual (Act1);
2480 begin
2481 -- Binary operator case
2483 if Present (Act2) then
2485 -- If more than two operands, then not binary operator after all
2487 if Present (Next_Actual (Act2)) then
2488 return;
2490 elsif Op_Name = Name_Op_Add
2491 or else Op_Name = Name_Op_Subtract
2492 or else Op_Name = Name_Op_Multiply
2493 or else Op_Name = Name_Op_Divide
2494 or else Op_Name = Name_Op_Mod
2495 or else Op_Name = Name_Op_Rem
2496 or else Op_Name = Name_Op_Expon
2497 then
2498 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
2500 elsif Op_Name = Name_Op_And
2501 or else Op_Name = Name_Op_Or
2502 or else Op_Name = Name_Op_Xor
2503 then
2504 Find_Boolean_Types (Act1, Act2, Op_Id, N);
2506 elsif Op_Name = Name_Op_Lt
2507 or else Op_Name = Name_Op_Le
2508 or else Op_Name = Name_Op_Gt
2509 or else Op_Name = Name_Op_Ge
2510 then
2511 Find_Comparison_Types (Act1, Act2, Op_Id, N);
2513 elsif Op_Name = Name_Op_Eq
2514 or else Op_Name = Name_Op_Ne
2515 then
2516 Find_Equality_Types (Act1, Act2, Op_Id, N);
2518 elsif Op_Name = Name_Op_Concat then
2519 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
2521 -- Is this else null correct, or should it be an abort???
2523 else
2524 null;
2525 end if;
2527 -- Unary operator case
2529 else
2530 if Op_Name = Name_Op_Subtract or else
2531 Op_Name = Name_Op_Add or else
2532 Op_Name = Name_Op_Abs
2533 then
2534 Find_Unary_Types (Act1, Op_Id, N);
2536 elsif
2537 Op_Name = Name_Op_Not
2538 then
2539 Find_Negation_Types (Act1, Op_Id, N);
2541 -- Is this else null correct, or should it be an abort???
2543 else
2544 null;
2545 end if;
2546 end if;
2547 end Analyze_Operator_Call;
2549 -------------------------------------------
2550 -- Analyze_Overloaded_Selected_Component --
2551 -------------------------------------------
2553 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
2554 Nam : constant Node_Id := Prefix (N);
2555 Sel : constant Node_Id := Selector_Name (N);
2556 Comp : Entity_Id;
2557 I : Interp_Index;
2558 It : Interp;
2559 T : Entity_Id;
2561 begin
2562 Set_Etype (Sel, Any_Type);
2564 Get_First_Interp (Nam, I, It);
2565 while Present (It.Typ) loop
2566 if Is_Access_Type (It.Typ) then
2567 T := Designated_Type (It.Typ);
2568 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2569 else
2570 T := It.Typ;
2571 end if;
2573 if Is_Record_Type (T) then
2575 -- If the prefix is a class-wide type, the visible components are
2576 -- those of the base type.
2578 if Is_Class_Wide_Type (T) then
2579 T := Etype (T);
2580 end if;
2582 Comp := First_Entity (T);
2583 while Present (Comp) loop
2584 if Chars (Comp) = Chars (Sel)
2585 and then Is_Visible_Component (Comp)
2586 then
2587 Set_Entity (Sel, Comp);
2588 Set_Etype (Sel, Etype (Comp));
2589 Add_One_Interp (N, Etype (Comp), Etype (Comp));
2591 -- This also specifies a candidate to resolve the name.
2592 -- Further overloading will be resolved from context.
2594 Set_Etype (Nam, It.Typ);
2595 end if;
2597 Next_Entity (Comp);
2598 end loop;
2600 elsif Is_Concurrent_Type (T) then
2601 Comp := First_Entity (T);
2602 while Present (Comp)
2603 and then Comp /= First_Private_Entity (T)
2604 loop
2605 if Chars (Comp) = Chars (Sel) then
2606 if Is_Overloadable (Comp) then
2607 Add_One_Interp (Sel, Comp, Etype (Comp));
2608 else
2609 Set_Entity_With_Style_Check (Sel, Comp);
2610 Generate_Reference (Comp, Sel);
2611 end if;
2613 Set_Etype (Sel, Etype (Comp));
2614 Set_Etype (N, Etype (Comp));
2615 Set_Etype (Nam, It.Typ);
2617 -- For access type case, introduce explicit deference for
2618 -- more uniform treatment of entry calls. Do this only
2619 -- once if several interpretations yield an access type.
2621 if Is_Access_Type (Etype (Nam))
2622 and then Nkind (Nam) /= N_Explicit_Dereference
2623 then
2624 Insert_Explicit_Dereference (Nam);
2625 Error_Msg_NW
2626 (Warn_On_Dereference, "?implicit dereference", N);
2627 end if;
2628 end if;
2630 Next_Entity (Comp);
2631 end loop;
2633 Set_Is_Overloaded (N, Is_Overloaded (Sel));
2634 end if;
2636 Get_Next_Interp (I, It);
2637 end loop;
2639 if Etype (N) = Any_Type
2640 and then not Try_Object_Operation (N)
2641 then
2642 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
2643 Set_Entity (Sel, Any_Id);
2644 Set_Etype (Sel, Any_Type);
2645 end if;
2646 end Analyze_Overloaded_Selected_Component;
2648 ----------------------------------
2649 -- Analyze_Qualified_Expression --
2650 ----------------------------------
2652 procedure Analyze_Qualified_Expression (N : Node_Id) is
2653 Mark : constant Entity_Id := Subtype_Mark (N);
2654 Expr : constant Node_Id := Expression (N);
2655 I : Interp_Index;
2656 It : Interp;
2657 T : Entity_Id;
2659 begin
2660 Analyze_Expression (Expr);
2662 Set_Etype (N, Any_Type);
2663 Find_Type (Mark);
2664 T := Entity (Mark);
2665 Set_Etype (N, T);
2667 if T = Any_Type then
2668 return;
2669 end if;
2671 Check_Fully_Declared (T, N);
2673 -- If expected type is class-wide, check for exact match before
2674 -- expansion, because if the expression is a dispatching call it
2675 -- may be rewritten as explicit dereference with class-wide result.
2676 -- If expression is overloaded, retain only interpretations that
2677 -- will yield exact matches.
2679 if Is_Class_Wide_Type (T) then
2680 if not Is_Overloaded (Expr) then
2681 if Base_Type (Etype (Expr)) /= Base_Type (T) then
2682 if Nkind (Expr) = N_Aggregate then
2683 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
2684 else
2685 Wrong_Type (Expr, T);
2686 end if;
2687 end if;
2689 else
2690 Get_First_Interp (Expr, I, It);
2692 while Present (It.Nam) loop
2693 if Base_Type (It.Typ) /= Base_Type (T) then
2694 Remove_Interp (I);
2695 end if;
2697 Get_Next_Interp (I, It);
2698 end loop;
2699 end if;
2700 end if;
2702 Set_Etype (N, T);
2703 end Analyze_Qualified_Expression;
2705 -------------------
2706 -- Analyze_Range --
2707 -------------------
2709 procedure Analyze_Range (N : Node_Id) is
2710 L : constant Node_Id := Low_Bound (N);
2711 H : constant Node_Id := High_Bound (N);
2712 I1, I2 : Interp_Index;
2713 It1, It2 : Interp;
2715 procedure Check_Common_Type (T1, T2 : Entity_Id);
2716 -- Verify the compatibility of two types, and choose the
2717 -- non universal one if the other is universal.
2719 procedure Check_High_Bound (T : Entity_Id);
2720 -- Test one interpretation of the low bound against all those
2721 -- of the high bound.
2723 procedure Check_Universal_Expression (N : Node_Id);
2724 -- In Ada83, reject bounds of a universal range that are not
2725 -- literals or entity names.
2727 -----------------------
2728 -- Check_Common_Type --
2729 -----------------------
2731 procedure Check_Common_Type (T1, T2 : Entity_Id) is
2732 begin
2733 if Covers (T1 => T1, T2 => T2)
2734 or else
2735 Covers (T1 => T2, T2 => T1)
2736 then
2737 if T1 = Universal_Integer
2738 or else T1 = Universal_Real
2739 or else T1 = Any_Character
2740 then
2741 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
2743 elsif T1 = T2 then
2744 Add_One_Interp (N, T1, T1);
2746 else
2747 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
2748 end if;
2749 end if;
2750 end Check_Common_Type;
2752 ----------------------
2753 -- Check_High_Bound --
2754 ----------------------
2756 procedure Check_High_Bound (T : Entity_Id) is
2757 begin
2758 if not Is_Overloaded (H) then
2759 Check_Common_Type (T, Etype (H));
2760 else
2761 Get_First_Interp (H, I2, It2);
2762 while Present (It2.Typ) loop
2763 Check_Common_Type (T, It2.Typ);
2764 Get_Next_Interp (I2, It2);
2765 end loop;
2766 end if;
2767 end Check_High_Bound;
2769 -----------------------------
2770 -- Is_Universal_Expression --
2771 -----------------------------
2773 procedure Check_Universal_Expression (N : Node_Id) is
2774 begin
2775 if Etype (N) = Universal_Integer
2776 and then Nkind (N) /= N_Integer_Literal
2777 and then not Is_Entity_Name (N)
2778 and then Nkind (N) /= N_Attribute_Reference
2779 then
2780 Error_Msg_N ("illegal bound in discrete range", N);
2781 end if;
2782 end Check_Universal_Expression;
2784 -- Start of processing for Analyze_Range
2786 begin
2787 Set_Etype (N, Any_Type);
2788 Analyze_Expression (L);
2789 Analyze_Expression (H);
2791 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
2792 return;
2794 else
2795 if not Is_Overloaded (L) then
2796 Check_High_Bound (Etype (L));
2797 else
2798 Get_First_Interp (L, I1, It1);
2799 while Present (It1.Typ) loop
2800 Check_High_Bound (It1.Typ);
2801 Get_Next_Interp (I1, It1);
2802 end loop;
2803 end if;
2805 -- If result is Any_Type, then we did not find a compatible pair
2807 if Etype (N) = Any_Type then
2808 Error_Msg_N ("incompatible types in range ", N);
2809 end if;
2810 end if;
2812 if Ada_Version = Ada_83
2813 and then
2814 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
2815 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
2816 then
2817 Check_Universal_Expression (L);
2818 Check_Universal_Expression (H);
2819 end if;
2820 end Analyze_Range;
2822 -----------------------
2823 -- Analyze_Reference --
2824 -----------------------
2826 procedure Analyze_Reference (N : Node_Id) is
2827 P : constant Node_Id := Prefix (N);
2828 E : Entity_Id;
2829 T : Entity_Id;
2830 Acc_Type : Entity_Id;
2832 begin
2833 Analyze (P);
2835 -- An interesting error check, if we take the 'Reference of an object
2836 -- for which a pragma Atomic or Volatile has been given, and the type
2837 -- of the object is not Atomic or Volatile, then we are in trouble. The
2838 -- problem is that no trace of the atomic/volatile status will remain
2839 -- for the backend to respect when it deals with the resulting pointer,
2840 -- since the pointer type will not be marked atomic (it is a pointer to
2841 -- the base type of the object).
2843 -- It is not clear if that can ever occur, but in case it does, we will
2844 -- generate an error message. Not clear if this message can ever be
2845 -- generated, and pretty clear that it represents a bug if it is, still
2846 -- seems worth checking!
2848 T := Etype (P);
2850 if Is_Entity_Name (P)
2851 and then Is_Object_Reference (P)
2852 then
2853 E := Entity (P);
2854 T := Etype (P);
2856 if (Has_Atomic_Components (E)
2857 and then not Has_Atomic_Components (T))
2858 or else
2859 (Has_Volatile_Components (E)
2860 and then not Has_Volatile_Components (T))
2861 or else (Is_Atomic (E) and then not Is_Atomic (T))
2862 or else (Is_Volatile (E) and then not Is_Volatile (T))
2863 then
2864 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
2865 end if;
2866 end if;
2868 -- Carry on with normal processing
2870 Acc_Type := Create_Itype (E_Allocator_Type, N);
2871 Set_Etype (Acc_Type, Acc_Type);
2872 Set_Directly_Designated_Type (Acc_Type, Etype (P));
2873 Set_Etype (N, Acc_Type);
2874 end Analyze_Reference;
2876 --------------------------------
2877 -- Analyze_Selected_Component --
2878 --------------------------------
2880 -- Prefix is a record type or a task or protected type. In the
2881 -- later case, the selector must denote a visible entry.
2883 procedure Analyze_Selected_Component (N : Node_Id) is
2884 Name : constant Node_Id := Prefix (N);
2885 Sel : constant Node_Id := Selector_Name (N);
2886 Act_Decl : Node_Id;
2887 Comp : Entity_Id;
2888 Has_Candidate : Boolean := False;
2889 In_Scope : Boolean;
2890 Parent_N : Node_Id;
2891 Pent : Entity_Id := Empty;
2892 Prefix_Type : Entity_Id;
2894 Type_To_Use : Entity_Id;
2895 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
2896 -- a class-wide type, we use its root type, whose components are
2897 -- present in the class-wide type.
2899 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
2900 -- It is known that the parent of N denotes a subprogram call. Comp
2901 -- is an overloadable component of the concurrent type of the prefix.
2902 -- Determine whether all formals of the parent of N and Comp are mode
2903 -- conformant. If the parent node is not analyzed yet it may be an
2904 -- indexed component rather than a function call.
2906 ------------------------------
2907 -- Has_Mode_Conformant_Spec --
2908 ------------------------------
2910 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
2911 Comp_Param : Entity_Id;
2912 Param : Node_Id;
2913 Param_Typ : Entity_Id;
2915 begin
2916 Comp_Param := First_Formal (Comp);
2918 if Nkind (Parent (N)) = N_Indexed_Component then
2919 Param := First (Expressions (Parent (N)));
2920 else
2921 Param := First (Parameter_Associations (Parent (N)));
2922 end if;
2924 while Present (Comp_Param)
2925 and then Present (Param)
2926 loop
2927 Param_Typ := Find_Parameter_Type (Param);
2929 if Present (Param_Typ)
2930 and then
2931 not Conforming_Types
2932 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
2933 then
2934 return False;
2935 end if;
2937 Next_Formal (Comp_Param);
2938 Next (Param);
2939 end loop;
2941 -- One of the specs has additional formals
2943 if Present (Comp_Param) or else Present (Param) then
2944 return False;
2945 end if;
2947 return True;
2948 end Has_Mode_Conformant_Spec;
2950 -- Start of processing for Analyze_Selected_Component
2952 begin
2953 Set_Etype (N, Any_Type);
2955 if Is_Overloaded (Name) then
2956 Analyze_Overloaded_Selected_Component (N);
2957 return;
2959 elsif Etype (Name) = Any_Type then
2960 Set_Entity (Sel, Any_Id);
2961 Set_Etype (Sel, Any_Type);
2962 return;
2964 else
2965 Prefix_Type := Etype (Name);
2966 end if;
2968 if Is_Access_Type (Prefix_Type) then
2970 -- A RACW object can never be used as prefix of a selected
2971 -- component since that means it is dereferenced without
2972 -- being a controlling operand of a dispatching operation
2973 -- (RM E.2.2(16/1)). Before reporting an error, we must check
2974 -- whether this is actually a dispatching call in prefix form.
2976 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
2977 and then Comes_From_Source (N)
2978 then
2979 if Try_Object_Operation (N) then
2980 return;
2981 else
2982 Error_Msg_N
2983 ("invalid dereference of a remote access-to-class-wide value",
2985 end if;
2987 -- Normal case of selected component applied to access type
2989 else
2990 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2992 if Is_Entity_Name (Name) then
2993 Pent := Entity (Name);
2994 elsif Nkind (Name) = N_Selected_Component
2995 and then Is_Entity_Name (Selector_Name (Name))
2996 then
2997 Pent := Entity (Selector_Name (Name));
2998 end if;
3000 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3001 end if;
3003 -- If we have an explicit dereference of a remote access-to-class-wide
3004 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
3005 -- have to check for the case of a prefix that is a controlling operand
3006 -- of a prefixed dispatching call, as the dereference is legal in that
3007 -- case. Normally this condition is checked in Validate_Remote_Access_
3008 -- To_Class_Wide_Type, but we have to defer the checking for selected
3009 -- component prefixes because of the prefixed dispatching call case.
3010 -- Note that implicit dereferences are checked for this just above.
3012 elsif Nkind (Name) = N_Explicit_Dereference
3013 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3014 and then Comes_From_Source (N)
3015 then
3016 if Try_Object_Operation (N) then
3017 return;
3018 else
3019 Error_Msg_N
3020 ("invalid dereference of a remote access-to-class-wide value",
3022 end if;
3023 end if;
3025 -- (Ada 2005): if the prefix is the limited view of a type, and
3026 -- the context already includes the full view, use the full view
3027 -- in what follows, either to retrieve a component of to find
3028 -- a primitive operation. If the prefix is an explicit dereference,
3029 -- set the type of the prefix to reflect this transformation.
3030 -- If the non-limited view is itself an incomplete type, get the
3031 -- full view if available.
3033 if Is_Incomplete_Type (Prefix_Type)
3034 and then From_With_Type (Prefix_Type)
3035 and then Present (Non_Limited_View (Prefix_Type))
3036 then
3037 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3039 if Nkind (N) = N_Explicit_Dereference then
3040 Set_Etype (Prefix (N), Prefix_Type);
3041 end if;
3043 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3044 and then From_With_Type (Prefix_Type)
3045 and then Present (Non_Limited_View (Etype (Prefix_Type)))
3046 then
3047 Prefix_Type :=
3048 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3050 if Nkind (N) = N_Explicit_Dereference then
3051 Set_Etype (Prefix (N), Prefix_Type);
3052 end if;
3053 end if;
3055 if Ekind (Prefix_Type) = E_Private_Subtype then
3056 Prefix_Type := Base_Type (Prefix_Type);
3057 end if;
3059 Type_To_Use := Prefix_Type;
3061 -- For class-wide types, use the entity list of the root type. This
3062 -- indirection is specially important for private extensions because
3063 -- only the root type get switched (not the class-wide type).
3065 if Is_Class_Wide_Type (Prefix_Type) then
3066 Type_To_Use := Root_Type (Prefix_Type);
3067 end if;
3069 Comp := First_Entity (Type_To_Use);
3071 -- If the selector has an original discriminant, the node appears in
3072 -- an instance. Replace the discriminant with the corresponding one
3073 -- in the current discriminated type. For nested generics, this must
3074 -- be done transitively, so note the new original discriminant.
3076 if Nkind (Sel) = N_Identifier
3077 and then Present (Original_Discriminant (Sel))
3078 then
3079 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3081 -- Mark entity before rewriting, for completeness and because
3082 -- subsequent semantic checks might examine the original node.
3084 Set_Entity (Sel, Comp);
3085 Rewrite (Selector_Name (N),
3086 New_Occurrence_Of (Comp, Sloc (N)));
3087 Set_Original_Discriminant (Selector_Name (N), Comp);
3088 Set_Etype (N, Etype (Comp));
3090 if Is_Access_Type (Etype (Name)) then
3091 Insert_Explicit_Dereference (Name);
3092 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3093 end if;
3095 elsif Is_Record_Type (Prefix_Type) then
3097 -- Find component with given name
3099 while Present (Comp) loop
3100 if Chars (Comp) = Chars (Sel)
3101 and then Is_Visible_Component (Comp)
3102 then
3103 Set_Entity_With_Style_Check (Sel, Comp);
3104 Set_Etype (Sel, Etype (Comp));
3106 if Ekind (Comp) = E_Discriminant then
3107 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3108 Error_Msg_N
3109 ("cannot reference discriminant of Unchecked_Union",
3110 Sel);
3111 end if;
3113 if Is_Generic_Type (Prefix_Type)
3114 or else
3115 Is_Generic_Type (Root_Type (Prefix_Type))
3116 then
3117 Set_Original_Discriminant (Sel, Comp);
3118 end if;
3119 end if;
3121 -- Resolve the prefix early otherwise it is not possible to
3122 -- build the actual subtype of the component: it may need
3123 -- to duplicate this prefix and duplication is only allowed
3124 -- on fully resolved expressions.
3126 Resolve (Name);
3128 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
3129 -- subtypes in a package specification.
3130 -- Example:
3132 -- limited with Pkg;
3133 -- package Pkg is
3134 -- type Acc_Inc is access Pkg.T;
3135 -- X : Acc_Inc;
3136 -- N : Natural := X.all.Comp; -- ERROR, limited view
3137 -- end Pkg; -- Comp is not visible
3139 if Nkind (Name) = N_Explicit_Dereference
3140 and then From_With_Type (Etype (Prefix (Name)))
3141 and then not Is_Potentially_Use_Visible (Etype (Name))
3142 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3143 N_Package_Specification
3144 then
3145 Error_Msg_NE
3146 ("premature usage of incomplete}", Prefix (Name),
3147 Etype (Prefix (Name)));
3148 end if;
3150 -- We never need an actual subtype for the case of a selection
3151 -- for a indexed component of a non-packed array, since in
3152 -- this case gigi generates all the checks and can find the
3153 -- necessary bounds information.
3155 -- We also do not need an actual subtype for the case of
3156 -- a first, last, length, or range attribute applied to a
3157 -- non-packed array, since gigi can again get the bounds in
3158 -- these cases (gigi cannot handle the packed case, since it
3159 -- has the bounds of the packed array type, not the original
3160 -- bounds of the type). However, if the prefix is itself a
3161 -- selected component, as in a.b.c (i), gigi may regard a.b.c
3162 -- as a dynamic-sized temporary, so we do generate an actual
3163 -- subtype for this case.
3165 Parent_N := Parent (N);
3167 if not Is_Packed (Etype (Comp))
3168 and then
3169 ((Nkind (Parent_N) = N_Indexed_Component
3170 and then Nkind (Name) /= N_Selected_Component)
3171 or else
3172 (Nkind (Parent_N) = N_Attribute_Reference
3173 and then (Attribute_Name (Parent_N) = Name_First
3174 or else
3175 Attribute_Name (Parent_N) = Name_Last
3176 or else
3177 Attribute_Name (Parent_N) = Name_Length
3178 or else
3179 Attribute_Name (Parent_N) = Name_Range)))
3180 then
3181 Set_Etype (N, Etype (Comp));
3183 -- If full analysis is not enabled, we do not generate an
3184 -- actual subtype, because in the absence of expansion
3185 -- reference to a formal of a protected type, for example,
3186 -- will not be properly transformed, and will lead to
3187 -- out-of-scope references in gigi.
3189 -- In all other cases, we currently build an actual subtype.
3190 -- It seems likely that many of these cases can be avoided,
3191 -- but right now, the front end makes direct references to the
3192 -- bounds (e.g. in generating a length check), and if we do
3193 -- not make an actual subtype, we end up getting a direct
3194 -- reference to a discriminant, which will not do.
3196 elsif Full_Analysis then
3197 Act_Decl :=
3198 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3199 Insert_Action (N, Act_Decl);
3201 if No (Act_Decl) then
3202 Set_Etype (N, Etype (Comp));
3204 else
3205 -- Component type depends on discriminants. Enter the
3206 -- main attributes of the subtype.
3208 declare
3209 Subt : constant Entity_Id :=
3210 Defining_Identifier (Act_Decl);
3212 begin
3213 Set_Etype (Subt, Base_Type (Etype (Comp)));
3214 Set_Ekind (Subt, Ekind (Etype (Comp)));
3215 Set_Etype (N, Subt);
3216 end;
3217 end if;
3219 -- If Full_Analysis not enabled, just set the Etype
3221 else
3222 Set_Etype (N, Etype (Comp));
3223 end if;
3225 return;
3226 end if;
3228 -- If the prefix is a private extension, check only the visible
3229 -- components of the partial view. This must include the tag,
3230 -- which can appear in expanded code in a tag check.
3232 if Ekind (Type_To_Use) = E_Record_Type_With_Private
3233 and then Chars (Selector_Name (N)) /= Name_uTag
3234 then
3235 exit when Comp = Last_Entity (Type_To_Use);
3236 end if;
3238 Next_Entity (Comp);
3239 end loop;
3241 -- Ada 2005 (AI-252): The selected component can be interpreted as
3242 -- a prefixed view of a subprogram. Depending on the context, this is
3243 -- either a name that can appear in a renaming declaration, or part
3244 -- of an enclosing call given in prefix form.
3246 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
3247 -- selected component should resolve to a name.
3249 if Ada_Version >= Ada_05
3250 and then Is_Tagged_Type (Prefix_Type)
3251 and then not Is_Concurrent_Type (Prefix_Type)
3252 then
3253 if Nkind (Parent (N)) = N_Generic_Association
3254 or else Nkind (Parent (N)) = N_Requeue_Statement
3255 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
3256 then
3257 if Find_Primitive_Operation (N) then
3258 return;
3259 end if;
3261 elsif Try_Object_Operation (N) then
3262 return;
3263 end if;
3265 -- If the transformation fails, it will be necessary to redo the
3266 -- analysis with all errors enabled, to indicate candidate
3267 -- interpretations and reasons for each failure ???
3269 end if;
3271 elsif Is_Private_Type (Prefix_Type) then
3273 -- Allow access only to discriminants of the type. If the type has
3274 -- no full view, gigi uses the parent type for the components, so we
3275 -- do the same here.
3277 if No (Full_View (Prefix_Type)) then
3278 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
3279 Comp := First_Entity (Type_To_Use);
3280 end if;
3282 while Present (Comp) loop
3283 if Chars (Comp) = Chars (Sel) then
3284 if Ekind (Comp) = E_Discriminant then
3285 Set_Entity_With_Style_Check (Sel, Comp);
3286 Generate_Reference (Comp, Sel);
3288 Set_Etype (Sel, Etype (Comp));
3289 Set_Etype (N, Etype (Comp));
3291 if Is_Generic_Type (Prefix_Type)
3292 or else Is_Generic_Type (Root_Type (Prefix_Type))
3293 then
3294 Set_Original_Discriminant (Sel, Comp);
3295 end if;
3297 -- Before declaring an error, check whether this is tagged
3298 -- private type and a call to a primitive operation.
3300 elsif Ada_Version >= Ada_05
3301 and then Is_Tagged_Type (Prefix_Type)
3302 and then Try_Object_Operation (N)
3303 then
3304 return;
3306 else
3307 Error_Msg_NE
3308 ("invisible selector for }",
3309 N, First_Subtype (Prefix_Type));
3310 Set_Entity (Sel, Any_Id);
3311 Set_Etype (N, Any_Type);
3312 end if;
3314 return;
3315 end if;
3317 Next_Entity (Comp);
3318 end loop;
3320 elsif Is_Concurrent_Type (Prefix_Type) then
3322 -- Find visible operation with given name. For a protected type,
3323 -- the possible candidates are discriminants, entries or protected
3324 -- procedures. For a task type, the set can only include entries or
3325 -- discriminants if the task type is not an enclosing scope. If it
3326 -- is an enclosing scope (e.g. in an inner task) then all entities
3327 -- are visible, but the prefix must denote the enclosing scope, i.e.
3328 -- can only be a direct name or an expanded name.
3330 Set_Etype (Sel, Any_Type);
3331 In_Scope := In_Open_Scopes (Prefix_Type);
3333 while Present (Comp) loop
3334 if Chars (Comp) = Chars (Sel) then
3335 if Is_Overloadable (Comp) then
3336 Add_One_Interp (Sel, Comp, Etype (Comp));
3338 -- If the prefix is tagged, the correct interpretation may
3339 -- lie in the primitive or class-wide operations of the
3340 -- type. Perform a simple conformance check to determine
3341 -- whether Try_Object_Operation should be invoked even if
3342 -- a visible entity is found.
3344 if Is_Tagged_Type (Prefix_Type)
3345 and then
3346 Nkind_In (Parent (N), N_Procedure_Call_Statement,
3347 N_Function_Call,
3348 N_Indexed_Component)
3349 and then Has_Mode_Conformant_Spec (Comp)
3350 then
3351 Has_Candidate := True;
3352 end if;
3354 elsif Ekind (Comp) = E_Discriminant
3355 or else Ekind (Comp) = E_Entry_Family
3356 or else (In_Scope
3357 and then Is_Entity_Name (Name))
3358 then
3359 Set_Entity_With_Style_Check (Sel, Comp);
3360 Generate_Reference (Comp, Sel);
3362 else
3363 goto Next_Comp;
3364 end if;
3366 Set_Etype (Sel, Etype (Comp));
3367 Set_Etype (N, Etype (Comp));
3369 if Ekind (Comp) = E_Discriminant then
3370 Set_Original_Discriminant (Sel, Comp);
3371 end if;
3373 -- For access type case, introduce explicit deference for more
3374 -- uniform treatment of entry calls.
3376 if Is_Access_Type (Etype (Name)) then
3377 Insert_Explicit_Dereference (Name);
3378 Error_Msg_NW
3379 (Warn_On_Dereference, "?implicit dereference", N);
3380 end if;
3381 end if;
3383 <<Next_Comp>>
3384 Next_Entity (Comp);
3385 exit when not In_Scope
3386 and then
3387 Comp = First_Private_Entity (Base_Type (Prefix_Type));
3388 end loop;
3390 -- If there is no visible entity with the given name or none of the
3391 -- visible entities are plausible interpretations, check whether
3392 -- there is some other primitive operation with that name.
3394 if Ada_Version >= Ada_05
3395 and then Is_Tagged_Type (Prefix_Type)
3396 then
3397 if (Etype (N) = Any_Type
3398 or else not Has_Candidate)
3399 and then Try_Object_Operation (N)
3400 then
3401 return;
3403 -- If the context is not syntactically a procedure call, it
3404 -- may be a call to a primitive function declared outside of
3405 -- the synchronized type.
3407 -- If the context is a procedure call, there might still be
3408 -- an overloading between an entry and a primitive procedure
3409 -- declared outside of the synchronized type, called in prefix
3410 -- notation. This is harder to disambiguate because in one case
3411 -- the controlling formal is implicit ???
3413 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
3414 and then Nkind (Parent (N)) /= N_Indexed_Component
3415 and then Try_Object_Operation (N)
3416 then
3417 return;
3418 end if;
3419 end if;
3421 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3423 else
3424 -- Invalid prefix
3426 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
3427 end if;
3429 -- If N still has no type, the component is not defined in the prefix
3431 if Etype (N) = Any_Type then
3433 -- If the prefix is a single concurrent object, use its name in the
3434 -- error message, rather than that of its anonymous type.
3436 if Is_Concurrent_Type (Prefix_Type)
3437 and then Is_Internal_Name (Chars (Prefix_Type))
3438 and then not Is_Derived_Type (Prefix_Type)
3439 and then Is_Entity_Name (Name)
3440 then
3442 Error_Msg_Node_2 := Entity (Name);
3443 Error_Msg_NE ("no selector& for&", N, Sel);
3445 Check_Misspelled_Selector (Type_To_Use, Sel);
3447 elsif Is_Generic_Type (Prefix_Type)
3448 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
3449 and then Prefix_Type /= Etype (Prefix_Type)
3450 and then Is_Record_Type (Etype (Prefix_Type))
3451 then
3452 -- If this is a derived formal type, the parent may have
3453 -- different visibility at this point. Try for an inherited
3454 -- component before reporting an error.
3456 Set_Etype (Prefix (N), Etype (Prefix_Type));
3457 Analyze_Selected_Component (N);
3458 return;
3460 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
3461 and then Is_Generic_Actual_Type (Prefix_Type)
3462 and then Present (Full_View (Prefix_Type))
3463 then
3464 -- Similarly, if this the actual for a formal derived type, the
3465 -- component inherited from the generic parent may not be visible
3466 -- in the actual, but the selected component is legal.
3468 declare
3469 Comp : Entity_Id;
3471 begin
3472 Comp :=
3473 First_Component (Generic_Parent_Type (Parent (Prefix_Type)));
3474 while Present (Comp) loop
3475 if Chars (Comp) = Chars (Sel) then
3476 Set_Entity_With_Style_Check (Sel, Comp);
3477 Set_Etype (Sel, Etype (Comp));
3478 Set_Etype (N, Etype (Comp));
3479 return;
3480 end if;
3482 Next_Component (Comp);
3483 end loop;
3485 pragma Assert (Etype (N) /= Any_Type);
3486 end;
3488 else
3489 if Ekind (Prefix_Type) = E_Record_Subtype then
3491 -- Check whether this is a component of the base type
3492 -- which is absent from a statically constrained subtype.
3493 -- This will raise constraint error at run-time, but is
3494 -- not a compile-time error. When the selector is illegal
3495 -- for base type as well fall through and generate a
3496 -- compilation error anyway.
3498 Comp := First_Component (Base_Type (Prefix_Type));
3499 while Present (Comp) loop
3500 if Chars (Comp) = Chars (Sel)
3501 and then Is_Visible_Component (Comp)
3502 then
3503 Set_Entity_With_Style_Check (Sel, Comp);
3504 Generate_Reference (Comp, Sel);
3505 Set_Etype (Sel, Etype (Comp));
3506 Set_Etype (N, Etype (Comp));
3508 -- Emit appropriate message. Gigi will replace the
3509 -- node subsequently with the appropriate Raise.
3511 Apply_Compile_Time_Constraint_Error
3512 (N, "component not present in }?",
3513 CE_Discriminant_Check_Failed,
3514 Ent => Prefix_Type, Rep => False);
3515 Set_Raises_Constraint_Error (N);
3516 return;
3517 end if;
3519 Next_Component (Comp);
3520 end loop;
3522 end if;
3524 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
3525 Error_Msg_NE ("no selector& for}", N, Sel);
3527 Check_Misspelled_Selector (Type_To_Use, Sel);
3528 end if;
3530 Set_Entity (Sel, Any_Id);
3531 Set_Etype (Sel, Any_Type);
3532 end if;
3533 end Analyze_Selected_Component;
3535 ---------------------------
3536 -- Analyze_Short_Circuit --
3537 ---------------------------
3539 procedure Analyze_Short_Circuit (N : Node_Id) is
3540 L : constant Node_Id := Left_Opnd (N);
3541 R : constant Node_Id := Right_Opnd (N);
3542 Ind : Interp_Index;
3543 It : Interp;
3545 begin
3546 Analyze_Expression (L);
3547 Analyze_Expression (R);
3548 Set_Etype (N, Any_Type);
3550 if not Is_Overloaded (L) then
3551 if Root_Type (Etype (L)) = Standard_Boolean
3552 and then Has_Compatible_Type (R, Etype (L))
3553 then
3554 Add_One_Interp (N, Etype (L), Etype (L));
3555 end if;
3557 else
3558 Get_First_Interp (L, Ind, It);
3559 while Present (It.Typ) loop
3560 if Root_Type (It.Typ) = Standard_Boolean
3561 and then Has_Compatible_Type (R, It.Typ)
3562 then
3563 Add_One_Interp (N, It.Typ, It.Typ);
3564 end if;
3566 Get_Next_Interp (Ind, It);
3567 end loop;
3568 end if;
3570 -- Here we have failed to find an interpretation. Clearly we know that
3571 -- it is not the case that both operands can have an interpretation of
3572 -- Boolean, but this is by far the most likely intended interpretation.
3573 -- So we simply resolve both operands as Booleans, and at least one of
3574 -- these resolutions will generate an error message, and we do not need
3575 -- to give another error message on the short circuit operation itself.
3577 if Etype (N) = Any_Type then
3578 Resolve (L, Standard_Boolean);
3579 Resolve (R, Standard_Boolean);
3580 Set_Etype (N, Standard_Boolean);
3581 end if;
3582 end Analyze_Short_Circuit;
3584 -------------------
3585 -- Analyze_Slice --
3586 -------------------
3588 procedure Analyze_Slice (N : Node_Id) is
3589 P : constant Node_Id := Prefix (N);
3590 D : constant Node_Id := Discrete_Range (N);
3591 Array_Type : Entity_Id;
3593 procedure Analyze_Overloaded_Slice;
3594 -- If the prefix is overloaded, select those interpretations that
3595 -- yield a one-dimensional array type.
3597 ------------------------------
3598 -- Analyze_Overloaded_Slice --
3599 ------------------------------
3601 procedure Analyze_Overloaded_Slice is
3602 I : Interp_Index;
3603 It : Interp;
3604 Typ : Entity_Id;
3606 begin
3607 Set_Etype (N, Any_Type);
3609 Get_First_Interp (P, I, It);
3610 while Present (It.Nam) loop
3611 Typ := It.Typ;
3613 if Is_Access_Type (Typ) then
3614 Typ := Designated_Type (Typ);
3615 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3616 end if;
3618 if Is_Array_Type (Typ)
3619 and then Number_Dimensions (Typ) = 1
3620 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
3621 then
3622 Add_One_Interp (N, Typ, Typ);
3623 end if;
3625 Get_Next_Interp (I, It);
3626 end loop;
3628 if Etype (N) = Any_Type then
3629 Error_Msg_N ("expect array type in prefix of slice", N);
3630 end if;
3631 end Analyze_Overloaded_Slice;
3633 -- Start of processing for Analyze_Slice
3635 begin
3636 Analyze (P);
3637 Analyze (D);
3639 if Is_Overloaded (P) then
3640 Analyze_Overloaded_Slice;
3642 else
3643 Array_Type := Etype (P);
3644 Set_Etype (N, Any_Type);
3646 if Is_Access_Type (Array_Type) then
3647 Array_Type := Designated_Type (Array_Type);
3648 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3649 end if;
3651 if not Is_Array_Type (Array_Type) then
3652 Wrong_Type (P, Any_Array);
3654 elsif Number_Dimensions (Array_Type) > 1 then
3655 Error_Msg_N
3656 ("type is not one-dimensional array in slice prefix", N);
3658 elsif not
3659 Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
3660 then
3661 Wrong_Type (D, Etype (First_Index (Array_Type)));
3663 else
3664 Set_Etype (N, Array_Type);
3665 end if;
3666 end if;
3667 end Analyze_Slice;
3669 -----------------------------
3670 -- Analyze_Type_Conversion --
3671 -----------------------------
3673 procedure Analyze_Type_Conversion (N : Node_Id) is
3674 Expr : constant Node_Id := Expression (N);
3675 T : Entity_Id;
3677 begin
3678 -- If Conversion_OK is set, then the Etype is already set, and the
3679 -- only processing required is to analyze the expression. This is
3680 -- used to construct certain "illegal" conversions which are not
3681 -- allowed by Ada semantics, but can be handled OK by Gigi, see
3682 -- Sinfo for further details.
3684 if Conversion_OK (N) then
3685 Analyze (Expr);
3686 return;
3687 end if;
3689 -- Otherwise full type analysis is required, as well as some semantic
3690 -- checks to make sure the argument of the conversion is appropriate.
3692 Find_Type (Subtype_Mark (N));
3693 T := Entity (Subtype_Mark (N));
3694 Set_Etype (N, T);
3695 Check_Fully_Declared (T, N);
3696 Analyze_Expression (Expr);
3697 Validate_Remote_Type_Type_Conversion (N);
3699 -- Only remaining step is validity checks on the argument. These
3700 -- are skipped if the conversion does not come from the source.
3702 if not Comes_From_Source (N) then
3703 return;
3705 -- If there was an error in a generic unit, no need to replicate the
3706 -- error message. Conversely, constant-folding in the generic may
3707 -- transform the argument of a conversion into a string literal, which
3708 -- is legal. Therefore the following tests are not performed in an
3709 -- instance.
3711 elsif In_Instance then
3712 return;
3714 elsif Nkind (Expr) = N_Null then
3715 Error_Msg_N ("argument of conversion cannot be null", N);
3716 Error_Msg_N ("\use qualified expression instead", N);
3717 Set_Etype (N, Any_Type);
3719 elsif Nkind (Expr) = N_Aggregate then
3720 Error_Msg_N ("argument of conversion cannot be aggregate", N);
3721 Error_Msg_N ("\use qualified expression instead", N);
3723 elsif Nkind (Expr) = N_Allocator then
3724 Error_Msg_N ("argument of conversion cannot be an allocator", N);
3725 Error_Msg_N ("\use qualified expression instead", N);
3727 elsif Nkind (Expr) = N_String_Literal then
3728 Error_Msg_N ("argument of conversion cannot be string literal", N);
3729 Error_Msg_N ("\use qualified expression instead", N);
3731 elsif Nkind (Expr) = N_Character_Literal then
3732 if Ada_Version = Ada_83 then
3733 Resolve (Expr, T);
3734 else
3735 Error_Msg_N ("argument of conversion cannot be character literal",
3737 Error_Msg_N ("\use qualified expression instead", N);
3738 end if;
3740 elsif Nkind (Expr) = N_Attribute_Reference
3741 and then
3742 (Attribute_Name (Expr) = Name_Access or else
3743 Attribute_Name (Expr) = Name_Unchecked_Access or else
3744 Attribute_Name (Expr) = Name_Unrestricted_Access)
3745 then
3746 Error_Msg_N ("argument of conversion cannot be access", N);
3747 Error_Msg_N ("\use qualified expression instead", N);
3748 end if;
3749 end Analyze_Type_Conversion;
3751 ----------------------
3752 -- Analyze_Unary_Op --
3753 ----------------------
3755 procedure Analyze_Unary_Op (N : Node_Id) is
3756 R : constant Node_Id := Right_Opnd (N);
3757 Op_Id : Entity_Id := Entity (N);
3759 begin
3760 Set_Etype (N, Any_Type);
3761 Candidate_Type := Empty;
3763 Analyze_Expression (R);
3765 if Present (Op_Id) then
3766 if Ekind (Op_Id) = E_Operator then
3767 Find_Unary_Types (R, Op_Id, N);
3768 else
3769 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3770 end if;
3772 else
3773 Op_Id := Get_Name_Entity_Id (Chars (N));
3774 while Present (Op_Id) loop
3775 if Ekind (Op_Id) = E_Operator then
3776 if No (Next_Entity (First_Entity (Op_Id))) then
3777 Find_Unary_Types (R, Op_Id, N);
3778 end if;
3780 elsif Is_Overloadable (Op_Id) then
3781 Analyze_User_Defined_Unary_Op (N, Op_Id);
3782 end if;
3784 Op_Id := Homonym (Op_Id);
3785 end loop;
3786 end if;
3788 Operator_Check (N);
3789 end Analyze_Unary_Op;
3791 ----------------------------------
3792 -- Analyze_Unchecked_Expression --
3793 ----------------------------------
3795 procedure Analyze_Unchecked_Expression (N : Node_Id) is
3796 begin
3797 Analyze (Expression (N), Suppress => All_Checks);
3798 Set_Etype (N, Etype (Expression (N)));
3799 Save_Interps (Expression (N), N);
3800 end Analyze_Unchecked_Expression;
3802 ---------------------------------------
3803 -- Analyze_Unchecked_Type_Conversion --
3804 ---------------------------------------
3806 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
3807 begin
3808 Find_Type (Subtype_Mark (N));
3809 Analyze_Expression (Expression (N));
3810 Set_Etype (N, Entity (Subtype_Mark (N)));
3811 end Analyze_Unchecked_Type_Conversion;
3813 ------------------------------------
3814 -- Analyze_User_Defined_Binary_Op --
3815 ------------------------------------
3817 procedure Analyze_User_Defined_Binary_Op
3818 (N : Node_Id;
3819 Op_Id : Entity_Id)
3821 begin
3822 -- Only do analysis if the operator Comes_From_Source, since otherwise
3823 -- the operator was generated by the expander, and all such operators
3824 -- always refer to the operators in package Standard.
3826 if Comes_From_Source (N) then
3827 declare
3828 F1 : constant Entity_Id := First_Formal (Op_Id);
3829 F2 : constant Entity_Id := Next_Formal (F1);
3831 begin
3832 -- Verify that Op_Id is a visible binary function. Note that since
3833 -- we know Op_Id is overloaded, potentially use visible means use
3834 -- visible for sure (RM 9.4(11)).
3836 if Ekind (Op_Id) = E_Function
3837 and then Present (F2)
3838 and then (Is_Immediately_Visible (Op_Id)
3839 or else Is_Potentially_Use_Visible (Op_Id))
3840 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
3841 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
3842 then
3843 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3845 if Debug_Flag_E then
3846 Write_Str ("user defined operator ");
3847 Write_Name (Chars (Op_Id));
3848 Write_Str (" on node ");
3849 Write_Int (Int (N));
3850 Write_Eol;
3851 end if;
3852 end if;
3853 end;
3854 end if;
3855 end Analyze_User_Defined_Binary_Op;
3857 -----------------------------------
3858 -- Analyze_User_Defined_Unary_Op --
3859 -----------------------------------
3861 procedure Analyze_User_Defined_Unary_Op
3862 (N : Node_Id;
3863 Op_Id : Entity_Id)
3865 begin
3866 -- Only do analysis if the operator Comes_From_Source, since otherwise
3867 -- the operator was generated by the expander, and all such operators
3868 -- always refer to the operators in package Standard.
3870 if Comes_From_Source (N) then
3871 declare
3872 F : constant Entity_Id := First_Formal (Op_Id);
3874 begin
3875 -- Verify that Op_Id is a visible unary function. Note that since
3876 -- we know Op_Id is overloaded, potentially use visible means use
3877 -- visible for sure (RM 9.4(11)).
3879 if Ekind (Op_Id) = E_Function
3880 and then No (Next_Formal (F))
3881 and then (Is_Immediately_Visible (Op_Id)
3882 or else Is_Potentially_Use_Visible (Op_Id))
3883 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
3884 then
3885 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3886 end if;
3887 end;
3888 end if;
3889 end Analyze_User_Defined_Unary_Op;
3891 ---------------------------
3892 -- Check_Arithmetic_Pair --
3893 ---------------------------
3895 procedure Check_Arithmetic_Pair
3896 (T1, T2 : Entity_Id;
3897 Op_Id : Entity_Id;
3898 N : Node_Id)
3900 Op_Name : constant Name_Id := Chars (Op_Id);
3902 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
3903 -- Check whether the fixed-point type Typ has a user-defined operator
3904 -- (multiplication or division) that should hide the corresponding
3905 -- predefined operator. Used to implement Ada 2005 AI-264, to make
3906 -- such operators more visible and therefore useful.
3908 -- If the name of the operation is an expanded name with prefix
3909 -- Standard, the predefined universal fixed operator is available,
3910 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
3912 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
3913 -- Get specific type (i.e. non-universal type if there is one)
3915 ------------------
3916 -- Has_Fixed_Op --
3917 ------------------
3919 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
3920 Bas : constant Entity_Id := Base_Type (Typ);
3921 Ent : Entity_Id;
3922 F1 : Entity_Id;
3923 F2 : Entity_Id;
3925 begin
3926 -- If the universal_fixed operation is given explicitly the rule
3927 -- concerning primitive operations of the type do not apply.
3929 if Nkind (N) = N_Function_Call
3930 and then Nkind (Name (N)) = N_Expanded_Name
3931 and then Entity (Prefix (Name (N))) = Standard_Standard
3932 then
3933 return False;
3934 end if;
3936 -- The operation is treated as primitive if it is declared in the
3937 -- same scope as the type, and therefore on the same entity chain.
3939 Ent := Next_Entity (Typ);
3940 while Present (Ent) loop
3941 if Chars (Ent) = Chars (Op) then
3942 F1 := First_Formal (Ent);
3943 F2 := Next_Formal (F1);
3945 -- The operation counts as primitive if either operand or
3946 -- result are of the given base type, and both operands are
3947 -- fixed point types.
3949 if (Base_Type (Etype (F1)) = Bas
3950 and then Is_Fixed_Point_Type (Etype (F2)))
3952 or else
3953 (Base_Type (Etype (F2)) = Bas
3954 and then Is_Fixed_Point_Type (Etype (F1)))
3956 or else
3957 (Base_Type (Etype (Ent)) = Bas
3958 and then Is_Fixed_Point_Type (Etype (F1))
3959 and then Is_Fixed_Point_Type (Etype (F2)))
3960 then
3961 return True;
3962 end if;
3963 end if;
3965 Next_Entity (Ent);
3966 end loop;
3968 return False;
3969 end Has_Fixed_Op;
3971 -------------------
3972 -- Specific_Type --
3973 -------------------
3975 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
3976 begin
3977 if T1 = Universal_Integer or else T1 = Universal_Real then
3978 return Base_Type (T2);
3979 else
3980 return Base_Type (T1);
3981 end if;
3982 end Specific_Type;
3984 -- Start of processing for Check_Arithmetic_Pair
3986 begin
3987 if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
3989 if Is_Numeric_Type (T1)
3990 and then Is_Numeric_Type (T2)
3991 and then (Covers (T1 => T1, T2 => T2)
3992 or else
3993 Covers (T1 => T2, T2 => T1))
3994 then
3995 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3996 end if;
3998 elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4000 if Is_Fixed_Point_Type (T1)
4001 and then (Is_Fixed_Point_Type (T2)
4002 or else T2 = Universal_Real)
4003 then
4004 -- If Treat_Fixed_As_Integer is set then the Etype is already set
4005 -- and no further processing is required (this is the case of an
4006 -- operator constructed by Exp_Fixd for a fixed point operation)
4007 -- Otherwise add one interpretation with universal fixed result
4008 -- If the operator is given in functional notation, it comes
4009 -- from source and Fixed_As_Integer cannot apply.
4011 if (Nkind (N) not in N_Op
4012 or else not Treat_Fixed_As_Integer (N))
4013 and then
4014 (not Has_Fixed_Op (T1, Op_Id)
4015 or else Nkind (Parent (N)) = N_Type_Conversion)
4016 then
4017 Add_One_Interp (N, Op_Id, Universal_Fixed);
4018 end if;
4020 elsif Is_Fixed_Point_Type (T2)
4021 and then (Nkind (N) not in N_Op
4022 or else not Treat_Fixed_As_Integer (N))
4023 and then T1 = Universal_Real
4024 and then
4025 (not Has_Fixed_Op (T1, Op_Id)
4026 or else Nkind (Parent (N)) = N_Type_Conversion)
4027 then
4028 Add_One_Interp (N, Op_Id, Universal_Fixed);
4030 elsif Is_Numeric_Type (T1)
4031 and then Is_Numeric_Type (T2)
4032 and then (Covers (T1 => T1, T2 => T2)
4033 or else
4034 Covers (T1 => T2, T2 => T1))
4035 then
4036 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4038 elsif Is_Fixed_Point_Type (T1)
4039 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4040 or else T2 = Universal_Integer)
4041 then
4042 Add_One_Interp (N, Op_Id, T1);
4044 elsif T2 = Universal_Real
4045 and then Base_Type (T1) = Base_Type (Standard_Integer)
4046 and then Op_Name = Name_Op_Multiply
4047 then
4048 Add_One_Interp (N, Op_Id, Any_Fixed);
4050 elsif T1 = Universal_Real
4051 and then Base_Type (T2) = Base_Type (Standard_Integer)
4052 then
4053 Add_One_Interp (N, Op_Id, Any_Fixed);
4055 elsif Is_Fixed_Point_Type (T2)
4056 and then (Base_Type (T1) = Base_Type (Standard_Integer)
4057 or else T1 = Universal_Integer)
4058 and then Op_Name = Name_Op_Multiply
4059 then
4060 Add_One_Interp (N, Op_Id, T2);
4062 elsif T1 = Universal_Real and then T2 = Universal_Integer then
4063 Add_One_Interp (N, Op_Id, T1);
4065 elsif T2 = Universal_Real
4066 and then T1 = Universal_Integer
4067 and then Op_Name = Name_Op_Multiply
4068 then
4069 Add_One_Interp (N, Op_Id, T2);
4070 end if;
4072 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4074 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
4075 -- set does not require any special processing, since the Etype is
4076 -- already set (case of operation constructed by Exp_Fixed).
4078 if Is_Integer_Type (T1)
4079 and then (Covers (T1 => T1, T2 => T2)
4080 or else
4081 Covers (T1 => T2, T2 => T1))
4082 then
4083 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4084 end if;
4086 elsif Op_Name = Name_Op_Expon then
4087 if Is_Numeric_Type (T1)
4088 and then not Is_Fixed_Point_Type (T1)
4089 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4090 or else T2 = Universal_Integer)
4091 then
4092 Add_One_Interp (N, Op_Id, Base_Type (T1));
4093 end if;
4095 else pragma Assert (Nkind (N) in N_Op_Shift);
4097 -- If not one of the predefined operators, the node may be one
4098 -- of the intrinsic functions. Its kind is always specific, and
4099 -- we can use it directly, rather than the name of the operation.
4101 if Is_Integer_Type (T1)
4102 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4103 or else T2 = Universal_Integer)
4104 then
4105 Add_One_Interp (N, Op_Id, Base_Type (T1));
4106 end if;
4107 end if;
4108 end Check_Arithmetic_Pair;
4110 -------------------------------
4111 -- Check_Misspelled_Selector --
4112 -------------------------------
4114 procedure Check_Misspelled_Selector
4115 (Prefix : Entity_Id;
4116 Sel : Node_Id)
4118 Max_Suggestions : constant := 2;
4119 Nr_Of_Suggestions : Natural := 0;
4121 Suggestion_1 : Entity_Id := Empty;
4122 Suggestion_2 : Entity_Id := Empty;
4124 Comp : Entity_Id;
4126 begin
4127 -- All the components of the prefix of selector Sel are matched
4128 -- against Sel and a count is maintained of possible misspellings.
4129 -- When at the end of the analysis there are one or two (not more!)
4130 -- possible misspellings, these misspellings will be suggested as
4131 -- possible correction.
4133 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
4135 -- Concurrent types should be handled as well ???
4137 return;
4138 end if;
4140 Comp := First_Entity (Prefix);
4141 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
4142 if Is_Visible_Component (Comp) then
4143 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
4144 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
4146 case Nr_Of_Suggestions is
4147 when 1 => Suggestion_1 := Comp;
4148 when 2 => Suggestion_2 := Comp;
4149 when others => exit;
4150 end case;
4151 end if;
4152 end if;
4154 Comp := Next_Entity (Comp);
4155 end loop;
4157 -- Report at most two suggestions
4159 if Nr_Of_Suggestions = 1 then
4160 Error_Msg_NE
4161 ("\possible misspelling of&", Sel, Suggestion_1);
4163 elsif Nr_Of_Suggestions = 2 then
4164 Error_Msg_Node_2 := Suggestion_2;
4165 Error_Msg_NE
4166 ("\possible misspelling of& or&", Sel, Suggestion_1);
4167 end if;
4168 end Check_Misspelled_Selector;
4170 ----------------------
4171 -- Defined_In_Scope --
4172 ----------------------
4174 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
4176 S1 : constant Entity_Id := Scope (Base_Type (T));
4177 begin
4178 return S1 = S
4179 or else (S1 = System_Aux_Id and then S = Scope (S1));
4180 end Defined_In_Scope;
4182 -------------------
4183 -- Diagnose_Call --
4184 -------------------
4186 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
4187 Actual : Node_Id;
4188 X : Interp_Index;
4189 It : Interp;
4190 Err_Mode : Boolean;
4191 New_Nam : Node_Id;
4192 Void_Interp_Seen : Boolean := False;
4194 Success : Boolean;
4195 pragma Warnings (Off, Boolean);
4197 begin
4198 if Ada_Version >= Ada_05 then
4199 Actual := First_Actual (N);
4200 while Present (Actual) loop
4202 -- Ada 2005 (AI-50217): Post an error in case of premature
4203 -- usage of an entity from the limited view.
4205 if not Analyzed (Etype (Actual))
4206 and then From_With_Type (Etype (Actual))
4207 then
4208 Error_Msg_Qual_Level := 1;
4209 Error_Msg_NE
4210 ("missing with_clause for scope of imported type&",
4211 Actual, Etype (Actual));
4212 Error_Msg_Qual_Level := 0;
4213 end if;
4215 Next_Actual (Actual);
4216 end loop;
4217 end if;
4219 -- Analyze each candidate call again, with full error reporting
4220 -- for each.
4222 Error_Msg_N
4223 ("no candidate interpretations match the actuals:!", Nam);
4224 Err_Mode := All_Errors_Mode;
4225 All_Errors_Mode := True;
4227 -- If this is a call to an operation of a concurrent type,
4228 -- the failed interpretations have been removed from the
4229 -- name. Recover them to provide full diagnostics.
4231 if Nkind (Parent (Nam)) = N_Selected_Component then
4232 Set_Entity (Nam, Empty);
4233 New_Nam := New_Copy_Tree (Parent (Nam));
4234 Set_Is_Overloaded (New_Nam, False);
4235 Set_Is_Overloaded (Selector_Name (New_Nam), False);
4236 Set_Parent (New_Nam, Parent (Parent (Nam)));
4237 Analyze_Selected_Component (New_Nam);
4238 Get_First_Interp (Selector_Name (New_Nam), X, It);
4239 else
4240 Get_First_Interp (Nam, X, It);
4241 end if;
4243 while Present (It.Nam) loop
4244 if Etype (It.Nam) = Standard_Void_Type then
4245 Void_Interp_Seen := True;
4246 end if;
4248 Analyze_One_Call (N, It.Nam, True, Success);
4249 Get_Next_Interp (X, It);
4250 end loop;
4252 if Nkind (N) = N_Function_Call then
4253 Get_First_Interp (Nam, X, It);
4254 while Present (It.Nam) loop
4255 if Ekind (It.Nam) = E_Function
4256 or else Ekind (It.Nam) = E_Operator
4257 then
4258 return;
4259 else
4260 Get_Next_Interp (X, It);
4261 end if;
4262 end loop;
4264 -- If all interpretations are procedures, this deserves a
4265 -- more precise message. Ditto if this appears as the prefix
4266 -- of a selected component, which may be a lexical error.
4268 Error_Msg_N
4269 ("\context requires function call, found procedure name", Nam);
4271 if Nkind (Parent (N)) = N_Selected_Component
4272 and then N = Prefix (Parent (N))
4273 then
4274 Error_Msg_N (
4275 "\period should probably be semicolon", Parent (N));
4276 end if;
4278 elsif Nkind (N) = N_Procedure_Call_Statement
4279 and then not Void_Interp_Seen
4280 then
4281 Error_Msg_N (
4282 "\function name found in procedure call", Nam);
4283 end if;
4285 All_Errors_Mode := Err_Mode;
4286 end Diagnose_Call;
4288 ---------------------------
4289 -- Find_Arithmetic_Types --
4290 ---------------------------
4292 procedure Find_Arithmetic_Types
4293 (L, R : Node_Id;
4294 Op_Id : Entity_Id;
4295 N : Node_Id)
4297 Index1 : Interp_Index;
4298 Index2 : Interp_Index;
4299 It1 : Interp;
4300 It2 : Interp;
4302 procedure Check_Right_Argument (T : Entity_Id);
4303 -- Check right operand of operator
4305 --------------------------
4306 -- Check_Right_Argument --
4307 --------------------------
4309 procedure Check_Right_Argument (T : Entity_Id) is
4310 begin
4311 if not Is_Overloaded (R) then
4312 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
4313 else
4314 Get_First_Interp (R, Index2, It2);
4315 while Present (It2.Typ) loop
4316 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
4317 Get_Next_Interp (Index2, It2);
4318 end loop;
4319 end if;
4320 end Check_Right_Argument;
4322 -- Start processing for Find_Arithmetic_Types
4324 begin
4325 if not Is_Overloaded (L) then
4326 Check_Right_Argument (Etype (L));
4328 else
4329 Get_First_Interp (L, Index1, It1);
4330 while Present (It1.Typ) loop
4331 Check_Right_Argument (It1.Typ);
4332 Get_Next_Interp (Index1, It1);
4333 end loop;
4334 end if;
4336 end Find_Arithmetic_Types;
4338 ------------------------
4339 -- Find_Boolean_Types --
4340 ------------------------
4342 procedure Find_Boolean_Types
4343 (L, R : Node_Id;
4344 Op_Id : Entity_Id;
4345 N : Node_Id)
4347 Index : Interp_Index;
4348 It : Interp;
4350 procedure Check_Numeric_Argument (T : Entity_Id);
4351 -- Special case for logical operations one of whose operands is an
4352 -- integer literal. If both are literal the result is any modular type.
4354 ----------------------------
4355 -- Check_Numeric_Argument --
4356 ----------------------------
4358 procedure Check_Numeric_Argument (T : Entity_Id) is
4359 begin
4360 if T = Universal_Integer then
4361 Add_One_Interp (N, Op_Id, Any_Modular);
4363 elsif Is_Modular_Integer_Type (T) then
4364 Add_One_Interp (N, Op_Id, T);
4365 end if;
4366 end Check_Numeric_Argument;
4368 -- Start of processing for Find_Boolean_Types
4370 begin
4371 if not Is_Overloaded (L) then
4372 if Etype (L) = Universal_Integer
4373 or else Etype (L) = Any_Modular
4374 then
4375 if not Is_Overloaded (R) then
4376 Check_Numeric_Argument (Etype (R));
4378 else
4379 Get_First_Interp (R, Index, It);
4380 while Present (It.Typ) loop
4381 Check_Numeric_Argument (It.Typ);
4382 Get_Next_Interp (Index, It);
4383 end loop;
4384 end if;
4386 -- If operands are aggregates, we must assume that they may be
4387 -- boolean arrays, and leave disambiguation for the second pass.
4388 -- If only one is an aggregate, verify that the other one has an
4389 -- interpretation as a boolean array
4391 elsif Nkind (L) = N_Aggregate then
4392 if Nkind (R) = N_Aggregate then
4393 Add_One_Interp (N, Op_Id, Etype (L));
4395 elsif not Is_Overloaded (R) then
4396 if Valid_Boolean_Arg (Etype (R)) then
4397 Add_One_Interp (N, Op_Id, Etype (R));
4398 end if;
4400 else
4401 Get_First_Interp (R, Index, It);
4402 while Present (It.Typ) loop
4403 if Valid_Boolean_Arg (It.Typ) then
4404 Add_One_Interp (N, Op_Id, It.Typ);
4405 end if;
4407 Get_Next_Interp (Index, It);
4408 end loop;
4409 end if;
4411 elsif Valid_Boolean_Arg (Etype (L))
4412 and then Has_Compatible_Type (R, Etype (L))
4413 then
4414 Add_One_Interp (N, Op_Id, Etype (L));
4415 end if;
4417 else
4418 Get_First_Interp (L, Index, It);
4419 while Present (It.Typ) loop
4420 if Valid_Boolean_Arg (It.Typ)
4421 and then Has_Compatible_Type (R, It.Typ)
4422 then
4423 Add_One_Interp (N, Op_Id, It.Typ);
4424 end if;
4426 Get_Next_Interp (Index, It);
4427 end loop;
4428 end if;
4429 end Find_Boolean_Types;
4431 ---------------------------
4432 -- Find_Comparison_Types --
4433 ---------------------------
4435 procedure Find_Comparison_Types
4436 (L, R : Node_Id;
4437 Op_Id : Entity_Id;
4438 N : Node_Id)
4440 Index : Interp_Index;
4441 It : Interp;
4442 Found : Boolean := False;
4443 I_F : Interp_Index;
4444 T_F : Entity_Id;
4445 Scop : Entity_Id := Empty;
4447 procedure Try_One_Interp (T1 : Entity_Id);
4448 -- Routine to try one proposed interpretation. Note that the context
4449 -- of the operator plays no role in resolving the arguments, so that
4450 -- if there is more than one interpretation of the operands that is
4451 -- compatible with comparison, the operation is ambiguous.
4453 --------------------
4454 -- Try_One_Interp --
4455 --------------------
4457 procedure Try_One_Interp (T1 : Entity_Id) is
4458 begin
4460 -- If the operator is an expanded name, then the type of the operand
4461 -- must be defined in the corresponding scope. If the type is
4462 -- universal, the context will impose the correct type.
4464 if Present (Scop)
4465 and then not Defined_In_Scope (T1, Scop)
4466 and then T1 /= Universal_Integer
4467 and then T1 /= Universal_Real
4468 and then T1 /= Any_String
4469 and then T1 /= Any_Composite
4470 then
4471 return;
4472 end if;
4474 if Valid_Comparison_Arg (T1)
4475 and then Has_Compatible_Type (R, T1)
4476 then
4477 if Found
4478 and then Base_Type (T1) /= Base_Type (T_F)
4479 then
4480 It := Disambiguate (L, I_F, Index, Any_Type);
4482 if It = No_Interp then
4483 Ambiguous_Operands (N);
4484 Set_Etype (L, Any_Type);
4485 return;
4487 else
4488 T_F := It.Typ;
4489 end if;
4491 else
4492 Found := True;
4493 T_F := T1;
4494 I_F := Index;
4495 end if;
4497 Set_Etype (L, T_F);
4498 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4500 end if;
4501 end Try_One_Interp;
4503 -- Start processing for Find_Comparison_Types
4505 begin
4506 -- If left operand is aggregate, the right operand has to
4507 -- provide a usable type for it.
4509 if Nkind (L) = N_Aggregate
4510 and then Nkind (R) /= N_Aggregate
4511 then
4512 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
4513 return;
4514 end if;
4516 if Nkind (N) = N_Function_Call
4517 and then Nkind (Name (N)) = N_Expanded_Name
4518 then
4519 Scop := Entity (Prefix (Name (N)));
4521 -- The prefix may be a package renaming, and the subsequent test
4522 -- requires the original package.
4524 if Ekind (Scop) = E_Package
4525 and then Present (Renamed_Entity (Scop))
4526 then
4527 Scop := Renamed_Entity (Scop);
4528 Set_Entity (Prefix (Name (N)), Scop);
4529 end if;
4530 end if;
4532 if not Is_Overloaded (L) then
4533 Try_One_Interp (Etype (L));
4535 else
4536 Get_First_Interp (L, Index, It);
4537 while Present (It.Typ) loop
4538 Try_One_Interp (It.Typ);
4539 Get_Next_Interp (Index, It);
4540 end loop;
4541 end if;
4542 end Find_Comparison_Types;
4544 ----------------------------------------
4545 -- Find_Non_Universal_Interpretations --
4546 ----------------------------------------
4548 procedure Find_Non_Universal_Interpretations
4549 (N : Node_Id;
4550 R : Node_Id;
4551 Op_Id : Entity_Id;
4552 T1 : Entity_Id)
4554 Index : Interp_Index;
4555 It : Interp;
4557 begin
4558 if T1 = Universal_Integer
4559 or else T1 = Universal_Real
4560 then
4561 if not Is_Overloaded (R) then
4562 Add_One_Interp
4563 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
4564 else
4565 Get_First_Interp (R, Index, It);
4566 while Present (It.Typ) loop
4567 if Covers (It.Typ, T1) then
4568 Add_One_Interp
4569 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
4570 end if;
4572 Get_Next_Interp (Index, It);
4573 end loop;
4574 end if;
4575 else
4576 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
4577 end if;
4578 end Find_Non_Universal_Interpretations;
4580 ------------------------------
4581 -- Find_Concatenation_Types --
4582 ------------------------------
4584 procedure Find_Concatenation_Types
4585 (L, R : Node_Id;
4586 Op_Id : Entity_Id;
4587 N : Node_Id)
4589 Op_Type : constant Entity_Id := Etype (Op_Id);
4591 begin
4592 if Is_Array_Type (Op_Type)
4593 and then not Is_Limited_Type (Op_Type)
4595 and then (Has_Compatible_Type (L, Op_Type)
4596 or else
4597 Has_Compatible_Type (L, Component_Type (Op_Type)))
4599 and then (Has_Compatible_Type (R, Op_Type)
4600 or else
4601 Has_Compatible_Type (R, Component_Type (Op_Type)))
4602 then
4603 Add_One_Interp (N, Op_Id, Op_Type);
4604 end if;
4605 end Find_Concatenation_Types;
4607 -------------------------
4608 -- Find_Equality_Types --
4609 -------------------------
4611 procedure Find_Equality_Types
4612 (L, R : Node_Id;
4613 Op_Id : Entity_Id;
4614 N : Node_Id)
4616 Index : Interp_Index;
4617 It : Interp;
4618 Found : Boolean := False;
4619 I_F : Interp_Index;
4620 T_F : Entity_Id;
4621 Scop : Entity_Id := Empty;
4623 procedure Try_One_Interp (T1 : Entity_Id);
4624 -- The context of the operator plays no role in resolving the
4625 -- arguments, so that if there is more than one interpretation
4626 -- of the operands that is compatible with equality, the construct
4627 -- is ambiguous and an error can be emitted now, after trying to
4628 -- disambiguate, i.e. applying preference rules.
4630 --------------------
4631 -- Try_One_Interp --
4632 --------------------
4634 procedure Try_One_Interp (T1 : Entity_Id) is
4635 begin
4636 -- If the operator is an expanded name, then the type of the operand
4637 -- must be defined in the corresponding scope. If the type is
4638 -- universal, the context will impose the correct type. An anonymous
4639 -- type for a 'Access reference is also universal in this sense, as
4640 -- the actual type is obtained from context.
4641 -- In Ada 2005, the equality operator for anonymous access types
4642 -- is declared in Standard, and preference rules apply to it.
4644 if Present (Scop) then
4645 if Defined_In_Scope (T1, Scop)
4646 or else T1 = Universal_Integer
4647 or else T1 = Universal_Real
4648 or else T1 = Any_Access
4649 or else T1 = Any_String
4650 or else T1 = Any_Composite
4651 or else (Ekind (T1) = E_Access_Subprogram_Type
4652 and then not Comes_From_Source (T1))
4653 then
4654 null;
4656 elsif Ekind (T1) = E_Anonymous_Access_Type
4657 and then Scop = Standard_Standard
4658 then
4659 null;
4661 else
4662 -- The scope does not contain an operator for the type
4664 return;
4665 end if;
4666 end if;
4668 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
4669 -- Do not allow anonymous access types in equality operators.
4671 if Ada_Version < Ada_05
4672 and then Ekind (T1) = E_Anonymous_Access_Type
4673 then
4674 return;
4675 end if;
4677 if T1 /= Standard_Void_Type
4678 and then not Is_Limited_Type (T1)
4679 and then not Is_Limited_Composite (T1)
4680 and then Has_Compatible_Type (R, T1)
4681 then
4682 if Found
4683 and then Base_Type (T1) /= Base_Type (T_F)
4684 then
4685 It := Disambiguate (L, I_F, Index, Any_Type);
4687 if It = No_Interp then
4688 Ambiguous_Operands (N);
4689 Set_Etype (L, Any_Type);
4690 return;
4692 else
4693 T_F := It.Typ;
4694 end if;
4696 else
4697 Found := True;
4698 T_F := T1;
4699 I_F := Index;
4700 end if;
4702 if not Analyzed (L) then
4703 Set_Etype (L, T_F);
4704 end if;
4706 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4708 -- Case of operator was not visible, Etype still set to Any_Type
4710 if Etype (N) = Any_Type then
4711 Found := False;
4712 end if;
4714 elsif Scop = Standard_Standard
4715 and then Ekind (T1) = E_Anonymous_Access_Type
4716 then
4717 Found := True;
4718 end if;
4719 end Try_One_Interp;
4721 -- Start of processing for Find_Equality_Types
4723 begin
4724 -- If left operand is aggregate, the right operand has to
4725 -- provide a usable type for it.
4727 if Nkind (L) = N_Aggregate
4728 and then Nkind (R) /= N_Aggregate
4729 then
4730 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
4731 return;
4732 end if;
4734 if Nkind (N) = N_Function_Call
4735 and then Nkind (Name (N)) = N_Expanded_Name
4736 then
4737 Scop := Entity (Prefix (Name (N)));
4739 -- The prefix may be a package renaming, and the subsequent test
4740 -- requires the original package.
4742 if Ekind (Scop) = E_Package
4743 and then Present (Renamed_Entity (Scop))
4744 then
4745 Scop := Renamed_Entity (Scop);
4746 Set_Entity (Prefix (Name (N)), Scop);
4747 end if;
4748 end if;
4750 if not Is_Overloaded (L) then
4751 Try_One_Interp (Etype (L));
4753 else
4754 Get_First_Interp (L, Index, It);
4755 while Present (It.Typ) loop
4756 Try_One_Interp (It.Typ);
4757 Get_Next_Interp (Index, It);
4758 end loop;
4759 end if;
4760 end Find_Equality_Types;
4762 -------------------------
4763 -- Find_Negation_Types --
4764 -------------------------
4766 procedure Find_Negation_Types
4767 (R : Node_Id;
4768 Op_Id : Entity_Id;
4769 N : Node_Id)
4771 Index : Interp_Index;
4772 It : Interp;
4774 begin
4775 if not Is_Overloaded (R) then
4776 if Etype (R) = Universal_Integer then
4777 Add_One_Interp (N, Op_Id, Any_Modular);
4778 elsif Valid_Boolean_Arg (Etype (R)) then
4779 Add_One_Interp (N, Op_Id, Etype (R));
4780 end if;
4782 else
4783 Get_First_Interp (R, Index, It);
4784 while Present (It.Typ) loop
4785 if Valid_Boolean_Arg (It.Typ) then
4786 Add_One_Interp (N, Op_Id, It.Typ);
4787 end if;
4789 Get_Next_Interp (Index, It);
4790 end loop;
4791 end if;
4792 end Find_Negation_Types;
4794 ------------------------------
4795 -- Find_Primitive_Operation --
4796 ------------------------------
4798 function Find_Primitive_Operation (N : Node_Id) return Boolean is
4799 Obj : constant Node_Id := Prefix (N);
4800 Op : constant Node_Id := Selector_Name (N);
4802 Prim : Elmt_Id;
4803 Prims : Elist_Id;
4804 Typ : Entity_Id;
4806 begin
4807 Set_Etype (Op, Any_Type);
4809 if Is_Access_Type (Etype (Obj)) then
4810 Typ := Designated_Type (Etype (Obj));
4811 else
4812 Typ := Etype (Obj);
4813 end if;
4815 if Is_Class_Wide_Type (Typ) then
4816 Typ := Root_Type (Typ);
4817 end if;
4819 Prims := Primitive_Operations (Typ);
4821 Prim := First_Elmt (Prims);
4822 while Present (Prim) loop
4823 if Chars (Node (Prim)) = Chars (Op) then
4824 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
4825 Set_Etype (N, Etype (Node (Prim)));
4826 end if;
4828 Next_Elmt (Prim);
4829 end loop;
4831 -- Now look for class-wide operations of the type or any of its
4832 -- ancestors by iterating over the homonyms of the selector.
4834 declare
4835 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
4836 Hom : Entity_Id;
4838 begin
4839 Hom := Current_Entity (Op);
4840 while Present (Hom) loop
4841 if (Ekind (Hom) = E_Procedure
4842 or else
4843 Ekind (Hom) = E_Function)
4844 and then Scope (Hom) = Scope (Typ)
4845 and then Present (First_Formal (Hom))
4846 and then
4847 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
4848 or else
4849 (Is_Access_Type (Etype (First_Formal (Hom)))
4850 and then
4851 Ekind (Etype (First_Formal (Hom))) =
4852 E_Anonymous_Access_Type
4853 and then
4854 Base_Type
4855 (Designated_Type (Etype (First_Formal (Hom)))) =
4856 Cls_Type))
4857 then
4858 Add_One_Interp (Op, Hom, Etype (Hom));
4859 Set_Etype (N, Etype (Hom));
4860 end if;
4862 Hom := Homonym (Hom);
4863 end loop;
4864 end;
4866 return Etype (Op) /= Any_Type;
4867 end Find_Primitive_Operation;
4869 ----------------------
4870 -- Find_Unary_Types --
4871 ----------------------
4873 procedure Find_Unary_Types
4874 (R : Node_Id;
4875 Op_Id : Entity_Id;
4876 N : Node_Id)
4878 Index : Interp_Index;
4879 It : Interp;
4881 begin
4882 if not Is_Overloaded (R) then
4883 if Is_Numeric_Type (Etype (R)) then
4884 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
4885 end if;
4887 else
4888 Get_First_Interp (R, Index, It);
4889 while Present (It.Typ) loop
4890 if Is_Numeric_Type (It.Typ) then
4891 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
4892 end if;
4894 Get_Next_Interp (Index, It);
4895 end loop;
4896 end if;
4897 end Find_Unary_Types;
4899 ------------------
4900 -- Junk_Operand --
4901 ------------------
4903 function Junk_Operand (N : Node_Id) return Boolean is
4904 Enode : Node_Id;
4906 begin
4907 if Error_Posted (N) then
4908 return False;
4909 end if;
4911 -- Get entity to be tested
4913 if Is_Entity_Name (N)
4914 and then Present (Entity (N))
4915 then
4916 Enode := N;
4918 -- An odd case, a procedure name gets converted to a very peculiar
4919 -- function call, and here is where we detect this happening.
4921 elsif Nkind (N) = N_Function_Call
4922 and then Is_Entity_Name (Name (N))
4923 and then Present (Entity (Name (N)))
4924 then
4925 Enode := Name (N);
4927 -- Another odd case, there are at least some cases of selected
4928 -- components where the selected component is not marked as having
4929 -- an entity, even though the selector does have an entity
4931 elsif Nkind (N) = N_Selected_Component
4932 and then Present (Entity (Selector_Name (N)))
4933 then
4934 Enode := Selector_Name (N);
4936 else
4937 return False;
4938 end if;
4940 -- Now test the entity we got to see if it is a bad case
4942 case Ekind (Entity (Enode)) is
4944 when E_Package =>
4945 Error_Msg_N
4946 ("package name cannot be used as operand", Enode);
4948 when Generic_Unit_Kind =>
4949 Error_Msg_N
4950 ("generic unit name cannot be used as operand", Enode);
4952 when Type_Kind =>
4953 Error_Msg_N
4954 ("subtype name cannot be used as operand", Enode);
4956 when Entry_Kind =>
4957 Error_Msg_N
4958 ("entry name cannot be used as operand", Enode);
4960 when E_Procedure =>
4961 Error_Msg_N
4962 ("procedure name cannot be used as operand", Enode);
4964 when E_Exception =>
4965 Error_Msg_N
4966 ("exception name cannot be used as operand", Enode);
4968 when E_Block | E_Label | E_Loop =>
4969 Error_Msg_N
4970 ("label name cannot be used as operand", Enode);
4972 when others =>
4973 return False;
4975 end case;
4977 return True;
4978 end Junk_Operand;
4980 --------------------
4981 -- Operator_Check --
4982 --------------------
4984 procedure Operator_Check (N : Node_Id) is
4985 begin
4986 Remove_Abstract_Operations (N);
4988 -- Test for case of no interpretation found for operator
4990 if Etype (N) = Any_Type then
4991 declare
4992 L : Node_Id;
4993 R : Node_Id;
4994 Op_Id : Entity_Id := Empty;
4996 begin
4997 R := Right_Opnd (N);
4999 if Nkind (N) in N_Binary_Op then
5000 L := Left_Opnd (N);
5001 else
5002 L := Empty;
5003 end if;
5005 -- If either operand has no type, then don't complain further,
5006 -- since this simply means that we have a propagated error.
5008 if R = Error
5009 or else Etype (R) = Any_Type
5010 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5011 then
5012 return;
5014 -- We explicitly check for the case of concatenation of component
5015 -- with component to avoid reporting spurious matching array types
5016 -- that might happen to be lurking in distant packages (such as
5017 -- run-time packages). This also prevents inconsistencies in the
5018 -- messages for certain ACVC B tests, which can vary depending on
5019 -- types declared in run-time interfaces. Another improvement when
5020 -- aggregates are present is to look for a well-typed operand.
5022 elsif Present (Candidate_Type)
5023 and then (Nkind (N) /= N_Op_Concat
5024 or else Is_Array_Type (Etype (L))
5025 or else Is_Array_Type (Etype (R)))
5026 then
5028 if Nkind (N) = N_Op_Concat then
5029 if Etype (L) /= Any_Composite
5030 and then Is_Array_Type (Etype (L))
5031 then
5032 Candidate_Type := Etype (L);
5034 elsif Etype (R) /= Any_Composite
5035 and then Is_Array_Type (Etype (R))
5036 then
5037 Candidate_Type := Etype (R);
5038 end if;
5039 end if;
5041 Error_Msg_NE
5042 ("operator for} is not directly visible!",
5043 N, First_Subtype (Candidate_Type));
5044 Error_Msg_N ("use clause would make operation legal!", N);
5045 return;
5047 -- If either operand is a junk operand (e.g. package name), then
5048 -- post appropriate error messages, but do not complain further.
5050 -- Note that the use of OR in this test instead of OR ELSE is
5051 -- quite deliberate, we may as well check both operands in the
5052 -- binary operator case.
5054 elsif Junk_Operand (R)
5055 or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
5056 then
5057 return;
5059 -- If we have a logical operator, one of whose operands is
5060 -- Boolean, then we know that the other operand cannot resolve to
5061 -- Boolean (since we got no interpretations), but in that case we
5062 -- pretty much know that the other operand should be Boolean, so
5063 -- resolve it that way (generating an error)
5065 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
5066 if Etype (L) = Standard_Boolean then
5067 Resolve (R, Standard_Boolean);
5068 return;
5069 elsif Etype (R) = Standard_Boolean then
5070 Resolve (L, Standard_Boolean);
5071 return;
5072 end if;
5074 -- For an arithmetic operator or comparison operator, if one
5075 -- of the operands is numeric, then we know the other operand
5076 -- is not the same numeric type. If it is a non-numeric type,
5077 -- then probably it is intended to match the other operand.
5079 elsif Nkind_In (N, N_Op_Add,
5080 N_Op_Divide,
5081 N_Op_Ge,
5082 N_Op_Gt,
5083 N_Op_Le)
5084 or else
5085 Nkind_In (N, N_Op_Lt,
5086 N_Op_Mod,
5087 N_Op_Multiply,
5088 N_Op_Rem,
5089 N_Op_Subtract)
5090 then
5091 if Is_Numeric_Type (Etype (L))
5092 and then not Is_Numeric_Type (Etype (R))
5093 then
5094 Resolve (R, Etype (L));
5095 return;
5097 elsif Is_Numeric_Type (Etype (R))
5098 and then not Is_Numeric_Type (Etype (L))
5099 then
5100 Resolve (L, Etype (R));
5101 return;
5102 end if;
5104 -- Comparisons on A'Access are common enough to deserve a
5105 -- special message.
5107 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
5108 and then Ekind (Etype (L)) = E_Access_Attribute_Type
5109 and then Ekind (Etype (R)) = E_Access_Attribute_Type
5110 then
5111 Error_Msg_N
5112 ("two access attributes cannot be compared directly", N);
5113 Error_Msg_N
5114 ("\use qualified expression for one of the operands",
5116 return;
5118 -- Another one for C programmers
5120 elsif Nkind (N) = N_Op_Concat
5121 and then Valid_Boolean_Arg (Etype (L))
5122 and then Valid_Boolean_Arg (Etype (R))
5123 then
5124 Error_Msg_N ("invalid operands for concatenation", N);
5125 Error_Msg_N ("\maybe AND was meant", N);
5126 return;
5128 -- A special case for comparison of access parameter with null
5130 elsif Nkind (N) = N_Op_Eq
5131 and then Is_Entity_Name (L)
5132 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
5133 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
5134 N_Access_Definition
5135 and then Nkind (R) = N_Null
5136 then
5137 Error_Msg_N ("access parameter is not allowed to be null", L);
5138 Error_Msg_N ("\(call would raise Constraint_Error)", L);
5139 return;
5140 end if;
5142 -- If we fall through then just give general message. Note that in
5143 -- the following messages, if the operand is overloaded we choose
5144 -- an arbitrary type to complain about, but that is probably more
5145 -- useful than not giving a type at all.
5147 if Nkind (N) in N_Unary_Op then
5148 Error_Msg_Node_2 := Etype (R);
5149 Error_Msg_N ("operator& not defined for}", N);
5150 return;
5152 else
5153 if Nkind (N) in N_Binary_Op then
5154 if not Is_Overloaded (L)
5155 and then not Is_Overloaded (R)
5156 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
5157 then
5158 Error_Msg_Node_2 := First_Subtype (Etype (R));
5159 Error_Msg_N ("there is no applicable operator& for}", N);
5161 else
5162 -- Another attempt to find a fix: one of the candidate
5163 -- interpretations may not be use-visible. This has
5164 -- already been checked for predefined operators, so
5165 -- we examine only user-defined functions.
5167 Op_Id := Get_Name_Entity_Id (Chars (N));
5169 while Present (Op_Id) loop
5170 if Ekind (Op_Id) /= E_Operator
5171 and then Is_Overloadable (Op_Id)
5172 then
5173 if not Is_Immediately_Visible (Op_Id)
5174 and then not In_Use (Scope (Op_Id))
5175 and then not Is_Abstract_Subprogram (Op_Id)
5176 and then not Is_Hidden (Op_Id)
5177 and then Ekind (Scope (Op_Id)) = E_Package
5178 and then
5179 Has_Compatible_Type
5180 (L, Etype (First_Formal (Op_Id)))
5181 and then Present
5182 (Next_Formal (First_Formal (Op_Id)))
5183 and then
5184 Has_Compatible_Type
5186 Etype (Next_Formal (First_Formal (Op_Id))))
5187 then
5188 Error_Msg_N
5189 ("No legal interpretation for operator&", N);
5190 Error_Msg_NE
5191 ("\use clause on& would make operation legal",
5192 N, Scope (Op_Id));
5193 exit;
5194 end if;
5195 end if;
5197 Op_Id := Homonym (Op_Id);
5198 end loop;
5200 if No (Op_Id) then
5201 Error_Msg_N ("invalid operand types for operator&", N);
5203 if Nkind (N) /= N_Op_Concat then
5204 Error_Msg_NE ("\left operand has}!", N, Etype (L));
5205 Error_Msg_NE ("\right operand has}!", N, Etype (R));
5206 end if;
5207 end if;
5208 end if;
5209 end if;
5210 end if;
5211 end;
5212 end if;
5213 end Operator_Check;
5215 -----------------------------------------
5216 -- Process_Implicit_Dereference_Prefix --
5217 -----------------------------------------
5219 function Process_Implicit_Dereference_Prefix
5220 (E : Entity_Id;
5221 P : Entity_Id) return Entity_Id
5223 Ref : Node_Id;
5224 Typ : constant Entity_Id := Designated_Type (Etype (P));
5226 begin
5227 if Present (E)
5228 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
5229 then
5230 -- We create a dummy reference to E to ensure that the reference
5231 -- is not considered as part of an assignment (an implicit
5232 -- dereference can never assign to its prefix). The Comes_From_Source
5233 -- attribute needs to be propagated for accurate warnings.
5235 Ref := New_Reference_To (E, Sloc (P));
5236 Set_Comes_From_Source (Ref, Comes_From_Source (P));
5237 Generate_Reference (E, Ref);
5238 end if;
5240 -- An implicit dereference is a legal occurrence of an
5241 -- incomplete type imported through a limited_with clause,
5242 -- if the full view is visible.
5244 if From_With_Type (Typ)
5245 and then not From_With_Type (Scope (Typ))
5246 and then
5247 (Is_Immediately_Visible (Scope (Typ))
5248 or else
5249 (Is_Child_Unit (Scope (Typ))
5250 and then Is_Visible_Child_Unit (Scope (Typ))))
5251 then
5252 return Available_View (Typ);
5253 else
5254 return Typ;
5255 end if;
5257 end Process_Implicit_Dereference_Prefix;
5259 --------------------------------
5260 -- Remove_Abstract_Operations --
5261 --------------------------------
5263 procedure Remove_Abstract_Operations (N : Node_Id) is
5264 Abstract_Op : Entity_Id := Empty;
5265 Address_Kludge : Boolean := False;
5266 I : Interp_Index;
5267 It : Interp;
5269 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
5270 -- activate this if either extensions are enabled, or if the abstract
5271 -- operation in question comes from a predefined file. This latter test
5272 -- allows us to use abstract to make operations invisible to users. In
5273 -- particular, if type Address is non-private and abstract subprograms
5274 -- are used to hide its operators, they will be truly hidden.
5276 type Operand_Position is (First_Op, Second_Op);
5277 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
5279 procedure Remove_Address_Interpretations (Op : Operand_Position);
5280 -- Ambiguities may arise when the operands are literal and the address
5281 -- operations in s-auxdec are visible. In that case, remove the
5282 -- interpretation of a literal as Address, to retain the semantics of
5283 -- Address as a private type.
5285 ------------------------------------
5286 -- Remove_Address_Interpretations --
5287 ------------------------------------
5289 procedure Remove_Address_Interpretations (Op : Operand_Position) is
5290 Formal : Entity_Id;
5292 begin
5293 if Is_Overloaded (N) then
5294 Get_First_Interp (N, I, It);
5295 while Present (It.Nam) loop
5296 Formal := First_Entity (It.Nam);
5298 if Op = Second_Op then
5299 Formal := Next_Entity (Formal);
5300 end if;
5302 if Is_Descendent_Of_Address (Etype (Formal)) then
5303 Address_Kludge := True;
5304 Remove_Interp (I);
5305 end if;
5307 Get_Next_Interp (I, It);
5308 end loop;
5309 end if;
5310 end Remove_Address_Interpretations;
5312 -- Start of processing for Remove_Abstract_Operations
5314 begin
5315 if Is_Overloaded (N) then
5316 Get_First_Interp (N, I, It);
5318 while Present (It.Nam) loop
5319 if Is_Overloadable (It.Nam)
5320 and then Is_Abstract_Subprogram (It.Nam)
5321 and then not Is_Dispatching_Operation (It.Nam)
5322 then
5323 Abstract_Op := It.Nam;
5325 if Is_Descendent_Of_Address (It.Typ) then
5326 Address_Kludge := True;
5327 Remove_Interp (I);
5328 exit;
5330 -- In Ada 2005, this operation does not participate in Overload
5331 -- resolution. If the operation is defined in a predefined
5332 -- unit, it is one of the operations declared abstract in some
5333 -- variants of System, and it must be removed as well.
5335 elsif Ada_Version >= Ada_05
5336 or else Is_Predefined_File_Name
5337 (Unit_File_Name (Get_Source_Unit (It.Nam)))
5338 then
5339 Remove_Interp (I);
5340 exit;
5341 end if;
5342 end if;
5344 Get_Next_Interp (I, It);
5345 end loop;
5347 if No (Abstract_Op) then
5349 -- If some interpretation yields an integer type, it is still
5350 -- possible that there are address interpretations. Remove them
5351 -- if one operand is a literal, to avoid spurious ambiguities
5352 -- on systems where Address is a visible integer type.
5354 if Is_Overloaded (N)
5355 and then Nkind (N) in N_Op
5356 and then Is_Integer_Type (Etype (N))
5357 then
5358 if Nkind (N) in N_Binary_Op then
5359 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
5360 Remove_Address_Interpretations (Second_Op);
5362 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
5363 Remove_Address_Interpretations (First_Op);
5364 end if;
5365 end if;
5366 end if;
5368 elsif Nkind (N) in N_Op then
5370 -- Remove interpretations that treat literals as addresses. This
5371 -- is never appropriate, even when Address is defined as a visible
5372 -- Integer type. The reason is that we would really prefer Address
5373 -- to behave as a private type, even in this case, which is there
5374 -- only to accommodate oddities of VMS address sizes. If Address
5375 -- is a visible integer type, we get lots of overload ambiguities.
5377 if Nkind (N) in N_Binary_Op then
5378 declare
5379 U1 : constant Boolean :=
5380 Present (Universal_Interpretation (Right_Opnd (N)));
5381 U2 : constant Boolean :=
5382 Present (Universal_Interpretation (Left_Opnd (N)));
5384 begin
5385 if U1 then
5386 Remove_Address_Interpretations (Second_Op);
5387 end if;
5389 if U2 then
5390 Remove_Address_Interpretations (First_Op);
5391 end if;
5393 if not (U1 and U2) then
5395 -- Remove corresponding predefined operator, which is
5396 -- always added to the overload set.
5398 Get_First_Interp (N, I, It);
5399 while Present (It.Nam) loop
5400 if Scope (It.Nam) = Standard_Standard
5401 and then Base_Type (It.Typ) =
5402 Base_Type (Etype (Abstract_Op))
5403 then
5404 Remove_Interp (I);
5405 end if;
5407 Get_Next_Interp (I, It);
5408 end loop;
5410 elsif Is_Overloaded (N)
5411 and then Present (Univ_Type)
5412 then
5413 -- If both operands have a universal interpretation,
5414 -- it is still necessary to remove interpretations that
5415 -- yield Address. Any remaining ambiguities will be
5416 -- removed in Disambiguate.
5418 Get_First_Interp (N, I, It);
5419 while Present (It.Nam) loop
5420 if Is_Descendent_Of_Address (It.Typ) then
5421 Remove_Interp (I);
5423 elsif not Is_Type (It.Nam) then
5424 Set_Entity (N, It.Nam);
5425 end if;
5427 Get_Next_Interp (I, It);
5428 end loop;
5429 end if;
5430 end;
5431 end if;
5433 elsif Nkind (N) = N_Function_Call
5434 and then
5435 (Nkind (Name (N)) = N_Operator_Symbol
5436 or else
5437 (Nkind (Name (N)) = N_Expanded_Name
5438 and then
5439 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
5440 then
5442 declare
5443 Arg1 : constant Node_Id := First (Parameter_Associations (N));
5444 U1 : constant Boolean :=
5445 Present (Universal_Interpretation (Arg1));
5446 U2 : constant Boolean :=
5447 Present (Next (Arg1)) and then
5448 Present (Universal_Interpretation (Next (Arg1)));
5450 begin
5451 if U1 then
5452 Remove_Address_Interpretations (First_Op);
5453 end if;
5455 if U2 then
5456 Remove_Address_Interpretations (Second_Op);
5457 end if;
5459 if not (U1 and U2) then
5460 Get_First_Interp (N, I, It);
5461 while Present (It.Nam) loop
5462 if Scope (It.Nam) = Standard_Standard
5463 and then It.Typ = Base_Type (Etype (Abstract_Op))
5464 then
5465 Remove_Interp (I);
5466 end if;
5468 Get_Next_Interp (I, It);
5469 end loop;
5470 end if;
5471 end;
5472 end if;
5474 -- If the removal has left no valid interpretations, emit an error
5475 -- message now and label node as illegal.
5477 if Present (Abstract_Op) then
5478 Get_First_Interp (N, I, It);
5480 if No (It.Nam) then
5482 -- Removal of abstract operation left no viable candidate
5484 Set_Etype (N, Any_Type);
5485 Error_Msg_Sloc := Sloc (Abstract_Op);
5486 Error_Msg_NE
5487 ("cannot call abstract operation& declared#", N, Abstract_Op);
5489 -- In Ada 2005, an abstract operation may disable predefined
5490 -- operators. Since the context is not yet known, we mark the
5491 -- predefined operators as potentially hidden. Do not include
5492 -- predefined operators when addresses are involved since this
5493 -- case is handled separately.
5495 elsif Ada_Version >= Ada_05
5496 and then not Address_Kludge
5497 then
5498 while Present (It.Nam) loop
5499 if Is_Numeric_Type (It.Typ)
5500 and then Scope (It.Typ) = Standard_Standard
5501 then
5502 Set_Abstract_Op (I, Abstract_Op);
5503 end if;
5505 Get_Next_Interp (I, It);
5506 end loop;
5507 end if;
5508 end if;
5509 end if;
5510 end Remove_Abstract_Operations;
5512 -----------------------
5513 -- Try_Indirect_Call --
5514 -----------------------
5516 function Try_Indirect_Call
5517 (N : Node_Id;
5518 Nam : Entity_Id;
5519 Typ : Entity_Id) return Boolean
5521 Actual : Node_Id;
5522 Formal : Entity_Id;
5524 Call_OK : Boolean;
5525 pragma Warnings (Off, Call_OK);
5527 begin
5528 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
5530 Actual := First_Actual (N);
5531 Formal := First_Formal (Designated_Type (Typ));
5532 while Present (Actual) and then Present (Formal) loop
5533 if not Has_Compatible_Type (Actual, Etype (Formal)) then
5534 return False;
5535 end if;
5537 Next (Actual);
5538 Next_Formal (Formal);
5539 end loop;
5541 if No (Actual) and then No (Formal) then
5542 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
5544 -- Nam is a candidate interpretation for the name in the call,
5545 -- if it is not an indirect call.
5547 if not Is_Type (Nam)
5548 and then Is_Entity_Name (Name (N))
5549 then
5550 Set_Entity (Name (N), Nam);
5551 end if;
5553 return True;
5554 else
5555 return False;
5556 end if;
5557 end Try_Indirect_Call;
5559 ----------------------
5560 -- Try_Indexed_Call --
5561 ----------------------
5563 function Try_Indexed_Call
5564 (N : Node_Id;
5565 Nam : Entity_Id;
5566 Typ : Entity_Id;
5567 Skip_First : Boolean) return Boolean
5569 Loc : constant Source_Ptr := Sloc (N);
5570 Actuals : constant List_Id := Parameter_Associations (N);
5571 Actual : Node_Id;
5572 Index : Entity_Id;
5574 begin
5575 Actual := First (Actuals);
5577 -- If the call was originally written in prefix form, skip the first
5578 -- actual, which is obviously not defaulted.
5580 if Skip_First then
5581 Next (Actual);
5582 end if;
5584 Index := First_Index (Typ);
5585 while Present (Actual) and then Present (Index) loop
5587 -- If the parameter list has a named association, the expression
5588 -- is definitely a call and not an indexed component.
5590 if Nkind (Actual) = N_Parameter_Association then
5591 return False;
5592 end if;
5594 if Is_Entity_Name (Actual)
5595 and then Is_Type (Entity (Actual))
5596 and then No (Next (Actual))
5597 then
5598 Rewrite (N,
5599 Make_Slice (Loc,
5600 Prefix => Make_Function_Call (Loc,
5601 Name => Relocate_Node (Name (N))),
5602 Discrete_Range =>
5603 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
5605 Analyze (N);
5606 return True;
5608 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
5609 return False;
5610 end if;
5612 Next (Actual);
5613 Next_Index (Index);
5614 end loop;
5616 if No (Actual) and then No (Index) then
5617 Add_One_Interp (N, Nam, Component_Type (Typ));
5619 -- Nam is a candidate interpretation for the name in the call,
5620 -- if it is not an indirect call.
5622 if not Is_Type (Nam)
5623 and then Is_Entity_Name (Name (N))
5624 then
5625 Set_Entity (Name (N), Nam);
5626 end if;
5628 return True;
5629 else
5630 return False;
5631 end if;
5632 end Try_Indexed_Call;
5634 --------------------------
5635 -- Try_Object_Operation --
5636 --------------------------
5638 function Try_Object_Operation (N : Node_Id) return Boolean is
5639 K : constant Node_Kind := Nkind (Parent (N));
5640 Is_Subprg_Call : constant Boolean := Nkind_In
5641 (K, N_Procedure_Call_Statement,
5642 N_Function_Call);
5643 Loc : constant Source_Ptr := Sloc (N);
5644 Obj : constant Node_Id := Prefix (N);
5645 Subprog : constant Node_Id :=
5646 Make_Identifier (Sloc (Selector_Name (N)),
5647 Chars => Chars (Selector_Name (N)));
5648 -- Identifier on which possible interpretations will be collected
5650 Report_Error : Boolean := False;
5651 -- If no candidate interpretation matches the context, redo the
5652 -- analysis with error enabled to provide additional information.
5654 Actual : Node_Id;
5655 Candidate : Entity_Id := Empty;
5656 New_Call_Node : Node_Id := Empty;
5657 Node_To_Replace : Node_Id;
5658 Obj_Type : Entity_Id := Etype (Obj);
5659 Success : Boolean := False;
5661 function Valid_Candidate
5662 (Success : Boolean;
5663 Call : Node_Id;
5664 Subp : Entity_Id) return Entity_Id;
5665 -- If the subprogram is a valid interpretation, record it, and add
5666 -- to the list of interpretations of Subprog.
5668 procedure Complete_Object_Operation
5669 (Call_Node : Node_Id;
5670 Node_To_Replace : Node_Id);
5671 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
5672 -- Call_Node, insert the object (or its dereference) as the first actual
5673 -- in the call, and complete the analysis of the call.
5675 procedure Report_Ambiguity (Op : Entity_Id);
5676 -- If a prefixed procedure call is ambiguous, indicate whether the
5677 -- call includes an implicit dereference or an implicit 'Access.
5679 procedure Transform_Object_Operation
5680 (Call_Node : out Node_Id;
5681 Node_To_Replace : out Node_Id);
5682 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
5683 -- Call_Node is the resulting subprogram call, Node_To_Replace is
5684 -- either N or the parent of N, and Subprog is a reference to the
5685 -- subprogram we are trying to match.
5687 function Try_Class_Wide_Operation
5688 (Call_Node : Node_Id;
5689 Node_To_Replace : Node_Id) return Boolean;
5690 -- Traverse all ancestor types looking for a class-wide subprogram
5691 -- for which the current operation is a valid non-dispatching call.
5693 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
5694 -- If prefix is overloaded, its interpretation may include different
5695 -- tagged types, and we must examine the primitive operations and
5696 -- the class-wide operations of each in order to find candidate
5697 -- interpretations for the call as a whole.
5699 function Try_Primitive_Operation
5700 (Call_Node : Node_Id;
5701 Node_To_Replace : Node_Id) return Boolean;
5702 -- Traverse the list of primitive subprograms looking for a dispatching
5703 -- operation for which the current node is a valid call .
5705 ---------------------
5706 -- Valid_Candidate --
5707 ---------------------
5709 function Valid_Candidate
5710 (Success : Boolean;
5711 Call : Node_Id;
5712 Subp : Entity_Id) return Entity_Id
5714 Comp_Type : Entity_Id;
5716 begin
5717 -- If the subprogram is a valid interpretation, record it in global
5718 -- variable Subprog, to collect all possible overloadings.
5720 if Success then
5721 if Subp /= Entity (Subprog) then
5722 Add_One_Interp (Subprog, Subp, Etype (Subp));
5723 end if;
5724 end if;
5726 -- If the call may be an indexed call, retrieve component type of
5727 -- resulting expression, and add possible interpretation.
5729 Comp_Type := Empty;
5731 if Nkind (Call) = N_Function_Call
5732 and then Nkind (Parent (N)) = N_Indexed_Component
5733 and then Needs_One_Actual (Subp)
5734 then
5735 if Is_Array_Type (Etype (Subp)) then
5736 Comp_Type := Component_Type (Etype (Subp));
5738 elsif Is_Access_Type (Etype (Subp))
5739 and then Is_Array_Type (Designated_Type (Etype (Subp)))
5740 then
5741 Comp_Type := Component_Type (Designated_Type (Etype (Subp)));
5742 end if;
5743 end if;
5745 if Present (Comp_Type)
5746 and then Etype (Subprog) /= Comp_Type
5747 then
5748 Add_One_Interp (Subprog, Subp, Comp_Type);
5749 end if;
5751 if Etype (Call) /= Any_Type then
5752 return Subp;
5753 else
5754 return Empty;
5755 end if;
5756 end Valid_Candidate;
5758 -------------------------------
5759 -- Complete_Object_Operation --
5760 -------------------------------
5762 procedure Complete_Object_Operation
5763 (Call_Node : Node_Id;
5764 Node_To_Replace : Node_Id)
5766 Control : constant Entity_Id := First_Formal (Entity (Subprog));
5767 Formal_Type : constant Entity_Id := Etype (Control);
5768 First_Actual : Node_Id;
5770 begin
5771 -- Place the name of the operation, with its interpretations,
5772 -- on the rewritten call.
5774 Set_Name (Call_Node, Subprog);
5776 First_Actual := First (Parameter_Associations (Call_Node));
5778 -- For cross-reference purposes, treat the new node as being in
5779 -- the source if the original one is.
5781 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
5782 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
5784 if Nkind (N) = N_Selected_Component
5785 and then not Inside_A_Generic
5786 then
5787 Set_Entity (Selector_Name (N), Entity (Subprog));
5788 end if;
5790 -- If need be, rewrite first actual as an explicit dereference
5791 -- If the call is overloaded, the rewriting can only be done
5792 -- once the primitive operation is identified.
5794 if Is_Overloaded (Subprog) then
5796 -- The prefix itself may be overloaded, and its interpretations
5797 -- must be propagated to the new actual in the call.
5799 if Is_Overloaded (Obj) then
5800 Save_Interps (Obj, First_Actual);
5801 end if;
5803 Rewrite (First_Actual, Obj);
5805 elsif not Is_Access_Type (Formal_Type)
5806 and then Is_Access_Type (Etype (Obj))
5807 then
5808 Rewrite (First_Actual,
5809 Make_Explicit_Dereference (Sloc (Obj), Obj));
5810 Analyze (First_Actual);
5812 -- If we need to introduce an explicit dereference, verify that
5813 -- the resulting actual is compatible with the mode of the formal.
5815 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
5816 and then Is_Access_Constant (Etype (Obj))
5817 then
5818 Error_Msg_NE
5819 ("expect variable in call to&", Prefix (N), Entity (Subprog));
5820 end if;
5822 -- Conversely, if the formal is an access parameter and the object
5823 -- is not, replace the actual with a 'Access reference. Its analysis
5824 -- will check that the object is aliased.
5826 elsif Is_Access_Type (Formal_Type)
5827 and then not Is_Access_Type (Etype (Obj))
5828 then
5829 -- A special case: A.all'access is illegal if A is an access to a
5830 -- constant and the context requires an access to a variable.
5832 if not Is_Access_Constant (Formal_Type) then
5833 if (Nkind (Obj) = N_Explicit_Dereference
5834 and then Is_Access_Constant (Etype (Prefix (Obj))))
5835 or else not Is_Variable (Obj)
5836 then
5837 Error_Msg_NE
5838 ("actual for& must be a variable", Obj, Control);
5839 end if;
5840 end if;
5842 Rewrite (First_Actual,
5843 Make_Attribute_Reference (Loc,
5844 Attribute_Name => Name_Access,
5845 Prefix => Relocate_Node (Obj)));
5847 if not Is_Aliased_View (Obj) then
5848 Error_Msg_NE
5849 ("object in prefixed call to& must be aliased"
5850 & " (RM-2005 4.3.1 (13))",
5851 Prefix (First_Actual), Subprog);
5852 end if;
5854 Analyze (First_Actual);
5856 else
5857 if Is_Overloaded (Obj) then
5858 Save_Interps (Obj, First_Actual);
5859 end if;
5861 Rewrite (First_Actual, Obj);
5862 end if;
5864 Rewrite (Node_To_Replace, Call_Node);
5866 -- Propagate the interpretations collected in subprog to the new
5867 -- function call node, to be resolved from context.
5869 if Is_Overloaded (Subprog) then
5870 Save_Interps (Subprog, Node_To_Replace);
5871 else
5872 Analyze (Node_To_Replace);
5873 end if;
5874 end Complete_Object_Operation;
5876 ----------------------
5877 -- Report_Ambiguity --
5878 ----------------------
5880 procedure Report_Ambiguity (Op : Entity_Id) is
5881 Access_Formal : constant Boolean :=
5882 Is_Access_Type (Etype (First_Formal (Op)));
5883 Access_Actual : constant Boolean :=
5884 Is_Access_Type (Etype (Prefix (N)));
5886 begin
5887 Error_Msg_Sloc := Sloc (Op);
5889 if Access_Formal and then not Access_Actual then
5890 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5891 Error_Msg_N
5892 ("\possible interpretation"
5893 & " (inherited, with implicit 'Access) #", N);
5894 else
5895 Error_Msg_N
5896 ("\possible interpretation (with implicit 'Access) #", N);
5897 end if;
5899 elsif not Access_Formal and then Access_Actual then
5900 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5901 Error_Msg_N
5902 ("\possible interpretation"
5903 & " ( inherited, with implicit dereference) #", N);
5904 else
5905 Error_Msg_N
5906 ("\possible interpretation (with implicit dereference) #", N);
5907 end if;
5909 else
5910 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5911 Error_Msg_N ("\possible interpretation (inherited)#", N);
5912 else
5913 Error_Msg_N ("\possible interpretation#", N);
5914 end if;
5915 end if;
5916 end Report_Ambiguity;
5918 --------------------------------
5919 -- Transform_Object_Operation --
5920 --------------------------------
5922 procedure Transform_Object_Operation
5923 (Call_Node : out Node_Id;
5924 Node_To_Replace : out Node_Id)
5926 Dummy : constant Node_Id := New_Copy (Obj);
5927 -- Placeholder used as a first parameter in the call, replaced
5928 -- eventually by the proper object.
5930 Parent_Node : constant Node_Id := Parent (N);
5932 Actual : Node_Id;
5933 Actuals : List_Id;
5935 begin
5936 -- Common case covering 1) Call to a procedure and 2) Call to a
5937 -- function that has some additional actuals.
5939 if Nkind_In (Parent_Node, N_Function_Call,
5940 N_Procedure_Call_Statement)
5942 -- N is a selected component node containing the name of the
5943 -- subprogram. If N is not the name of the parent node we must
5944 -- not replace the parent node by the new construct. This case
5945 -- occurs when N is a parameterless call to a subprogram that
5946 -- is an actual parameter of a call to another subprogram. For
5947 -- example:
5948 -- Some_Subprogram (..., Obj.Operation, ...)
5950 and then Name (Parent_Node) = N
5951 then
5952 Node_To_Replace := Parent_Node;
5954 Actuals := Parameter_Associations (Parent_Node);
5956 if Present (Actuals) then
5957 Prepend (Dummy, Actuals);
5958 else
5959 Actuals := New_List (Dummy);
5960 end if;
5962 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
5963 Call_Node :=
5964 Make_Procedure_Call_Statement (Loc,
5965 Name => New_Copy (Subprog),
5966 Parameter_Associations => Actuals);
5968 else
5969 Call_Node :=
5970 Make_Function_Call (Loc,
5971 Name => New_Copy (Subprog),
5972 Parameter_Associations => Actuals);
5974 end if;
5976 -- Before analysis, a function call appears as an indexed component
5977 -- if there are no named associations.
5979 elsif Nkind (Parent_Node) = N_Indexed_Component
5980 and then N = Prefix (Parent_Node)
5981 then
5982 Node_To_Replace := Parent_Node;
5984 Actuals := Expressions (Parent_Node);
5986 Actual := First (Actuals);
5987 while Present (Actual) loop
5988 Analyze (Actual);
5989 Next (Actual);
5990 end loop;
5992 Prepend (Dummy, Actuals);
5994 Call_Node :=
5995 Make_Function_Call (Loc,
5996 Name => New_Copy (Subprog),
5997 Parameter_Associations => Actuals);
5999 -- Parameterless call: Obj.F is rewritten as F (Obj)
6001 else
6002 Node_To_Replace := N;
6004 Call_Node :=
6005 Make_Function_Call (Loc,
6006 Name => New_Copy (Subprog),
6007 Parameter_Associations => New_List (Dummy));
6008 end if;
6009 end Transform_Object_Operation;
6011 ------------------------------
6012 -- Try_Class_Wide_Operation --
6013 ------------------------------
6015 function Try_Class_Wide_Operation
6016 (Call_Node : Node_Id;
6017 Node_To_Replace : Node_Id) return Boolean
6019 Anc_Type : Entity_Id;
6020 Matching_Op : Entity_Id := Empty;
6021 Error : Boolean;
6023 procedure Traverse_Homonyms
6024 (Anc_Type : Entity_Id;
6025 Error : out Boolean);
6026 -- Traverse the homonym chain of the subprogram searching for those
6027 -- homonyms whose first formal has the Anc_Type's class-wide type,
6028 -- or an anonymous access type designating the class-wide type. If
6029 -- an ambiguity is detected, then Error is set to True.
6031 procedure Traverse_Interfaces
6032 (Anc_Type : Entity_Id;
6033 Error : out Boolean);
6034 -- Traverse the list of interfaces, if any, associated with Anc_Type
6035 -- and search for acceptable class-wide homonyms associated with each
6036 -- interface. If an ambiguity is detected, then Error is set to True.
6038 -----------------------
6039 -- Traverse_Homonyms --
6040 -----------------------
6042 procedure Traverse_Homonyms
6043 (Anc_Type : Entity_Id;
6044 Error : out Boolean)
6046 Cls_Type : Entity_Id;
6047 Hom : Entity_Id;
6048 Hom_Ref : Node_Id;
6049 Success : Boolean;
6051 begin
6052 Error := False;
6054 Cls_Type := Class_Wide_Type (Anc_Type);
6056 Hom := Current_Entity (Subprog);
6058 -- Find operation whose first parameter is of the class-wide
6059 -- type, a subtype thereof, or an anonymous access to same.
6061 while Present (Hom) loop
6062 if (Ekind (Hom) = E_Procedure
6063 or else
6064 Ekind (Hom) = E_Function)
6065 and then Scope (Hom) = Scope (Anc_Type)
6066 and then Present (First_Formal (Hom))
6067 and then
6068 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6069 or else
6070 (Is_Access_Type (Etype (First_Formal (Hom)))
6071 and then
6072 Ekind (Etype (First_Formal (Hom))) =
6073 E_Anonymous_Access_Type
6074 and then
6075 Base_Type
6076 (Designated_Type (Etype (First_Formal (Hom)))) =
6077 Cls_Type))
6078 then
6079 Set_Etype (Call_Node, Any_Type);
6080 Set_Is_Overloaded (Call_Node, False);
6081 Success := False;
6083 if No (Matching_Op) then
6084 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
6085 Set_Etype (Call_Node, Any_Type);
6086 Set_Parent (Call_Node, Parent (Node_To_Replace));
6088 Set_Name (Call_Node, Hom_Ref);
6090 Analyze_One_Call
6091 (N => Call_Node,
6092 Nam => Hom,
6093 Report => Report_Error,
6094 Success => Success,
6095 Skip_First => True);
6097 Matching_Op :=
6098 Valid_Candidate (Success, Call_Node, Hom);
6100 else
6101 Analyze_One_Call
6102 (N => Call_Node,
6103 Nam => Hom,
6104 Report => Report_Error,
6105 Success => Success,
6106 Skip_First => True);
6108 if Present (Valid_Candidate (Success, Call_Node, Hom))
6109 and then Nkind (Call_Node) /= N_Function_Call
6110 then
6111 Error_Msg_NE ("ambiguous call to&", N, Hom);
6112 Report_Ambiguity (Matching_Op);
6113 Report_Ambiguity (Hom);
6114 Error := True;
6115 return;
6116 end if;
6117 end if;
6118 end if;
6120 Hom := Homonym (Hom);
6121 end loop;
6122 end Traverse_Homonyms;
6124 -------------------------
6125 -- Traverse_Interfaces --
6126 -------------------------
6128 procedure Traverse_Interfaces
6129 (Anc_Type : Entity_Id;
6130 Error : out Boolean)
6132 Intface_List : constant List_Id :=
6133 Abstract_Interface_List (Anc_Type);
6134 Intface : Node_Id;
6136 begin
6137 Error := False;
6139 if Is_Non_Empty_List (Intface_List) then
6140 Intface := First (Intface_List);
6141 while Present (Intface) loop
6143 -- Look for acceptable class-wide homonyms associated with
6144 -- the interface.
6146 Traverse_Homonyms (Etype (Intface), Error);
6148 if Error then
6149 return;
6150 end if;
6152 -- Continue the search by looking at each of the interface's
6153 -- associated interface ancestors.
6155 Traverse_Interfaces (Etype (Intface), Error);
6157 if Error then
6158 return;
6159 end if;
6161 Next (Intface);
6162 end loop;
6163 end if;
6164 end Traverse_Interfaces;
6166 -- Start of processing for Try_Class_Wide_Operation
6168 begin
6169 -- Loop through ancestor types (including interfaces), traversing
6170 -- the homonym chain of the subprogram, trying out those homonyms
6171 -- whose first formal has the class-wide type of the ancestor, or
6172 -- an anonymous access type designating the class-wide type.
6174 Anc_Type := Obj_Type;
6175 loop
6176 -- Look for a match among homonyms associated with the ancestor
6178 Traverse_Homonyms (Anc_Type, Error);
6180 if Error then
6181 return True;
6182 end if;
6184 -- Continue the search for matches among homonyms associated with
6185 -- any interfaces implemented by the ancestor.
6187 Traverse_Interfaces (Anc_Type, Error);
6189 if Error then
6190 return True;
6191 end if;
6193 exit when Etype (Anc_Type) = Anc_Type;
6194 Anc_Type := Etype (Anc_Type);
6195 end loop;
6197 if Present (Matching_Op) then
6198 Set_Etype (Call_Node, Etype (Matching_Op));
6199 end if;
6201 return Present (Matching_Op);
6202 end Try_Class_Wide_Operation;
6204 -----------------------------------
6205 -- Try_One_Prefix_Interpretation --
6206 -----------------------------------
6208 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
6209 begin
6210 Obj_Type := T;
6212 if Is_Access_Type (Obj_Type) then
6213 Obj_Type := Designated_Type (Obj_Type);
6214 end if;
6216 if Ekind (Obj_Type) = E_Private_Subtype then
6217 Obj_Type := Base_Type (Obj_Type);
6218 end if;
6220 if Is_Class_Wide_Type (Obj_Type) then
6221 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
6222 end if;
6224 -- The type may have be obtained through a limited_with clause,
6225 -- in which case the primitive operations are available on its
6226 -- non-limited view. If still incomplete, retrieve full view.
6228 if Ekind (Obj_Type) = E_Incomplete_Type
6229 and then From_With_Type (Obj_Type)
6230 then
6231 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
6232 end if;
6234 -- If the object is not tagged, or the type is still an incomplete
6235 -- type, this is not a prefixed call.
6237 if not Is_Tagged_Type (Obj_Type)
6238 or else Is_Incomplete_Type (Obj_Type)
6239 then
6240 return;
6241 end if;
6243 if Try_Primitive_Operation
6244 (Call_Node => New_Call_Node,
6245 Node_To_Replace => Node_To_Replace)
6246 or else
6247 Try_Class_Wide_Operation
6248 (Call_Node => New_Call_Node,
6249 Node_To_Replace => Node_To_Replace)
6250 then
6251 null;
6252 end if;
6253 end Try_One_Prefix_Interpretation;
6255 -----------------------------
6256 -- Try_Primitive_Operation --
6257 -----------------------------
6259 function Try_Primitive_Operation
6260 (Call_Node : Node_Id;
6261 Node_To_Replace : Node_Id) return Boolean
6263 Elmt : Elmt_Id;
6264 Prim_Op : Entity_Id;
6265 Matching_Op : Entity_Id := Empty;
6266 Prim_Op_Ref : Node_Id := Empty;
6268 Corr_Type : Entity_Id := Empty;
6269 -- If the prefix is a synchronized type, the controlling type of
6270 -- the primitive operation is the corresponding record type, else
6271 -- this is the object type itself.
6273 Success : Boolean := False;
6275 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
6276 -- For tagged types the candidate interpretations are found in
6277 -- the list of primitive operations of the type and its ancestors.
6278 -- For formal tagged types we have to find the operations declared
6279 -- in the same scope as the type (including in the generic formal
6280 -- part) because the type itself carries no primitive operations,
6281 -- except for formal derived types that inherit the operations of
6282 -- the parent and progenitors.
6283 -- If the context is a generic subprogram body, the generic formals
6284 -- are visible by name, but are not in the entity list of the
6285 -- subprogram because that list starts with the subprogram formals.
6286 -- We retrieve the candidate operations from the generic declaration.
6288 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
6289 -- Verify that the prefix, dereferenced if need be, is a valid
6290 -- controlling argument in a call to Op. The remaining actuals
6291 -- are checked in the subsequent call to Analyze_One_Call.
6293 ------------------------------
6294 -- Collect_Generic_Type_Ops --
6295 ------------------------------
6297 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
6298 Bas : constant Entity_Id := Base_Type (T);
6299 Candidates : constant Elist_Id := New_Elmt_List;
6300 Subp : Entity_Id;
6301 Formal : Entity_Id;
6303 procedure Check_Candidate;
6304 -- The operation is a candidate if its first parameter is a
6305 -- controlling operand of the desired type.
6307 -----------------------
6308 -- Check_Candidate; --
6309 -----------------------
6311 procedure Check_Candidate is
6312 begin
6313 Formal := First_Formal (Subp);
6315 if Present (Formal)
6316 and then Is_Controlling_Formal (Formal)
6317 and then
6318 (Base_Type (Etype (Formal)) = Bas
6319 or else
6320 (Is_Access_Type (Etype (Formal))
6321 and then Designated_Type (Etype (Formal)) = Bas))
6322 then
6323 Append_Elmt (Subp, Candidates);
6324 end if;
6325 end Check_Candidate;
6327 -- Start of processing for Collect_Generic_Type_Ops
6329 begin
6330 if Is_Derived_Type (T) then
6331 return Primitive_Operations (T);
6333 elsif Ekind (Scope (T)) = E_Procedure
6334 or else Ekind (Scope (T)) = E_Function
6335 then
6336 -- Scan the list of generic formals to find subprograms
6337 -- that may have a first controlling formal of the type.
6339 declare
6340 Decl : Node_Id;
6342 begin
6343 Decl :=
6344 First (Generic_Formal_Declarations
6345 (Unit_Declaration_Node (Scope (T))));
6346 while Present (Decl) loop
6347 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
6348 Subp := Defining_Entity (Decl);
6349 Check_Candidate;
6350 end if;
6352 Next (Decl);
6353 end loop;
6354 end;
6356 return Candidates;
6358 else
6359 -- Scan the list of entities declared in the same scope as
6360 -- the type. In general this will be an open scope, given that
6361 -- the call we are analyzing can only appear within a generic
6362 -- declaration or body (either the one that declares T, or a
6363 -- child unit).
6365 Subp := First_Entity (Scope (T));
6366 while Present (Subp) loop
6367 if Is_Overloadable (Subp) then
6368 Check_Candidate;
6369 end if;
6371 Next_Entity (Subp);
6372 end loop;
6374 return Candidates;
6375 end if;
6376 end Collect_Generic_Type_Ops;
6378 -----------------------------
6379 -- Valid_First_Argument_Of --
6380 -----------------------------
6382 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
6383 Typ : constant Entity_Id := Etype (First_Formal (Op));
6385 begin
6386 -- Simple case. Object may be a subtype of the tagged type or
6387 -- may be the corresponding record of a synchronized type.
6389 return Obj_Type = Typ
6390 or else Base_Type (Obj_Type) = Typ
6391 or else Corr_Type = Typ
6393 -- Prefix can be dereferenced
6395 or else
6396 (Is_Access_Type (Corr_Type)
6397 and then Designated_Type (Corr_Type) = Typ)
6399 -- Formal is an access parameter, for which the object
6400 -- can provide an access.
6402 or else
6403 (Ekind (Typ) = E_Anonymous_Access_Type
6404 and then Designated_Type (Typ) = Base_Type (Corr_Type));
6405 end Valid_First_Argument_Of;
6407 -- Start of processing for Try_Primitive_Operation
6409 begin
6410 -- Look for subprograms in the list of primitive operations. The name
6411 -- must be identical, and the kind of call indicates the expected
6412 -- kind of operation (function or procedure). If the type is a
6413 -- (tagged) synchronized type, the primitive ops are attached to the
6414 -- corresponding record (base) type.
6416 if Is_Concurrent_Type (Obj_Type) then
6417 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
6418 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
6420 elsif not Is_Generic_Type (Obj_Type) then
6421 Corr_Type := Obj_Type;
6422 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
6424 else
6425 Corr_Type := Obj_Type;
6426 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
6427 end if;
6429 while Present (Elmt) loop
6430 Prim_Op := Node (Elmt);
6432 if Chars (Prim_Op) = Chars (Subprog)
6433 and then Present (First_Formal (Prim_Op))
6434 and then Valid_First_Argument_Of (Prim_Op)
6435 and then
6436 (Nkind (Call_Node) = N_Function_Call)
6437 = (Ekind (Prim_Op) = E_Function)
6438 then
6439 -- Ada 2005 (AI-251): If this primitive operation corresponds
6440 -- with an immediate ancestor interface there is no need to add
6441 -- it to the list of interpretations; the corresponding aliased
6442 -- primitive is also in this list of primitive operations and
6443 -- will be used instead.
6445 if (Present (Interface_Alias (Prim_Op))
6446 and then Is_Ancestor (Find_Dispatching_Type
6447 (Alias (Prim_Op)), Corr_Type))
6448 or else
6450 -- Do not consider hidden primitives unless the type is
6451 -- in an open scope or we are within an instance, where
6452 -- visibility is known to be correct.
6454 (Is_Hidden (Prim_Op)
6455 and then not Is_Immediately_Visible (Obj_Type)
6456 and then not In_Instance)
6457 then
6458 goto Continue;
6459 end if;
6461 Set_Etype (Call_Node, Any_Type);
6462 Set_Is_Overloaded (Call_Node, False);
6464 if No (Matching_Op) then
6465 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
6466 Candidate := Prim_Op;
6468 Set_Parent (Call_Node, Parent (Node_To_Replace));
6470 Set_Name (Call_Node, Prim_Op_Ref);
6471 Success := False;
6473 Analyze_One_Call
6474 (N => Call_Node,
6475 Nam => Prim_Op,
6476 Report => Report_Error,
6477 Success => Success,
6478 Skip_First => True);
6480 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
6482 -- More than one interpretation, collect for subsequent
6483 -- disambiguation. If this is a procedure call and there
6484 -- is another match, report ambiguity now.
6486 else
6487 Analyze_One_Call
6488 (N => Call_Node,
6489 Nam => Prim_Op,
6490 Report => Report_Error,
6491 Success => Success,
6492 Skip_First => True);
6494 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
6495 and then Nkind (Call_Node) /= N_Function_Call
6496 then
6497 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
6498 Report_Ambiguity (Matching_Op);
6499 Report_Ambiguity (Prim_Op);
6500 return True;
6501 end if;
6502 end if;
6503 end if;
6505 <<Continue>>
6506 Next_Elmt (Elmt);
6507 end loop;
6509 if Present (Matching_Op) then
6510 Set_Etype (Call_Node, Etype (Matching_Op));
6511 end if;
6513 return Present (Matching_Op);
6514 end Try_Primitive_Operation;
6516 -- Start of processing for Try_Object_Operation
6518 begin
6519 Analyze_Expression (Obj);
6521 -- Analyze the actuals if node is known to be a subprogram call
6523 if Is_Subprg_Call and then N = Name (Parent (N)) then
6524 Actual := First (Parameter_Associations (Parent (N)));
6525 while Present (Actual) loop
6526 Analyze_Expression (Actual);
6527 Next (Actual);
6528 end loop;
6529 end if;
6531 -- Build a subprogram call node, using a copy of Obj as its first
6532 -- actual. This is a placeholder, to be replaced by an explicit
6533 -- dereference when needed.
6535 Transform_Object_Operation
6536 (Call_Node => New_Call_Node,
6537 Node_To_Replace => Node_To_Replace);
6539 Set_Etype (New_Call_Node, Any_Type);
6540 Set_Etype (Subprog, Any_Type);
6541 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
6543 if not Is_Overloaded (Obj) then
6544 Try_One_Prefix_Interpretation (Obj_Type);
6546 else
6547 declare
6548 I : Interp_Index;
6549 It : Interp;
6550 begin
6551 Get_First_Interp (Obj, I, It);
6552 while Present (It.Nam) loop
6553 Try_One_Prefix_Interpretation (It.Typ);
6554 Get_Next_Interp (I, It);
6555 end loop;
6556 end;
6557 end if;
6559 if Etype (New_Call_Node) /= Any_Type then
6560 Complete_Object_Operation
6561 (Call_Node => New_Call_Node,
6562 Node_To_Replace => Node_To_Replace);
6563 return True;
6565 elsif Present (Candidate) then
6567 -- The argument list is not type correct. Re-analyze with error
6568 -- reporting enabled, and use one of the possible candidates.
6569 -- In All_Errors_Mode, re-analyze all failed interpretations.
6571 if All_Errors_Mode then
6572 Report_Error := True;
6573 if Try_Primitive_Operation
6574 (Call_Node => New_Call_Node,
6575 Node_To_Replace => Node_To_Replace)
6577 or else
6578 Try_Class_Wide_Operation
6579 (Call_Node => New_Call_Node,
6580 Node_To_Replace => Node_To_Replace)
6581 then
6582 null;
6583 end if;
6585 else
6586 Analyze_One_Call
6587 (N => New_Call_Node,
6588 Nam => Candidate,
6589 Report => True,
6590 Success => Success,
6591 Skip_First => True);
6592 end if;
6594 -- No need for further errors
6596 return True;
6598 else
6599 -- There was no candidate operation, so report it as an error
6600 -- in the caller: Analyze_Selected_Component.
6602 return False;
6603 end if;
6604 end Try_Object_Operation;
6606 ---------
6607 -- wpo --
6608 ---------
6610 procedure wpo (T : Entity_Id) is
6611 Op : Entity_Id;
6612 E : Elmt_Id;
6614 begin
6615 if not Is_Tagged_Type (T) then
6616 return;
6617 end if;
6619 E := First_Elmt (Primitive_Operations (Base_Type (T)));
6620 while Present (E) loop
6621 Op := Node (E);
6622 Write_Int (Int (Op));
6623 Write_Str (" === ");
6624 Write_Name (Chars (Op));
6625 Write_Str (" in ");
6626 Write_Name (Chars (Scope (Op)));
6627 Next_Elmt (E);
6628 Write_Eol;
6629 end loop;
6630 end wpo;
6632 end Sem_Ch4;