PR middle-end/66867
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob66a2acf6ca02b1ede7c3bff262b56766a84e3fe5
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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Itypes; use Itypes;
35 with Lib; use Lib;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Namet.Sp; use Namet.Sp;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
66 package body Sem_Ch4 is
68 -- Tables which speed up the identification of dangerous calls to Ada 2012
69 -- functions with writable actuals (AI05-0144).
71 -- The following table enumerates the Ada constructs which may evaluate in
72 -- arbitrary order. It does not cover all the language constructs which can
73 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
75 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
76 (N_Aggregate => True,
77 N_Assignment_Statement => True,
78 N_Entry_Call_Statement => True,
79 N_Extension_Aggregate => True,
80 N_Full_Type_Declaration => True,
81 N_Indexed_Component => True,
82 N_Object_Declaration => True,
83 N_Pragma => True,
84 N_Range => True,
85 N_Slice => True,
86 N_Array_Type_Definition => True,
87 N_Membership_Test => True,
88 N_Binary_Op => True,
89 N_Subprogram_Call => True,
90 others => False);
92 -- The following table enumerates the nodes on which we stop climbing when
93 -- locating the outermost Ada construct that can be evaluated in arbitrary
94 -- order.
96 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
97 (N_Aggregate => True,
98 N_Assignment_Statement => True,
99 N_Entry_Call_Statement => True,
100 N_Extended_Return_Statement => True,
101 N_Extension_Aggregate => True,
102 N_Full_Type_Declaration => True,
103 N_Object_Declaration => True,
104 N_Object_Renaming_Declaration => True,
105 N_Package_Specification => True,
106 N_Pragma => True,
107 N_Procedure_Call_Statement => True,
108 N_Simple_Return_Statement => True,
109 N_Has_Condition => True,
110 others => False);
112 -----------------------
113 -- Local Subprograms --
114 -----------------------
116 procedure Analyze_Concatenation_Rest (N : Node_Id);
117 -- Does the "rest" of the work of Analyze_Concatenation, after the left
118 -- operand has been analyzed. See Analyze_Concatenation for details.
120 procedure Analyze_Expression (N : Node_Id);
121 -- For expressions that are not names, this is just a call to analyze. If
122 -- the expression is a name, it may be a call to a parameterless function,
123 -- and if so must be converted into an explicit call node and analyzed as
124 -- such. This deproceduring must be done during the first pass of overload
125 -- resolution, because otherwise a procedure call with overloaded actuals
126 -- may fail to resolve.
128 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
129 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
130 -- operator name or an expanded name whose selector is an operator name,
131 -- and one possible interpretation is as a predefined operator.
133 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
134 -- If the prefix of a selected_component is overloaded, the proper
135 -- interpretation that yields a record type with the proper selector
136 -- name must be selected.
138 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
139 -- Procedure to analyze a user defined binary operator, which is resolved
140 -- like a function, but instead of a list of actuals it is presented
141 -- with the left and right operands of an operator node.
143 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
144 -- Procedure to analyze a user defined unary operator, which is resolved
145 -- like a function, but instead of a list of actuals, it is presented with
146 -- the operand of the operator node.
148 procedure Ambiguous_Operands (N : Node_Id);
149 -- For equality, membership, and comparison operators with overloaded
150 -- arguments, list possible interpretations.
152 procedure Analyze_One_Call
153 (N : Node_Id;
154 Nam : Entity_Id;
155 Report : Boolean;
156 Success : out Boolean;
157 Skip_First : Boolean := False);
158 -- Check one interpretation of an overloaded subprogram name for
159 -- compatibility with the types of the actuals in a call. If there is a
160 -- single interpretation which does not match, post error if Report is
161 -- set to True.
163 -- Nam is the entity that provides the formals against which the actuals
164 -- are checked. Nam is either the name of a subprogram, or the internal
165 -- subprogram type constructed for an access_to_subprogram. If the actuals
166 -- are compatible with Nam, then Nam is added to the list of candidate
167 -- interpretations for N, and Success is set to True.
169 -- The flag Skip_First is used when analyzing a call that was rewritten
170 -- from object notation. In this case the first actual may have to receive
171 -- an explicit dereference, depending on the first formal of the operation
172 -- being called. The caller will have verified that the object is legal
173 -- for the call. If the remaining parameters match, the first parameter
174 -- will rewritten as a dereference if needed, prior to completing analysis.
176 procedure Check_Misspelled_Selector
177 (Prefix : Entity_Id;
178 Sel : Node_Id);
179 -- Give possible misspelling message if Sel seems likely to be a mis-
180 -- spelling of one of the selectors of the Prefix. This is called by
181 -- Analyze_Selected_Component after producing an invalid selector error
182 -- message.
184 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
185 -- Verify that type T is declared in scope S. Used to find interpretations
186 -- for operators given by expanded names. This is abstracted as a separate
187 -- function to handle extensions to System, where S is System, but T is
188 -- declared in the extension.
190 procedure Find_Arithmetic_Types
191 (L, R : Node_Id;
192 Op_Id : Entity_Id;
193 N : Node_Id);
194 -- L and R are the operands of an arithmetic operator. Find consistent
195 -- pairs of interpretations for L and R that have a numeric type consistent
196 -- with the semantics of the operator.
198 procedure Find_Comparison_Types
199 (L, R : Node_Id;
200 Op_Id : Entity_Id;
201 N : Node_Id);
202 -- L and R are operands of a comparison operator. Find consistent pairs of
203 -- interpretations for L and R.
205 procedure Find_Concatenation_Types
206 (L, R : Node_Id;
207 Op_Id : Entity_Id;
208 N : Node_Id);
209 -- For the four varieties of concatenation
211 procedure Find_Equality_Types
212 (L, R : Node_Id;
213 Op_Id : Entity_Id;
214 N : Node_Id);
215 -- Ditto for equality operators
217 procedure Find_Boolean_Types
218 (L, R : Node_Id;
219 Op_Id : Entity_Id;
220 N : Node_Id);
221 -- Ditto for binary logical operations
223 procedure Find_Negation_Types
224 (R : Node_Id;
225 Op_Id : Entity_Id;
226 N : Node_Id);
227 -- Find consistent interpretation for operand of negation operator
229 procedure Find_Non_Universal_Interpretations
230 (N : Node_Id;
231 R : Node_Id;
232 Op_Id : Entity_Id;
233 T1 : Entity_Id);
234 -- For equality and comparison operators, the result is always boolean,
235 -- and the legality of the operation is determined from the visibility
236 -- of the operand types. If one of the operands has a universal interpre-
237 -- tation, the legality check uses some compatible non-universal
238 -- interpretation of the other operand. N can be an operator node, or
239 -- a function call whose name is an operator designator. Any_Access, which
240 -- is the initial type of the literal NULL, is a universal type for the
241 -- purpose of this routine.
243 function Find_Primitive_Operation (N : Node_Id) return Boolean;
244 -- Find candidate interpretations for the name Obj.Proc when it appears
245 -- in a subprogram renaming declaration.
247 procedure Find_Unary_Types
248 (R : Node_Id;
249 Op_Id : Entity_Id;
250 N : Node_Id);
251 -- Unary arithmetic types: plus, minus, abs
253 procedure Check_Arithmetic_Pair
254 (T1, T2 : Entity_Id;
255 Op_Id : Entity_Id;
256 N : Node_Id);
257 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
258 -- for left and right operand. Determine whether they constitute a valid
259 -- pair for the given operator, and record the corresponding interpretation
260 -- of the operator node. The node N may be an operator node (the usual
261 -- case) or a function call whose prefix is an operator designator. In
262 -- both cases Op_Id is the operator name itself.
264 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
265 -- Give detailed information on overloaded call where none of the
266 -- interpretations match. N is the call node, Nam the designator for
267 -- the overloaded entity being called.
269 function Junk_Operand (N : Node_Id) return Boolean;
270 -- Test for an operand that is an inappropriate entity (e.g. a package
271 -- name or a label). If so, issue an error message and return True. If
272 -- the operand is not an inappropriate entity kind, return False.
274 procedure Operator_Check (N : Node_Id);
275 -- Verify that an operator has received some valid interpretation. If none
276 -- was found, determine whether a use clause would make the operation
277 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
278 -- every type compatible with the operator, even if the operator for the
279 -- type is not directly visible. The routine uses this type to emit a more
280 -- informative message.
282 function Process_Implicit_Dereference_Prefix
283 (E : Entity_Id;
284 P : Node_Id) return Entity_Id;
285 -- Called when P is the prefix of an implicit dereference, denoting an
286 -- object E. The function returns the designated type of the prefix, taking
287 -- into account that the designated type of an anonymous access type may be
288 -- a limited view, when the non-limited view is visible.
290 -- If in semantics only mode (-gnatc or generic), the function also records
291 -- that the prefix is a reference to E, if any. Normally, such a reference
292 -- is generated only when the implicit dereference is expanded into an
293 -- explicit one, but for consistency we must generate the reference when
294 -- expansion is disabled as well.
296 procedure Remove_Abstract_Operations (N : Node_Id);
297 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
298 -- operation is not a candidate interpretation.
300 function Try_Container_Indexing
301 (N : Node_Id;
302 Prefix : Node_Id;
303 Exprs : List_Id) return Boolean;
304 -- AI05-0139: Generalized indexing to support iterators over containers
306 function Try_Indexed_Call
307 (N : Node_Id;
308 Nam : Entity_Id;
309 Typ : Entity_Id;
310 Skip_First : Boolean) return Boolean;
311 -- If a function has defaults for all its actuals, a call to it may in fact
312 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
313 -- interpretation as an indexing, prior to analysis as a call. If both are
314 -- possible, the node is overloaded with both interpretations (same symbol
315 -- but two different types). If the call is written in prefix form, the
316 -- prefix becomes the first parameter in the call, and only the remaining
317 -- actuals must be checked for the presence of defaults.
319 function Try_Indirect_Call
320 (N : Node_Id;
321 Nam : Entity_Id;
322 Typ : Entity_Id) return Boolean;
323 -- Similarly, a function F that needs no actuals can return an access to a
324 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
325 -- the call may be overloaded with both interpretations.
327 function Try_Object_Operation
328 (N : Node_Id;
329 CW_Test_Only : Boolean := False) return Boolean;
330 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
331 -- is a call in this notation, it is transformed into a normal subprogram
332 -- call where the prefix is a parameter, and True is returned. If node
333 -- N is not of this form, it is unchanged, and False is returned. If
334 -- CW_Test_Only is true then N is an N_Selected_Component node which
335 -- is part of a call to an entry or procedure of a tagged concurrent
336 -- type and this routine is invoked to search for class-wide subprograms
337 -- conflicting with the target entity.
339 procedure wpo (T : Entity_Id);
340 pragma Warnings (Off, wpo);
341 -- Used for debugging: obtain list of primitive operations even if
342 -- type is not frozen and dispatch table is not built yet.
344 ------------------------
345 -- Ambiguous_Operands --
346 ------------------------
348 procedure Ambiguous_Operands (N : Node_Id) is
349 procedure List_Operand_Interps (Opnd : Node_Id);
351 --------------------------
352 -- List_Operand_Interps --
353 --------------------------
355 procedure List_Operand_Interps (Opnd : Node_Id) is
356 Nam : Node_Id;
357 Err : Node_Id := N;
359 begin
360 if Is_Overloaded (Opnd) then
361 if Nkind (Opnd) in N_Op then
362 Nam := Opnd;
364 elsif Nkind (Opnd) = N_Function_Call then
365 Nam := Name (Opnd);
367 elsif Ada_Version >= Ada_2012 then
368 declare
369 It : Interp;
370 I : Interp_Index;
372 begin
373 Get_First_Interp (Opnd, I, It);
374 while Present (It.Nam) loop
375 if Has_Implicit_Dereference (It.Typ) then
376 Error_Msg_N
377 ("can be interpreted as implicit dereference", Opnd);
378 return;
379 end if;
381 Get_Next_Interp (I, It);
382 end loop;
383 end;
385 return;
386 end if;
388 else
389 return;
390 end if;
392 if Opnd = Left_Opnd (N) then
393 Error_Msg_N
394 ("\left operand has the following interpretations", N);
395 else
396 Error_Msg_N
397 ("\right operand has the following interpretations", N);
398 Err := Opnd;
399 end if;
401 List_Interps (Nam, Err);
402 end List_Operand_Interps;
404 -- Start of processing for Ambiguous_Operands
406 begin
407 if Nkind (N) in N_Membership_Test then
408 Error_Msg_N ("ambiguous operands for membership", N);
410 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
411 Error_Msg_N ("ambiguous operands for equality", N);
413 else
414 Error_Msg_N ("ambiguous operands for comparison", N);
415 end if;
417 if All_Errors_Mode then
418 List_Operand_Interps (Left_Opnd (N));
419 List_Operand_Interps (Right_Opnd (N));
420 else
421 Error_Msg_N ("\use -gnatf switch for details", N);
422 end if;
423 end Ambiguous_Operands;
425 -----------------------
426 -- Analyze_Aggregate --
427 -----------------------
429 -- Most of the analysis of Aggregates requires that the type be known,
430 -- and is therefore put off until resolution.
432 procedure Analyze_Aggregate (N : Node_Id) is
433 begin
434 if No (Etype (N)) then
435 Set_Etype (N, Any_Composite);
436 end if;
437 end Analyze_Aggregate;
439 -----------------------
440 -- Analyze_Allocator --
441 -----------------------
443 procedure Analyze_Allocator (N : Node_Id) is
444 Loc : constant Source_Ptr := Sloc (N);
445 Sav_Errs : constant Nat := Serious_Errors_Detected;
446 E : Node_Id := Expression (N);
447 Acc_Type : Entity_Id;
448 Type_Id : Entity_Id;
449 P : Node_Id;
450 C : Node_Id;
451 Onode : Node_Id;
453 begin
454 Check_SPARK_05_Restriction ("allocator is not allowed", N);
456 -- Deal with allocator restrictions
458 -- In accordance with H.4(7), the No_Allocators restriction only applies
459 -- to user-written allocators. The same consideration applies to the
460 -- No_Standard_Allocators_Before_Elaboration restriction.
462 if Comes_From_Source (N) then
463 Check_Restriction (No_Allocators, N);
465 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
466 -- look at enclosing context, checking task/main subprogram case.
468 C := N;
469 P := Parent (C);
470 while Present (P) loop
472 -- For the task case we need a handled sequence of statements,
473 -- where the occurrence of the allocator is within the statements
474 -- and the parent is a task body
476 if Nkind (P) = N_Handled_Sequence_Of_Statements
477 and then Is_List_Member (C)
478 and then List_Containing (C) = Statements (P)
479 then
480 Onode := Original_Node (Parent (P));
482 -- Check for allocator within task body, this is a definite
483 -- violation of No_Allocators_After_Elaboration we can detect
484 -- at compile time.
486 if Nkind (Onode) = N_Task_Body then
487 Check_Restriction
488 (No_Standard_Allocators_After_Elaboration, N);
489 exit;
490 end if;
491 end if;
493 -- The other case is appearance in a subprogram body. This is
494 -- a violation if this is a library level subprogram with no
495 -- parameters. Note that this is now a static error even if the
496 -- subprogram is not the main program (this is a change, in an
497 -- earlier version only the main program was affected, and the
498 -- check had to be done in the binder.
500 if Nkind (P) = N_Subprogram_Body
501 and then Nkind (Parent (P)) = N_Compilation_Unit
502 and then No (Parameter_Specifications (Specification (P)))
503 then
504 Check_Restriction
505 (No_Standard_Allocators_After_Elaboration, N);
506 end if;
508 C := P;
509 P := Parent (C);
510 end loop;
511 end if;
513 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
514 -- any. The expected type for the name is any type. A non-overloading
515 -- rule then requires it to be of a type descended from
516 -- System.Storage_Pools.Subpools.Subpool_Handle.
518 -- This isn't exactly what the AI says, but it seems to be the right
519 -- rule. The AI should be fixed.???
521 declare
522 Subpool : constant Node_Id := Subpool_Handle_Name (N);
524 begin
525 if Present (Subpool) then
526 Analyze (Subpool);
528 if Is_Overloaded (Subpool) then
529 Error_Msg_N ("ambiguous subpool handle", Subpool);
530 end if;
532 -- Check that Etype (Subpool) is descended from Subpool_Handle
534 Resolve (Subpool);
535 end if;
536 end;
538 -- Analyze the qualified expression or subtype indication
540 if Nkind (E) = N_Qualified_Expression then
541 Acc_Type := Create_Itype (E_Allocator_Type, N);
542 Set_Etype (Acc_Type, Acc_Type);
543 Find_Type (Subtype_Mark (E));
545 -- Analyze the qualified expression, and apply the name resolution
546 -- rule given in 4.7(3).
548 Analyze (E);
549 Type_Id := Etype (E);
550 Set_Directly_Designated_Type (Acc_Type, Type_Id);
552 -- A qualified expression requires an exact match of the type,
553 -- class-wide matching is not allowed.
555 -- if Is_Class_Wide_Type (Type_Id)
556 -- and then Base_Type
557 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
558 -- then
559 -- Wrong_Type (Expression (E), Type_Id);
560 -- end if;
562 -- We don't analyze the qualified expression itself because it's
563 -- part of the allocator. It is fully analyzed and resolved when
564 -- the allocator is resolved with the context type.
566 Set_Etype (E, Type_Id);
568 -- Case where allocator has a subtype indication
570 else
571 declare
572 Def_Id : Entity_Id;
573 Base_Typ : Entity_Id;
575 begin
576 -- If the allocator includes a N_Subtype_Indication then a
577 -- constraint is present, otherwise the node is a subtype mark.
578 -- Introduce an explicit subtype declaration into the tree
579 -- defining some anonymous subtype and rewrite the allocator to
580 -- use this subtype rather than the subtype indication.
582 -- It is important to introduce the explicit subtype declaration
583 -- so that the bounds of the subtype indication are attached to
584 -- the tree in case the allocator is inside a generic unit.
586 -- Finally, if there is no subtype indication and the type is
587 -- a tagged unconstrained type with discriminants, the designated
588 -- object is constrained by their default values, and it is
589 -- simplest to introduce an explicit constraint now. In some cases
590 -- this is done during expansion, but freeze actions are certain
591 -- to be emitted in the proper order if constraint is explicit.
593 if Is_Entity_Name (E) and then Expander_Active then
594 Find_Type (E);
595 Type_Id := Entity (E);
597 if Is_Tagged_Type (Type_Id)
598 and then Has_Discriminants (Type_Id)
599 and then not Is_Constrained (Type_Id)
600 and then
601 Present
602 (Discriminant_Default_Value
603 (First_Discriminant (Type_Id)))
604 then
605 declare
606 Constr : constant List_Id := New_List;
607 Loc : constant Source_Ptr := Sloc (E);
608 Discr : Entity_Id := First_Discriminant (Type_Id);
610 begin
611 if Present (Discriminant_Default_Value (Discr)) then
612 while Present (Discr) loop
613 Append (Discriminant_Default_Value (Discr), Constr);
614 Next_Discriminant (Discr);
615 end loop;
617 Rewrite (E,
618 Make_Subtype_Indication (Loc,
619 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
620 Constraint =>
621 Make_Index_Or_Discriminant_Constraint (Loc,
622 Constraints => Constr)));
623 end if;
624 end;
625 end if;
626 end if;
628 if Nkind (E) = N_Subtype_Indication then
630 -- A constraint is only allowed for a composite type in Ada
631 -- 95. In Ada 83, a constraint is also allowed for an
632 -- access-to-composite type, but the constraint is ignored.
634 Find_Type (Subtype_Mark (E));
635 Base_Typ := Entity (Subtype_Mark (E));
637 if Is_Elementary_Type (Base_Typ) then
638 if not (Ada_Version = Ada_83
639 and then Is_Access_Type (Base_Typ))
640 then
641 Error_Msg_N ("constraint not allowed here", E);
643 if Nkind (Constraint (E)) =
644 N_Index_Or_Discriminant_Constraint
645 then
646 Error_Msg_N -- CODEFIX
647 ("\if qualified expression was meant, " &
648 "use apostrophe", Constraint (E));
649 end if;
650 end if;
652 -- Get rid of the bogus constraint:
654 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
655 Analyze_Allocator (N);
656 return;
657 end if;
659 if Expander_Active then
660 Def_Id := Make_Temporary (Loc, 'S');
662 Insert_Action (E,
663 Make_Subtype_Declaration (Loc,
664 Defining_Identifier => Def_Id,
665 Subtype_Indication => Relocate_Node (E)));
667 if Sav_Errs /= Serious_Errors_Detected
668 and then Nkind (Constraint (E)) =
669 N_Index_Or_Discriminant_Constraint
670 then
671 Error_Msg_N -- CODEFIX
672 ("if qualified expression was meant, "
673 & "use apostrophe!", Constraint (E));
674 end if;
676 E := New_Occurrence_Of (Def_Id, Loc);
677 Rewrite (Expression (N), E);
678 end if;
679 end if;
681 Type_Id := Process_Subtype (E, N);
682 Acc_Type := Create_Itype (E_Allocator_Type, N);
683 Set_Etype (Acc_Type, Acc_Type);
684 Set_Directly_Designated_Type (Acc_Type, Type_Id);
685 Check_Fully_Declared (Type_Id, N);
687 -- Ada 2005 (AI-231): If the designated type is itself an access
688 -- type that excludes null, its default initialization will
689 -- be a null object, and we can insert an unconditional raise
690 -- before the allocator.
692 -- Ada 2012 (AI-104): A not null indication here is altogether
693 -- illegal.
695 if Can_Never_Be_Null (Type_Id) then
696 declare
697 Not_Null_Check : constant Node_Id :=
698 Make_Raise_Constraint_Error (Sloc (E),
699 Reason => CE_Null_Not_Allowed);
701 begin
702 if Expander_Active then
703 Insert_Action (N, Not_Null_Check);
704 Analyze (Not_Null_Check);
706 elsif Warn_On_Ada_2012_Compatibility then
707 Error_Msg_N
708 ("null value not allowed here in Ada 2012?y?", E);
709 end if;
710 end;
711 end if;
713 -- Check for missing initialization. Skip this check if we already
714 -- had errors on analyzing the allocator, since in that case these
715 -- are probably cascaded errors.
717 if not Is_Definite_Subtype (Type_Id)
718 and then Serious_Errors_Detected = Sav_Errs
719 then
720 -- The build-in-place machinery may produce an allocator when
721 -- the designated type is indefinite but the underlying type is
722 -- not. In this case the unknown discriminants are meaningless
723 -- and should not trigger error messages. Check the parent node
724 -- because the allocator is marked as coming from source.
726 if Present (Underlying_Type (Type_Id))
727 and then Is_Definite_Subtype (Underlying_Type (Type_Id))
728 and then not Comes_From_Source (Parent (N))
729 then
730 null;
732 elsif Is_Class_Wide_Type (Type_Id) then
733 Error_Msg_N
734 ("initialization required in class-wide allocation", N);
736 else
737 if Ada_Version < Ada_2005
738 and then Is_Limited_Type (Type_Id)
739 then
740 Error_Msg_N ("unconstrained allocation not allowed", N);
742 if Is_Array_Type (Type_Id) then
743 Error_Msg_N
744 ("\constraint with array bounds required", N);
746 elsif Has_Unknown_Discriminants (Type_Id) then
747 null;
749 else pragma Assert (Has_Discriminants (Type_Id));
750 Error_Msg_N
751 ("\constraint with discriminant values required", N);
752 end if;
754 -- Limited Ada 2005 and general non-limited case
756 else
757 Error_Msg_N
758 ("uninitialized unconstrained allocation not "
759 & "allowed", N);
761 if Is_Array_Type (Type_Id) then
762 Error_Msg_N
763 ("\qualified expression or constraint with "
764 & "array bounds required", N);
766 elsif Has_Unknown_Discriminants (Type_Id) then
767 Error_Msg_N ("\qualified expression required", N);
769 else pragma Assert (Has_Discriminants (Type_Id));
770 Error_Msg_N
771 ("\qualified expression or constraint with "
772 & "discriminant values required", N);
773 end if;
774 end if;
775 end if;
776 end if;
777 end;
778 end if;
780 if Is_Abstract_Type (Type_Id) then
781 Error_Msg_N ("cannot allocate abstract object", E);
782 end if;
784 if Has_Task (Designated_Type (Acc_Type)) then
785 Check_Restriction (No_Tasking, N);
786 Check_Restriction (Max_Tasks, N);
787 Check_Restriction (No_Task_Allocators, N);
788 end if;
790 -- Check restriction against dynamically allocated protected objects
792 if Has_Protected (Designated_Type (Acc_Type)) then
793 Check_Restriction (No_Protected_Type_Allocators, N);
794 end if;
796 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
797 -- type is nested, and the designated type needs finalization. The rule
798 -- is conservative in that class-wide types need finalization.
800 if Needs_Finalization (Designated_Type (Acc_Type))
801 and then not Is_Library_Level_Entity (Acc_Type)
802 then
803 Check_Restriction (No_Nested_Finalization, N);
804 end if;
806 -- Check that an allocator of a nested access type doesn't create a
807 -- protected object when restriction No_Local_Protected_Objects applies.
809 if Has_Protected (Designated_Type (Acc_Type))
810 and then not Is_Library_Level_Entity (Acc_Type)
811 then
812 Check_Restriction (No_Local_Protected_Objects, N);
813 end if;
815 -- Likewise for No_Local_Timing_Events
817 if Has_Timing_Event (Designated_Type (Acc_Type))
818 and then not Is_Library_Level_Entity (Acc_Type)
819 then
820 Check_Restriction (No_Local_Timing_Events, N);
821 end if;
823 -- If the No_Streams restriction is set, check that the type of the
824 -- object is not, and does not contain, any subtype derived from
825 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
826 -- Has_Stream just for efficiency reasons. There is no point in
827 -- spending time on a Has_Stream check if the restriction is not set.
829 if Restriction_Check_Required (No_Streams) then
830 if Has_Stream (Designated_Type (Acc_Type)) then
831 Check_Restriction (No_Streams, N);
832 end if;
833 end if;
835 Set_Etype (N, Acc_Type);
837 if not Is_Library_Level_Entity (Acc_Type) then
838 Check_Restriction (No_Local_Allocators, N);
839 end if;
841 if Serious_Errors_Detected > Sav_Errs then
842 Set_Error_Posted (N);
843 Set_Etype (N, Any_Type);
844 end if;
845 end Analyze_Allocator;
847 ---------------------------
848 -- Analyze_Arithmetic_Op --
849 ---------------------------
851 procedure Analyze_Arithmetic_Op (N : Node_Id) is
852 L : constant Node_Id := Left_Opnd (N);
853 R : constant Node_Id := Right_Opnd (N);
854 Op_Id : Entity_Id;
856 begin
857 Candidate_Type := Empty;
858 Analyze_Expression (L);
859 Analyze_Expression (R);
861 -- If the entity is already set, the node is the instantiation of a
862 -- generic node with a non-local reference, or was manufactured by a
863 -- call to Make_Op_xxx. In either case the entity is known to be valid,
864 -- and we do not need to collect interpretations, instead we just get
865 -- the single possible interpretation.
867 Op_Id := Entity (N);
869 if Present (Op_Id) then
870 if Ekind (Op_Id) = E_Operator then
872 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
873 and then Treat_Fixed_As_Integer (N)
874 then
875 null;
876 else
877 Set_Etype (N, Any_Type);
878 Find_Arithmetic_Types (L, R, Op_Id, N);
879 end if;
881 else
882 Set_Etype (N, Any_Type);
883 Add_One_Interp (N, Op_Id, Etype (Op_Id));
884 end if;
886 -- Entity is not already set, so we do need to collect interpretations
888 else
889 Set_Etype (N, Any_Type);
891 Op_Id := Get_Name_Entity_Id (Chars (N));
892 while Present (Op_Id) loop
893 if Ekind (Op_Id) = E_Operator
894 and then Present (Next_Entity (First_Entity (Op_Id)))
895 then
896 Find_Arithmetic_Types (L, R, Op_Id, N);
898 -- The following may seem superfluous, because an operator cannot
899 -- be generic, but this ignores the cleverness of the author of
900 -- ACVC bc1013a.
902 elsif Is_Overloadable (Op_Id) then
903 Analyze_User_Defined_Binary_Op (N, Op_Id);
904 end if;
906 Op_Id := Homonym (Op_Id);
907 end loop;
908 end if;
910 Operator_Check (N);
911 Check_Function_Writable_Actuals (N);
912 end Analyze_Arithmetic_Op;
914 ------------------
915 -- Analyze_Call --
916 ------------------
918 -- Function, procedure, and entry calls are checked here. The Name in
919 -- the call may be overloaded. The actuals have been analyzed and may
920 -- themselves be overloaded. On exit from this procedure, the node N
921 -- may have zero, one or more interpretations. In the first case an
922 -- error message is produced. In the last case, the node is flagged
923 -- as overloaded and the interpretations are collected in All_Interp.
925 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
926 -- the type-checking is similar to that of other calls.
928 procedure Analyze_Call (N : Node_Id) is
929 Actuals : constant List_Id := Parameter_Associations (N);
930 Nam : Node_Id;
931 X : Interp_Index;
932 It : Interp;
933 Nam_Ent : Entity_Id;
934 Success : Boolean := False;
936 Deref : Boolean := False;
937 -- Flag indicates whether an interpretation of the prefix is a
938 -- parameterless call that returns an access_to_subprogram.
940 procedure Check_Mixed_Parameter_And_Named_Associations;
941 -- Check that parameter and named associations are not mixed. This is
942 -- a restriction in SPARK mode.
944 procedure Check_Writable_Actuals (N : Node_Id);
945 -- If the call has out or in-out parameters then mark its outermost
946 -- enclosing construct as a node on which the writable actuals check
947 -- must be performed.
949 function Name_Denotes_Function return Boolean;
950 -- If the type of the name is an access to subprogram, this may be the
951 -- type of a name, or the return type of the function being called. If
952 -- the name is not an entity then it can denote a protected function.
953 -- Until we distinguish Etype from Return_Type, we must use this routine
954 -- to resolve the meaning of the name in the call.
956 procedure No_Interpretation;
957 -- Output error message when no valid interpretation exists
959 --------------------------------------------------
960 -- Check_Mixed_Parameter_And_Named_Associations --
961 --------------------------------------------------
963 procedure Check_Mixed_Parameter_And_Named_Associations is
964 Actual : Node_Id;
965 Named_Seen : Boolean;
967 begin
968 Named_Seen := False;
970 Actual := First (Actuals);
971 while Present (Actual) loop
972 case Nkind (Actual) is
973 when N_Parameter_Association =>
974 if Named_Seen then
975 Check_SPARK_05_Restriction
976 ("named association cannot follow positional one",
977 Actual);
978 exit;
979 end if;
981 when others =>
982 Named_Seen := True;
983 end case;
985 Next (Actual);
986 end loop;
987 end Check_Mixed_Parameter_And_Named_Associations;
989 ----------------------------
990 -- Check_Writable_Actuals --
991 ----------------------------
993 -- The identification of conflicts in calls to functions with writable
994 -- actuals is performed in the analysis phase of the front end to ensure
995 -- that it reports exactly the same errors compiling with and without
996 -- expansion enabled. It is performed in two stages:
998 -- 1) When a call to a function with out-mode parameters is found,
999 -- we climb to the outermost enclosing construct that can be
1000 -- evaluated in arbitrary order and we mark it with the flag
1001 -- Check_Actuals.
1003 -- 2) When the analysis of the marked node is complete, we traverse
1004 -- its decorated subtree searching for conflicts (see function
1005 -- Sem_Util.Check_Function_Writable_Actuals).
1007 -- The unique exception to this general rule is for aggregates, since
1008 -- their analysis is performed by the front end in the resolution
1009 -- phase. For aggregates we do not climb to their enclosing construct:
1010 -- we restrict the analysis to the subexpressions initializing the
1011 -- aggregate components.
1013 -- This implies that the analysis of expressions containing aggregates
1014 -- is not complete, since there may be conflicts on writable actuals
1015 -- involving subexpressions of the enclosing logical or arithmetic
1016 -- expressions. However, we cannot wait and perform the analysis when
1017 -- the whole subtree is resolved, since the subtrees may be transformed,
1018 -- thus adding extra complexity and computation cost to identify and
1019 -- report exactly the same errors compiling with and without expansion
1020 -- enabled.
1022 procedure Check_Writable_Actuals (N : Node_Id) is
1023 begin
1024 if Comes_From_Source (N)
1025 and then Present (Get_Subprogram_Entity (N))
1026 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1027 then
1028 -- For procedures and entries there is no need to climb since
1029 -- we only need to check if the actuals of this call invoke
1030 -- functions whose out-mode parameters overlap.
1032 if Nkind (N) /= N_Function_Call then
1033 Set_Check_Actuals (N);
1035 -- For calls to functions we climb to the outermost enclosing
1036 -- construct where the out-mode actuals of this function may
1037 -- introduce conflicts.
1039 else
1040 declare
1041 Outermost : Node_Id;
1042 P : Node_Id := N;
1044 begin
1045 while Present (P) loop
1047 -- For object declarations we can climb to the node from
1048 -- its object definition branch or from its initializing
1049 -- expression. We prefer to mark the child node as the
1050 -- outermost construct to avoid adding further complexity
1051 -- to the routine that will later take care of
1052 -- performing the writable actuals check.
1054 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1055 and then not Nkind_In (P, N_Assignment_Statement,
1056 N_Object_Declaration)
1057 then
1058 Outermost := P;
1059 end if;
1061 -- Avoid climbing more than needed!
1063 exit when Stop_Subtree_Climbing (Nkind (P))
1064 or else (Nkind (P) = N_Range
1065 and then not
1066 Nkind_In (Parent (P), N_In, N_Not_In));
1068 P := Parent (P);
1069 end loop;
1071 Set_Check_Actuals (Outermost);
1072 end;
1073 end if;
1074 end if;
1075 end Check_Writable_Actuals;
1077 ---------------------------
1078 -- Name_Denotes_Function --
1079 ---------------------------
1081 function Name_Denotes_Function return Boolean is
1082 begin
1083 if Is_Entity_Name (Nam) then
1084 return Ekind (Entity (Nam)) = E_Function;
1085 elsif Nkind (Nam) = N_Selected_Component then
1086 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1087 else
1088 return False;
1089 end if;
1090 end Name_Denotes_Function;
1092 -----------------------
1093 -- No_Interpretation --
1094 -----------------------
1096 procedure No_Interpretation is
1097 L : constant Boolean := Is_List_Member (N);
1098 K : constant Node_Kind := Nkind (Parent (N));
1100 begin
1101 -- If the node is in a list whose parent is not an expression then it
1102 -- must be an attempted procedure call.
1104 if L and then K not in N_Subexpr then
1105 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1106 Error_Msg_NE
1107 ("must instantiate generic procedure& before call",
1108 Nam, Entity (Nam));
1109 else
1110 Error_Msg_N ("procedure or entry name expected", Nam);
1111 end if;
1113 -- Check for tasking cases where only an entry call will do
1115 elsif not L
1116 and then Nkind_In (K, N_Entry_Call_Alternative,
1117 N_Triggering_Alternative)
1118 then
1119 Error_Msg_N ("entry name expected", Nam);
1121 -- Otherwise give general error message
1123 else
1124 Error_Msg_N ("invalid prefix in call", Nam);
1125 end if;
1126 end No_Interpretation;
1128 -- Start of processing for Analyze_Call
1130 begin
1131 if Restriction_Check_Required (SPARK_05) then
1132 Check_Mixed_Parameter_And_Named_Associations;
1133 end if;
1135 -- Initialize the type of the result of the call to the error type,
1136 -- which will be reset if the type is successfully resolved.
1138 Set_Etype (N, Any_Type);
1140 Nam := Name (N);
1142 if not Is_Overloaded (Nam) then
1144 -- Only one interpretation to check
1146 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1147 Nam_Ent := Etype (Nam);
1149 -- If the prefix is an access_to_subprogram, this may be an indirect
1150 -- call. This is the case if the name in the call is not an entity
1151 -- name, or if it is a function name in the context of a procedure
1152 -- call. In this latter case, we have a call to a parameterless
1153 -- function that returns a pointer_to_procedure which is the entity
1154 -- being called. Finally, F (X) may be a call to a parameterless
1155 -- function that returns a pointer to a function with parameters.
1156 -- Note that if F returns an access-to-subprogram whose designated
1157 -- type is an array, F (X) cannot be interpreted as an indirect call
1158 -- through the result of the call to F.
1160 elsif Is_Access_Type (Etype (Nam))
1161 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1162 and then
1163 (not Name_Denotes_Function
1164 or else Nkind (N) = N_Procedure_Call_Statement
1165 or else
1166 (Nkind (Parent (N)) /= N_Explicit_Dereference
1167 and then Is_Entity_Name (Nam)
1168 and then No (First_Formal (Entity (Nam)))
1169 and then not
1170 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1171 and then Present (Actuals)))
1172 then
1173 Nam_Ent := Designated_Type (Etype (Nam));
1174 Insert_Explicit_Dereference (Nam);
1176 -- Selected component case. Simple entry or protected operation,
1177 -- where the entry name is given by the selector name.
1179 elsif Nkind (Nam) = N_Selected_Component then
1180 Nam_Ent := Entity (Selector_Name (Nam));
1182 if not Ekind_In (Nam_Ent, E_Entry,
1183 E_Entry_Family,
1184 E_Function,
1185 E_Procedure)
1186 then
1187 Error_Msg_N ("name in call is not a callable entity", Nam);
1188 Set_Etype (N, Any_Type);
1189 return;
1190 end if;
1192 -- If the name is an Indexed component, it can be a call to a member
1193 -- of an entry family. The prefix must be a selected component whose
1194 -- selector is the entry. Analyze_Procedure_Call normalizes several
1195 -- kinds of call into this form.
1197 elsif Nkind (Nam) = N_Indexed_Component then
1198 if Nkind (Prefix (Nam)) = N_Selected_Component then
1199 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1200 else
1201 Error_Msg_N ("name in call is not a callable entity", Nam);
1202 Set_Etype (N, Any_Type);
1203 return;
1204 end if;
1206 elsif not Is_Entity_Name (Nam) then
1207 Error_Msg_N ("name in call is not a callable entity", Nam);
1208 Set_Etype (N, Any_Type);
1209 return;
1211 else
1212 Nam_Ent := Entity (Nam);
1214 -- If not overloadable, this may be a generalized indexing
1215 -- operation with named associations. Rewrite again as an
1216 -- indexed component and analyze as container indexing.
1218 if not Is_Overloadable (Nam_Ent) then
1219 if Present
1220 (Find_Value_Of_Aspect
1221 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1222 then
1223 Replace (N,
1224 Make_Indexed_Component (Sloc (N),
1225 Prefix => Nam,
1226 Expressions => Parameter_Associations (N)));
1228 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1229 return;
1230 else
1231 No_Interpretation;
1232 end if;
1234 else
1235 No_Interpretation;
1236 end if;
1238 return;
1239 end if;
1240 end if;
1242 -- Operations generated for RACW stub types are called only through
1243 -- dispatching, and can never be the static interpretation of a call.
1245 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1246 No_Interpretation;
1247 return;
1248 end if;
1250 Analyze_One_Call (N, Nam_Ent, True, Success);
1252 -- If this is an indirect call, the return type of the access_to
1253 -- subprogram may be an incomplete type. At the point of the call,
1254 -- use the full type if available, and at the same time update the
1255 -- return type of the access_to_subprogram.
1257 if Success
1258 and then Nkind (Nam) = N_Explicit_Dereference
1259 and then Ekind (Etype (N)) = E_Incomplete_Type
1260 and then Present (Full_View (Etype (N)))
1261 then
1262 Set_Etype (N, Full_View (Etype (N)));
1263 Set_Etype (Nam_Ent, Etype (N));
1264 end if;
1266 -- Overloaded call
1268 else
1269 -- An overloaded selected component must denote overloaded operations
1270 -- of a concurrent type. The interpretations are attached to the
1271 -- simple name of those operations.
1273 if Nkind (Nam) = N_Selected_Component then
1274 Nam := Selector_Name (Nam);
1275 end if;
1277 Get_First_Interp (Nam, X, It);
1278 while Present (It.Nam) loop
1279 Nam_Ent := It.Nam;
1280 Deref := False;
1282 -- Name may be call that returns an access to subprogram, or more
1283 -- generally an overloaded expression one of whose interpretations
1284 -- yields an access to subprogram. If the name is an entity, we do
1285 -- not dereference, because the node is a call that returns the
1286 -- access type: note difference between f(x), where the call may
1287 -- return an access subprogram type, and f(x)(y), where the type
1288 -- returned by the call to f is implicitly dereferenced to analyze
1289 -- the outer call.
1291 if Is_Access_Type (Nam_Ent) then
1292 Nam_Ent := Designated_Type (Nam_Ent);
1294 elsif Is_Access_Type (Etype (Nam_Ent))
1295 and then
1296 (not Is_Entity_Name (Nam)
1297 or else Nkind (N) = N_Procedure_Call_Statement)
1298 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1299 = E_Subprogram_Type
1300 then
1301 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1303 if Is_Entity_Name (Nam) then
1304 Deref := True;
1305 end if;
1306 end if;
1308 -- If the call has been rewritten from a prefixed call, the first
1309 -- parameter has been analyzed, but may need a subsequent
1310 -- dereference, so skip its analysis now.
1312 if N /= Original_Node (N)
1313 and then Nkind (Original_Node (N)) = Nkind (N)
1314 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1315 and then Present (Parameter_Associations (N))
1316 and then Present (Etype (First (Parameter_Associations (N))))
1317 then
1318 Analyze_One_Call
1319 (N, Nam_Ent, False, Success, Skip_First => True);
1320 else
1321 Analyze_One_Call (N, Nam_Ent, False, Success);
1322 end if;
1324 -- If the interpretation succeeds, mark the proper type of the
1325 -- prefix (any valid candidate will do). If not, remove the
1326 -- candidate interpretation. This only needs to be done for
1327 -- overloaded protected operations, for other entities disambi-
1328 -- guation is done directly in Resolve.
1330 if Success then
1331 if Deref
1332 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1333 then
1334 Set_Entity (Nam, It.Nam);
1335 Insert_Explicit_Dereference (Nam);
1336 Set_Etype (Nam, Nam_Ent);
1338 else
1339 Set_Etype (Nam, It.Typ);
1340 end if;
1342 elsif Nkind_In (Name (N), N_Selected_Component,
1343 N_Function_Call)
1344 then
1345 Remove_Interp (X);
1346 end if;
1348 Get_Next_Interp (X, It);
1349 end loop;
1351 -- If the name is the result of a function call, it can only be a
1352 -- call to a function returning an access to subprogram. Insert
1353 -- explicit dereference.
1355 if Nkind (Nam) = N_Function_Call then
1356 Insert_Explicit_Dereference (Nam);
1357 end if;
1359 if Etype (N) = Any_Type then
1361 -- None of the interpretations is compatible with the actuals
1363 Diagnose_Call (N, Nam);
1365 -- Special checks for uninstantiated put routines
1367 if Nkind (N) = N_Procedure_Call_Statement
1368 and then Is_Entity_Name (Nam)
1369 and then Chars (Nam) = Name_Put
1370 and then List_Length (Actuals) = 1
1371 then
1372 declare
1373 Arg : constant Node_Id := First (Actuals);
1374 Typ : Entity_Id;
1376 begin
1377 if Nkind (Arg) = N_Parameter_Association then
1378 Typ := Etype (Explicit_Actual_Parameter (Arg));
1379 else
1380 Typ := Etype (Arg);
1381 end if;
1383 if Is_Signed_Integer_Type (Typ) then
1384 Error_Msg_N
1385 ("possible missing instantiation of "
1386 & "'Text_'I'O.'Integer_'I'O!", Nam);
1388 elsif Is_Modular_Integer_Type (Typ) then
1389 Error_Msg_N
1390 ("possible missing instantiation of "
1391 & "'Text_'I'O.'Modular_'I'O!", Nam);
1393 elsif Is_Floating_Point_Type (Typ) then
1394 Error_Msg_N
1395 ("possible missing instantiation of "
1396 & "'Text_'I'O.'Float_'I'O!", Nam);
1398 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1399 Error_Msg_N
1400 ("possible missing instantiation of "
1401 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1403 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1404 Error_Msg_N
1405 ("possible missing instantiation of "
1406 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1408 elsif Is_Enumeration_Type (Typ) then
1409 Error_Msg_N
1410 ("possible missing instantiation of "
1411 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1412 end if;
1413 end;
1414 end if;
1416 elsif not Is_Overloaded (N)
1417 and then Is_Entity_Name (Nam)
1418 then
1419 -- Resolution yields a single interpretation. Verify that the
1420 -- reference has capitalization consistent with the declaration.
1422 Set_Entity_With_Checks (Nam, Entity (Nam));
1423 Generate_Reference (Entity (Nam), Nam);
1425 Set_Etype (Nam, Etype (Entity (Nam)));
1426 else
1427 Remove_Abstract_Operations (N);
1428 end if;
1430 End_Interp_List;
1431 end if;
1433 if Ada_Version >= Ada_2012 then
1435 -- Check if the call contains a function with writable actuals
1437 Check_Writable_Actuals (N);
1439 -- If found and the outermost construct that can be evaluated in
1440 -- an arbitrary order is precisely this call, then check all its
1441 -- actuals.
1443 Check_Function_Writable_Actuals (N);
1444 end if;
1445 end Analyze_Call;
1447 -----------------------------
1448 -- Analyze_Case_Expression --
1449 -----------------------------
1451 procedure Analyze_Case_Expression (N : Node_Id) is
1452 procedure Non_Static_Choice_Error (Choice : Node_Id);
1453 -- Error routine invoked by the generic instantiation below when
1454 -- the case expression has a non static choice.
1456 package Case_Choices_Analysis is new
1457 Generic_Analyze_Choices
1458 (Process_Associated_Node => No_OP);
1459 use Case_Choices_Analysis;
1461 package Case_Choices_Checking is new
1462 Generic_Check_Choices
1463 (Process_Empty_Choice => No_OP,
1464 Process_Non_Static_Choice => Non_Static_Choice_Error,
1465 Process_Associated_Node => No_OP);
1466 use Case_Choices_Checking;
1468 -----------------------------
1469 -- Non_Static_Choice_Error --
1470 -----------------------------
1472 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1473 begin
1474 Flag_Non_Static_Expr
1475 ("choice given in case expression is not static!", Choice);
1476 end Non_Static_Choice_Error;
1478 -- Local variables
1480 Expr : constant Node_Id := Expression (N);
1481 Alt : Node_Id;
1482 Exp_Type : Entity_Id;
1483 Exp_Btype : Entity_Id;
1485 FirstX : Node_Id := Empty;
1486 -- First expression in the case for which there is some type information
1487 -- available, i.e. it is not Any_Type, which can happen because of some
1488 -- error, or from the use of e.g. raise Constraint_Error.
1490 Others_Present : Boolean;
1491 -- Indicates if Others was present
1493 Wrong_Alt : Node_Id;
1494 -- For error reporting
1496 -- Start of processing for Analyze_Case_Expression
1498 begin
1499 if Comes_From_Source (N) then
1500 Check_Compiler_Unit ("case expression", N);
1501 end if;
1503 Analyze_And_Resolve (Expr, Any_Discrete);
1504 Check_Unset_Reference (Expr);
1505 Exp_Type := Etype (Expr);
1506 Exp_Btype := Base_Type (Exp_Type);
1508 Alt := First (Alternatives (N));
1509 while Present (Alt) loop
1510 Analyze (Expression (Alt));
1512 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1513 FirstX := Expression (Alt);
1514 end if;
1516 Next (Alt);
1517 end loop;
1519 -- Get our initial type from the first expression for which we got some
1520 -- useful type information from the expression.
1522 if not Is_Overloaded (FirstX) then
1523 Set_Etype (N, Etype (FirstX));
1525 else
1526 declare
1527 I : Interp_Index;
1528 It : Interp;
1530 begin
1531 Set_Etype (N, Any_Type);
1533 Get_First_Interp (FirstX, I, It);
1534 while Present (It.Nam) loop
1536 -- For each interpretation of the first expression, we only
1537 -- add the interpretation if every other expression in the
1538 -- case expression alternatives has a compatible type.
1540 Alt := Next (First (Alternatives (N)));
1541 while Present (Alt) loop
1542 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1543 Next (Alt);
1544 end loop;
1546 if No (Alt) then
1547 Add_One_Interp (N, It.Typ, It.Typ);
1548 else
1549 Wrong_Alt := Alt;
1550 end if;
1552 Get_Next_Interp (I, It);
1553 end loop;
1554 end;
1555 end if;
1557 Exp_Btype := Base_Type (Exp_Type);
1559 -- The expression must be of a discrete type which must be determinable
1560 -- independently of the context in which the expression occurs, but
1561 -- using the fact that the expression must be of a discrete type.
1562 -- Moreover, the type this expression must not be a character literal
1563 -- (which is always ambiguous).
1565 -- If error already reported by Resolve, nothing more to do
1567 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1568 return;
1570 -- Special casee message for character literal
1572 elsif Exp_Btype = Any_Character then
1573 Error_Msg_N
1574 ("character literal as case expression is ambiguous", Expr);
1575 return;
1576 end if;
1578 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1579 Error_Msg_N
1580 ("type incompatible with that of previous alternatives",
1581 Expression (Wrong_Alt));
1582 return;
1583 end if;
1585 -- If the case expression is a formal object of mode in out, then
1586 -- treat it as having a nonstatic subtype by forcing use of the base
1587 -- type (which has to get passed to Check_Case_Choices below). Also
1588 -- use base type when the case expression is parenthesized.
1590 if Paren_Count (Expr) > 0
1591 or else (Is_Entity_Name (Expr)
1592 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1593 then
1594 Exp_Type := Exp_Btype;
1595 end if;
1597 -- The case expression alternatives cover the range of a static subtype
1598 -- subject to aspect Static_Predicate. Do not check the choices when the
1599 -- case expression has not been fully analyzed yet because this may lead
1600 -- to bogus errors.
1602 if Is_OK_Static_Subtype (Exp_Type)
1603 and then Has_Static_Predicate_Aspect (Exp_Type)
1604 and then In_Spec_Expression
1605 then
1606 null;
1608 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1610 else
1611 Analyze_Choices (Alternatives (N), Exp_Type);
1612 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1613 end if;
1615 if Exp_Type = Universal_Integer and then not Others_Present then
1616 Error_Msg_N
1617 ("case on universal integer requires OTHERS choice", Expr);
1618 end if;
1619 end Analyze_Case_Expression;
1621 ---------------------------
1622 -- Analyze_Comparison_Op --
1623 ---------------------------
1625 procedure Analyze_Comparison_Op (N : Node_Id) is
1626 L : constant Node_Id := Left_Opnd (N);
1627 R : constant Node_Id := Right_Opnd (N);
1628 Op_Id : Entity_Id := Entity (N);
1630 begin
1631 Set_Etype (N, Any_Type);
1632 Candidate_Type := Empty;
1634 Analyze_Expression (L);
1635 Analyze_Expression (R);
1637 if Present (Op_Id) then
1638 if Ekind (Op_Id) = E_Operator then
1639 Find_Comparison_Types (L, R, Op_Id, N);
1640 else
1641 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1642 end if;
1644 if Is_Overloaded (L) then
1645 Set_Etype (L, Intersect_Types (L, R));
1646 end if;
1648 else
1649 Op_Id := Get_Name_Entity_Id (Chars (N));
1650 while Present (Op_Id) loop
1651 if Ekind (Op_Id) = E_Operator then
1652 Find_Comparison_Types (L, R, Op_Id, N);
1653 else
1654 Analyze_User_Defined_Binary_Op (N, Op_Id);
1655 end if;
1657 Op_Id := Homonym (Op_Id);
1658 end loop;
1659 end if;
1661 Operator_Check (N);
1662 Check_Function_Writable_Actuals (N);
1663 end Analyze_Comparison_Op;
1665 ---------------------------
1666 -- Analyze_Concatenation --
1667 ---------------------------
1669 procedure Analyze_Concatenation (N : Node_Id) is
1671 -- We wish to avoid deep recursion, because concatenations are often
1672 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1673 -- operands nonrecursively until we find something that is not a
1674 -- concatenation (A in this case), or has already been analyzed. We
1675 -- analyze that, and then walk back up the tree following Parent
1676 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1677 -- work at each level. The Parent pointers allow us to avoid recursion,
1678 -- and thus avoid running out of memory.
1680 NN : Node_Id := N;
1681 L : Node_Id;
1683 begin
1684 Candidate_Type := Empty;
1686 -- The following code is equivalent to:
1688 -- Set_Etype (N, Any_Type);
1689 -- Analyze_Expression (Left_Opnd (N));
1690 -- Analyze_Concatenation_Rest (N);
1692 -- where the Analyze_Expression call recurses back here if the left
1693 -- operand is a concatenation.
1695 -- Walk down left operands
1697 loop
1698 Set_Etype (NN, Any_Type);
1699 L := Left_Opnd (NN);
1700 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1701 NN := L;
1702 end loop;
1704 -- Now (given the above example) NN is A&B and L is A
1706 -- First analyze L ...
1708 Analyze_Expression (L);
1710 -- ... then walk NN back up until we reach N (where we started), calling
1711 -- Analyze_Concatenation_Rest along the way.
1713 loop
1714 Analyze_Concatenation_Rest (NN);
1715 exit when NN = N;
1716 NN := Parent (NN);
1717 end loop;
1718 end Analyze_Concatenation;
1720 --------------------------------
1721 -- Analyze_Concatenation_Rest --
1722 --------------------------------
1724 -- If the only one-dimensional array type in scope is String,
1725 -- this is the resulting type of the operation. Otherwise there
1726 -- will be a concatenation operation defined for each user-defined
1727 -- one-dimensional array.
1729 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1730 L : constant Node_Id := Left_Opnd (N);
1731 R : constant Node_Id := Right_Opnd (N);
1732 Op_Id : Entity_Id := Entity (N);
1733 LT : Entity_Id;
1734 RT : Entity_Id;
1736 begin
1737 Analyze_Expression (R);
1739 -- If the entity is present, the node appears in an instance, and
1740 -- denotes a predefined concatenation operation. The resulting type is
1741 -- obtained from the arguments when possible. If the arguments are
1742 -- aggregates, the array type and the concatenation type must be
1743 -- visible.
1745 if Present (Op_Id) then
1746 if Ekind (Op_Id) = E_Operator then
1747 LT := Base_Type (Etype (L));
1748 RT := Base_Type (Etype (R));
1750 if Is_Array_Type (LT)
1751 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1752 then
1753 Add_One_Interp (N, Op_Id, LT);
1755 elsif Is_Array_Type (RT)
1756 and then LT = Base_Type (Component_Type (RT))
1757 then
1758 Add_One_Interp (N, Op_Id, RT);
1760 -- If one operand is a string type or a user-defined array type,
1761 -- and the other is a literal, result is of the specific type.
1763 elsif
1764 (Root_Type (LT) = Standard_String
1765 or else Scope (LT) /= Standard_Standard)
1766 and then Etype (R) = Any_String
1767 then
1768 Add_One_Interp (N, Op_Id, LT);
1770 elsif
1771 (Root_Type (RT) = Standard_String
1772 or else Scope (RT) /= Standard_Standard)
1773 and then Etype (L) = Any_String
1774 then
1775 Add_One_Interp (N, Op_Id, RT);
1777 elsif not Is_Generic_Type (Etype (Op_Id)) then
1778 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1780 else
1781 -- Type and its operations must be visible
1783 Set_Entity (N, Empty);
1784 Analyze_Concatenation (N);
1785 end if;
1787 else
1788 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1789 end if;
1791 else
1792 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1793 while Present (Op_Id) loop
1794 if Ekind (Op_Id) = E_Operator then
1796 -- Do not consider operators declared in dead code, they can
1797 -- not be part of the resolution.
1799 if Is_Eliminated (Op_Id) then
1800 null;
1801 else
1802 Find_Concatenation_Types (L, R, Op_Id, N);
1803 end if;
1805 else
1806 Analyze_User_Defined_Binary_Op (N, Op_Id);
1807 end if;
1809 Op_Id := Homonym (Op_Id);
1810 end loop;
1811 end if;
1813 Operator_Check (N);
1814 end Analyze_Concatenation_Rest;
1816 -------------------------
1817 -- Analyze_Equality_Op --
1818 -------------------------
1820 procedure Analyze_Equality_Op (N : Node_Id) is
1821 Loc : constant Source_Ptr := Sloc (N);
1822 L : constant Node_Id := Left_Opnd (N);
1823 R : constant Node_Id := Right_Opnd (N);
1824 Op_Id : Entity_Id;
1826 begin
1827 Set_Etype (N, Any_Type);
1828 Candidate_Type := Empty;
1830 Analyze_Expression (L);
1831 Analyze_Expression (R);
1833 -- If the entity is set, the node is a generic instance with a non-local
1834 -- reference to the predefined operator or to a user-defined function.
1835 -- It can also be an inequality that is expanded into the negation of a
1836 -- call to a user-defined equality operator.
1838 -- For the predefined case, the result is Boolean, regardless of the
1839 -- type of the operands. The operands may even be limited, if they are
1840 -- generic actuals. If they are overloaded, label the left argument with
1841 -- the common type that must be present, or with the type of the formal
1842 -- of the user-defined function.
1844 if Present (Entity (N)) then
1845 Op_Id := Entity (N);
1847 if Ekind (Op_Id) = E_Operator then
1848 Add_One_Interp (N, Op_Id, Standard_Boolean);
1849 else
1850 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1851 end if;
1853 if Is_Overloaded (L) then
1854 if Ekind (Op_Id) = E_Operator then
1855 Set_Etype (L, Intersect_Types (L, R));
1856 else
1857 Set_Etype (L, Etype (First_Formal (Op_Id)));
1858 end if;
1859 end if;
1861 else
1862 Op_Id := Get_Name_Entity_Id (Chars (N));
1863 while Present (Op_Id) loop
1864 if Ekind (Op_Id) = E_Operator then
1865 Find_Equality_Types (L, R, Op_Id, N);
1866 else
1867 Analyze_User_Defined_Binary_Op (N, Op_Id);
1868 end if;
1870 Op_Id := Homonym (Op_Id);
1871 end loop;
1872 end if;
1874 -- If there was no match, and the operator is inequality, this may be
1875 -- a case where inequality has not been made explicit, as for tagged
1876 -- types. Analyze the node as the negation of an equality operation.
1877 -- This cannot be done earlier, because before analysis we cannot rule
1878 -- out the presence of an explicit inequality.
1880 if Etype (N) = Any_Type
1881 and then Nkind (N) = N_Op_Ne
1882 then
1883 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1884 while Present (Op_Id) loop
1885 if Ekind (Op_Id) = E_Operator then
1886 Find_Equality_Types (L, R, Op_Id, N);
1887 else
1888 Analyze_User_Defined_Binary_Op (N, Op_Id);
1889 end if;
1891 Op_Id := Homonym (Op_Id);
1892 end loop;
1894 if Etype (N) /= Any_Type then
1895 Op_Id := Entity (N);
1897 Rewrite (N,
1898 Make_Op_Not (Loc,
1899 Right_Opnd =>
1900 Make_Op_Eq (Loc,
1901 Left_Opnd => Left_Opnd (N),
1902 Right_Opnd => Right_Opnd (N))));
1904 Set_Entity (Right_Opnd (N), Op_Id);
1905 Analyze (N);
1906 end if;
1907 end if;
1909 Operator_Check (N);
1910 Check_Function_Writable_Actuals (N);
1911 end Analyze_Equality_Op;
1913 ----------------------------------
1914 -- Analyze_Explicit_Dereference --
1915 ----------------------------------
1917 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1918 Loc : constant Source_Ptr := Sloc (N);
1919 P : constant Node_Id := Prefix (N);
1920 T : Entity_Id;
1921 I : Interp_Index;
1922 It : Interp;
1923 New_N : Node_Id;
1925 function Is_Function_Type return Boolean;
1926 -- Check whether node may be interpreted as an implicit function call
1928 ----------------------
1929 -- Is_Function_Type --
1930 ----------------------
1932 function Is_Function_Type return Boolean is
1933 I : Interp_Index;
1934 It : Interp;
1936 begin
1937 if not Is_Overloaded (N) then
1938 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1939 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1941 else
1942 Get_First_Interp (N, I, It);
1943 while Present (It.Nam) loop
1944 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1945 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1946 then
1947 return False;
1948 end if;
1950 Get_Next_Interp (I, It);
1951 end loop;
1953 return True;
1954 end if;
1955 end Is_Function_Type;
1957 -- Start of processing for Analyze_Explicit_Dereference
1959 begin
1960 -- If source node, check SPARK restriction. We guard this with the
1961 -- source node check, because ???
1963 if Comes_From_Source (N) then
1964 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
1965 end if;
1967 -- In formal verification mode, keep track of all reads and writes
1968 -- through explicit dereferences.
1970 if GNATprove_Mode then
1971 SPARK_Specific.Generate_Dereference (N);
1972 end if;
1974 Analyze (P);
1975 Set_Etype (N, Any_Type);
1977 -- Test for remote access to subprogram type, and if so return
1978 -- after rewriting the original tree.
1980 if Remote_AST_E_Dereference (P) then
1981 return;
1982 end if;
1984 -- Normal processing for other than remote access to subprogram type
1986 if not Is_Overloaded (P) then
1987 if Is_Access_Type (Etype (P)) then
1989 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1990 -- avoid other problems caused by the Private_Subtype and it is
1991 -- safe to go to the Base_Type because this is the same as
1992 -- converting the access value to its Base_Type.
1994 declare
1995 DT : Entity_Id := Designated_Type (Etype (P));
1997 begin
1998 if Ekind (DT) = E_Private_Subtype
1999 and then Is_For_Access_Subtype (DT)
2000 then
2001 DT := Base_Type (DT);
2002 end if;
2004 -- An explicit dereference is a legal occurrence of an
2005 -- incomplete type imported through a limited_with clause, if
2006 -- the full view is visible, or if we are within an instance
2007 -- body, where the enclosing body has a regular with_clause
2008 -- on the unit.
2010 if From_Limited_With (DT)
2011 and then not From_Limited_With (Scope (DT))
2012 and then
2013 (Is_Immediately_Visible (Scope (DT))
2014 or else
2015 (Is_Child_Unit (Scope (DT))
2016 and then Is_Visible_Lib_Unit (Scope (DT)))
2017 or else In_Instance_Body)
2018 then
2019 Set_Etype (N, Available_View (DT));
2021 else
2022 Set_Etype (N, DT);
2023 end if;
2024 end;
2026 elsif Etype (P) /= Any_Type then
2027 Error_Msg_N ("prefix of dereference must be an access type", N);
2028 return;
2029 end if;
2031 else
2032 Get_First_Interp (P, I, It);
2033 while Present (It.Nam) loop
2034 T := It.Typ;
2036 if Is_Access_Type (T) then
2037 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2038 end if;
2040 Get_Next_Interp (I, It);
2041 end loop;
2043 -- Error if no interpretation of the prefix has an access type
2045 if Etype (N) = Any_Type then
2046 Error_Msg_N
2047 ("access type required in prefix of explicit dereference", P);
2048 Set_Etype (N, Any_Type);
2049 return;
2050 end if;
2051 end if;
2053 if Is_Function_Type
2054 and then Nkind (Parent (N)) /= N_Indexed_Component
2056 and then (Nkind (Parent (N)) /= N_Function_Call
2057 or else N /= Name (Parent (N)))
2059 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2060 or else N /= Name (Parent (N)))
2062 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2063 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2064 or else
2065 (Attribute_Name (Parent (N)) /= Name_Address
2066 and then
2067 Attribute_Name (Parent (N)) /= Name_Access))
2068 then
2069 -- Name is a function call with no actuals, in a context that
2070 -- requires deproceduring (including as an actual in an enclosing
2071 -- function or procedure call). There are some pathological cases
2072 -- where the prefix might include functions that return access to
2073 -- subprograms and others that return a regular type. Disambiguation
2074 -- of those has to take place in Resolve.
2076 New_N :=
2077 Make_Function_Call (Loc,
2078 Name => Make_Explicit_Dereference (Loc, P),
2079 Parameter_Associations => New_List);
2081 -- If the prefix is overloaded, remove operations that have formals,
2082 -- we know that this is a parameterless call.
2084 if Is_Overloaded (P) then
2085 Get_First_Interp (P, I, It);
2086 while Present (It.Nam) loop
2087 T := It.Typ;
2089 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2090 Set_Etype (P, T);
2091 else
2092 Remove_Interp (I);
2093 end if;
2095 Get_Next_Interp (I, It);
2096 end loop;
2097 end if;
2099 Rewrite (N, New_N);
2100 Analyze (N);
2102 elsif not Is_Function_Type
2103 and then Is_Overloaded (N)
2104 then
2105 -- The prefix may include access to subprograms and other access
2106 -- types. If the context selects the interpretation that is a
2107 -- function call (not a procedure call) we cannot rewrite the node
2108 -- yet, but we include the result of the call interpretation.
2110 Get_First_Interp (N, I, It);
2111 while Present (It.Nam) loop
2112 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2113 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2114 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2115 then
2116 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2117 end if;
2119 Get_Next_Interp (I, It);
2120 end loop;
2121 end if;
2123 -- A value of remote access-to-class-wide must not be dereferenced
2124 -- (RM E.2.2(16)).
2126 Validate_Remote_Access_To_Class_Wide_Type (N);
2127 end Analyze_Explicit_Dereference;
2129 ------------------------
2130 -- Analyze_Expression --
2131 ------------------------
2133 procedure Analyze_Expression (N : Node_Id) is
2134 begin
2136 -- If the expression is an indexed component that will be rewritten
2137 -- as a container indexing, it has already been analyzed.
2139 if Nkind (N) = N_Indexed_Component
2140 and then Present (Generalized_Indexing (N))
2141 then
2142 null;
2144 else
2145 Analyze (N);
2146 Check_Parameterless_Call (N);
2147 end if;
2148 end Analyze_Expression;
2150 -------------------------------------
2151 -- Analyze_Expression_With_Actions --
2152 -------------------------------------
2154 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2155 A : Node_Id;
2157 begin
2158 A := First (Actions (N));
2159 while Present (A) loop
2160 Analyze (A);
2161 Next (A);
2162 end loop;
2164 Analyze_Expression (Expression (N));
2165 Set_Etype (N, Etype (Expression (N)));
2166 end Analyze_Expression_With_Actions;
2168 ---------------------------
2169 -- Analyze_If_Expression --
2170 ---------------------------
2172 procedure Analyze_If_Expression (N : Node_Id) is
2173 Condition : constant Node_Id := First (Expressions (N));
2174 Then_Expr : constant Node_Id := Next (Condition);
2175 Else_Expr : Node_Id;
2177 begin
2178 -- Defend against error of missing expressions from previous error
2180 if No (Then_Expr) then
2181 Check_Error_Detected;
2182 return;
2183 end if;
2185 if Comes_From_Source (N) then
2186 Check_SPARK_05_Restriction ("if expression is not allowed", N);
2187 end if;
2189 Else_Expr := Next (Then_Expr);
2191 if Comes_From_Source (N) then
2192 Check_Compiler_Unit ("if expression", N);
2193 end if;
2195 -- Analyze and resolve the condition. We need to resolve this now so
2196 -- that it gets folded to True/False if possible, before we analyze
2197 -- the THEN/ELSE branches, because when analyzing these branches, we
2198 -- may call Is_Statically_Unevaluated, which expects the condition of
2199 -- an enclosing IF to have been analyze/resolved/evaluated.
2201 Analyze_Expression (Condition);
2202 Resolve (Condition, Any_Boolean);
2204 -- Analyze THEN expression and (if present) ELSE expression. For those
2205 -- we delay resolution in the normal manner, because of overloading etc.
2207 Analyze_Expression (Then_Expr);
2209 if Present (Else_Expr) then
2210 Analyze_Expression (Else_Expr);
2211 end if;
2213 -- If then expression not overloaded, then that decides the type
2215 if not Is_Overloaded (Then_Expr) then
2216 Set_Etype (N, Etype (Then_Expr));
2218 -- Case where then expression is overloaded
2220 else
2221 declare
2222 I : Interp_Index;
2223 It : Interp;
2225 begin
2226 Set_Etype (N, Any_Type);
2228 -- Loop through interpretations of Then_Expr
2230 Get_First_Interp (Then_Expr, I, It);
2231 while Present (It.Nam) loop
2233 -- Add possible interpretation of Then_Expr if no Else_Expr, or
2234 -- Else_Expr is present and has a compatible type.
2236 if No (Else_Expr)
2237 or else Has_Compatible_Type (Else_Expr, It.Typ)
2238 then
2239 Add_One_Interp (N, It.Typ, It.Typ);
2240 end if;
2242 Get_Next_Interp (I, It);
2243 end loop;
2245 -- If no valid interpretation has been found, then the type of the
2246 -- ELSE expression does not match any interpretation of the THEN
2247 -- expression.
2249 if Etype (N) = Any_Type then
2250 Error_Msg_N
2251 ("type incompatible with that of `THEN` expression",
2252 Else_Expr);
2253 return;
2254 end if;
2255 end;
2256 end if;
2257 end Analyze_If_Expression;
2259 ------------------------------------
2260 -- Analyze_Indexed_Component_Form --
2261 ------------------------------------
2263 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2264 P : constant Node_Id := Prefix (N);
2265 Exprs : constant List_Id := Expressions (N);
2266 Exp : Node_Id;
2267 P_T : Entity_Id;
2268 E : Node_Id;
2269 U_N : Entity_Id;
2271 procedure Process_Function_Call;
2272 -- Prefix in indexed component form is an overloadable entity, so the
2273 -- node is a function call. Reformat it as such.
2275 procedure Process_Indexed_Component;
2276 -- Prefix in indexed component form is actually an indexed component.
2277 -- This routine processes it, knowing that the prefix is already
2278 -- resolved.
2280 procedure Process_Indexed_Component_Or_Slice;
2281 -- An indexed component with a single index may designate a slice if
2282 -- the index is a subtype mark. This routine disambiguates these two
2283 -- cases by resolving the prefix to see if it is a subtype mark.
2285 procedure Process_Overloaded_Indexed_Component;
2286 -- If the prefix of an indexed component is overloaded, the proper
2287 -- interpretation is selected by the index types and the context.
2289 ---------------------------
2290 -- Process_Function_Call --
2291 ---------------------------
2293 procedure Process_Function_Call is
2294 Loc : constant Source_Ptr := Sloc (N);
2295 Actual : Node_Id;
2297 begin
2298 Change_Node (N, N_Function_Call);
2299 Set_Name (N, P);
2300 Set_Parameter_Associations (N, Exprs);
2302 -- Analyze actuals prior to analyzing the call itself
2304 Actual := First (Parameter_Associations (N));
2305 while Present (Actual) loop
2306 Analyze (Actual);
2307 Check_Parameterless_Call (Actual);
2309 -- Move to next actual. Note that we use Next, not Next_Actual
2310 -- here. The reason for this is a bit subtle. If a function call
2311 -- includes named associations, the parser recognizes the node
2312 -- as a call, and it is analyzed as such. If all associations are
2313 -- positional, the parser builds an indexed_component node, and
2314 -- it is only after analysis of the prefix that the construct
2315 -- is recognized as a call, in which case Process_Function_Call
2316 -- rewrites the node and analyzes the actuals. If the list of
2317 -- actuals is malformed, the parser may leave the node as an
2318 -- indexed component (despite the presence of named associations).
2319 -- The iterator Next_Actual is equivalent to Next if the list is
2320 -- positional, but follows the normalized chain of actuals when
2321 -- named associations are present. In this case normalization has
2322 -- not taken place, and actuals remain unanalyzed, which leads to
2323 -- subsequent crashes or loops if there is an attempt to continue
2324 -- analysis of the program.
2326 -- IF there is a single actual and it is a type name, the node
2327 -- can only be interpreted as a slice of a parameterless call.
2328 -- Rebuild the node as such and analyze.
2330 if No (Next (Actual))
2331 and then Is_Entity_Name (Actual)
2332 and then Is_Type (Entity (Actual))
2333 and then Is_Discrete_Type (Entity (Actual))
2334 then
2335 Replace (N,
2336 Make_Slice (Loc,
2337 Prefix => P,
2338 Discrete_Range =>
2339 New_Occurrence_Of (Entity (Actual), Loc)));
2340 Analyze (N);
2341 return;
2343 else
2344 Next (Actual);
2345 end if;
2346 end loop;
2348 Analyze_Call (N);
2349 end Process_Function_Call;
2351 -------------------------------
2352 -- Process_Indexed_Component --
2353 -------------------------------
2355 procedure Process_Indexed_Component is
2356 Exp : Node_Id;
2357 Array_Type : Entity_Id;
2358 Index : Node_Id;
2359 Pent : Entity_Id := Empty;
2361 begin
2362 Exp := First (Exprs);
2364 if Is_Overloaded (P) then
2365 Process_Overloaded_Indexed_Component;
2367 else
2368 Array_Type := Etype (P);
2370 if Is_Entity_Name (P) then
2371 Pent := Entity (P);
2372 elsif Nkind (P) = N_Selected_Component
2373 and then Is_Entity_Name (Selector_Name (P))
2374 then
2375 Pent := Entity (Selector_Name (P));
2376 end if;
2378 -- Prefix must be appropriate for an array type, taking into
2379 -- account a possible implicit dereference.
2381 if Is_Access_Type (Array_Type) then
2382 Error_Msg_NW
2383 (Warn_On_Dereference, "?d?implicit dereference", N);
2384 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2385 end if;
2387 if Is_Array_Type (Array_Type) then
2388 null;
2390 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2391 Analyze (Exp);
2392 Set_Etype (N, Any_Type);
2394 if not Has_Compatible_Type
2395 (Exp, Entry_Index_Type (Pent))
2396 then
2397 Error_Msg_N ("invalid index type in entry name", N);
2399 elsif Present (Next (Exp)) then
2400 Error_Msg_N ("too many subscripts in entry reference", N);
2402 else
2403 Set_Etype (N, Etype (P));
2404 end if;
2406 return;
2408 elsif Is_Record_Type (Array_Type)
2409 and then Remote_AST_I_Dereference (P)
2410 then
2411 return;
2413 elsif Try_Container_Indexing (N, P, Exprs) then
2414 return;
2416 elsif Array_Type = Any_Type then
2417 Set_Etype (N, Any_Type);
2419 -- In most cases the analysis of the prefix will have emitted
2420 -- an error already, but if the prefix may be interpreted as a
2421 -- call in prefixed notation, the report is left to the caller.
2422 -- To prevent cascaded errors, report only if no previous ones.
2424 if Serious_Errors_Detected = 0 then
2425 Error_Msg_N ("invalid prefix in indexed component", P);
2427 if Nkind (P) = N_Expanded_Name then
2428 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2429 end if;
2430 end if;
2432 return;
2434 -- Here we definitely have a bad indexing
2436 else
2437 if Nkind (Parent (N)) = N_Requeue_Statement
2438 and then Present (Pent) and then Ekind (Pent) = E_Entry
2439 then
2440 Error_Msg_N
2441 ("REQUEUE does not permit parameters", First (Exprs));
2443 elsif Is_Entity_Name (P)
2444 and then Etype (P) = Standard_Void_Type
2445 then
2446 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2448 else
2449 Error_Msg_N ("array type required in indexed component", P);
2450 end if;
2452 Set_Etype (N, Any_Type);
2453 return;
2454 end if;
2456 Index := First_Index (Array_Type);
2457 while Present (Index) and then Present (Exp) loop
2458 if not Has_Compatible_Type (Exp, Etype (Index)) then
2459 Wrong_Type (Exp, Etype (Index));
2460 Set_Etype (N, Any_Type);
2461 return;
2462 end if;
2464 Next_Index (Index);
2465 Next (Exp);
2466 end loop;
2468 Set_Etype (N, Component_Type (Array_Type));
2469 Check_Implicit_Dereference (N, Etype (N));
2471 if Present (Index) then
2472 Error_Msg_N
2473 ("too few subscripts in array reference", First (Exprs));
2475 elsif Present (Exp) then
2476 Error_Msg_N ("too many subscripts in array reference", Exp);
2477 end if;
2478 end if;
2479 end Process_Indexed_Component;
2481 ----------------------------------------
2482 -- Process_Indexed_Component_Or_Slice --
2483 ----------------------------------------
2485 procedure Process_Indexed_Component_Or_Slice is
2486 begin
2487 Exp := First (Exprs);
2488 while Present (Exp) loop
2489 Analyze_Expression (Exp);
2490 Next (Exp);
2491 end loop;
2493 Exp := First (Exprs);
2495 -- If one index is present, and it is a subtype name, then the node
2496 -- denotes a slice (note that the case of an explicit range for a
2497 -- slice was already built as an N_Slice node in the first place,
2498 -- so that case is not handled here).
2500 -- We use a replace rather than a rewrite here because this is one
2501 -- of the cases in which the tree built by the parser is plain wrong.
2503 if No (Next (Exp))
2504 and then Is_Entity_Name (Exp)
2505 and then Is_Type (Entity (Exp))
2506 then
2507 Replace (N,
2508 Make_Slice (Sloc (N),
2509 Prefix => P,
2510 Discrete_Range => New_Copy (Exp)));
2511 Analyze (N);
2513 -- Otherwise (more than one index present, or single index is not
2514 -- a subtype name), then we have the indexed component case.
2516 else
2517 Process_Indexed_Component;
2518 end if;
2519 end Process_Indexed_Component_Or_Slice;
2521 ------------------------------------------
2522 -- Process_Overloaded_Indexed_Component --
2523 ------------------------------------------
2525 procedure Process_Overloaded_Indexed_Component is
2526 Exp : Node_Id;
2527 I : Interp_Index;
2528 It : Interp;
2529 Typ : Entity_Id;
2530 Index : Node_Id;
2531 Found : Boolean;
2533 begin
2534 Set_Etype (N, Any_Type);
2536 Get_First_Interp (P, I, It);
2537 while Present (It.Nam) loop
2538 Typ := It.Typ;
2540 if Is_Access_Type (Typ) then
2541 Typ := Designated_Type (Typ);
2542 Error_Msg_NW
2543 (Warn_On_Dereference, "?d?implicit dereference", N);
2544 end if;
2546 if Is_Array_Type (Typ) then
2548 -- Got a candidate: verify that index types are compatible
2550 Index := First_Index (Typ);
2551 Found := True;
2552 Exp := First (Exprs);
2553 while Present (Index) and then Present (Exp) loop
2554 if Has_Compatible_Type (Exp, Etype (Index)) then
2555 null;
2556 else
2557 Found := False;
2558 Remove_Interp (I);
2559 exit;
2560 end if;
2562 Next_Index (Index);
2563 Next (Exp);
2564 end loop;
2566 if Found and then No (Index) and then No (Exp) then
2567 declare
2568 CT : constant Entity_Id :=
2569 Base_Type (Component_Type (Typ));
2570 begin
2571 Add_One_Interp (N, CT, CT);
2572 Check_Implicit_Dereference (N, CT);
2573 end;
2574 end if;
2576 elsif Try_Container_Indexing (N, P, Exprs) then
2577 return;
2579 end if;
2581 Get_Next_Interp (I, It);
2582 end loop;
2584 if Etype (N) = Any_Type then
2585 Error_Msg_N ("no legal interpretation for indexed component", N);
2586 Set_Is_Overloaded (N, False);
2587 end if;
2589 End_Interp_List;
2590 end Process_Overloaded_Indexed_Component;
2592 -- Start of processing for Analyze_Indexed_Component_Form
2594 begin
2595 -- Get name of array, function or type
2597 Analyze (P);
2599 -- If P is an explicit dereference whose prefix is of a remote access-
2600 -- to-subprogram type, then N has already been rewritten as a subprogram
2601 -- call and analyzed.
2603 if Nkind (N) in N_Subprogram_Call then
2604 return;
2606 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2607 -- the indexed component denotes a loop name, the indexed form is turned
2608 -- into an attribute reference.
2610 elsif Nkind (N) = N_Attribute_Reference
2611 and then Attribute_Name (N) = Name_Loop_Entry
2612 then
2613 return;
2614 end if;
2616 pragma Assert (Nkind (N) = N_Indexed_Component);
2618 P_T := Base_Type (Etype (P));
2620 if Is_Entity_Name (P) and then Present (Entity (P)) then
2621 U_N := Entity (P);
2623 if Is_Type (U_N) then
2625 -- Reformat node as a type conversion
2627 E := Remove_Head (Exprs);
2629 if Present (First (Exprs)) then
2630 Error_Msg_N
2631 ("argument of type conversion must be single expression", N);
2632 end if;
2634 Change_Node (N, N_Type_Conversion);
2635 Set_Subtype_Mark (N, P);
2636 Set_Etype (N, U_N);
2637 Set_Expression (N, E);
2639 -- After changing the node, call for the specific Analysis
2640 -- routine directly, to avoid a double call to the expander.
2642 Analyze_Type_Conversion (N);
2643 return;
2644 end if;
2646 if Is_Overloadable (U_N) then
2647 Process_Function_Call;
2649 elsif Ekind (Etype (P)) = E_Subprogram_Type
2650 or else (Is_Access_Type (Etype (P))
2651 and then
2652 Ekind (Designated_Type (Etype (P))) =
2653 E_Subprogram_Type)
2654 then
2655 -- Call to access_to-subprogram with possible implicit dereference
2657 Process_Function_Call;
2659 elsif Is_Generic_Subprogram (U_N) then
2661 -- A common beginner's (or C++ templates fan) error
2663 Error_Msg_N ("generic subprogram cannot be called", N);
2664 Set_Etype (N, Any_Type);
2665 return;
2667 else
2668 Process_Indexed_Component_Or_Slice;
2669 end if;
2671 -- If not an entity name, prefix is an expression that may denote
2672 -- an array or an access-to-subprogram.
2674 else
2675 if Ekind (P_T) = E_Subprogram_Type
2676 or else (Is_Access_Type (P_T)
2677 and then
2678 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2679 then
2680 Process_Function_Call;
2682 elsif Nkind (P) = N_Selected_Component
2683 and then Present (Entity (Selector_Name (P)))
2684 and then Is_Overloadable (Entity (Selector_Name (P)))
2685 then
2686 Process_Function_Call;
2688 -- In ASIS mode within a generic, a prefixed call is analyzed and
2689 -- partially rewritten but the original indexed component has not
2690 -- yet been rewritten as a call. Perform the replacement now.
2692 elsif Nkind (P) = N_Selected_Component
2693 and then Nkind (Parent (P)) = N_Function_Call
2694 and then ASIS_Mode
2695 then
2696 Rewrite (N, Parent (P));
2697 Analyze (N);
2699 else
2700 -- Indexed component, slice, or a call to a member of a family
2701 -- entry, which will be converted to an entry call later.
2703 Process_Indexed_Component_Or_Slice;
2704 end if;
2705 end if;
2707 Analyze_Dimension (N);
2708 end Analyze_Indexed_Component_Form;
2710 ------------------------
2711 -- Analyze_Logical_Op --
2712 ------------------------
2714 procedure Analyze_Logical_Op (N : Node_Id) is
2715 L : constant Node_Id := Left_Opnd (N);
2716 R : constant Node_Id := Right_Opnd (N);
2717 Op_Id : Entity_Id := Entity (N);
2719 begin
2720 Set_Etype (N, Any_Type);
2721 Candidate_Type := Empty;
2723 Analyze_Expression (L);
2724 Analyze_Expression (R);
2726 if Present (Op_Id) then
2728 if Ekind (Op_Id) = E_Operator then
2729 Find_Boolean_Types (L, R, Op_Id, N);
2730 else
2731 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2732 end if;
2734 else
2735 Op_Id := Get_Name_Entity_Id (Chars (N));
2736 while Present (Op_Id) loop
2737 if Ekind (Op_Id) = E_Operator then
2738 Find_Boolean_Types (L, R, Op_Id, N);
2739 else
2740 Analyze_User_Defined_Binary_Op (N, Op_Id);
2741 end if;
2743 Op_Id := Homonym (Op_Id);
2744 end loop;
2745 end if;
2747 Operator_Check (N);
2748 Check_Function_Writable_Actuals (N);
2749 end Analyze_Logical_Op;
2751 ---------------------------
2752 -- Analyze_Membership_Op --
2753 ---------------------------
2755 procedure Analyze_Membership_Op (N : Node_Id) is
2756 Loc : constant Source_Ptr := Sloc (N);
2757 L : constant Node_Id := Left_Opnd (N);
2758 R : constant Node_Id := Right_Opnd (N);
2760 Index : Interp_Index;
2761 It : Interp;
2762 Found : Boolean := False;
2763 I_F : Interp_Index;
2764 T_F : Entity_Id;
2766 procedure Try_One_Interp (T1 : Entity_Id);
2767 -- Routine to try one proposed interpretation. Note that the context
2768 -- of the operation plays no role in resolving the arguments, so that
2769 -- if there is more than one interpretation of the operands that is
2770 -- compatible with a membership test, the operation is ambiguous.
2772 --------------------
2773 -- Try_One_Interp --
2774 --------------------
2776 procedure Try_One_Interp (T1 : Entity_Id) is
2777 begin
2778 if Has_Compatible_Type (R, T1) then
2779 if Found
2780 and then Base_Type (T1) /= Base_Type (T_F)
2781 then
2782 It := Disambiguate (L, I_F, Index, Any_Type);
2784 if It = No_Interp then
2785 Ambiguous_Operands (N);
2786 Set_Etype (L, Any_Type);
2787 return;
2789 else
2790 T_F := It.Typ;
2791 end if;
2793 else
2794 Found := True;
2795 T_F := T1;
2796 I_F := Index;
2797 end if;
2799 Set_Etype (L, T_F);
2800 end if;
2801 end Try_One_Interp;
2803 procedure Analyze_Set_Membership;
2804 -- If a set of alternatives is present, analyze each and find the
2805 -- common type to which they must all resolve.
2807 ----------------------------
2808 -- Analyze_Set_Membership --
2809 ----------------------------
2811 procedure Analyze_Set_Membership is
2812 Alt : Node_Id;
2813 Index : Interp_Index;
2814 It : Interp;
2815 Candidate_Interps : Node_Id;
2816 Common_Type : Entity_Id := Empty;
2818 begin
2819 if Comes_From_Source (N) then
2820 Check_Compiler_Unit ("set membership", N);
2821 end if;
2823 Analyze (L);
2824 Candidate_Interps := L;
2826 if not Is_Overloaded (L) then
2827 Common_Type := Etype (L);
2829 Alt := First (Alternatives (N));
2830 while Present (Alt) loop
2831 Analyze (Alt);
2833 if not Has_Compatible_Type (Alt, Common_Type) then
2834 Wrong_Type (Alt, Common_Type);
2835 end if;
2837 Next (Alt);
2838 end loop;
2840 else
2841 Alt := First (Alternatives (N));
2842 while Present (Alt) loop
2843 Analyze (Alt);
2844 if not Is_Overloaded (Alt) then
2845 Common_Type := Etype (Alt);
2847 else
2848 Get_First_Interp (Alt, Index, It);
2849 while Present (It.Typ) loop
2850 if not
2851 Has_Compatible_Type (Candidate_Interps, It.Typ)
2852 then
2853 Remove_Interp (Index);
2854 end if;
2856 Get_Next_Interp (Index, It);
2857 end loop;
2859 Get_First_Interp (Alt, Index, It);
2861 if No (It.Typ) then
2862 Error_Msg_N ("alternative has no legal type", Alt);
2863 return;
2864 end if;
2866 -- If alternative is not overloaded, we have a unique type
2867 -- for all of them.
2869 Set_Etype (Alt, It.Typ);
2870 Get_Next_Interp (Index, It);
2872 if No (It.Typ) then
2873 Set_Is_Overloaded (Alt, False);
2874 Common_Type := Etype (Alt);
2875 end if;
2877 Candidate_Interps := Alt;
2878 end if;
2880 Next (Alt);
2881 end loop;
2882 end if;
2884 Set_Etype (N, Standard_Boolean);
2886 if Present (Common_Type) then
2887 Set_Etype (L, Common_Type);
2889 -- The left operand may still be overloaded, to be resolved using
2890 -- the Common_Type.
2892 else
2893 Error_Msg_N ("cannot resolve membership operation", N);
2894 end if;
2895 end Analyze_Set_Membership;
2897 -- Start of processing for Analyze_Membership_Op
2899 begin
2900 Analyze_Expression (L);
2902 if No (R) and then Ada_Version >= Ada_2012 then
2903 Analyze_Set_Membership;
2904 Check_Function_Writable_Actuals (N);
2906 return;
2907 end if;
2909 if Nkind (R) = N_Range
2910 or else (Nkind (R) = N_Attribute_Reference
2911 and then Attribute_Name (R) = Name_Range)
2912 then
2913 Analyze (R);
2915 if not Is_Overloaded (L) then
2916 Try_One_Interp (Etype (L));
2918 else
2919 Get_First_Interp (L, Index, It);
2920 while Present (It.Typ) loop
2921 Try_One_Interp (It.Typ);
2922 Get_Next_Interp (Index, It);
2923 end loop;
2924 end if;
2926 -- If not a range, it can be a subtype mark, or else it is a degenerate
2927 -- membership test with a singleton value, i.e. a test for equality,
2928 -- if the types are compatible.
2930 else
2931 Analyze (R);
2933 if Is_Entity_Name (R)
2934 and then Is_Type (Entity (R))
2935 then
2936 Find_Type (R);
2937 Check_Fully_Declared (Entity (R), R);
2939 elsif Ada_Version >= Ada_2012
2940 and then Has_Compatible_Type (R, Etype (L))
2941 then
2942 if Nkind (N) = N_In then
2943 Rewrite (N,
2944 Make_Op_Eq (Loc,
2945 Left_Opnd => L,
2946 Right_Opnd => R));
2947 else
2948 Rewrite (N,
2949 Make_Op_Ne (Loc,
2950 Left_Opnd => L,
2951 Right_Opnd => R));
2952 end if;
2954 Analyze (N);
2955 return;
2957 else
2958 -- In all versions of the language, if we reach this point there
2959 -- is a previous error that will be diagnosed below.
2961 Find_Type (R);
2962 end if;
2963 end if;
2965 -- Compatibility between expression and subtype mark or range is
2966 -- checked during resolution. The result of the operation is Boolean
2967 -- in any case.
2969 Set_Etype (N, Standard_Boolean);
2971 if Comes_From_Source (N)
2972 and then Present (Right_Opnd (N))
2973 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2974 then
2975 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2976 end if;
2978 Check_Function_Writable_Actuals (N);
2979 end Analyze_Membership_Op;
2981 -----------------
2982 -- Analyze_Mod --
2983 -----------------
2985 procedure Analyze_Mod (N : Node_Id) is
2986 begin
2987 -- A special warning check, if we have an expression of the form:
2988 -- expr mod 2 * literal
2989 -- where literal is 64 or less, then probably what was meant was
2990 -- expr mod 2 ** literal
2991 -- so issue an appropriate warning.
2993 if Warn_On_Suspicious_Modulus_Value
2994 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2995 and then Intval (Right_Opnd (N)) = Uint_2
2996 and then Nkind (Parent (N)) = N_Op_Multiply
2997 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2998 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2999 then
3000 Error_Msg_N
3001 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
3002 end if;
3004 -- Remaining processing is same as for other arithmetic operators
3006 Analyze_Arithmetic_Op (N);
3007 end Analyze_Mod;
3009 ----------------------
3010 -- Analyze_Negation --
3011 ----------------------
3013 procedure Analyze_Negation (N : Node_Id) is
3014 R : constant Node_Id := Right_Opnd (N);
3015 Op_Id : Entity_Id := Entity (N);
3017 begin
3018 Set_Etype (N, Any_Type);
3019 Candidate_Type := Empty;
3021 Analyze_Expression (R);
3023 if Present (Op_Id) then
3024 if Ekind (Op_Id) = E_Operator then
3025 Find_Negation_Types (R, Op_Id, N);
3026 else
3027 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3028 end if;
3030 else
3031 Op_Id := Get_Name_Entity_Id (Chars (N));
3032 while Present (Op_Id) loop
3033 if Ekind (Op_Id) = E_Operator then
3034 Find_Negation_Types (R, Op_Id, N);
3035 else
3036 Analyze_User_Defined_Unary_Op (N, Op_Id);
3037 end if;
3039 Op_Id := Homonym (Op_Id);
3040 end loop;
3041 end if;
3043 Operator_Check (N);
3044 end Analyze_Negation;
3046 ------------------
3047 -- Analyze_Null --
3048 ------------------
3050 procedure Analyze_Null (N : Node_Id) is
3051 begin
3052 Check_SPARK_05_Restriction ("null is not allowed", N);
3054 Set_Etype (N, Any_Access);
3055 end Analyze_Null;
3057 ----------------------
3058 -- Analyze_One_Call --
3059 ----------------------
3061 procedure Analyze_One_Call
3062 (N : Node_Id;
3063 Nam : Entity_Id;
3064 Report : Boolean;
3065 Success : out Boolean;
3066 Skip_First : Boolean := False)
3068 Actuals : constant List_Id := Parameter_Associations (N);
3069 Prev_T : constant Entity_Id := Etype (N);
3071 Must_Skip : constant Boolean := Skip_First
3072 or else Nkind (Original_Node (N)) = N_Selected_Component
3073 or else
3074 (Nkind (Original_Node (N)) = N_Indexed_Component
3075 and then Nkind (Prefix (Original_Node (N)))
3076 = N_Selected_Component);
3077 -- The first formal must be omitted from the match when trying to find
3078 -- a primitive operation that is a possible interpretation, and also
3079 -- after the call has been rewritten, because the corresponding actual
3080 -- is already known to be compatible, and because this may be an
3081 -- indexing of a call with default parameters.
3083 Formal : Entity_Id;
3084 Actual : Node_Id;
3085 Is_Indexed : Boolean := False;
3086 Is_Indirect : Boolean := False;
3087 Subp_Type : constant Entity_Id := Etype (Nam);
3088 Norm_OK : Boolean;
3090 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3091 -- There may be a user-defined operator that hides the current
3092 -- interpretation. We must check for this independently of the
3093 -- analysis of the call with the user-defined operation, because
3094 -- the parameter names may be wrong and yet the hiding takes place.
3095 -- This fixes a problem with ACATS test B34014O.
3097 -- When the type Address is a visible integer type, and the DEC
3098 -- system extension is visible, the predefined operator may be
3099 -- hidden as well, by one of the address operations in auxdec.
3100 -- Finally, The abstract operations on address do not hide the
3101 -- predefined operator (this is the purpose of making them abstract).
3103 procedure Indicate_Name_And_Type;
3104 -- If candidate interpretation matches, indicate name and type of
3105 -- result on call node.
3107 ----------------------------
3108 -- Indicate_Name_And_Type --
3109 ----------------------------
3111 procedure Indicate_Name_And_Type is
3112 begin
3113 Add_One_Interp (N, Nam, Etype (Nam));
3114 Check_Implicit_Dereference (N, Etype (Nam));
3115 Success := True;
3117 -- If the prefix of the call is a name, indicate the entity
3118 -- being called. If it is not a name, it is an expression that
3119 -- denotes an access to subprogram or else an entry or family. In
3120 -- the latter case, the name is a selected component, and the entity
3121 -- being called is noted on the selector.
3123 if not Is_Type (Nam) then
3124 if Is_Entity_Name (Name (N)) then
3125 Set_Entity (Name (N), Nam);
3126 Set_Etype (Name (N), Etype (Nam));
3128 elsif Nkind (Name (N)) = N_Selected_Component then
3129 Set_Entity (Selector_Name (Name (N)), Nam);
3130 end if;
3131 end if;
3133 if Debug_Flag_E and not Report then
3134 Write_Str (" Overloaded call ");
3135 Write_Int (Int (N));
3136 Write_Str (" compatible with ");
3137 Write_Int (Int (Nam));
3138 Write_Eol;
3139 end if;
3140 end Indicate_Name_And_Type;
3142 ------------------------
3143 -- Operator_Hidden_By --
3144 ------------------------
3146 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3147 Act1 : constant Node_Id := First_Actual (N);
3148 Act2 : constant Node_Id := Next_Actual (Act1);
3149 Form1 : constant Entity_Id := First_Formal (Fun);
3150 Form2 : constant Entity_Id := Next_Formal (Form1);
3152 begin
3153 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3154 return False;
3156 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3157 return False;
3159 elsif Present (Form2) then
3160 if No (Act2)
3161 or else not Has_Compatible_Type (Act2, Etype (Form2))
3162 then
3163 return False;
3164 end if;
3166 elsif Present (Act2) then
3167 return False;
3168 end if;
3170 -- Now we know that the arity of the operator matches the function,
3171 -- and the function call is a valid interpretation. The function
3172 -- hides the operator if it has the right signature, or if one of
3173 -- its operands is a non-abstract operation on Address when this is
3174 -- a visible integer type.
3176 return Hides_Op (Fun, Nam)
3177 or else Is_Descendant_Of_Address (Etype (Form1))
3178 or else
3179 (Present (Form2)
3180 and then Is_Descendant_Of_Address (Etype (Form2)));
3181 end Operator_Hidden_By;
3183 -- Start of processing for Analyze_One_Call
3185 begin
3186 Success := False;
3188 -- If the subprogram has no formals or if all the formals have defaults,
3189 -- and the return type is an array type, the node may denote an indexing
3190 -- of the result of a parameterless call. In Ada 2005, the subprogram
3191 -- may have one non-defaulted formal, and the call may have been written
3192 -- in prefix notation, so that the rebuilt parameter list has more than
3193 -- one actual.
3195 if not Is_Overloadable (Nam)
3196 and then Ekind (Nam) /= E_Subprogram_Type
3197 and then Ekind (Nam) /= E_Entry_Family
3198 then
3199 return;
3200 end if;
3202 -- An indexing requires at least one actual. The name of the call cannot
3203 -- be an implicit indirect call, so it cannot be a generated explicit
3204 -- dereference.
3206 if not Is_Empty_List (Actuals)
3207 and then
3208 (Needs_No_Actuals (Nam)
3209 or else
3210 (Needs_One_Actual (Nam)
3211 and then Present (Next_Actual (First (Actuals)))))
3212 then
3213 if Is_Array_Type (Subp_Type)
3214 and then
3215 (Nkind (Name (N)) /= N_Explicit_Dereference
3216 or else Comes_From_Source (Name (N)))
3217 then
3218 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3220 elsif Is_Access_Type (Subp_Type)
3221 and then Is_Array_Type (Designated_Type (Subp_Type))
3222 then
3223 Is_Indexed :=
3224 Try_Indexed_Call
3225 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3227 -- The prefix can also be a parameterless function that returns an
3228 -- access to subprogram, in which case this is an indirect call.
3229 -- If this succeeds, an explicit dereference is added later on,
3230 -- in Analyze_Call or Resolve_Call.
3232 elsif Is_Access_Type (Subp_Type)
3233 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3234 then
3235 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3236 end if;
3238 end if;
3240 -- If the call has been transformed into a slice, it is of the form
3241 -- F (Subtype) where F is parameterless. The node has been rewritten in
3242 -- Try_Indexed_Call and there is nothing else to do.
3244 if Is_Indexed
3245 and then Nkind (N) = N_Slice
3246 then
3247 return;
3248 end if;
3250 Normalize_Actuals
3251 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3253 if not Norm_OK then
3255 -- If an indirect call is a possible interpretation, indicate
3256 -- success to the caller. This may be an indexing of an explicit
3257 -- dereference of a call that returns an access type (see above).
3259 if Is_Indirect
3260 or else (Is_Indexed
3261 and then Nkind (Name (N)) = N_Explicit_Dereference
3262 and then Comes_From_Source (Name (N)))
3263 then
3264 Success := True;
3265 return;
3267 -- Mismatch in number or names of parameters
3269 elsif Debug_Flag_E then
3270 Write_Str (" normalization fails in call ");
3271 Write_Int (Int (N));
3272 Write_Str (" with subprogram ");
3273 Write_Int (Int (Nam));
3274 Write_Eol;
3275 end if;
3277 -- If the context expects a function call, discard any interpretation
3278 -- that is a procedure. If the node is not overloaded, leave as is for
3279 -- better error reporting when type mismatch is found.
3281 elsif Nkind (N) = N_Function_Call
3282 and then Is_Overloaded (Name (N))
3283 and then Ekind (Nam) = E_Procedure
3284 then
3285 return;
3287 -- Ditto for function calls in a procedure context
3289 elsif Nkind (N) = N_Procedure_Call_Statement
3290 and then Is_Overloaded (Name (N))
3291 and then Etype (Nam) /= Standard_Void_Type
3292 then
3293 return;
3295 elsif No (Actuals) then
3297 -- If Normalize succeeds, then there are default parameters for
3298 -- all formals.
3300 Indicate_Name_And_Type;
3302 elsif Ekind (Nam) = E_Operator then
3303 if Nkind (N) = N_Procedure_Call_Statement then
3304 return;
3305 end if;
3307 -- This can occur when the prefix of the call is an operator
3308 -- name or an expanded name whose selector is an operator name.
3310 Analyze_Operator_Call (N, Nam);
3312 if Etype (N) /= Prev_T then
3314 -- Check that operator is not hidden by a function interpretation
3316 if Is_Overloaded (Name (N)) then
3317 declare
3318 I : Interp_Index;
3319 It : Interp;
3321 begin
3322 Get_First_Interp (Name (N), I, It);
3323 while Present (It.Nam) loop
3324 if Operator_Hidden_By (It.Nam) then
3325 Set_Etype (N, Prev_T);
3326 return;
3327 end if;
3329 Get_Next_Interp (I, It);
3330 end loop;
3331 end;
3332 end if;
3334 -- If operator matches formals, record its name on the call.
3335 -- If the operator is overloaded, Resolve will select the
3336 -- correct one from the list of interpretations. The call
3337 -- node itself carries the first candidate.
3339 Set_Entity (Name (N), Nam);
3340 Success := True;
3342 elsif Report and then Etype (N) = Any_Type then
3343 Error_Msg_N ("incompatible arguments for operator", N);
3344 end if;
3346 else
3347 -- Normalize_Actuals has chained the named associations in the
3348 -- correct order of the formals.
3350 Actual := First_Actual (N);
3351 Formal := First_Formal (Nam);
3353 -- If we are analyzing a call rewritten from object notation, skip
3354 -- first actual, which may be rewritten later as an explicit
3355 -- dereference.
3357 if Must_Skip then
3358 Next_Actual (Actual);
3359 Next_Formal (Formal);
3360 end if;
3362 while Present (Actual) and then Present (Formal) loop
3363 if Nkind (Parent (Actual)) /= N_Parameter_Association
3364 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3365 then
3366 -- The actual can be compatible with the formal, but we must
3367 -- also check that the context is not an address type that is
3368 -- visibly an integer type. In this case the use of literals is
3369 -- illegal, except in the body of descendants of system, where
3370 -- arithmetic operations on address are of course used.
3372 if Has_Compatible_Type (Actual, Etype (Formal))
3373 and then
3374 (Etype (Actual) /= Universal_Integer
3375 or else not Is_Descendant_Of_Address (Etype (Formal))
3376 or else
3377 Is_Predefined_File_Name
3378 (Unit_File_Name (Get_Source_Unit (N))))
3379 then
3380 Next_Actual (Actual);
3381 Next_Formal (Formal);
3383 -- In Allow_Integer_Address mode, we allow an actual integer to
3384 -- match a formal address type and vice versa. We only do this
3385 -- if we are certain that an error will otherwise be issued
3387 elsif Address_Integer_Convert_OK
3388 (Etype (Actual), Etype (Formal))
3389 and then (Report and not Is_Indexed and not Is_Indirect)
3390 then
3391 -- Handle this case by introducing an unchecked conversion
3393 Rewrite (Actual,
3394 Unchecked_Convert_To (Etype (Formal),
3395 Relocate_Node (Actual)));
3396 Analyze_And_Resolve (Actual, Etype (Formal));
3397 Next_Actual (Actual);
3398 Next_Formal (Formal);
3400 -- Under relaxed RM semantics silently replace occurrences of
3401 -- null by System.Address_Null. We only do this if we know that
3402 -- an error will otherwise be issued.
3404 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3405 and then (Report and not Is_Indexed and not Is_Indirect)
3406 then
3407 Replace_Null_By_Null_Address (Actual);
3408 Analyze_And_Resolve (Actual, Etype (Formal));
3409 Next_Actual (Actual);
3410 Next_Formal (Formal);
3412 -- For an Ada 2012 predicate or invariant, a call may mention
3413 -- an incomplete type, while resolution of the corresponding
3414 -- predicate function may see the full view, as a consequence
3415 -- of the delayed resolution of the corresponding expressions.
3417 elsif Ekind (Etype (Formal)) = E_Incomplete_Type
3418 and then Full_View (Etype (Formal)) = Etype (Actual)
3419 then
3420 Set_Etype (Formal, Etype (Actual));
3421 Next_Actual (Actual);
3422 Next_Formal (Formal);
3424 else
3425 if Debug_Flag_E then
3426 Write_Str (" type checking fails in call ");
3427 Write_Int (Int (N));
3428 Write_Str (" with formal ");
3429 Write_Int (Int (Formal));
3430 Write_Str (" in subprogram ");
3431 Write_Int (Int (Nam));
3432 Write_Eol;
3433 end if;
3435 -- Comment needed on the following test???
3437 if Report and not Is_Indexed and not Is_Indirect then
3439 -- Ada 2005 (AI-251): Complete the error notification
3440 -- to help new Ada 2005 users.
3442 if Is_Class_Wide_Type (Etype (Formal))
3443 and then Is_Interface (Etype (Etype (Formal)))
3444 and then not Interface_Present_In_Ancestor
3445 (Typ => Etype (Actual),
3446 Iface => Etype (Etype (Formal)))
3447 then
3448 Error_Msg_NE
3449 ("(Ada 2005) does not implement interface }",
3450 Actual, Etype (Etype (Formal)));
3451 end if;
3453 Wrong_Type (Actual, Etype (Formal));
3455 if Nkind (Actual) = N_Op_Eq
3456 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3457 then
3458 Formal := First_Formal (Nam);
3459 while Present (Formal) loop
3460 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3461 Error_Msg_N -- CODEFIX
3462 ("possible misspelling of `='>`!", Actual);
3463 exit;
3464 end if;
3466 Next_Formal (Formal);
3467 end loop;
3468 end if;
3470 if All_Errors_Mode then
3471 Error_Msg_Sloc := Sloc (Nam);
3473 if Etype (Formal) = Any_Type then
3474 Error_Msg_N
3475 ("there is no legal actual parameter", Actual);
3476 end if;
3478 if Is_Overloadable (Nam)
3479 and then Present (Alias (Nam))
3480 and then not Comes_From_Source (Nam)
3481 then
3482 Error_Msg_NE
3483 ("\\ =='> in call to inherited operation & #!",
3484 Actual, Nam);
3486 elsif Ekind (Nam) = E_Subprogram_Type then
3487 declare
3488 Access_To_Subprogram_Typ :
3489 constant Entity_Id :=
3490 Defining_Identifier
3491 (Associated_Node_For_Itype (Nam));
3492 begin
3493 Error_Msg_NE
3494 ("\\ =='> in call to dereference of &#!",
3495 Actual, Access_To_Subprogram_Typ);
3496 end;
3498 else
3499 Error_Msg_NE
3500 ("\\ =='> in call to &#!", Actual, Nam);
3502 end if;
3503 end if;
3504 end if;
3506 return;
3507 end if;
3509 else
3510 -- Normalize_Actuals has verified that a default value exists
3511 -- for this formal. Current actual names a subsequent formal.
3513 Next_Formal (Formal);
3514 end if;
3515 end loop;
3517 -- On exit, all actuals match
3519 Indicate_Name_And_Type;
3520 end if;
3521 end Analyze_One_Call;
3523 ---------------------------
3524 -- Analyze_Operator_Call --
3525 ---------------------------
3527 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3528 Op_Name : constant Name_Id := Chars (Op_Id);
3529 Act1 : constant Node_Id := First_Actual (N);
3530 Act2 : constant Node_Id := Next_Actual (Act1);
3532 begin
3533 -- Binary operator case
3535 if Present (Act2) then
3537 -- If more than two operands, then not binary operator after all
3539 if Present (Next_Actual (Act2)) then
3540 return;
3541 end if;
3543 -- Otherwise action depends on operator
3545 case Op_Name is
3546 when Name_Op_Add |
3547 Name_Op_Subtract |
3548 Name_Op_Multiply |
3549 Name_Op_Divide |
3550 Name_Op_Mod |
3551 Name_Op_Rem |
3552 Name_Op_Expon =>
3553 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3555 when Name_Op_And |
3556 Name_Op_Or |
3557 Name_Op_Xor =>
3558 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3560 when Name_Op_Lt |
3561 Name_Op_Le |
3562 Name_Op_Gt |
3563 Name_Op_Ge =>
3564 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3566 when Name_Op_Eq |
3567 Name_Op_Ne =>
3568 Find_Equality_Types (Act1, Act2, Op_Id, N);
3570 when Name_Op_Concat =>
3571 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3573 -- Is this when others, or should it be an abort???
3575 when others =>
3576 null;
3577 end case;
3579 -- Unary operator case
3581 else
3582 case Op_Name is
3583 when Name_Op_Subtract |
3584 Name_Op_Add |
3585 Name_Op_Abs =>
3586 Find_Unary_Types (Act1, Op_Id, N);
3588 when Name_Op_Not =>
3589 Find_Negation_Types (Act1, Op_Id, N);
3591 -- Is this when others correct, or should it be an abort???
3593 when others =>
3594 null;
3595 end case;
3596 end if;
3597 end Analyze_Operator_Call;
3599 -------------------------------------------
3600 -- Analyze_Overloaded_Selected_Component --
3601 -------------------------------------------
3603 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3604 Nam : constant Node_Id := Prefix (N);
3605 Sel : constant Node_Id := Selector_Name (N);
3606 Comp : Entity_Id;
3607 I : Interp_Index;
3608 It : Interp;
3609 T : Entity_Id;
3611 begin
3612 Set_Etype (Sel, Any_Type);
3614 Get_First_Interp (Nam, I, It);
3615 while Present (It.Typ) loop
3616 if Is_Access_Type (It.Typ) then
3617 T := Designated_Type (It.Typ);
3618 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3619 else
3620 T := It.Typ;
3621 end if;
3623 -- Locate the component. For a private prefix the selector can denote
3624 -- a discriminant.
3626 if Is_Record_Type (T) or else Is_Private_Type (T) then
3628 -- If the prefix is a class-wide type, the visible components are
3629 -- those of the base type.
3631 if Is_Class_Wide_Type (T) then
3632 T := Etype (T);
3633 end if;
3635 Comp := First_Entity (T);
3636 while Present (Comp) loop
3637 if Chars (Comp) = Chars (Sel)
3638 and then Is_Visible_Component (Comp)
3639 then
3641 -- AI05-105: if the context is an object renaming with
3642 -- an anonymous access type, the expected type of the
3643 -- object must be anonymous. This is a name resolution rule.
3645 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3646 or else No (Access_Definition (Parent (N)))
3647 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3648 or else
3649 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3650 then
3651 Set_Entity (Sel, Comp);
3652 Set_Etype (Sel, Etype (Comp));
3653 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3654 Check_Implicit_Dereference (N, Etype (Comp));
3656 -- This also specifies a candidate to resolve the name.
3657 -- Further overloading will be resolved from context.
3658 -- The selector name itself does not carry overloading
3659 -- information.
3661 Set_Etype (Nam, It.Typ);
3663 else
3664 -- Named access type in the context of a renaming
3665 -- declaration with an access definition. Remove
3666 -- inapplicable candidate.
3668 Remove_Interp (I);
3669 end if;
3670 end if;
3672 Next_Entity (Comp);
3673 end loop;
3675 elsif Is_Concurrent_Type (T) then
3676 Comp := First_Entity (T);
3677 while Present (Comp)
3678 and then Comp /= First_Private_Entity (T)
3679 loop
3680 if Chars (Comp) = Chars (Sel) then
3681 if Is_Overloadable (Comp) then
3682 Add_One_Interp (Sel, Comp, Etype (Comp));
3683 else
3684 Set_Entity_With_Checks (Sel, Comp);
3685 Generate_Reference (Comp, Sel);
3686 end if;
3688 Set_Etype (Sel, Etype (Comp));
3689 Set_Etype (N, Etype (Comp));
3690 Set_Etype (Nam, It.Typ);
3692 -- For access type case, introduce explicit dereference for
3693 -- more uniform treatment of entry calls. Do this only once
3694 -- if several interpretations yield an access type.
3696 if Is_Access_Type (Etype (Nam))
3697 and then Nkind (Nam) /= N_Explicit_Dereference
3698 then
3699 Insert_Explicit_Dereference (Nam);
3700 Error_Msg_NW
3701 (Warn_On_Dereference, "?d?implicit dereference", N);
3702 end if;
3703 end if;
3705 Next_Entity (Comp);
3706 end loop;
3708 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3709 end if;
3711 Get_Next_Interp (I, It);
3712 end loop;
3714 if Etype (N) = Any_Type
3715 and then not Try_Object_Operation (N)
3716 then
3717 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3718 Set_Entity (Sel, Any_Id);
3719 Set_Etype (Sel, Any_Type);
3720 end if;
3721 end Analyze_Overloaded_Selected_Component;
3723 ----------------------------------
3724 -- Analyze_Qualified_Expression --
3725 ----------------------------------
3727 procedure Analyze_Qualified_Expression (N : Node_Id) is
3728 Mark : constant Entity_Id := Subtype_Mark (N);
3729 Expr : constant Node_Id := Expression (N);
3730 I : Interp_Index;
3731 It : Interp;
3732 T : Entity_Id;
3734 begin
3735 Analyze_Expression (Expr);
3737 Set_Etype (N, Any_Type);
3738 Find_Type (Mark);
3739 T := Entity (Mark);
3740 Set_Etype (N, T);
3742 if T = Any_Type then
3743 return;
3744 end if;
3746 Check_Fully_Declared (T, N);
3748 -- If expected type is class-wide, check for exact match before
3749 -- expansion, because if the expression is a dispatching call it
3750 -- may be rewritten as explicit dereference with class-wide result.
3751 -- If expression is overloaded, retain only interpretations that
3752 -- will yield exact matches.
3754 if Is_Class_Wide_Type (T) then
3755 if not Is_Overloaded (Expr) then
3756 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3757 if Nkind (Expr) = N_Aggregate then
3758 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3759 else
3760 Wrong_Type (Expr, T);
3761 end if;
3762 end if;
3764 else
3765 Get_First_Interp (Expr, I, It);
3767 while Present (It.Nam) loop
3768 if Base_Type (It.Typ) /= Base_Type (T) then
3769 Remove_Interp (I);
3770 end if;
3772 Get_Next_Interp (I, It);
3773 end loop;
3774 end if;
3775 end if;
3777 Set_Etype (N, T);
3778 end Analyze_Qualified_Expression;
3780 -----------------------------------
3781 -- Analyze_Quantified_Expression --
3782 -----------------------------------
3784 procedure Analyze_Quantified_Expression (N : Node_Id) is
3785 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3786 -- If the iterator is part of a quantified expression, and the range is
3787 -- known to be statically empty, emit a warning and replace expression
3788 -- with its static value. Returns True if the replacement occurs.
3790 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3791 -- Determine whether if expression If_Expr lacks an else part or if it
3792 -- has one, it evaluates to True.
3794 --------------------
3795 -- Is_Empty_Range --
3796 --------------------
3798 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
3799 Loc : constant Source_Ptr := Sloc (N);
3801 begin
3802 if Is_Array_Type (Typ)
3803 and then Compile_Time_Known_Bounds (Typ)
3804 and then
3805 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
3806 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
3807 then
3808 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3810 if All_Present (N) then
3811 Error_Msg_N
3812 ("??quantified expression with ALL "
3813 & "over a null range has value True", N);
3814 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3816 else
3817 Error_Msg_N
3818 ("??quantified expression with SOME "
3819 & "over a null range has value False", N);
3820 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3821 end if;
3823 Analyze (N);
3824 return True;
3826 else
3827 return False;
3828 end if;
3829 end Is_Empty_Range;
3831 -----------------------------
3832 -- No_Else_Or_Trivial_True --
3833 -----------------------------
3835 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
3836 Else_Expr : constant Node_Id :=
3837 Next (Next (First (Expressions (If_Expr))));
3838 begin
3839 return
3840 No (Else_Expr)
3841 or else (Compile_Time_Known_Value (Else_Expr)
3842 and then Is_True (Expr_Value (Else_Expr)));
3843 end No_Else_Or_Trivial_True;
3845 -- Local variables
3847 Cond : constant Node_Id := Condition (N);
3848 Loop_Id : Entity_Id;
3849 QE_Scop : Entity_Id;
3851 -- Start of processing for Analyze_Quantified_Expression
3853 begin
3854 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
3856 -- Create a scope to emulate the loop-like behavior of the quantified
3857 -- expression. The scope is needed to provide proper visibility of the
3858 -- loop variable.
3860 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
3861 Set_Etype (QE_Scop, Standard_Void_Type);
3862 Set_Scope (QE_Scop, Current_Scope);
3863 Set_Parent (QE_Scop, N);
3865 Push_Scope (QE_Scop);
3867 -- All constituents are preanalyzed and resolved to avoid untimely
3868 -- generation of various temporaries and types. Full analysis and
3869 -- expansion is carried out when the quantified expression is
3870 -- transformed into an expression with actions.
3872 if Present (Iterator_Specification (N)) then
3873 Preanalyze (Iterator_Specification (N));
3875 -- Do not proceed with the analysis when the range of iteration is
3876 -- empty. The appropriate error is issued by Is_Empty_Range.
3878 if Is_Entity_Name (Name (Iterator_Specification (N)))
3879 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
3880 then
3881 return;
3882 end if;
3884 else pragma Assert (Present (Loop_Parameter_Specification (N)));
3885 declare
3886 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
3888 begin
3889 Preanalyze (Loop_Par);
3891 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
3892 and then Parent (Loop_Par) /= N
3893 then
3894 -- The parser cannot distinguish between a loop specification
3895 -- and an iterator specification. If after pre-analysis the
3896 -- proper form has been recognized, rewrite the expression to
3897 -- reflect the right kind. This is needed for proper ASIS
3898 -- navigation. If expansion is enabled, the transformation is
3899 -- performed when the expression is rewritten as a loop.
3901 Set_Iterator_Specification (N,
3902 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
3904 Set_Defining_Identifier (Iterator_Specification (N),
3905 Relocate_Node (Defining_Identifier (Loop_Par)));
3906 Set_Name (Iterator_Specification (N),
3907 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
3908 Set_Comes_From_Source (Iterator_Specification (N),
3909 Comes_From_Source (Loop_Parameter_Specification (N)));
3910 Set_Loop_Parameter_Specification (N, Empty);
3911 end if;
3912 end;
3913 end if;
3915 Preanalyze_And_Resolve (Cond, Standard_Boolean);
3917 End_Scope;
3918 Set_Etype (N, Standard_Boolean);
3920 -- Verify that the loop variable is used within the condition of the
3921 -- quantified expression.
3923 if Present (Iterator_Specification (N)) then
3924 Loop_Id := Defining_Identifier (Iterator_Specification (N));
3925 else
3926 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
3927 end if;
3929 if Warn_On_Suspicious_Contract
3930 and then not Referenced (Loop_Id, Cond)
3931 then
3932 -- Generating C, this check causes spurious warnings on inlined
3933 -- postconditions; we can safely disable it because this check
3934 -- was previously performed when analyzing the internally built
3935 -- postconditions procedure.
3937 if Modify_Tree_For_C and then In_Inlined_Body then
3938 null;
3939 else
3940 Error_Msg_N ("?T?unused variable &", Loop_Id);
3941 end if;
3942 end if;
3944 -- Diagnose a possible misuse of the SOME existential quantifier. When
3945 -- we have a quantified expression of the form:
3947 -- for some X => (if P then Q [else True])
3949 -- any value for X that makes P False results in the if expression being
3950 -- trivially True, and so also results in the quantified expression
3951 -- being trivially True.
3953 if Warn_On_Suspicious_Contract
3954 and then not All_Present (N)
3955 and then Nkind (Cond) = N_If_Expression
3956 and then No_Else_Or_Trivial_True (Cond)
3957 then
3958 Error_Msg_N ("?T?suspicious expression", N);
3959 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
3960 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
3961 end if;
3962 end Analyze_Quantified_Expression;
3964 -------------------
3965 -- Analyze_Range --
3966 -------------------
3968 procedure Analyze_Range (N : Node_Id) is
3969 L : constant Node_Id := Low_Bound (N);
3970 H : constant Node_Id := High_Bound (N);
3971 I1, I2 : Interp_Index;
3972 It1, It2 : Interp;
3974 procedure Check_Common_Type (T1, T2 : Entity_Id);
3975 -- Verify the compatibility of two types, and choose the
3976 -- non universal one if the other is universal.
3978 procedure Check_High_Bound (T : Entity_Id);
3979 -- Test one interpretation of the low bound against all those
3980 -- of the high bound.
3982 procedure Check_Universal_Expression (N : Node_Id);
3983 -- In Ada 83, reject bounds of a universal range that are not literals
3984 -- or entity names.
3986 -----------------------
3987 -- Check_Common_Type --
3988 -----------------------
3990 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3991 begin
3992 if Covers (T1 => T1, T2 => T2)
3993 or else
3994 Covers (T1 => T2, T2 => T1)
3995 then
3996 if T1 = Universal_Integer
3997 or else T1 = Universal_Real
3998 or else T1 = Any_Character
3999 then
4000 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4002 elsif T1 = T2 then
4003 Add_One_Interp (N, T1, T1);
4005 else
4006 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4007 end if;
4008 end if;
4009 end Check_Common_Type;
4011 ----------------------
4012 -- Check_High_Bound --
4013 ----------------------
4015 procedure Check_High_Bound (T : Entity_Id) is
4016 begin
4017 if not Is_Overloaded (H) then
4018 Check_Common_Type (T, Etype (H));
4019 else
4020 Get_First_Interp (H, I2, It2);
4021 while Present (It2.Typ) loop
4022 Check_Common_Type (T, It2.Typ);
4023 Get_Next_Interp (I2, It2);
4024 end loop;
4025 end if;
4026 end Check_High_Bound;
4028 -----------------------------
4029 -- Is_Universal_Expression --
4030 -----------------------------
4032 procedure Check_Universal_Expression (N : Node_Id) is
4033 begin
4034 if Etype (N) = Universal_Integer
4035 and then Nkind (N) /= N_Integer_Literal
4036 and then not Is_Entity_Name (N)
4037 and then Nkind (N) /= N_Attribute_Reference
4038 then
4039 Error_Msg_N ("illegal bound in discrete range", N);
4040 end if;
4041 end Check_Universal_Expression;
4043 -- Start of processing for Analyze_Range
4045 begin
4046 Set_Etype (N, Any_Type);
4047 Analyze_Expression (L);
4048 Analyze_Expression (H);
4050 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4051 return;
4053 else
4054 if not Is_Overloaded (L) then
4055 Check_High_Bound (Etype (L));
4056 else
4057 Get_First_Interp (L, I1, It1);
4058 while Present (It1.Typ) loop
4059 Check_High_Bound (It1.Typ);
4060 Get_Next_Interp (I1, It1);
4061 end loop;
4062 end if;
4064 -- If result is Any_Type, then we did not find a compatible pair
4066 if Etype (N) = Any_Type then
4067 Error_Msg_N ("incompatible types in range ", N);
4068 end if;
4069 end if;
4071 if Ada_Version = Ada_83
4072 and then
4073 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4074 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4075 then
4076 Check_Universal_Expression (L);
4077 Check_Universal_Expression (H);
4078 end if;
4080 Check_Function_Writable_Actuals (N);
4081 end Analyze_Range;
4083 -----------------------
4084 -- Analyze_Reference --
4085 -----------------------
4087 procedure Analyze_Reference (N : Node_Id) is
4088 P : constant Node_Id := Prefix (N);
4089 E : Entity_Id;
4090 T : Entity_Id;
4091 Acc_Type : Entity_Id;
4093 begin
4094 Analyze (P);
4096 -- An interesting error check, if we take the 'Ref of an object for
4097 -- which a pragma Atomic or Volatile has been given, and the type of the
4098 -- object is not Atomic or Volatile, then we are in trouble. The problem
4099 -- is that no trace of the atomic/volatile status will remain for the
4100 -- backend to respect when it deals with the resulting pointer, since
4101 -- the pointer type will not be marked atomic (it is a pointer to the
4102 -- base type of the object).
4104 -- It is not clear if that can ever occur, but in case it does, we will
4105 -- generate an error message. Not clear if this message can ever be
4106 -- generated, and pretty clear that it represents a bug if it is, still
4107 -- seems worth checking, except in CodePeer mode where we do not really
4108 -- care and don't want to bother the user.
4110 T := Etype (P);
4112 if Is_Entity_Name (P)
4113 and then Is_Object_Reference (P)
4114 and then not CodePeer_Mode
4115 then
4116 E := Entity (P);
4117 T := Etype (P);
4119 if (Has_Atomic_Components (E)
4120 and then not Has_Atomic_Components (T))
4121 or else
4122 (Has_Volatile_Components (E)
4123 and then not Has_Volatile_Components (T))
4124 or else (Is_Atomic (E) and then not Is_Atomic (T))
4125 or else (Is_Volatile (E) and then not Is_Volatile (T))
4126 then
4127 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4128 end if;
4129 end if;
4131 -- Carry on with normal processing
4133 Acc_Type := Create_Itype (E_Allocator_Type, N);
4134 Set_Etype (Acc_Type, Acc_Type);
4135 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4136 Set_Etype (N, Acc_Type);
4137 end Analyze_Reference;
4139 --------------------------------
4140 -- Analyze_Selected_Component --
4141 --------------------------------
4143 -- Prefix is a record type or a task or protected type. In the latter case,
4144 -- the selector must denote a visible entry.
4146 procedure Analyze_Selected_Component (N : Node_Id) is
4147 Name : constant Node_Id := Prefix (N);
4148 Sel : constant Node_Id := Selector_Name (N);
4149 Act_Decl : Node_Id;
4150 Comp : Entity_Id;
4151 Has_Candidate : Boolean := False;
4152 In_Scope : Boolean;
4153 Parent_N : Node_Id;
4154 Pent : Entity_Id := Empty;
4155 Prefix_Type : Entity_Id;
4157 Type_To_Use : Entity_Id;
4158 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4159 -- a class-wide type, we use its root type, whose components are
4160 -- present in the class-wide type.
4162 Is_Single_Concurrent_Object : Boolean;
4163 -- Set True if the prefix is a single task or a single protected object
4165 procedure Find_Component_In_Instance (Rec : Entity_Id);
4166 -- In an instance, a component of a private extension may not be visible
4167 -- while it was visible in the generic. Search candidate scope for a
4168 -- component with the proper identifier. This is only done if all other
4169 -- searches have failed. If a match is found, the Etype of both N and
4170 -- Sel are set from this component, and the entity of Sel is set to
4171 -- reference this component. If no match is found, Entity (Sel) remains
4172 -- unset. For a derived type that is an actual of the instance, the
4173 -- desired component may be found in any ancestor.
4175 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4176 -- It is known that the parent of N denotes a subprogram call. Comp
4177 -- is an overloadable component of the concurrent type of the prefix.
4178 -- Determine whether all formals of the parent of N and Comp are mode
4179 -- conformant. If the parent node is not analyzed yet it may be an
4180 -- indexed component rather than a function call.
4182 function Has_Dereference (Nod : Node_Id) return Boolean;
4183 -- Check whether prefix includes a dereference at any level.
4185 --------------------------------
4186 -- Find_Component_In_Instance --
4187 --------------------------------
4189 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4190 Comp : Entity_Id;
4191 Typ : Entity_Id;
4193 begin
4194 Typ := Rec;
4195 while Present (Typ) loop
4196 Comp := First_Component (Typ);
4197 while Present (Comp) loop
4198 if Chars (Comp) = Chars (Sel) then
4199 Set_Entity_With_Checks (Sel, Comp);
4200 Set_Etype (Sel, Etype (Comp));
4201 Set_Etype (N, Etype (Comp));
4202 return;
4203 end if;
4205 Next_Component (Comp);
4206 end loop;
4208 -- If not found, the component may be declared in the parent
4209 -- type or its full view, if any.
4211 if Is_Derived_Type (Typ) then
4212 Typ := Etype (Typ);
4214 if Is_Private_Type (Typ) then
4215 Typ := Full_View (Typ);
4216 end if;
4218 else
4219 return;
4220 end if;
4221 end loop;
4223 -- If we fall through, no match, so no changes made
4225 return;
4226 end Find_Component_In_Instance;
4228 ------------------------------
4229 -- Has_Mode_Conformant_Spec --
4230 ------------------------------
4232 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4233 Comp_Param : Entity_Id;
4234 Param : Node_Id;
4235 Param_Typ : Entity_Id;
4237 begin
4238 Comp_Param := First_Formal (Comp);
4240 if Nkind (Parent (N)) = N_Indexed_Component then
4241 Param := First (Expressions (Parent (N)));
4242 else
4243 Param := First (Parameter_Associations (Parent (N)));
4244 end if;
4246 while Present (Comp_Param)
4247 and then Present (Param)
4248 loop
4249 Param_Typ := Find_Parameter_Type (Param);
4251 if Present (Param_Typ)
4252 and then
4253 not Conforming_Types
4254 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4255 then
4256 return False;
4257 end if;
4259 Next_Formal (Comp_Param);
4260 Next (Param);
4261 end loop;
4263 -- One of the specs has additional formals; there is no match, unless
4264 -- this may be an indexing of a parameterless call.
4266 -- Note that when expansion is disabled, the corresponding record
4267 -- type of synchronized types is not constructed, so that there is
4268 -- no point is attempting an interpretation as a prefixed call, as
4269 -- this is bound to fail because the primitive operations will not
4270 -- be properly located.
4272 if Present (Comp_Param) or else Present (Param) then
4273 if Needs_No_Actuals (Comp)
4274 and then Is_Array_Type (Etype (Comp))
4275 and then not Expander_Active
4276 then
4277 return True;
4278 else
4279 return False;
4280 end if;
4281 end if;
4283 return True;
4284 end Has_Mode_Conformant_Spec;
4286 ---------------------
4287 -- Has_Dereference --
4288 ---------------------
4290 function Has_Dereference (Nod : Node_Id) return Boolean is
4291 begin
4292 if Nkind (Nod) = N_Explicit_Dereference then
4293 return True;
4295 -- When expansion is disabled an explicit dereference may not have
4296 -- been inserted, but if this is an access type the indirection makes
4297 -- the call safe.
4299 elsif Is_Access_Type (Etype (Nod)) then
4300 return True;
4302 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4303 return Has_Dereference (Prefix (Nod));
4305 else
4306 return False;
4307 end if;
4308 end Has_Dereference;
4310 -- Start of processing for Analyze_Selected_Component
4312 begin
4313 Set_Etype (N, Any_Type);
4315 if Is_Overloaded (Name) then
4316 Analyze_Overloaded_Selected_Component (N);
4317 return;
4319 elsif Etype (Name) = Any_Type then
4320 Set_Entity (Sel, Any_Id);
4321 Set_Etype (Sel, Any_Type);
4322 return;
4324 else
4325 Prefix_Type := Etype (Name);
4326 end if;
4328 if Is_Access_Type (Prefix_Type) then
4330 -- A RACW object can never be used as prefix of a selected component
4331 -- since that means it is dereferenced without being a controlling
4332 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4333 -- reporting an error, we must check whether this is actually a
4334 -- dispatching call in prefix form.
4336 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4337 and then Comes_From_Source (N)
4338 then
4339 if Try_Object_Operation (N) then
4340 return;
4341 else
4342 Error_Msg_N
4343 ("invalid dereference of a remote access-to-class-wide value",
4345 end if;
4347 -- Normal case of selected component applied to access type
4349 else
4350 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4352 if Is_Entity_Name (Name) then
4353 Pent := Entity (Name);
4354 elsif Nkind (Name) = N_Selected_Component
4355 and then Is_Entity_Name (Selector_Name (Name))
4356 then
4357 Pent := Entity (Selector_Name (Name));
4358 end if;
4360 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4361 end if;
4363 -- If we have an explicit dereference of a remote access-to-class-wide
4364 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4365 -- have to check for the case of a prefix that is a controlling operand
4366 -- of a prefixed dispatching call, as the dereference is legal in that
4367 -- case. Normally this condition is checked in Validate_Remote_Access_
4368 -- To_Class_Wide_Type, but we have to defer the checking for selected
4369 -- component prefixes because of the prefixed dispatching call case.
4370 -- Note that implicit dereferences are checked for this just above.
4372 elsif Nkind (Name) = N_Explicit_Dereference
4373 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4374 and then Comes_From_Source (N)
4375 then
4376 if Try_Object_Operation (N) then
4377 return;
4378 else
4379 Error_Msg_N
4380 ("invalid dereference of a remote access-to-class-wide value",
4382 end if;
4383 end if;
4385 -- (Ada 2005): if the prefix is the limited view of a type, and
4386 -- the context already includes the full view, use the full view
4387 -- in what follows, either to retrieve a component of to find
4388 -- a primitive operation. If the prefix is an explicit dereference,
4389 -- set the type of the prefix to reflect this transformation.
4390 -- If the non-limited view is itself an incomplete type, get the
4391 -- full view if available.
4393 if From_Limited_With (Prefix_Type)
4394 and then Has_Non_Limited_View (Prefix_Type)
4395 then
4396 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4398 if Nkind (N) = N_Explicit_Dereference then
4399 Set_Etype (Prefix (N), Prefix_Type);
4400 end if;
4401 end if;
4403 if Ekind (Prefix_Type) = E_Private_Subtype then
4404 Prefix_Type := Base_Type (Prefix_Type);
4405 end if;
4407 Type_To_Use := Prefix_Type;
4409 -- For class-wide types, use the entity list of the root type. This
4410 -- indirection is specially important for private extensions because
4411 -- only the root type get switched (not the class-wide type).
4413 if Is_Class_Wide_Type (Prefix_Type) then
4414 Type_To_Use := Root_Type (Prefix_Type);
4415 end if;
4417 -- If the prefix is a single concurrent object, use its name in error
4418 -- messages, rather than that of its anonymous type.
4420 Is_Single_Concurrent_Object :=
4421 Is_Concurrent_Type (Prefix_Type)
4422 and then Is_Internal_Name (Chars (Prefix_Type))
4423 and then not Is_Derived_Type (Prefix_Type)
4424 and then Is_Entity_Name (Name);
4426 Comp := First_Entity (Type_To_Use);
4428 -- If the selector has an original discriminant, the node appears in
4429 -- an instance. Replace the discriminant with the corresponding one
4430 -- in the current discriminated type. For nested generics, this must
4431 -- be done transitively, so note the new original discriminant.
4433 if Nkind (Sel) = N_Identifier
4434 and then In_Instance
4435 and then Present (Original_Discriminant (Sel))
4436 then
4437 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4439 -- Mark entity before rewriting, for completeness and because
4440 -- subsequent semantic checks might examine the original node.
4442 Set_Entity (Sel, Comp);
4443 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4444 Set_Original_Discriminant (Selector_Name (N), Comp);
4445 Set_Etype (N, Etype (Comp));
4446 Check_Implicit_Dereference (N, Etype (Comp));
4448 if Is_Access_Type (Etype (Name)) then
4449 Insert_Explicit_Dereference (Name);
4450 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4451 end if;
4453 elsif Is_Record_Type (Prefix_Type) then
4455 -- Find component with given name. In an instance, if the node is
4456 -- known as a prefixed call, do not examine components whose
4457 -- visibility may be accidental.
4459 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4460 if Chars (Comp) = Chars (Sel)
4461 and then Is_Visible_Component (Comp, N)
4462 then
4463 Set_Entity_With_Checks (Sel, Comp);
4464 Set_Etype (Sel, Etype (Comp));
4466 if Ekind (Comp) = E_Discriminant then
4467 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4468 Error_Msg_N
4469 ("cannot reference discriminant of unchecked union",
4470 Sel);
4471 end if;
4473 if Is_Generic_Type (Prefix_Type)
4474 or else
4475 Is_Generic_Type (Root_Type (Prefix_Type))
4476 then
4477 Set_Original_Discriminant (Sel, Comp);
4478 end if;
4479 end if;
4481 -- Resolve the prefix early otherwise it is not possible to
4482 -- build the actual subtype of the component: it may need
4483 -- to duplicate this prefix and duplication is only allowed
4484 -- on fully resolved expressions.
4486 Resolve (Name);
4488 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4489 -- subtypes in a package specification.
4490 -- Example:
4492 -- limited with Pkg;
4493 -- package Pkg is
4494 -- type Acc_Inc is access Pkg.T;
4495 -- X : Acc_Inc;
4496 -- N : Natural := X.all.Comp; -- ERROR, limited view
4497 -- end Pkg; -- Comp is not visible
4499 if Nkind (Name) = N_Explicit_Dereference
4500 and then From_Limited_With (Etype (Prefix (Name)))
4501 and then not Is_Potentially_Use_Visible (Etype (Name))
4502 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4503 N_Package_Specification
4504 then
4505 Error_Msg_NE
4506 ("premature usage of incomplete}", Prefix (Name),
4507 Etype (Prefix (Name)));
4508 end if;
4510 -- We never need an actual subtype for the case of a selection
4511 -- for a indexed component of a non-packed array, since in
4512 -- this case gigi generates all the checks and can find the
4513 -- necessary bounds information.
4515 -- We also do not need an actual subtype for the case of a
4516 -- first, last, length, or range attribute applied to a
4517 -- non-packed array, since gigi can again get the bounds in
4518 -- these cases (gigi cannot handle the packed case, since it
4519 -- has the bounds of the packed array type, not the original
4520 -- bounds of the type). However, if the prefix is itself a
4521 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4522 -- as a dynamic-sized temporary, so we do generate an actual
4523 -- subtype for this case.
4525 Parent_N := Parent (N);
4527 if not Is_Packed (Etype (Comp))
4528 and then
4529 ((Nkind (Parent_N) = N_Indexed_Component
4530 and then Nkind (Name) /= N_Selected_Component)
4531 or else
4532 (Nkind (Parent_N) = N_Attribute_Reference
4533 and then
4534 Nam_In (Attribute_Name (Parent_N), Name_First,
4535 Name_Last,
4536 Name_Length,
4537 Name_Range)))
4538 then
4539 Set_Etype (N, Etype (Comp));
4541 -- If full analysis is not enabled, we do not generate an
4542 -- actual subtype, because in the absence of expansion
4543 -- reference to a formal of a protected type, for example,
4544 -- will not be properly transformed, and will lead to
4545 -- out-of-scope references in gigi.
4547 -- In all other cases, we currently build an actual subtype.
4548 -- It seems likely that many of these cases can be avoided,
4549 -- but right now, the front end makes direct references to the
4550 -- bounds (e.g. in generating a length check), and if we do
4551 -- not make an actual subtype, we end up getting a direct
4552 -- reference to a discriminant, which will not do.
4554 elsif Full_Analysis then
4555 Act_Decl :=
4556 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4557 Insert_Action (N, Act_Decl);
4559 if No (Act_Decl) then
4560 Set_Etype (N, Etype (Comp));
4562 else
4563 -- Component type depends on discriminants. Enter the
4564 -- main attributes of the subtype.
4566 declare
4567 Subt : constant Entity_Id :=
4568 Defining_Identifier (Act_Decl);
4570 begin
4571 Set_Etype (Subt, Base_Type (Etype (Comp)));
4572 Set_Ekind (Subt, Ekind (Etype (Comp)));
4573 Set_Etype (N, Subt);
4574 end;
4575 end if;
4577 -- If Full_Analysis not enabled, just set the Etype
4579 else
4580 Set_Etype (N, Etype (Comp));
4581 end if;
4583 Check_Implicit_Dereference (N, Etype (N));
4584 return;
4585 end if;
4587 -- If the prefix is a private extension, check only the visible
4588 -- components of the partial view. This must include the tag,
4589 -- which can appear in expanded code in a tag check.
4591 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4592 and then Chars (Selector_Name (N)) /= Name_uTag
4593 then
4594 exit when Comp = Last_Entity (Type_To_Use);
4595 end if;
4597 Next_Entity (Comp);
4598 end loop;
4600 -- Ada 2005 (AI-252): The selected component can be interpreted as
4601 -- a prefixed view of a subprogram. Depending on the context, this is
4602 -- either a name that can appear in a renaming declaration, or part
4603 -- of an enclosing call given in prefix form.
4605 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4606 -- selected component should resolve to a name.
4608 if Ada_Version >= Ada_2005
4609 and then Is_Tagged_Type (Prefix_Type)
4610 and then not Is_Concurrent_Type (Prefix_Type)
4611 then
4612 if Nkind (Parent (N)) = N_Generic_Association
4613 or else Nkind (Parent (N)) = N_Requeue_Statement
4614 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4615 then
4616 if Find_Primitive_Operation (N) then
4617 return;
4618 end if;
4620 elsif Try_Object_Operation (N) then
4621 return;
4622 end if;
4624 -- If the transformation fails, it will be necessary to redo the
4625 -- analysis with all errors enabled, to indicate candidate
4626 -- interpretations and reasons for each failure ???
4628 end if;
4630 elsif Is_Private_Type (Prefix_Type) then
4632 -- Allow access only to discriminants of the type. If the type has
4633 -- no full view, gigi uses the parent type for the components, so we
4634 -- do the same here.
4636 if No (Full_View (Prefix_Type)) then
4637 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4638 Comp := First_Entity (Type_To_Use);
4639 end if;
4641 while Present (Comp) loop
4642 if Chars (Comp) = Chars (Sel) then
4643 if Ekind (Comp) = E_Discriminant then
4644 Set_Entity_With_Checks (Sel, Comp);
4645 Generate_Reference (Comp, Sel);
4647 Set_Etype (Sel, Etype (Comp));
4648 Set_Etype (N, Etype (Comp));
4649 Check_Implicit_Dereference (N, Etype (N));
4651 if Is_Generic_Type (Prefix_Type)
4652 or else Is_Generic_Type (Root_Type (Prefix_Type))
4653 then
4654 Set_Original_Discriminant (Sel, Comp);
4655 end if;
4657 -- Before declaring an error, check whether this is tagged
4658 -- private type and a call to a primitive operation.
4660 elsif Ada_Version >= Ada_2005
4661 and then Is_Tagged_Type (Prefix_Type)
4662 and then Try_Object_Operation (N)
4663 then
4664 return;
4666 else
4667 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4668 Error_Msg_NE ("invisible selector& for }", N, Sel);
4669 Set_Entity (Sel, Any_Id);
4670 Set_Etype (N, Any_Type);
4671 end if;
4673 return;
4674 end if;
4676 Next_Entity (Comp);
4677 end loop;
4679 elsif Is_Concurrent_Type (Prefix_Type) then
4681 -- Find visible operation with given name. For a protected type,
4682 -- the possible candidates are discriminants, entries or protected
4683 -- procedures. For a task type, the set can only include entries or
4684 -- discriminants if the task type is not an enclosing scope. If it
4685 -- is an enclosing scope (e.g. in an inner task) then all entities
4686 -- are visible, but the prefix must denote the enclosing scope, i.e.
4687 -- can only be a direct name or an expanded name.
4689 Set_Etype (Sel, Any_Type);
4690 In_Scope := In_Open_Scopes (Prefix_Type);
4692 while Present (Comp) loop
4693 if Chars (Comp) = Chars (Sel) then
4694 if Is_Overloadable (Comp) then
4695 Add_One_Interp (Sel, Comp, Etype (Comp));
4697 -- If the prefix is tagged, the correct interpretation may
4698 -- lie in the primitive or class-wide operations of the
4699 -- type. Perform a simple conformance check to determine
4700 -- whether Try_Object_Operation should be invoked even if
4701 -- a visible entity is found.
4703 if Is_Tagged_Type (Prefix_Type)
4704 and then
4705 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4706 N_Function_Call,
4707 N_Indexed_Component)
4708 and then Has_Mode_Conformant_Spec (Comp)
4709 then
4710 Has_Candidate := True;
4711 end if;
4713 -- Note: a selected component may not denote a component of a
4714 -- protected type (4.1.3(7)).
4716 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4717 or else (In_Scope
4718 and then not Is_Protected_Type (Prefix_Type)
4719 and then Is_Entity_Name (Name))
4720 then
4721 Set_Entity_With_Checks (Sel, Comp);
4722 Generate_Reference (Comp, Sel);
4724 -- The selector is not overloadable, so we have a candidate
4725 -- interpretation.
4727 Has_Candidate := True;
4729 else
4730 goto Next_Comp;
4731 end if;
4733 Set_Etype (Sel, Etype (Comp));
4734 Set_Etype (N, Etype (Comp));
4736 if Ekind (Comp) = E_Discriminant then
4737 Set_Original_Discriminant (Sel, Comp);
4738 end if;
4740 -- For access type case, introduce explicit dereference for
4741 -- more uniform treatment of entry calls.
4743 if Is_Access_Type (Etype (Name)) then
4744 Insert_Explicit_Dereference (Name);
4745 Error_Msg_NW
4746 (Warn_On_Dereference, "?d?implicit dereference", N);
4747 end if;
4748 end if;
4750 <<Next_Comp>>
4751 Next_Entity (Comp);
4752 exit when not In_Scope
4753 and then
4754 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4755 end loop;
4757 -- If the scope is a current instance, the prefix cannot be an
4758 -- expression of the same type, unless the selector designates a
4759 -- public operation (otherwise that would represent an attempt to
4760 -- reach an internal entity of another synchronized object).
4761 -- This is legal if prefix is an access to such type and there is
4762 -- a dereference, or is a component with a dereferenced prefix.
4763 -- It is also legal if the prefix is a component of a task type,
4764 -- and the selector is one of the task operations.
4766 if In_Scope
4767 and then not Is_Entity_Name (Name)
4768 and then not Has_Dereference (Name)
4769 then
4770 if Is_Task_Type (Prefix_Type)
4771 and then Present (Entity (Sel))
4772 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
4773 then
4774 null;
4776 else
4777 Error_Msg_NE
4778 ("invalid reference to internal operation of some object of "
4779 & "type &", N, Type_To_Use);
4780 Set_Entity (Sel, Any_Id);
4781 Set_Etype (Sel, Any_Type);
4782 return;
4783 end if;
4784 end if;
4786 -- If there is no visible entity with the given name or none of the
4787 -- visible entities are plausible interpretations, check whether
4788 -- there is some other primitive operation with that name.
4790 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
4791 if (Etype (N) = Any_Type
4792 or else not Has_Candidate)
4793 and then Try_Object_Operation (N)
4794 then
4795 return;
4797 -- If the context is not syntactically a procedure call, it
4798 -- may be a call to a primitive function declared outside of
4799 -- the synchronized type.
4801 -- If the context is a procedure call, there might still be
4802 -- an overloading between an entry and a primitive procedure
4803 -- declared outside of the synchronized type, called in prefix
4804 -- notation. This is harder to disambiguate because in one case
4805 -- the controlling formal is implicit ???
4807 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4808 and then Nkind (Parent (N)) /= N_Indexed_Component
4809 and then Try_Object_Operation (N)
4810 then
4811 return;
4812 end if;
4814 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4815 -- entry or procedure of a tagged concurrent type we must check
4816 -- if there are class-wide subprograms covering the primitive. If
4817 -- true then Try_Object_Operation reports the error.
4819 if Has_Candidate
4820 and then Is_Concurrent_Type (Prefix_Type)
4821 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4822 then
4823 -- Duplicate the call. This is required to avoid problems with
4824 -- the tree transformations performed by Try_Object_Operation.
4825 -- Set properly the parent of the copied call, because it is
4826 -- about to be reanalyzed.
4828 declare
4829 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4831 begin
4832 Set_Parent (Par, Parent (Parent (N)));
4834 if Try_Object_Operation
4835 (Sinfo.Name (Par), CW_Test_Only => True)
4836 then
4837 return;
4838 end if;
4839 end;
4840 end if;
4841 end if;
4843 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4845 -- Case of a prefix of a protected type: selector might denote
4846 -- an invisible private component.
4848 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4849 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4850 Next_Entity (Comp);
4851 end loop;
4853 if Present (Comp) then
4854 if Is_Single_Concurrent_Object then
4855 Error_Msg_Node_2 := Entity (Name);
4856 Error_Msg_NE ("invisible selector& for &", N, Sel);
4858 else
4859 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4860 Error_Msg_NE ("invisible selector& for }", N, Sel);
4861 end if;
4862 return;
4863 end if;
4864 end if;
4866 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4868 else
4869 -- Invalid prefix
4871 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4872 end if;
4874 -- If N still has no type, the component is not defined in the prefix
4876 if Etype (N) = Any_Type then
4878 if Is_Single_Concurrent_Object then
4879 Error_Msg_Node_2 := Entity (Name);
4880 Error_Msg_NE ("no selector& for&", N, Sel);
4882 Check_Misspelled_Selector (Type_To_Use, Sel);
4884 -- If this is a derived formal type, the parent may have different
4885 -- visibility at this point. Try for an inherited component before
4886 -- reporting an error.
4888 elsif Is_Generic_Type (Prefix_Type)
4889 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4890 and then Prefix_Type /= Etype (Prefix_Type)
4891 and then Is_Record_Type (Etype (Prefix_Type))
4892 then
4893 Set_Etype (Prefix (N), Etype (Prefix_Type));
4894 Analyze_Selected_Component (N);
4895 return;
4897 -- Similarly, if this is the actual for a formal derived type, or
4898 -- a derived type thereof, the component inherited from the generic
4899 -- parent may not be visible in the actual, but the selected
4900 -- component is legal. Climb up the derivation chain of the generic
4901 -- parent type until we find the proper ancestor type.
4903 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
4904 declare
4905 Par : Entity_Id := Prefix_Type;
4906 begin
4907 -- Climb up derivation chain to generic actual subtype
4909 while not Is_Generic_Actual_Type (Par) loop
4910 if Ekind (Par) = E_Record_Type then
4911 Par := Parent_Subtype (Par);
4912 exit when No (Par);
4913 else
4914 exit when Par = Etype (Par);
4915 Par := Etype (Par);
4916 end if;
4917 end loop;
4919 if Present (Par) and then Is_Generic_Actual_Type (Par) then
4921 -- Now look for component in ancestor types
4923 Par := Generic_Parent_Type (Declaration_Node (Par));
4924 loop
4925 Find_Component_In_Instance (Par);
4926 exit when Present (Entity (Sel))
4927 or else Par = Etype (Par);
4928 Par := Etype (Par);
4929 end loop;
4931 -- Another special case: the type is an extension of a private
4932 -- type T, is an actual in an instance, and we are in the body
4933 -- of the instance, so the generic body had a full view of the
4934 -- type declaration for T or of some ancestor that defines the
4935 -- component in question.
4937 elsif Is_Derived_Type (Type_To_Use)
4938 and then Used_As_Generic_Actual (Type_To_Use)
4939 and then In_Instance_Body
4940 then
4941 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
4943 -- In ASIS mode the generic parent type may be absent. Examine
4944 -- the parent type directly for a component that may have been
4945 -- visible in a parent generic unit.
4947 elsif Is_Derived_Type (Prefix_Type) then
4948 Par := Etype (Prefix_Type);
4949 Find_Component_In_Instance (Par);
4950 end if;
4951 end;
4953 -- The search above must have eventually succeeded, since the
4954 -- selected component was legal in the generic.
4956 if No (Entity (Sel)) then
4957 raise Program_Error;
4958 end if;
4960 return;
4962 -- Component not found, specialize error message when appropriate
4964 else
4965 if Ekind (Prefix_Type) = E_Record_Subtype then
4967 -- Check whether this is a component of the base type which
4968 -- is absent from a statically constrained subtype. This will
4969 -- raise constraint error at run time, but is not a compile-
4970 -- time error. When the selector is illegal for base type as
4971 -- well fall through and generate a compilation error anyway.
4973 Comp := First_Component (Base_Type (Prefix_Type));
4974 while Present (Comp) loop
4975 if Chars (Comp) = Chars (Sel)
4976 and then Is_Visible_Component (Comp)
4977 then
4978 Set_Entity_With_Checks (Sel, Comp);
4979 Generate_Reference (Comp, Sel);
4980 Set_Etype (Sel, Etype (Comp));
4981 Set_Etype (N, Etype (Comp));
4983 -- Emit appropriate message. The node will be replaced
4984 -- by an appropriate raise statement.
4986 -- Note that in SPARK mode, as with all calls to apply a
4987 -- compile time constraint error, this will be made into
4988 -- an error to simplify the processing of the formal
4989 -- verification backend.
4991 Apply_Compile_Time_Constraint_Error
4992 (N, "component not present in }??",
4993 CE_Discriminant_Check_Failed,
4994 Ent => Prefix_Type, Rep => False);
4996 Set_Raises_Constraint_Error (N);
4997 return;
4998 end if;
5000 Next_Component (Comp);
5001 end loop;
5003 end if;
5005 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5006 Error_Msg_NE ("no selector& for}", N, Sel);
5008 -- Add information in the case of an incomplete prefix
5010 if Is_Incomplete_Type (Type_To_Use) then
5011 declare
5012 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5014 begin
5015 if From_Limited_With (Scope (Type_To_Use)) then
5016 Error_Msg_NE
5017 ("\limited view of& has no components", N, Inc);
5019 else
5020 Error_Msg_NE
5021 ("\premature usage of incomplete type&", N, Inc);
5023 if Nkind (Parent (Inc)) =
5024 N_Incomplete_Type_Declaration
5025 then
5026 -- Record location of premature use in entity so that
5027 -- a continuation message is generated when the
5028 -- completion is seen.
5030 Set_Premature_Use (Parent (Inc), N);
5031 end if;
5032 end if;
5033 end;
5034 end if;
5036 Check_Misspelled_Selector (Type_To_Use, Sel);
5037 end if;
5039 Set_Entity (Sel, Any_Id);
5040 Set_Etype (Sel, Any_Type);
5041 end if;
5042 end Analyze_Selected_Component;
5044 ---------------------------
5045 -- Analyze_Short_Circuit --
5046 ---------------------------
5048 procedure Analyze_Short_Circuit (N : Node_Id) is
5049 L : constant Node_Id := Left_Opnd (N);
5050 R : constant Node_Id := Right_Opnd (N);
5051 Ind : Interp_Index;
5052 It : Interp;
5054 begin
5055 Analyze_Expression (L);
5056 Analyze_Expression (R);
5057 Set_Etype (N, Any_Type);
5059 if not Is_Overloaded (L) then
5060 if Root_Type (Etype (L)) = Standard_Boolean
5061 and then Has_Compatible_Type (R, Etype (L))
5062 then
5063 Add_One_Interp (N, Etype (L), Etype (L));
5064 end if;
5066 else
5067 Get_First_Interp (L, Ind, It);
5068 while Present (It.Typ) loop
5069 if Root_Type (It.Typ) = Standard_Boolean
5070 and then Has_Compatible_Type (R, It.Typ)
5071 then
5072 Add_One_Interp (N, It.Typ, It.Typ);
5073 end if;
5075 Get_Next_Interp (Ind, It);
5076 end loop;
5077 end if;
5079 -- Here we have failed to find an interpretation. Clearly we know that
5080 -- it is not the case that both operands can have an interpretation of
5081 -- Boolean, but this is by far the most likely intended interpretation.
5082 -- So we simply resolve both operands as Booleans, and at least one of
5083 -- these resolutions will generate an error message, and we do not need
5084 -- to give another error message on the short circuit operation itself.
5086 if Etype (N) = Any_Type then
5087 Resolve (L, Standard_Boolean);
5088 Resolve (R, Standard_Boolean);
5089 Set_Etype (N, Standard_Boolean);
5090 end if;
5091 end Analyze_Short_Circuit;
5093 -------------------
5094 -- Analyze_Slice --
5095 -------------------
5097 procedure Analyze_Slice (N : Node_Id) is
5098 D : constant Node_Id := Discrete_Range (N);
5099 P : constant Node_Id := Prefix (N);
5100 Array_Type : Entity_Id;
5101 Index_Type : Entity_Id;
5103 procedure Analyze_Overloaded_Slice;
5104 -- If the prefix is overloaded, select those interpretations that
5105 -- yield a one-dimensional array type.
5107 ------------------------------
5108 -- Analyze_Overloaded_Slice --
5109 ------------------------------
5111 procedure Analyze_Overloaded_Slice is
5112 I : Interp_Index;
5113 It : Interp;
5114 Typ : Entity_Id;
5116 begin
5117 Set_Etype (N, Any_Type);
5119 Get_First_Interp (P, I, It);
5120 while Present (It.Nam) loop
5121 Typ := It.Typ;
5123 if Is_Access_Type (Typ) then
5124 Typ := Designated_Type (Typ);
5125 Error_Msg_NW
5126 (Warn_On_Dereference, "?d?implicit dereference", N);
5127 end if;
5129 if Is_Array_Type (Typ)
5130 and then Number_Dimensions (Typ) = 1
5131 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5132 then
5133 Add_One_Interp (N, Typ, Typ);
5134 end if;
5136 Get_Next_Interp (I, It);
5137 end loop;
5139 if Etype (N) = Any_Type then
5140 Error_Msg_N ("expect array type in prefix of slice", N);
5141 end if;
5142 end Analyze_Overloaded_Slice;
5144 -- Start of processing for Analyze_Slice
5146 begin
5147 if Comes_From_Source (N) then
5148 Check_SPARK_05_Restriction ("slice is not allowed", N);
5149 end if;
5151 Analyze (P);
5152 Analyze (D);
5154 if Is_Overloaded (P) then
5155 Analyze_Overloaded_Slice;
5157 else
5158 Array_Type := Etype (P);
5159 Set_Etype (N, Any_Type);
5161 if Is_Access_Type (Array_Type) then
5162 Array_Type := Designated_Type (Array_Type);
5163 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5164 end if;
5166 if not Is_Array_Type (Array_Type) then
5167 Wrong_Type (P, Any_Array);
5169 elsif Number_Dimensions (Array_Type) > 1 then
5170 Error_Msg_N
5171 ("type is not one-dimensional array in slice prefix", N);
5173 else
5174 if Ekind (Array_Type) = E_String_Literal_Subtype then
5175 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5176 else
5177 Index_Type := Etype (First_Index (Array_Type));
5178 end if;
5180 if not Has_Compatible_Type (D, Index_Type) then
5181 Wrong_Type (D, Index_Type);
5182 else
5183 Set_Etype (N, Array_Type);
5184 end if;
5185 end if;
5186 end if;
5187 end Analyze_Slice;
5189 -----------------------------
5190 -- Analyze_Type_Conversion --
5191 -----------------------------
5193 procedure Analyze_Type_Conversion (N : Node_Id) is
5194 Expr : constant Node_Id := Expression (N);
5195 Typ : Entity_Id;
5197 begin
5198 -- If Conversion_OK is set, then the Etype is already set, and the only
5199 -- processing required is to analyze the expression. This is used to
5200 -- construct certain "illegal" conversions which are not allowed by Ada
5201 -- semantics, but can be handled by Gigi, see Sinfo for further details.
5203 if Conversion_OK (N) then
5204 Analyze (Expr);
5205 return;
5206 end if;
5208 -- Otherwise full type analysis is required, as well as some semantic
5209 -- checks to make sure the argument of the conversion is appropriate.
5211 Find_Type (Subtype_Mark (N));
5212 Typ := Entity (Subtype_Mark (N));
5213 Set_Etype (N, Typ);
5214 Check_Fully_Declared (Typ, N);
5215 Analyze_Expression (Expr);
5216 Validate_Remote_Type_Type_Conversion (N);
5218 -- Only remaining step is validity checks on the argument. These
5219 -- are skipped if the conversion does not come from the source.
5221 if not Comes_From_Source (N) then
5222 return;
5224 -- If there was an error in a generic unit, no need to replicate the
5225 -- error message. Conversely, constant-folding in the generic may
5226 -- transform the argument of a conversion into a string literal, which
5227 -- is legal. Therefore the following tests are not performed in an
5228 -- instance. The same applies to an inlined body.
5230 elsif In_Instance or In_Inlined_Body then
5231 return;
5233 elsif Nkind (Expr) = N_Null then
5234 Error_Msg_N ("argument of conversion cannot be null", N);
5235 Error_Msg_N ("\use qualified expression instead", N);
5236 Set_Etype (N, Any_Type);
5238 elsif Nkind (Expr) = N_Aggregate then
5239 Error_Msg_N ("argument of conversion cannot be aggregate", N);
5240 Error_Msg_N ("\use qualified expression instead", N);
5242 elsif Nkind (Expr) = N_Allocator then
5243 Error_Msg_N ("argument of conversion cannot be an allocator", N);
5244 Error_Msg_N ("\use qualified expression instead", N);
5246 elsif Nkind (Expr) = N_String_Literal then
5247 Error_Msg_N ("argument of conversion cannot be string literal", N);
5248 Error_Msg_N ("\use qualified expression instead", N);
5250 elsif Nkind (Expr) = N_Character_Literal then
5251 if Ada_Version = Ada_83 then
5252 Resolve (Expr, Typ);
5253 else
5254 Error_Msg_N ("argument of conversion cannot be character literal",
5256 Error_Msg_N ("\use qualified expression instead", N);
5257 end if;
5259 elsif Nkind (Expr) = N_Attribute_Reference
5260 and then Nam_In (Attribute_Name (Expr), Name_Access,
5261 Name_Unchecked_Access,
5262 Name_Unrestricted_Access)
5263 then
5264 Error_Msg_N ("argument of conversion cannot be access", N);
5265 Error_Msg_N ("\use qualified expression instead", N);
5266 end if;
5268 -- A formal parameter of a specific tagged type whose related subprogram
5269 -- is subject to pragma Extensions_Visible with value "False" cannot
5270 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5271 -- internally generated expressions.
5273 if Is_Class_Wide_Type (Typ)
5274 and then Comes_From_Source (Expr)
5275 and then Is_EVF_Expression (Expr)
5276 then
5277 Error_Msg_N
5278 ("formal parameter cannot be converted to class-wide type when "
5279 & "Extensions_Visible is False", Expr);
5280 end if;
5281 end Analyze_Type_Conversion;
5283 ----------------------
5284 -- Analyze_Unary_Op --
5285 ----------------------
5287 procedure Analyze_Unary_Op (N : Node_Id) is
5288 R : constant Node_Id := Right_Opnd (N);
5289 Op_Id : Entity_Id := Entity (N);
5291 begin
5292 Set_Etype (N, Any_Type);
5293 Candidate_Type := Empty;
5295 Analyze_Expression (R);
5297 if Present (Op_Id) then
5298 if Ekind (Op_Id) = E_Operator then
5299 Find_Unary_Types (R, Op_Id, N);
5300 else
5301 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5302 end if;
5304 else
5305 Op_Id := Get_Name_Entity_Id (Chars (N));
5306 while Present (Op_Id) loop
5307 if Ekind (Op_Id) = E_Operator then
5308 if No (Next_Entity (First_Entity (Op_Id))) then
5309 Find_Unary_Types (R, Op_Id, N);
5310 end if;
5312 elsif Is_Overloadable (Op_Id) then
5313 Analyze_User_Defined_Unary_Op (N, Op_Id);
5314 end if;
5316 Op_Id := Homonym (Op_Id);
5317 end loop;
5318 end if;
5320 Operator_Check (N);
5321 end Analyze_Unary_Op;
5323 ----------------------------------
5324 -- Analyze_Unchecked_Expression --
5325 ----------------------------------
5327 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5328 begin
5329 Analyze (Expression (N), Suppress => All_Checks);
5330 Set_Etype (N, Etype (Expression (N)));
5331 Save_Interps (Expression (N), N);
5332 end Analyze_Unchecked_Expression;
5334 ---------------------------------------
5335 -- Analyze_Unchecked_Type_Conversion --
5336 ---------------------------------------
5338 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5339 begin
5340 Find_Type (Subtype_Mark (N));
5341 Analyze_Expression (Expression (N));
5342 Set_Etype (N, Entity (Subtype_Mark (N)));
5343 end Analyze_Unchecked_Type_Conversion;
5345 ------------------------------------
5346 -- Analyze_User_Defined_Binary_Op --
5347 ------------------------------------
5349 procedure Analyze_User_Defined_Binary_Op
5350 (N : Node_Id;
5351 Op_Id : Entity_Id)
5353 begin
5354 -- Only do analysis if the operator Comes_From_Source, since otherwise
5355 -- the operator was generated by the expander, and all such operators
5356 -- always refer to the operators in package Standard.
5358 if Comes_From_Source (N) then
5359 declare
5360 F1 : constant Entity_Id := First_Formal (Op_Id);
5361 F2 : constant Entity_Id := Next_Formal (F1);
5363 begin
5364 -- Verify that Op_Id is a visible binary function. Note that since
5365 -- we know Op_Id is overloaded, potentially use visible means use
5366 -- visible for sure (RM 9.4(11)).
5368 if Ekind (Op_Id) = E_Function
5369 and then Present (F2)
5370 and then (Is_Immediately_Visible (Op_Id)
5371 or else Is_Potentially_Use_Visible (Op_Id))
5372 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5373 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5374 then
5375 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5377 -- If the left operand is overloaded, indicate that the current
5378 -- type is a viable candidate. This is redundant in most cases,
5379 -- but for equality and comparison operators where the context
5380 -- does not impose a type on the operands, setting the proper
5381 -- type is necessary to avoid subsequent ambiguities during
5382 -- resolution, when both user-defined and predefined operators
5383 -- may be candidates.
5385 if Is_Overloaded (Left_Opnd (N)) then
5386 Set_Etype (Left_Opnd (N), Etype (F1));
5387 end if;
5389 if Debug_Flag_E then
5390 Write_Str ("user defined operator ");
5391 Write_Name (Chars (Op_Id));
5392 Write_Str (" on node ");
5393 Write_Int (Int (N));
5394 Write_Eol;
5395 end if;
5396 end if;
5397 end;
5398 end if;
5399 end Analyze_User_Defined_Binary_Op;
5401 -----------------------------------
5402 -- Analyze_User_Defined_Unary_Op --
5403 -----------------------------------
5405 procedure Analyze_User_Defined_Unary_Op
5406 (N : Node_Id;
5407 Op_Id : Entity_Id)
5409 begin
5410 -- Only do analysis if the operator Comes_From_Source, since otherwise
5411 -- the operator was generated by the expander, and all such operators
5412 -- always refer to the operators in package Standard.
5414 if Comes_From_Source (N) then
5415 declare
5416 F : constant Entity_Id := First_Formal (Op_Id);
5418 begin
5419 -- Verify that Op_Id is a visible unary function. Note that since
5420 -- we know Op_Id is overloaded, potentially use visible means use
5421 -- visible for sure (RM 9.4(11)).
5423 if Ekind (Op_Id) = E_Function
5424 and then No (Next_Formal (F))
5425 and then (Is_Immediately_Visible (Op_Id)
5426 or else Is_Potentially_Use_Visible (Op_Id))
5427 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5428 then
5429 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5430 end if;
5431 end;
5432 end if;
5433 end Analyze_User_Defined_Unary_Op;
5435 ---------------------------
5436 -- Check_Arithmetic_Pair --
5437 ---------------------------
5439 procedure Check_Arithmetic_Pair
5440 (T1, T2 : Entity_Id;
5441 Op_Id : Entity_Id;
5442 N : Node_Id)
5444 Op_Name : constant Name_Id := Chars (Op_Id);
5446 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5447 -- Check whether the fixed-point type Typ has a user-defined operator
5448 -- (multiplication or division) that should hide the corresponding
5449 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5450 -- such operators more visible and therefore useful.
5452 -- If the name of the operation is an expanded name with prefix
5453 -- Standard, the predefined universal fixed operator is available,
5454 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5456 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5457 -- Get specific type (i.e. non-universal type if there is one)
5459 ------------------
5460 -- Has_Fixed_Op --
5461 ------------------
5463 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5464 Bas : constant Entity_Id := Base_Type (Typ);
5465 Ent : Entity_Id;
5466 F1 : Entity_Id;
5467 F2 : Entity_Id;
5469 begin
5470 -- If the universal_fixed operation is given explicitly the rule
5471 -- concerning primitive operations of the type do not apply.
5473 if Nkind (N) = N_Function_Call
5474 and then Nkind (Name (N)) = N_Expanded_Name
5475 and then Entity (Prefix (Name (N))) = Standard_Standard
5476 then
5477 return False;
5478 end if;
5480 -- The operation is treated as primitive if it is declared in the
5481 -- same scope as the type, and therefore on the same entity chain.
5483 Ent := Next_Entity (Typ);
5484 while Present (Ent) loop
5485 if Chars (Ent) = Chars (Op) then
5486 F1 := First_Formal (Ent);
5487 F2 := Next_Formal (F1);
5489 -- The operation counts as primitive if either operand or
5490 -- result are of the given base type, and both operands are
5491 -- fixed point types.
5493 if (Base_Type (Etype (F1)) = Bas
5494 and then Is_Fixed_Point_Type (Etype (F2)))
5496 or else
5497 (Base_Type (Etype (F2)) = Bas
5498 and then Is_Fixed_Point_Type (Etype (F1)))
5500 or else
5501 (Base_Type (Etype (Ent)) = Bas
5502 and then Is_Fixed_Point_Type (Etype (F1))
5503 and then Is_Fixed_Point_Type (Etype (F2)))
5504 then
5505 return True;
5506 end if;
5507 end if;
5509 Next_Entity (Ent);
5510 end loop;
5512 return False;
5513 end Has_Fixed_Op;
5515 -------------------
5516 -- Specific_Type --
5517 -------------------
5519 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5520 begin
5521 if T1 = Universal_Integer or else T1 = Universal_Real then
5522 return Base_Type (T2);
5523 else
5524 return Base_Type (T1);
5525 end if;
5526 end Specific_Type;
5528 -- Start of processing for Check_Arithmetic_Pair
5530 begin
5531 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5532 if Is_Numeric_Type (T1)
5533 and then Is_Numeric_Type (T2)
5534 and then (Covers (T1 => T1, T2 => T2)
5535 or else
5536 Covers (T1 => T2, T2 => T1))
5537 then
5538 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5539 end if;
5541 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5542 if Is_Fixed_Point_Type (T1)
5543 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5544 then
5545 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5546 -- and no further processing is required (this is the case of an
5547 -- operator constructed by Exp_Fixd for a fixed point operation)
5548 -- Otherwise add one interpretation with universal fixed result
5549 -- If the operator is given in functional notation, it comes
5550 -- from source and Fixed_As_Integer cannot apply.
5552 if (Nkind (N) not in N_Op
5553 or else not Treat_Fixed_As_Integer (N))
5554 and then
5555 (not Has_Fixed_Op (T1, Op_Id)
5556 or else Nkind (Parent (N)) = N_Type_Conversion)
5557 then
5558 Add_One_Interp (N, Op_Id, Universal_Fixed);
5559 end if;
5561 elsif Is_Fixed_Point_Type (T2)
5562 and then (Nkind (N) not in N_Op
5563 or else not Treat_Fixed_As_Integer (N))
5564 and then T1 = Universal_Real
5565 and then
5566 (not Has_Fixed_Op (T1, Op_Id)
5567 or else Nkind (Parent (N)) = N_Type_Conversion)
5568 then
5569 Add_One_Interp (N, Op_Id, Universal_Fixed);
5571 elsif Is_Numeric_Type (T1)
5572 and then Is_Numeric_Type (T2)
5573 and then (Covers (T1 => T1, T2 => T2)
5574 or else
5575 Covers (T1 => T2, T2 => T1))
5576 then
5577 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5579 elsif Is_Fixed_Point_Type (T1)
5580 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5581 or else T2 = Universal_Integer)
5582 then
5583 Add_One_Interp (N, Op_Id, T1);
5585 elsif T2 = Universal_Real
5586 and then Base_Type (T1) = Base_Type (Standard_Integer)
5587 and then Op_Name = Name_Op_Multiply
5588 then
5589 Add_One_Interp (N, Op_Id, Any_Fixed);
5591 elsif T1 = Universal_Real
5592 and then Base_Type (T2) = Base_Type (Standard_Integer)
5593 then
5594 Add_One_Interp (N, Op_Id, Any_Fixed);
5596 elsif Is_Fixed_Point_Type (T2)
5597 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5598 or else T1 = Universal_Integer)
5599 and then Op_Name = Name_Op_Multiply
5600 then
5601 Add_One_Interp (N, Op_Id, T2);
5603 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5604 Add_One_Interp (N, Op_Id, T1);
5606 elsif T2 = Universal_Real
5607 and then T1 = Universal_Integer
5608 and then Op_Name = Name_Op_Multiply
5609 then
5610 Add_One_Interp (N, Op_Id, T2);
5611 end if;
5613 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5615 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5616 -- set does not require any special processing, since the Etype is
5617 -- already set (case of operation constructed by Exp_Fixed).
5619 if Is_Integer_Type (T1)
5620 and then (Covers (T1 => T1, T2 => T2)
5621 or else
5622 Covers (T1 => T2, T2 => T1))
5623 then
5624 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5625 end if;
5627 elsif Op_Name = Name_Op_Expon then
5628 if Is_Numeric_Type (T1)
5629 and then not Is_Fixed_Point_Type (T1)
5630 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5631 or else T2 = Universal_Integer)
5632 then
5633 Add_One_Interp (N, Op_Id, Base_Type (T1));
5634 end if;
5636 else pragma Assert (Nkind (N) in N_Op_Shift);
5638 -- If not one of the predefined operators, the node may be one
5639 -- of the intrinsic functions. Its kind is always specific, and
5640 -- we can use it directly, rather than the name of the operation.
5642 if Is_Integer_Type (T1)
5643 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5644 or else T2 = Universal_Integer)
5645 then
5646 Add_One_Interp (N, Op_Id, Base_Type (T1));
5647 end if;
5648 end if;
5649 end Check_Arithmetic_Pair;
5651 -------------------------------
5652 -- Check_Misspelled_Selector --
5653 -------------------------------
5655 procedure Check_Misspelled_Selector
5656 (Prefix : Entity_Id;
5657 Sel : Node_Id)
5659 Max_Suggestions : constant := 2;
5660 Nr_Of_Suggestions : Natural := 0;
5662 Suggestion_1 : Entity_Id := Empty;
5663 Suggestion_2 : Entity_Id := Empty;
5665 Comp : Entity_Id;
5667 begin
5668 -- All the components of the prefix of selector Sel are matched against
5669 -- Sel and a count is maintained of possible misspellings. When at
5670 -- the end of the analysis there are one or two (not more) possible
5671 -- misspellings, these misspellings will be suggested as possible
5672 -- correction.
5674 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5676 -- Concurrent types should be handled as well ???
5678 return;
5679 end if;
5681 Comp := First_Entity (Prefix);
5682 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5683 if Is_Visible_Component (Comp) then
5684 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5685 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5687 case Nr_Of_Suggestions is
5688 when 1 => Suggestion_1 := Comp;
5689 when 2 => Suggestion_2 := Comp;
5690 when others => exit;
5691 end case;
5692 end if;
5693 end if;
5695 Comp := Next_Entity (Comp);
5696 end loop;
5698 -- Report at most two suggestions
5700 if Nr_Of_Suggestions = 1 then
5701 Error_Msg_NE -- CODEFIX
5702 ("\possible misspelling of&", Sel, Suggestion_1);
5704 elsif Nr_Of_Suggestions = 2 then
5705 Error_Msg_Node_2 := Suggestion_2;
5706 Error_Msg_NE -- CODEFIX
5707 ("\possible misspelling of& or&", Sel, Suggestion_1);
5708 end if;
5709 end Check_Misspelled_Selector;
5711 ----------------------
5712 -- Defined_In_Scope --
5713 ----------------------
5715 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5717 S1 : constant Entity_Id := Scope (Base_Type (T));
5718 begin
5719 return S1 = S
5720 or else (S1 = System_Aux_Id and then S = Scope (S1));
5721 end Defined_In_Scope;
5723 -------------------
5724 -- Diagnose_Call --
5725 -------------------
5727 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5728 Actual : Node_Id;
5729 X : Interp_Index;
5730 It : Interp;
5731 Err_Mode : Boolean;
5732 New_Nam : Node_Id;
5733 Void_Interp_Seen : Boolean := False;
5735 Success : Boolean;
5736 pragma Warnings (Off, Boolean);
5738 begin
5739 if Ada_Version >= Ada_2005 then
5740 Actual := First_Actual (N);
5741 while Present (Actual) loop
5743 -- Ada 2005 (AI-50217): Post an error in case of premature
5744 -- usage of an entity from the limited view.
5746 if not Analyzed (Etype (Actual))
5747 and then From_Limited_With (Etype (Actual))
5748 then
5749 Error_Msg_Qual_Level := 1;
5750 Error_Msg_NE
5751 ("missing with_clause for scope of imported type&",
5752 Actual, Etype (Actual));
5753 Error_Msg_Qual_Level := 0;
5754 end if;
5756 Next_Actual (Actual);
5757 end loop;
5758 end if;
5760 -- Analyze each candidate call again, with full error reporting
5761 -- for each.
5763 Error_Msg_N
5764 ("no candidate interpretations match the actuals:!", Nam);
5765 Err_Mode := All_Errors_Mode;
5766 All_Errors_Mode := True;
5768 -- If this is a call to an operation of a concurrent type,
5769 -- the failed interpretations have been removed from the
5770 -- name. Recover them to provide full diagnostics.
5772 if Nkind (Parent (Nam)) = N_Selected_Component then
5773 Set_Entity (Nam, Empty);
5774 New_Nam := New_Copy_Tree (Parent (Nam));
5775 Set_Is_Overloaded (New_Nam, False);
5776 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5777 Set_Parent (New_Nam, Parent (Parent (Nam)));
5778 Analyze_Selected_Component (New_Nam);
5779 Get_First_Interp (Selector_Name (New_Nam), X, It);
5780 else
5781 Get_First_Interp (Nam, X, It);
5782 end if;
5784 while Present (It.Nam) loop
5785 if Etype (It.Nam) = Standard_Void_Type then
5786 Void_Interp_Seen := True;
5787 end if;
5789 Analyze_One_Call (N, It.Nam, True, Success);
5790 Get_Next_Interp (X, It);
5791 end loop;
5793 if Nkind (N) = N_Function_Call then
5794 Get_First_Interp (Nam, X, It);
5795 while Present (It.Nam) loop
5796 if Ekind_In (It.Nam, E_Function, E_Operator) then
5797 return;
5798 else
5799 Get_Next_Interp (X, It);
5800 end if;
5801 end loop;
5803 -- If all interpretations are procedures, this deserves a
5804 -- more precise message. Ditto if this appears as the prefix
5805 -- of a selected component, which may be a lexical error.
5807 Error_Msg_N
5808 ("\context requires function call, found procedure name", Nam);
5810 if Nkind (Parent (N)) = N_Selected_Component
5811 and then N = Prefix (Parent (N))
5812 then
5813 Error_Msg_N -- CODEFIX
5814 ("\period should probably be semicolon", Parent (N));
5815 end if;
5817 elsif Nkind (N) = N_Procedure_Call_Statement
5818 and then not Void_Interp_Seen
5819 then
5820 Error_Msg_N (
5821 "\function name found in procedure call", Nam);
5822 end if;
5824 All_Errors_Mode := Err_Mode;
5825 end Diagnose_Call;
5827 ---------------------------
5828 -- Find_Arithmetic_Types --
5829 ---------------------------
5831 procedure Find_Arithmetic_Types
5832 (L, R : Node_Id;
5833 Op_Id : Entity_Id;
5834 N : Node_Id)
5836 Index1 : Interp_Index;
5837 Index2 : Interp_Index;
5838 It1 : Interp;
5839 It2 : Interp;
5841 procedure Check_Right_Argument (T : Entity_Id);
5842 -- Check right operand of operator
5844 --------------------------
5845 -- Check_Right_Argument --
5846 --------------------------
5848 procedure Check_Right_Argument (T : Entity_Id) is
5849 begin
5850 if not Is_Overloaded (R) then
5851 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5852 else
5853 Get_First_Interp (R, Index2, It2);
5854 while Present (It2.Typ) loop
5855 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5856 Get_Next_Interp (Index2, It2);
5857 end loop;
5858 end if;
5859 end Check_Right_Argument;
5861 -- Start of processing for Find_Arithmetic_Types
5863 begin
5864 if not Is_Overloaded (L) then
5865 Check_Right_Argument (Etype (L));
5867 else
5868 Get_First_Interp (L, Index1, It1);
5869 while Present (It1.Typ) loop
5870 Check_Right_Argument (It1.Typ);
5871 Get_Next_Interp (Index1, It1);
5872 end loop;
5873 end if;
5875 end Find_Arithmetic_Types;
5877 ------------------------
5878 -- Find_Boolean_Types --
5879 ------------------------
5881 procedure Find_Boolean_Types
5882 (L, R : Node_Id;
5883 Op_Id : Entity_Id;
5884 N : Node_Id)
5886 Index : Interp_Index;
5887 It : Interp;
5889 procedure Check_Numeric_Argument (T : Entity_Id);
5890 -- Special case for logical operations one of whose operands is an
5891 -- integer literal. If both are literal the result is any modular type.
5893 ----------------------------
5894 -- Check_Numeric_Argument --
5895 ----------------------------
5897 procedure Check_Numeric_Argument (T : Entity_Id) is
5898 begin
5899 if T = Universal_Integer then
5900 Add_One_Interp (N, Op_Id, Any_Modular);
5902 elsif Is_Modular_Integer_Type (T) then
5903 Add_One_Interp (N, Op_Id, T);
5904 end if;
5905 end Check_Numeric_Argument;
5907 -- Start of processing for Find_Boolean_Types
5909 begin
5910 if not Is_Overloaded (L) then
5911 if Etype (L) = Universal_Integer
5912 or else Etype (L) = Any_Modular
5913 then
5914 if not Is_Overloaded (R) then
5915 Check_Numeric_Argument (Etype (R));
5917 else
5918 Get_First_Interp (R, Index, It);
5919 while Present (It.Typ) loop
5920 Check_Numeric_Argument (It.Typ);
5921 Get_Next_Interp (Index, It);
5922 end loop;
5923 end if;
5925 -- If operands are aggregates, we must assume that they may be
5926 -- boolean arrays, and leave disambiguation for the second pass.
5927 -- If only one is an aggregate, verify that the other one has an
5928 -- interpretation as a boolean array
5930 elsif Nkind (L) = N_Aggregate then
5931 if Nkind (R) = N_Aggregate then
5932 Add_One_Interp (N, Op_Id, Etype (L));
5934 elsif not Is_Overloaded (R) then
5935 if Valid_Boolean_Arg (Etype (R)) then
5936 Add_One_Interp (N, Op_Id, Etype (R));
5937 end if;
5939 else
5940 Get_First_Interp (R, Index, It);
5941 while Present (It.Typ) loop
5942 if Valid_Boolean_Arg (It.Typ) then
5943 Add_One_Interp (N, Op_Id, It.Typ);
5944 end if;
5946 Get_Next_Interp (Index, It);
5947 end loop;
5948 end if;
5950 elsif Valid_Boolean_Arg (Etype (L))
5951 and then Has_Compatible_Type (R, Etype (L))
5952 then
5953 Add_One_Interp (N, Op_Id, Etype (L));
5954 end if;
5956 else
5957 Get_First_Interp (L, Index, It);
5958 while Present (It.Typ) loop
5959 if Valid_Boolean_Arg (It.Typ)
5960 and then Has_Compatible_Type (R, It.Typ)
5961 then
5962 Add_One_Interp (N, Op_Id, It.Typ);
5963 end if;
5965 Get_Next_Interp (Index, It);
5966 end loop;
5967 end if;
5968 end Find_Boolean_Types;
5970 ---------------------------
5971 -- Find_Comparison_Types --
5972 ---------------------------
5974 procedure Find_Comparison_Types
5975 (L, R : Node_Id;
5976 Op_Id : Entity_Id;
5977 N : Node_Id)
5979 Index : Interp_Index;
5980 It : Interp;
5981 Found : Boolean := False;
5982 I_F : Interp_Index;
5983 T_F : Entity_Id;
5984 Scop : Entity_Id := Empty;
5986 procedure Try_One_Interp (T1 : Entity_Id);
5987 -- Routine to try one proposed interpretation. Note that the context
5988 -- of the operator plays no role in resolving the arguments, so that
5989 -- if there is more than one interpretation of the operands that is
5990 -- compatible with comparison, the operation is ambiguous.
5992 --------------------
5993 -- Try_One_Interp --
5994 --------------------
5996 procedure Try_One_Interp (T1 : Entity_Id) is
5997 begin
5999 -- If the operator is an expanded name, then the type of the operand
6000 -- must be defined in the corresponding scope. If the type is
6001 -- universal, the context will impose the correct type.
6003 if Present (Scop)
6004 and then not Defined_In_Scope (T1, Scop)
6005 and then T1 /= Universal_Integer
6006 and then T1 /= Universal_Real
6007 and then T1 /= Any_String
6008 and then T1 /= Any_Composite
6009 then
6010 return;
6011 end if;
6013 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6014 if Found and then Base_Type (T1) /= Base_Type (T_F) then
6015 It := Disambiguate (L, I_F, Index, Any_Type);
6017 if It = No_Interp then
6018 Ambiguous_Operands (N);
6019 Set_Etype (L, Any_Type);
6020 return;
6022 else
6023 T_F := It.Typ;
6024 end if;
6026 else
6027 Found := True;
6028 T_F := T1;
6029 I_F := Index;
6030 end if;
6032 Set_Etype (L, T_F);
6033 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6035 end if;
6036 end Try_One_Interp;
6038 -- Start of processing for Find_Comparison_Types
6040 begin
6041 -- If left operand is aggregate, the right operand has to
6042 -- provide a usable type for it.
6044 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
6045 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6046 return;
6047 end if;
6049 if Nkind (N) = N_Function_Call
6050 and then Nkind (Name (N)) = N_Expanded_Name
6051 then
6052 Scop := Entity (Prefix (Name (N)));
6054 -- The prefix may be a package renaming, and the subsequent test
6055 -- requires the original package.
6057 if Ekind (Scop) = E_Package
6058 and then Present (Renamed_Entity (Scop))
6059 then
6060 Scop := Renamed_Entity (Scop);
6061 Set_Entity (Prefix (Name (N)), Scop);
6062 end if;
6063 end if;
6065 if not Is_Overloaded (L) then
6066 Try_One_Interp (Etype (L));
6068 else
6069 Get_First_Interp (L, Index, It);
6070 while Present (It.Typ) loop
6071 Try_One_Interp (It.Typ);
6072 Get_Next_Interp (Index, It);
6073 end loop;
6074 end if;
6075 end Find_Comparison_Types;
6077 ----------------------------------------
6078 -- Find_Non_Universal_Interpretations --
6079 ----------------------------------------
6081 procedure Find_Non_Universal_Interpretations
6082 (N : Node_Id;
6083 R : Node_Id;
6084 Op_Id : Entity_Id;
6085 T1 : Entity_Id)
6087 Index : Interp_Index;
6088 It : Interp;
6090 begin
6091 if T1 = Universal_Integer or else T1 = Universal_Real
6093 -- If the left operand of an equality operator is null, the visibility
6094 -- of the operator must be determined from the interpretation of the
6095 -- right operand. This processing must be done for Any_Access, which
6096 -- is the internal representation of the type of the literal null.
6098 or else T1 = Any_Access
6099 then
6100 if not Is_Overloaded (R) then
6101 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
6102 else
6103 Get_First_Interp (R, Index, It);
6104 while Present (It.Typ) loop
6105 if Covers (It.Typ, T1) then
6106 Add_One_Interp
6107 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6108 end if;
6110 Get_Next_Interp (Index, It);
6111 end loop;
6112 end if;
6113 else
6114 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6115 end if;
6116 end Find_Non_Universal_Interpretations;
6118 ------------------------------
6119 -- Find_Concatenation_Types --
6120 ------------------------------
6122 procedure Find_Concatenation_Types
6123 (L, R : Node_Id;
6124 Op_Id : Entity_Id;
6125 N : Node_Id)
6127 Op_Type : constant Entity_Id := Etype (Op_Id);
6129 begin
6130 if Is_Array_Type (Op_Type)
6131 and then not Is_Limited_Type (Op_Type)
6133 and then (Has_Compatible_Type (L, Op_Type)
6134 or else
6135 Has_Compatible_Type (L, Component_Type (Op_Type)))
6137 and then (Has_Compatible_Type (R, Op_Type)
6138 or else
6139 Has_Compatible_Type (R, Component_Type (Op_Type)))
6140 then
6141 Add_One_Interp (N, Op_Id, Op_Type);
6142 end if;
6143 end Find_Concatenation_Types;
6145 -------------------------
6146 -- Find_Equality_Types --
6147 -------------------------
6149 procedure Find_Equality_Types
6150 (L, R : Node_Id;
6151 Op_Id : Entity_Id;
6152 N : Node_Id)
6154 Index : Interp_Index;
6155 It : Interp;
6156 Found : Boolean := False;
6157 I_F : Interp_Index;
6158 T_F : Entity_Id;
6159 Scop : Entity_Id := Empty;
6161 procedure Try_One_Interp (T1 : Entity_Id);
6162 -- The context of the equality operator plays no role in resolving the
6163 -- arguments, so that if there is more than one interpretation of the
6164 -- operands that is compatible with equality, the construct is ambiguous
6165 -- and an error can be emitted now, after trying to disambiguate, i.e.
6166 -- applying preference rules.
6168 --------------------
6169 -- Try_One_Interp --
6170 --------------------
6172 procedure Try_One_Interp (T1 : Entity_Id) is
6173 Bas : constant Entity_Id := Base_Type (T1);
6175 begin
6176 -- If the operator is an expanded name, then the type of the operand
6177 -- must be defined in the corresponding scope. If the type is
6178 -- universal, the context will impose the correct type. An anonymous
6179 -- type for a 'Access reference is also universal in this sense, as
6180 -- the actual type is obtained from context.
6182 -- In Ada 2005, the equality operator for anonymous access types
6183 -- is declared in Standard, and preference rules apply to it.
6185 if Present (Scop) then
6186 if Defined_In_Scope (T1, Scop)
6187 or else T1 = Universal_Integer
6188 or else T1 = Universal_Real
6189 or else T1 = Any_Access
6190 or else T1 = Any_String
6191 or else T1 = Any_Composite
6192 or else (Ekind (T1) = E_Access_Subprogram_Type
6193 and then not Comes_From_Source (T1))
6194 then
6195 null;
6197 elsif Ekind (T1) = E_Anonymous_Access_Type
6198 and then Scop = Standard_Standard
6199 then
6200 null;
6202 else
6203 -- The scope does not contain an operator for the type
6205 return;
6206 end if;
6208 -- If we have infix notation, the operator must be usable. Within
6209 -- an instance, if the type is already established we know it is
6210 -- correct. If an operand is universal it is compatible with any
6211 -- numeric type.
6213 elsif In_Open_Scopes (Scope (Bas))
6214 or else Is_Potentially_Use_Visible (Bas)
6215 or else In_Use (Bas)
6216 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
6218 -- In an instance, the type may have been immediately visible.
6219 -- Either the types are compatible, or one operand is universal
6220 -- (numeric or null).
6222 or else (In_Instance
6223 and then
6224 (First_Subtype (T1) = First_Subtype (Etype (R))
6225 or else Nkind (R) = N_Null
6226 or else
6227 (Is_Numeric_Type (T1)
6228 and then Is_Universal_Numeric_Type (Etype (R)))))
6230 -- In Ada 2005, the equality on anonymous access types is declared
6231 -- in Standard, and is always visible.
6233 or else Ekind (T1) = E_Anonymous_Access_Type
6234 then
6235 null;
6237 else
6238 -- Save candidate type for subsequent error message, if any
6240 if not Is_Limited_Type (T1) then
6241 Candidate_Type := T1;
6242 end if;
6244 return;
6245 end if;
6247 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6248 -- Do not allow anonymous access types in equality operators.
6250 if Ada_Version < Ada_2005
6251 and then Ekind (T1) = E_Anonymous_Access_Type
6252 then
6253 return;
6254 end if;
6256 -- If the right operand has a type compatible with T1, check for an
6257 -- acceptable interpretation, unless T1 is limited (no predefined
6258 -- equality available), or this is use of a "/=" for a tagged type.
6259 -- In the latter case, possible interpretations of equality need
6260 -- to be considered, we don't want the default inequality declared
6261 -- in Standard to be chosen, and the "/=" will be rewritten as a
6262 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
6263 -- that rewriting happens during analysis rather than being
6264 -- delayed until expansion (this is needed for ASIS, which only sees
6265 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
6266 -- is Name_Op_Eq then we still proceed with the interpretation,
6267 -- because that indicates the potential rewriting case where the
6268 -- interpretation to consider is actually "=" and the node may be
6269 -- about to be rewritten by Analyze_Equality_Op.
6271 if T1 /= Standard_Void_Type
6272 and then Has_Compatible_Type (R, T1)
6274 and then
6275 ((not Is_Limited_Type (T1)
6276 and then not Is_Limited_Composite (T1))
6278 or else
6279 (Is_Array_Type (T1)
6280 and then not Is_Limited_Type (Component_Type (T1))
6281 and then Available_Full_View_Of_Component (T1)))
6283 and then
6284 (Nkind (N) /= N_Op_Ne
6285 or else not Is_Tagged_Type (T1)
6286 or else Chars (Op_Id) = Name_Op_Eq)
6287 then
6288 if Found
6289 and then Base_Type (T1) /= Base_Type (T_F)
6290 then
6291 It := Disambiguate (L, I_F, Index, Any_Type);
6293 if It = No_Interp then
6294 Ambiguous_Operands (N);
6295 Set_Etype (L, Any_Type);
6296 return;
6298 else
6299 T_F := It.Typ;
6300 end if;
6302 else
6303 Found := True;
6304 T_F := T1;
6305 I_F := Index;
6306 end if;
6308 if not Analyzed (L) then
6309 Set_Etype (L, T_F);
6310 end if;
6312 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6314 -- Case of operator was not visible, Etype still set to Any_Type
6316 if Etype (N) = Any_Type then
6317 Found := False;
6318 end if;
6320 elsif Scop = Standard_Standard
6321 and then Ekind (T1) = E_Anonymous_Access_Type
6322 then
6323 Found := True;
6324 end if;
6325 end Try_One_Interp;
6327 -- Start of processing for Find_Equality_Types
6329 begin
6330 -- If left operand is aggregate, the right operand has to
6331 -- provide a usable type for it.
6333 if Nkind (L) = N_Aggregate
6334 and then Nkind (R) /= N_Aggregate
6335 then
6336 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6337 return;
6338 end if;
6340 if Nkind (N) = N_Function_Call
6341 and then Nkind (Name (N)) = N_Expanded_Name
6342 then
6343 Scop := Entity (Prefix (Name (N)));
6345 -- The prefix may be a package renaming, and the subsequent test
6346 -- requires the original package.
6348 if Ekind (Scop) = E_Package
6349 and then Present (Renamed_Entity (Scop))
6350 then
6351 Scop := Renamed_Entity (Scop);
6352 Set_Entity (Prefix (Name (N)), Scop);
6353 end if;
6354 end if;
6356 if not Is_Overloaded (L) then
6357 Try_One_Interp (Etype (L));
6359 else
6360 Get_First_Interp (L, Index, It);
6361 while Present (It.Typ) loop
6362 Try_One_Interp (It.Typ);
6363 Get_Next_Interp (Index, It);
6364 end loop;
6365 end if;
6366 end Find_Equality_Types;
6368 -------------------------
6369 -- Find_Negation_Types --
6370 -------------------------
6372 procedure Find_Negation_Types
6373 (R : Node_Id;
6374 Op_Id : Entity_Id;
6375 N : Node_Id)
6377 Index : Interp_Index;
6378 It : Interp;
6380 begin
6381 if not Is_Overloaded (R) then
6382 if Etype (R) = Universal_Integer then
6383 Add_One_Interp (N, Op_Id, Any_Modular);
6384 elsif Valid_Boolean_Arg (Etype (R)) then
6385 Add_One_Interp (N, Op_Id, Etype (R));
6386 end if;
6388 else
6389 Get_First_Interp (R, Index, It);
6390 while Present (It.Typ) loop
6391 if Valid_Boolean_Arg (It.Typ) then
6392 Add_One_Interp (N, Op_Id, It.Typ);
6393 end if;
6395 Get_Next_Interp (Index, It);
6396 end loop;
6397 end if;
6398 end Find_Negation_Types;
6400 ------------------------------
6401 -- Find_Primitive_Operation --
6402 ------------------------------
6404 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6405 Obj : constant Node_Id := Prefix (N);
6406 Op : constant Node_Id := Selector_Name (N);
6408 Prim : Elmt_Id;
6409 Prims : Elist_Id;
6410 Typ : Entity_Id;
6412 begin
6413 Set_Etype (Op, Any_Type);
6415 if Is_Access_Type (Etype (Obj)) then
6416 Typ := Designated_Type (Etype (Obj));
6417 else
6418 Typ := Etype (Obj);
6419 end if;
6421 if Is_Class_Wide_Type (Typ) then
6422 Typ := Root_Type (Typ);
6423 end if;
6425 Prims := Primitive_Operations (Typ);
6427 Prim := First_Elmt (Prims);
6428 while Present (Prim) loop
6429 if Chars (Node (Prim)) = Chars (Op) then
6430 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6431 Set_Etype (N, Etype (Node (Prim)));
6432 end if;
6434 Next_Elmt (Prim);
6435 end loop;
6437 -- Now look for class-wide operations of the type or any of its
6438 -- ancestors by iterating over the homonyms of the selector.
6440 declare
6441 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6442 Hom : Entity_Id;
6444 begin
6445 Hom := Current_Entity (Op);
6446 while Present (Hom) loop
6447 if (Ekind (Hom) = E_Procedure
6448 or else
6449 Ekind (Hom) = E_Function)
6450 and then Scope (Hom) = Scope (Typ)
6451 and then Present (First_Formal (Hom))
6452 and then
6453 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6454 or else
6455 (Is_Access_Type (Etype (First_Formal (Hom)))
6456 and then
6457 Ekind (Etype (First_Formal (Hom))) =
6458 E_Anonymous_Access_Type
6459 and then
6460 Base_Type
6461 (Designated_Type (Etype (First_Formal (Hom)))) =
6462 Cls_Type))
6463 then
6464 Add_One_Interp (Op, Hom, Etype (Hom));
6465 Set_Etype (N, Etype (Hom));
6466 end if;
6468 Hom := Homonym (Hom);
6469 end loop;
6470 end;
6472 return Etype (Op) /= Any_Type;
6473 end Find_Primitive_Operation;
6475 ----------------------
6476 -- Find_Unary_Types --
6477 ----------------------
6479 procedure Find_Unary_Types
6480 (R : Node_Id;
6481 Op_Id : Entity_Id;
6482 N : Node_Id)
6484 Index : Interp_Index;
6485 It : Interp;
6487 begin
6488 if not Is_Overloaded (R) then
6489 if Is_Numeric_Type (Etype (R)) then
6491 -- In an instance a generic actual may be a numeric type even if
6492 -- the formal in the generic unit was not. In that case, the
6493 -- predefined operator was not a possible interpretation in the
6494 -- generic, and cannot be one in the instance, unless the operator
6495 -- is an actual of an instance.
6497 if In_Instance
6498 and then
6499 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6500 then
6501 null;
6502 else
6503 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6504 end if;
6505 end if;
6507 else
6508 Get_First_Interp (R, Index, It);
6509 while Present (It.Typ) loop
6510 if Is_Numeric_Type (It.Typ) then
6511 if In_Instance
6512 and then
6513 not Is_Numeric_Type
6514 (Corresponding_Generic_Type (Etype (It.Typ)))
6515 then
6516 null;
6518 else
6519 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6520 end if;
6521 end if;
6523 Get_Next_Interp (Index, It);
6524 end loop;
6525 end if;
6526 end Find_Unary_Types;
6528 ------------------
6529 -- Junk_Operand --
6530 ------------------
6532 function Junk_Operand (N : Node_Id) return Boolean is
6533 Enode : Node_Id;
6535 begin
6536 if Error_Posted (N) then
6537 return False;
6538 end if;
6540 -- Get entity to be tested
6542 if Is_Entity_Name (N)
6543 and then Present (Entity (N))
6544 then
6545 Enode := N;
6547 -- An odd case, a procedure name gets converted to a very peculiar
6548 -- function call, and here is where we detect this happening.
6550 elsif Nkind (N) = N_Function_Call
6551 and then Is_Entity_Name (Name (N))
6552 and then Present (Entity (Name (N)))
6553 then
6554 Enode := Name (N);
6556 -- Another odd case, there are at least some cases of selected
6557 -- components where the selected component is not marked as having
6558 -- an entity, even though the selector does have an entity
6560 elsif Nkind (N) = N_Selected_Component
6561 and then Present (Entity (Selector_Name (N)))
6562 then
6563 Enode := Selector_Name (N);
6565 else
6566 return False;
6567 end if;
6569 -- Now test the entity we got to see if it is a bad case
6571 case Ekind (Entity (Enode)) is
6573 when E_Package =>
6574 Error_Msg_N
6575 ("package name cannot be used as operand", Enode);
6577 when Generic_Unit_Kind =>
6578 Error_Msg_N
6579 ("generic unit name cannot be used as operand", Enode);
6581 when Type_Kind =>
6582 Error_Msg_N
6583 ("subtype name cannot be used as operand", Enode);
6585 when Entry_Kind =>
6586 Error_Msg_N
6587 ("entry name cannot be used as operand", Enode);
6589 when E_Procedure =>
6590 Error_Msg_N
6591 ("procedure name cannot be used as operand", Enode);
6593 when E_Exception =>
6594 Error_Msg_N
6595 ("exception name cannot be used as operand", Enode);
6597 when E_Block | E_Label | E_Loop =>
6598 Error_Msg_N
6599 ("label name cannot be used as operand", Enode);
6601 when others =>
6602 return False;
6604 end case;
6606 return True;
6607 end Junk_Operand;
6609 --------------------
6610 -- Operator_Check --
6611 --------------------
6613 procedure Operator_Check (N : Node_Id) is
6614 begin
6615 Remove_Abstract_Operations (N);
6617 -- Test for case of no interpretation found for operator
6619 if Etype (N) = Any_Type then
6620 declare
6621 L : Node_Id;
6622 R : Node_Id;
6623 Op_Id : Entity_Id := Empty;
6625 begin
6626 R := Right_Opnd (N);
6628 if Nkind (N) in N_Binary_Op then
6629 L := Left_Opnd (N);
6630 else
6631 L := Empty;
6632 end if;
6634 -- If either operand has no type, then don't complain further,
6635 -- since this simply means that we have a propagated error.
6637 if R = Error
6638 or else Etype (R) = Any_Type
6639 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6640 then
6641 -- For the rather unusual case where one of the operands is
6642 -- a Raise_Expression, whose initial type is Any_Type, use
6643 -- the type of the other operand.
6645 if Nkind (L) = N_Raise_Expression then
6646 Set_Etype (L, Etype (R));
6647 Set_Etype (N, Etype (R));
6649 elsif Nkind (R) = N_Raise_Expression then
6650 Set_Etype (R, Etype (L));
6651 Set_Etype (N, Etype (L));
6652 end if;
6654 return;
6656 -- We explicitly check for the case of concatenation of component
6657 -- with component to avoid reporting spurious matching array types
6658 -- that might happen to be lurking in distant packages (such as
6659 -- run-time packages). This also prevents inconsistencies in the
6660 -- messages for certain ACVC B tests, which can vary depending on
6661 -- types declared in run-time interfaces. Another improvement when
6662 -- aggregates are present is to look for a well-typed operand.
6664 elsif Present (Candidate_Type)
6665 and then (Nkind (N) /= N_Op_Concat
6666 or else Is_Array_Type (Etype (L))
6667 or else Is_Array_Type (Etype (R)))
6668 then
6669 if Nkind (N) = N_Op_Concat then
6670 if Etype (L) /= Any_Composite
6671 and then Is_Array_Type (Etype (L))
6672 then
6673 Candidate_Type := Etype (L);
6675 elsif Etype (R) /= Any_Composite
6676 and then Is_Array_Type (Etype (R))
6677 then
6678 Candidate_Type := Etype (R);
6679 end if;
6680 end if;
6682 Error_Msg_NE -- CODEFIX
6683 ("operator for} is not directly visible!",
6684 N, First_Subtype (Candidate_Type));
6686 declare
6687 U : constant Node_Id :=
6688 Cunit (Get_Source_Unit (Candidate_Type));
6689 begin
6690 if Unit_Is_Visible (U) then
6691 Error_Msg_N -- CODEFIX
6692 ("use clause would make operation legal!", N);
6693 else
6694 Error_Msg_NE -- CODEFIX
6695 ("add with_clause and use_clause for&!",
6696 N, Defining_Entity (Unit (U)));
6697 end if;
6698 end;
6699 return;
6701 -- If either operand is a junk operand (e.g. package name), then
6702 -- post appropriate error messages, but do not complain further.
6704 -- Note that the use of OR in this test instead of OR ELSE is
6705 -- quite deliberate, we may as well check both operands in the
6706 -- binary operator case.
6708 elsif Junk_Operand (R)
6709 or -- really mean OR here and not OR ELSE, see above
6710 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6711 then
6712 return;
6714 -- If we have a logical operator, one of whose operands is
6715 -- Boolean, then we know that the other operand cannot resolve to
6716 -- Boolean (since we got no interpretations), but in that case we
6717 -- pretty much know that the other operand should be Boolean, so
6718 -- resolve it that way (generating an error).
6720 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6721 if Etype (L) = Standard_Boolean then
6722 Resolve (R, Standard_Boolean);
6723 return;
6724 elsif Etype (R) = Standard_Boolean then
6725 Resolve (L, Standard_Boolean);
6726 return;
6727 end if;
6729 -- For an arithmetic operator or comparison operator, if one
6730 -- of the operands is numeric, then we know the other operand
6731 -- is not the same numeric type. If it is a non-numeric type,
6732 -- then probably it is intended to match the other operand.
6734 elsif Nkind_In (N, N_Op_Add,
6735 N_Op_Divide,
6736 N_Op_Ge,
6737 N_Op_Gt,
6738 N_Op_Le)
6739 or else
6740 Nkind_In (N, N_Op_Lt,
6741 N_Op_Mod,
6742 N_Op_Multiply,
6743 N_Op_Rem,
6744 N_Op_Subtract)
6745 then
6746 -- If Allow_Integer_Address is active, check whether the
6747 -- operation becomes legal after converting an operand.
6749 if Is_Numeric_Type (Etype (L))
6750 and then not Is_Numeric_Type (Etype (R))
6751 then
6752 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6753 Rewrite (R,
6754 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6756 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6757 Analyze_Comparison_Op (N);
6758 else
6759 Analyze_Arithmetic_Op (N);
6760 end if;
6761 else
6762 Resolve (R, Etype (L));
6763 end if;
6765 return;
6767 elsif Is_Numeric_Type (Etype (R))
6768 and then not Is_Numeric_Type (Etype (L))
6769 then
6770 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
6771 Rewrite (L,
6772 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
6774 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6775 Analyze_Comparison_Op (N);
6776 else
6777 Analyze_Arithmetic_Op (N);
6778 end if;
6780 return;
6782 else
6783 Resolve (L, Etype (R));
6784 end if;
6786 return;
6788 elsif Allow_Integer_Address
6789 and then Is_Descendant_Of_Address (Etype (L))
6790 and then Is_Descendant_Of_Address (Etype (R))
6791 and then not Error_Posted (N)
6792 then
6793 declare
6794 Addr_Type : constant Entity_Id := Etype (L);
6796 begin
6797 Rewrite (L,
6798 Unchecked_Convert_To (
6799 Standard_Integer, Relocate_Node (L)));
6800 Rewrite (R,
6801 Unchecked_Convert_To (
6802 Standard_Integer, Relocate_Node (R)));
6804 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6805 Analyze_Comparison_Op (N);
6806 else
6807 Analyze_Arithmetic_Op (N);
6808 end if;
6810 -- If this is an operand in an enclosing arithmetic
6811 -- operation, Convert the result as an address so that
6812 -- arithmetic folding of address can continue.
6814 if Nkind (Parent (N)) in N_Op then
6815 Rewrite (N,
6816 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
6817 end if;
6819 return;
6820 end;
6822 -- Under relaxed RM semantics silently replace occurrences of
6823 -- null by System.Address_Null.
6825 elsif Null_To_Null_Address_Convert_OK (N) then
6826 Replace_Null_By_Null_Address (N);
6828 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6829 Analyze_Comparison_Op (N);
6830 else
6831 Analyze_Arithmetic_Op (N);
6832 end if;
6834 return;
6835 end if;
6837 -- Comparisons on A'Access are common enough to deserve a
6838 -- special message.
6840 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6841 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6842 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6843 then
6844 Error_Msg_N
6845 ("two access attributes cannot be compared directly", N);
6846 Error_Msg_N
6847 ("\use qualified expression for one of the operands",
6849 return;
6851 -- Another one for C programmers
6853 elsif Nkind (N) = N_Op_Concat
6854 and then Valid_Boolean_Arg (Etype (L))
6855 and then Valid_Boolean_Arg (Etype (R))
6856 then
6857 Error_Msg_N ("invalid operands for concatenation", N);
6858 Error_Msg_N -- CODEFIX
6859 ("\maybe AND was meant", N);
6860 return;
6862 -- A special case for comparison of access parameter with null
6864 elsif Nkind (N) = N_Op_Eq
6865 and then Is_Entity_Name (L)
6866 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6867 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6868 N_Access_Definition
6869 and then Nkind (R) = N_Null
6870 then
6871 Error_Msg_N ("access parameter is not allowed to be null", L);
6872 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6873 return;
6875 -- Another special case for exponentiation, where the right
6876 -- operand must be Natural, independently of the base.
6878 elsif Nkind (N) = N_Op_Expon
6879 and then Is_Numeric_Type (Etype (L))
6880 and then not Is_Overloaded (R)
6881 and then
6882 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6883 and then Base_Type (Etype (R)) /= Universal_Integer
6884 then
6885 if Ada_Version >= Ada_2012
6886 and then Has_Dimension_System (Etype (L))
6887 then
6888 Error_Msg_NE
6889 ("exponent for dimensioned type must be a rational" &
6890 ", found}", R, Etype (R));
6891 else
6892 Error_Msg_NE
6893 ("exponent must be of type Natural, found}", R, Etype (R));
6894 end if;
6896 return;
6898 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
6899 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6900 Rewrite (R,
6901 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6902 Analyze_Equality_Op (N);
6903 return;
6905 -- Under relaxed RM semantics silently replace occurrences of
6906 -- null by System.Address_Null.
6908 elsif Null_To_Null_Address_Convert_OK (N) then
6909 Replace_Null_By_Null_Address (N);
6910 Analyze_Equality_Op (N);
6911 return;
6912 end if;
6913 end if;
6915 -- If we fall through then just give general message. Note that in
6916 -- the following messages, if the operand is overloaded we choose
6917 -- an arbitrary type to complain about, but that is probably more
6918 -- useful than not giving a type at all.
6920 if Nkind (N) in N_Unary_Op then
6921 Error_Msg_Node_2 := Etype (R);
6922 Error_Msg_N ("operator& not defined for}", N);
6923 return;
6925 else
6926 if Nkind (N) in N_Binary_Op then
6927 if not Is_Overloaded (L)
6928 and then not Is_Overloaded (R)
6929 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6930 then
6931 Error_Msg_Node_2 := First_Subtype (Etype (R));
6932 Error_Msg_N ("there is no applicable operator& for}", N);
6934 else
6935 -- Another attempt to find a fix: one of the candidate
6936 -- interpretations may not be use-visible. This has
6937 -- already been checked for predefined operators, so
6938 -- we examine only user-defined functions.
6940 Op_Id := Get_Name_Entity_Id (Chars (N));
6942 while Present (Op_Id) loop
6943 if Ekind (Op_Id) /= E_Operator
6944 and then Is_Overloadable (Op_Id)
6945 then
6946 if not Is_Immediately_Visible (Op_Id)
6947 and then not In_Use (Scope (Op_Id))
6948 and then not Is_Abstract_Subprogram (Op_Id)
6949 and then not Is_Hidden (Op_Id)
6950 and then Ekind (Scope (Op_Id)) = E_Package
6951 and then
6952 Has_Compatible_Type
6953 (L, Etype (First_Formal (Op_Id)))
6954 and then Present
6955 (Next_Formal (First_Formal (Op_Id)))
6956 and then
6957 Has_Compatible_Type
6959 Etype (Next_Formal (First_Formal (Op_Id))))
6960 then
6961 Error_Msg_N
6962 ("No legal interpretation for operator&", N);
6963 Error_Msg_NE
6964 ("\use clause on& would make operation legal",
6965 N, Scope (Op_Id));
6966 exit;
6967 end if;
6968 end if;
6970 Op_Id := Homonym (Op_Id);
6971 end loop;
6973 if No (Op_Id) then
6974 Error_Msg_N ("invalid operand types for operator&", N);
6976 if Nkind (N) /= N_Op_Concat then
6977 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6978 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6980 -- For concatenation operators it is more difficult to
6981 -- determine which is the wrong operand. It is worth
6982 -- flagging explicitly an access type, for those who
6983 -- might think that a dereference happens here.
6985 elsif Is_Access_Type (Etype (L)) then
6986 Error_Msg_N ("\left operand is access type", N);
6988 elsif Is_Access_Type (Etype (R)) then
6989 Error_Msg_N ("\right operand is access type", N);
6990 end if;
6991 end if;
6992 end if;
6993 end if;
6994 end if;
6995 end;
6996 end if;
6997 end Operator_Check;
6999 -----------------------------------------
7000 -- Process_Implicit_Dereference_Prefix --
7001 -----------------------------------------
7003 function Process_Implicit_Dereference_Prefix
7004 (E : Entity_Id;
7005 P : Entity_Id) return Entity_Id
7007 Ref : Node_Id;
7008 Typ : constant Entity_Id := Designated_Type (Etype (P));
7010 begin
7011 if Present (E)
7012 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7013 then
7014 -- We create a dummy reference to E to ensure that the reference is
7015 -- not considered as part of an assignment (an implicit dereference
7016 -- can never assign to its prefix). The Comes_From_Source attribute
7017 -- needs to be propagated for accurate warnings.
7019 Ref := New_Occurrence_Of (E, Sloc (P));
7020 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7021 Generate_Reference (E, Ref);
7022 end if;
7024 -- An implicit dereference is a legal occurrence of an incomplete type
7025 -- imported through a limited_with clause, if the full view is visible.
7027 if From_Limited_With (Typ)
7028 and then not From_Limited_With (Scope (Typ))
7029 and then
7030 (Is_Immediately_Visible (Scope (Typ))
7031 or else
7032 (Is_Child_Unit (Scope (Typ))
7033 and then Is_Visible_Lib_Unit (Scope (Typ))))
7034 then
7035 return Available_View (Typ);
7036 else
7037 return Typ;
7038 end if;
7039 end Process_Implicit_Dereference_Prefix;
7041 --------------------------------
7042 -- Remove_Abstract_Operations --
7043 --------------------------------
7045 procedure Remove_Abstract_Operations (N : Node_Id) is
7046 Abstract_Op : Entity_Id := Empty;
7047 Address_Descendant : Boolean := False;
7048 I : Interp_Index;
7049 It : Interp;
7051 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7052 -- activate this if either extensions are enabled, or if the abstract
7053 -- operation in question comes from a predefined file. This latter test
7054 -- allows us to use abstract to make operations invisible to users. In
7055 -- particular, if type Address is non-private and abstract subprograms
7056 -- are used to hide its operators, they will be truly hidden.
7058 type Operand_Position is (First_Op, Second_Op);
7059 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
7061 procedure Remove_Address_Interpretations (Op : Operand_Position);
7062 -- Ambiguities may arise when the operands are literal and the address
7063 -- operations in s-auxdec are visible. In that case, remove the
7064 -- interpretation of a literal as Address, to retain the semantics
7065 -- of Address as a private type.
7067 ------------------------------------
7068 -- Remove_Address_Interpretations --
7069 ------------------------------------
7071 procedure Remove_Address_Interpretations (Op : Operand_Position) is
7072 Formal : Entity_Id;
7074 begin
7075 if Is_Overloaded (N) then
7076 Get_First_Interp (N, I, It);
7077 while Present (It.Nam) loop
7078 Formal := First_Entity (It.Nam);
7080 if Op = Second_Op then
7081 Formal := Next_Entity (Formal);
7082 end if;
7084 if Is_Descendant_Of_Address (Etype (Formal)) then
7085 Address_Descendant := True;
7086 Remove_Interp (I);
7087 end if;
7089 Get_Next_Interp (I, It);
7090 end loop;
7091 end if;
7092 end Remove_Address_Interpretations;
7094 -- Start of processing for Remove_Abstract_Operations
7096 begin
7097 if Is_Overloaded (N) then
7098 if Debug_Flag_V then
7099 Write_Str ("Remove_Abstract_Operations: ");
7100 Write_Overloads (N);
7101 end if;
7103 Get_First_Interp (N, I, It);
7105 while Present (It.Nam) loop
7106 if Is_Overloadable (It.Nam)
7107 and then Is_Abstract_Subprogram (It.Nam)
7108 and then not Is_Dispatching_Operation (It.Nam)
7109 then
7110 Abstract_Op := It.Nam;
7112 if Is_Descendant_Of_Address (It.Typ) then
7113 Address_Descendant := True;
7114 Remove_Interp (I);
7115 exit;
7117 -- In Ada 2005, this operation does not participate in overload
7118 -- resolution. If the operation is defined in a predefined
7119 -- unit, it is one of the operations declared abstract in some
7120 -- variants of System, and it must be removed as well.
7122 elsif Ada_Version >= Ada_2005
7123 or else Is_Predefined_File_Name
7124 (Unit_File_Name (Get_Source_Unit (It.Nam)))
7125 then
7126 Remove_Interp (I);
7127 exit;
7128 end if;
7129 end if;
7131 Get_Next_Interp (I, It);
7132 end loop;
7134 if No (Abstract_Op) then
7136 -- If some interpretation yields an integer type, it is still
7137 -- possible that there are address interpretations. Remove them
7138 -- if one operand is a literal, to avoid spurious ambiguities
7139 -- on systems where Address is a visible integer type.
7141 if Is_Overloaded (N)
7142 and then Nkind (N) in N_Op
7143 and then Is_Integer_Type (Etype (N))
7144 then
7145 if Nkind (N) in N_Binary_Op then
7146 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7147 Remove_Address_Interpretations (Second_Op);
7149 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
7150 Remove_Address_Interpretations (First_Op);
7151 end if;
7152 end if;
7153 end if;
7155 elsif Nkind (N) in N_Op then
7157 -- Remove interpretations that treat literals as addresses. This
7158 -- is never appropriate, even when Address is defined as a visible
7159 -- Integer type. The reason is that we would really prefer Address
7160 -- to behave as a private type, even in this case. If Address is a
7161 -- visible integer type, we get lots of overload ambiguities.
7163 if Nkind (N) in N_Binary_Op then
7164 declare
7165 U1 : constant Boolean :=
7166 Present (Universal_Interpretation (Right_Opnd (N)));
7167 U2 : constant Boolean :=
7168 Present (Universal_Interpretation (Left_Opnd (N)));
7170 begin
7171 if U1 then
7172 Remove_Address_Interpretations (Second_Op);
7173 end if;
7175 if U2 then
7176 Remove_Address_Interpretations (First_Op);
7177 end if;
7179 if not (U1 and U2) then
7181 -- Remove corresponding predefined operator, which is
7182 -- always added to the overload set.
7184 Get_First_Interp (N, I, It);
7185 while Present (It.Nam) loop
7186 if Scope (It.Nam) = Standard_Standard
7187 and then Base_Type (It.Typ) =
7188 Base_Type (Etype (Abstract_Op))
7189 then
7190 Remove_Interp (I);
7191 end if;
7193 Get_Next_Interp (I, It);
7194 end loop;
7196 elsif Is_Overloaded (N)
7197 and then Present (Univ_Type)
7198 then
7199 -- If both operands have a universal interpretation,
7200 -- it is still necessary to remove interpretations that
7201 -- yield Address. Any remaining ambiguities will be
7202 -- removed in Disambiguate.
7204 Get_First_Interp (N, I, It);
7205 while Present (It.Nam) loop
7206 if Is_Descendant_Of_Address (It.Typ) then
7207 Remove_Interp (I);
7209 elsif not Is_Type (It.Nam) then
7210 Set_Entity (N, It.Nam);
7211 end if;
7213 Get_Next_Interp (I, It);
7214 end loop;
7215 end if;
7216 end;
7217 end if;
7219 elsif Nkind (N) = N_Function_Call
7220 and then
7221 (Nkind (Name (N)) = N_Operator_Symbol
7222 or else
7223 (Nkind (Name (N)) = N_Expanded_Name
7224 and then
7225 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7226 then
7228 declare
7229 Arg1 : constant Node_Id := First (Parameter_Associations (N));
7230 U1 : constant Boolean :=
7231 Present (Universal_Interpretation (Arg1));
7232 U2 : constant Boolean :=
7233 Present (Next (Arg1)) and then
7234 Present (Universal_Interpretation (Next (Arg1)));
7236 begin
7237 if U1 then
7238 Remove_Address_Interpretations (First_Op);
7239 end if;
7241 if U2 then
7242 Remove_Address_Interpretations (Second_Op);
7243 end if;
7245 if not (U1 and U2) then
7246 Get_First_Interp (N, I, It);
7247 while Present (It.Nam) loop
7248 if Scope (It.Nam) = Standard_Standard
7249 and then It.Typ = Base_Type (Etype (Abstract_Op))
7250 then
7251 Remove_Interp (I);
7252 end if;
7254 Get_Next_Interp (I, It);
7255 end loop;
7256 end if;
7257 end;
7258 end if;
7260 -- If the removal has left no valid interpretations, emit an error
7261 -- message now and label node as illegal.
7263 if Present (Abstract_Op) then
7264 Get_First_Interp (N, I, It);
7266 if No (It.Nam) then
7268 -- Removal of abstract operation left no viable candidate
7270 Set_Etype (N, Any_Type);
7271 Error_Msg_Sloc := Sloc (Abstract_Op);
7272 Error_Msg_NE
7273 ("cannot call abstract operation& declared#", N, Abstract_Op);
7275 -- In Ada 2005, an abstract operation may disable predefined
7276 -- operators. Since the context is not yet known, we mark the
7277 -- predefined operators as potentially hidden. Do not include
7278 -- predefined operators when addresses are involved since this
7279 -- case is handled separately.
7281 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
7282 while Present (It.Nam) loop
7283 if Is_Numeric_Type (It.Typ)
7284 and then Scope (It.Typ) = Standard_Standard
7285 then
7286 Set_Abstract_Op (I, Abstract_Op);
7287 end if;
7289 Get_Next_Interp (I, It);
7290 end loop;
7291 end if;
7292 end if;
7294 if Debug_Flag_V then
7295 Write_Str ("Remove_Abstract_Operations done: ");
7296 Write_Overloads (N);
7297 end if;
7298 end if;
7299 end Remove_Abstract_Operations;
7301 ----------------------------
7302 -- Try_Container_Indexing --
7303 ----------------------------
7305 function Try_Container_Indexing
7306 (N : Node_Id;
7307 Prefix : Node_Id;
7308 Exprs : List_Id) return Boolean
7310 Pref_Typ : constant Entity_Id := Etype (Prefix);
7312 function Constant_Indexing_OK return Boolean;
7313 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7314 -- for the type, or else node not a target of assignment, or an actual
7315 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7317 function Find_Indexing_Operations
7318 (T : Entity_Id;
7319 Nam : Name_Id;
7320 Is_Constant : Boolean) return Node_Id;
7321 -- Return a reference to the primitive operation of type T denoted by
7322 -- name Nam. If the operation is overloaded, the reference carries all
7323 -- interpretations. Flag Is_Constant should be set when the context is
7324 -- constant indexing.
7326 --------------------------
7327 -- Constant_Indexing_OK --
7328 --------------------------
7330 function Constant_Indexing_OK return Boolean is
7331 Par : Node_Id;
7333 begin
7334 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
7335 return True;
7337 elsif not Is_Variable (Prefix) then
7338 return True;
7339 end if;
7341 Par := N;
7342 while Present (Par) loop
7343 if Nkind (Parent (Par)) = N_Assignment_Statement
7344 and then Par = Name (Parent (Par))
7345 then
7346 return False;
7348 -- The call may be overloaded, in which case we assume that its
7349 -- resolution does not depend on the type of the parameter that
7350 -- includes the indexing operation.
7352 elsif Nkind_In (Parent (Par), N_Function_Call,
7353 N_Procedure_Call_Statement)
7354 and then Is_Entity_Name (Name (Parent (Par)))
7355 then
7356 declare
7357 Actual : Node_Id;
7358 Formal : Entity_Id;
7359 Proc : Entity_Id;
7361 begin
7362 -- We should look for an interpretation with the proper
7363 -- number of formals, and determine whether it is an
7364 -- In_Parameter, but for now we examine the formal that
7365 -- corresponds to the indexing, and assume that variable
7366 -- indexing is required if some interpretation has an
7367 -- assignable formal at that position. Still does not
7368 -- cover the most complex cases ???
7370 if Is_Overloaded (Name (Parent (Par))) then
7371 declare
7372 Proc : constant Node_Id := Name (Parent (Par));
7373 A : Node_Id;
7374 F : Entity_Id;
7375 I : Interp_Index;
7376 It : Interp;
7378 begin
7379 Get_First_Interp (Proc, I, It);
7380 while Present (It.Nam) loop
7381 F := First_Formal (It.Nam);
7382 A := First (Parameter_Associations (Parent (Par)));
7384 while Present (F) and then Present (A) loop
7385 if A = Par then
7386 if Ekind (F) /= E_In_Parameter then
7387 return False;
7388 else
7389 exit; -- interpretation is safe
7390 end if;
7391 end if;
7393 Next_Formal (F);
7394 Next_Actual (A);
7395 end loop;
7397 Get_Next_Interp (I, It);
7398 end loop;
7399 end;
7401 return True;
7403 else
7404 Proc := Entity (Name (Parent (Par)));
7406 -- If this is an indirect call, get formals from
7407 -- designated type.
7409 if Is_Access_Subprogram_Type (Etype (Proc)) then
7410 Proc := Designated_Type (Etype (Proc));
7411 end if;
7412 end if;
7414 Formal := First_Formal (Proc);
7415 Actual := First_Actual (Parent (Par));
7417 -- Find corresponding actual
7419 while Present (Actual) loop
7420 exit when Actual = Par;
7421 Next_Actual (Actual);
7423 if Present (Formal) then
7424 Next_Formal (Formal);
7426 -- Otherwise this is a parameter mismatch, the error is
7427 -- reported elsewhere.
7429 else
7430 return False;
7431 end if;
7432 end loop;
7434 return Ekind (Formal) = E_In_Parameter;
7435 end;
7437 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7438 return False;
7440 -- If the indexed component is a prefix it may be the first actual
7441 -- of a prefixed call. Retrieve the called entity, if any, and
7442 -- check its first formal. Determine if the context is a procedure
7443 -- or function call.
7445 elsif Nkind (Parent (Par)) = N_Selected_Component then
7446 declare
7447 Sel : constant Node_Id := Selector_Name (Parent (Par));
7448 Nam : constant Entity_Id := Current_Entity (Sel);
7450 begin
7451 if Present (Nam) and then Is_Overloadable (Nam) then
7452 if Nkind (Parent (Parent (Par))) =
7453 N_Procedure_Call_Statement
7454 then
7455 return False;
7457 elsif Ekind (Nam) = E_Function
7458 and then Present (First_Formal (Nam))
7459 then
7460 return Ekind (First_Formal (Nam)) = E_In_Parameter;
7461 end if;
7462 end if;
7463 end;
7465 elsif Nkind (Par) in N_Op then
7466 return True;
7467 end if;
7469 Par := Parent (Par);
7470 end loop;
7472 -- In all other cases, constant indexing is legal
7474 return True;
7475 end Constant_Indexing_OK;
7477 ------------------------------
7478 -- Find_Indexing_Operations --
7479 ------------------------------
7481 function Find_Indexing_Operations
7482 (T : Entity_Id;
7483 Nam : Name_Id;
7484 Is_Constant : Boolean) return Node_Id
7486 procedure Inspect_Declarations
7487 (Typ : Entity_Id;
7488 Ref : in out Node_Id);
7489 -- Traverse the declarative list where type Typ resides and collect
7490 -- all suitable interpretations in node Ref.
7492 procedure Inspect_Primitives
7493 (Typ : Entity_Id;
7494 Ref : in out Node_Id);
7495 -- Traverse the list of primitive operations of type Typ and collect
7496 -- all suitable interpretations in node Ref.
7498 function Is_OK_Candidate
7499 (Subp_Id : Entity_Id;
7500 Typ : Entity_Id) return Boolean;
7501 -- Determine whether subprogram Subp_Id is a suitable indexing
7502 -- operation for type Typ. To qualify as such, the subprogram must
7503 -- be a function, have at least two parameters, and the type of the
7504 -- first parameter must be either Typ, or Typ'Class, or access [to
7505 -- constant] with designated type Typ or Typ'Class.
7507 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7508 -- Store subprogram Subp_Id as an interpretation in node Ref
7510 --------------------------
7511 -- Inspect_Declarations --
7512 --------------------------
7514 procedure Inspect_Declarations
7515 (Typ : Entity_Id;
7516 Ref : in out Node_Id)
7518 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
7519 Decl : Node_Id;
7520 Subp_Id : Entity_Id;
7522 begin
7523 -- Ensure that the routine is not called with itypes, which lack a
7524 -- declarative node.
7526 pragma Assert (Present (Typ_Decl));
7527 pragma Assert (Is_List_Member (Typ_Decl));
7529 Decl := First (List_Containing (Typ_Decl));
7530 while Present (Decl) loop
7531 if Nkind (Decl) = N_Subprogram_Declaration then
7532 Subp_Id := Defining_Entity (Decl);
7534 if Is_OK_Candidate (Subp_Id, Typ) then
7535 Record_Interp (Subp_Id, Ref);
7536 end if;
7537 end if;
7539 Next (Decl);
7540 end loop;
7541 end Inspect_Declarations;
7543 ------------------------
7544 -- Inspect_Primitives --
7545 ------------------------
7547 procedure Inspect_Primitives
7548 (Typ : Entity_Id;
7549 Ref : in out Node_Id)
7551 Prim_Elmt : Elmt_Id;
7552 Prim_Id : Entity_Id;
7554 begin
7555 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
7556 while Present (Prim_Elmt) loop
7557 Prim_Id := Node (Prim_Elmt);
7559 if Is_OK_Candidate (Prim_Id, Typ) then
7560 Record_Interp (Prim_Id, Ref);
7561 end if;
7563 Next_Elmt (Prim_Elmt);
7564 end loop;
7565 end Inspect_Primitives;
7567 ---------------------
7568 -- Is_OK_Candidate --
7569 ---------------------
7571 function Is_OK_Candidate
7572 (Subp_Id : Entity_Id;
7573 Typ : Entity_Id) return Boolean
7575 Formal : Entity_Id;
7576 Formal_Typ : Entity_Id;
7577 Param_Typ : Node_Id;
7579 begin
7580 -- To classify as a suitable candidate, the subprogram must be a
7581 -- function whose name matches the argument of aspect Constant or
7582 -- Variable_Indexing.
7584 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
7585 Formal := First_Formal (Subp_Id);
7587 -- The candidate requires at least two parameters
7589 if Present (Formal) and then Present (Next_Formal (Formal)) then
7590 Formal_Typ := Empty;
7591 Param_Typ := Parameter_Type (Parent (Formal));
7593 -- Use the designated type when the first parameter is of an
7594 -- access type.
7596 if Nkind (Param_Typ) = N_Access_Definition
7597 and then Present (Subtype_Mark (Param_Typ))
7598 then
7599 -- When the context is a constant indexing, the access
7600 -- definition must be access-to-constant. This does not
7601 -- apply to variable indexing.
7603 if not Is_Constant
7604 or else Constant_Present (Param_Typ)
7605 then
7606 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
7607 end if;
7609 -- Otherwise use the parameter type
7611 else
7612 Formal_Typ := Etype (Param_Typ);
7613 end if;
7615 if Present (Formal_Typ) then
7617 -- Use the specific type when the parameter type is
7618 -- class-wide.
7620 if Is_Class_Wide_Type (Formal_Typ) then
7621 Formal_Typ := Etype (Base_Type (Formal_Typ));
7622 end if;
7624 -- Use the full view when the parameter type is private
7625 -- or incomplete.
7627 if Is_Incomplete_Or_Private_Type (Formal_Typ)
7628 and then Present (Full_View (Formal_Typ))
7629 then
7630 Formal_Typ := Full_View (Formal_Typ);
7631 end if;
7633 -- The type of the first parameter must denote the type
7634 -- of the container or acts as its ancestor type.
7636 return
7637 Formal_Typ = Typ
7638 or else Is_Ancestor (Formal_Typ, Typ);
7639 end if;
7640 end if;
7641 end if;
7643 return False;
7644 end Is_OK_Candidate;
7646 -------------------
7647 -- Record_Interp --
7648 -------------------
7650 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
7651 begin
7652 if Present (Ref) then
7653 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
7655 -- Otherwise this is the first interpretation. Create a reference
7656 -- where all remaining interpretations will be collected.
7658 else
7659 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
7660 end if;
7661 end Record_Interp;
7663 -- Local variables
7665 Ref : Node_Id;
7666 Typ : Entity_Id;
7668 -- Start of processing for Find_Indexing_Operations
7670 begin
7671 Typ := T;
7673 -- Use the specific type when the parameter type is class-wide
7675 if Is_Class_Wide_Type (Typ) then
7676 Typ := Root_Type (Typ);
7677 end if;
7679 Ref := Empty;
7680 Typ := Underlying_Type (Base_Type (Typ));
7682 Inspect_Primitives (Typ, Ref);
7683 Inspect_Declarations (Typ, Ref);
7685 return Ref;
7686 end Find_Indexing_Operations;
7688 -- Local variables
7690 Loc : constant Source_Ptr := Sloc (N);
7691 Assoc : List_Id;
7692 C_Type : Entity_Id;
7693 Func : Entity_Id;
7694 Func_Name : Node_Id;
7695 Indexing : Node_Id;
7697 Is_Constant_Indexing : Boolean := False;
7698 -- This flag reflects the nature of the container indexing. Note that
7699 -- the context may be suited for constant indexing, but the type may
7700 -- lack a Constant_Indexing annotation.
7702 -- Start of processing for Try_Container_Indexing
7704 begin
7705 -- Node may have been analyzed already when testing for a prefixed
7706 -- call, in which case do not redo analysis.
7708 if Present (Generalized_Indexing (N)) then
7709 return True;
7710 end if;
7712 C_Type := Pref_Typ;
7714 -- If indexing a class-wide container, obtain indexing primitive from
7715 -- specific type.
7717 if Is_Class_Wide_Type (C_Type) then
7718 C_Type := Etype (Base_Type (C_Type));
7719 end if;
7721 -- Check whether the type has a specified indexing aspect
7723 Func_Name := Empty;
7725 -- The context is suitable for constant indexing, so obtain the name of
7726 -- the indexing function from aspect Constant_Indexing.
7728 if Constant_Indexing_OK then
7729 Func_Name :=
7730 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
7731 end if;
7733 if Present (Func_Name) then
7734 Is_Constant_Indexing := True;
7736 -- Otherwise attempt variable indexing
7738 else
7739 Func_Name :=
7740 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
7741 end if;
7743 -- The type is not subject to either form of indexing, therefore the
7744 -- indexed component does not denote container indexing. If this is a
7745 -- true error, it is diagnosed by the caller.
7747 if No (Func_Name) then
7749 -- The prefix itself may be an indexing of a container. Rewrite it
7750 -- as such and retry.
7752 if Has_Implicit_Dereference (Pref_Typ) then
7753 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
7754 return Try_Container_Indexing (N, Prefix, Exprs);
7756 -- Otherwise this is definitely not container indexing
7758 else
7759 return False;
7760 end if;
7762 -- If the container type is derived from another container type, the
7763 -- value of the inherited aspect is the Reference operation declared
7764 -- for the parent type.
7766 -- However, Reference is also a primitive operation of the type, and the
7767 -- inherited operation has a different signature. We retrieve the right
7768 -- ones (the function may be overloaded) from the list of primitive
7769 -- operations of the derived type.
7771 -- Note that predefined containers are typically all derived from one of
7772 -- the Controlled types. The code below is motivated by containers that
7773 -- are derived from other types with a Reference aspect.
7775 elsif Is_Derived_Type (C_Type)
7776 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
7777 then
7778 Func_Name :=
7779 Find_Indexing_Operations
7780 (T => C_Type,
7781 Nam => Chars (Func_Name),
7782 Is_Constant => Is_Constant_Indexing);
7783 end if;
7785 Assoc := New_List (Relocate_Node (Prefix));
7787 -- A generalized indexing may have nore than one index expression, so
7788 -- transfer all of them to the argument list to be used in the call.
7789 -- Note that there may be named associations, in which case the node
7790 -- was rewritten earlier as a call, and has been transformed back into
7791 -- an indexed expression to share the following processing.
7793 -- The generalized indexing node is the one on which analysis and
7794 -- resolution take place. Before expansion the original node is replaced
7795 -- with the generalized indexing node, which is a call, possibly with a
7796 -- dereference operation.
7798 if Comes_From_Source (N) then
7799 Check_Compiler_Unit ("generalized indexing", N);
7800 end if;
7802 -- Create argument list for function call that represents generalized
7803 -- indexing. Note that indices (i.e. actuals) may themselves be
7804 -- overloaded.
7806 declare
7807 Arg : Node_Id;
7808 New_Arg : Node_Id;
7810 begin
7811 Arg := First (Exprs);
7812 while Present (Arg) loop
7813 New_Arg := Relocate_Node (Arg);
7815 -- The arguments can be parameter associations, in which case the
7816 -- explicit actual parameter carries the overloadings.
7818 if Nkind (New_Arg) /= N_Parameter_Association then
7819 Save_Interps (Arg, New_Arg);
7820 end if;
7822 Append (New_Arg, Assoc);
7823 Next (Arg);
7824 end loop;
7825 end;
7827 if not Is_Overloaded (Func_Name) then
7828 Func := Entity (Func_Name);
7829 Indexing :=
7830 Make_Function_Call (Loc,
7831 Name => New_Occurrence_Of (Func, Loc),
7832 Parameter_Associations => Assoc);
7833 Set_Parent (Indexing, Parent (N));
7834 Set_Generalized_Indexing (N, Indexing);
7835 Analyze (Indexing);
7836 Set_Etype (N, Etype (Indexing));
7838 -- If the return type of the indexing function is a reference type,
7839 -- add the dereference as a possible interpretation. Note that the
7840 -- indexing aspect may be a function that returns the element type
7841 -- with no intervening implicit dereference, and that the reference
7842 -- discriminant is not the first discriminant.
7844 if Has_Discriminants (Etype (Func)) then
7845 Check_Implicit_Dereference (N, Etype (Func));
7846 end if;
7848 else
7849 -- If there are multiple indexing functions, build a function call
7850 -- and analyze it for each of the possible interpretations.
7852 Indexing :=
7853 Make_Function_Call (Loc,
7854 Name =>
7855 Make_Identifier (Loc, Chars (Func_Name)),
7856 Parameter_Associations => Assoc);
7858 Set_Parent (Indexing, Parent (N));
7859 Set_Generalized_Indexing (N, Indexing);
7860 Set_Etype (N, Any_Type);
7861 Set_Etype (Name (Indexing), Any_Type);
7863 declare
7864 I : Interp_Index;
7865 It : Interp;
7866 Success : Boolean;
7868 begin
7869 Get_First_Interp (Func_Name, I, It);
7870 Set_Etype (Indexing, Any_Type);
7872 -- Analyze eacn candidae function with the given actuals
7874 while Present (It.Nam) loop
7875 Analyze_One_Call (Indexing, It.Nam, False, Success);
7876 Get_Next_Interp (I, It);
7877 end loop;
7879 -- If there are several successful candidates, resolution will
7880 -- be by result. Mark the interpretations of the function name
7881 -- itself.
7883 if Is_Overloaded (Indexing) then
7884 Get_First_Interp (Indexing, I, It);
7886 while Present (It.Nam) loop
7887 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
7888 Get_Next_Interp (I, It);
7889 end loop;
7891 else
7892 Set_Etype (Name (Indexing), Etype (Indexing));
7893 end if;
7895 -- Now add the candidate interpretations to the indexing node
7896 -- itself, to be replaced later by the function call.
7898 if Is_Overloaded (Name (Indexing)) then
7899 Get_First_Interp (Name (Indexing), I, It);
7901 while Present (It.Nam) loop
7902 Add_One_Interp (N, It.Nam, It.Typ);
7904 -- Add dereference interpretation if the result type has
7905 -- implicit reference discriminants.
7907 if Has_Discriminants (Etype (It.Nam)) then
7908 Check_Implicit_Dereference (N, Etype (It.Nam));
7909 end if;
7911 Get_Next_Interp (I, It);
7912 end loop;
7914 else
7915 Set_Etype (N, Etype (Name (Indexing)));
7916 if Has_Discriminants (Etype (N)) then
7917 Check_Implicit_Dereference (N, Etype (N));
7918 end if;
7919 end if;
7920 end;
7921 end if;
7923 if Etype (Indexing) = Any_Type then
7924 Error_Msg_NE
7925 ("container cannot be indexed with&", N, Etype (First (Exprs)));
7926 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
7927 end if;
7929 return True;
7930 end Try_Container_Indexing;
7932 -----------------------
7933 -- Try_Indirect_Call --
7934 -----------------------
7936 function Try_Indirect_Call
7937 (N : Node_Id;
7938 Nam : Entity_Id;
7939 Typ : Entity_Id) return Boolean
7941 Actual : Node_Id;
7942 Formal : Entity_Id;
7944 Call_OK : Boolean;
7945 pragma Warnings (Off, Call_OK);
7947 begin
7948 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
7950 Actual := First_Actual (N);
7951 Formal := First_Formal (Designated_Type (Typ));
7952 while Present (Actual) and then Present (Formal) loop
7953 if not Has_Compatible_Type (Actual, Etype (Formal)) then
7954 return False;
7955 end if;
7957 Next (Actual);
7958 Next_Formal (Formal);
7959 end loop;
7961 if No (Actual) and then No (Formal) then
7962 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
7964 -- Nam is a candidate interpretation for the name in the call,
7965 -- if it is not an indirect call.
7967 if not Is_Type (Nam)
7968 and then Is_Entity_Name (Name (N))
7969 then
7970 Set_Entity (Name (N), Nam);
7971 end if;
7973 return True;
7975 else
7976 return False;
7977 end if;
7978 end Try_Indirect_Call;
7980 ----------------------
7981 -- Try_Indexed_Call --
7982 ----------------------
7984 function Try_Indexed_Call
7985 (N : Node_Id;
7986 Nam : Entity_Id;
7987 Typ : Entity_Id;
7988 Skip_First : Boolean) return Boolean
7990 Loc : constant Source_Ptr := Sloc (N);
7991 Actuals : constant List_Id := Parameter_Associations (N);
7992 Actual : Node_Id;
7993 Index : Entity_Id;
7995 begin
7996 Actual := First (Actuals);
7998 -- If the call was originally written in prefix form, skip the first
7999 -- actual, which is obviously not defaulted.
8001 if Skip_First then
8002 Next (Actual);
8003 end if;
8005 Index := First_Index (Typ);
8006 while Present (Actual) and then Present (Index) loop
8008 -- If the parameter list has a named association, the expression
8009 -- is definitely a call and not an indexed component.
8011 if Nkind (Actual) = N_Parameter_Association then
8012 return False;
8013 end if;
8015 if Is_Entity_Name (Actual)
8016 and then Is_Type (Entity (Actual))
8017 and then No (Next (Actual))
8018 then
8019 -- A single actual that is a type name indicates a slice if the
8020 -- type is discrete, and an error otherwise.
8022 if Is_Discrete_Type (Entity (Actual)) then
8023 Rewrite (N,
8024 Make_Slice (Loc,
8025 Prefix =>
8026 Make_Function_Call (Loc,
8027 Name => Relocate_Node (Name (N))),
8028 Discrete_Range =>
8029 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8031 Analyze (N);
8033 else
8034 Error_Msg_N ("invalid use of type in expression", Actual);
8035 Set_Etype (N, Any_Type);
8036 end if;
8038 return True;
8040 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
8041 return False;
8042 end if;
8044 Next (Actual);
8045 Next_Index (Index);
8046 end loop;
8048 if No (Actual) and then No (Index) then
8049 Add_One_Interp (N, Nam, Component_Type (Typ));
8051 -- Nam is a candidate interpretation for the name in the call,
8052 -- if it is not an indirect call.
8054 if not Is_Type (Nam)
8055 and then Is_Entity_Name (Name (N))
8056 then
8057 Set_Entity (Name (N), Nam);
8058 end if;
8060 return True;
8061 else
8062 return False;
8063 end if;
8064 end Try_Indexed_Call;
8066 --------------------------
8067 -- Try_Object_Operation --
8068 --------------------------
8070 function Try_Object_Operation
8071 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8073 K : constant Node_Kind := Nkind (Parent (N));
8074 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
8075 Loc : constant Source_Ptr := Sloc (N);
8076 Obj : constant Node_Id := Prefix (N);
8078 Subprog : constant Node_Id :=
8079 Make_Identifier (Sloc (Selector_Name (N)),
8080 Chars => Chars (Selector_Name (N)));
8081 -- Identifier on which possible interpretations will be collected
8083 Report_Error : Boolean := False;
8084 -- If no candidate interpretation matches the context, redo analysis
8085 -- with Report_Error True to provide additional information.
8087 Actual : Node_Id;
8088 Candidate : Entity_Id := Empty;
8089 New_Call_Node : Node_Id := Empty;
8090 Node_To_Replace : Node_Id;
8091 Obj_Type : Entity_Id := Etype (Obj);
8092 Success : Boolean := False;
8094 function Valid_Candidate
8095 (Success : Boolean;
8096 Call : Node_Id;
8097 Subp : Entity_Id) return Entity_Id;
8098 -- If the subprogram is a valid interpretation, record it, and add
8099 -- to the list of interpretations of Subprog. Otherwise return Empty.
8101 procedure Complete_Object_Operation
8102 (Call_Node : Node_Id;
8103 Node_To_Replace : Node_Id);
8104 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8105 -- Call_Node, insert the object (or its dereference) as the first actual
8106 -- in the call, and complete the analysis of the call.
8108 procedure Report_Ambiguity (Op : Entity_Id);
8109 -- If a prefixed procedure call is ambiguous, indicate whether the
8110 -- call includes an implicit dereference or an implicit 'Access.
8112 procedure Transform_Object_Operation
8113 (Call_Node : out Node_Id;
8114 Node_To_Replace : out Node_Id);
8115 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
8116 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8117 -- either N or the parent of N, and Subprog is a reference to the
8118 -- subprogram we are trying to match.
8120 function Try_Class_Wide_Operation
8121 (Call_Node : Node_Id;
8122 Node_To_Replace : Node_Id) return Boolean;
8123 -- Traverse all ancestor types looking for a class-wide subprogram
8124 -- for which the current operation is a valid non-dispatching call.
8126 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8127 -- If prefix is overloaded, its interpretation may include different
8128 -- tagged types, and we must examine the primitive operations and
8129 -- the class-wide operations of each in order to find candidate
8130 -- interpretations for the call as a whole.
8132 function Try_Primitive_Operation
8133 (Call_Node : Node_Id;
8134 Node_To_Replace : Node_Id) return Boolean;
8135 -- Traverse the list of primitive subprograms looking for a dispatching
8136 -- operation for which the current node is a valid call .
8138 ---------------------
8139 -- Valid_Candidate --
8140 ---------------------
8142 function Valid_Candidate
8143 (Success : Boolean;
8144 Call : Node_Id;
8145 Subp : Entity_Id) return Entity_Id
8147 Arr_Type : Entity_Id;
8148 Comp_Type : Entity_Id;
8150 begin
8151 -- If the subprogram is a valid interpretation, record it in global
8152 -- variable Subprog, to collect all possible overloadings.
8154 if Success then
8155 if Subp /= Entity (Subprog) then
8156 Add_One_Interp (Subprog, Subp, Etype (Subp));
8157 end if;
8158 end if;
8160 -- If the call may be an indexed call, retrieve component type of
8161 -- resulting expression, and add possible interpretation.
8163 Arr_Type := Empty;
8164 Comp_Type := Empty;
8166 if Nkind (Call) = N_Function_Call
8167 and then Nkind (Parent (N)) = N_Indexed_Component
8168 and then Needs_One_Actual (Subp)
8169 then
8170 if Is_Array_Type (Etype (Subp)) then
8171 Arr_Type := Etype (Subp);
8173 elsif Is_Access_Type (Etype (Subp))
8174 and then Is_Array_Type (Designated_Type (Etype (Subp)))
8175 then
8176 Arr_Type := Designated_Type (Etype (Subp));
8177 end if;
8178 end if;
8180 if Present (Arr_Type) then
8182 -- Verify that the actuals (excluding the object) match the types
8183 -- of the indexes.
8185 declare
8186 Actual : Node_Id;
8187 Index : Node_Id;
8189 begin
8190 Actual := Next (First_Actual (Call));
8191 Index := First_Index (Arr_Type);
8192 while Present (Actual) and then Present (Index) loop
8193 if not Has_Compatible_Type (Actual, Etype (Index)) then
8194 Arr_Type := Empty;
8195 exit;
8196 end if;
8198 Next_Actual (Actual);
8199 Next_Index (Index);
8200 end loop;
8202 if No (Actual)
8203 and then No (Index)
8204 and then Present (Arr_Type)
8205 then
8206 Comp_Type := Component_Type (Arr_Type);
8207 end if;
8208 end;
8210 if Present (Comp_Type)
8211 and then Etype (Subprog) /= Comp_Type
8212 then
8213 Add_One_Interp (Subprog, Subp, Comp_Type);
8214 end if;
8215 end if;
8217 if Etype (Call) /= Any_Type then
8218 return Subp;
8219 else
8220 return Empty;
8221 end if;
8222 end Valid_Candidate;
8224 -------------------------------
8225 -- Complete_Object_Operation --
8226 -------------------------------
8228 procedure Complete_Object_Operation
8229 (Call_Node : Node_Id;
8230 Node_To_Replace : Node_Id)
8232 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8233 Formal_Type : constant Entity_Id := Etype (Control);
8234 First_Actual : Node_Id;
8236 begin
8237 -- Place the name of the operation, with its interpretations,
8238 -- on the rewritten call.
8240 Set_Name (Call_Node, Subprog);
8242 First_Actual := First (Parameter_Associations (Call_Node));
8244 -- For cross-reference purposes, treat the new node as being in the
8245 -- source if the original one is. Set entity and type, even though
8246 -- they may be overwritten during resolution if overloaded.
8248 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8249 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8251 if Nkind (N) = N_Selected_Component
8252 and then not Inside_A_Generic
8253 then
8254 Set_Entity (Selector_Name (N), Entity (Subprog));
8255 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
8256 end if;
8258 -- If need be, rewrite first actual as an explicit dereference. If
8259 -- the call is overloaded, the rewriting can only be done once the
8260 -- primitive operation is identified.
8262 if Is_Overloaded (Subprog) then
8264 -- The prefix itself may be overloaded, and its interpretations
8265 -- must be propagated to the new actual in the call.
8267 if Is_Overloaded (Obj) then
8268 Save_Interps (Obj, First_Actual);
8269 end if;
8271 Rewrite (First_Actual, Obj);
8273 elsif not Is_Access_Type (Formal_Type)
8274 and then Is_Access_Type (Etype (Obj))
8275 then
8276 Rewrite (First_Actual,
8277 Make_Explicit_Dereference (Sloc (Obj), Obj));
8278 Analyze (First_Actual);
8280 -- If we need to introduce an explicit dereference, verify that
8281 -- the resulting actual is compatible with the mode of the formal.
8283 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8284 and then Is_Access_Constant (Etype (Obj))
8285 then
8286 Error_Msg_NE
8287 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8288 end if;
8290 -- Conversely, if the formal is an access parameter and the object
8291 -- is not, replace the actual with a 'Access reference. Its analysis
8292 -- will check that the object is aliased.
8294 elsif Is_Access_Type (Formal_Type)
8295 and then not Is_Access_Type (Etype (Obj))
8296 then
8297 -- A special case: A.all'access is illegal if A is an access to a
8298 -- constant and the context requires an access to a variable.
8300 if not Is_Access_Constant (Formal_Type) then
8301 if (Nkind (Obj) = N_Explicit_Dereference
8302 and then Is_Access_Constant (Etype (Prefix (Obj))))
8303 or else not Is_Variable (Obj)
8304 then
8305 Error_Msg_NE
8306 ("actual for & must be a variable", Obj, Control);
8307 end if;
8308 end if;
8310 Rewrite (First_Actual,
8311 Make_Attribute_Reference (Loc,
8312 Attribute_Name => Name_Access,
8313 Prefix => Relocate_Node (Obj)));
8315 if not Is_Aliased_View (Obj) then
8316 Error_Msg_NE
8317 ("object in prefixed call to & must be aliased "
8318 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
8319 end if;
8321 Analyze (First_Actual);
8323 else
8324 if Is_Overloaded (Obj) then
8325 Save_Interps (Obj, First_Actual);
8326 end if;
8328 Rewrite (First_Actual, Obj);
8329 end if;
8331 -- The operation is obtained from the dispatch table and not by
8332 -- visibility, and may be declared in a unit that is not explicitly
8333 -- referenced in the source, but is nevertheless required in the
8334 -- context of the current unit. Indicate that operation and its scope
8335 -- are referenced, to prevent spurious and misleading warnings. If
8336 -- the operation is overloaded, all primitives are in the same scope
8337 -- and we can use any of them.
8339 Set_Referenced (Entity (Subprog), True);
8340 Set_Referenced (Scope (Entity (Subprog)), True);
8342 Rewrite (Node_To_Replace, Call_Node);
8344 -- Propagate the interpretations collected in subprog to the new
8345 -- function call node, to be resolved from context.
8347 if Is_Overloaded (Subprog) then
8348 Save_Interps (Subprog, Node_To_Replace);
8350 else
8351 -- The type of the subprogram may be a limited view obtained
8352 -- transitively from another unit. If full view is available,
8353 -- use it to analyze call.
8355 declare
8356 T : constant Entity_Id := Etype (Subprog);
8357 begin
8358 if From_Limited_With (T) then
8359 Set_Etype (Entity (Subprog), Available_View (T));
8360 end if;
8361 end;
8363 Analyze (Node_To_Replace);
8365 -- If the operation has been rewritten into a call, which may get
8366 -- subsequently an explicit dereference, preserve the type on the
8367 -- original node (selected component or indexed component) for
8368 -- subsequent legality tests, e.g. Is_Variable. which examines
8369 -- the original node.
8371 if Nkind (Node_To_Replace) = N_Function_Call then
8372 Set_Etype
8373 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8374 end if;
8375 end if;
8376 end Complete_Object_Operation;
8378 ----------------------
8379 -- Report_Ambiguity --
8380 ----------------------
8382 procedure Report_Ambiguity (Op : Entity_Id) is
8383 Access_Actual : constant Boolean :=
8384 Is_Access_Type (Etype (Prefix (N)));
8385 Access_Formal : Boolean := False;
8387 begin
8388 Error_Msg_Sloc := Sloc (Op);
8390 if Present (First_Formal (Op)) then
8391 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8392 end if;
8394 if Access_Formal and then not Access_Actual then
8395 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8396 Error_Msg_N
8397 ("\possible interpretation "
8398 & "(inherited, with implicit 'Access) #", N);
8399 else
8400 Error_Msg_N
8401 ("\possible interpretation (with implicit 'Access) #", N);
8402 end if;
8404 elsif not Access_Formal and then Access_Actual then
8405 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8406 Error_Msg_N
8407 ("\possible interpretation "
8408 & "(inherited, with implicit dereference) #", N);
8409 else
8410 Error_Msg_N
8411 ("\possible interpretation (with implicit dereference) #", N);
8412 end if;
8414 else
8415 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8416 Error_Msg_N ("\possible interpretation (inherited)#", N);
8417 else
8418 Error_Msg_N -- CODEFIX
8419 ("\possible interpretation#", N);
8420 end if;
8421 end if;
8422 end Report_Ambiguity;
8424 --------------------------------
8425 -- Transform_Object_Operation --
8426 --------------------------------
8428 procedure Transform_Object_Operation
8429 (Call_Node : out Node_Id;
8430 Node_To_Replace : out Node_Id)
8432 Dummy : constant Node_Id := New_Copy (Obj);
8433 -- Placeholder used as a first parameter in the call, replaced
8434 -- eventually by the proper object.
8436 Parent_Node : constant Node_Id := Parent (N);
8438 Actual : Node_Id;
8439 Actuals : List_Id;
8441 begin
8442 -- Common case covering 1) Call to a procedure and 2) Call to a
8443 -- function that has some additional actuals.
8445 if Nkind (Parent_Node) in N_Subprogram_Call
8447 -- N is a selected component node containing the name of the
8448 -- subprogram. If N is not the name of the parent node we must
8449 -- not replace the parent node by the new construct. This case
8450 -- occurs when N is a parameterless call to a subprogram that
8451 -- is an actual parameter of a call to another subprogram. For
8452 -- example:
8453 -- Some_Subprogram (..., Obj.Operation, ...)
8455 and then Name (Parent_Node) = N
8456 then
8457 Node_To_Replace := Parent_Node;
8459 Actuals := Parameter_Associations (Parent_Node);
8461 if Present (Actuals) then
8462 Prepend (Dummy, Actuals);
8463 else
8464 Actuals := New_List (Dummy);
8465 end if;
8467 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8468 Call_Node :=
8469 Make_Procedure_Call_Statement (Loc,
8470 Name => New_Copy (Subprog),
8471 Parameter_Associations => Actuals);
8473 else
8474 Call_Node :=
8475 Make_Function_Call (Loc,
8476 Name => New_Copy (Subprog),
8477 Parameter_Associations => Actuals);
8478 end if;
8480 -- Before analysis, a function call appears as an indexed component
8481 -- if there are no named associations.
8483 elsif Nkind (Parent_Node) = N_Indexed_Component
8484 and then N = Prefix (Parent_Node)
8485 then
8486 Node_To_Replace := Parent_Node;
8487 Actuals := Expressions (Parent_Node);
8489 Actual := First (Actuals);
8490 while Present (Actual) loop
8491 Analyze (Actual);
8492 Next (Actual);
8493 end loop;
8495 Prepend (Dummy, Actuals);
8497 Call_Node :=
8498 Make_Function_Call (Loc,
8499 Name => New_Copy (Subprog),
8500 Parameter_Associations => Actuals);
8502 -- Parameterless call: Obj.F is rewritten as F (Obj)
8504 else
8505 Node_To_Replace := N;
8507 Call_Node :=
8508 Make_Function_Call (Loc,
8509 Name => New_Copy (Subprog),
8510 Parameter_Associations => New_List (Dummy));
8511 end if;
8512 end Transform_Object_Operation;
8514 ------------------------------
8515 -- Try_Class_Wide_Operation --
8516 ------------------------------
8518 function Try_Class_Wide_Operation
8519 (Call_Node : Node_Id;
8520 Node_To_Replace : Node_Id) return Boolean
8522 Anc_Type : Entity_Id;
8523 Matching_Op : Entity_Id := Empty;
8524 Error : Boolean;
8526 procedure Traverse_Homonyms
8527 (Anc_Type : Entity_Id;
8528 Error : out Boolean);
8529 -- Traverse the homonym chain of the subprogram searching for those
8530 -- homonyms whose first formal has the Anc_Type's class-wide type,
8531 -- or an anonymous access type designating the class-wide type. If
8532 -- an ambiguity is detected, then Error is set to True.
8534 procedure Traverse_Interfaces
8535 (Anc_Type : Entity_Id;
8536 Error : out Boolean);
8537 -- Traverse the list of interfaces, if any, associated with Anc_Type
8538 -- and search for acceptable class-wide homonyms associated with each
8539 -- interface. If an ambiguity is detected, then Error is set to True.
8541 -----------------------
8542 -- Traverse_Homonyms --
8543 -----------------------
8545 procedure Traverse_Homonyms
8546 (Anc_Type : Entity_Id;
8547 Error : out Boolean)
8549 Cls_Type : Entity_Id;
8550 Hom : Entity_Id;
8551 Hom_Ref : Node_Id;
8552 Success : Boolean;
8554 begin
8555 Error := False;
8557 Cls_Type := Class_Wide_Type (Anc_Type);
8559 Hom := Current_Entity (Subprog);
8561 -- Find a non-hidden operation whose first parameter is of the
8562 -- class-wide type, a subtype thereof, or an anonymous access
8563 -- to same. If in an instance, the operation can be considered
8564 -- even if hidden (it may be hidden because the instantiation
8565 -- is expanded after the containing package has been analyzed).
8567 while Present (Hom) loop
8568 if Ekind_In (Hom, E_Procedure, E_Function)
8569 and then (not Is_Hidden (Hom) or else In_Instance)
8570 and then Scope (Hom) = Scope (Anc_Type)
8571 and then Present (First_Formal (Hom))
8572 and then
8573 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
8574 or else
8575 (Is_Access_Type (Etype (First_Formal (Hom)))
8576 and then
8577 Ekind (Etype (First_Formal (Hom))) =
8578 E_Anonymous_Access_Type
8579 and then
8580 Base_Type
8581 (Designated_Type (Etype (First_Formal (Hom)))) =
8582 Cls_Type))
8583 then
8584 -- If the context is a procedure call, ignore functions
8585 -- in the name of the call.
8587 if Ekind (Hom) = E_Function
8588 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
8589 and then N = Name (Parent (N))
8590 then
8591 goto Next_Hom;
8593 -- If the context is a function call, ignore procedures
8594 -- in the name of the call.
8596 elsif Ekind (Hom) = E_Procedure
8597 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
8598 then
8599 goto Next_Hom;
8600 end if;
8602 Set_Etype (Call_Node, Any_Type);
8603 Set_Is_Overloaded (Call_Node, False);
8604 Success := False;
8606 if No (Matching_Op) then
8607 Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog));
8608 Set_Etype (Call_Node, Any_Type);
8609 Set_Parent (Call_Node, Parent (Node_To_Replace));
8611 Set_Name (Call_Node, Hom_Ref);
8613 Analyze_One_Call
8614 (N => Call_Node,
8615 Nam => Hom,
8616 Report => Report_Error,
8617 Success => Success,
8618 Skip_First => True);
8620 Matching_Op :=
8621 Valid_Candidate (Success, Call_Node, Hom);
8623 else
8624 Analyze_One_Call
8625 (N => Call_Node,
8626 Nam => Hom,
8627 Report => Report_Error,
8628 Success => Success,
8629 Skip_First => True);
8631 if Present (Valid_Candidate (Success, Call_Node, Hom))
8632 and then Nkind (Call_Node) /= N_Function_Call
8633 then
8634 Error_Msg_NE ("ambiguous call to&", N, Hom);
8635 Report_Ambiguity (Matching_Op);
8636 Report_Ambiguity (Hom);
8637 Error := True;
8638 return;
8639 end if;
8640 end if;
8641 end if;
8643 <<Next_Hom>>
8644 Hom := Homonym (Hom);
8645 end loop;
8646 end Traverse_Homonyms;
8648 -------------------------
8649 -- Traverse_Interfaces --
8650 -------------------------
8652 procedure Traverse_Interfaces
8653 (Anc_Type : Entity_Id;
8654 Error : out Boolean)
8656 Intface_List : constant List_Id :=
8657 Abstract_Interface_List (Anc_Type);
8658 Intface : Node_Id;
8660 begin
8661 Error := False;
8663 if Is_Non_Empty_List (Intface_List) then
8664 Intface := First (Intface_List);
8665 while Present (Intface) loop
8667 -- Look for acceptable class-wide homonyms associated with
8668 -- the interface.
8670 Traverse_Homonyms (Etype (Intface), Error);
8672 if Error then
8673 return;
8674 end if;
8676 -- Continue the search by looking at each of the interface's
8677 -- associated interface ancestors.
8679 Traverse_Interfaces (Etype (Intface), Error);
8681 if Error then
8682 return;
8683 end if;
8685 Next (Intface);
8686 end loop;
8687 end if;
8688 end Traverse_Interfaces;
8690 -- Start of processing for Try_Class_Wide_Operation
8692 begin
8693 -- If we are searching only for conflicting class-wide subprograms
8694 -- then initialize directly Matching_Op with the target entity.
8696 if CW_Test_Only then
8697 Matching_Op := Entity (Selector_Name (N));
8698 end if;
8700 -- Loop through ancestor types (including interfaces), traversing
8701 -- the homonym chain of the subprogram, trying out those homonyms
8702 -- whose first formal has the class-wide type of the ancestor, or
8703 -- an anonymous access type designating the class-wide type.
8705 Anc_Type := Obj_Type;
8706 loop
8707 -- Look for a match among homonyms associated with the ancestor
8709 Traverse_Homonyms (Anc_Type, Error);
8711 if Error then
8712 return True;
8713 end if;
8715 -- Continue the search for matches among homonyms associated with
8716 -- any interfaces implemented by the ancestor.
8718 Traverse_Interfaces (Anc_Type, Error);
8720 if Error then
8721 return True;
8722 end if;
8724 exit when Etype (Anc_Type) = Anc_Type;
8725 Anc_Type := Etype (Anc_Type);
8726 end loop;
8728 if Present (Matching_Op) then
8729 Set_Etype (Call_Node, Etype (Matching_Op));
8730 end if;
8732 return Present (Matching_Op);
8733 end Try_Class_Wide_Operation;
8735 -----------------------------------
8736 -- Try_One_Prefix_Interpretation --
8737 -----------------------------------
8739 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
8741 -- If the interpretation does not have a valid candidate type,
8742 -- preserve current value of Obj_Type for subsequent errors.
8744 Prev_Obj_Type : constant Entity_Id := Obj_Type;
8746 begin
8747 Obj_Type := T;
8749 if Is_Access_Type (Obj_Type) then
8750 Obj_Type := Designated_Type (Obj_Type);
8751 end if;
8753 if Ekind (Obj_Type) = E_Private_Subtype then
8754 Obj_Type := Base_Type (Obj_Type);
8755 end if;
8757 if Is_Class_Wide_Type (Obj_Type) then
8758 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
8759 end if;
8761 -- The type may have be obtained through a limited_with clause,
8762 -- in which case the primitive operations are available on its
8763 -- non-limited view. If still incomplete, retrieve full view.
8765 if Ekind (Obj_Type) = E_Incomplete_Type
8766 and then From_Limited_With (Obj_Type)
8767 and then Has_Non_Limited_View (Obj_Type)
8768 then
8769 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
8770 end if;
8772 -- If the object is not tagged, or the type is still an incomplete
8773 -- type, this is not a prefixed call.
8775 if not Is_Tagged_Type (Obj_Type)
8776 or else Is_Incomplete_Type (Obj_Type)
8777 then
8779 -- Restore previous type if current one is not legal candidate
8781 Obj_Type := Prev_Obj_Type;
8782 return;
8783 end if;
8785 declare
8786 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
8787 CW_Result : Boolean;
8788 Prim_Result : Boolean;
8789 pragma Unreferenced (CW_Result);
8791 begin
8792 if not CW_Test_Only then
8793 Prim_Result :=
8794 Try_Primitive_Operation
8795 (Call_Node => New_Call_Node,
8796 Node_To_Replace => Node_To_Replace);
8797 end if;
8799 -- Check if there is a class-wide subprogram covering the
8800 -- primitive. This check must be done even if a candidate
8801 -- was found in order to report ambiguous calls.
8803 if not (Prim_Result) then
8804 CW_Result :=
8805 Try_Class_Wide_Operation
8806 (Call_Node => New_Call_Node,
8807 Node_To_Replace => Node_To_Replace);
8809 -- If we found a primitive we search for class-wide subprograms
8810 -- using a duplicate of the call node (done to avoid missing its
8811 -- decoration if there is no ambiguity).
8813 else
8814 CW_Result :=
8815 Try_Class_Wide_Operation
8816 (Call_Node => Dup_Call_Node,
8817 Node_To_Replace => Node_To_Replace);
8818 end if;
8819 end;
8820 end Try_One_Prefix_Interpretation;
8822 -----------------------------
8823 -- Try_Primitive_Operation --
8824 -----------------------------
8826 function Try_Primitive_Operation
8827 (Call_Node : Node_Id;
8828 Node_To_Replace : Node_Id) return Boolean
8830 Elmt : Elmt_Id;
8831 Prim_Op : Entity_Id;
8832 Matching_Op : Entity_Id := Empty;
8833 Prim_Op_Ref : Node_Id := Empty;
8835 Corr_Type : Entity_Id := Empty;
8836 -- If the prefix is a synchronized type, the controlling type of
8837 -- the primitive operation is the corresponding record type, else
8838 -- this is the object type itself.
8840 Success : Boolean := False;
8842 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
8843 -- For tagged types the candidate interpretations are found in
8844 -- the list of primitive operations of the type and its ancestors.
8845 -- For formal tagged types we have to find the operations declared
8846 -- in the same scope as the type (including in the generic formal
8847 -- part) because the type itself carries no primitive operations,
8848 -- except for formal derived types that inherit the operations of
8849 -- the parent and progenitors.
8851 -- If the context is a generic subprogram body, the generic formals
8852 -- are visible by name, but are not in the entity list of the
8853 -- subprogram because that list starts with the subprogram formals.
8854 -- We retrieve the candidate operations from the generic declaration.
8856 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
8857 -- Prefix notation can also be used on operations that are not
8858 -- primitives of the type, but are declared in the same immediate
8859 -- declarative part, which can only mean the corresponding package
8860 -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the
8861 -- list of primitives with body operations with the same name that
8862 -- may be candidates, so that Try_Primitive_Operations can examine
8863 -- them if no real primitive is found.
8865 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
8866 -- An operation that overrides an inherited operation in the private
8867 -- part of its package may be hidden, but if the inherited operation
8868 -- is visible a direct call to it will dispatch to the private one,
8869 -- which is therefore a valid candidate.
8871 function Names_Match
8872 (Obj_Type : Entity_Id;
8873 Prim_Op : Entity_Id;
8874 Subprog : Entity_Id) return Boolean;
8875 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
8876 -- is a protected type then compare also the original name of Prim_Op
8877 -- with the name of Subprog (since the expander may have added a
8878 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
8880 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
8881 -- Verify that the prefix, dereferenced if need be, is a valid
8882 -- controlling argument in a call to Op. The remaining actuals
8883 -- are checked in the subsequent call to Analyze_One_Call.
8885 ------------------------------
8886 -- Collect_Generic_Type_Ops --
8887 ------------------------------
8889 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
8890 Bas : constant Entity_Id := Base_Type (T);
8891 Candidates : constant Elist_Id := New_Elmt_List;
8892 Subp : Entity_Id;
8893 Formal : Entity_Id;
8895 procedure Check_Candidate;
8896 -- The operation is a candidate if its first parameter is a
8897 -- controlling operand of the desired type.
8899 -----------------------
8900 -- Check_Candidate; --
8901 -----------------------
8903 procedure Check_Candidate is
8904 begin
8905 Formal := First_Formal (Subp);
8907 if Present (Formal)
8908 and then Is_Controlling_Formal (Formal)
8909 and then
8910 (Base_Type (Etype (Formal)) = Bas
8911 or else
8912 (Is_Access_Type (Etype (Formal))
8913 and then Designated_Type (Etype (Formal)) = Bas))
8914 then
8915 Append_Elmt (Subp, Candidates);
8916 end if;
8917 end Check_Candidate;
8919 -- Start of processing for Collect_Generic_Type_Ops
8921 begin
8922 if Is_Derived_Type (T) then
8923 return Primitive_Operations (T);
8925 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
8927 -- Scan the list of generic formals to find subprograms
8928 -- that may have a first controlling formal of the type.
8930 if Nkind (Unit_Declaration_Node (Scope (T))) =
8931 N_Generic_Subprogram_Declaration
8932 then
8933 declare
8934 Decl : Node_Id;
8936 begin
8937 Decl :=
8938 First (Generic_Formal_Declarations
8939 (Unit_Declaration_Node (Scope (T))));
8940 while Present (Decl) loop
8941 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
8942 Subp := Defining_Entity (Decl);
8943 Check_Candidate;
8944 end if;
8946 Next (Decl);
8947 end loop;
8948 end;
8949 end if;
8950 return Candidates;
8952 else
8953 -- Scan the list of entities declared in the same scope as
8954 -- the type. In general this will be an open scope, given that
8955 -- the call we are analyzing can only appear within a generic
8956 -- declaration or body (either the one that declares T, or a
8957 -- child unit).
8959 -- For a subtype representing a generic actual type, go to the
8960 -- base type.
8962 if Is_Generic_Actual_Type (T) then
8963 Subp := First_Entity (Scope (Base_Type (T)));
8964 else
8965 Subp := First_Entity (Scope (T));
8966 end if;
8968 while Present (Subp) loop
8969 if Is_Overloadable (Subp) then
8970 Check_Candidate;
8971 end if;
8973 Next_Entity (Subp);
8974 end loop;
8976 return Candidates;
8977 end if;
8978 end Collect_Generic_Type_Ops;
8980 ----------------------------
8981 -- Extended_Primitive_Ops --
8982 ----------------------------
8984 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
8985 Type_Scope : constant Entity_Id := Scope (T);
8987 Body_Decls : List_Id;
8988 Op_Found : Boolean;
8989 Op : Entity_Id;
8990 Op_List : Elist_Id;
8992 begin
8993 Op_List := Primitive_Operations (T);
8995 if Ekind (Type_Scope) = E_Package
8996 and then In_Package_Body (Type_Scope)
8997 and then In_Open_Scopes (Type_Scope)
8998 then
8999 -- Retrieve list of declarations of package body.
9001 Body_Decls :=
9002 Declarations
9003 (Unit_Declaration_Node
9004 (Corresponding_Body
9005 (Unit_Declaration_Node (Type_Scope))));
9007 Op := Current_Entity (Subprog);
9008 Op_Found := False;
9009 while Present (Op) loop
9010 if Comes_From_Source (Op)
9011 and then Is_Overloadable (Op)
9013 -- Exclude overriding primitive operations of a type
9014 -- extension declared in the package body, to prevent
9015 -- duplicates in extended list.
9017 and then not Is_Primitive (Op)
9018 and then Is_List_Member (Unit_Declaration_Node (Op))
9019 and then List_Containing (Unit_Declaration_Node (Op)) =
9020 Body_Decls
9021 then
9022 if not Op_Found then
9024 -- Copy list of primitives so it is not affected for
9025 -- other uses.
9027 Op_List := New_Copy_Elist (Op_List);
9028 Op_Found := True;
9029 end if;
9031 Append_Elmt (Op, Op_List);
9032 end if;
9034 Op := Homonym (Op);
9035 end loop;
9036 end if;
9038 return Op_List;
9039 end Extended_Primitive_Ops;
9041 ---------------------------
9042 -- Is_Private_Overriding --
9043 ---------------------------
9045 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
9046 Visible_Op : constant Entity_Id := Homonym (Op);
9048 begin
9049 return Present (Visible_Op)
9050 and then Scope (Op) = Scope (Visible_Op)
9051 and then not Comes_From_Source (Visible_Op)
9052 and then Alias (Visible_Op) = Op
9053 and then not Is_Hidden (Visible_Op);
9054 end Is_Private_Overriding;
9056 -----------------
9057 -- Names_Match --
9058 -----------------
9060 function Names_Match
9061 (Obj_Type : Entity_Id;
9062 Prim_Op : Entity_Id;
9063 Subprog : Entity_Id) return Boolean is
9064 begin
9065 -- Common case: exact match
9067 if Chars (Prim_Op) = Chars (Subprog) then
9068 return True;
9070 -- For protected type primitives the expander may have built the
9071 -- name of the dispatching primitive prepending the type name to
9072 -- avoid conflicts with the name of the protected subprogram (see
9073 -- Exp_Ch9.Build_Selected_Name).
9075 elsif Is_Protected_Type (Obj_Type) then
9076 return
9077 Present (Original_Protected_Subprogram (Prim_Op))
9078 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9079 Chars (Subprog);
9080 end if;
9082 return False;
9083 end Names_Match;
9085 -----------------------------
9086 -- Valid_First_Argument_Of --
9087 -----------------------------
9089 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9090 Typ : Entity_Id := Etype (First_Formal (Op));
9092 begin
9093 if Is_Concurrent_Type (Typ)
9094 and then Present (Corresponding_Record_Type (Typ))
9095 then
9096 Typ := Corresponding_Record_Type (Typ);
9097 end if;
9099 -- Simple case. Object may be a subtype of the tagged type or
9100 -- may be the corresponding record of a synchronized type.
9102 return Obj_Type = Typ
9103 or else Base_Type (Obj_Type) = Typ
9104 or else Corr_Type = Typ
9106 -- Prefix can be dereferenced
9108 or else
9109 (Is_Access_Type (Corr_Type)
9110 and then Designated_Type (Corr_Type) = Typ)
9112 -- Formal is an access parameter, for which the object
9113 -- can provide an access.
9115 or else
9116 (Ekind (Typ) = E_Anonymous_Access_Type
9117 and then
9118 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
9119 end Valid_First_Argument_Of;
9121 -- Start of processing for Try_Primitive_Operation
9123 begin
9124 -- Look for subprograms in the list of primitive operations. The name
9125 -- must be identical, and the kind of call indicates the expected
9126 -- kind of operation (function or procedure). If the type is a
9127 -- (tagged) synchronized type, the primitive ops are attached to the
9128 -- corresponding record (base) type.
9130 if Is_Concurrent_Type (Obj_Type) then
9131 if Present (Corresponding_Record_Type (Obj_Type)) then
9132 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
9133 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
9134 else
9135 Corr_Type := Obj_Type;
9136 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9137 end if;
9139 elsif not Is_Generic_Type (Obj_Type) then
9140 Corr_Type := Obj_Type;
9141 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
9143 else
9144 Corr_Type := Obj_Type;
9145 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9146 end if;
9148 while Present (Elmt) loop
9149 Prim_Op := Node (Elmt);
9151 if Names_Match (Obj_Type, Prim_Op, Subprog)
9152 and then Present (First_Formal (Prim_Op))
9153 and then Valid_First_Argument_Of (Prim_Op)
9154 and then
9155 (Nkind (Call_Node) = N_Function_Call)
9157 (Ekind (Prim_Op) = E_Function)
9158 then
9159 -- Ada 2005 (AI-251): If this primitive operation corresponds
9160 -- to an immediate ancestor interface there is no need to add
9161 -- it to the list of interpretations; the corresponding aliased
9162 -- primitive is also in this list of primitive operations and
9163 -- will be used instead.
9165 if (Present (Interface_Alias (Prim_Op))
9166 and then Is_Ancestor (Find_Dispatching_Type
9167 (Alias (Prim_Op)), Corr_Type))
9169 -- Do not consider hidden primitives unless the type is in an
9170 -- open scope or we are within an instance, where visibility
9171 -- is known to be correct, or else if this is an overriding
9172 -- operation in the private part for an inherited operation.
9174 or else (Is_Hidden (Prim_Op)
9175 and then not Is_Immediately_Visible (Obj_Type)
9176 and then not In_Instance
9177 and then not Is_Private_Overriding (Prim_Op))
9178 then
9179 goto Continue;
9180 end if;
9182 Set_Etype (Call_Node, Any_Type);
9183 Set_Is_Overloaded (Call_Node, False);
9185 if No (Matching_Op) then
9186 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
9187 Candidate := Prim_Op;
9189 Set_Parent (Call_Node, Parent (Node_To_Replace));
9191 Set_Name (Call_Node, Prim_Op_Ref);
9192 Success := False;
9194 Analyze_One_Call
9195 (N => Call_Node,
9196 Nam => Prim_Op,
9197 Report => Report_Error,
9198 Success => Success,
9199 Skip_First => True);
9201 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
9203 -- More than one interpretation, collect for subsequent
9204 -- disambiguation. If this is a procedure call and there
9205 -- is another match, report ambiguity now.
9207 else
9208 Analyze_One_Call
9209 (N => Call_Node,
9210 Nam => Prim_Op,
9211 Report => Report_Error,
9212 Success => Success,
9213 Skip_First => True);
9215 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9216 and then Nkind (Call_Node) /= N_Function_Call
9217 then
9218 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
9219 Report_Ambiguity (Matching_Op);
9220 Report_Ambiguity (Prim_Op);
9221 return True;
9222 end if;
9223 end if;
9224 end if;
9226 <<Continue>>
9227 Next_Elmt (Elmt);
9228 end loop;
9230 if Present (Matching_Op) then
9231 Set_Etype (Call_Node, Etype (Matching_Op));
9232 end if;
9234 return Present (Matching_Op);
9235 end Try_Primitive_Operation;
9237 -- Start of processing for Try_Object_Operation
9239 begin
9240 Analyze_Expression (Obj);
9242 -- Analyze the actuals if node is known to be a subprogram call
9244 if Is_Subprg_Call and then N = Name (Parent (N)) then
9245 Actual := First (Parameter_Associations (Parent (N)));
9246 while Present (Actual) loop
9247 Analyze_Expression (Actual);
9248 Next (Actual);
9249 end loop;
9250 end if;
9252 -- Build a subprogram call node, using a copy of Obj as its first
9253 -- actual. This is a placeholder, to be replaced by an explicit
9254 -- dereference when needed.
9256 Transform_Object_Operation
9257 (Call_Node => New_Call_Node,
9258 Node_To_Replace => Node_To_Replace);
9260 Set_Etype (New_Call_Node, Any_Type);
9261 Set_Etype (Subprog, Any_Type);
9262 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
9264 if not Is_Overloaded (Obj) then
9265 Try_One_Prefix_Interpretation (Obj_Type);
9267 else
9268 declare
9269 I : Interp_Index;
9270 It : Interp;
9271 begin
9272 Get_First_Interp (Obj, I, It);
9273 while Present (It.Nam) loop
9274 Try_One_Prefix_Interpretation (It.Typ);
9275 Get_Next_Interp (I, It);
9276 end loop;
9277 end;
9278 end if;
9280 if Etype (New_Call_Node) /= Any_Type then
9282 -- No need to complete the tree transformations if we are only
9283 -- searching for conflicting class-wide subprograms
9285 if CW_Test_Only then
9286 return False;
9287 else
9288 Complete_Object_Operation
9289 (Call_Node => New_Call_Node,
9290 Node_To_Replace => Node_To_Replace);
9291 return True;
9292 end if;
9294 elsif Present (Candidate) then
9296 -- The argument list is not type correct. Re-analyze with error
9297 -- reporting enabled, and use one of the possible candidates.
9298 -- In All_Errors_Mode, re-analyze all failed interpretations.
9300 if All_Errors_Mode then
9301 Report_Error := True;
9302 if Try_Primitive_Operation
9303 (Call_Node => New_Call_Node,
9304 Node_To_Replace => Node_To_Replace)
9306 or else
9307 Try_Class_Wide_Operation
9308 (Call_Node => New_Call_Node,
9309 Node_To_Replace => Node_To_Replace)
9310 then
9311 null;
9312 end if;
9314 else
9315 Analyze_One_Call
9316 (N => New_Call_Node,
9317 Nam => Candidate,
9318 Report => True,
9319 Success => Success,
9320 Skip_First => True);
9321 end if;
9323 -- No need for further errors
9325 return True;
9327 else
9328 -- There was no candidate operation, so report it as an error
9329 -- in the caller: Analyze_Selected_Component.
9331 return False;
9332 end if;
9333 end Try_Object_Operation;
9335 ---------
9336 -- wpo --
9337 ---------
9339 procedure wpo (T : Entity_Id) is
9340 Op : Entity_Id;
9341 E : Elmt_Id;
9343 begin
9344 if not Is_Tagged_Type (T) then
9345 return;
9346 end if;
9348 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9349 while Present (E) loop
9350 Op := Node (E);
9351 Write_Int (Int (Op));
9352 Write_Str (" === ");
9353 Write_Name (Chars (Op));
9354 Write_Str (" in ");
9355 Write_Name (Chars (Scope (Op)));
9356 Next_Elmt (E);
9357 Write_Eol;
9358 end loop;
9359 end wpo;
9361 end Sem_Ch4;